navPath.cpp 19 KB

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