AINodeStorage.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. AINodeStorage::AINodeStorage(const int3 & Sizes)
  13. : sizes(Sizes)
  14. {
  15. nodes.resize(boost::extents[sizes.x][sizes.y][sizes.z][EPathfindingLayer::NUM_LAYERS][NUM_CHAINS]);
  16. }
  17. AINodeStorage::~AINodeStorage()
  18. {
  19. }
  20. const AIPathNode * AINodeStorage::getAINode(const CGPathNode * node) const
  21. {
  22. return static_cast<const AIPathNode *>(node);
  23. }
  24. void AINodeStorage::updateAINode(CGPathNode * node, std::function<void(AIPathNode *)> updater)
  25. {
  26. auto aiNode = static_cast<AIPathNode *>(node);
  27. updater(aiNode);
  28. }
  29. bool AINodeStorage::isBattleNode(const CGPathNode * node) const
  30. {
  31. return (getAINode(node)->chainMask & BATTLE_CHAIN) > 0;
  32. }
  33. boost::optional<AIPathNode *> AINodeStorage::getOrCreateNode(const int3 & pos, const EPathfindingLayer layer, int chainNumber)
  34. {
  35. auto chains = nodes[pos.x][pos.y][pos.z][layer];
  36. for(AIPathNode & node : chains)
  37. {
  38. if(node.chainMask == chainNumber)
  39. {
  40. return &node;
  41. }
  42. if(node.chainMask == 0)
  43. {
  44. node.chainMask = chainNumber;
  45. return &node;
  46. }
  47. }
  48. return boost::none;
  49. }
  50. CGPathNode * AINodeStorage::getInitialNode()
  51. {
  52. auto hpos = hero->getPosition(false);
  53. auto initialNode =
  54. getOrCreateNode(hpos, hero->boat ? EPathfindingLayer::SAIL : EPathfindingLayer::LAND, NORMAL_CHAIN)
  55. .get();
  56. initialNode->turns = 0;
  57. initialNode->moveRemains = hero->movement;
  58. initialNode->danger = 0;
  59. return initialNode;
  60. }
  61. void AINodeStorage::resetTile(const int3 & coord, EPathfindingLayer layer, CGPathNode::EAccessibility accessibility)
  62. {
  63. for(int i = 0; i < NUM_CHAINS; i++)
  64. {
  65. AIPathNode & heroNode = nodes[coord.x][coord.y][coord.z][layer][i];
  66. heroNode.chainMask = 0;
  67. heroNode.danger = 0;
  68. heroNode.specialAction.reset();
  69. heroNode.update(coord, layer, accessibility);
  70. }
  71. }
  72. void AINodeStorage::commit(CDestinationNodeInfo & destination, const PathNodeInfo & source)
  73. {
  74. const AIPathNode * srcNode = getAINode(source.node);
  75. updateAINode(destination.node, [&](AIPathNode * dstNode) {
  76. dstNode->moveRemains = destination.movementLeft;
  77. dstNode->turns = destination.turn;
  78. dstNode->danger = srcNode->danger;
  79. dstNode->action = destination.action;
  80. dstNode->theNodeBefore = srcNode->theNodeBefore;
  81. });
  82. }
  83. std::vector<CGPathNode *> AINodeStorage::calculateNeighbours(
  84. const PathNodeInfo & source,
  85. const PathfinderConfig * pathfinderConfig,
  86. const CPathfinderHelper * pathfinderHelper)
  87. {
  88. std::vector<CGPathNode *> neighbours;
  89. const AIPathNode * srcNode = getAINode(source.node);
  90. auto accessibleNeighbourTiles = pathfinderHelper->getNeighbourTiles(source);
  91. for(auto & neighbour : accessibleNeighbourTiles)
  92. {
  93. for(EPathfindingLayer i = EPathfindingLayer::LAND; i <= EPathfindingLayer::AIR; i.advance(1))
  94. {
  95. auto nextNode = getOrCreateNode(neighbour, i, srcNode->chainMask);
  96. if(!nextNode || nextNode.get()->accessible == CGPathNode::NOT_SET)
  97. continue;
  98. neighbours.push_back(nextNode.get());
  99. }
  100. }
  101. return neighbours;
  102. }
  103. std::vector<CGPathNode *> AINodeStorage::calculateTeleportations(
  104. const PathNodeInfo & source,
  105. const PathfinderConfig * pathfinderConfig,
  106. const CPathfinderHelper * pathfinderHelper)
  107. {
  108. std::vector<CGPathNode *> neighbours;
  109. auto accessibleExits = pathfinderHelper->getTeleportExits(source);
  110. auto srcNode = getAINode(source.node);
  111. for(auto & neighbour : accessibleExits)
  112. {
  113. auto node = getOrCreateNode(neighbour, source.node->layer, srcNode->chainMask);
  114. if(!node)
  115. continue;
  116. neighbours.push_back(node.get());
  117. }
  118. return neighbours;
  119. }
  120. bool AINodeStorage::hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) const
  121. {
  122. auto pos = destination.coord;
  123. auto chains = nodes[pos.x][pos.y][pos.z][EPathfindingLayer::LAND];
  124. auto destinationNode = getAINode(destination.node);
  125. for(const AIPathNode & node : chains)
  126. {
  127. auto sameNode = node.chainMask == destinationNode->chainMask;
  128. if(sameNode || node.action == CGPathNode::ENodeAction::UNKNOWN)
  129. {
  130. continue;
  131. }
  132. if(node.danger <= destinationNode->danger && destinationNode->chainMask == 1 && node.chainMask == 0)
  133. {
  134. if(node.turns < destinationNode->turns
  135. || (node.turns == destinationNode->turns && node.moveRemains >= destinationNode->moveRemains))
  136. {
  137. logAi->trace(
  138. "Block ineficient move %s:->%s, mask=%i, mp diff: %i",
  139. source.coord.toString(),
  140. destination.coord.toString(),
  141. destinationNode->chainMask,
  142. node.moveRemains - destinationNode->moveRemains);
  143. return true;
  144. }
  145. }
  146. }
  147. return false;
  148. }
  149. std::vector<AIPath> AINodeStorage::getChainInfo(int3 pos, bool isOnLand) const
  150. {
  151. std::vector<AIPath> paths;
  152. auto chains = nodes[pos.x][pos.y][pos.z][isOnLand ? EPathfindingLayer::LAND : EPathfindingLayer::SAIL];
  153. for(const AIPathNode & node : chains)
  154. {
  155. if(node.action == CGPathNode::ENodeAction::UNKNOWN)
  156. {
  157. continue;
  158. }
  159. AIPath path;
  160. const AIPathNode * current = &node;
  161. while(current != nullptr)
  162. {
  163. AIPathNodeInfo pathNode;
  164. pathNode.movementPointsLeft = current->moveRemains;
  165. pathNode.movementPointsUsed = (int)(current->turns * hero->maxMovePoints(true) + hero->movement) - (int)current->moveRemains;
  166. pathNode.turns = current->turns;
  167. pathNode.danger = current->danger;
  168. pathNode.coord = current->coord;
  169. path.nodes.push_back(pathNode);
  170. path.specialAction = current->specialAction;
  171. current = getAINode(current->theNodeBefore);
  172. }
  173. paths.push_back(path);
  174. }
  175. return paths;
  176. }
  177. AIPath::AIPath()
  178. : nodes({})
  179. {
  180. }
  181. int3 AIPath::firstTileToGet() const
  182. {
  183. if(nodes.size())
  184. {
  185. return nodes.back().coord;
  186. }
  187. return int3(-1, -1, -1);
  188. }
  189. uint64_t AIPath::getPathDanger() const
  190. {
  191. if(nodes.size())
  192. {
  193. return nodes.front().danger;
  194. }
  195. return 0;
  196. }
  197. uint32_t AIPath::movementCost() const
  198. {
  199. if(nodes.size())
  200. {
  201. return nodes.front().movementPointsUsed;
  202. }
  203. // TODO: boost:optional?
  204. return 0;
  205. }
  206. uint64_t AIPath::getTotalDanger(HeroPtr hero) const
  207. {
  208. uint64_t pathDanger = getPathDanger();
  209. uint64_t objDanger = evaluateDanger(nodes.front().coord, hero.get()); // bank danger is not checked by pathfinder
  210. uint64_t danger = pathDanger > objDanger ? pathDanger : objDanger;
  211. return danger;
  212. }