navPath.cpp 19 KB

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