CrowdAgent.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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/include/DetourCommon.h>
  33. #include <DetourCrowd/include/DetourCrowd.h>
  34. #include "../DebugNew.h"
  35. namespace Atomic
  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. ATTRIBUTE("Target Position", Vector3, targetPosition_, Vector3::ZERO, AM_DEFAULT);
  92. ATTRIBUTE("Target Velocity", Vector3, targetVelocity_, Vector3::ZERO, AM_DEFAULT);
  93. ENUM_ATTRIBUTE("Requested Target Type", requestedTargetType_, crowdAgentRequestedTargetTypeNames,
  94. DEFAULT_AGENT_REQUEST_TARGET_TYPE, AM_DEFAULT);
  95. ACCESSOR_ATTRIBUTE("Update Node Position", GetUpdateNodePosition, SetUpdateNodePosition, bool, true, AM_DEFAULT);
  96. ATTRIBUTE("Max Accel", float, maxAccel_, DEFAULT_AGENT_MAX_ACCEL, AM_DEFAULT);
  97. ATTRIBUTE("Max Speed", float, maxSpeed_, DEFAULT_AGENT_MAX_SPEED, AM_DEFAULT);
  98. ATTRIBUTE("Radius", float, radius_, 0.0f, AM_DEFAULT);
  99. ATTRIBUTE("Height", float, height_, 0.0f, AM_DEFAULT);
  100. ATTRIBUTE("Query Filter Type", unsigned, queryFilterType_, DEFAULT_AGENT_QUERY_FILTER_TYPE, AM_DEFAULT);
  101. ATTRIBUTE("Obstacle Avoidance Type", unsigned, obstacleAvoidanceType_, DEFAULT_AGENT_OBSTACLE_AVOIDANCE_TYPE, AM_DEFAULT);
  102. ENUM_ATTRIBUTE("Navigation Pushiness", navPushiness_, crowdAgentPushinessNames, DEFAULT_AGENT_NAVIGATION_PUSHINESS, AM_DEFAULT);
  103. 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. 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. // Reevaluate states as handling of event may have resulted in changes
  255. previousAgentState_ = GetAgentState();
  256. previousTargetState_ = GetTargetState();
  257. }
  258. // Save the initial position to prevent CrowdAgentReposition event being triggered unnecessarily
  259. previousPosition_ = GetPosition();
  260. }
  261. return agentCrowdId_;
  262. }
  263. void CrowdAgent::RemoveAgentFromCrowd()
  264. {
  265. if (IsInCrowd())
  266. {
  267. crowdManager_->RemoveAgent(this);
  268. agentCrowdId_ = -1;
  269. }
  270. }
  271. void CrowdAgent::SetTargetPosition(const Vector3& position)
  272. {
  273. if (position != targetPosition_ || CA_REQUESTEDTARGET_POSITION != requestedTargetType_)
  274. {
  275. targetPosition_ = position;
  276. requestedTargetType_ = CA_REQUESTEDTARGET_POSITION;
  277. MarkNetworkUpdate();
  278. if (!IsInCrowd())
  279. AddAgentToCrowd();
  280. if (IsInCrowd()) // Make sure the previous method call is successful
  281. {
  282. dtPolyRef nearestRef;
  283. Vector3 nearestPos = crowdManager_->FindNearestPoint(position, queryFilterType_, &nearestRef);
  284. crowdManager_->GetCrowd()->requestMoveTarget(agentCrowdId_, nearestRef, nearestPos.Data());
  285. }
  286. }
  287. }
  288. void CrowdAgent::SetTargetVelocity(const Vector3& velocity)
  289. {
  290. if (velocity != targetVelocity_ || CA_REQUESTEDTARGET_VELOCITY != requestedTargetType_)
  291. {
  292. targetVelocity_ = velocity;
  293. requestedTargetType_ = CA_REQUESTEDTARGET_VELOCITY;
  294. MarkNetworkUpdate();
  295. if (IsInCrowd())
  296. crowdManager_->GetCrowd()->requestMoveVelocity(agentCrowdId_, velocity.Data());
  297. }
  298. }
  299. void CrowdAgent::ResetTarget()
  300. {
  301. if (CA_REQUESTEDTARGET_NONE != requestedTargetType_)
  302. {
  303. requestedTargetType_ = CA_REQUESTEDTARGET_NONE;
  304. MarkNetworkUpdate();
  305. if (IsInCrowd())
  306. crowdManager_->GetCrowd()->resetMoveTarget(agentCrowdId_);
  307. }
  308. }
  309. void CrowdAgent::SetUpdateNodePosition(bool unodepos)
  310. {
  311. if (unodepos != updateNodePosition_)
  312. {
  313. updateNodePosition_ = unodepos;
  314. MarkNetworkUpdate();
  315. }
  316. }
  317. void CrowdAgent::SetMaxAccel(float maxAccel)
  318. {
  319. if (maxAccel != maxAccel_ && maxAccel >= 0.f)
  320. {
  321. maxAccel_ = maxAccel;
  322. UpdateParameters(SCOPE_BASE_PARAMS);
  323. MarkNetworkUpdate();
  324. }
  325. }
  326. void CrowdAgent::SetMaxSpeed(float maxSpeed)
  327. {
  328. if (maxSpeed != maxSpeed_ && maxSpeed >= 0.f)
  329. {
  330. maxSpeed_ = maxSpeed;
  331. UpdateParameters(SCOPE_BASE_PARAMS);
  332. MarkNetworkUpdate();
  333. }
  334. }
  335. void CrowdAgent::SetRadius(float radius)
  336. {
  337. if (radius != radius_ && radius > 0.f)
  338. {
  339. radius_ = radius;
  340. UpdateParameters(SCOPE_BASE_PARAMS | SCOPE_NAVIGATION_PUSHINESS_PARAMS);
  341. MarkNetworkUpdate();
  342. }
  343. }
  344. void CrowdAgent::SetHeight(float height)
  345. {
  346. if (height != height_ && height > 0.f)
  347. {
  348. height_ = height;
  349. UpdateParameters(SCOPE_BASE_PARAMS);
  350. MarkNetworkUpdate();
  351. }
  352. }
  353. void CrowdAgent::SetQueryFilterType(unsigned queryFilterType)
  354. {
  355. if (queryFilterType != queryFilterType_)
  356. {
  357. if (queryFilterType >= DT_CROWD_MAX_QUERY_FILTER_TYPE)
  358. {
  359. LOGERRORF("The specified filter type index (%d) exceeds the maximum allowed value (%d)", queryFilterType,
  360. DT_CROWD_MAX_QUERY_FILTER_TYPE);
  361. return;
  362. }
  363. queryFilterType_ = queryFilterType;
  364. UpdateParameters(SCOPE_BASE_PARAMS);
  365. MarkNetworkUpdate();
  366. }
  367. }
  368. void CrowdAgent::SetObstacleAvoidanceType(unsigned obstacleAvoidanceType)
  369. {
  370. if (obstacleAvoidanceType != obstacleAvoidanceType_)
  371. {
  372. if (obstacleAvoidanceType >= DT_CROWD_MAX_OBSTAVOIDANCE_PARAMS)
  373. {
  374. LOGERRORF("The specified obstacle avoidance type index (%d) exceeds the maximum allowed value (%d)",
  375. obstacleAvoidanceType, DT_CROWD_MAX_OBSTAVOIDANCE_PARAMS);
  376. return;
  377. }
  378. obstacleAvoidanceType_ = obstacleAvoidanceType;
  379. UpdateParameters(SCOPE_BASE_PARAMS);
  380. MarkNetworkUpdate();
  381. }
  382. }
  383. void CrowdAgent::SetNavigationQuality(NavigationQuality val)
  384. {
  385. if (val != navQuality_)
  386. {
  387. navQuality_ = val;
  388. UpdateParameters(SCOPE_NAVIGATION_QUALITY_PARAMS);
  389. MarkNetworkUpdate();
  390. }
  391. }
  392. void CrowdAgent::SetNavigationPushiness(NavigationPushiness val)
  393. {
  394. if (val != navPushiness_)
  395. {
  396. navPushiness_ = val;
  397. UpdateParameters(SCOPE_NAVIGATION_PUSHINESS_PARAMS);
  398. MarkNetworkUpdate();
  399. }
  400. }
  401. Vector3 CrowdAgent::GetPosition() const
  402. {
  403. const dtCrowdAgent* agent = GetDetourCrowdAgent();
  404. return agent ? Vector3(agent->npos) : node_->GetPosition();
  405. }
  406. Vector3 CrowdAgent::GetDesiredVelocity() const
  407. {
  408. const dtCrowdAgent* agent = GetDetourCrowdAgent();
  409. return agent ? Vector3(agent->dvel) : Vector3::ZERO;
  410. }
  411. Vector3 CrowdAgent::GetActualVelocity() const
  412. {
  413. const dtCrowdAgent* agent = GetDetourCrowdAgent();
  414. return agent ? Vector3(agent->vel) : Vector3::ZERO;
  415. }
  416. CrowdAgentState CrowdAgent::GetAgentState() const
  417. {
  418. const dtCrowdAgent* agent = GetDetourCrowdAgent();
  419. return agent ? (CrowdAgentState)agent->state : CA_STATE_INVALID;
  420. }
  421. CrowdAgentTargetState CrowdAgent::GetTargetState() const
  422. {
  423. const dtCrowdAgent* agent = GetDetourCrowdAgent();
  424. return agent ? (CrowdAgentTargetState)agent->targetState : CA_TARGET_NONE;
  425. }
  426. bool CrowdAgent::HasArrived() const
  427. {
  428. // Is the agent at or near the end of its path and within its own radius of the goal?
  429. const dtCrowdAgent* agent = GetDetourCrowdAgent();
  430. return agent && (!agent->ncorners || (agent->cornerFlags[agent->ncorners - 1] & DT_STRAIGHTPATH_END &&
  431. dtVdist2D(agent->npos, &agent->cornerVerts[(agent->ncorners - 1) * 3]) <=
  432. agent->params.radius));
  433. }
  434. bool CrowdAgent::IsInCrowd() const
  435. {
  436. return crowdManager_ && agentCrowdId_ != -1;
  437. }
  438. void CrowdAgent::OnCrowdUpdate(dtCrowdAgent* ag, float dt)
  439. {
  440. assert (ag);
  441. if (node_)
  442. {
  443. Vector3 newPos(ag->npos);
  444. Vector3 newVel(ag->vel);
  445. // Notify parent node of the reposition
  446. if (newPos != previousPosition_)
  447. {
  448. previousPosition_ = newPos;
  449. using namespace CrowdAgentReposition;
  450. VariantMap& map = GetEventDataMap();
  451. map[P_NODE] = node_;
  452. map[P_CROWD_AGENT] = this;
  453. map[P_POSITION] = newPos;
  454. map[P_VELOCITY] = newVel;
  455. map[P_ARRIVED] = HasArrived();
  456. map[P_TIMESTEP] = dt;
  457. crowdManager_->SendEvent(E_CROWD_AGENT_REPOSITION, map);
  458. if (updateNodePosition_)
  459. {
  460. ignoreTransformChanges_ = true;
  461. node_->SetPosition(newPos);
  462. ignoreTransformChanges_ = false;
  463. }
  464. }
  465. // Send a notification event if we've reached the destination
  466. CrowdAgentTargetState newTargetState = GetTargetState();
  467. CrowdAgentState newAgentState = GetAgentState();
  468. if (newAgentState != previousAgentState_ || newTargetState != previousTargetState_)
  469. {
  470. using namespace CrowdAgentStateChanged;
  471. VariantMap& map = GetEventDataMap();
  472. map[P_NODE] = node_;
  473. map[P_CROWD_AGENT] = this;
  474. map[P_CROWD_TARGET_STATE] = newTargetState;
  475. map[P_CROWD_AGENT_STATE] = newAgentState;
  476. map[P_POSITION] = newPos;
  477. map[P_VELOCITY] = newVel;
  478. crowdManager_->SendEvent(E_CROWD_AGENT_STATE_CHANGED, map);
  479. // Send a failure event if either state is a failed status
  480. if (newAgentState == CA_STATE_INVALID || newTargetState == CA_TARGET_FAILED)
  481. {
  482. VariantMap& map = GetEventDataMap();
  483. map[P_NODE] = node_;
  484. map[P_CROWD_AGENT] = this;
  485. map[P_CROWD_TARGET_STATE] = newTargetState;
  486. map[P_CROWD_AGENT_STATE] = newAgentState;
  487. map[P_POSITION] = newPos;
  488. map[P_VELOCITY] = newVel;
  489. crowdManager_->SendEvent(E_CROWD_AGENT_FAILURE, map);
  490. }
  491. // State may have been altered during the handling of the event
  492. previousAgentState_ = GetAgentState();
  493. previousTargetState_ = GetTargetState();
  494. }
  495. }
  496. }
  497. void CrowdAgent::OnNodeSet(Node* node)
  498. {
  499. if (node)
  500. node->AddListener(this);
  501. }
  502. void CrowdAgent::OnSceneSet(Scene* scene)
  503. {
  504. if (scene)
  505. {
  506. if (scene == node_)
  507. LOGERROR(GetTypeName() + " should not be created to the root scene node");
  508. crowdManager_ = scene->GetOrCreateComponent<CrowdManager>();
  509. AddAgentToCrowd();
  510. }
  511. else
  512. RemoveAgentFromCrowd();
  513. }
  514. void CrowdAgent::OnMarkedDirty(Node* node)
  515. {
  516. if (!ignoreTransformChanges_ && IsEnabledEffective())
  517. {
  518. dtCrowdAgent* agent = const_cast<dtCrowdAgent*>(GetDetourCrowdAgent());
  519. if (agent)
  520. {
  521. memcpy(agent->npos, node->GetWorldPosition().Data(), sizeof(float) * 3);
  522. // If the node has been externally altered, provide the opportunity for DetourCrowd to reevaluate the crowd agent
  523. if (agent->state == CA_STATE_INVALID)
  524. agent->state = CA_STATE_WALKING;
  525. }
  526. }
  527. }
  528. const dtCrowdAgent* CrowdAgent::GetDetourCrowdAgent() const
  529. {
  530. return IsInCrowd() ? crowdManager_->GetDetourCrowdAgent(agentCrowdId_) : 0;
  531. }
  532. }