CrowdManager.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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 "../Navigation/CrowdAgent.h"
  28. #include "../Navigation/CrowdManager.h"
  29. #include "../Navigation/DynamicNavigationMesh.h"
  30. #include "../Navigation/NavigationEvents.h"
  31. #include "../Scene/Node.h"
  32. #include "../Scene/Scene.h"
  33. #include "../Scene/SceneEvents.h"
  34. #include <DetourCrowd/DetourCrowd.h>
  35. #include "../DebugNew.h"
  36. namespace Urho3D
  37. {
  38. extern const char* NAVIGATION_CATEGORY;
  39. static const unsigned DEFAULT_MAX_AGENTS = 512;
  40. static const float DEFAULT_MAX_AGENT_RADIUS = 0.f;
  41. void CrowdAgentUpdateCallback(dtCrowdAgent* ag, float dt)
  42. {
  43. static_cast<CrowdAgent*>(ag->params.userData)->OnCrowdUpdate(ag, dt);
  44. }
  45. CrowdManager::CrowdManager(Context* context) :
  46. Component(context),
  47. crowd_(0),
  48. navigationMesh_(0),
  49. navigationMeshId_(0),
  50. maxAgents_(DEFAULT_MAX_AGENTS),
  51. maxAgentRadius_(DEFAULT_MAX_AGENT_RADIUS),
  52. numQueryFilterTypes_(0),
  53. numObstacleAvoidanceTypes_(0)
  54. {
  55. // The actual buffer is allocated inside dtCrowd, we only track the number of "slots" being configured explicitly
  56. numAreas_.Reserve(DT_CROWD_MAX_QUERY_FILTER_TYPE);
  57. for (unsigned i = 0; i < DT_CROWD_MAX_QUERY_FILTER_TYPE; ++i)
  58. numAreas_.Push(0);
  59. }
  60. CrowdManager::~CrowdManager()
  61. {
  62. dtFreeCrowd(crowd_);
  63. crowd_ = 0;
  64. }
  65. void CrowdManager::RegisterObject(Context* context)
  66. {
  67. context->RegisterFactory<CrowdManager>(NAVIGATION_CATEGORY);
  68. ATTRIBUTE("Max Agents", unsigned, maxAgents_, DEFAULT_MAX_AGENTS, AM_DEFAULT);
  69. ATTRIBUTE("Max Agent Radius", float, maxAgentRadius_, DEFAULT_MAX_AGENT_RADIUS, AM_DEFAULT);
  70. ATTRIBUTE("Navigation Mesh", unsigned, navigationMeshId_, 0, AM_DEFAULT | AM_COMPONENTID);
  71. MIXED_ACCESSOR_ATTRIBUTE("Filter Types", GetQueryFilterTypesAttr, SetQueryFilterTypesAttr, VariantVector,
  72. Variant::emptyVariantVector, AM_DEFAULT);
  73. MIXED_ACCESSOR_ATTRIBUTE("Obstacle Avoidance Types", GetObstacleAvoidanceTypesAttr, SetObstacleAvoidanceTypesAttr,
  74. VariantVector, Variant::emptyVariantVector, AM_DEFAULT);
  75. }
  76. void CrowdManager::ApplyAttributes()
  77. {
  78. // Values from Editor, saved-file, or network must be checked before applying
  79. maxAgents_ = (unsigned)Max(1, maxAgents_);
  80. maxAgentRadius_ = Max(0.f, maxAgentRadius_);
  81. bool navMeshChange = false;
  82. Scene* scene = GetScene();
  83. if (scene && navigationMeshId_)
  84. {
  85. NavigationMesh* navMesh = dynamic_cast<NavigationMesh*>(scene->GetComponent(navigationMeshId_));
  86. if (navMesh)
  87. {
  88. navMeshChange = navMesh != navigationMesh_;
  89. navigationMesh_ = navMesh;
  90. }
  91. }
  92. // In case of receiving an invalid component id, revert it back to the existing navmesh component id (if any)
  93. navigationMeshId_ = navigationMesh_ ? navigationMesh_->GetID() : 0;
  94. // If the Detour crowd initialization parameters have changed then recreate it
  95. if (crowd_ && (navMeshChange || crowd_->getAgentCount() != maxAgents_ || crowd_->getMaxAgentRadius() != maxAgentRadius_))
  96. CreateCrowd();
  97. }
  98. void CrowdManager::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  99. {
  100. if (debug && crowd_)
  101. {
  102. // Current position-to-target line
  103. for (int i = 0; i < crowd_->getAgentCount(); i++)
  104. {
  105. const dtCrowdAgent* ag = crowd_->getAgent(i);
  106. if (!ag->active)
  107. continue;
  108. // Draw CrowdAgent shape (from its radius & height)
  109. CrowdAgent* crowdAgent = static_cast<CrowdAgent*>(ag->params.userData);
  110. crowdAgent->DrawDebugGeometry(debug, depthTest);
  111. // Draw move target if any
  112. if (crowdAgent->GetTargetState() == CA_TARGET_NONE)
  113. continue;
  114. Color color(0.6f, 0.2f, 0.2f, 1.0f);
  115. // Draw line to target
  116. Vector3 pos1(ag->npos[0], ag->npos[1], ag->npos[2]);
  117. Vector3 pos2;
  118. for (int i = 0; i < ag->ncorners; ++i)
  119. {
  120. pos2.x_ = ag->cornerVerts[i * 3];
  121. pos2.y_ = ag->cornerVerts[i * 3 + 1];
  122. pos2.z_ = ag->cornerVerts[i * 3 + 2];
  123. debug->AddLine(pos1, pos2, color, depthTest);
  124. pos1 = pos2;
  125. }
  126. pos2.x_ = ag->targetPos[0];
  127. pos2.y_ = ag->targetPos[1];
  128. pos2.z_ = ag->targetPos[2];
  129. debug->AddLine(pos1, pos2, color, depthTest);
  130. // Draw target circle
  131. debug->AddSphere(Sphere(pos2, 0.5f), color, depthTest);
  132. }
  133. }
  134. }
  135. void CrowdManager::DrawDebugGeometry(bool depthTest)
  136. {
  137. Scene* scene = GetScene();
  138. if (scene)
  139. {
  140. DebugRenderer* debug = scene->GetComponent<DebugRenderer>();
  141. if (debug)
  142. DrawDebugGeometry(debug, depthTest);
  143. }
  144. }
  145. void CrowdManager::SetCrowdTarget(const Vector3& position, Node* node)
  146. {
  147. if (!crowd_)
  148. return;
  149. PODVector<CrowdAgent*> agents = GetAgents(node, false); // Get all crowd agent components
  150. Vector3 moveTarget(position);
  151. for (unsigned i = 0; i < agents.Size(); ++i)
  152. {
  153. // Give application a chance to determine the desired crowd formation when they reach the target position
  154. CrowdAgent* agent = agents[i];
  155. using namespace CrowdAgentFormation;
  156. VariantMap& map = GetEventDataMap();
  157. map[P_NODE] = agent->GetNode();
  158. map[P_CROWD_AGENT] = agent;
  159. map[P_INDEX] = i;
  160. map[P_SIZE] = agents.Size();
  161. map[P_POSITION] = moveTarget; // Expect the event handler will modify this position accordingly
  162. SendEvent(E_CROWD_AGENT_FORMATION, map);
  163. moveTarget = map[P_POSITION].GetVector3();
  164. agent->SetTargetPosition(moveTarget);
  165. }
  166. }
  167. void CrowdManager::SetCrowdVelocity(const Vector3& velocity, Node* node)
  168. {
  169. if (!crowd_)
  170. return;
  171. PODVector<CrowdAgent*> agents = GetAgents(node, true); // Get only crowd agent components already in the crowd
  172. for (unsigned i = 0; i < agents.Size(); ++i)
  173. agents[i]->SetTargetVelocity(velocity);
  174. }
  175. void CrowdManager::ResetCrowdTarget(Node* node)
  176. {
  177. if (!crowd_)
  178. return;
  179. PODVector<CrowdAgent*> agents = GetAgents(node, true);
  180. for (unsigned i = 0; i < agents.Size(); ++i)
  181. agents[i]->ResetTarget();
  182. }
  183. void CrowdManager::SetMaxAgents(unsigned maxAgents)
  184. {
  185. if (maxAgents != maxAgents_ && maxAgents > 0)
  186. {
  187. maxAgents_ = maxAgents;
  188. CreateCrowd();
  189. MarkNetworkUpdate();
  190. }
  191. }
  192. void CrowdManager::SetMaxAgentRadius(float maxAgentRadius)
  193. {
  194. if (maxAgentRadius != maxAgentRadius_ && maxAgentRadius > 0.f)
  195. {
  196. maxAgentRadius_ = maxAgentRadius;
  197. CreateCrowd();
  198. MarkNetworkUpdate();
  199. }
  200. }
  201. void CrowdManager::SetNavigationMesh(NavigationMesh* navMesh)
  202. {
  203. if (navMesh != navigationMesh_) // It is possible to reset navmesh pointer back to 0
  204. {
  205. navigationMesh_ = navMesh;
  206. navigationMeshId_ = navMesh ? navMesh->GetID() : 0;
  207. CreateCrowd();
  208. MarkNetworkUpdate();
  209. }
  210. }
  211. void CrowdManager::SetQueryFilterTypesAttr(const VariantVector& value)
  212. {
  213. if (!crowd_)
  214. return;
  215. unsigned index = 0;
  216. unsigned queryFilterType = 0;
  217. numQueryFilterTypes_ = index < value.Size() ? Min(value[index++].GetUInt(), DT_CROWD_MAX_QUERY_FILTER_TYPE) : 0;
  218. while (queryFilterType < numQueryFilterTypes_)
  219. {
  220. if (index + 3 <= value.Size())
  221. {
  222. dtQueryFilter* filter = crowd_->getEditableFilter(queryFilterType);
  223. assert(filter);
  224. filter->setIncludeFlags((unsigned short)value[index++].GetUInt());
  225. filter->setExcludeFlags((unsigned short)value[index++].GetUInt());
  226. unsigned prevNumAreas = numAreas_[queryFilterType];
  227. numAreas_[queryFilterType] = Min(value[index++].GetUInt(), DT_MAX_AREAS);
  228. // Must loop thru based on previous number of areas, the new area cost (if any) can only be set in the next attribute get/set iteration
  229. if (index + prevNumAreas <= value.Size())
  230. {
  231. for (unsigned i = 0; i < prevNumAreas; ++i)
  232. filter->setAreaCost(i, value[index++].GetFloat());
  233. }
  234. }
  235. ++queryFilterType;
  236. }
  237. }
  238. void CrowdManager::SetIncludeFlags(unsigned queryFilterType, unsigned short flags)
  239. {
  240. dtQueryFilter* filter = const_cast<dtQueryFilter*>(GetDetourQueryFilter(queryFilterType));
  241. if (filter)
  242. {
  243. filter->setIncludeFlags(flags);
  244. if (numQueryFilterTypes_ < queryFilterType + 1)
  245. numQueryFilterTypes_ = queryFilterType + 1;
  246. MarkNetworkUpdate();
  247. }
  248. }
  249. void CrowdManager::SetExcludeFlags(unsigned queryFilterType, unsigned short flags)
  250. {
  251. dtQueryFilter* filter = const_cast<dtQueryFilter*>(GetDetourQueryFilter(queryFilterType));
  252. if (filter)
  253. {
  254. filter->setExcludeFlags(flags);
  255. if (numQueryFilterTypes_ < queryFilterType + 1)
  256. numQueryFilterTypes_ = queryFilterType + 1;
  257. MarkNetworkUpdate();
  258. }
  259. }
  260. void CrowdManager::SetAreaCost(unsigned queryFilterType, unsigned areaID, float cost)
  261. {
  262. dtQueryFilter* filter = const_cast<dtQueryFilter*>(GetDetourQueryFilter(queryFilterType));
  263. if (filter && areaID < DT_MAX_AREAS)
  264. {
  265. filter->setAreaCost((int)areaID, cost);
  266. if (numQueryFilterTypes_ < queryFilterType + 1)
  267. numQueryFilterTypes_ = queryFilterType + 1;
  268. if (numAreas_[queryFilterType] < areaID + 1)
  269. numAreas_[queryFilterType] = areaID + 1;
  270. MarkNetworkUpdate();
  271. }
  272. }
  273. void CrowdManager::SetObstacleAvoidanceTypesAttr(const VariantVector& value)
  274. {
  275. if (!crowd_)
  276. return;
  277. unsigned index = 0;
  278. unsigned obstacleAvoidanceType = 0;
  279. numObstacleAvoidanceTypes_ = index < value.Size() ? Min(value[index++].GetUInt(), DT_CROWD_MAX_OBSTAVOIDANCE_PARAMS) : 0;
  280. while (obstacleAvoidanceType < numObstacleAvoidanceTypes_)
  281. {
  282. if (index + 10 <= value.Size())
  283. {
  284. dtObstacleAvoidanceParams params;
  285. params.velBias = value[index++].GetFloat();
  286. params.weightDesVel = value[index++].GetFloat();
  287. params.weightCurVel = value[index++].GetFloat();
  288. params.weightSide = value[index++].GetFloat();
  289. params.weightToi = value[index++].GetFloat();
  290. params.horizTime = value[index++].GetFloat();
  291. params.gridSize = (unsigned char)value[index++].GetUInt();
  292. params.adaptiveDivs = (unsigned char)value[index++].GetUInt();
  293. params.adaptiveRings = (unsigned char)value[index++].GetUInt();
  294. params.adaptiveDepth = (unsigned char)value[index++].GetUInt();
  295. crowd_->setObstacleAvoidanceParams(obstacleAvoidanceType, &params);
  296. }
  297. ++obstacleAvoidanceType;
  298. }
  299. }
  300. void CrowdManager::SetObstacleAvoidanceParams(unsigned obstacleAvoidanceType, const CrowdObstacleAvoidanceParams& params)
  301. {
  302. if (crowd_ && obstacleAvoidanceType < DT_CROWD_MAX_OBSTAVOIDANCE_PARAMS)
  303. {
  304. crowd_->setObstacleAvoidanceParams(obstacleAvoidanceType, reinterpret_cast<const dtObstacleAvoidanceParams*>(&params));
  305. if (numObstacleAvoidanceTypes_ < obstacleAvoidanceType + 1)
  306. numObstacleAvoidanceTypes_ = obstacleAvoidanceType + 1;
  307. MarkNetworkUpdate();
  308. }
  309. }
  310. Vector3 CrowdManager::FindNearestPoint(const Vector3& point, int queryFilterType, dtPolyRef* nearestRef)
  311. {
  312. if (nearestRef)
  313. *nearestRef = 0;
  314. return crowd_ && navigationMesh_ ?
  315. navigationMesh_->FindNearestPoint(point, Vector3(crowd_->getQueryExtents()), crowd_->getFilter(queryFilterType), nearestRef) : point;
  316. }
  317. Vector3 CrowdManager::MoveAlongSurface(const Vector3& start, const Vector3& end, int queryFilterType, int maxVisited)
  318. {
  319. return crowd_ && navigationMesh_ ?
  320. navigationMesh_->MoveAlongSurface(start, end, Vector3(crowd_->getQueryExtents()), maxVisited, crowd_->getFilter(queryFilterType)) :
  321. end;
  322. }
  323. void CrowdManager::FindPath(PODVector<Vector3>& dest, const Vector3& start, const Vector3& end, int queryFilterType)
  324. {
  325. if (crowd_ && navigationMesh_)
  326. navigationMesh_->FindPath(dest, start, end, Vector3(crowd_->getQueryExtents()), crowd_->getFilter(queryFilterType));
  327. }
  328. Vector3 CrowdManager::GetRandomPoint(int queryFilterType, dtPolyRef* randomRef)
  329. {
  330. if (randomRef)
  331. *randomRef = 0;
  332. return crowd_ && navigationMesh_ ? navigationMesh_->GetRandomPoint(crowd_->getFilter(queryFilterType), randomRef) :
  333. Vector3::ZERO;
  334. }
  335. Vector3 CrowdManager::GetRandomPointInCircle(const Vector3& center, float radius, int queryFilterType, dtPolyRef* randomRef)
  336. {
  337. if (randomRef)
  338. *randomRef = 0;
  339. return crowd_ && navigationMesh_ ?
  340. navigationMesh_->GetRandomPointInCircle(center, radius, Vector3(crowd_->getQueryExtents()),
  341. crowd_->getFilter(queryFilterType), randomRef) : center;
  342. }
  343. float CrowdManager::GetDistanceToWall(const Vector3& point, float radius, int queryFilterType, Vector3* hitPos, Vector3* hitNormal)
  344. {
  345. if (hitPos)
  346. *hitPos = Vector3::ZERO;
  347. if (hitNormal)
  348. *hitNormal = Vector3::DOWN;
  349. return crowd_ && navigationMesh_ ?
  350. navigationMesh_->GetDistanceToWall(point, radius, Vector3(crowd_->getQueryExtents()), crowd_->getFilter(queryFilterType),
  351. hitPos, hitNormal) : radius;
  352. }
  353. Vector3 CrowdManager::Raycast(const Vector3& start, const Vector3& end, int queryFilterType, Vector3* hitNormal)
  354. {
  355. if (hitNormal)
  356. *hitNormal = Vector3::DOWN;
  357. return crowd_ && navigationMesh_ ?
  358. navigationMesh_->Raycast(start, end, Vector3(crowd_->getQueryExtents()), crowd_->getFilter(queryFilterType), hitNormal)
  359. : end;
  360. }
  361. unsigned CrowdManager::GetNumAreas(unsigned queryFilterType) const
  362. {
  363. return queryFilterType < numQueryFilterTypes_ ? numAreas_[queryFilterType] : 0;
  364. }
  365. VariantVector CrowdManager::GetQueryFilterTypesAttr() const
  366. {
  367. VariantVector ret;
  368. if (crowd_)
  369. {
  370. unsigned totalNumAreas = 0;
  371. for (unsigned i = 0; i < numQueryFilterTypes_; ++i)
  372. totalNumAreas += numAreas_[i];
  373. ret.Reserve(numQueryFilterTypes_ * 3 + totalNumAreas + 1);
  374. ret.Push(numQueryFilterTypes_);
  375. for (unsigned i = 0; i < numQueryFilterTypes_; ++i)
  376. {
  377. const dtQueryFilter* filter = crowd_->getFilter(i);
  378. assert(filter);
  379. ret.Push(filter->getIncludeFlags());
  380. ret.Push(filter->getExcludeFlags());
  381. ret.Push(numAreas_[i]);
  382. for (unsigned j = 0; j < numAreas_[i]; ++j)
  383. ret.Push(filter->getAreaCost(j));
  384. }
  385. }
  386. else
  387. ret.Push(0);
  388. return ret;
  389. }
  390. unsigned short CrowdManager::GetIncludeFlags(unsigned queryFilterType) const
  391. {
  392. if (queryFilterType >= numQueryFilterTypes_)
  393. LOGWARNINGF("Query filter type %d is not configured yet, returning the default include flags initialized by dtCrowd",
  394. queryFilterType);
  395. const dtQueryFilter* filter = GetDetourQueryFilter(queryFilterType);
  396. return (unsigned short)(filter ? filter->getIncludeFlags() : 0xffff);
  397. }
  398. unsigned short CrowdManager::GetExcludeFlags(unsigned queryFilterType) const
  399. {
  400. if (queryFilterType >= numQueryFilterTypes_)
  401. LOGWARNINGF("Query filter type %d is not configured yet, returning the default exclude flags initialized by dtCrowd",
  402. queryFilterType);
  403. const dtQueryFilter* filter = GetDetourQueryFilter(queryFilterType);
  404. return (unsigned short)(filter ? filter->getExcludeFlags() : 0);
  405. }
  406. float CrowdManager::GetAreaCost(unsigned queryFilterType, unsigned areaID) const
  407. {
  408. if (queryFilterType >= numQueryFilterTypes_ || areaID >= numAreas_[queryFilterType])
  409. LOGWARNINGF(
  410. "Query filter type %d and/or area id %d are not configured yet, returning the default area cost initialized by dtCrowd",
  411. queryFilterType, areaID);
  412. const dtQueryFilter* filter = GetDetourQueryFilter(queryFilterType);
  413. return filter ? filter->getAreaCost((int)areaID) : 1.f;
  414. }
  415. VariantVector CrowdManager::GetObstacleAvoidanceTypesAttr() const
  416. {
  417. VariantVector ret;
  418. if (crowd_)
  419. {
  420. ret.Reserve(numObstacleAvoidanceTypes_ * 10 + 1);
  421. ret.Push(numObstacleAvoidanceTypes_);
  422. for (unsigned i = 0; i < numObstacleAvoidanceTypes_; ++i)
  423. {
  424. const dtObstacleAvoidanceParams* params = crowd_->getObstacleAvoidanceParams(i);
  425. assert(params);
  426. ret.Push(params->velBias);
  427. ret.Push(params->weightDesVel);
  428. ret.Push(params->weightCurVel);
  429. ret.Push(params->weightSide);
  430. ret.Push(params->weightToi);
  431. ret.Push(params->horizTime);
  432. ret.Push(params->gridSize);
  433. ret.Push(params->adaptiveDivs);
  434. ret.Push(params->adaptiveRings);
  435. ret.Push(params->adaptiveDepth);
  436. }
  437. }
  438. else
  439. ret.Push(0);
  440. return ret;
  441. }
  442. const CrowdObstacleAvoidanceParams& CrowdManager::GetObstacleAvoidanceParams(unsigned obstacleAvoidanceType) const
  443. {
  444. static const CrowdObstacleAvoidanceParams EMPTY_PARAMS = CrowdObstacleAvoidanceParams();
  445. const dtObstacleAvoidanceParams* params = crowd_ ? crowd_->getObstacleAvoidanceParams(obstacleAvoidanceType) : 0;
  446. return params ? *reinterpret_cast<const CrowdObstacleAvoidanceParams*>(params) : EMPTY_PARAMS;
  447. }
  448. PODVector<CrowdAgent*> CrowdManager::GetAgents(Node* node, bool inCrowdFilter) const
  449. {
  450. if (!node)
  451. node = GetScene();
  452. PODVector<CrowdAgent*> agents;
  453. node->GetComponents<CrowdAgent>(agents, true);
  454. if (inCrowdFilter)
  455. {
  456. PODVector<CrowdAgent*>::Iterator i = agents.Begin();
  457. while (i != agents.End())
  458. {
  459. if ((*i)->IsInCrowd())
  460. ++i;
  461. else
  462. i = agents.Erase(i);
  463. }
  464. }
  465. return agents;
  466. }
  467. bool CrowdManager::CreateCrowd()
  468. {
  469. if (!navigationMesh_ || !navigationMesh_->InitializeQuery())
  470. return false;
  471. // Preserve the existing crowd configuration before recreating it
  472. VariantVector queryFilterTypeConfiguration, obstacleAvoidanceTypeConfiguration;
  473. bool recreate = crowd_ != 0;
  474. if (recreate)
  475. {
  476. queryFilterTypeConfiguration = GetQueryFilterTypesAttr();
  477. obstacleAvoidanceTypeConfiguration = GetObstacleAvoidanceTypesAttr();
  478. dtFreeCrowd(crowd_);
  479. }
  480. crowd_ = dtAllocCrowd();
  481. // Initialize the crowd
  482. if (maxAgentRadius_ == 0.f)
  483. maxAgentRadius_ = navigationMesh_->GetAgentRadius();
  484. if (!crowd_->init(maxAgents_, maxAgentRadius_, navigationMesh_->navMesh_, CrowdAgentUpdateCallback))
  485. {
  486. LOGERROR("Could not initialize DetourCrowd");
  487. return false;
  488. }
  489. if (recreate)
  490. {
  491. // Reconfigure the newly initialized crowd
  492. SetQueryFilterTypesAttr(queryFilterTypeConfiguration);
  493. SetObstacleAvoidanceTypesAttr(obstacleAvoidanceTypeConfiguration);
  494. // Re-add the existing crowd agents
  495. PODVector<CrowdAgent*> agents = GetAgents();
  496. for (unsigned i = 0; i < agents.Size(); ++i)
  497. {
  498. // Keep adding until the crowd cannot take it anymore
  499. if (agents[i]->AddAgentToCrowd(true) == -1)
  500. {
  501. LOGWARNINGF("CrowdManager: %d crowd agents orphaned", agents.Size() - i);
  502. break;
  503. }
  504. }
  505. }
  506. return true;
  507. }
  508. int CrowdManager::AddAgent(CrowdAgent* agent, const Vector3& pos)
  509. {
  510. if (!crowd_ || !navigationMesh_ || !agent)
  511. return -1;
  512. dtCrowdAgentParams params;
  513. params.userData = agent;
  514. if (agent->radius_ == 0.f)
  515. agent->radius_ = navigationMesh_->GetAgentRadius();
  516. if (agent->height_ == 0.f)
  517. agent->height_ = navigationMesh_->GetAgentHeight();
  518. return crowd_->addAgent(pos.Data(), &params);
  519. }
  520. void CrowdManager::RemoveAgent(CrowdAgent* agent)
  521. {
  522. if (!crowd_ || !agent)
  523. return;
  524. dtCrowdAgent* agt = crowd_->getEditableAgent(agent->GetAgentCrowdId());
  525. if (agt)
  526. agt->params.userData = 0;
  527. crowd_->removeAgent(agent->GetAgentCrowdId());
  528. }
  529. void CrowdManager::OnSceneSet(Scene* scene)
  530. {
  531. // Subscribe to the scene subsystem update, which will trigger the crowd update step, and grab a reference
  532. // to the scene's NavigationMesh
  533. if (scene)
  534. {
  535. if (scene != node_)
  536. {
  537. LOGERROR("CrowdManager is a scene component and should only be attached to the scene node");
  538. return;
  539. }
  540. SubscribeToEvent(scene, E_SCENESUBSYSTEMUPDATE, URHO3D_HANDLER(CrowdManager, HandleSceneSubsystemUpdate));
  541. // Attempt to auto discover a NavigationMesh component (or its derivative) under the scene node
  542. NavigationMesh* navMesh = scene->GetDerivedComponent<NavigationMesh>(true);
  543. if (navMesh)
  544. {
  545. navigationMesh_ = navMesh;
  546. navigationMeshId_ = navMesh->GetID();
  547. CreateCrowd();
  548. SubscribeToEvent(navMesh->GetNode(), E_NAVIGATION_MESH_REBUILT, URHO3D_HANDLER(CrowdManager, HandleNavMeshChanged));
  549. SubscribeToEvent(navMesh->GetNode(), E_COMPONENTREMOVED, URHO3D_HANDLER(CrowdManager, HandleNavMeshChanged));
  550. }
  551. }
  552. else
  553. {
  554. UnsubscribeFromEvent(E_SCENESUBSYSTEMUPDATE);
  555. UnsubscribeFromEvent(E_NAVIGATION_MESH_REBUILT);
  556. UnsubscribeFromEvent(E_COMPONENTREMOVED);
  557. navigationMesh_ = 0;
  558. }
  559. }
  560. void CrowdManager::Update(float delta)
  561. {
  562. assert(crowd_ && navigationMesh_);
  563. PROFILE(UpdateCrowd);
  564. crowd_->update(delta, 0);
  565. }
  566. const dtCrowdAgent* CrowdManager::GetDetourCrowdAgent(int agent) const
  567. {
  568. return crowd_ ? crowd_->getAgent(agent) : 0;
  569. }
  570. const dtQueryFilter* CrowdManager::GetDetourQueryFilter(unsigned queryFilterType) const
  571. {
  572. return crowd_ ? crowd_->getFilter(queryFilterType) : 0;
  573. }
  574. void CrowdManager::HandleSceneSubsystemUpdate(StringHash eventType, VariantMap& eventData)
  575. {
  576. // Perform update tick as long as the crowd is initialized and the associated navmesh has not been removed
  577. if (crowd_ && navigationMesh_)
  578. {
  579. using namespace SceneSubsystemUpdate;
  580. if (IsEnabledEffective())
  581. Update(eventData[P_TIMESTEP].GetFloat());
  582. }
  583. }
  584. void CrowdManager::HandleNavMeshChanged(StringHash eventType, VariantMap& eventData)
  585. {
  586. NavigationMesh* navMesh;
  587. if (eventType == E_NAVIGATION_MESH_REBUILT)
  588. {
  589. // The mesh being rebuilt may not have existed before
  590. navMesh = static_cast<NavigationMesh*>(eventData[NavigationMeshRebuilt::P_MESH].GetPtr());
  591. }
  592. else
  593. {
  594. // eventType == E_COMPONENTREMOVED
  595. navMesh = static_cast<NavigationMesh*>(eventData[ComponentRemoved::P_COMPONENT].GetPtr());
  596. // Only interested in navmesh component being used to initialized the crowd
  597. if (navMesh != navigationMesh_)
  598. return;
  599. // Since this is a component removed event, reset our own navmesh pointer
  600. navMesh = 0;
  601. }
  602. SetNavigationMesh(navMesh);
  603. }
  604. }