AINodeStorage.h 5.3 KB

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