AINodeStorage.h 7.2 KB

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