AINodeStorage.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * AINodeStorage.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 "AINodeStorage.h"
  12. #include "Actions/TownPortalAction.h"
  13. #include "../Goals/Goals.h"
  14. #include "../../../CCallback.h"
  15. #include "../../../lib/mapping/CMap.h"
  16. #include "../../../lib/mapObjects/MapObjects.h"
  17. #include "../../../lib/PathfinderUtil.h"
  18. #include "../../../lib/CPlayerState.h"
  19. AINodeStorage::AINodeStorage(const int3 & Sizes)
  20. : sizes(Sizes)
  21. {
  22. nodes.resize(boost::extents[sizes.x][sizes.y][sizes.z][EPathfindingLayer::NUM_LAYERS][NUM_CHAINS]);
  23. dangerEvaluator.reset(new FuzzyHelper());
  24. }
  25. AINodeStorage::~AINodeStorage() = default;
  26. void AINodeStorage::initialize(const PathfinderOptions & options, const CGameState * gs)
  27. {
  28. if(heroChainPass)
  29. return;
  30. //TODO: fix this code duplication with NodeStorage::initialize, problem is to keep `resetTile` inline
  31. int3 pos;
  32. const PlayerColor player = ai->playerID;
  33. const int3 sizes = gs->getMapSize();
  34. const auto & fow = static_cast<const CGameInfoCallback *>(gs)->getPlayerTeam(player)->fogOfWarMap;
  35. //make 200% sure that these are loop invariants (also a bit shorter code), let compiler do the rest(loop unswitching)
  36. const bool useFlying = options.useFlying;
  37. const bool useWaterWalking = options.useWaterWalking;
  38. for(pos.x=0; pos.x < sizes.x; ++pos.x)
  39. {
  40. for(pos.y=0; pos.y < sizes.y; ++pos.y)
  41. {
  42. for(pos.z=0; pos.z < sizes.z; ++pos.z)
  43. {
  44. const TerrainTile * tile = &gs->map->getTile(pos);
  45. switch(tile->terType)
  46. {
  47. case ETerrainType::ROCK:
  48. break;
  49. case ETerrainType::WATER:
  50. resetTile(pos, ELayer::SAIL, PathfinderUtil::evaluateAccessibility<ELayer::SAIL>(pos, tile, fow, player, gs));
  51. if(useFlying)
  52. resetTile(pos, ELayer::AIR, PathfinderUtil::evaluateAccessibility<ELayer::AIR>(pos, tile, fow, player, gs));
  53. if(useWaterWalking)
  54. resetTile(pos, ELayer::WATER, PathfinderUtil::evaluateAccessibility<ELayer::WATER>(pos, tile, fow, player, gs));
  55. break;
  56. default:
  57. resetTile(pos, ELayer::LAND, PathfinderUtil::evaluateAccessibility<ELayer::LAND>(pos, tile, fow, player, gs));
  58. if(useFlying)
  59. resetTile(pos, ELayer::AIR, PathfinderUtil::evaluateAccessibility<ELayer::AIR>(pos, tile, fow, player, gs));
  60. break;
  61. }
  62. }
  63. }
  64. }
  65. }
  66. void AINodeStorage::clear()
  67. {
  68. actors.clear();
  69. heroChainPass = false;
  70. }
  71. const AIPathNode * AINodeStorage::getAINode(const CGPathNode * node) const
  72. {
  73. return static_cast<const AIPathNode *>(node);
  74. }
  75. void AINodeStorage::updateAINode(CGPathNode * node, std::function<void(AIPathNode *)> updater)
  76. {
  77. auto aiNode = static_cast<AIPathNode *>(node);
  78. updater(aiNode);
  79. }
  80. boost::optional<AIPathNode *> AINodeStorage::getOrCreateNode(
  81. const int3 & pos,
  82. const EPathfindingLayer layer,
  83. const ChainActor * actor)
  84. {
  85. auto chains = nodes[pos.x][pos.y][pos.z][layer];
  86. for(AIPathNode & node : chains)
  87. {
  88. if(node.actor == actor)
  89. {
  90. return &node;
  91. }
  92. if(!node.actor)
  93. {
  94. node.actor = actor;
  95. return &node;
  96. }
  97. }
  98. return boost::none;
  99. }
  100. std::vector<CGPathNode *> AINodeStorage::getInitialNodes()
  101. {
  102. if(heroChainPass)
  103. return heroChain;
  104. std::vector<CGPathNode *> initialNodes;
  105. for(auto actorPtr : actors)
  106. {
  107. ChainActor * actor = actorPtr.get();
  108. AIPathNode * initialNode =
  109. getOrCreateNode(actor->initialPosition, actor->layer, actor)
  110. .get();
  111. initialNode->turns = actor->initialTurn;
  112. initialNode->moveRemains = actor->initialMovement;
  113. initialNode->danger = 0;
  114. initialNode->cost = actor->initialTurn;
  115. initialNode->action = CGPathNode::ENodeAction::NORMAL;
  116. if(actor->isMovable)
  117. {
  118. initialNodes.push_back(initialNode);
  119. }
  120. else
  121. {
  122. initialNode->locked = true;
  123. }
  124. }
  125. return initialNodes;
  126. }
  127. void AINodeStorage::resetTile(const int3 & coord, EPathfindingLayer layer, CGPathNode::EAccessibility accessibility)
  128. {
  129. for(int i = 0; i < NUM_CHAINS; i++)
  130. {
  131. AIPathNode & heroNode = nodes[coord.x][coord.y][coord.z][layer][i];
  132. heroNode.actor = nullptr;
  133. heroNode.danger = 0;
  134. heroNode.manaCost = 0;
  135. heroNode.specialAction.reset();
  136. heroNode.armyLoss = 0;
  137. heroNode.chainOther = nullptr;
  138. heroNode.update(coord, layer, accessibility);
  139. }
  140. }
  141. void AINodeStorage::commit(CDestinationNodeInfo & destination, const PathNodeInfo & source)
  142. {
  143. const AIPathNode * srcNode = getAINode(source.node);
  144. updateAINode(destination.node, [&](AIPathNode * dstNode)
  145. {
  146. commit(dstNode, srcNode, destination.action, destination.turn, destination.movementLeft, destination.cost);
  147. if(dstNode->specialAction && dstNode->actor)
  148. {
  149. dstNode->specialAction->applyOnDestination(dstNode->actor->hero, destination, source, dstNode, srcNode);
  150. }
  151. #ifdef VCMI_TRACE_PATHFINDER_EX
  152. logAi->trace(
  153. "Commited %s -> %s, cost: %f, hero: %s, mask: %i",
  154. source.coord.toString(),
  155. destination.coord.toString(),
  156. destination.cost,
  157. dstNode->actor->hero->name,
  158. dstNode->actor->chainMask);
  159. #endif
  160. });
  161. }
  162. void AINodeStorage::commit(
  163. AIPathNode * destination,
  164. const AIPathNode * source,
  165. CGPathNode::ENodeAction action,
  166. int turn,
  167. int movementLeft,
  168. float cost) const
  169. {
  170. destination->action = action;
  171. destination->cost = cost;
  172. destination->moveRemains = movementLeft;
  173. destination->turns = turn;
  174. destination->armyLoss = source->armyLoss;
  175. destination->manaCost = source->manaCost;
  176. destination->danger = source->danger;
  177. destination->theNodeBefore = source->theNodeBefore;
  178. destination->chainOther = nullptr;
  179. }
  180. std::vector<CGPathNode *> AINodeStorage::calculateNeighbours(
  181. const PathNodeInfo & source,
  182. const PathfinderConfig * pathfinderConfig,
  183. const CPathfinderHelper * pathfinderHelper)
  184. {
  185. std::vector<CGPathNode *> neighbours;
  186. neighbours.reserve(16);
  187. const AIPathNode * srcNode = getAINode(source.node);
  188. auto accessibleNeighbourTiles = pathfinderHelper->getNeighbourTiles(source);
  189. for(auto & neighbour : accessibleNeighbourTiles)
  190. {
  191. for(EPathfindingLayer i = EPathfindingLayer::LAND; i <= EPathfindingLayer::AIR; i.advance(1))
  192. {
  193. auto nextNode = getOrCreateNode(neighbour, i, srcNode->actor);
  194. if(!nextNode || nextNode.get()->accessible == CGPathNode::NOT_SET)
  195. continue;
  196. neighbours.push_back(nextNode.get());
  197. }
  198. }
  199. return neighbours;
  200. }
  201. bool AINodeStorage::calculateHeroChain()
  202. {
  203. heroChainPass = true;
  204. heroChain.resize(0);
  205. std::vector<AIPathNode *> buffer;
  206. buffer.reserve(NUM_CHAINS);
  207. foreach_tile_pos([&](const int3 & pos) {
  208. auto layer = EPathfindingLayer::LAND;
  209. auto chains = nodes[pos.x][pos.y][pos.z][layer];
  210. buffer.resize(0);
  211. for(AIPathNode & node : chains)
  212. {
  213. if(node.turns <= heroChainMaxTurns && node.action != CGPathNode::ENodeAction::UNKNOWN)
  214. buffer.push_back(&node);
  215. }
  216. for(AIPathNode * node : buffer)
  217. {
  218. addHeroChain(node, buffer);
  219. }
  220. });
  221. return heroChain.size();
  222. }
  223. void AINodeStorage::addHeroChain(AIPathNode * srcNode, std::vector<AIPathNode *> variants)
  224. {
  225. for(AIPathNode * node : variants)
  226. {
  227. if(node == srcNode || !node->actor || node->turns > heroChainMaxTurns
  228. || node->action == CGPathNode::ENodeAction::UNKNOWN && node->actor->hero)
  229. {
  230. continue;
  231. }
  232. #ifdef VCMI_TRACE_PATHFINDER_EX
  233. logAi->trace(
  234. "Thy exchange %s[%i] -> %s[%i] at %s",
  235. node->actor->hero->name,
  236. node->actor->chainMask,
  237. srcNode->actor->hero->name,
  238. srcNode->actor->chainMask,
  239. srcNode->coord.toString());
  240. #endif
  241. addHeroChain(srcNode, node);
  242. //addHeroChain(&node, srcNode);
  243. }
  244. }
  245. void AINodeStorage::addHeroChain(AIPathNode * carrier, AIPathNode * other)
  246. {
  247. if(!carrier->actor->isMovable)
  248. return;
  249. if(carrier->actor->canExchange(other->actor))
  250. {
  251. #ifdef VCMI_TRACE_PATHFINDER_EX
  252. logAi->trace(
  253. "Exchange allowed %s[%i] -> %s[%i] at %s",
  254. other->actor->hero->name,
  255. other->actor->chainMask,
  256. carrier->actor->hero->name,
  257. carrier->actor->chainMask,
  258. carrier->coord.toString());
  259. #endif
  260. bool hasLessMp = carrier->turns > other->turns || carrier->moveRemains < other->moveRemains;
  261. bool hasLessExperience = carrier->actor->hero->exp < other->actor->hero->exp;
  262. if(hasLessMp && hasLessExperience)
  263. {
  264. #ifdef VCMI_TRACE_PATHFINDER_EX
  265. logAi->trace("Exchange at %s is ineficient. Blocked.", carrier->coord.toString());
  266. #endif
  267. return;
  268. }
  269. auto newActor = carrier->actor->exchange(other->actor);
  270. auto chainNodeOptional = getOrCreateNode(carrier->coord, carrier->layer, newActor);
  271. if(!chainNodeOptional)
  272. {
  273. #ifdef VCMI_TRACE_PATHFINDER_EX
  274. logAi->trace("Exchange at %s can not allocate node. Blocked.", carrier->coord.toString());
  275. #endif
  276. return;
  277. }
  278. auto chainNode = chainNodeOptional.get();
  279. if(chainNode->action != CGPathNode::ENodeAction::UNKNOWN)
  280. {
  281. #ifdef VCMI_TRACE_PATHFINDER_EX
  282. logAi->trace("Exchange at %s node is already in use. Blocked.", carrier->coord.toString());
  283. #endif
  284. return;
  285. }
  286. if(commitExchange(chainNode, carrier, other))
  287. {
  288. #ifdef VCMI_TRACE_PATHFINDER_EX
  289. logAi->trace(
  290. "Chain accepted at %s %s -> %s, mask %i, cost %f",
  291. chainNode->coord.toString(),
  292. other->actor->hero->name,
  293. chainNode->actor->hero->name,
  294. chainNode->actor->chainMask,
  295. chainNode->cost);
  296. #endif
  297. heroChain.push_back(chainNode);
  298. }
  299. }
  300. }
  301. bool AINodeStorage::commitExchange(
  302. AIPathNode * exchangeNode,
  303. AIPathNode * carrierParentNode,
  304. AIPathNode * otherParentNode) const
  305. {
  306. auto carrierActor = carrierParentNode->actor;
  307. auto exchangeActor = exchangeNode->actor;
  308. auto otherActor = otherParentNode->actor;
  309. auto armyLoss = carrierParentNode->armyLoss + otherParentNode->armyLoss;
  310. auto turns = carrierParentNode->turns;
  311. auto cost = carrierParentNode->cost + otherParentNode->cost / 1000.0;
  312. auto movementLeft = carrierParentNode->moveRemains;
  313. if(carrierParentNode->turns < otherParentNode->turns)
  314. {
  315. int moveRemains = exchangeActor->hero->maxMovePoints(exchangeNode->layer);
  316. float waitingCost = otherParentNode->turns - carrierParentNode->turns - 1
  317. + carrierParentNode->moveRemains / (float)moveRemains;
  318. turns = otherParentNode->turns;
  319. cost += waitingCost;
  320. movementLeft = moveRemains;
  321. }
  322. if(exchangeNode->turns != 0xFF && exchangeNode->cost < cost)
  323. {
  324. #ifdef VCMI_TRACE_PATHFINDER_EX
  325. logAi->trace("Exchange at %s is is not effective enough. %f < %f", exchangeNode->coord.toString(), exchangeNode->cost, cost);
  326. #endif
  327. return false;
  328. }
  329. commit(exchangeNode, carrierParentNode, carrierParentNode->action, turns, movementLeft, cost);
  330. exchangeNode->chainOther = otherParentNode;
  331. exchangeNode->armyLoss = armyLoss;
  332. return true;
  333. }
  334. const CGHeroInstance * AINodeStorage::getHero(const CGPathNode * node) const
  335. {
  336. auto aiNode = getAINode(node);
  337. return aiNode->actor->hero;
  338. }
  339. const std::set<const CGHeroInstance *> AINodeStorage::getAllHeroes() const
  340. {
  341. std::set<const CGHeroInstance *> heroes;
  342. for(auto actor : actors)
  343. {
  344. if(actor->hero)
  345. heroes.insert(actor->hero);
  346. }
  347. return heroes;
  348. }
  349. void AINodeStorage::setHeroes(std::vector<HeroPtr> heroes, const VCAI * _ai)
  350. {
  351. cb = _ai->myCb.get();
  352. ai = _ai;
  353. for(auto & hero : heroes)
  354. {
  355. uint64_t mask = 1 << actors.size();
  356. actors.push_back(std::make_shared<HeroActor>(hero.get(), mask, ai));
  357. }
  358. }
  359. void AINodeStorage::setTownsAndDwellings(
  360. const std::vector<const CGTownInstance *> & towns,
  361. const std::vector<const CGObjectInstance *> & visitableObjs)
  362. {
  363. for(auto town : towns)
  364. {
  365. uint64_t mask = 1 << actors.size();
  366. if(town->getUpperArmy()->getArmyStrength())
  367. {
  368. actors.push_back(std::make_shared<TownGarrisonActor>(town, mask));
  369. }
  370. }
  371. auto dayOfWeek = cb->getDate(Date::DAY_OF_WEEK);
  372. auto waitForGrowth = dayOfWeek > 4;
  373. for(auto obj: visitableObjs)
  374. {
  375. const CGDwelling * dwelling = dynamic_cast<const CGDwelling *>(obj);
  376. if(dwelling)
  377. {
  378. uint64_t mask = 1 << actors.size();
  379. auto dwellingActor = std::make_shared<DwellingActor>(dwelling, mask, false, dayOfWeek);
  380. if(dwellingActor->creatureSet->getArmyStrength())
  381. {
  382. actors.push_back(dwellingActor);
  383. }
  384. if(waitForGrowth)
  385. {
  386. mask = 1 << actors.size();
  387. dwellingActor = std::make_shared<DwellingActor>(dwelling, mask, waitForGrowth, dayOfWeek);
  388. if(dwellingActor->creatureSet->getArmyStrength())
  389. {
  390. actors.push_back(dwellingActor);
  391. }
  392. }
  393. }
  394. }
  395. }
  396. std::vector<CGPathNode *> AINodeStorage::calculateTeleportations(
  397. const PathNodeInfo & source,
  398. const PathfinderConfig * pathfinderConfig,
  399. const CPathfinderHelper * pathfinderHelper)
  400. {
  401. std::vector<CGPathNode *> neighbours;
  402. if(source.isNodeObjectVisitable())
  403. {
  404. auto accessibleExits = pathfinderHelper->getTeleportExits(source);
  405. auto srcNode = getAINode(source.node);
  406. for(auto & neighbour : accessibleExits)
  407. {
  408. auto node = getOrCreateNode(neighbour, source.node->layer, srcNode->actor);
  409. if(!node)
  410. continue;
  411. neighbours.push_back(node.get());
  412. }
  413. }
  414. if(source.isInitialPosition)
  415. {
  416. calculateTownPortalTeleportations(source, neighbours);
  417. }
  418. return neighbours;
  419. }
  420. void AINodeStorage::calculateTownPortalTeleportations(
  421. const PathNodeInfo & source,
  422. std::vector<CGPathNode *> & neighbours)
  423. {
  424. SpellID spellID = SpellID::TOWN_PORTAL;
  425. const CSpell * townPortal = spellID.toSpell();
  426. auto srcNode = getAINode(source.node);
  427. auto hero = srcNode->actor->hero;
  428. if(hero->canCastThisSpell(townPortal) && hero->mana >= hero->getSpellCost(townPortal))
  429. {
  430. auto towns = cb->getTownsInfo(false);
  431. vstd::erase_if(towns, [&](const CGTownInstance * t) -> bool
  432. {
  433. return cb->getPlayerRelations(hero->tempOwner, t->tempOwner) == PlayerRelations::ENEMIES;
  434. });
  435. if(!towns.size())
  436. {
  437. return;
  438. }
  439. // TODO: Copy/Paste from TownPortalMechanics
  440. auto skillLevel = hero->getSpellSchoolLevel(townPortal);
  441. auto movementCost = GameConstants::BASE_MOVEMENT_COST * (skillLevel >= 3 ? 2 : 3);
  442. if(hero->movement < movementCost)
  443. {
  444. return;
  445. }
  446. if(skillLevel < SecSkillLevel::ADVANCED)
  447. {
  448. const CGTownInstance * nearestTown = *vstd::minElementByFun(towns, [&](const CGTownInstance * t) -> int
  449. {
  450. return hero->visitablePos().dist2dSQ(t->visitablePos());
  451. });
  452. towns = std::vector<const CGTownInstance *>{ nearestTown };
  453. }
  454. for(const CGTownInstance * targetTown : towns)
  455. {
  456. if(targetTown->visitingHero)
  457. continue;
  458. auto nodeOptional = getOrCreateNode(targetTown->visitablePos(), EPathfindingLayer::LAND, srcNode->actor->castActor);
  459. if(nodeOptional)
  460. {
  461. #ifdef VCMI_TRACE_PATHFINDER
  462. logAi->trace("Adding town portal node at %s", targetTown->name);
  463. #endif
  464. AIPathNode * node = nodeOptional.get();
  465. node->theNodeBefore = source.node;
  466. node->specialAction.reset(new AIPathfinding::TownPortalAction(targetTown));
  467. node->moveRemains = source.node->moveRemains;
  468. neighbours.push_back(node);
  469. }
  470. }
  471. }
  472. }
  473. bool AINodeStorage::hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) const
  474. {
  475. auto pos = destination.coord;
  476. auto chains = nodes[pos.x][pos.y][pos.z][EPathfindingLayer::LAND];
  477. auto destinationNode = getAINode(destination.node);
  478. for(const AIPathNode & node : chains)
  479. {
  480. auto sameNode = node.actor == destinationNode->actor;
  481. if(sameNode || node.action == CGPathNode::ENodeAction::UNKNOWN)
  482. {
  483. continue;
  484. }
  485. if(node.danger <= destinationNode->danger && destinationNode->actor == node.actor->battleActor)
  486. {
  487. if(node.cost < destinationNode->cost)
  488. {
  489. #ifdef VCMI_TRACE_PATHFINDER
  490. logAi->trace(
  491. "Block ineficient move %s:->%s, mask=%i, mp diff: %i",
  492. source.coord.toString(),
  493. destination.coord.toString(),
  494. destinationNode->actor->chainMask,
  495. node.moveRemains - destinationNode->moveRemains);
  496. #endif
  497. return true;
  498. }
  499. }
  500. }
  501. return false;
  502. }
  503. bool AINodeStorage::isTileAccessible(const HeroPtr & hero, const int3 & pos, const EPathfindingLayer layer) const
  504. {
  505. auto chains = nodes[pos.x][pos.y][pos.z][layer];
  506. for(const AIPathNode & node : chains)
  507. {
  508. if(node.action != CGPathNode::ENodeAction::UNKNOWN
  509. && node.actor && node.actor->hero == hero.h)
  510. {
  511. return true;
  512. }
  513. }
  514. return false;
  515. }
  516. std::vector<AIPath> AINodeStorage::getChainInfo(const int3 & pos, bool isOnLand) const
  517. {
  518. std::vector<AIPath> paths;
  519. paths.reserve(NUM_CHAINS / 4);
  520. auto chains = nodes[pos.x][pos.y][pos.z][isOnLand ? EPathfindingLayer::LAND : EPathfindingLayer::SAIL];
  521. for(const AIPathNode & node : chains)
  522. {
  523. if(node.action == CGPathNode::ENodeAction::UNKNOWN || !node.actor || !node.actor->hero)
  524. {
  525. continue;
  526. }
  527. AIPath path;
  528. path.targetHero = node.actor->hero;
  529. path.heroArmy = node.actor->creatureSet;
  530. path.armyLoss = node.armyLoss;
  531. path.targetObjectDanger = evaluateDanger(pos, path.targetHero);
  532. path.chainMask = node.actor->chainMask;
  533. fillChainInfo(&node, path);
  534. paths.push_back(path);
  535. }
  536. return paths;
  537. }
  538. void AINodeStorage::fillChainInfo(const AIPathNode * node, AIPath & path) const
  539. {
  540. while(node != nullptr)
  541. {
  542. if(!node->actor->hero)
  543. return;
  544. if(node->chainOther)
  545. fillChainInfo(node->chainOther, path);
  546. if(node->actor->hero->visitablePos() != node->coord)
  547. {
  548. AIPathNodeInfo pathNode;
  549. pathNode.cost = node->cost;
  550. pathNode.targetHero = node->actor->hero;
  551. pathNode.turns = node->turns;
  552. pathNode.danger = node->danger;
  553. pathNode.coord = node->coord;
  554. path.nodes.push_back(pathNode);
  555. }
  556. path.specialAction = node->specialAction;
  557. node = getAINode(node->theNodeBefore);
  558. }
  559. }
  560. AIPath::AIPath()
  561. : nodes({})
  562. {
  563. }
  564. int3 AIPath::firstTileToGet() const
  565. {
  566. if(nodes.size())
  567. {
  568. return nodes.back().coord;
  569. }
  570. return int3(-1, -1, -1);
  571. }
  572. const AIPathNodeInfo & AIPath::firstNode() const
  573. {
  574. return nodes.back();
  575. }
  576. uint64_t AIPath::getPathDanger() const
  577. {
  578. if(nodes.size())
  579. {
  580. return nodes.front().danger;
  581. }
  582. return 0;
  583. }
  584. float AIPath::movementCost() const
  585. {
  586. if(nodes.size())
  587. {
  588. return nodes.front().cost;
  589. }
  590. // TODO: boost:optional?
  591. return 0.0;
  592. }
  593. uint64_t AIPath::getHeroStrength() const
  594. {
  595. return targetHero->getFightingStrength() * heroArmy->getArmyStrength();
  596. }
  597. uint64_t AIPath::getTotalDanger(HeroPtr hero) const
  598. {
  599. uint64_t pathDanger = getPathDanger();
  600. uint64_t danger = pathDanger > targetObjectDanger ? pathDanger : targetObjectDanger;
  601. return danger;
  602. }