AINodeStorage.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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
  12. #define VCMI_TRACE_PATHFINDER_EX
  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. #include <inttypes.h>
  21. struct AIPathNode : public CGPathNode
  22. {
  23. uint64_t danger;
  24. uint64_t armyLoss;
  25. uint32_t manaCost;
  26. const AIPathNode * chainOther;
  27. std::shared_ptr<const ISpecialAction> specialAction;
  28. const ChainActor * actor;
  29. };
  30. struct AIPathNodeInfo
  31. {
  32. float cost;
  33. int turns;
  34. int3 coord;
  35. uint64_t danger;
  36. const CGHeroInstance * targetHero;
  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. const AIPathNodeInfo & firstNode() const;
  54. float movementCost() const;
  55. uint64_t getHeroStrength() const;
  56. };
  57. struct ExchangeCandidate : public AIPathNode
  58. {
  59. AIPathNode * carrierParent;
  60. AIPathNode * otherParent;
  61. };
  62. class AINodeStorage : public INodeStorage
  63. {
  64. private:
  65. int3 sizes;
  66. /// 1-3 - position on map, 4 - layer (air, water, land), 5 - chain (normal, battle, spellcast and combinations)
  67. boost::multi_array<AIPathNode, 5> nodes;
  68. const CPlayerSpecificInfoCallback * cb;
  69. const VCAI * ai;
  70. std::unique_ptr<FuzzyHelper> dangerEvaluator;
  71. std::vector<std::shared_ptr<ChainActor>> actors;
  72. std::vector<CGPathNode *> heroChain;
  73. bool heroChainPass; // true if we need to calculate hero chain
  74. int heroChainMaxTurns = 0;
  75. public:
  76. /// 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.
  77. static const int NUM_CHAINS = 5 * GameConstants::MAX_HEROES_PER_PLAYER;
  78. AINodeStorage(const int3 & sizes);
  79. ~AINodeStorage();
  80. void initialize(const PathfinderOptions & options, const CGameState * gs) override;
  81. virtual std::vector<CGPathNode *> getInitialNodes() override;
  82. virtual std::vector<CGPathNode *> calculateNeighbours(
  83. const PathNodeInfo & source,
  84. const PathfinderConfig * pathfinderConfig,
  85. const CPathfinderHelper * pathfinderHelper) override;
  86. virtual std::vector<CGPathNode *> calculateTeleportations(
  87. const PathNodeInfo & source,
  88. const PathfinderConfig * pathfinderConfig,
  89. const CPathfinderHelper * pathfinderHelper) override;
  90. virtual void commit(CDestinationNodeInfo & destination, const PathNodeInfo & source) override;
  91. const AIPathNode * getAINode(const CGPathNode * node) const;
  92. void updateAINode(CGPathNode * node, std::function<void (AIPathNode *)> updater);
  93. bool hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) const;
  94. template<class NodeRange>
  95. bool hasBetterChain(
  96. const CGPathNode * source,
  97. const AIPathNode * destinationNode,
  98. const NodeRange & chains) const;
  99. boost::optional<AIPathNode *> getOrCreateNode(const int3 & coord, const EPathfindingLayer layer, const ChainActor * actor);
  100. std::vector<AIPath> getChainInfo(const int3 & pos, bool isOnLand) const;
  101. bool isTileAccessible(const HeroPtr & hero, const int3 & pos, const EPathfindingLayer layer) const;
  102. void setHeroes(std::vector<HeroPtr> heroes, const VCAI * ai);
  103. void setTownsAndDwellings(
  104. const std::vector<const CGTownInstance *> & towns,
  105. const std::set<const CGObjectInstance *> & visitableObjs);
  106. const CGHeroInstance * getHero(const CGPathNode * node) const;
  107. const std::set<const CGHeroInstance *> getAllHeroes() const;
  108. void clear();
  109. bool calculateHeroChain();
  110. uint64_t evaluateDanger(const int3 & tile, const CGHeroInstance * hero) const
  111. {
  112. return dangerEvaluator->evaluateDanger(tile, hero, ai);
  113. }
  114. private:
  115. STRONG_INLINE
  116. void resetTile(const int3 & tile, EPathfindingLayer layer, CGPathNode::EAccessibility accessibility);
  117. void calculateHeroChain(
  118. AIPathNode * srcNode,
  119. const std::vector<AIPathNode *> & variants,
  120. std::vector<ExchangeCandidate> & result) const;
  121. void calculateHeroChain(
  122. AIPathNode * carrier,
  123. AIPathNode * other,
  124. std::vector<ExchangeCandidate> & result) const;
  125. void cleanupInefectiveChains(std::vector<ExchangeCandidate> & result) const;
  126. void addHeroChain(const std::vector<ExchangeCandidate> & result);
  127. void calculateTownPortalTeleportations(const PathNodeInfo & source, std::vector<CGPathNode *> & neighbours);
  128. void fillChainInfo(const AIPathNode * node, AIPath & path) const;
  129. void commit(
  130. AIPathNode * destination,
  131. const AIPathNode * source,
  132. CGPathNode::ENodeAction action,
  133. int turn,
  134. int movementLeft,
  135. float cost) const;
  136. ExchangeCandidate calculateExchange(
  137. ChainActor * exchangeActor,
  138. AIPathNode * carrierParentNode,
  139. AIPathNode * otherParentNode) const;
  140. };