AINodeStorage.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 VCMI_TRACE_PATHFINDER 1
  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/ISpecialAction.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 ISpecialAction> 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 ISpecialAction> specialAction;
  39. };
  40. struct AIPath
  41. {
  42. std::vector<AIPathNodeInfo> nodes;
  43. std::shared_ptr<const ISpecialAction> specialAction;
  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();
  66. };
  67. struct ExchangeCandidate : public AIPathNode
  68. {
  69. AIPathNode * carrierParent;
  70. AIPathNode * otherParent;
  71. };
  72. class AINodeStorage : public INodeStorage
  73. {
  74. private:
  75. int3 sizes;
  76. /// 1-3 - position on map, 4 - layer (air, water, land), 5 - chain (normal, battle, spellcast and combinations)
  77. boost::multi_array<AIPathNode, 5> nodes;
  78. const CPlayerSpecificInfoCallback * cb;
  79. const VCAI * ai;
  80. std::unique_ptr<FuzzyHelper> dangerEvaluator;
  81. std::vector<std::shared_ptr<ChainActor>> actors;
  82. std::vector<CGPathNode *> heroChain;
  83. bool heroChainPass; // true if we need to calculate hero chain
  84. int heroChainTurn;
  85. PlayerColor playerID;
  86. public:
  87. /// 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.
  88. static const int NUM_CHAINS = 10 * GameConstants::MAX_HEROES_PER_PLAYER;
  89. AINodeStorage(const int3 & sizes);
  90. ~AINodeStorage();
  91. void initialize(const PathfinderOptions & options, const CGameState * gs) override;
  92. virtual std::vector<CGPathNode *> getInitialNodes() override;
  93. virtual std::vector<CGPathNode *> calculateNeighbours(
  94. const PathNodeInfo & source,
  95. const PathfinderConfig * pathfinderConfig,
  96. const CPathfinderHelper * pathfinderHelper) override;
  97. virtual std::vector<CGPathNode *> calculateTeleportations(
  98. const PathNodeInfo & source,
  99. const PathfinderConfig * pathfinderConfig,
  100. const CPathfinderHelper * pathfinderHelper) override;
  101. virtual void commit(CDestinationNodeInfo & destination, const PathNodeInfo & source) override;
  102. void commit(
  103. AIPathNode * destination,
  104. const AIPathNode * source,
  105. CGPathNode::ENodeAction action,
  106. int turn,
  107. int movementLeft,
  108. float cost) const;
  109. const AIPathNode * getAINode(const CGPathNode * node) const;
  110. void updateAINode(CGPathNode * node, std::function<void (AIPathNode *)> updater);
  111. bool hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) const;
  112. bool isMovementIneficient(const PathNodeInfo & source, CDestinationNodeInfo & destination) const
  113. {
  114. // further chain distribution is calculated as the last stage
  115. if(heroChainPass && destination.node->turns > heroChainTurn)
  116. return true;
  117. return hasBetterChain(source, destination);
  118. }
  119. template<class NodeRange>
  120. bool hasBetterChain(
  121. const CGPathNode * source,
  122. const AIPathNode * destinationNode,
  123. const NodeRange & chains) const;
  124. boost::optional<AIPathNode *> getOrCreateNode(const int3 & coord, const EPathfindingLayer layer, const ChainActor * actor);
  125. std::vector<AIPath> getChainInfo(const int3 & pos, bool isOnLand) const;
  126. bool isTileAccessible(const HeroPtr & hero, const int3 & pos, const EPathfindingLayer layer) const;
  127. void setHeroes(std::vector<HeroPtr> heroes, const VCAI * ai);
  128. void setTownsAndDwellings(
  129. const std::vector<const CGTownInstance *> & towns,
  130. const std::set<const CGObjectInstance *> & visitableObjs);
  131. const CGHeroInstance * getHero(const CGPathNode * node) const;
  132. const std::set<const CGHeroInstance *> getAllHeroes() const;
  133. void clear();
  134. bool calculateHeroChain();
  135. uint64_t evaluateDanger(const int3 & tile, const CGHeroInstance * hero) const
  136. {
  137. return dangerEvaluator->evaluateDanger(tile, hero, ai);
  138. }
  139. uint64_t evaluateArmyLoss(const CGHeroInstance * hero, uint64_t armyValue, uint64_t danger) const
  140. {
  141. double ratio = (double)danger / (armyValue * hero->getFightingStrength());
  142. return (uint64_t)(armyValue * ratio * ratio * ratio);
  143. }
  144. private:
  145. STRONG_INLINE
  146. void resetTile(const int3 & tile, EPathfindingLayer layer, CGPathNode::EAccessibility accessibility);
  147. void calculateHeroChain(
  148. AIPathNode * srcNode,
  149. const std::vector<AIPathNode *> & variants,
  150. std::vector<ExchangeCandidate> & result) const;
  151. void calculateHeroChain(
  152. AIPathNode * carrier,
  153. AIPathNode * other,
  154. std::vector<ExchangeCandidate> & result) const;
  155. void cleanupInefectiveChains(std::vector<ExchangeCandidate> & result) const;
  156. void addHeroChain(const std::vector<ExchangeCandidate> & result);
  157. void calculateTownPortalTeleportations(std::vector<CGPathNode *> & neighbours);
  158. void fillChainInfo(const AIPathNode * node, AIPath & path, int parentIndex) const;
  159. ExchangeCandidate calculateExchange(
  160. ChainActor * exchangeActor,
  161. AIPathNode * carrierParentNode,
  162. AIPathNode * otherParentNode) const;
  163. };