CrowdAgent.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. #include "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Core/Profiler.h"
  25. #include "../Graphics/DebugRenderer.h"
  26. #include "../IO/Log.h"
  27. #include "../IO/MemoryBuffer.h"
  28. #include "../Navigation/NavigationEvents.h"
  29. #include "../Navigation/CrowdAgent.h"
  30. #include "../Scene/Node.h"
  31. #include "../Scene/Scene.h"
  32. #include <Detour/DetourCommon.h>
  33. #include <DetourCrowd/DetourCrowd.h>
  34. #include "../DebugNew.h"
  35. namespace Urho3D
  36. {
  37. extern const char* NAVIGATION_CATEGORY;
  38. static const CrowdAgentRequestedTarget DEFAULT_AGENT_REQUEST_TARGET_TYPE = CA_REQUESTEDTARGET_NONE;
  39. static const float DEFAULT_AGENT_MAX_SPEED = 0.f;
  40. static const float DEFAULT_AGENT_MAX_ACCEL = 0.f;
  41. static const unsigned DEFAULT_AGENT_QUERY_FILTER_TYPE = 0;
  42. static const unsigned DEFAULT_AGENT_OBSTACLE_AVOIDANCE_TYPE = 0;
  43. static const NavigationQuality DEFAULT_AGENT_AVOIDANCE_QUALITY = NAVIGATIONQUALITY_HIGH;
  44. static const NavigationPushiness DEFAULT_AGENT_NAVIGATION_PUSHINESS = NAVIGATIONPUSHINESS_MEDIUM;
  45. static const unsigned SCOPE_NAVIGATION_QUALITY_PARAMS = 1;
  46. static const unsigned SCOPE_NAVIGATION_PUSHINESS_PARAMS = 2;
  47. static const unsigned SCOPE_BASE_PARAMS = M_MAX_UNSIGNED & ~SCOPE_NAVIGATION_QUALITY_PARAMS & ~SCOPE_NAVIGATION_PUSHINESS_PARAMS;
  48. static const char* crowdAgentRequestedTargetTypeNames[] = {
  49. "none",
  50. "position",
  51. "velocity",
  52. 0
  53. };
  54. static const char* crowdAgentAvoidanceQualityNames[] = {
  55. "low",
  56. "medium",
  57. "high",
  58. 0
  59. };
  60. static const char* crowdAgentPushinessNames[] = {
  61. "low",
  62. "medium",
  63. "high",
  64. 0
  65. };
  66. CrowdAgent::CrowdAgent(Context* context) :
  67. Component(context),
  68. agentCrowdId_(-1),
  69. requestedTargetType_(DEFAULT_AGENT_REQUEST_TARGET_TYPE),
  70. updateNodePosition_(true),
  71. maxAccel_(DEFAULT_AGENT_MAX_ACCEL),
  72. maxSpeed_(DEFAULT_AGENT_MAX_SPEED),
  73. radius_(0.0f),
  74. height_(0.0f),
  75. queryFilterType_(DEFAULT_AGENT_QUERY_FILTER_TYPE),
  76. obstacleAvoidanceType_(DEFAULT_AGENT_OBSTACLE_AVOIDANCE_TYPE),
  77. navQuality_(DEFAULT_AGENT_AVOIDANCE_QUALITY),
  78. navPushiness_(DEFAULT_AGENT_NAVIGATION_PUSHINESS),
  79. previousTargetState_(CA_TARGET_NONE),
  80. previousAgentState_(CA_STATE_WALKING),
  81. ignoreTransformChanges_(false)
  82. {
  83. }
  84. CrowdAgent::~CrowdAgent()
  85. {
  86. RemoveAgentFromCrowd();
  87. }
  88. void CrowdAgent::RegisterObject(Context* context)
  89. {
  90. context->RegisterFactory<CrowdAgent>(NAVIGATION_CATEGORY);
  91. URHO3D_ATTRIBUTE("Target Position", Vector3, targetPosition_, Vector3::ZERO, AM_DEFAULT);
  92. URHO3D_ATTRIBUTE("Target Velocity", Vector3, targetVelocity_, Vector3::ZERO, AM_DEFAULT);
  93. URHO3D_ENUM_ATTRIBUTE("Requested Target Type", requestedTargetType_, crowdAgentRequestedTargetTypeNames,
  94. DEFAULT_AGENT_REQUEST_TARGET_TYPE, AM_DEFAULT);
  95. URHO3D_ACCESSOR_ATTRIBUTE("Update Node Position", GetUpdateNodePosition, SetUpdateNodePosition, bool, true, AM_DEFAULT);
  96. URHO3D_ATTRIBUTE("Max Accel", float, maxAccel_, DEFAULT_AGENT_MAX_ACCEL, AM_DEFAULT);
  97. URHO3D_ATTRIBUTE("Max Speed", float, maxSpeed_, DEFAULT_AGENT_MAX_SPEED, AM_DEFAULT);
  98. URHO3D_ATTRIBUTE("Radius", float, radius_, 0.0f, AM_DEFAULT);
  99. URHO3D_ATTRIBUTE("Height", float, height_, 0.0f, AM_DEFAULT);
  100. URHO3D_ATTRIBUTE("Query Filter Type", unsigned, queryFilterType_, DEFAULT_AGENT_QUERY_FILTER_TYPE, AM_DEFAULT);
  101. URHO3D_ATTRIBUTE("Obstacle Avoidance Type", unsigned, obstacleAvoidanceType_, DEFAULT_AGENT_OBSTACLE_AVOIDANCE_TYPE, AM_DEFAULT);
  102. URHO3D_ENUM_ATTRIBUTE("Navigation Pushiness", navPushiness_, crowdAgentPushinessNames, DEFAULT_AGENT_NAVIGATION_PUSHINESS, AM_DEFAULT);
  103. URHO3D_ENUM_ATTRIBUTE("Navigation Quality", navQuality_, crowdAgentAvoidanceQualityNames, DEFAULT_AGENT_AVOIDANCE_QUALITY, AM_DEFAULT);
  104. }
  105. void CrowdAgent::ApplyAttributes()
  106. {
  107. // Values from Editor, saved-file, or network must be checked before applying
  108. maxAccel_ = Max(0.f, maxAccel_);
  109. maxSpeed_ = Max(0.f, maxSpeed_);
  110. radius_ = Max(0.f, radius_);
  111. height_ = Max(0.f, height_);
  112. queryFilterType_ = Min(queryFilterType_, DT_CROWD_MAX_QUERY_FILTER_TYPE - 1);
  113. obstacleAvoidanceType_ = Min(obstacleAvoidanceType_, DT_CROWD_MAX_OBSTAVOIDANCE_PARAMS - 1);
  114. UpdateParameters();
  115. // Set or reset target after we have attributes applied to the agent's parameters.
  116. CrowdAgentRequestedTarget requestedTargetType = requestedTargetType_;
  117. if (CA_REQUESTEDTARGET_NONE != requestedTargetType_)
  118. {
  119. // Assign a dummy value such that the value check in the setter method passes
  120. requestedTargetType_ = CA_REQUESTEDTARGET_NONE;
  121. if (requestedTargetType == CA_REQUESTEDTARGET_POSITION)
  122. SetTargetPosition(targetPosition_);
  123. else
  124. SetTargetVelocity(targetVelocity_);
  125. }
  126. else
  127. {
  128. requestedTargetType_ = CA_REQUESTEDTARGET_POSITION;
  129. ResetTarget();
  130. }
  131. }
  132. void CrowdAgent::OnSetEnabled()
  133. {
  134. bool enabled = IsEnabledEffective();
  135. if (enabled && !IsInCrowd())
  136. AddAgentToCrowd();
  137. else if (!enabled && IsInCrowd())
  138. RemoveAgentFromCrowd();
  139. }
  140. void CrowdAgent::DrawDebugGeometry(bool depthTest)
  141. {
  142. Scene* scene = GetScene();
  143. if (scene)
  144. {
  145. DebugRenderer* debug = scene->GetComponent<DebugRenderer>();
  146. if (debug)
  147. DrawDebugGeometry(debug, depthTest);
  148. }
  149. }
  150. void CrowdAgent::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  151. {
  152. if (node_)
  153. {
  154. const Vector3 pos = GetPosition();
  155. const Vector3 vel = GetActualVelocity();
  156. const Vector3 desiredVel = GetDesiredVelocity();
  157. const Vector3 agentHeightVec(0, height_, 0);
  158. debug->AddLine(pos + 0.5f * agentHeightVec, pos + vel + 0.5f * agentHeightVec, Color::GREEN, depthTest);
  159. debug->AddLine(pos + 0.25f * agentHeightVec, pos + desiredVel + 0.25f * agentHeightVec, Color::RED, depthTest);
  160. debug->AddCylinder(pos, radius_, height_, HasArrived() ? Color::GREEN : Color::WHITE, depthTest);
  161. }
  162. }
  163. void CrowdAgent::UpdateParameters(unsigned scope)
  164. {
  165. const dtCrowdAgent* agent = GetDetourCrowdAgent();
  166. if (agent)
  167. {
  168. dtCrowdAgentParams params = agent->params;
  169. if (scope & SCOPE_NAVIGATION_QUALITY_PARAMS)
  170. {
  171. switch (navQuality_)
  172. {
  173. case NAVIGATIONQUALITY_LOW:
  174. params.updateFlags = 0
  175. | DT_CROWD_OPTIMIZE_VIS
  176. | DT_CROWD_ANTICIPATE_TURNS;
  177. break;
  178. case NAVIGATIONQUALITY_MEDIUM:
  179. params.updateFlags = 0
  180. | DT_CROWD_OPTIMIZE_TOPO
  181. | DT_CROWD_OPTIMIZE_VIS
  182. | DT_CROWD_ANTICIPATE_TURNS
  183. | DT_CROWD_SEPARATION;
  184. break;
  185. case NAVIGATIONQUALITY_HIGH:
  186. params.updateFlags = 0
  187. // Path finding
  188. | DT_CROWD_OPTIMIZE_TOPO
  189. | DT_CROWD_OPTIMIZE_VIS
  190. // Steering
  191. | DT_CROWD_ANTICIPATE_TURNS
  192. | DT_CROWD_SEPARATION
  193. // Velocity planning
  194. | DT_CROWD_OBSTACLE_AVOIDANCE;
  195. break;
  196. }
  197. }
  198. if (scope & SCOPE_NAVIGATION_PUSHINESS_PARAMS)
  199. {
  200. switch (navPushiness_)
  201. {
  202. case NAVIGATIONPUSHINESS_LOW:
  203. params.separationWeight = 4.0f;
  204. params.collisionQueryRange = radius_ * 16.0f;
  205. break;
  206. case NAVIGATIONPUSHINESS_MEDIUM:
  207. params.separationWeight = 2.0f;
  208. params.collisionQueryRange = radius_ * 8.0f;
  209. break;
  210. case NAVIGATIONPUSHINESS_HIGH:
  211. params.separationWeight = 0.5f;
  212. params.collisionQueryRange = radius_ * 1.0f;
  213. break;
  214. }
  215. }
  216. if (scope & SCOPE_BASE_PARAMS)
  217. {
  218. params.radius = radius_;
  219. params.height = height_;
  220. params.maxAcceleration = maxAccel_;
  221. params.maxSpeed = maxSpeed_;
  222. params.pathOptimizationRange = radius_ * 30.0f;
  223. params.queryFilterType = (unsigned char)queryFilterType_;
  224. params.obstacleAvoidanceType = (unsigned char)obstacleAvoidanceType_;
  225. }
  226. crowdManager_->GetCrowd()->updateAgentParameters(agentCrowdId_, &params);
  227. }
  228. }
  229. int CrowdAgent::AddAgentToCrowd(bool force)
  230. {
  231. if (!node_ || !crowdManager_ || !crowdManager_->crowd_)
  232. return -1;
  233. if (force || !IsInCrowd())
  234. {
  235. URHO3D_PROFILE(AddAgentToCrowd);
  236. agentCrowdId_ = crowdManager_->AddAgent(this, node_->GetPosition());
  237. if (agentCrowdId_ == -1)
  238. return -1;
  239. ApplyAttributes();
  240. previousAgentState_ = GetAgentState();
  241. previousTargetState_ = GetTargetState();
  242. // Agent created, but initial state is invalid and needs to be addressed
  243. if (previousAgentState_ == CA_STATE_INVALID)
  244. {
  245. using namespace CrowdAgentFailure;
  246. VariantMap& map = GetEventDataMap();
  247. map[P_NODE] = GetNode();
  248. map[P_CROWD_AGENT] = this;
  249. map[P_CROWD_TARGET_STATE] = previousTargetState_;
  250. map[P_CROWD_AGENT_STATE] = previousAgentState_;
  251. map[P_POSITION] = GetPosition();
  252. map[P_VELOCITY] = GetActualVelocity();
  253. crowdManager_->SendEvent(E_CROWD_AGENT_FAILURE, map);
  254. Node* node = GetNode();
  255. if (node)
  256. node->SendEvent(E_CROWD_AGENT_NODE_FAILURE, map);
  257. // Reevaluate states as handling of event may have resulted in changes
  258. previousAgentState_ = GetAgentState();
  259. previousTargetState_ = GetTargetState();
  260. }
  261. // Save the initial position to prevent CrowdAgentReposition event being triggered unnecessarily
  262. previousPosition_ = GetPosition();
  263. }
  264. return agentCrowdId_;
  265. }
  266. void CrowdAgent::RemoveAgentFromCrowd()
  267. {
  268. if (IsInCrowd())
  269. {
  270. crowdManager_->RemoveAgent(this);
  271. agentCrowdId_ = -1;
  272. }
  273. }
  274. void CrowdAgent::SetTargetPosition(const Vector3& position)
  275. {
  276. if (position != targetPosition_ || CA_REQUESTEDTARGET_POSITION != requestedTargetType_)
  277. {
  278. targetPosition_ = position;
  279. requestedTargetType_ = CA_REQUESTEDTARGET_POSITION;
  280. MarkNetworkUpdate();
  281. if (!IsInCrowd())
  282. AddAgentToCrowd();
  283. if (IsInCrowd()) // Make sure the previous method call is successful
  284. {
  285. dtPolyRef nearestRef;
  286. Vector3 nearestPos = crowdManager_->FindNearestPoint(position, queryFilterType_, &nearestRef);
  287. crowdManager_->GetCrowd()->requestMoveTarget(agentCrowdId_, nearestRef, nearestPos.Data());
  288. }
  289. }
  290. }
  291. void CrowdAgent::SetTargetVelocity(const Vector3& velocity)
  292. {
  293. if (velocity != targetVelocity_ || CA_REQUESTEDTARGET_VELOCITY != requestedTargetType_)
  294. {
  295. targetVelocity_ = velocity;
  296. requestedTargetType_ = CA_REQUESTEDTARGET_VELOCITY;
  297. MarkNetworkUpdate();
  298. if (IsInCrowd())
  299. crowdManager_->GetCrowd()->requestMoveVelocity(agentCrowdId_, velocity.Data());
  300. }
  301. }
  302. void CrowdAgent::ResetTarget()
  303. {
  304. if (CA_REQUESTEDTARGET_NONE != requestedTargetType_)
  305. {
  306. requestedTargetType_ = CA_REQUESTEDTARGET_NONE;
  307. MarkNetworkUpdate();
  308. if (IsInCrowd())
  309. crowdManager_->GetCrowd()->resetMoveTarget(agentCrowdId_);
  310. }
  311. }
  312. void CrowdAgent::SetUpdateNodePosition(bool unodepos)
  313. {
  314. if (unodepos != updateNodePosition_)
  315. {
  316. updateNodePosition_ = unodepos;
  317. MarkNetworkUpdate();
  318. }
  319. }
  320. void CrowdAgent::SetMaxAccel(float maxAccel)
  321. {
  322. if (maxAccel != maxAccel_ && maxAccel >= 0.f)
  323. {
  324. maxAccel_ = maxAccel;
  325. UpdateParameters(SCOPE_BASE_PARAMS);
  326. MarkNetworkUpdate();
  327. }
  328. }
  329. void CrowdAgent::SetMaxSpeed(float maxSpeed)
  330. {
  331. if (maxSpeed != maxSpeed_ && maxSpeed >= 0.f)
  332. {
  333. maxSpeed_ = maxSpeed;
  334. UpdateParameters(SCOPE_BASE_PARAMS);
  335. MarkNetworkUpdate();
  336. }
  337. }
  338. void CrowdAgent::SetRadius(float radius)
  339. {
  340. if (radius != radius_ && radius > 0.f)
  341. {
  342. radius_ = radius;
  343. UpdateParameters(SCOPE_BASE_PARAMS | SCOPE_NAVIGATION_PUSHINESS_PARAMS);
  344. MarkNetworkUpdate();
  345. }
  346. }
  347. void CrowdAgent::SetHeight(float height)
  348. {
  349. if (height != height_ && height > 0.f)
  350. {
  351. height_ = height;
  352. UpdateParameters(SCOPE_BASE_PARAMS);
  353. MarkNetworkUpdate();
  354. }
  355. }
  356. void CrowdAgent::SetQueryFilterType(unsigned queryFilterType)
  357. {
  358. if (queryFilterType != queryFilterType_)
  359. {
  360. if (queryFilterType >= DT_CROWD_MAX_QUERY_FILTER_TYPE)
  361. {
  362. URHO3D_LOGERRORF("The specified filter type index (%d) exceeds the maximum allowed value (%d)", queryFilterType,
  363. DT_CROWD_MAX_QUERY_FILTER_TYPE);
  364. return;
  365. }
  366. queryFilterType_ = queryFilterType;
  367. UpdateParameters(SCOPE_BASE_PARAMS);
  368. MarkNetworkUpdate();
  369. }
  370. }
  371. void CrowdAgent::SetObstacleAvoidanceType(unsigned obstacleAvoidanceType)
  372. {
  373. if (obstacleAvoidanceType != obstacleAvoidanceType_)
  374. {
  375. if (obstacleAvoidanceType >= DT_CROWD_MAX_OBSTAVOIDANCE_PARAMS)
  376. {
  377. URHO3D_LOGERRORF("The specified obstacle avoidance type index (%d) exceeds the maximum allowed value (%d)",
  378. obstacleAvoidanceType, DT_CROWD_MAX_OBSTAVOIDANCE_PARAMS);
  379. return;
  380. }
  381. obstacleAvoidanceType_ = obstacleAvoidanceType;
  382. UpdateParameters(SCOPE_BASE_PARAMS);
  383. MarkNetworkUpdate();
  384. }
  385. }
  386. void CrowdAgent::SetNavigationQuality(NavigationQuality val)
  387. {
  388. if (val != navQuality_)
  389. {
  390. navQuality_ = val;
  391. UpdateParameters(SCOPE_NAVIGATION_QUALITY_PARAMS);
  392. MarkNetworkUpdate();
  393. }
  394. }
  395. void CrowdAgent::SetNavigationPushiness(NavigationPushiness val)
  396. {
  397. if (val != navPushiness_)
  398. {
  399. navPushiness_ = val;
  400. UpdateParameters(SCOPE_NAVIGATION_PUSHINESS_PARAMS);
  401. MarkNetworkUpdate();
  402. }
  403. }
  404. Vector3 CrowdAgent::GetPosition() const
  405. {
  406. const dtCrowdAgent* agent = GetDetourCrowdAgent();
  407. return agent ? Vector3(agent->npos) : node_->GetPosition();
  408. }
  409. Vector3 CrowdAgent::GetDesiredVelocity() const
  410. {
  411. const dtCrowdAgent* agent = GetDetourCrowdAgent();
  412. return agent ? Vector3(agent->dvel) : Vector3::ZERO;
  413. }
  414. Vector3 CrowdAgent::GetActualVelocity() const
  415. {
  416. const dtCrowdAgent* agent = GetDetourCrowdAgent();
  417. return agent ? Vector3(agent->vel) : Vector3::ZERO;
  418. }
  419. CrowdAgentState CrowdAgent::GetAgentState() const
  420. {
  421. const dtCrowdAgent* agent = GetDetourCrowdAgent();
  422. return agent ? (CrowdAgentState)agent->state : CA_STATE_INVALID;
  423. }
  424. CrowdAgentTargetState CrowdAgent::GetTargetState() const
  425. {
  426. const dtCrowdAgent* agent = GetDetourCrowdAgent();
  427. return agent ? (CrowdAgentTargetState)agent->targetState : CA_TARGET_NONE;
  428. }
  429. bool CrowdAgent::HasArrived() const
  430. {
  431. // Is the agent at or near the end of its path and within its own radius of the goal?
  432. const dtCrowdAgent* agent = GetDetourCrowdAgent();
  433. return agent && (!agent->ncorners || (agent->cornerFlags[agent->ncorners - 1] & DT_STRAIGHTPATH_END &&
  434. dtVdist2D(agent->npos, &agent->cornerVerts[(agent->ncorners - 1) * 3]) <=
  435. agent->params.radius));
  436. }
  437. bool CrowdAgent::IsInCrowd() const
  438. {
  439. return crowdManager_ && agentCrowdId_ != -1;
  440. }
  441. void CrowdAgent::OnCrowdUpdate(dtCrowdAgent* ag, float dt)
  442. {
  443. assert (ag);
  444. if (node_)
  445. {
  446. Vector3 newPos(ag->npos);
  447. Vector3 newVel(ag->vel);
  448. // Notify parent node of the reposition
  449. if (newPos != previousPosition_)
  450. {
  451. previousPosition_ = newPos;
  452. using namespace CrowdAgentReposition;
  453. VariantMap& map = GetEventDataMap();
  454. map[P_NODE] = node_;
  455. map[P_CROWD_AGENT] = this;
  456. map[P_POSITION] = newPos;
  457. map[P_VELOCITY] = newVel;
  458. map[P_ARRIVED] = HasArrived();
  459. map[P_TIMESTEP] = dt;
  460. crowdManager_->SendEvent(E_CROWD_AGENT_REPOSITION, map);
  461. node_->SendEvent(E_CROWD_AGENT_NODE_REPOSITION, map);
  462. if (updateNodePosition_)
  463. {
  464. ignoreTransformChanges_ = true;
  465. node_->SetPosition(newPos);
  466. ignoreTransformChanges_ = false;
  467. }
  468. }
  469. // Send a notification event if we've reached the destination
  470. CrowdAgentTargetState newTargetState = GetTargetState();
  471. CrowdAgentState newAgentState = GetAgentState();
  472. if (newAgentState != previousAgentState_ || newTargetState != previousTargetState_)
  473. {
  474. using namespace CrowdAgentStateChanged;
  475. VariantMap& map = GetEventDataMap();
  476. map[P_NODE] = node_;
  477. map[P_CROWD_AGENT] = this;
  478. map[P_CROWD_TARGET_STATE] = newTargetState;
  479. map[P_CROWD_AGENT_STATE] = newAgentState;
  480. map[P_POSITION] = newPos;
  481. map[P_VELOCITY] = newVel;
  482. crowdManager_->SendEvent(E_CROWD_AGENT_STATE_CHANGED, map);
  483. node_->SendEvent(E_CROWD_AGENT_NODE_STATE_CHANGED, map);
  484. // Send a failure event if either state is a failed status
  485. if (newAgentState == CA_STATE_INVALID || newTargetState == CA_TARGET_FAILED)
  486. {
  487. VariantMap& map = GetEventDataMap();
  488. map[P_NODE] = node_;
  489. map[P_CROWD_AGENT] = this;
  490. map[P_CROWD_TARGET_STATE] = newTargetState;
  491. map[P_CROWD_AGENT_STATE] = newAgentState;
  492. map[P_POSITION] = newPos;
  493. map[P_VELOCITY] = newVel;
  494. crowdManager_->SendEvent(E_CROWD_AGENT_FAILURE, map);
  495. node_->SendEvent(E_CROWD_AGENT_NODE_FAILURE, map);
  496. }
  497. // State may have been altered during the handling of the event
  498. previousAgentState_ = GetAgentState();
  499. previousTargetState_ = GetTargetState();
  500. }
  501. }
  502. }
  503. void CrowdAgent::OnNodeSet(Node* node)
  504. {
  505. if (node)
  506. node->AddListener(this);
  507. }
  508. void CrowdAgent::OnSceneSet(Scene* scene)
  509. {
  510. if (scene)
  511. {
  512. if (scene == node_)
  513. URHO3D_LOGERROR(GetTypeName() + " should not be created to the root scene node");
  514. crowdManager_ = scene->GetOrCreateComponent<CrowdManager>();
  515. AddAgentToCrowd();
  516. }
  517. else
  518. RemoveAgentFromCrowd();
  519. }
  520. void CrowdAgent::OnMarkedDirty(Node* node)
  521. {
  522. if (!ignoreTransformChanges_ && IsEnabledEffective())
  523. {
  524. dtCrowdAgent* agent = const_cast<dtCrowdAgent*>(GetDetourCrowdAgent());
  525. if (agent)
  526. {
  527. memcpy(agent->npos, node->GetWorldPosition().Data(), sizeof(float) * 3);
  528. // If the node has been externally altered, provide the opportunity for DetourCrowd to reevaluate the crowd agent
  529. if (agent->state == CA_STATE_INVALID)
  530. agent->state = CA_STATE_WALKING;
  531. }
  532. }
  533. }
  534. const dtCrowdAgent* CrowdAgent::GetDetourCrowdAgent() const
  535. {
  536. return IsInCrowd() ? crowdManager_->GetDetourCrowdAgent(agentCrowdId_) : 0;
  537. }
  538. }