AINodeStorage.h 7.4 KB

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