navPath.cpp 19 KB

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