AINodeStorage.h 6.4 KB

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