CrowdAgent.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // Copyright (c) 2008-2015 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. #pragma once
  23. #include "../Navigation/CrowdManager.h"
  24. #include "../Scene/Component.h"
  25. namespace Atomic
  26. {
  27. enum CrowdAgentRequestedTarget
  28. {
  29. CA_REQUESTEDTARGET_NONE = 0,
  30. CA_REQUESTEDTARGET_POSITION,
  31. CA_REQUESTEDTARGET_VELOCITY
  32. };
  33. enum CrowdAgentTargetState
  34. {
  35. CA_TARGET_NONE = 0,
  36. CA_TARGET_FAILED,
  37. CA_TARGET_VALID,
  38. CA_TARGET_REQUESTING,
  39. CA_TARGET_WAITINGFORQUEUE,
  40. CA_TARGET_WAITINGFORPATH,
  41. CA_TARGET_VELOCITY
  42. };
  43. enum CrowdAgentState
  44. {
  45. CA_STATE_INVALID = 0, ///< The agent is not in a valid state.
  46. CA_STATE_WALKING, ///< The agent is traversing a normal navigation mesh polygon.
  47. CA_STATE_OFFMESH ///< The agent is traversing an off-mesh connection.
  48. };
  49. enum NavigationQuality
  50. {
  51. NAVIGATIONQUALITY_LOW = 0,
  52. NAVIGATIONQUALITY_MEDIUM = 1,
  53. NAVIGATIONQUALITY_HIGH = 2
  54. };
  55. enum NavigationPushiness
  56. {
  57. NAVIGATIONPUSHINESS_LOW = 0,
  58. NAVIGATIONPUSHINESS_MEDIUM,
  59. NAVIGATIONPUSHINESS_HIGH
  60. };
  61. /// Crowd agent component, requires a CrowdManager component in the scene. When not set explicitly, agent's radius and height are defaulted to navigation mesh's agent radius and height, respectively.
  62. class ATOMIC_API CrowdAgent : public Component
  63. {
  64. OBJECT(CrowdAgent);
  65. friend class CrowdManager;
  66. friend void CrowdAgentUpdateCallback(dtCrowdAgent* ag, float dt);
  67. public:
  68. /// Construct.
  69. CrowdAgent(Context* context);
  70. /// Destruct.
  71. virtual ~CrowdAgent();
  72. /// Register object factory.
  73. static void RegisterObject(Context* context);
  74. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  75. virtual void ApplyAttributes();
  76. /// Handle enabled/disabled state change.
  77. virtual void OnSetEnabled();
  78. /// Draw debug geometry.
  79. void DrawDebugGeometry(bool);
  80. /// Draw debug feelers.
  81. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  82. /// Submit a new target position request for this agent.
  83. void SetTargetPosition(const Vector3& position);
  84. /// Submit a new target velocity request for this agent.
  85. void SetTargetVelocity(const Vector3& velocity);
  86. /// Reset any target request for the specified agent.
  87. void ResetTarget();
  88. /// Update the node position. When set to false, the node position should be updated by other means (e.g. using Physics) in response to the E_CROWD_AGENT_REPOSITION event.
  89. void SetUpdateNodePosition(bool unodepos);
  90. /// Set the agent's max acceleration.
  91. void SetMaxAccel(float maxAccel);
  92. /// Set the agent's max velocity.
  93. void SetMaxSpeed(float maxSpeed);
  94. /// Set the agent's radius.
  95. void SetRadius(float radius);
  96. /// Set the agent's height.
  97. void SetHeight(float height);
  98. /// Set the agent's query filter type.
  99. void SetQueryFilterType(unsigned queryFilterType);
  100. /// Set the agent's obstacle avoidance type.
  101. void SetObstacleAvoidanceType(unsigned obstacleAvoidanceType);
  102. /// Set the agent's navigation quality.
  103. void SetNavigationQuality(NavigationQuality val);
  104. /// Set the agent's navigation pushiness.
  105. void SetNavigationPushiness(NavigationPushiness val);
  106. /// Return the agent's position.
  107. Vector3 GetPosition() const;
  108. /// Return the agent's desired velocity.
  109. Vector3 GetDesiredVelocity() const;
  110. /// Return the agent's actual velocity.
  111. Vector3 GetActualVelocity() const;
  112. /// Return the agent's requested target position.
  113. const Vector3& GetTargetPosition() const { return targetPosition_; }
  114. /// Return the agent's requested target velocity.
  115. const Vector3& GetTargetVelocity() const { return targetVelocity_; }
  116. /// Return the agent's requested target type, if any.
  117. CrowdAgentRequestedTarget GetRequestedTargetType() const { return requestedTargetType_; }
  118. /// Return the agent's state.
  119. CrowdAgentState GetAgentState() const;
  120. /// Return the agent's target state.
  121. CrowdAgentTargetState GetTargetState() const;
  122. /// Return true when the node's position should be updated by the CrowdManager.
  123. bool GetUpdateNodePosition() const { return updateNodePosition_; }
  124. /// Return the agent id.
  125. int GetAgentCrowdId() const { return agentCrowdId_; }
  126. /// Get the agent's max acceleration.
  127. float GetMaxAccel() const { return maxAccel_; }
  128. /// Get the agent's max velocity.
  129. float GetMaxSpeed() const { return maxSpeed_; }
  130. /// Get the agent's radius.
  131. float GetRadius() const { return radius_; }
  132. /// Get the agent's height.
  133. float GetHeight() const { return height_; }
  134. /// Get the agent's query filter type.
  135. unsigned GetQueryFilterType() const { return queryFilterType_; }
  136. /// Get the agent's obstacle avoidance type.
  137. unsigned GetObstacleAvoidanceType() const { return obstacleAvoidanceType_; }
  138. /// Get the agent's navigation quality.
  139. NavigationQuality GetNavigationQuality() const { return navQuality_; }
  140. /// Get the agent's navigation pushiness.
  141. NavigationPushiness GetNavigationPushiness() const { return navPushiness_; }
  142. /// Return true when the agent has a target.
  143. bool HasRequestedTarget() const { return requestedTargetType_ != CA_REQUESTEDTARGET_NONE; }
  144. /// Return true when the agent has arrived at its target.
  145. bool HasArrived() const;
  146. /// Return true when the agent is in crowd (being managed by a crowd manager).
  147. bool IsInCrowd() const;
  148. protected:
  149. /// Handle crowd agent being updated. It is called by CrowdManager::Update() via callback.
  150. virtual void OnCrowdUpdate(dtCrowdAgent* ag, float dt);
  151. /// Handle node being assigned.
  152. virtual void OnNodeSet(Node* node);
  153. /// Handle node being assigned.
  154. virtual void OnSceneSet(Scene* scene);
  155. /// \todo Handle node transform being dirtied.
  156. virtual void OnMarkedDirty(Node* node);
  157. /// Get internal Detour crowd agent.
  158. const dtCrowdAgent* GetDetourCrowdAgent() const;
  159. private:
  160. /// Update Detour crowd agent parameter.
  161. void UpdateParameters(unsigned scope = M_MAX_UNSIGNED);
  162. /// Add agent into crowd.
  163. int AddAgentToCrowd(bool force = false);
  164. /// Remove agent from crowd.
  165. void RemoveAgentFromCrowd();
  166. /// Crowd manager.
  167. WeakPtr<CrowdManager> crowdManager_;
  168. /// Crowd manager reference to this agent.
  169. int agentCrowdId_;
  170. /// Requested target position.
  171. Vector3 targetPosition_;
  172. /// Requested target velocity.
  173. Vector3 targetVelocity_;
  174. /// Requested target type.
  175. CrowdAgentRequestedTarget requestedTargetType_;
  176. /// Flag indicating the node's position should be updated by Detour crowd manager.
  177. bool updateNodePosition_;
  178. /// Agent's max acceleration.
  179. float maxAccel_;
  180. /// Agent's max Velocity.
  181. float maxSpeed_;
  182. /// Agent's radius, if 0 the navigation mesh's setting will be used.
  183. float radius_;
  184. /// Agent's height, if 0 the navigation mesh's setting will be used.
  185. float height_;
  186. /// Agent's query filter type, it is an index to the query filter buffer configured in Detour crowd manager.
  187. unsigned queryFilterType_;
  188. /// Agent's obstacle avoidance type, it is an index to the obstacle avoidance array configured in Detour crowd manager. It is ignored when agent's navigation quality is not set to "NAVIGATIONQUALITY_HIGH".
  189. unsigned obstacleAvoidanceType_;
  190. /// Agent's navigation quality. The higher the setting, the higher the CPU usage during crowd simulation.
  191. NavigationQuality navQuality_;
  192. /// Agent's navigation pushiness. The higher the setting, the stronger the agent pushes its colliding neighbours around.
  193. NavigationPushiness navPushiness_;
  194. /// Agent's previous position used to check for position changes.
  195. Vector3 previousPosition_;
  196. /// Agent's previous target state used to check for state changes.
  197. CrowdAgentTargetState previousTargetState_;
  198. /// Agent's previous agent state used to check for state changes.
  199. CrowdAgentState previousAgentState_;
  200. /// Internal flag to ignore transform changes because it came from us, used in OnCrowdAgentReposition().
  201. bool ignoreTransformChanges_;
  202. };
  203. }