AINodeStorage.cpp 5.7 KB

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