navPath.cpp 19 KB

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