navPath.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 Daniel Buckmaster
  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
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell 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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "torqueRecast.h"
  23. #include "navPath.h"
  24. #include "duDebugDrawTorque.h"
  25. #include "console/consoleTypes.h"
  26. #include "console/engineAPI.h"
  27. #include "console/typeValidators.h"
  28. #include "math/mathTypes.h"
  29. #include "scene/sceneRenderState.h"
  30. #include "gfx/gfxDrawUtil.h"
  31. #include "renderInstance/renderPassManager.h"
  32. #include "gfx/primBuilder.h"
  33. #include "core/stream/bitStream.h"
  34. #include "math/mathIO.h"
  35. #include <DetourDebugDraw.h>
  36. #include <climits>
  37. extern bool gEditingMission;
  38. IMPLEMENT_CO_NETOBJECT_V1(NavPath);
  39. NavPath::NavPath() :
  40. mFrom(0.0f, 0.0f, 0.0f),
  41. mTo(0.0f, 0.0f, 0.0f)
  42. {
  43. mTypeMask |= MarkerObjectType;
  44. mMesh = NULL;
  45. mWaypoints = NULL;
  46. mFrom.set(0, 0, 0);
  47. mFromSet = false;
  48. mTo.set(0, 0, 0);
  49. mToSet = false;
  50. mLength = 0.0f;
  51. mCurIndex = -1;
  52. mIsLooping = false;
  53. mAutoUpdate = false;
  54. mIsSliced = false;
  55. mMaxIterations = 1;
  56. mAlwaysRender = false;
  57. mXray = false;
  58. mRenderSearch = false;
  59. mQuery = NULL;
  60. }
  61. NavPath::~NavPath()
  62. {
  63. dtFreeNavMeshQuery(mQuery);
  64. mQuery = NULL;
  65. }
  66. void NavPath::checkAutoUpdate()
  67. {
  68. EventManager *em = NavMesh::getEventManager();
  69. em->removeAll(this);
  70. if(mMesh)
  71. {
  72. if(mAutoUpdate)
  73. {
  74. em->subscribe(this, "NavMeshRemoved");
  75. em->subscribe(this, "NavMeshUpdate");
  76. em->subscribe(this, "NavMeshUpdateBox");
  77. em->subscribe(this, "NavMeshObstacleAdded");
  78. em->subscribe(this, "NavMeshObstacleRemoved");
  79. }
  80. }
  81. }
  82. bool NavPath::setProtectedMesh(void *obj, const char *index, const char *data)
  83. {
  84. NavPath *object = static_cast<NavPath*>(obj);
  85. if(Sim::findObject(data, object->mMesh))
  86. object->checkAutoUpdate();
  87. return true;
  88. }
  89. bool NavPath::setProtectedWaypoints(void *obj, const char *index, const char *data)
  90. {
  91. SimPath::Path *points = NULL;
  92. NavPath *object = static_cast<NavPath*>(obj);
  93. if(Sim::findObject(data, points))
  94. {
  95. object->mWaypoints = points;
  96. object->mIsLooping = points->isLooping();
  97. }
  98. else
  99. object->mWaypoints = NULL;
  100. return false;
  101. }
  102. bool NavPath::setProtectedAutoUpdate(void *obj, const char *index, const char *data)
  103. {
  104. NavPath *object = static_cast<NavPath*>(obj);
  105. object->mAutoUpdate = dAtob(data);
  106. object->checkAutoUpdate();
  107. return false;
  108. }
  109. bool NavPath::setProtectedFrom(void *obj, const char *index, const char *data)
  110. {
  111. NavPath *object = static_cast<NavPath*>(obj);
  112. if(dStrcmp(data, ""))
  113. {
  114. object->mFromSet = true;
  115. return true;
  116. }
  117. else
  118. {
  119. object->mFromSet = false;
  120. return false;
  121. }
  122. }
  123. bool NavPath::setProtectedTo(void *obj, const char *index, const char *data)
  124. {
  125. NavPath *object = static_cast<NavPath*>(obj);
  126. if(dStrcmp(data, ""))
  127. {
  128. object->mToSet = true;
  129. return true;
  130. }
  131. else
  132. {
  133. object->mToSet = false;
  134. return false;
  135. }
  136. }
  137. const char *NavPath::getProtectedFrom(void *obj, const char *data)
  138. {
  139. NavPath *object = static_cast<NavPath*>(obj);
  140. if(object->mFromSet)
  141. return data;
  142. else
  143. return StringTable->EmptyString();
  144. }
  145. const char *NavPath::getProtectedTo(void *obj, const char *data)
  146. {
  147. NavPath *object = static_cast<NavPath*>(obj);
  148. if(object->mToSet)
  149. return data;
  150. else
  151. return StringTable->EmptyString();
  152. }
  153. IRangeValidator ValidIterations(1, S32_MAX);
  154. void NavPath::initPersistFields()
  155. {
  156. addGroup("NavPath");
  157. addProtectedField("from", TypePoint3F, Offset(mFrom, NavPath),
  158. &setProtectedFrom, &getProtectedFrom,
  159. "World location this path starts at.");
  160. addProtectedField("to", TypePoint3F, Offset(mTo, NavPath),
  161. &setProtectedTo, &getProtectedTo,
  162. "World location this path should end at.");
  163. addProtectedField("mesh", TypeRealString, Offset(mMeshName, NavPath),
  164. &setProtectedMesh, &defaultProtectedGetFn,
  165. "Name of the NavMesh object this path travels within.");
  166. addProtectedField("waypoints", TYPEID<SimPath::Path>(), Offset(mWaypoints, NavPath),
  167. &setProtectedWaypoints, &defaultProtectedGetFn,
  168. "Path containing waypoints for this NavPath to visit.");
  169. addField("isLooping", TypeBool, Offset(mIsLooping, NavPath),
  170. "Does this path loop?");
  171. addField("isSliced", TypeBool, Offset(mIsSliced, NavPath),
  172. "Plan this path over multiple updates instead of all at once.");
  173. addFieldV("maxIterations", TypeS32, Offset(mMaxIterations, NavPath), &ValidIterations,
  174. "Maximum iterations of path planning this path does per tick.");
  175. addProtectedField("autoUpdate", TypeBool, Offset(mAutoUpdate, NavPath),
  176. &setProtectedAutoUpdate, &defaultProtectedGetFn,
  177. "If set, this path will automatically replan when its navigation mesh changes.");
  178. endGroup("NavPath");
  179. addGroup("Flags");
  180. addField("allowWalk", TypeBool, Offset(mLinkTypes.walk, NavPath),
  181. "Allow the path to use dry land.");
  182. addField("allowJump", TypeBool, Offset(mLinkTypes.jump, NavPath),
  183. "Allow the path to use jump links.");
  184. addField("allowDrop", TypeBool, Offset(mLinkTypes.drop, NavPath),
  185. "Allow the path to use drop links.");
  186. addField("allowSwim", TypeBool, Offset(mLinkTypes.swim, NavPath),
  187. "Allow the path to move in water.");
  188. addField("allowLedge", TypeBool, Offset(mLinkTypes.ledge, NavPath),
  189. "Allow the path to jump ledges.");
  190. addField("allowClimb", TypeBool, Offset(mLinkTypes.climb, NavPath),
  191. "Allow the path to use climb links.");
  192. addField("allowTeleport", TypeBool, Offset(mLinkTypes.teleport, NavPath),
  193. "Allow the path to use teleporters.");
  194. endGroup("Flags");
  195. addGroup("NavPath Render");
  196. addField("alwaysRender", TypeBool, Offset(mAlwaysRender, NavPath),
  197. "Render this NavPath even when not selected.");
  198. addField("xray", TypeBool, Offset(mXray, NavPath),
  199. "Render this NavPath through other objects.");
  200. addField("renderSearch", TypeBool, Offset(mRenderSearch, NavPath),
  201. "Render the closed list of this NavPath's search.");
  202. endGroup("NavPath Render");
  203. Parent::initPersistFields();
  204. }
  205. bool NavPath::onAdd()
  206. {
  207. if(!Parent::onAdd())
  208. return false;
  209. if(gEditingMission)
  210. mNetFlags.set(Ghostable);
  211. resize();
  212. addToScene();
  213. if(isServerObject())
  214. {
  215. mQuery = dtAllocNavMeshQuery();
  216. if(!mQuery)
  217. return false;
  218. checkAutoUpdate();
  219. if(!plan())
  220. setProcessTick(true);
  221. }
  222. return true;
  223. }
  224. void NavPath::onRemove()
  225. {
  226. Parent::onRemove();
  227. removeFromScene();
  228. }
  229. bool NavPath::init()
  230. {
  231. mStatus = DT_FAILURE;
  232. // Check that all the right data is provided.
  233. if(!mMesh || !mMesh->getNavMesh())
  234. return false;
  235. if(!(mFromSet && mToSet) && !(mWaypoints && mWaypoints->size()))
  236. return false;
  237. // Initialise our query.
  238. if(dtStatusFailed(mQuery->init(mMesh->getNavMesh(), MaxPathLen)))
  239. return false;
  240. mPoints.clear();
  241. mFlags.clear();
  242. mVisitPoints.clear();
  243. mLength = 0.0f;
  244. if(isServerObject())
  245. setMaskBits(PathMask);
  246. // Add points we need to visit in reverse order.
  247. if(mWaypoints && mWaypoints->size())
  248. {
  249. if(mIsLooping && mFromSet)
  250. mVisitPoints.push_back(mFrom);
  251. if(mToSet)
  252. mVisitPoints.push_front(mTo);
  253. for(S32 i = mWaypoints->size() - 1; i >= 0; i--)
  254. {
  255. SceneObject *s = dynamic_cast<SceneObject*>(mWaypoints->at(i));
  256. if(s)
  257. {
  258. mVisitPoints.push_back(s->getPosition());
  259. // This is potentially slow, but safe.
  260. if(!i && mIsLooping && !mFromSet)
  261. mVisitPoints.push_front(s->getPosition());
  262. }
  263. }
  264. if(mFromSet)
  265. mVisitPoints.push_back(mFrom);
  266. }
  267. else
  268. {
  269. if(mIsLooping)
  270. mVisitPoints.push_back(mFrom);
  271. mVisitPoints.push_back(mTo);
  272. mVisitPoints.push_back(mFrom);
  273. }
  274. return true;
  275. }
  276. void NavPath::resize()
  277. {
  278. if(!mPoints.size())
  279. {
  280. mObjBox.set(Point3F(-0.5f, -0.5f, -0.5f),
  281. Point3F( 0.5f, 0.5f, 0.5f));
  282. resetWorldBox();
  283. setTransform(MatrixF(true));
  284. return;
  285. }
  286. Point3F max(mPoints[0]), min(mPoints[0]), pos(0.0f);
  287. for(U32 i = 1; i < mPoints.size(); i++)
  288. {
  289. Point3F p = mPoints[i];
  290. max.x = getMax(max.x, p.x);
  291. max.y = getMax(max.y, p.y);
  292. max.z = getMax(max.z, p.z);
  293. min.x = getMin(min.x, p.x);
  294. min.y = getMin(min.y, p.y);
  295. min.z = getMin(min.z, p.z);
  296. pos += p;
  297. }
  298. pos /= mPoints.size();
  299. min -= Point3F(0.5f, 0.5f, 0.5f);
  300. max += Point3F(0.5f, 0.5f, 0.5f);
  301. mObjBox.set(min - pos, max - pos);
  302. MatrixF mat = Parent::getTransform();
  303. mat.setPosition(pos);
  304. Parent::setTransform(mat);
  305. }
  306. bool NavPath::plan()
  307. {
  308. PROFILE_SCOPE(NavPath_plan);
  309. // Initialise filter.
  310. mFilter.setIncludeFlags(mLinkTypes.getFlags());
  311. // Initialise query and visit locations.
  312. if(!init())
  313. return false;
  314. if(mIsSliced)
  315. return planSliced();
  316. else
  317. return planInstant();
  318. }
  319. bool NavPath::planSliced()
  320. {
  321. bool visited = visitNext();
  322. if(visited)
  323. setProcessTick(true);
  324. return visited;
  325. }
  326. bool NavPath::planInstant()
  327. {
  328. setProcessTick(false);
  329. visitNext();
  330. S32 store = mMaxIterations;
  331. mMaxIterations = INT_MAX;
  332. while(update());
  333. mMaxIterations = store;
  334. return finalise();
  335. }
  336. bool NavPath::visitNext()
  337. {
  338. U32 s = mVisitPoints.size();
  339. if(s < 2)
  340. return false;
  341. // Current leg of journey.
  342. Point3F &start = mVisitPoints[s-1];
  343. Point3F &end = mVisitPoints[s-2];
  344. // Drop to height of statics.
  345. RayInfo info;
  346. if(getContainer()->castRay(start, start - Point3F(0, 0, mMesh->mWalkableHeight * 2.0f), StaticObjectType, &info))
  347. start = info.point;
  348. if(getContainer()->castRay(end + Point3F(0, 0, 0.1f), end - Point3F(0, 0, mMesh->mWalkableHeight * 2.0f), StaticObjectType, &info))
  349. end = info.point;
  350. // Convert to Detour-friendly coordinates and data structures.
  351. F32 from[] = {start.x, start.z, -start.y};
  352. F32 to[] = {end.x, end.z, -end.y};
  353. F32 extx = mMesh->mWalkableRadius * 4.0f;
  354. F32 extz = mMesh->mWalkableHeight;
  355. F32 extents[] = {extx, extz, extx};
  356. dtPolyRef startRef, endRef;
  357. if(dtStatusFailed(mQuery->findNearestPoly(from, extents, &mFilter, &startRef, NULL)) || !startRef)
  358. {
  359. //Con::errorf("No NavMesh polygon near visit point (%g, %g, %g) of NavPath %s",
  360. //start.x, start.y, start.z, getIdString());
  361. return false;
  362. }
  363. if(dtStatusFailed(mQuery->findNearestPoly(to, extents, &mFilter, &endRef, NULL)) || !endRef)
  364. {
  365. //Con::errorf("No NavMesh polygon near visit point (%g, %g, %g) of NavPath %s",
  366. //end.x, end.y, end.z, getIdString());
  367. return false;
  368. }
  369. // Init sliced pathfind.
  370. mStatus = mQuery->initSlicedFindPath(startRef, endRef, from, to, &mFilter);
  371. if(dtStatusFailed(mStatus))
  372. return false;
  373. return true;
  374. }
  375. bool NavPath::update()
  376. {
  377. PROFILE_SCOPE(NavPath_update);
  378. if(dtStatusInProgress(mStatus))
  379. mStatus = mQuery->updateSlicedFindPath(mMaxIterations, NULL);
  380. if(dtStatusSucceed(mStatus))
  381. {
  382. // Add points from this leg.
  383. dtPolyRef path[MaxPathLen];
  384. S32 pathLen;
  385. mStatus = mQuery->finalizeSlicedFindPath(path, &pathLen, MaxPathLen);
  386. if(dtStatusSucceed(mStatus) && pathLen)
  387. {
  388. F32 straightPath[MaxPathLen * 3];
  389. S32 straightPathLen;
  390. dtPolyRef straightPathPolys[MaxPathLen];
  391. U8 straightPathFlags[MaxPathLen];
  392. U32 s = mVisitPoints.size();
  393. Point3F start = mVisitPoints[s-1];
  394. Point3F end = mVisitPoints[s-2];
  395. F32 from[] = {start.x, start.z, -start.y};
  396. F32 to[] = {end.x, end.z, -end.y};
  397. mQuery->findStraightPath(from, to, path, pathLen,
  398. straightPath, straightPathFlags,
  399. straightPathPolys, &straightPathLen, MaxPathLen);
  400. s = mPoints.size();
  401. mPoints.increment(straightPathLen);
  402. mFlags.increment(straightPathLen);
  403. for(U32 i = 0; i < straightPathLen; i++)
  404. {
  405. F32 *f = straightPath + i * 3;
  406. mPoints[s + i] = RCtoDTS(f);
  407. mMesh->getNavMesh()->getPolyFlags(straightPathPolys[i], &mFlags[s + i]);
  408. // Add to length
  409. if(s > 0 || i > 0)
  410. mLength += (mPoints[s+i] - mPoints[s+i-1]).len();
  411. }
  412. if(isServerObject())
  413. setMaskBits(PathMask);
  414. }
  415. else
  416. return false;
  417. // Check to see where we still need to visit.
  418. if(mVisitPoints.size() > 1)
  419. {
  420. //Next leg of the journey.
  421. mVisitPoints.pop_back();
  422. return visitNext();
  423. }
  424. else
  425. {
  426. // Finished!
  427. return false;
  428. }
  429. }
  430. else if(dtStatusFailed(mStatus))
  431. {
  432. // Something went wrong in planning.
  433. return false;
  434. }
  435. return true;
  436. }
  437. bool NavPath::finalise()
  438. {
  439. setProcessTick(false);
  440. resize();
  441. return success();
  442. }
  443. void NavPath::processTick(const Move *move)
  444. {
  445. PROFILE_SCOPE(NavPath_processTick);
  446. if(!mMesh)
  447. if(Sim::findObject(mMeshName.c_str(), mMesh))
  448. plan();
  449. if(dtStatusInProgress(mStatus))
  450. update();
  451. }
  452. Point3F NavPath::getNode(S32 idx) const
  453. {
  454. if(idx < size() && idx >= 0)
  455. return mPoints[idx];
  456. return Point3F(0,0,0);
  457. }
  458. U16 NavPath::getFlags(S32 idx) const
  459. {
  460. if(idx < size() && idx >= 0)
  461. return mFlags[idx];
  462. return 0;
  463. }
  464. S32 NavPath::size() const
  465. {
  466. return mPoints.size();
  467. }
  468. void NavPath::onEditorEnable()
  469. {
  470. mNetFlags.set(Ghostable);
  471. }
  472. void NavPath::onEditorDisable()
  473. {
  474. mNetFlags.clear(Ghostable);
  475. }
  476. void NavPath::inspectPostApply()
  477. {
  478. plan();
  479. }
  480. void NavPath::onDeleteNotify(SimObject *obj)
  481. {
  482. if(obj == (SimObject*)mMesh)
  483. {
  484. mMesh = NULL;
  485. plan();
  486. }
  487. }
  488. void NavPath::prepRenderImage(SceneRenderState *state)
  489. {
  490. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  491. ri->renderDelegate.bind(this, &NavPath::renderSimple);
  492. ri->type = RenderPassManager::RIT_Editor;
  493. ri->translucentSort = true;
  494. ri->defaultKey = 1;
  495. state->getRenderPass()->addInst(ri);
  496. }
  497. void NavPath::renderSimple(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat)
  498. {
  499. if(overrideMat)
  500. return;
  501. if(state->isReflectPass() || !(isSelected() || mAlwaysRender))
  502. return;
  503. GFXDrawUtil *drawer = GFX->getDrawUtil();
  504. GFXStateBlockDesc desc;
  505. desc.setZReadWrite(true, false);
  506. desc.setBlend(true);
  507. desc.setCullMode(GFXCullNone);
  508. if(isSelected())
  509. {
  510. drawer->drawCube(desc, getWorldBox(), ColorI(136, 255, 228, 5));
  511. desc.setFillModeWireframe();
  512. drawer->drawCube(desc, getWorldBox(), ColorI::BLACK);
  513. }
  514. desc.setZReadWrite(!mXray, false);
  515. ColorI pathColour(255, 0, 255);
  516. if(!mIsLooping)
  517. {
  518. desc.setFillModeSolid();
  519. if(mFromSet) drawer->drawCube(desc, Point3F(0.2f, 0.2f, 0.2f), mFrom, pathColour);
  520. if(mToSet) drawer->drawCube(desc, Point3F(0.2f, 0.2f, 0.2f), mTo, pathColour);
  521. }
  522. GFXStateBlockRef sb = GFX->createStateBlock(desc);
  523. GFX->setStateBlock(sb);
  524. PrimBuild::color3i(pathColour.red, pathColour.green, pathColour.blue);
  525. PrimBuild::begin(GFXLineStrip, mPoints.size());
  526. for (U32 i = 0; i < mPoints.size(); i++)
  527. PrimBuild::vertex3fv(mPoints[i]);
  528. PrimBuild::end();
  529. if(mRenderSearch && getServerObject())
  530. {
  531. NavPath *np = static_cast<NavPath*>(getServerObject());
  532. if(np->mQuery && !dtStatusSucceed(np->mStatus))
  533. {
  534. duDebugDrawTorque dd;
  535. dd.overrideColor(duRGBA(250, 20, 20, 255));
  536. duDebugDrawNavMeshNodes(&dd, *np->mQuery);
  537. dd.render();
  538. }
  539. }
  540. }
  541. U32 NavPath::packUpdate(NetConnection *conn, U32 mask, BitStream *stream)
  542. {
  543. U32 retMask = Parent::packUpdate(conn, mask, stream);
  544. stream->writeFlag(mIsLooping);
  545. stream->writeFlag(mAlwaysRender);
  546. stream->writeFlag(mXray);
  547. stream->writeFlag(mRenderSearch);
  548. if(stream->writeFlag(mFromSet))
  549. mathWrite(*stream, mFrom);
  550. if(stream->writeFlag(mToSet))
  551. mathWrite(*stream, mTo);
  552. if(stream->writeFlag(mask & PathMask))
  553. {
  554. stream->writeInt(mPoints.size(), 32);
  555. for(U32 i = 0; i < mPoints.size(); i++)
  556. {
  557. mathWrite(*stream, mPoints[i]);
  558. stream->writeInt(mFlags[i], 16);
  559. }
  560. }
  561. return retMask;
  562. }
  563. void NavPath::unpackUpdate(NetConnection *conn, BitStream *stream)
  564. {
  565. Parent::unpackUpdate(conn, stream);
  566. mIsLooping = stream->readFlag();
  567. mAlwaysRender = stream->readFlag();
  568. mXray = stream->readFlag();
  569. mRenderSearch = stream->readFlag();
  570. if((mFromSet = stream->readFlag()) == true)
  571. mathRead(*stream, &mFrom);
  572. if((mToSet = stream->readFlag()) == true)
  573. mathRead(*stream, &mTo);
  574. if(stream->readFlag())
  575. {
  576. mPoints.clear();
  577. mFlags.clear();
  578. mPoints.setSize(stream->readInt(32));
  579. mFlags.setSize(mPoints.size());
  580. for(U32 i = 0; i < mPoints.size(); i++)
  581. {
  582. Point3F p;
  583. mathRead(*stream, &p);
  584. mPoints[i] = p;
  585. mFlags[i] = stream->readInt(16);
  586. }
  587. resize();
  588. }
  589. }
  590. DefineEngineMethod(NavPath, plan, bool, (),,
  591. "@brief Find a path using the already-specified path properties.")
  592. {
  593. return object->plan();
  594. }
  595. DefineEngineMethod(NavPath, onNavMeshUpdate, void, (const char *data),,
  596. "@brief Callback when this path's NavMesh is loaded or rebuilt.")
  597. {
  598. if(object->mMesh && !dStrcmp(data, object->mMesh->getIdString()))
  599. object->plan();
  600. }
  601. DefineEngineMethod(NavPath, onNavMeshUpdateBox, void, (const char *data),,
  602. "@brief Callback when a particular area in this path's NavMesh is rebuilt.")
  603. {
  604. String s(data);
  605. U32 space = s.find(' ');
  606. if(space != String::NPos)
  607. {
  608. String id = s.substr(0, space);
  609. if(!object->mMesh || id.compare(object->mMesh->getIdString()))
  610. return;
  611. String boxstr = s.substr(space + 1);
  612. Box3F box;
  613. castConsoleTypeFromString(box, boxstr.c_str());
  614. if(object->getWorldBox().isOverlapped(box))
  615. object->plan();
  616. }
  617. }
  618. DefineEngineMethod(NavPath, size, S32, (),,
  619. "@brief Return the number of nodes in this path.")
  620. {
  621. return object->size();
  622. }
  623. DefineEngineMethod(NavPath, getNode, Point3F, (S32 idx),,
  624. "@brief Get a specified node along the path.")
  625. {
  626. return object->getNode(idx);
  627. }
  628. DefineEngineMethod(NavPath, getFlags, S32, (S32 idx),,
  629. "@brief Get a specified node along the path.")
  630. {
  631. return (S32)object->getFlags(idx);
  632. }
  633. DefineEngineMethod(NavPath, getLength, F32, (),,
  634. "@brief Get the length of this path.")
  635. {
  636. return object->getLength();
  637. }