CrowdAgent.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #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. NAVIGATIONPUSHINESS_NONE
  61. };
  62. /// 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.
  63. class ATOMIC_API CrowdAgent : public Component
  64. {
  65. ATOMIC_OBJECT(CrowdAgent, Component);
  66. friend class CrowdManager;
  67. friend void CrowdAgentUpdateCallback(dtCrowdAgent* ag, float dt);
  68. public:
  69. /// Construct.
  70. CrowdAgent(Context* context);
  71. /// Destruct.
  72. virtual ~CrowdAgent();
  73. /// Register object factory.
  74. static void RegisterObject(Context* context);
  75. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  76. virtual void ApplyAttributes();
  77. /// Handle enabled/disabled state change.
  78. virtual void OnSetEnabled();
  79. /// Draw debug geometry.
  80. void DrawDebugGeometry(bool depthTest);
  81. /// Draw debug feelers.
  82. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  83. /// Submit a new target position request for this agent.
  84. void SetTargetPosition(const Vector3& position);
  85. /// Submit a new target velocity request for this agent.
  86. void SetTargetVelocity(const Vector3& velocity);
  87. /// Reset any target request for the specified agent. Note that the agent will continue to move into the current direction; set a zero target velocity to actually stop.
  88. void ResetTarget();
  89. /// 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.
  90. void SetUpdateNodePosition(bool unodepos);
  91. /// Set the agent's max acceleration.
  92. void SetMaxAccel(float maxAccel);
  93. /// Set the agent's max velocity.
  94. void SetMaxSpeed(float maxSpeed);
  95. /// Set the agent's radius.
  96. void SetRadius(float radius);
  97. /// Set the agent's height.
  98. void SetHeight(float height);
  99. /// Set the agent's query filter type.
  100. void SetQueryFilterType(unsigned queryFilterType);
  101. /// Set the agent's obstacle avoidance type.
  102. void SetObstacleAvoidanceType(unsigned obstacleAvoidanceType);
  103. /// Set the agent's navigation quality.
  104. void SetNavigationQuality(NavigationQuality val);
  105. /// Set the agent's navigation pushiness.
  106. void SetNavigationPushiness(NavigationPushiness val);
  107. /// Return the agent's position.
  108. Vector3 GetPosition() const;
  109. /// Return the agent's desired velocity.
  110. Vector3 GetDesiredVelocity() const;
  111. /// Return the agent's actual velocity.
  112. Vector3 GetActualVelocity() const;
  113. /// Return the agent's requested target position.
  114. const Vector3& GetTargetPosition() const { return targetPosition_; }
  115. /// Return the agent's requested target velocity.
  116. const Vector3& GetTargetVelocity() const { return targetVelocity_; }
  117. /// Return the agent's requested target type, if any.
  118. CrowdAgentRequestedTarget GetRequestedTargetType() const { return requestedTargetType_; }
  119. /// Return the agent's state.
  120. CrowdAgentState GetAgentState() const;
  121. /// Return the agent's target state.
  122. CrowdAgentTargetState GetTargetState() const;
  123. /// Return true when the node's position should be updated by the CrowdManager.
  124. bool GetUpdateNodePosition() const { return updateNodePosition_; }
  125. /// Return the agent id.
  126. int GetAgentCrowdId() const { return agentCrowdId_; }
  127. /// Get the agent's max acceleration.
  128. float GetMaxAccel() const { return maxAccel_; }
  129. /// Get the agent's max velocity.
  130. float GetMaxSpeed() const { return maxSpeed_; }
  131. /// Get the agent's radius.
  132. float GetRadius() const { return radius_; }
  133. /// Get the agent's height.
  134. float GetHeight() const { return height_; }
  135. /// Get the agent's query filter type.
  136. unsigned GetQueryFilterType() const { return queryFilterType_; }
  137. /// Get the agent's obstacle avoidance type.
  138. unsigned GetObstacleAvoidanceType() const { return obstacleAvoidanceType_; }
  139. /// Get the agent's navigation quality.
  140. NavigationQuality GetNavigationQuality() const { return navQuality_; }
  141. /// Get the agent's navigation pushiness.
  142. NavigationPushiness GetNavigationPushiness() const { return navPushiness_; }
  143. /// Return true when the agent has a target.
  144. bool HasRequestedTarget() const { return requestedTargetType_ != CA_REQUESTEDTARGET_NONE; }
  145. /// Return true when the agent has arrived at its target.
  146. bool HasArrived() const;
  147. /// Return true when the agent is in crowd (being managed by a crowd manager).
  148. bool IsInCrowd() const;
  149. protected:
  150. /// Handle crowd agent being updated. It is called by CrowdManager::Update() via callback.
  151. virtual void OnCrowdUpdate(dtCrowdAgent* ag, float dt);
  152. /// Handle node being assigned.
  153. virtual void OnNodeSet(Node* node);
  154. /// Handle node being assigned.
  155. virtual void OnSceneSet(Scene* scene);
  156. /// \todo Handle node transform being dirtied.
  157. virtual void OnMarkedDirty(Node* node);
  158. /// Get internal Detour crowd agent.
  159. const dtCrowdAgent* GetDetourCrowdAgent() const;
  160. /// Handle navigation mesh tile added.
  161. void HandleNavigationTileAdded(StringHash eventType, VariantMap& eventData);
  162. private:
  163. /// Update Detour crowd agent parameter.
  164. void UpdateParameters(unsigned scope = M_MAX_UNSIGNED);
  165. /// Add agent into crowd.
  166. int AddAgentToCrowd(bool force = false);
  167. /// Remove agent from crowd.
  168. void RemoveAgentFromCrowd();
  169. /// Crowd manager.
  170. WeakPtr<CrowdManager> crowdManager_;
  171. /// Crowd manager reference to this agent.
  172. int agentCrowdId_;
  173. /// Requested target position.
  174. Vector3 targetPosition_;
  175. /// Requested target velocity.
  176. Vector3 targetVelocity_;
  177. /// Requested target type.
  178. CrowdAgentRequestedTarget requestedTargetType_;
  179. /// Flag indicating the node's position should be updated by Detour crowd manager.
  180. bool updateNodePosition_;
  181. /// Agent's max acceleration.
  182. float maxAccel_;
  183. /// Agent's max Velocity.
  184. float maxSpeed_;
  185. /// Agent's radius, if 0 the navigation mesh's setting will be used.
  186. float radius_;
  187. /// Agent's height, if 0 the navigation mesh's setting will be used.
  188. float height_;
  189. /// Agent's query filter type, it is an index to the query filter buffer configured in Detour crowd manager.
  190. unsigned queryFilterType_;
  191. /// 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".
  192. unsigned obstacleAvoidanceType_;
  193. /// Agent's navigation quality. The higher the setting, the higher the CPU usage during crowd simulation.
  194. NavigationQuality navQuality_;
  195. /// Agent's navigation pushiness. The higher the setting, the stronger the agent pushes its colliding neighbours around.
  196. NavigationPushiness navPushiness_;
  197. /// Agent's previous position used to check for position changes.
  198. Vector3 previousPosition_;
  199. /// Agent's previous target state used to check for state changes.
  200. CrowdAgentTargetState previousTargetState_;
  201. /// Agent's previous agent state used to check for state changes.
  202. CrowdAgentState previousAgentState_;
  203. /// Internal flag to ignore transform changes because it came from us, used in OnCrowdAgentReposition().
  204. bool ignoreTransformChanges_;
  205. };
  206. }