NetworkPriority.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #include "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Network/NetworkPriority.h"
  25. #include "../DebugNew.h"
  26. namespace Atomic
  27. {
  28. const char* NETWORK_CATEGORY = "Network";
  29. static const float DEFAULT_BASE_PRIORITY = 100.0f;
  30. static const float DEFAULT_DISTANCE_FACTOR = 0.0f;
  31. static const float DEFAULT_MIN_PRIORITY = 0.0f;
  32. static const float UPDATE_THRESHOLD = 100.0f;
  33. NetworkPriority::NetworkPriority(Context* context) :
  34. Component(context),
  35. basePriority_(DEFAULT_BASE_PRIORITY),
  36. distanceFactor_(DEFAULT_DISTANCE_FACTOR),
  37. minPriority_(DEFAULT_MIN_PRIORITY),
  38. alwaysUpdateOwner_(true)
  39. {
  40. }
  41. NetworkPriority::~NetworkPriority()
  42. {
  43. }
  44. void NetworkPriority::RegisterObject(Context* context)
  45. {
  46. context->RegisterFactory<NetworkPriority>(NETWORK_CATEGORY);
  47. ATOMIC_ATTRIBUTE("Base Priority", float, basePriority_, DEFAULT_BASE_PRIORITY, AM_DEFAULT);
  48. ATOMIC_ATTRIBUTE("Distance Factor", float, distanceFactor_, DEFAULT_DISTANCE_FACTOR, AM_DEFAULT);
  49. ATOMIC_ATTRIBUTE("Minimum Priority", float, minPriority_, DEFAULT_MIN_PRIORITY, AM_DEFAULT);
  50. ATOMIC_ATTRIBUTE("Always Update Owner", bool, alwaysUpdateOwner_, true, AM_DEFAULT);
  51. }
  52. void NetworkPriority::SetBasePriority(float priority)
  53. {
  54. basePriority_ = Max(priority, 0.0f);
  55. MarkNetworkUpdate();
  56. }
  57. void NetworkPriority::SetDistanceFactor(float factor)
  58. {
  59. distanceFactor_ = Max(factor, 0.0f);
  60. MarkNetworkUpdate();
  61. }
  62. void NetworkPriority::SetMinPriority(float priority)
  63. {
  64. minPriority_ = Max(priority, 0.0f);
  65. MarkNetworkUpdate();
  66. }
  67. void NetworkPriority::SetAlwaysUpdateOwner(bool enable)
  68. {
  69. alwaysUpdateOwner_ = enable;
  70. MarkNetworkUpdate();
  71. }
  72. bool NetworkPriority::CheckUpdate(float distance, float& accumulator)
  73. {
  74. float currentPriority = Max(basePriority_ - distanceFactor_ * distance, minPriority_);
  75. accumulator += currentPriority;
  76. if (accumulator >= UPDATE_THRESHOLD)
  77. {
  78. accumulator = fmodf(accumulator, UPDATE_THRESHOLD);
  79. return true;
  80. }
  81. else
  82. return false;
  83. }
  84. }