CPathfinder.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. #include "StdInc.h"
  2. #include "CPathfinder.h"
  3. #include "CHeroHandler.h"
  4. #include "mapping/CMap.h"
  5. #include "CGameState.h"
  6. #include "mapObjects/CGHeroInstance.h"
  7. #include "GameConstants.h"
  8. #include "CStopWatch.h"
  9. /*
  10. * CPathfinder.cpp, part of VCMI engine
  11. *
  12. * Authors: listed in file AUTHORS in main folder
  13. *
  14. * License: GNU General Public License v2.0 or later
  15. * Full text of license available in license.txt file, in main folder
  16. *
  17. */
  18. CPathfinder::PathfinderOptions::PathfinderOptions()
  19. {
  20. useFlying = false;
  21. useWaterWalking = false;
  22. useEmbarkAndDisembark = true;
  23. useTeleportTwoWay = true;
  24. useTeleportOneWay = true;
  25. useTeleportOneWayRandom = false;
  26. useTeleportWhirlpool = false;
  27. lightweightFlyingMode = false;
  28. }
  29. CPathfinder::CPathfinder(CPathsInfo &_out, CGameState *_gs, const CGHeroInstance *_hero)
  30. : CGameInfoCallback(_gs, boost::optional<PlayerColor>()), out(_out), hero(_hero)
  31. {
  32. assert(hero);
  33. assert(hero == getHero(hero->id));
  34. out.hero = hero;
  35. out.hpos = hero->getPosition(false);
  36. if(!gs->map->isInTheMap(out.hpos)/* || !gs->map->isInTheMap(dest)*/) //check input
  37. {
  38. logGlobal->errorStream() << "CGameState::calculatePaths: Hero outside the gs->map? How dare you...";
  39. throw std::runtime_error("Wrong checksum");
  40. }
  41. if(hero->canFly())
  42. options.useFlying = true;
  43. if(hero->canWalkOnSea())
  44. options.useWaterWalking = true;
  45. if(CGWhirlpool::isProtected(hero))
  46. options.useTeleportWhirlpool = true;
  47. initializeGraph();
  48. neighbours.reserve(16);
  49. }
  50. void CPathfinder::calculatePaths()
  51. {
  52. int maxMovePointsLand = hero->maxMovePoints(true);
  53. int maxMovePointsWater = hero->maxMovePoints(false);
  54. auto maxMovePoints = [&](CGPathNode *cp) -> int
  55. {
  56. return cp->land ? maxMovePointsLand : maxMovePointsWater;
  57. };
  58. auto isBetterWay = [&](int remains, int turn) -> bool
  59. {
  60. if(dp->turns == 0xff) //we haven't been here before
  61. return true;
  62. else if(dp->turns > turn)
  63. return true;
  64. else if(dp->turns >= turn && dp->moveRemains < remains) //this route is faster
  65. return true;
  66. return false;
  67. };
  68. //logGlobal->infoStream() << boost::format("Calculating paths for hero %s (adress %d) of player %d") % hero->name % hero % hero->tempOwner;
  69. //initial tile - set cost on 0 and add to the queue
  70. CGPathNode *initialNode = out.getNode(out.hpos, hero->boat ? EPathfindingLayer::SAIL : EPathfindingLayer::LAND);
  71. initialNode->turns = 0;
  72. initialNode->moveRemains = hero->movement;
  73. pq.push(initialNode);
  74. while(!pq.empty())
  75. {
  76. cp = pq.top();
  77. pq.pop();
  78. cp->locked = true;
  79. int movement = cp->moveRemains, turn = cp->turns;
  80. if(!movement)
  81. {
  82. movement = maxMovePoints(cp);
  83. turn++;
  84. }
  85. //add accessible neighbouring nodes to the queue
  86. addNeighbours(cp->coord);
  87. for(auto & neighbour : neighbours)
  88. {
  89. dt = &gs->map->getTile(neighbour);
  90. for(EPathfindingLayer i = EPathfindingLayer::LAND; i <= EPathfindingLayer::AIR; i.advance(1))
  91. {
  92. dp = out.getNode(neighbour, i);
  93. if(dp->accessible == CGPathNode::NOT_SET)
  94. continue;
  95. if(dp->locked)
  96. continue;
  97. if(cp->layer != i && !isLayerTransitionPossible())
  98. continue;
  99. destAction = CGPathNode::UNKNOWN;
  100. if(!isMovementToDestPossible())
  101. continue;
  102. int cost = gs->getMovementCost(hero, cp->coord, dp->coord, movement);
  103. int remains = movement - cost;
  104. if(destAction == CGPathNode::EMBARK || destAction == CGPathNode::DISEMBARK)
  105. {
  106. remains = hero->movementPointsAfterEmbark(movement, cost, destAction - 1);
  107. cost = movement - remains;
  108. }
  109. int turnAtNextTile = turn;
  110. if(remains < 0)
  111. {
  112. //occurs rarely, when hero with low movepoints tries to leave the road
  113. turnAtNextTile++;
  114. int moveAtNextTile = maxMovePoints(cp);
  115. cost = gs->getMovementCost(hero, cp->coord, dp->coord, moveAtNextTile); //cost must be updated, movement points changed :(
  116. remains = moveAtNextTile - cost;
  117. }
  118. if(isBetterWay(remains, turnAtNextTile))
  119. {
  120. assert(dp != cp->theNodeBefore); //two tiles can't point to each other
  121. dp->moveRemains = remains;
  122. dp->turns = turnAtNextTile;
  123. dp->theNodeBefore = cp;
  124. dp->action = destAction;
  125. if(isMovementAfterDestPossible())
  126. pq.push(dp);
  127. }
  128. }
  129. } //neighbours loop
  130. //just add all passable teleport exits
  131. if(sTileObj && canVisitObject())
  132. {
  133. addTeleportExits();
  134. for(auto & neighbour : neighbours)
  135. {
  136. dp = out.getNode(neighbour, cp->layer);
  137. if(dp->locked)
  138. continue;
  139. if(isBetterWay(movement, turn))
  140. {
  141. dp->moveRemains = movement;
  142. dp->turns = turn;
  143. dp->theNodeBefore = cp;
  144. dp->action = CGPathNode::NORMAL;
  145. pq.push(dp);
  146. }
  147. }
  148. }
  149. } //queue loop
  150. }
  151. void CPathfinder::addNeighbours(const int3 &coord)
  152. {
  153. neighbours.clear();
  154. ct = &gs->map->getTile(coord);
  155. std::vector<int3> tiles;
  156. gs->getNeighbours(*ct, coord, tiles, boost::logic::indeterminate, !cp->land);
  157. sTileObj = ct->topVisitableObj(coord == out.hpos);
  158. if(canVisitObject())
  159. {
  160. if(sTileObj)
  161. {
  162. for(int3 tile: tiles)
  163. {
  164. if(canMoveBetween(tile, sTileObj->visitablePos()))
  165. neighbours.push_back(tile);
  166. }
  167. }
  168. else
  169. vstd::concatenate(neighbours, tiles);
  170. }
  171. else
  172. vstd::concatenate(neighbours, tiles);
  173. }
  174. void CPathfinder::addTeleportExits(bool noTeleportExcludes)
  175. {
  176. assert(sTileObj);
  177. neighbours.clear();
  178. auto isAllowedTeleportEntrance = [&](const CGTeleport * obj) -> bool
  179. {
  180. if(!gs->isTeleportEntrancePassable(obj, hero->tempOwner))
  181. return false;
  182. if(noTeleportExcludes)
  183. return true;
  184. auto whirlpool = dynamic_cast<const CGWhirlpool *>(obj);
  185. if(whirlpool)
  186. {
  187. if(addTeleportWhirlpool(whirlpool))
  188. return true;
  189. }
  190. else if(addTeleportTwoWay(obj) || addTeleportOneWay(obj) || addTeleportOneWayRandom(obj))
  191. return true;
  192. return false;
  193. };
  194. const CGTeleport *sTileTeleport = dynamic_cast<const CGTeleport *>(sTileObj);
  195. if(isAllowedTeleportEntrance(sTileTeleport))
  196. {
  197. for(auto objId : gs->getTeleportChannelExits(sTileTeleport->channel, hero->tempOwner))
  198. {
  199. auto obj = getObj(objId);
  200. if(CGTeleport::isExitPassable(gs, hero, obj))
  201. neighbours.push_back(obj->visitablePos());
  202. }
  203. }
  204. }
  205. bool CPathfinder::isLayerTransitionPossible()
  206. {
  207. if((cp->layer == EPathfindingLayer::AIR || cp->layer == EPathfindingLayer::WATER)
  208. && dp->layer != EPathfindingLayer::LAND)
  209. {
  210. return false;
  211. }
  212. else if(cp->layer == EPathfindingLayer::LAND && dp->layer == EPathfindingLayer::AIR)
  213. {
  214. if(options.lightweightFlyingMode && !isSourceInitialPosition())
  215. return false;
  216. }
  217. else if(cp->layer == EPathfindingLayer::SAIL && dp->layer != EPathfindingLayer::LAND)
  218. return false;
  219. else if(cp->layer == EPathfindingLayer::SAIL && dp->layer == EPathfindingLayer::LAND)
  220. {
  221. if(!dt->isCoastal())
  222. return false;
  223. //tile must be accessible -> exception: unblocked blockvis tiles -> clear but guarded by nearby monster coast
  224. if((dp->accessible != CGPathNode::ACCESSIBLE && (dp->accessible != CGPathNode::BLOCKVIS || dt->blocked))
  225. || dt->visitable) //TODO: passableness problem -> town says it's passable (thus accessible) but we obviously can't disembark onto town gate
  226. return false;
  227. }
  228. else if(cp->layer == EPathfindingLayer::LAND && dp->layer == EPathfindingLayer::SAIL)
  229. {
  230. if(dp->accessible == CGPathNode::ACCESSIBLE) //cannot enter empty water tile from land -> it has to be visitable
  231. return false;
  232. }
  233. return true;
  234. }
  235. bool CPathfinder::isMovementToDestPossible()
  236. {
  237. auto obj = dt->topVisitableObj();
  238. switch(dp->layer)
  239. {
  240. case EPathfindingLayer::LAND:
  241. if(!canMoveBetween(cp->coord, dp->coord) || dp->accessible == CGPathNode::BLOCKED)
  242. return false;
  243. if(isSourceGuarded() && !isDestinationGuardian()) // Can step into tile of guard
  244. return false;
  245. if(cp->layer == EPathfindingLayer::SAIL)
  246. destAction = CGPathNode::DISEMBARK;
  247. break;
  248. case EPathfindingLayer::SAIL:
  249. if(!canMoveBetween(cp->coord, dp->coord) || dp->accessible == CGPathNode::BLOCKED)
  250. return false;
  251. if(isSourceGuarded() && !isDestinationGuardian()) // Can step into tile of guard
  252. return false;
  253. if(cp->layer == EPathfindingLayer::LAND)
  254. {
  255. if(!obj)
  256. return false;
  257. if(obj->ID == Obj::BOAT)
  258. destAction = CGPathNode::EMBARK;
  259. else if(obj->ID != Obj::HERO)
  260. return false;
  261. }
  262. break;
  263. case EPathfindingLayer::AIR:
  264. //if(!canMoveBetween(cp->coord, dp->coord))
  265. // return false;
  266. break;
  267. case EPathfindingLayer::WATER:
  268. if(!canMoveBetween(cp->coord, dp->coord) || dp->accessible != CGPathNode::ACCESSIBLE)
  269. return false;
  270. if(isDestinationGuarded())
  271. return false;
  272. break;
  273. }
  274. if(destAction == CGPathNode::UNKNOWN)
  275. {
  276. destAction = CGPathNode::NORMAL;
  277. if(dp->layer == EPathfindingLayer::LAND || dp->layer == EPathfindingLayer::SAIL)
  278. {
  279. if(obj)
  280. {
  281. auto objRel = getPlayerRelations(obj->tempOwner, hero->tempOwner);
  282. if(obj->ID == Obj::HERO)
  283. {
  284. if(objRel == PlayerRelations::ENEMIES)
  285. destAction = CGPathNode::BATTLE;
  286. else
  287. destAction = CGPathNode::BLOCKING_VISIT;
  288. }
  289. else if(obj->ID == Obj::TOWN && objRel == PlayerRelations::ENEMIES)
  290. {
  291. const CGTownInstance * townObj = dynamic_cast<const CGTownInstance *>(obj);
  292. if (townObj->armedGarrison())
  293. destAction = CGPathNode::BATTLE;
  294. }
  295. else if(obj->ID == Obj::GARRISON || obj->ID == Obj::GARRISON2)
  296. {
  297. const CGGarrison * garrisonObj = dynamic_cast<const CGGarrison *>(obj);
  298. if((garrisonObj->stacksCount() && objRel == PlayerRelations::ENEMIES) || isDestinationGuarded(true))
  299. destAction = CGPathNode::BATTLE;
  300. }
  301. else if(isDestinationGuardian())
  302. destAction = CGPathNode::BATTLE;
  303. else if(obj->blockVisit)
  304. destAction = CGPathNode::BLOCKING_VISIT;
  305. if(destAction == CGPathNode::NORMAL)
  306. destAction = CGPathNode::VISIT;
  307. }
  308. else if(isDestinationGuarded())
  309. destAction = CGPathNode::BATTLE;
  310. }
  311. }
  312. return true;
  313. }
  314. bool CPathfinder::isMovementAfterDestPossible()
  315. {
  316. switch(dp->layer)
  317. {
  318. case EPathfindingLayer::LAND:
  319. case EPathfindingLayer::SAIL:
  320. if(dp->accessible == CGPathNode::ACCESSIBLE)
  321. return true;
  322. if(dp->coord == out.hpos)
  323. return true; // This one is tricky, we can ignore fact that tile is not ACCESSIBLE in case if it's our hero block it. Though this need investigation
  324. if(dp->accessible == CGPathNode::VISITABLE && CGTeleport::isTeleport(dt->topVisitableObj()))
  325. {
  326. /// For now we'll always allow transit over teleporters
  327. /// Transit over whirlpools only allowed when hero protected
  328. auto whirlpool = dynamic_cast<const CGWhirlpool *>(dt->topVisitableObj());
  329. if(!whirlpool || options.useTeleportWhirlpool)
  330. return true;
  331. }
  332. if((destAction == CGPathNode::EMBARK || destAction == CGPathNode::DISEMBARK) && options.useEmbarkAndDisembark)
  333. return true;
  334. break;
  335. case EPathfindingLayer::AIR:
  336. case EPathfindingLayer::WATER:
  337. return true;
  338. break;
  339. }
  340. return false;
  341. }
  342. bool CPathfinder::isSourceInitialPosition()
  343. {
  344. return cp->coord == out.hpos;
  345. }
  346. int3 CPathfinder::getSourceGuardPosition()
  347. {
  348. return gs->map->guardingCreaturePositions[cp->coord.x][cp->coord.y][cp->coord.z];
  349. }
  350. bool CPathfinder::isSourceGuarded()
  351. {
  352. //map can start with hero on guarded tile or teleport there using dimension door
  353. //so threat tile hero standing on like it's not guarded because it's should be possible to move out of here
  354. if(getSourceGuardPosition() != int3(-1, -1, -1) && !isSourceInitialPosition())
  355. {
  356. //special case -> hero embarked a boat standing on a guarded tile -> we must allow to move away from that tile
  357. if(cp->accessible != CGPathNode::VISITABLE
  358. || !cp->theNodeBefore->land
  359. || ct->topVisitableId() != Obj::BOAT)
  360. {
  361. return true;
  362. }
  363. }
  364. return false;
  365. }
  366. bool CPathfinder::isDestinationGuarded(bool ignoreAccessibility)
  367. {
  368. if(gs->map->guardingCreaturePositions[dp->coord.x][dp->coord.y][dp->coord.z].valid()
  369. && (ignoreAccessibility || dp->accessible == CGPathNode::BLOCKVIS))
  370. {
  371. return true;
  372. }
  373. return false;
  374. }
  375. bool CPathfinder::isDestinationGuardian()
  376. {
  377. return getSourceGuardPosition() == dp->coord;
  378. }
  379. void CPathfinder::initializeGraph()
  380. {
  381. auto updateNode = [&](int3 pos, EPathfindingLayer layer, const TerrainTile *tinfo)
  382. {
  383. auto node = out.getNode(pos, layer);
  384. node->reset();
  385. node->accessible = evaluateAccessibility(pos, tinfo);
  386. node->land = tinfo->terType != ETerrainType::WATER;
  387. };
  388. int3 pos;
  389. for(pos.x=0; pos.x < out.sizes.x; ++pos.x)
  390. {
  391. for(pos.y=0; pos.y < out.sizes.y; ++pos.y)
  392. {
  393. for(pos.z=0; pos.z < out.sizes.z; ++pos.z)
  394. {
  395. const TerrainTile *tinfo = &gs->map->getTile(pos);
  396. switch(tinfo->terType)
  397. {
  398. case ETerrainType::ROCK:
  399. break;
  400. case ETerrainType::WATER:
  401. updateNode(pos, EPathfindingLayer::SAIL, tinfo);
  402. if(options.useFlying)
  403. updateNode(pos, EPathfindingLayer::AIR, tinfo);
  404. if(options.useWaterWalking)
  405. updateNode(pos, EPathfindingLayer::WATER, tinfo);
  406. break;
  407. default:
  408. updateNode(pos, EPathfindingLayer::LAND, tinfo);
  409. if(options.useFlying)
  410. updateNode(pos, EPathfindingLayer::AIR, tinfo);
  411. break;
  412. }
  413. }
  414. }
  415. }
  416. }
  417. CGPathNode::EAccessibility CPathfinder::evaluateAccessibility(const int3 &pos, const TerrainTile *tinfo) const
  418. {
  419. CGPathNode::EAccessibility ret = (tinfo->blocked ? CGPathNode::BLOCKED : CGPathNode::ACCESSIBLE);
  420. if(tinfo->terType == ETerrainType::ROCK || !isVisible(pos, hero->tempOwner))
  421. return CGPathNode::BLOCKED;
  422. if(tinfo->visitable)
  423. {
  424. if(tinfo->visitableObjects.front()->ID == Obj::SANCTUARY && tinfo->visitableObjects.back()->ID == Obj::HERO && tinfo->visitableObjects.back()->tempOwner != hero->tempOwner) //non-owned hero stands on Sanctuary
  425. {
  426. return CGPathNode::BLOCKED;
  427. }
  428. else
  429. {
  430. for(const CGObjectInstance *obj : tinfo->visitableObjects)
  431. {
  432. if(obj->passableFor(hero->tempOwner))
  433. {
  434. ret = CGPathNode::ACCESSIBLE;
  435. }
  436. else if(obj->blockVisit)
  437. {
  438. return CGPathNode::BLOCKVIS;
  439. }
  440. else if(obj->ID != Obj::EVENT) //pathfinder should ignore placed events
  441. {
  442. ret = CGPathNode::VISITABLE;
  443. }
  444. }
  445. }
  446. }
  447. else if(gs->map->guardingCreaturePositions[pos.x][pos.y][pos.z].valid()
  448. && !tinfo->blocked)
  449. {
  450. // Monster close by; blocked visit for battle.
  451. return CGPathNode::BLOCKVIS;
  452. }
  453. return ret;
  454. }
  455. bool CPathfinder::canMoveBetween(const int3 &a, const int3 &b) const
  456. {
  457. return gs->checkForVisitableDir(a, b);
  458. }
  459. bool CPathfinder::addTeleportTwoWay(const CGTeleport * obj) const
  460. {
  461. return options.useTeleportTwoWay && gs->isTeleportChannelBidirectional(obj->channel, hero->tempOwner);
  462. }
  463. bool CPathfinder::addTeleportOneWay(const CGTeleport * obj) const
  464. {
  465. if(options.useTeleportOneWay && isTeleportChannelUnidirectional(obj->channel, hero->tempOwner))
  466. {
  467. auto passableExits = CGTeleport::getPassableExits(gs, hero, gs->getTeleportChannelExits(obj->channel, hero->tempOwner));
  468. if(passableExits.size() == 1)
  469. return true;
  470. }
  471. return false;
  472. }
  473. bool CPathfinder::addTeleportOneWayRandom(const CGTeleport * obj) const
  474. {
  475. if(options.useTeleportOneWayRandom && isTeleportChannelUnidirectional(obj->channel, hero->tempOwner))
  476. {
  477. auto passableExits = CGTeleport::getPassableExits(gs, hero, gs->getTeleportChannelExits(obj->channel, hero->tempOwner));
  478. if(passableExits.size() > 1)
  479. return true;
  480. }
  481. return false;
  482. }
  483. bool CPathfinder::addTeleportWhirlpool(const CGWhirlpool * obj) const
  484. {
  485. return options.useTeleportWhirlpool && obj;
  486. }
  487. bool CPathfinder::canVisitObject() const
  488. {
  489. //hero can't visit objects while walking on water or flying
  490. return cp->layer == EPathfindingLayer::LAND || cp->layer == EPathfindingLayer::SAIL;
  491. }
  492. CGPathNode::CGPathNode(int3 Coord, EPathfindingLayer Layer)
  493. : coord(Coord), layer(Layer)
  494. {
  495. reset();
  496. }
  497. void CGPathNode::reset()
  498. {
  499. locked = false;
  500. accessible = NOT_SET;
  501. land = 0;
  502. moveRemains = 0;
  503. turns = 255;
  504. theNodeBefore = nullptr;
  505. action = UNKNOWN;
  506. }
  507. bool CGPathNode::reachable() const
  508. {
  509. return turns < 255;
  510. }
  511. int3 CGPath::startPos() const
  512. {
  513. return nodes[nodes.size()-1].coord;
  514. }
  515. int3 CGPath::endPos() const
  516. {
  517. return nodes[0].coord;
  518. }
  519. void CGPath::convert(ui8 mode)
  520. {
  521. if(mode==0)
  522. {
  523. for(auto & elem : nodes)
  524. {
  525. elem.coord = CGHeroInstance::convertPosition(elem.coord,true);
  526. }
  527. }
  528. }
  529. CPathsInfo::CPathsInfo(const int3 &Sizes)
  530. : sizes(Sizes)
  531. {
  532. hero = nullptr;
  533. nodes.resize(boost::extents[sizes.x][sizes.y][sizes.z][EPathfindingLayer::NUM_LAYERS]);
  534. for(int i = 0; i < sizes.x; i++)
  535. for(int j = 0; j < sizes.y; j++)
  536. for(int z = 0; z < sizes.z; z++)
  537. for(int l = 0; l < EPathfindingLayer::NUM_LAYERS; l++)
  538. nodes[i][j][z][l] = new CGPathNode(int3(i, j, z), static_cast<EPathfindingLayer>(l));
  539. }
  540. CPathsInfo::~CPathsInfo()
  541. {
  542. }
  543. const CGPathNode * CPathsInfo::getPathInfo(const int3 &tile, const EPathfindingLayer &layer) const
  544. {
  545. boost::unique_lock<boost::mutex> pathLock(pathMx);
  546. if(tile.x >= sizes.x || tile.y >= sizes.y || tile.z >= sizes.z || layer >= EPathfindingLayer::NUM_LAYERS)
  547. return nullptr;
  548. return getNode(tile, layer);
  549. }
  550. bool CPathsInfo::getPath(CGPath &out, const int3 &dst, const EPathfindingLayer &layer) const
  551. {
  552. boost::unique_lock<boost::mutex> pathLock(pathMx);
  553. out.nodes.clear();
  554. const CGPathNode *curnode = getNode(dst, layer);
  555. if(!curnode->theNodeBefore)
  556. return false;
  557. while(curnode)
  558. {
  559. CGPathNode cpn = *curnode;
  560. curnode = curnode->theNodeBefore;
  561. out.nodes.push_back(cpn);
  562. }
  563. return true;
  564. }
  565. int CPathsInfo::getDistance(const int3 &tile, const EPathfindingLayer &layer) const
  566. {
  567. boost::unique_lock<boost::mutex> pathLock(pathMx);
  568. CGPath ret;
  569. if(getPath(ret, tile, layer))
  570. return ret.nodes.size();
  571. else
  572. return 255;
  573. }
  574. CGPathNode *CPathsInfo::getNode(const int3 &coord, const EPathfindingLayer &layer) const
  575. {
  576. if(layer != EPathfindingLayer::AUTO)
  577. return nodes[coord.x][coord.y][coord.z][layer];
  578. auto landNode = nodes[coord.x][coord.y][coord.z][EPathfindingLayer::LAND];
  579. if(landNode->theNodeBefore)
  580. return landNode;
  581. else
  582. return nodes[coord.x][coord.y][coord.z][EPathfindingLayer::SAIL];
  583. }