CrowdAgent.h 9.9 KB

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