CPathfinder.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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 && source.nodeObject->ID == Obj::TOWN && source.objectRelations != PlayerRelations::ENEMIES)
  215. {
  216. auto * town = dynamic_cast<const CGTownInstance *>(source.nodeObject);
  217. assert(town);
  218. if (town && town->getFaction() == FactionID::INFERNO)
  219. {
  220. /// TODO: Find way to reuse CPlayerSpecificInfoCallback::getTownsInfo
  221. /// This may be handy if we allow to use teleportation to friendly towns
  222. for(const auto & exit : getCastleGates(source))
  223. {
  224. teleportationExits.push_back(exit);
  225. }
  226. }
  227. }
  228. return teleportationExits;
  229. }
  230. bool CPathfinderHelper::isHeroPatrolLocked() const
  231. {
  232. return patrolState == PATROL_LOCKED;
  233. }
  234. bool CPathfinderHelper::isPatrolMovementAllowed(const int3 & dst) const
  235. {
  236. if(patrolState == PATROL_RADIUS)
  237. {
  238. if(!vstd::contains(patrolTiles, dst))
  239. return false;
  240. }
  241. return true;
  242. }
  243. bool CPathfinder::isLayerTransitionPossible() const
  244. {
  245. ELayer destLayer = destination.node->layer;
  246. /// No layer transition allowed when previous node action is BATTLE
  247. if(source.node->action == EPathNodeAction::BATTLE)
  248. return false;
  249. switch(source.node->layer)
  250. {
  251. case ELayer::LAND:
  252. if(destLayer == ELayer::AIR)
  253. {
  254. if(!config->options.lightweightFlyingMode || source.isInitialPosition)
  255. return true;
  256. }
  257. else if(destLayer == ELayer::SAIL)
  258. {
  259. if(destination.tile->isWater())
  260. return true;
  261. }
  262. else
  263. return true;
  264. break;
  265. case ELayer::SAIL:
  266. if(destLayer == ELayer::LAND && !destination.tile->isWater())
  267. return true;
  268. break;
  269. case ELayer::AIR:
  270. if(destLayer == ELayer::LAND)
  271. return true;
  272. break;
  273. case ELayer::WATER:
  274. if(destLayer == ELayer::LAND)
  275. return true;
  276. break;
  277. }
  278. return false;
  279. }
  280. EPathNodeAction CPathfinder::getTeleportDestAction() const
  281. {
  282. EPathNodeAction action = EPathNodeAction::TELEPORT_NORMAL;
  283. if(destination.isNodeObjectVisitable() && destination.nodeHero)
  284. {
  285. if(destination.heroRelations == PlayerRelations::ENEMIES)
  286. action = EPathNodeAction::TELEPORT_BATTLE;
  287. else
  288. action = EPathNodeAction::TELEPORT_BLOCKING_VISIT;
  289. }
  290. return action;
  291. }
  292. bool CPathfinder::isDestinationGuardian() const
  293. {
  294. return gamestate->guardingCreaturePosition(destination.node->coord) == destination.node->coord;
  295. }
  296. void CPathfinderHelper::initializePatrol()
  297. {
  298. auto state = PATROL_NONE;
  299. if(hero->patrol.patrolling && !getPlayerState(hero->tempOwner)->human)
  300. {
  301. if(hero->patrol.patrolRadius)
  302. {
  303. state = PATROL_RADIUS;
  304. gs->getTilesInRange(patrolTiles, hero->patrol.initialPos, hero->patrol.patrolRadius, ETileVisibility::REVEALED, std::optional<PlayerColor>(), int3::DIST_MANHATTAN);
  305. }
  306. else
  307. state = PATROL_LOCKED;
  308. }
  309. patrolState = state;
  310. }
  311. void CPathfinder::initializeGraph()
  312. {
  313. INodeStorage * nodeStorage = config->nodeStorage.get();
  314. nodeStorage->initialize(config->options, gamestate);
  315. }
  316. bool CPathfinderHelper::canMoveBetween(const int3 & a, const int3 & b) const
  317. {
  318. return gs->checkForVisitableDir(a, b);
  319. }
  320. bool CPathfinderHelper::isAllowedTeleportEntrance(const CGTeleport * obj) const
  321. {
  322. if(!obj || !isTeleportEntrancePassable(obj, hero->tempOwner))
  323. return false;
  324. const auto * whirlpool = dynamic_cast<const CGWhirlpool *>(obj);
  325. if(whirlpool)
  326. {
  327. if(addTeleportWhirlpool(whirlpool))
  328. return true;
  329. }
  330. else if(addTeleportTwoWay(obj) || addTeleportOneWay(obj) || addTeleportOneWayRandom(obj))
  331. return true;
  332. return false;
  333. }
  334. bool CPathfinderHelper::addTeleportTwoWay(const CGTeleport * obj) const
  335. {
  336. return options.useTeleportTwoWay && isTeleportChannelBidirectional(obj->channel, hero->tempOwner);
  337. }
  338. bool CPathfinderHelper::addTeleportOneWay(const CGTeleport * obj) const
  339. {
  340. if(options.useTeleportOneWay && isTeleportChannelUnidirectional(obj->channel, hero->tempOwner))
  341. {
  342. auto passableExits = CGTeleport::getPassableExits(gs, hero, getTeleportChannelExits(obj->channel, hero->tempOwner));
  343. if(passableExits.size() == 1)
  344. return true;
  345. }
  346. return false;
  347. }
  348. bool CPathfinderHelper::addTeleportOneWayRandom(const CGTeleport * obj) const
  349. {
  350. if(options.useTeleportOneWayRandom && isTeleportChannelUnidirectional(obj->channel, hero->tempOwner))
  351. {
  352. auto passableExits = CGTeleport::getPassableExits(gs, hero, getTeleportChannelExits(obj->channel, hero->tempOwner));
  353. if(passableExits.size() > 1)
  354. return true;
  355. }
  356. return false;
  357. }
  358. bool CPathfinderHelper::addTeleportWhirlpool(const CGWhirlpool * obj) const
  359. {
  360. return options.useTeleportWhirlpool && hasBonusOfType(BonusType::WHIRLPOOL_PROTECTION) && obj;
  361. }
  362. int CPathfinderHelper::movementPointsAfterEmbark(int movement, int basicCost, bool disembark) const
  363. {
  364. return hero->movementPointsAfterEmbark(movement, basicCost, disembark, getTurnInfo());
  365. }
  366. bool CPathfinderHelper::passOneTurnLimitCheck(const PathNodeInfo & source) const
  367. {
  368. if(!options.oneTurnSpecialLayersLimit)
  369. return true;
  370. if(source.node->layer == EPathfindingLayer::WATER)
  371. return false;
  372. if(source.node->layer == EPathfindingLayer::AIR)
  373. {
  374. return options.originalMovementRules && source.node->accessible == EPathAccessibility::ACCESSIBLE;
  375. }
  376. return true;
  377. }
  378. int CPathfinderHelper::getGuardiansCount(int3 tile) const
  379. {
  380. return getGuardingCreatures(tile).size();
  381. }
  382. CPathfinderHelper::CPathfinderHelper(CGameState * gs, const CGHeroInstance * Hero, const PathfinderOptions & Options):
  383. CGameInfoCallback(gs),
  384. turn(-1),
  385. hero(Hero),
  386. options(Options),
  387. owner(Hero->tempOwner)
  388. {
  389. turnsInfo.reserve(16);
  390. updateTurnInfo();
  391. initializePatrol();
  392. SpellID flySpell = SpellID::FLY;
  393. canCastFly = Hero->canCastThisSpell(flySpell.toSpell());
  394. SpellID waterWalk = SpellID::WATER_WALK;
  395. canCastWaterWalk = Hero->canCastThisSpell(waterWalk.toSpell());
  396. }
  397. CPathfinderHelper::~CPathfinderHelper()
  398. {
  399. for(auto * ti : turnsInfo)
  400. delete ti;
  401. }
  402. void CPathfinderHelper::updateTurnInfo(const int Turn)
  403. {
  404. if(turn != Turn)
  405. {
  406. turn = Turn;
  407. if(turn >= turnsInfo.size())
  408. {
  409. auto * ti = new TurnInfo(hero, turn);
  410. turnsInfo.push_back(ti);
  411. }
  412. }
  413. }
  414. bool CPathfinderHelper::isLayerAvailable(const EPathfindingLayer & layer) const
  415. {
  416. switch(layer)
  417. {
  418. case EPathfindingLayer::AIR:
  419. if(!options.useFlying)
  420. return false;
  421. if(canCastFly && options.canUseCast)
  422. return true;
  423. break;
  424. case EPathfindingLayer::WATER:
  425. if(!options.useWaterWalking)
  426. return false;
  427. if(canCastWaterWalk && options.canUseCast)
  428. return true;
  429. break;
  430. }
  431. return turnsInfo[turn]->isLayerAvailable(layer);
  432. }
  433. const TurnInfo * CPathfinderHelper::getTurnInfo() const
  434. {
  435. return turnsInfo[turn];
  436. }
  437. bool CPathfinderHelper::hasBonusOfType(const BonusType type) const
  438. {
  439. return turnsInfo[turn]->hasBonusOfType(type);
  440. }
  441. int CPathfinderHelper::getMaxMovePoints(const EPathfindingLayer & layer) const
  442. {
  443. return turnsInfo[turn]->getMaxMovePoints(layer);
  444. }
  445. void CPathfinderHelper::getNeighbours(
  446. const TerrainTile & srcTile,
  447. const int3 & srcCoord,
  448. std::vector<int3> & vec,
  449. const boost::logic::tribool & onLand,
  450. const bool limitCoastSailing) const
  451. {
  452. CMap * map = gs->map;
  453. static const int3 dirs[] = {
  454. int3(-1, +1, +0), int3(0, +1, +0), int3(+1, +1, +0),
  455. int3(-1, +0, +0), /* source pos */ int3(+1, +0, +0),
  456. int3(-1, -1, +0), int3(0, -1, +0), int3(+1, -1, +0)
  457. };
  458. for(const auto & dir : dirs)
  459. {
  460. const int3 destCoord = srcCoord + dir;
  461. if(!map->isInTheMap(destCoord))
  462. continue;
  463. const TerrainTile & destTile = map->getTile(destCoord);
  464. if(!destTile.terType->isPassable())
  465. continue;
  466. // //we cannot visit things from blocked tiles
  467. // if(srcTile.blocked && !srcTile.visitable && destTile.visitable && srcTile.blockingObjects.front()->ID != HEROI_TYPE)
  468. // {
  469. // continue;
  470. // }
  471. /// Following condition let us avoid diagonal movement over coast when sailing
  472. if(srcTile.terType->isWater() && limitCoastSailing && destTile.terType->isWater() && dir.x && dir.y) //diagonal move through water
  473. {
  474. const int3 horizontalNeighbour = srcCoord + int3{dir.x, 0, 0};
  475. const int3 verticalNeighbour = srcCoord + int3{0, dir.y, 0};
  476. if(map->getTile(horizontalNeighbour).terType->isLand() || map->getTile(verticalNeighbour).terType->isLand())
  477. continue;
  478. }
  479. if(indeterminate(onLand) || onLand == destTile.terType->isLand())
  480. {
  481. vec.push_back(destCoord);
  482. }
  483. }
  484. }
  485. int CPathfinderHelper::getMovementCost(
  486. const PathNodeInfo & src,
  487. const PathNodeInfo & dst,
  488. const int remainingMovePoints,
  489. const bool checkLast) const
  490. {
  491. return getMovementCost(
  492. src.coord,
  493. dst.coord,
  494. src.tile,
  495. dst.tile,
  496. remainingMovePoints,
  497. checkLast,
  498. dst.node->layer == EPathfindingLayer::SAIL,
  499. dst.node->layer == EPathfindingLayer::WATER
  500. );
  501. }
  502. int CPathfinderHelper::getMovementCost(
  503. const int3 & src,
  504. const int3 & dst,
  505. const TerrainTile * ct,
  506. const TerrainTile * dt,
  507. const int remainingMovePoints,
  508. const bool checkLast,
  509. boost::logic::tribool isDstSailLayer,
  510. boost::logic::tribool isDstWaterLayer) const
  511. {
  512. if(src == dst) //same tile
  513. return 0;
  514. const auto * ti = getTurnInfo();
  515. if(ct == nullptr || dt == nullptr)
  516. {
  517. ct = hero->cb->getTile(src);
  518. dt = hero->cb->getTile(dst);
  519. }
  520. bool isSailLayer;
  521. if(indeterminate(isDstSailLayer))
  522. isSailLayer = hero->boat && hero->boat->layer == EPathfindingLayer::SAIL && dt->terType->isWater();
  523. else
  524. isSailLayer = static_cast<bool>(isDstSailLayer);
  525. bool isWaterLayer;
  526. if(indeterminate(isDstWaterLayer))
  527. isWaterLayer = ((hero->boat && hero->boat->layer == EPathfindingLayer::WATER) || ti->hasBonusOfType(BonusType::WATER_WALKING)) && dt->terType->isWater();
  528. else
  529. isWaterLayer = static_cast<bool>(isDstWaterLayer);
  530. bool isAirLayer = (hero->boat && hero->boat->layer == EPathfindingLayer::AIR) || ti->hasBonusOfType(BonusType::FLYING_MOVEMENT);
  531. int ret = hero->getTileMovementCost(*dt, *ct, ti);
  532. if(isSailLayer)
  533. {
  534. if(ct->hasFavorableWinds())
  535. ret = static_cast<int>(ret * 2.0 / 3);
  536. }
  537. else if(isAirLayer)
  538. vstd::amin(ret, GameConstants::BASE_MOVEMENT_COST + ti->valOfBonuses(BonusType::FLYING_MOVEMENT));
  539. else if(isWaterLayer && ti->hasBonusOfType(BonusType::WATER_WALKING))
  540. ret = static_cast<int>(ret * (100.0 + ti->valOfBonuses(BonusType::WATER_WALKING)) / 100.0);
  541. if(src.x != dst.x && src.y != dst.y) //it's diagonal move
  542. {
  543. int old = ret;
  544. ret = static_cast<int>(ret * M_SQRT2);
  545. //diagonal move costs too much but normal move is possible - allow diagonal move for remaining move points
  546. // https://heroes.thelazy.net/index.php/Movement#Diagonal_move_exception
  547. if(ret > remainingMovePoints && remainingMovePoints >= old)
  548. {
  549. return remainingMovePoints;
  550. }
  551. }
  552. const int left = remainingMovePoints - ret;
  553. constexpr auto maxCostOfOneStep = static_cast<int>(175 * M_SQRT2); // diagonal move on Swamp - 247 MP
  554. if(checkLast && left > 0 && left <= maxCostOfOneStep) //it might be the last tile - if no further move possible we take all move points
  555. {
  556. std::vector<int3> vec;
  557. vec.reserve(8); //optimization
  558. getNeighbours(*dt, dst, vec, ct->terType->isLand(), true);
  559. for(const auto & elem : vec)
  560. {
  561. int fcost = getMovementCost(dst, elem, nullptr, nullptr, left, false);
  562. if(fcost <= left)
  563. {
  564. return ret;
  565. }
  566. }
  567. ret = remainingMovePoints;
  568. }
  569. return ret;
  570. }
  571. VCMI_LIB_NAMESPACE_END