AINodeStorage.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * AINodeStorage.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. #pragma once
  11. #define PATHFINDER_TRACE_LEVEL 0
  12. #define AI_TRACE_LEVEL 0
  13. #define SCOUT_TURN_DISTANCE_LIMIT 3
  14. #include "../../../lib/CPathfinder.h"
  15. #include "../../../lib/mapObjects/CGHeroInstance.h"
  16. #include "../AIUtility.h"
  17. #include "../Engine/FuzzyHelper.h"
  18. #include "../Goals/AbstractGoal.h"
  19. #include "Actions/SpecialAction.h"
  20. #include "Actors.h"
  21. struct AIPathNode : public CGPathNode
  22. {
  23. uint64_t danger;
  24. uint64_t armyLoss;
  25. uint32_t manaCost;
  26. const AIPathNode * chainOther;
  27. std::shared_ptr<const SpecialAction> specialAction;
  28. const ChainActor * actor;
  29. STRONG_INLINE
  30. bool blocked() const
  31. {
  32. return accessible == CGPathNode::EAccessibility::NOT_SET
  33. || accessible == CGPathNode::EAccessibility::BLOCKED;
  34. }
  35. };
  36. struct AIPathNodeInfo
  37. {
  38. float cost;
  39. uint8_t turns;
  40. int3 coord;
  41. uint64_t danger;
  42. const CGHeroInstance * targetHero;
  43. int parentIndex;
  44. uint64_t chainMask;
  45. std::shared_ptr<const SpecialAction> specialAction;
  46. bool actionIsBlocked;
  47. };
  48. struct AIPath
  49. {
  50. std::vector<AIPathNodeInfo> nodes;
  51. uint64_t targetObjectDanger;
  52. uint64_t armyLoss;
  53. uint64_t targetObjectArmyLoss;
  54. const CGHeroInstance * targetHero;
  55. const CCreatureSet * heroArmy;
  56. uint64_t chainMask;
  57. uint8_t exchangeCount;
  58. AIPath();
  59. /// Gets danger of path excluding danger of visiting the target object like creature bank
  60. uint64_t getPathDanger() const;
  61. /// Gets danger of path including danger of visiting the target object like creature bank
  62. uint64_t getTotalDanger() const;
  63. /// Gets danger of path including danger of visiting the target object like creature bank
  64. uint64_t getTotalArmyLoss() const;
  65. int3 firstTileToGet() const;
  66. int3 targetTile() const;
  67. const AIPathNodeInfo & firstNode() const;
  68. const AIPathNodeInfo & targetNode() const;
  69. float movementCost() const;
  70. uint8_t turn() const;
  71. uint64_t getHeroStrength() const;
  72. std::string toString() const;
  73. std::shared_ptr<const SpecialAction> getFirstBlockedAction() const;
  74. bool containsHero(const CGHeroInstance * hero) const;
  75. };
  76. struct ExchangeCandidate : public AIPathNode
  77. {
  78. AIPathNode * carrierParent;
  79. AIPathNode * otherParent;
  80. };
  81. enum EHeroChainPass
  82. {
  83. INITIAL, // single heroes unlimited distance
  84. CHAIN, // chains with limited distance
  85. FINAL // same as SINGLE but for heroes from CHAIN pass
  86. };
  87. class AISharedStorage
  88. {
  89. /// 1-3 - position on map, 4 - layer (air, water, land), 5 - chain (normal, battle, spellcast and combinations)
  90. static std::shared_ptr<boost::multi_array<AIPathNode, 5>> shared;
  91. std::shared_ptr<boost::multi_array<AIPathNode, 5>> nodes;
  92. public:
  93. AISharedStorage(int3 mapSize);
  94. ~AISharedStorage();
  95. /*STRONG_INLINE
  96. boost::detail::multi_array::sub_array<AIPathNode, 1> get(int3 tile, EPathfindingLayer layer)
  97. {
  98. return (*nodes)[tile.x][tile.y][tile.z][layer];
  99. }*/
  100. STRONG_INLINE
  101. boost::detail::multi_array::sub_array<AIPathNode, 1> get(int3 tile, EPathfindingLayer layer) const
  102. {
  103. return (*nodes)[tile.x][tile.y][tile.z][layer];
  104. }
  105. };
  106. class AINodeStorage : public INodeStorage
  107. {
  108. private:
  109. int3 sizes;
  110. const CPlayerSpecificInfoCallback * cb;
  111. const Nullkiller * ai;
  112. std::unique_ptr<FuzzyHelper> dangerEvaluator;
  113. AISharedStorage nodes;
  114. std::vector<std::shared_ptr<ChainActor>> actors;
  115. std::vector<CGPathNode *> heroChain;
  116. EHeroChainPass heroChainPass; // true if we need to calculate hero chain
  117. uint64_t chainMask;
  118. int heroChainTurn;
  119. int heroChainMaxTurns;
  120. PlayerColor playerID;
  121. uint8_t scoutTurnDistanceLimit;
  122. public:
  123. /// more than 1 chain layer for each hero allows us to have more than 1 path to each tile so we can chose more optimal one.
  124. AINodeStorage(const Nullkiller * ai, const int3 & sizes);
  125. ~AINodeStorage();
  126. void initialize(const PathfinderOptions & options, const CGameState * gs) override;
  127. bool increaseHeroChainTurnLimit();
  128. bool selectFirstActor();
  129. bool selectNextActor();
  130. virtual std::vector<CGPathNode *> getInitialNodes() override;
  131. virtual std::vector<CGPathNode *> calculateNeighbours(
  132. const PathNodeInfo & source,
  133. const PathfinderConfig * pathfinderConfig,
  134. const CPathfinderHelper * pathfinderHelper) override;
  135. virtual std::vector<CGPathNode *> calculateTeleportations(
  136. const PathNodeInfo & source,
  137. const PathfinderConfig * pathfinderConfig,
  138. const CPathfinderHelper * pathfinderHelper) override;
  139. virtual void commit(CDestinationNodeInfo & destination, const PathNodeInfo & source) override;
  140. void commit(
  141. AIPathNode * destination,
  142. const AIPathNode * source,
  143. CGPathNode::ENodeAction action,
  144. int turn,
  145. int movementLeft,
  146. float cost) const;
  147. const AIPathNode * getAINode(const CGPathNode * node) const;
  148. void updateAINode(CGPathNode * node, std::function<void (AIPathNode *)> updater);
  149. bool hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) const;
  150. bool isMovementIneficient(const PathNodeInfo & source, CDestinationNodeInfo & destination) const
  151. {
  152. return hasBetterChain(source, destination);
  153. }
  154. bool isDistanceLimitReached(const PathNodeInfo & source, CDestinationNodeInfo & destination) const;
  155. template<class NodeRange>
  156. bool hasBetterChain(
  157. const CGPathNode * source,
  158. const AIPathNode * destinationNode,
  159. const NodeRange & chains) const;
  160. boost::optional<AIPathNode *> getOrCreateNode(const int3 & coord, const EPathfindingLayer layer, const ChainActor * actor);
  161. std::vector<AIPath> getChainInfo(const int3 & pos, bool isOnLand) const;
  162. bool isTileAccessible(const HeroPtr & hero, const int3 & pos, const EPathfindingLayer layer) const;
  163. void setHeroes(std::map<const CGHeroInstance *, HeroRole> heroes);
  164. void setScoutTurnDistanceLimit(uint8_t distanceLimit) { scoutTurnDistanceLimit = distanceLimit; }
  165. void setTownsAndDwellings(
  166. const std::vector<const CGTownInstance *> & towns,
  167. const std::set<const CGObjectInstance *> & visitableObjs);
  168. const CGHeroInstance * getHero(const CGPathNode * node) const;
  169. const std::set<const CGHeroInstance *> getAllHeroes() const;
  170. void clear();
  171. bool calculateHeroChain();
  172. bool calculateHeroChainFinal();
  173. uint64_t evaluateDanger(const int3 & tile, const CGHeroInstance * hero, bool checkGuards) const
  174. {
  175. return dangerEvaluator->evaluateDanger(tile, hero, checkGuards);
  176. }
  177. uint64_t evaluateArmyLoss(const CGHeroInstance * hero, uint64_t armyValue, uint64_t danger) const
  178. {
  179. double ratio = (double)danger / (armyValue * hero->getFightingStrength());
  180. return (uint64_t)(armyValue * ratio * ratio * ratio);
  181. }
  182. private:
  183. STRONG_INLINE
  184. void resetTile(const int3 & tile, EPathfindingLayer layer, CGPathNode::EAccessibility accessibility);
  185. void calculateHeroChain(
  186. AIPathNode * srcNode,
  187. const std::vector<AIPathNode *> & variants,
  188. std::vector<ExchangeCandidate> & result) const;
  189. void calculateHeroChain(
  190. AIPathNode * carrier,
  191. AIPathNode * other,
  192. std::vector<ExchangeCandidate> & result) const;
  193. void cleanupInefectiveChains(std::vector<ExchangeCandidate> & result) const;
  194. void addHeroChain(const std::vector<ExchangeCandidate> & result);
  195. void calculateTownPortalTeleportations(std::vector<CGPathNode *> & neighbours);
  196. void fillChainInfo(const AIPathNode * node, AIPath & path, int parentIndex) const;
  197. ExchangeCandidate calculateExchange(
  198. ChainActor * exchangeActor,
  199. AIPathNode * carrierParentNode,
  200. AIPathNode * otherParentNode) const;
  201. };