AINodeStorage.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #include "../../../lib/CPathfinder.h"
  12. #include "../../../lib/mapObjects/CGHeroInstance.h"
  13. #include "../AIUtility.h"
  14. #include "../FuzzyHelper.h"
  15. #include "../Goals/AbstractGoal.h"
  16. #include "Actions/ISpecialAction.h"
  17. class ChainActor
  18. {
  19. private:
  20. std::vector<ChainActor> specialActors;
  21. void copyFrom(ChainActor * base);
  22. void setupSpecialActors();
  23. public:
  24. // chain flags, can be combined meaning hero exchange and so on
  25. uint64_t chainMask;
  26. bool isMovable;
  27. bool allowUseResources;
  28. bool allowBattle;
  29. bool allowSpellCast;
  30. const CGHeroInstance * hero;
  31. const ChainActor * battleActor;
  32. const ChainActor * castActor;
  33. const ChainActor * resourceActor;
  34. int3 initialPosition;
  35. EPathfindingLayer layer;
  36. uint32_t initialMovement;
  37. uint32_t initialTurn;
  38. uint64_t armyValue;
  39. ChainActor()
  40. {
  41. }
  42. ChainActor(const CGHeroInstance * hero, int chainMask);
  43. };
  44. struct AIPathNode : public CGPathNode
  45. {
  46. uint64_t danger;
  47. uint64_t armyLoss;
  48. uint32_t manaCost;
  49. std::shared_ptr<const ISpecialAction> specialAction;
  50. const ChainActor * actor;
  51. };
  52. struct AIPathNodeInfo
  53. {
  54. float cost;
  55. int turns;
  56. int3 coord;
  57. uint64_t danger;
  58. };
  59. struct AIPath
  60. {
  61. std::vector<AIPathNodeInfo> nodes;
  62. std::shared_ptr<const ISpecialAction> specialAction;
  63. uint64_t targetObjectDanger;
  64. const CGHeroInstance * targetHero;
  65. AIPath();
  66. /// Gets danger of path excluding danger of visiting the target object like creature bank
  67. uint64_t getPathDanger() const;
  68. /// Gets danger of path including danger of visiting the target object like creature bank
  69. uint64_t getTotalDanger(HeroPtr hero) const;
  70. int3 firstTileToGet() const;
  71. float movementCost() const;
  72. };
  73. class AINodeStorage : public INodeStorage
  74. {
  75. private:
  76. int3 sizes;
  77. /// 1-3 - position on map, 4 - layer (air, water, land), 5 - chain (normal, battle, spellcast and combinations)
  78. boost::multi_array<AIPathNode, 5> nodes;
  79. const CPlayerSpecificInfoCallback * cb;
  80. const VCAI * ai;
  81. std::unique_ptr<FuzzyHelper> dangerEvaluator;
  82. std::vector<std::shared_ptr<ChainActor>> actors;
  83. STRONG_INLINE
  84. void resetTile(const int3 & tile, EPathfindingLayer layer, CGPathNode::EAccessibility accessibility);
  85. public:
  86. /// 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.
  87. static const int NUM_CHAINS = 3 * GameConstants::MAX_HEROES_PER_PLAYER;
  88. AINodeStorage(const int3 & sizes);
  89. ~AINodeStorage();
  90. void initialize(const PathfinderOptions & options, const CGameState * gs) override;
  91. virtual std::vector<CGPathNode *> getInitialNodes() override;
  92. virtual std::vector<CGPathNode *> calculateNeighbours(
  93. const PathNodeInfo & source,
  94. const PathfinderConfig * pathfinderConfig,
  95. const CPathfinderHelper * pathfinderHelper) override;
  96. virtual std::vector<CGPathNode *> calculateTeleportations(
  97. const PathNodeInfo & source,
  98. const PathfinderConfig * pathfinderConfig,
  99. const CPathfinderHelper * pathfinderHelper) override;
  100. virtual void commit(CDestinationNodeInfo & destination, const PathNodeInfo & source) override;
  101. const AIPathNode * getAINode(const CGPathNode * node) const;
  102. void updateAINode(CGPathNode * node, std::function<void (AIPathNode *)> updater);
  103. bool hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) 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. const CGHeroInstance * getHero(const CGPathNode * node) const;
  109. const std::set<const CGHeroInstance *> getAllHeroes() const;
  110. void clear();
  111. uint64_t evaluateDanger(const int3 & tile, const CGHeroInstance * hero) const
  112. {
  113. return dangerEvaluator->evaluateDanger(tile, hero, ai);
  114. }
  115. private:
  116. void calculateTownPortalTeleportations(const PathNodeInfo & source, std::vector<CGPathNode *> & neighbours);
  117. };