NetworkPriority.cpp 3.1 KB

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