navPath.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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->insert("");
  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->insert("");
  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. // Initialise filter.
  309. mFilter.setIncludeFlags(mLinkTypes.getFlags());
  310. // Initialise query and visit locations.
  311. if(!init())
  312. return false;
  313. if(mIsSliced)
  314. return planSliced();
  315. else
  316. return planInstant();
  317. }
  318. bool NavPath::planSliced()
  319. {
  320. bool visited = visitNext();
  321. if(visited)
  322. setProcessTick(true);
  323. return visited;
  324. }
  325. bool NavPath::planInstant()
  326. {
  327. setProcessTick(false);
  328. visitNext();
  329. S32 store = mMaxIterations;
  330. mMaxIterations = INT_MAX;
  331. while(update());
  332. mMaxIterations = store;
  333. return finalise();
  334. }
  335. bool NavPath::visitNext()
  336. {
  337. U32 s = mVisitPoints.size();
  338. if(s < 2)
  339. return false;
  340. // Current leg of journey.
  341. Point3F &start = mVisitPoints[s-1];
  342. Point3F &end = mVisitPoints[s-2];
  343. // Drop to height of statics.
  344. RayInfo info;
  345. if(getContainer()->castRay(start, start - Point3F(0, 0, mMesh->mWalkableHeight * 2.0f), StaticObjectType, &info))
  346. start = info.point;
  347. if(getContainer()->castRay(end + Point3F(0, 0, 0.1f), end - Point3F(0, 0, mMesh->mWalkableHeight * 2.0f), StaticObjectType, &info))
  348. end = info.point;
  349. // Convert to Detour-friendly coordinates and data structures.
  350. F32 from[] = {start.x, start.z, -start.y};
  351. F32 to[] = {end.x, end.z, -end.y};
  352. F32 extx = mMesh->mWalkableRadius * 4.0f;
  353. F32 extz = mMesh->mWalkableHeight;
  354. F32 extents[] = {extx, extz, extx};
  355. dtPolyRef startRef, endRef;
  356. if(dtStatusFailed(mQuery->findNearestPoly(from, extents, &mFilter, &startRef, NULL)) || !startRef)
  357. {
  358. Con::errorf("No NavMesh polygon near visit point (%g, %g, %g) of NavPath %s",
  359. start.x, start.y, start.z, getIdString());
  360. return false;
  361. }
  362. if(dtStatusFailed(mQuery->findNearestPoly(to, extents, &mFilter, &endRef, NULL)) || !endRef)
  363. {
  364. Con::errorf("No NavMesh polygon near visit point (%g, %g, %g) of NavPath %s",
  365. end.x, end.y, end.z, getIdString());
  366. return false;
  367. }
  368. // Init sliced pathfind.
  369. mStatus = mQuery->initSlicedFindPath(startRef, endRef, from, to, &mFilter);
  370. if(dtStatusFailed(mStatus))
  371. return false;
  372. return true;
  373. }
  374. bool NavPath::update()
  375. {
  376. if(dtStatusInProgress(mStatus))
  377. mStatus = mQuery->updateSlicedFindPath(mMaxIterations, NULL);
  378. if(dtStatusSucceed(mStatus))
  379. {
  380. // Add points from this leg.
  381. dtPolyRef path[MaxPathLen];
  382. S32 pathLen;
  383. mStatus = mQuery->finalizeSlicedFindPath(path, &pathLen, MaxPathLen);
  384. if(dtStatusSucceed(mStatus) && pathLen)
  385. {
  386. F32 straightPath[MaxPathLen * 3];
  387. S32 straightPathLen;
  388. dtPolyRef straightPathPolys[MaxPathLen];
  389. U8 straightPathFlags[MaxPathLen];
  390. U32 s = mVisitPoints.size();
  391. Point3F start = mVisitPoints[s-1];
  392. Point3F end = mVisitPoints[s-2];
  393. F32 from[] = {start.x, start.z, -start.y};
  394. F32 to[] = {end.x, end.z, -end.y};
  395. mQuery->findStraightPath(from, to, path, pathLen,
  396. straightPath, straightPathFlags,
  397. straightPathPolys, &straightPathLen, MaxPathLen);
  398. s = mPoints.size();
  399. mPoints.increment(straightPathLen);
  400. mFlags.increment(straightPathLen);
  401. for(U32 i = 0; i < straightPathLen; i++)
  402. {
  403. F32 *f = straightPath + i * 3;
  404. mPoints[s + i] = RCtoDTS(f);
  405. mMesh->getNavMesh()->getPolyFlags(straightPathPolys[i], &mFlags[s + i]);
  406. // Add to length
  407. if(s > 0 || i > 0)
  408. mLength += (mPoints[s+i] - mPoints[s+i-1]).len();
  409. }
  410. if(isServerObject())
  411. setMaskBits(PathMask);
  412. }
  413. else
  414. return false;
  415. // Check to see where we still need to visit.
  416. if(mVisitPoints.size() > 1)
  417. {
  418. //Next leg of the journey.
  419. mVisitPoints.pop_back();
  420. return visitNext();
  421. }
  422. else
  423. {
  424. // Finished!
  425. return false;
  426. }
  427. }
  428. else if(dtStatusFailed(mStatus))
  429. {
  430. // Something went wrong in planning.
  431. return false;
  432. }
  433. return true;
  434. }
  435. bool NavPath::finalise()
  436. {
  437. setProcessTick(false);
  438. resize();
  439. return success();
  440. }
  441. void NavPath::processTick(const Move *move)
  442. {
  443. if(!mMesh)
  444. if(Sim::findObject(mMeshName.c_str(), mMesh))
  445. plan();
  446. if(dtStatusInProgress(mStatus))
  447. update();
  448. }
  449. Point3F NavPath::getNode(S32 idx) const
  450. {
  451. if(idx < size() && idx >= 0)
  452. return mPoints[idx];
  453. return Point3F(0,0,0);
  454. }
  455. U16 NavPath::getFlags(S32 idx) const
  456. {
  457. if(idx < size() && idx >= 0)
  458. return mFlags[idx];
  459. return 0;
  460. }
  461. S32 NavPath::size() const
  462. {
  463. return mPoints.size();
  464. }
  465. void NavPath::onEditorEnable()
  466. {
  467. mNetFlags.set(Ghostable);
  468. }
  469. void NavPath::onEditorDisable()
  470. {
  471. mNetFlags.clear(Ghostable);
  472. }
  473. void NavPath::inspectPostApply()
  474. {
  475. plan();
  476. }
  477. void NavPath::onDeleteNotify(SimObject *obj)
  478. {
  479. if(obj == (SimObject*)mMesh)
  480. {
  481. mMesh = NULL;
  482. plan();
  483. }
  484. }
  485. void NavPath::prepRenderImage(SceneRenderState *state)
  486. {
  487. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  488. ri->renderDelegate.bind(this, &NavPath::renderSimple);
  489. ri->type = RenderPassManager::RIT_Editor;
  490. ri->translucentSort = true;
  491. ri->defaultKey = 1;
  492. state->getRenderPass()->addInst(ri);
  493. }
  494. void NavPath::renderSimple(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat)
  495. {
  496. if(overrideMat)
  497. return;
  498. if(state->isReflectPass() || !(isSelected() || mAlwaysRender))
  499. return;
  500. GFXDrawUtil *drawer = GFX->getDrawUtil();
  501. GFXStateBlockDesc desc;
  502. desc.setZReadWrite(true, false);
  503. desc.setBlend(true);
  504. desc.setCullMode(GFXCullNone);
  505. if(isSelected())
  506. {
  507. drawer->drawCube(desc, getWorldBox(), ColorI(136, 255, 228, 5));
  508. desc.setFillModeWireframe();
  509. drawer->drawCube(desc, getWorldBox(), ColorI::BLACK);
  510. }
  511. desc.setZReadWrite(!mXray, false);
  512. ColorI pathColour(255, 0, 255);
  513. if(!mIsLooping)
  514. {
  515. desc.setFillModeSolid();
  516. if(mFromSet) drawer->drawCube(desc, Point3F(0.2f, 0.2f, 0.2f), mFrom, pathColour);
  517. if(mToSet) drawer->drawCube(desc, Point3F(0.2f, 0.2f, 0.2f), mTo, pathColour);
  518. }
  519. GFXStateBlockRef sb = GFX->createStateBlock(desc);
  520. GFX->setStateBlock(sb);
  521. PrimBuild::color3i(pathColour.red, pathColour.green, pathColour.blue);
  522. PrimBuild::begin(GFXLineStrip, mPoints.size());
  523. for (U32 i = 0; i < mPoints.size(); i++)
  524. PrimBuild::vertex3fv(mPoints[i]);
  525. PrimBuild::end();
  526. if(mRenderSearch && getServerObject())
  527. {
  528. NavPath *np = static_cast<NavPath*>(getServerObject());
  529. if(np->mQuery && !dtStatusSucceed(np->mStatus))
  530. {
  531. duDebugDrawTorque dd;
  532. dd.overrideColor(duRGBA(250, 20, 20, 255));
  533. duDebugDrawNavMeshNodes(&dd, *np->mQuery);
  534. dd.render();
  535. }
  536. }
  537. }
  538. U32 NavPath::packUpdate(NetConnection *conn, U32 mask, BitStream *stream)
  539. {
  540. U32 retMask = Parent::packUpdate(conn, mask, stream);
  541. stream->writeFlag(mIsLooping);
  542. stream->writeFlag(mAlwaysRender);
  543. stream->writeFlag(mXray);
  544. stream->writeFlag(mRenderSearch);
  545. if(stream->writeFlag(mFromSet))
  546. mathWrite(*stream, mFrom);
  547. if(stream->writeFlag(mToSet))
  548. mathWrite(*stream, mTo);
  549. if(stream->writeFlag(mask & PathMask))
  550. {
  551. stream->writeInt(mPoints.size(), 32);
  552. for(U32 i = 0; i < mPoints.size(); i++)
  553. {
  554. mathWrite(*stream, mPoints[i]);
  555. stream->writeInt(mFlags[i], 16);
  556. }
  557. }
  558. return retMask;
  559. }
  560. void NavPath::unpackUpdate(NetConnection *conn, BitStream *stream)
  561. {
  562. Parent::unpackUpdate(conn, stream);
  563. mIsLooping = stream->readFlag();
  564. mAlwaysRender = stream->readFlag();
  565. mXray = stream->readFlag();
  566. mRenderSearch = stream->readFlag();
  567. if((mFromSet = stream->readFlag()) == true)
  568. mathRead(*stream, &mFrom);
  569. if((mToSet = stream->readFlag()) == true)
  570. mathRead(*stream, &mTo);
  571. if(stream->readFlag())
  572. {
  573. mPoints.clear();
  574. mFlags.clear();
  575. mPoints.setSize(stream->readInt(32));
  576. mFlags.setSize(mPoints.size());
  577. for(U32 i = 0; i < mPoints.size(); i++)
  578. {
  579. Point3F p;
  580. mathRead(*stream, &p);
  581. mPoints[i] = p;
  582. mFlags[i] = stream->readInt(16);
  583. }
  584. resize();
  585. }
  586. }
  587. DefineEngineMethod(NavPath, plan, bool, (),,
  588. "@brief Find a path using the already-specified path properties.")
  589. {
  590. return object->plan();
  591. }
  592. DefineEngineMethod(NavPath, onNavMeshUpdate, void, (const char *data),,
  593. "@brief Callback when this path's NavMesh is loaded or rebuilt.")
  594. {
  595. if(object->mMesh && !dStrcmp(data, object->mMesh->getIdString()))
  596. object->plan();
  597. }
  598. DefineEngineMethod(NavPath, onNavMeshUpdateBox, void, (const char *data),,
  599. "@brief Callback when a particular area in this path's NavMesh is rebuilt.")
  600. {
  601. String s(data);
  602. U32 space = s.find(' ');
  603. if(space != String::NPos)
  604. {
  605. String id = s.substr(0, space);
  606. if(!object->mMesh || id.compare(object->mMesh->getIdString()))
  607. return;
  608. String boxstr = s.substr(space + 1);
  609. Box3F box;
  610. castConsoleTypeFromString(box, boxstr.c_str());
  611. if(object->getWorldBox().isOverlapped(box))
  612. object->plan();
  613. }
  614. }
  615. DefineEngineMethod(NavPath, size, S32, (),,
  616. "@brief Return the number of nodes in this path.")
  617. {
  618. return object->size();
  619. }
  620. DefineEngineMethod(NavPath, getNode, Point3F, (S32 idx),,
  621. "@brief Get a specified node along the path.")
  622. {
  623. return object->getNode(idx);
  624. }
  625. DefineEngineMethod(NavPath, getFlags, S32, (S32 idx),,
  626. "@brief Get a specified node along the path.")
  627. {
  628. return (S32)object->getFlags(idx);
  629. }
  630. DefineEngineMethod(NavPath, getLength, F32, (),,
  631. "@brief Get the length of this path.")
  632. {
  633. return object->getLength();
  634. }