AINodeStorage.h 7.2 KB

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