AINodeStorage.h 4.6 KB

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