1 //
2 // Copyright (c) 2008-2017 the Urho3D project.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 //
22 
23 #include "../Precompiled.h"
24 
25 #include "../Core/Context.h"
26 #include "../Network/NetworkPriority.h"
27 
28 #include "../DebugNew.h"
29 
30 namespace Urho3D
31 {
32 
33 const char* NETWORK_CATEGORY = "Network";
34 
35 static const float DEFAULT_BASE_PRIORITY = 100.0f;
36 static const float DEFAULT_DISTANCE_FACTOR = 0.0f;
37 static const float DEFAULT_MIN_PRIORITY = 0.0f;
38 static const float UPDATE_THRESHOLD = 100.0f;
39 
NetworkPriority(Context * context)40 NetworkPriority::NetworkPriority(Context* context) :
41     Component(context),
42     basePriority_(DEFAULT_BASE_PRIORITY),
43     distanceFactor_(DEFAULT_DISTANCE_FACTOR),
44     minPriority_(DEFAULT_MIN_PRIORITY),
45     alwaysUpdateOwner_(true)
46 {
47 }
48 
~NetworkPriority()49 NetworkPriority::~NetworkPriority()
50 {
51 }
52 
RegisterObject(Context * context)53 void NetworkPriority::RegisterObject(Context* context)
54 {
55     context->RegisterFactory<NetworkPriority>(NETWORK_CATEGORY);
56 
57     URHO3D_ATTRIBUTE("Base Priority", float, basePriority_, DEFAULT_BASE_PRIORITY, AM_DEFAULT);
58     URHO3D_ATTRIBUTE("Distance Factor", float, distanceFactor_, DEFAULT_DISTANCE_FACTOR, AM_DEFAULT);
59     URHO3D_ATTRIBUTE("Minimum Priority", float, minPriority_, DEFAULT_MIN_PRIORITY, AM_DEFAULT);
60     URHO3D_ATTRIBUTE("Always Update Owner", bool, alwaysUpdateOwner_, true, AM_DEFAULT);
61 }
62 
SetBasePriority(float priority)63 void NetworkPriority::SetBasePriority(float priority)
64 {
65     basePriority_ = Max(priority, 0.0f);
66     MarkNetworkUpdate();
67 }
68 
SetDistanceFactor(float factor)69 void NetworkPriority::SetDistanceFactor(float factor)
70 {
71     distanceFactor_ = Max(factor, 0.0f);
72     MarkNetworkUpdate();
73 }
74 
SetMinPriority(float priority)75 void NetworkPriority::SetMinPriority(float priority)
76 {
77     minPriority_ = Max(priority, 0.0f);
78     MarkNetworkUpdate();
79 }
80 
SetAlwaysUpdateOwner(bool enable)81 void NetworkPriority::SetAlwaysUpdateOwner(bool enable)
82 {
83     alwaysUpdateOwner_ = enable;
84     MarkNetworkUpdate();
85 }
86 
CheckUpdate(float distance,float & accumulator)87 bool NetworkPriority::CheckUpdate(float distance, float& accumulator)
88 {
89     float currentPriority = Max(basePriority_ - distanceFactor_ * distance, minPriority_);
90     accumulator += currentPriority;
91     if (accumulator >= UPDATE_THRESHOLD)
92     {
93         accumulator = fmodf(accumulator, UPDATE_THRESHOLD);
94         return true;
95     }
96     else
97         return false;
98 }
99 
100 }
101