AINodeStorage.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 "../Goals/Goals.h"
  13. #include "../../../CCallback.h"
  14. #include "../../../lib/mapping/CMap.h"
  15. #include "../../../lib/mapObjects/MapObjects.h"
  16. extern boost::thread_specific_ptr<CCallback> cb;
  17. AINodeStorage::AINodeStorage(const int3 & Sizes)
  18. : sizes(Sizes)
  19. {
  20. nodes.resize(boost::extents[sizes.x][sizes.y][sizes.z][EPathfindingLayer::NUM_LAYERS][NUM_CHAINS]);
  21. }
  22. AINodeStorage::~AINodeStorage()
  23. {
  24. }
  25. const AIPathNode * AINodeStorage::getAINode(const CGPathNode * node) const
  26. {
  27. return static_cast<const AIPathNode *>(node);
  28. }
  29. void AINodeStorage::updateAINode(CGPathNode * node, std::function<void(AIPathNode *)> updater)
  30. {
  31. auto aiNode = static_cast<AIPathNode *>(node);
  32. updater(aiNode);
  33. }
  34. bool AINodeStorage::isBattleNode(const CGPathNode * node) const
  35. {
  36. return (getAINode(node)->chainMask & BATTLE_CHAIN) > 0;
  37. }
  38. boost::optional<AIPathNode *> AINodeStorage::getOrCreateNode(const int3 & pos, const EPathfindingLayer layer, int chainNumber)
  39. {
  40. auto chains = nodes[pos.x][pos.y][pos.z][layer];
  41. for(AIPathNode & node : chains)
  42. {
  43. if(node.chainMask == chainNumber)
  44. {
  45. return &node;
  46. }
  47. if(node.chainMask == 0)
  48. {
  49. node.chainMask = chainNumber;
  50. return &node;
  51. }
  52. }
  53. return boost::none;
  54. }
  55. CGPathNode * AINodeStorage::getInitialNode()
  56. {
  57. auto hpos = hero->getPosition(false);
  58. auto initialNode =
  59. getOrCreateNode(hpos, hero->boat ? EPathfindingLayer::SAIL : EPathfindingLayer::LAND, NORMAL_CHAIN)
  60. .get();
  61. initialNode->turns = 0;
  62. initialNode->moveRemains = hero->movement;
  63. initialNode->danger = 0;
  64. return initialNode;
  65. }
  66. void AINodeStorage::resetTile(const int3 & coord, EPathfindingLayer layer, CGPathNode::EAccessibility accessibility)
  67. {
  68. for(int i = 0; i < NUM_CHAINS; i++)
  69. {
  70. AIPathNode & heroNode = nodes[coord.x][coord.y][coord.z][layer][i];
  71. heroNode.chainMask = 0;
  72. heroNode.danger = 0;
  73. heroNode.manaCost = 0;
  74. heroNode.specialAction.reset();
  75. heroNode.update(coord, layer, accessibility);
  76. }
  77. }
  78. void AINodeStorage::commit(CDestinationNodeInfo & destination, const PathNodeInfo & source)
  79. {
  80. const AIPathNode * srcNode = getAINode(source.node);
  81. updateAINode(destination.node, [&](AIPathNode * dstNode) {
  82. dstNode->moveRemains = destination.movementLeft;
  83. dstNode->turns = destination.turn;
  84. dstNode->danger = srcNode->danger;
  85. dstNode->action = destination.action;
  86. dstNode->theNodeBefore = srcNode->theNodeBefore;
  87. dstNode->manaCost = srcNode->manaCost;
  88. if(dstNode->specialAction)
  89. {
  90. dstNode->specialAction->applyOnDestination(getHero(), destination, source, dstNode, srcNode);
  91. }
  92. });
  93. }
  94. std::vector<CGPathNode *> AINodeStorage::calculateNeighbours(
  95. const PathNodeInfo & source,
  96. const PathfinderConfig * pathfinderConfig,
  97. const CPathfinderHelper * pathfinderHelper)
  98. {
  99. std::vector<CGPathNode *> neighbours;
  100. neighbours.reserve(16);
  101. const AIPathNode * srcNode = getAINode(source.node);
  102. auto accessibleNeighbourTiles = pathfinderHelper->getNeighbourTiles(source);
  103. for(auto & neighbour : accessibleNeighbourTiles)
  104. {
  105. for(EPathfindingLayer i = EPathfindingLayer::LAND; i <= EPathfindingLayer::AIR; i.advance(1))
  106. {
  107. auto nextNode = getOrCreateNode(neighbour, i, srcNode->chainMask);
  108. if(!nextNode || nextNode.get()->accessible == CGPathNode::NOT_SET)
  109. continue;
  110. neighbours.push_back(nextNode.get());
  111. }
  112. }
  113. return neighbours;
  114. }
  115. void AINodeStorage::setHero(HeroPtr heroPtr)
  116. {
  117. hero = heroPtr.get();
  118. }
  119. class TownPortalAction : public ISpecialAction
  120. {
  121. private:
  122. const CGTownInstance * target;
  123. const HeroPtr hero;
  124. public:
  125. TownPortalAction(const CGTownInstance * target)
  126. :target(target)
  127. {
  128. }
  129. virtual Goals::TSubgoal whatToDo(HeroPtr hero) const override
  130. {
  131. const CGTownInstance * targetTown = target; // const pointer is not allowed in settown
  132. return sptr(Goals::AdventureSpellCast(hero, SpellID::TOWN_PORTAL).settown(targetTown).settile(targetTown->visitablePos()));
  133. }
  134. };
  135. std::vector<CGPathNode *> AINodeStorage::calculateTeleportations(
  136. const PathNodeInfo & source,
  137. const PathfinderConfig * pathfinderConfig,
  138. const CPathfinderHelper * pathfinderHelper)
  139. {
  140. std::vector<CGPathNode *> neighbours;
  141. if(source.isNodeObjectVisitable())
  142. {
  143. auto accessibleExits = pathfinderHelper->getTeleportExits(source);
  144. auto srcNode = getAINode(source.node);
  145. for(auto & neighbour : accessibleExits)
  146. {
  147. auto node = getOrCreateNode(neighbour, source.node->layer, srcNode->chainMask);
  148. if(!node)
  149. continue;
  150. neighbours.push_back(node.get());
  151. }
  152. }
  153. if(hero->getPosition(false) == source.coord)
  154. {
  155. calculateTownPortalTeleportations(source, neighbours);
  156. }
  157. return neighbours;
  158. }
  159. void AINodeStorage::calculateTownPortalTeleportations(
  160. const PathNodeInfo & source,
  161. std::vector<CGPathNode *> & neighbours)
  162. {
  163. SpellID spellID = SpellID::TOWN_PORTAL;
  164. const CSpell * townPortal = spellID.toSpell();
  165. auto srcNode = getAINode(source.node);
  166. if(hero->canCastThisSpell(townPortal) && hero->mana >= hero->getSpellCost(townPortal))
  167. {
  168. auto towns = cb->getTownsInfo(false);
  169. vstd::erase_if(towns, [&](const CGTownInstance * t) -> bool
  170. {
  171. return cb->getPlayerRelations(hero->tempOwner, t->tempOwner) == PlayerRelations::ENEMIES;
  172. });
  173. if(!towns.size())
  174. {
  175. return;
  176. }
  177. // TODO: Copy/Paste from TownPortalMechanics
  178. auto skillLevel = hero->getSpellSchoolLevel(townPortal);
  179. auto movementCost = GameConstants::BASE_MOVEMENT_COST * (skillLevel >= 3 ? 2 : 3);
  180. if(hero->movement < movementCost)
  181. {
  182. return;
  183. }
  184. if(skillLevel < SecSkillLevel::ADVANCED)
  185. {
  186. const CGTownInstance * nearestTown = *vstd::minElementByFun(towns, [&](const CGTownInstance * t) -> int
  187. {
  188. return hero->visitablePos().dist2dSQ(t->visitablePos());
  189. });
  190. towns = std::vector<const CGTownInstance *>{ nearestTown };
  191. }
  192. for(const CGTownInstance * targetTown : towns)
  193. {
  194. if(targetTown->visitingHero)
  195. continue;
  196. auto nodeOptional = getOrCreateNode(targetTown->visitablePos(), EPathfindingLayer::LAND, srcNode->chainMask | CAST_CHAIN);
  197. if(nodeOptional)
  198. {
  199. #ifdef VCMI_TRACE_PATHFINDER
  200. logAi->trace("Adding town portal node at %s", targetTown->name);
  201. #endif
  202. AIPathNode * node = nodeOptional.get();
  203. node->theNodeBefore = source.node;
  204. node->specialAction.reset(new TownPortalAction(targetTown));
  205. node->moveRemains = source.node->moveRemains;
  206. neighbours.push_back(node);
  207. }
  208. }
  209. }
  210. }
  211. bool AINodeStorage::hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) const
  212. {
  213. auto pos = destination.coord;
  214. auto chains = nodes[pos.x][pos.y][pos.z][EPathfindingLayer::LAND];
  215. auto destinationNode = getAINode(destination.node);
  216. for(const AIPathNode & node : chains)
  217. {
  218. auto sameNode = node.chainMask == destinationNode->chainMask;
  219. if(sameNode || node.action == CGPathNode::ENodeAction::UNKNOWN)
  220. {
  221. continue;
  222. }
  223. if(node.danger <= destinationNode->danger && destinationNode->chainMask == 1 && node.chainMask == 0)
  224. {
  225. if(node.turns < destinationNode->turns
  226. || (node.turns == destinationNode->turns && node.moveRemains >= destinationNode->moveRemains))
  227. {
  228. #ifdef VCMI_TRACE_PATHFINDER
  229. logAi->trace(
  230. "Block ineficient move %s:->%s, mask=%i, mp diff: %i",
  231. source.coord.toString(),
  232. destination.coord.toString(),
  233. destinationNode->chainMask,
  234. node.moveRemains - destinationNode->moveRemains);
  235. #endif
  236. return true;
  237. }
  238. }
  239. }
  240. return false;
  241. }
  242. bool AINodeStorage::isTileAccessible(int3 pos, const EPathfindingLayer layer) const
  243. {
  244. std::vector<AIPath> paths;
  245. auto chains = nodes[pos.x][pos.y][pos.z][layer];
  246. for(const AIPathNode & node : chains)
  247. {
  248. if(node.action != CGPathNode::ENodeAction::UNKNOWN)
  249. {
  250. return true;
  251. }
  252. }
  253. return false;
  254. }
  255. std::vector<AIPath> AINodeStorage::getChainInfo(int3 pos, bool isOnLand) const
  256. {
  257. std::vector<AIPath> paths;
  258. auto chains = nodes[pos.x][pos.y][pos.z][isOnLand ? EPathfindingLayer::LAND : EPathfindingLayer::SAIL];
  259. auto initialPos = hero->visitablePos();
  260. for(const AIPathNode & node : chains)
  261. {
  262. if(node.action == CGPathNode::ENodeAction::UNKNOWN)
  263. {
  264. continue;
  265. }
  266. AIPath path;
  267. const AIPathNode * current = &node;
  268. while(current != nullptr && current->coord != initialPos)
  269. {
  270. AIPathNodeInfo pathNode;
  271. pathNode.movementPointsLeft = current->moveRemains;
  272. pathNode.movementPointsUsed = (int)(current->turns * hero->maxMovePoints(true) + hero->movement) - (int)current->moveRemains;
  273. pathNode.turns = current->turns;
  274. pathNode.danger = current->danger;
  275. pathNode.coord = current->coord;
  276. path.nodes.push_back(pathNode);
  277. path.specialAction = current->specialAction;
  278. current = getAINode(current->theNodeBefore);
  279. }
  280. paths.push_back(path);
  281. }
  282. return paths;
  283. }
  284. AIPath::AIPath()
  285. : nodes({})
  286. {
  287. }
  288. int3 AIPath::firstTileToGet() const
  289. {
  290. if(nodes.size())
  291. {
  292. return nodes.back().coord;
  293. }
  294. return int3(-1, -1, -1);
  295. }
  296. uint64_t AIPath::getPathDanger() const
  297. {
  298. if(nodes.size())
  299. {
  300. return nodes.front().danger;
  301. }
  302. return 0;
  303. }
  304. uint32_t AIPath::movementCost() const
  305. {
  306. if(nodes.size())
  307. {
  308. return nodes.front().movementPointsUsed;
  309. }
  310. // TODO: boost:optional?
  311. return 0;
  312. }
  313. uint64_t AIPath::getTotalDanger(HeroPtr hero) const
  314. {
  315. uint64_t pathDanger = getPathDanger();
  316. uint64_t objDanger = evaluateDanger(nodes.front().coord, hero.get()); // bank danger is not checked by pathfinder
  317. uint64_t danger = pathDanger > objDanger ? pathDanger : objDanger;
  318. return danger;
  319. }