CPathfinder.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * CPathfinder.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CPathfinder.h"
  12. #include "INodeStorage.h"
  13. #include "PathfinderOptions.h"
  14. #include "PathfindingRules.h"
  15. #include "TurnInfo.h"
  16. #include "../gameState/CGameState.h"
  17. #include "../CPlayerState.h"
  18. #include "../TerrainHandler.h"
  19. #include "../mapObjects/CGHeroInstance.h"
  20. #include "../mapping/CMap.h"
  21. #include "spells/CSpellHandler.h"
  22. VCMI_LIB_NAMESPACE_BEGIN
  23. std::vector<int3> CPathfinderHelper::getNeighbourTiles(const PathNodeInfo & source) const
  24. {
  25. std::vector<int3> neighbourTiles;
  26. neighbourTiles.reserve(8);
  27. getNeighbours(
  28. *source.tile,
  29. source.node->coord,
  30. neighbourTiles,
  31. boost::logic::indeterminate,
  32. source.node->layer == EPathfindingLayer::SAIL);
  33. if(source.isNodeObjectVisitable())
  34. {
  35. vstd::erase_if(neighbourTiles, [&](const int3 & tile) -> bool
  36. {
  37. return !canMoveBetween(tile, source.nodeObject->visitablePos());
  38. });
  39. }
  40. return neighbourTiles;
  41. }
  42. CPathfinder::CPathfinder(CGameState * _gs, std::shared_ptr<PathfinderConfig> config):
  43. gamestate(_gs),
  44. config(std::move(config))
  45. {
  46. initializeGraph();
  47. }
  48. void CPathfinder::push(CGPathNode * node)
  49. {
  50. if(node && !node->inPQ)
  51. {
  52. node->inPQ = true;
  53. node->pq = &this->pq;
  54. auto handle = pq.push(node);
  55. node->pqHandle = handle;
  56. }
  57. }
  58. CGPathNode * CPathfinder::topAndPop()
  59. {
  60. auto * node = pq.top();
  61. pq.pop();
  62. node->inPQ = false;
  63. node->pq = nullptr;
  64. return node;
  65. }
  66. void CPathfinder::calculatePaths()
  67. {
  68. //logGlobal->info("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. std::vector<CGPathNode *> initialNodes = config->nodeStorage->getInitialNodes();
  71. int counter = 0;
  72. for(auto * initialNode : initialNodes)
  73. {
  74. if(!gamestate->isInTheMap(initialNode->coord)/* || !gs->map->isInTheMap(dest)*/) //check input
  75. {
  76. logGlobal->error("CGameState::calculatePaths: Hero outside the gs->map? How dare you...");
  77. throw std::runtime_error("Wrong checksum");
  78. }
  79. source.setNode(gamestate, initialNode);
  80. auto * hlp = config->getOrCreatePathfinderHelper(source, gamestate);
  81. if(hlp->isHeroPatrolLocked())
  82. continue;
  83. pq.push(initialNode);
  84. }
  85. while(!pq.empty())
  86. {
  87. counter++;
  88. auto * node = topAndPop();
  89. source.setNode(gamestate, node);
  90. source.node->locked = true;
  91. int movement = source.node->moveRemains;
  92. uint8_t turn = source.node->turns;
  93. float cost = source.node->getCost();
  94. auto * hlp = config->getOrCreatePathfinderHelper(source, gamestate);
  95. hlp->updateTurnInfo(turn);
  96. if(!movement)
  97. {
  98. hlp->updateTurnInfo(++turn);
  99. movement = hlp->getMaxMovePoints(source.node->layer);
  100. if(!hlp->passOneTurnLimitCheck(source))
  101. continue;
  102. if(turn >= hlp->options.turnLimit)
  103. continue;
  104. }
  105. source.isInitialPosition = source.nodeHero == hlp->hero;
  106. source.updateInfo(hlp, gamestate);
  107. //add accessible neighbouring nodes to the queue
  108. auto neighbourNodes = config->nodeStorage->calculateNeighbours(source, config.get(), hlp);
  109. for(CGPathNode * neighbour : neighbourNodes)
  110. {
  111. if(neighbour->locked)
  112. continue;
  113. if(!hlp->isLayerAvailable(neighbour->layer))
  114. continue;
  115. destination.setNode(gamestate, neighbour);
  116. hlp = config->getOrCreatePathfinderHelper(destination, gamestate);
  117. if(!hlp->isPatrolMovementAllowed(neighbour->coord))
  118. continue;
  119. /// Check transition without tile accessability rules
  120. if(source.node->layer != neighbour->layer && !isLayerTransitionPossible())
  121. continue;
  122. destination.turn = turn;
  123. destination.movementLeft = movement;
  124. destination.cost = cost;
  125. destination.updateInfo(hlp, gamestate);
  126. destination.isGuardianTile = destination.guarded && isDestinationGuardian();
  127. for(const auto & rule : config->rules)
  128. {
  129. rule->process(source, destination, config.get(), hlp);
  130. if(destination.blocked)
  131. break;
  132. }
  133. if(!destination.blocked)
  134. push(destination.node);
  135. } //neighbours loop
  136. //just add all passable teleport exits
  137. hlp = config->getOrCreatePathfinderHelper(source, gamestate);
  138. /// For now we disable teleports usage for patrol movement
  139. /// VCAI not aware about patrol and may stuck while attempt to use teleport
  140. if(hlp->patrolState == CPathfinderHelper::PATROL_RADIUS)
  141. continue;
  142. auto teleportationNodes = config->nodeStorage->calculateTeleportations(source, config.get(), hlp);
  143. for(CGPathNode * teleportNode : teleportationNodes)
  144. {
  145. if(teleportNode->locked)
  146. continue;
  147. /// TODO: We may consider use invisible exits on FoW border in future
  148. /// Useful for AI when at least one tile around exit is visible and passable
  149. /// Objects are usually visible on FoW border anyway so it's not cheating.
  150. ///
  151. /// For now it's disabled as it's will cause crashes in movement code.
  152. if(teleportNode->accessible == EPathAccessibility::BLOCKED)
  153. continue;
  154. destination.setNode(gamestate, teleportNode);
  155. destination.turn = turn;
  156. destination.movementLeft = movement;
  157. destination.cost = cost;
  158. if(destination.isBetterWay())
  159. {
  160. destination.action = getTeleportDestAction();
  161. config->nodeStorage->commit(destination, source);
  162. if(destination.node->action == EPathNodeAction::TELEPORT_NORMAL)
  163. push(destination.node);
  164. }
  165. }
  166. } //queue loop
  167. logAi->trace("CPathfinder finished with %s iterations", std::to_string(counter));
  168. }
  169. std::vector<int3> CPathfinderHelper::getAllowedTeleportChannelExits(const TeleportChannelID & channelID) const
  170. {
  171. std::vector<int3> allowedExits;
  172. for(const auto & objId : getTeleportChannelExits(channelID, hero->tempOwner))
  173. {
  174. const auto * obj = getObj(objId);
  175. if(dynamic_cast<const CGWhirlpool *>(obj))
  176. {
  177. auto pos = obj->getBlockedPos();
  178. for(const auto & p : pos)
  179. {
  180. if(gs->map->getTile(p).topVisitableId() == obj->ID)
  181. allowedExits.push_back(p);
  182. }
  183. }
  184. else if(obj && CGTeleport::isExitPassable(gs, hero, obj))
  185. allowedExits.push_back(obj->visitablePos());
  186. }
  187. return allowedExits;
  188. }
  189. std::vector<int3> CPathfinderHelper::getCastleGates(const PathNodeInfo & source) const
  190. {
  191. std::vector<int3> allowedExits;
  192. auto towns = getPlayerState(hero->tempOwner)->towns;
  193. for(const auto & town : towns)
  194. {
  195. if(town->id != source.nodeObject->id && town->visitingHero == nullptr
  196. && town->hasBuilt(BuildingID::CASTLE_GATE, ETownType::INFERNO))
  197. {
  198. allowedExits.push_back(town->visitablePos());
  199. }
  200. }
  201. return allowedExits;
  202. }
  203. std::vector<int3> CPathfinderHelper::getTeleportExits(const PathNodeInfo & source) const
  204. {
  205. std::vector<int3> teleportationExits;
  206. const auto * objTeleport = dynamic_cast<const CGTeleport *>(source.nodeObject);
  207. if(isAllowedTeleportEntrance(objTeleport))
  208. {
  209. for(const auto & exit : getAllowedTeleportChannelExits(objTeleport->channel))
  210. {
  211. teleportationExits.push_back(exit);
  212. }
  213. }
  214. else if(options.useCastleGate
  215. && (source.nodeObject->ID == Obj::TOWN && source.nodeObject->subID == ETownType::INFERNO
  216. && source.objectRelations != PlayerRelations::ENEMIES))
  217. {
  218. /// TODO: Find way to reuse CPlayerSpecificInfoCallback::getTownsInfo
  219. /// This may be handy if we allow to use teleportation to friendly towns
  220. for(const auto & exit : getCastleGates(source))
  221. {
  222. teleportationExits.push_back(exit);
  223. }
  224. }
  225. return teleportationExits;
  226. }
  227. bool CPathfinderHelper::isHeroPatrolLocked() const
  228. {
  229. return patrolState == PATROL_LOCKED;
  230. }
  231. bool CPathfinderHelper::isPatrolMovementAllowed(const int3 & dst) const
  232. {
  233. if(patrolState == PATROL_RADIUS)
  234. {
  235. if(!vstd::contains(patrolTiles, dst))
  236. return false;
  237. }
  238. return true;
  239. }
  240. bool CPathfinder::isLayerTransitionPossible() const
  241. {
  242. ELayer destLayer = destination.node->layer;
  243. /// No layer transition allowed when previous node action is BATTLE
  244. if(source.node->action == EPathNodeAction::BATTLE)
  245. return false;
  246. switch(source.node->layer)
  247. {
  248. case ELayer::LAND:
  249. if(destLayer == ELayer::AIR)
  250. {
  251. if(!config->options.lightweightFlyingMode || source.isInitialPosition)
  252. return true;
  253. }
  254. else if(destLayer == ELayer::SAIL)
  255. {
  256. if(destination.tile->isWater())
  257. return true;
  258. }
  259. else
  260. return true;
  261. break;
  262. case ELayer::SAIL:
  263. if(destLayer == ELayer::LAND && !destination.tile->isWater())
  264. return true;
  265. break;
  266. case ELayer::AIR:
  267. if(destLayer == ELayer::LAND)
  268. return true;
  269. break;
  270. case ELayer::WATER:
  271. if(destLayer == ELayer::LAND)
  272. return true;
  273. break;
  274. }
  275. return false;
  276. }
  277. EPathNodeAction CPathfinder::getTeleportDestAction() const
  278. {
  279. EPathNodeAction action = EPathNodeAction::TELEPORT_NORMAL;
  280. if(destination.isNodeObjectVisitable() && destination.nodeHero)
  281. {
  282. if(destination.heroRelations == PlayerRelations::ENEMIES)
  283. action = EPathNodeAction::TELEPORT_BATTLE;
  284. else
  285. action = EPathNodeAction::TELEPORT_BLOCKING_VISIT;
  286. }
  287. return action;
  288. }
  289. bool CPathfinder::isDestinationGuardian() const
  290. {
  291. return gamestate->guardingCreaturePosition(destination.node->coord) == destination.node->coord;
  292. }
  293. void CPathfinderHelper::initializePatrol()
  294. {
  295. auto state = PATROL_NONE;
  296. if(hero->patrol.patrolling && !getPlayerState(hero->tempOwner)->human)
  297. {
  298. if(hero->patrol.patrolRadius)
  299. {
  300. state = PATROL_RADIUS;
  301. gs->getTilesInRange(patrolTiles, hero->patrol.initialPos, hero->patrol.patrolRadius, ETileVisibility::REVEALED, std::optional<PlayerColor>(), int3::DIST_MANHATTAN);
  302. }
  303. else
  304. state = PATROL_LOCKED;
  305. }
  306. patrolState = state;
  307. }
  308. void CPathfinder::initializeGraph()
  309. {
  310. INodeStorage * nodeStorage = config->nodeStorage.get();
  311. nodeStorage->initialize(config->options, gamestate);
  312. }
  313. bool CPathfinderHelper::canMoveBetween(const int3 & a, const int3 & b) const
  314. {
  315. return gs->checkForVisitableDir(a, b);
  316. }
  317. bool CPathfinderHelper::isAllowedTeleportEntrance(const CGTeleport * obj) const
  318. {
  319. if(!obj || !isTeleportEntrancePassable(obj, hero->tempOwner))
  320. return false;
  321. const auto * whirlpool = dynamic_cast<const CGWhirlpool *>(obj);
  322. if(whirlpool)
  323. {
  324. if(addTeleportWhirlpool(whirlpool))
  325. return true;
  326. }
  327. else if(addTeleportTwoWay(obj) || addTeleportOneWay(obj) || addTeleportOneWayRandom(obj))
  328. return true;
  329. return false;
  330. }
  331. bool CPathfinderHelper::addTeleportTwoWay(const CGTeleport * obj) const
  332. {
  333. return options.useTeleportTwoWay && isTeleportChannelBidirectional(obj->channel, hero->tempOwner);
  334. }
  335. bool CPathfinderHelper::addTeleportOneWay(const CGTeleport * obj) const
  336. {
  337. if(options.useTeleportOneWay && isTeleportChannelUnidirectional(obj->channel, hero->tempOwner))
  338. {
  339. auto passableExits = CGTeleport::getPassableExits(gs, hero, getTeleportChannelExits(obj->channel, hero->tempOwner));
  340. if(passableExits.size() == 1)
  341. return true;
  342. }
  343. return false;
  344. }
  345. bool CPathfinderHelper::addTeleportOneWayRandom(const CGTeleport * obj) const
  346. {
  347. if(options.useTeleportOneWayRandom && isTeleportChannelUnidirectional(obj->channel, hero->tempOwner))
  348. {
  349. auto passableExits = CGTeleport::getPassableExits(gs, hero, getTeleportChannelExits(obj->channel, hero->tempOwner));
  350. if(passableExits.size() > 1)
  351. return true;
  352. }
  353. return false;
  354. }
  355. bool CPathfinderHelper::addTeleportWhirlpool(const CGWhirlpool * obj) const
  356. {
  357. return options.useTeleportWhirlpool && hasBonusOfType(BonusType::WHIRLPOOL_PROTECTION) && obj;
  358. }
  359. int CPathfinderHelper::movementPointsAfterEmbark(int movement, int basicCost, bool disembark) const
  360. {
  361. return hero->movementPointsAfterEmbark(movement, basicCost, disembark, getTurnInfo());
  362. }
  363. bool CPathfinderHelper::passOneTurnLimitCheck(const PathNodeInfo & source) const
  364. {
  365. if(!options.oneTurnSpecialLayersLimit)
  366. return true;
  367. if(source.node->layer == EPathfindingLayer::WATER)
  368. return false;
  369. if(source.node->layer == EPathfindingLayer::AIR)
  370. {
  371. return options.originalMovementRules && source.node->accessible == EPathAccessibility::ACCESSIBLE;
  372. }
  373. return true;
  374. }
  375. int CPathfinderHelper::getGuardiansCount(int3 tile) const
  376. {
  377. return getGuardingCreatures(tile).size();
  378. }
  379. CPathfinderHelper::CPathfinderHelper(CGameState * gs, const CGHeroInstance * Hero, const PathfinderOptions & Options):
  380. CGameInfoCallback(gs),
  381. turn(-1),
  382. hero(Hero),
  383. options(Options),
  384. owner(Hero->tempOwner)
  385. {
  386. turnsInfo.reserve(16);
  387. updateTurnInfo();
  388. initializePatrol();
  389. SpellID flySpell = SpellID::FLY;
  390. canCastFly = Hero->canCastThisSpell(flySpell.toSpell());
  391. SpellID waterWalk = SpellID::WATER_WALK;
  392. canCastWaterWalk = Hero->canCastThisSpell(waterWalk.toSpell());
  393. }
  394. CPathfinderHelper::~CPathfinderHelper()
  395. {
  396. for(auto * ti : turnsInfo)
  397. delete ti;
  398. }
  399. void CPathfinderHelper::updateTurnInfo(const int Turn)
  400. {
  401. if(turn != Turn)
  402. {
  403. turn = Turn;
  404. if(turn >= turnsInfo.size())
  405. {
  406. auto * ti = new TurnInfo(hero, turn);
  407. turnsInfo.push_back(ti);
  408. }
  409. }
  410. }
  411. bool CPathfinderHelper::isLayerAvailable(const EPathfindingLayer & layer) const
  412. {
  413. switch(layer)
  414. {
  415. case EPathfindingLayer::AIR:
  416. if(!options.useFlying)
  417. return false;
  418. if(canCastFly && options.canUseCast)
  419. return true;
  420. break;
  421. case EPathfindingLayer::WATER:
  422. if(!options.useWaterWalking)
  423. return false;
  424. if(canCastWaterWalk && options.canUseCast)
  425. return true;
  426. break;
  427. }
  428. return turnsInfo[turn]->isLayerAvailable(layer);
  429. }
  430. const TurnInfo * CPathfinderHelper::getTurnInfo() const
  431. {
  432. return turnsInfo[turn];
  433. }
  434. bool CPathfinderHelper::hasBonusOfType(const BonusType type, const int subtype) const
  435. {
  436. return turnsInfo[turn]->hasBonusOfType(type, subtype);
  437. }
  438. int CPathfinderHelper::getMaxMovePoints(const EPathfindingLayer & layer) const
  439. {
  440. return turnsInfo[turn]->getMaxMovePoints(layer);
  441. }
  442. void CPathfinderHelper::getNeighbours(
  443. const TerrainTile & srcTile,
  444. const int3 & srcCoord,
  445. std::vector<int3> & vec,
  446. const boost::logic::tribool & onLand,
  447. const bool limitCoastSailing) const
  448. {
  449. CMap * map = gs->map;
  450. static const int3 dirs[] = {
  451. int3(-1, +1, +0), int3(0, +1, +0), int3(+1, +1, +0),
  452. int3(-1, +0, +0), /* source pos */ int3(+1, +0, +0),
  453. int3(-1, -1, +0), int3(0, -1, +0), int3(+1, -1, +0)
  454. };
  455. for(const auto & dir : dirs)
  456. {
  457. const int3 destCoord = srcCoord + dir;
  458. if(!map->isInTheMap(destCoord))
  459. continue;
  460. const TerrainTile & destTile = map->getTile(destCoord);
  461. if(!destTile.terType->isPassable())
  462. continue;
  463. // //we cannot visit things from blocked tiles
  464. // if(srcTile.blocked && !srcTile.visitable && destTile.visitable && srcTile.blockingObjects.front()->ID != HEROI_TYPE)
  465. // {
  466. // continue;
  467. // }
  468. /// Following condition let us avoid diagonal movement over coast when sailing
  469. if(srcTile.terType->isWater() && limitCoastSailing && destTile.terType->isWater() && dir.x && dir.y) //diagonal move through water
  470. {
  471. const int3 horizontalNeighbour = srcCoord + int3{dir.x, 0, 0};
  472. const int3 verticalNeighbour = srcCoord + int3{0, dir.y, 0};
  473. if(map->getTile(horizontalNeighbour).terType->isLand() || map->getTile(verticalNeighbour).terType->isLand())
  474. continue;
  475. }
  476. if(indeterminate(onLand) || onLand == destTile.terType->isLand())
  477. {
  478. vec.push_back(destCoord);
  479. }
  480. }
  481. }
  482. int CPathfinderHelper::getMovementCost(
  483. const PathNodeInfo & src,
  484. const PathNodeInfo & dst,
  485. const int remainingMovePoints,
  486. const bool checkLast) const
  487. {
  488. return getMovementCost(
  489. src.coord,
  490. dst.coord,
  491. src.tile,
  492. dst.tile,
  493. remainingMovePoints,
  494. checkLast,
  495. dst.node->layer == EPathfindingLayer::SAIL,
  496. dst.node->layer == EPathfindingLayer::WATER
  497. );
  498. }
  499. int CPathfinderHelper::getMovementCost(
  500. const int3 & src,
  501. const int3 & dst,
  502. const TerrainTile * ct,
  503. const TerrainTile * dt,
  504. const int remainingMovePoints,
  505. const bool checkLast,
  506. boost::logic::tribool isDstSailLayer,
  507. boost::logic::tribool isDstWaterLayer) const
  508. {
  509. if(src == dst) //same tile
  510. return 0;
  511. const auto * ti = getTurnInfo();
  512. if(ct == nullptr || dt == nullptr)
  513. {
  514. ct = hero->cb->getTile(src);
  515. dt = hero->cb->getTile(dst);
  516. }
  517. bool isSailLayer;
  518. if(indeterminate(isDstSailLayer))
  519. isSailLayer = hero->boat && hero->boat->layer == EPathfindingLayer::SAIL && dt->terType->isWater();
  520. else
  521. isSailLayer = static_cast<bool>(isDstSailLayer);
  522. bool isWaterLayer;
  523. if(indeterminate(isDstWaterLayer))
  524. isWaterLayer = ((hero->boat && hero->boat->layer == EPathfindingLayer::WATER) || ti->hasBonusOfType(BonusType::WATER_WALKING)) && dt->terType->isWater();
  525. else
  526. isWaterLayer = static_cast<bool>(isDstWaterLayer);
  527. bool isAirLayer = (hero->boat && hero->boat->layer == EPathfindingLayer::AIR) || ti->hasBonusOfType(BonusType::FLYING_MOVEMENT);
  528. int ret = hero->getTileMovementCost(*dt, *ct, ti);
  529. if(isSailLayer)
  530. {
  531. if(ct->hasFavorableWinds())
  532. ret = static_cast<int>(ret * 2.0 / 3);
  533. }
  534. else if(isAirLayer)
  535. vstd::amin(ret, GameConstants::BASE_MOVEMENT_COST + ti->valOfBonuses(BonusType::FLYING_MOVEMENT));
  536. else if(isWaterLayer && ti->hasBonusOfType(BonusType::WATER_WALKING))
  537. ret = static_cast<int>(ret * (100.0 + ti->valOfBonuses(BonusType::WATER_WALKING)) / 100.0);
  538. if(src.x != dst.x && src.y != dst.y) //it's diagonal move
  539. {
  540. int old = ret;
  541. ret = static_cast<int>(ret * M_SQRT2);
  542. //diagonal move costs too much but normal move is possible - allow diagonal move for remaining move points
  543. // https://heroes.thelazy.net/index.php/Movement#Diagonal_move_exception
  544. if(ret > remainingMovePoints && remainingMovePoints >= old)
  545. {
  546. return remainingMovePoints;
  547. }
  548. }
  549. const int left = remainingMovePoints - ret;
  550. constexpr auto maxCostOfOneStep = static_cast<int>(175 * M_SQRT2); // diagonal move on Swamp - 247 MP
  551. if(checkLast && left > 0 && left <= maxCostOfOneStep) //it might be the last tile - if no further move possible we take all move points
  552. {
  553. std::vector<int3> vec;
  554. vec.reserve(8); //optimization
  555. getNeighbours(*dt, dst, vec, ct->terType->isLand(), true);
  556. for(const auto & elem : vec)
  557. {
  558. int fcost = getMovementCost(dst, elem, nullptr, nullptr, left, false);
  559. if(fcost <= left)
  560. {
  561. return ret;
  562. }
  563. }
  564. ret = remainingMovePoints;
  565. }
  566. return ret;
  567. }
  568. VCMI_LIB_NAMESPACE_END