CrowdAgent.cpp 20 KB

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