AINodeStorage.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #include "../../../lib/CPathfinder.h"
  14. #include "../../../lib/mapObjects/CGHeroInstance.h"
  15. #include "../AIUtility.h"
  16. #include "../Engine/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 Nullkiller * 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 Nullkiller * ai, 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. template<class NodeRange>
  129. bool hasBetterChain(
  130. const CGPathNode * source,
  131. const AIPathNode * destinationNode,
  132. const NodeRange & chains) const;
  133. boost::optional<AIPathNode *> getOrCreateNode(const int3 & coord, const EPathfindingLayer layer, const ChainActor * actor);
  134. std::vector<AIPath> getChainInfo(const int3 & pos, bool isOnLand) const;
  135. bool isTileAccessible(const HeroPtr & hero, const int3 & pos, const EPathfindingLayer layer) const;
  136. void setHeroes(std::map<const CGHeroInstance *, HeroRole> heroes);
  137. void setTownsAndDwellings(
  138. const std::vector<const CGTownInstance *> & towns,
  139. const std::set<const CGObjectInstance *> & visitableObjs);
  140. const CGHeroInstance * getHero(const CGPathNode * node) const;
  141. const std::set<const CGHeroInstance *> getAllHeroes() const;
  142. void clear();
  143. bool calculateHeroChain();
  144. bool calculateHeroChainFinal();
  145. uint64_t evaluateDanger(const int3 & tile, const CGHeroInstance * hero, bool checkGuards) const
  146. {
  147. return dangerEvaluator->evaluateDanger(tile, hero, checkGuards);
  148. }
  149. uint64_t evaluateArmyLoss(const CGHeroInstance * hero, uint64_t armyValue, uint64_t danger) const
  150. {
  151. double ratio = (double)danger / (armyValue * hero->getFightingStrength());
  152. return (uint64_t)(armyValue * ratio * ratio * ratio);
  153. }
  154. private:
  155. STRONG_INLINE
  156. void resetTile(const int3 & tile, EPathfindingLayer layer, CGPathNode::EAccessibility accessibility);
  157. void calculateHeroChain(
  158. AIPathNode * srcNode,
  159. const std::vector<AIPathNode *> & variants,
  160. std::vector<ExchangeCandidate> & result) const;
  161. void calculateHeroChain(
  162. AIPathNode * carrier,
  163. AIPathNode * other,
  164. std::vector<ExchangeCandidate> & result) const;
  165. void cleanupInefectiveChains(std::vector<ExchangeCandidate> & result) const;
  166. void addHeroChain(const std::vector<ExchangeCandidate> & result);
  167. void calculateTownPortalTeleportations(std::vector<CGPathNode *> & neighbours);
  168. void fillChainInfo(const AIPathNode * node, AIPath & path, int parentIndex) const;
  169. ExchangeCandidate calculateExchange(
  170. ChainActor * exchangeActor,
  171. AIPathNode * carrierParentNode,
  172. AIPathNode * otherParentNode) const;
  173. };