AINodeStorage.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 0
  12. #include "../../../lib/CPathfinder.h"
  13. #include "../../../lib/mapObjects/CGHeroInstance.h"
  14. #include "../AIUtility.h"
  15. #include "../FuzzyHelper.h"
  16. #include "../Goals/AbstractGoal.h"
  17. #include "Actions/ISpecialAction.h"
  18. #include "Actors.h"
  19. #include <inttypes.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. std::shared_ptr<const ISpecialAction> specialAction;
  38. };
  39. struct AIPath
  40. {
  41. std::vector<AIPathNodeInfo> nodes;
  42. std::shared_ptr<const ISpecialAction> specialAction;
  43. uint64_t targetObjectDanger;
  44. uint64_t armyLoss;
  45. const CGHeroInstance * targetHero;
  46. const CCreatureSet * heroArmy;
  47. uint64_t chainMask;
  48. AIPath();
  49. /// Gets danger of path excluding danger of visiting the target object like creature bank
  50. uint64_t getPathDanger() const;
  51. /// Gets danger of path including danger of visiting the target object like creature bank
  52. uint64_t getTotalDanger(HeroPtr hero) const;
  53. int3 firstTileToGet() const;
  54. int3 targetTile() const;
  55. const AIPathNodeInfo & firstNode() const;
  56. float movementCost() const;
  57. uint8_t turn() const;
  58. uint64_t getHeroStrength() const;
  59. std::string toString();
  60. };
  61. struct ExchangeCandidate : public AIPathNode
  62. {
  63. AIPathNode * carrierParent;
  64. AIPathNode * otherParent;
  65. };
  66. class AINodeStorage : public INodeStorage
  67. {
  68. private:
  69. int3 sizes;
  70. /// 1-3 - position on map, 4 - layer (air, water, land), 5 - chain (normal, battle, spellcast and combinations)
  71. boost::multi_array<AIPathNode, 5> nodes;
  72. const CPlayerSpecificInfoCallback * cb;
  73. const VCAI * ai;
  74. std::unique_ptr<FuzzyHelper> dangerEvaluator;
  75. std::vector<std::shared_ptr<ChainActor>> actors;
  76. std::vector<CGPathNode *> heroChain;
  77. bool heroChainPass; // true if we need to calculate hero chain
  78. int heroChainTurn;
  79. PlayerColor playerID;
  80. public:
  81. /// 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.
  82. static const int NUM_CHAINS = 5 * GameConstants::MAX_HEROES_PER_PLAYER;
  83. AINodeStorage(const int3 & sizes);
  84. ~AINodeStorage();
  85. void initialize(const PathfinderOptions & options, const CGameState * gs) override;
  86. virtual std::vector<CGPathNode *> getInitialNodes() override;
  87. virtual std::vector<CGPathNode *> calculateNeighbours(
  88. const PathNodeInfo & source,
  89. const PathfinderConfig * pathfinderConfig,
  90. const CPathfinderHelper * pathfinderHelper) override;
  91. virtual std::vector<CGPathNode *> calculateTeleportations(
  92. const PathNodeInfo & source,
  93. const PathfinderConfig * pathfinderConfig,
  94. const CPathfinderHelper * pathfinderHelper) override;
  95. virtual void commit(CDestinationNodeInfo & destination, const PathNodeInfo & source) override;
  96. const AIPathNode * getAINode(const CGPathNode * node) const;
  97. void updateAINode(CGPathNode * node, std::function<void (AIPathNode *)> updater);
  98. bool hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) const;
  99. template<class NodeRange>
  100. bool hasBetterChain(
  101. const CGPathNode * source,
  102. const AIPathNode * destinationNode,
  103. const NodeRange & chains) const;
  104. boost::optional<AIPathNode *> getOrCreateNode(const int3 & coord, const EPathfindingLayer layer, const ChainActor * actor);
  105. std::vector<AIPath> getChainInfo(const int3 & pos, bool isOnLand) const;
  106. bool isTileAccessible(const HeroPtr & hero, const int3 & pos, const EPathfindingLayer layer) const;
  107. void setHeroes(std::vector<HeroPtr> heroes, const VCAI * ai);
  108. void setTownsAndDwellings(
  109. const std::vector<const CGTownInstance *> & towns,
  110. const std::set<const CGObjectInstance *> & visitableObjs);
  111. const CGHeroInstance * getHero(const CGPathNode * node) const;
  112. const std::set<const CGHeroInstance *> getAllHeroes() const;
  113. void clear();
  114. bool calculateHeroChain();
  115. uint64_t evaluateDanger(const int3 & tile, const CGHeroInstance * hero) const
  116. {
  117. return dangerEvaluator->evaluateDanger(tile, hero, ai);
  118. }
  119. private:
  120. STRONG_INLINE
  121. void resetTile(const int3 & tile, EPathfindingLayer layer, CGPathNode::EAccessibility accessibility);
  122. void calculateHeroChain(
  123. AIPathNode * srcNode,
  124. const std::vector<AIPathNode *> & variants,
  125. std::vector<ExchangeCandidate> & result) const;
  126. void calculateHeroChain(
  127. AIPathNode * carrier,
  128. AIPathNode * other,
  129. std::vector<ExchangeCandidate> & result) const;
  130. void cleanupInefectiveChains(std::vector<ExchangeCandidate> & result) const;
  131. void addHeroChain(const std::vector<ExchangeCandidate> & result);
  132. void calculateTownPortalTeleportations(
  133. const PathNodeInfo & source,
  134. std::vector<CGPathNode *> & neighbours,
  135. const CPathfinderHelper * pathfinderHelper);
  136. void fillChainInfo(const AIPathNode * node, AIPath & path, int parentIndex) const;
  137. void commit(
  138. AIPathNode * destination,
  139. const AIPathNode * source,
  140. CGPathNode::ENodeAction action,
  141. int turn,
  142. int movementLeft,
  143. float cost) const;
  144. ExchangeCandidate calculateExchange(
  145. ChainActor * exchangeActor,
  146. AIPathNode * carrierParentNode,
  147. AIPathNode * otherParentNode) const;
  148. };