AINodeStorage.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. struct AIPathNode : public CGPathNode
  18. {
  19. uint32_t chainMask;
  20. uint64_t danger;
  21. uint32_t manaCost;
  22. std::shared_ptr<const ISpecialAction> specialAction;
  23. };
  24. struct AIPathNodeInfo
  25. {
  26. float cost;
  27. int turns;
  28. int3 coord;
  29. uint64_t danger;
  30. };
  31. struct AIPath
  32. {
  33. std::vector<AIPathNodeInfo> nodes;
  34. std::shared_ptr<const ISpecialAction> specialAction;
  35. uint64_t targetObjectDanger;
  36. AIPath();
  37. /// Gets danger of path excluding danger of visiting the target object like creature bank
  38. uint64_t getPathDanger() const;
  39. /// Gets danger of path including danger of visiting the target object like creature bank
  40. uint64_t getTotalDanger(HeroPtr hero) const;
  41. int3 firstTileToGet() const;
  42. float movementCost() const;
  43. };
  44. class AINodeStorage : public INodeStorage
  45. {
  46. private:
  47. int3 sizes;
  48. /// 1-3 - position on map, 4 - layer (air, water, land), 5 - chain (normal, battle, spellcast and combinations)
  49. boost::multi_array<AIPathNode, 5> nodes;
  50. const CPlayerSpecificInfoCallback * cb;
  51. const VCAI * ai;
  52. const CGHeroInstance * hero;
  53. std::unique_ptr<FuzzyHelper> dangerEvaluator;
  54. STRONG_INLINE
  55. void resetTile(const int3 & tile, EPathfindingLayer layer, CGPathNode::EAccessibility accessibility);
  56. public:
  57. /// more than 1 chain layer allows us to have more than 1 path to each tile so we can chose more optimal one.
  58. static const int NUM_CHAINS = 3;
  59. // chain flags, can be combined
  60. static const int NORMAL_CHAIN = 1;
  61. static const int BATTLE_CHAIN = 2;
  62. static const int CAST_CHAIN = 4;
  63. static const int RESOURCE_CHAIN = 8;
  64. AINodeStorage(const int3 & sizes);
  65. ~AINodeStorage();
  66. void initialize(const PathfinderOptions & options, const CGameState * gs, const CGHeroInstance * hero) override;
  67. virtual CGPathNode * getInitialNode() override;
  68. virtual std::vector<CGPathNode *> calculateNeighbours(
  69. const PathNodeInfo & source,
  70. const PathfinderConfig * pathfinderConfig,
  71. const CPathfinderHelper * pathfinderHelper) override;
  72. virtual std::vector<CGPathNode *> calculateTeleportations(
  73. const PathNodeInfo & source,
  74. const PathfinderConfig * pathfinderConfig,
  75. const CPathfinderHelper * pathfinderHelper) override;
  76. virtual void commit(CDestinationNodeInfo & destination, const PathNodeInfo & source) override;
  77. const AIPathNode * getAINode(const CGPathNode * node) const;
  78. void updateAINode(CGPathNode * node, std::function<void (AIPathNode *)> updater);
  79. bool isBattleNode(const CGPathNode * node) const;
  80. bool hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) const;
  81. boost::optional<AIPathNode *> getOrCreateNode(const int3 & coord, const EPathfindingLayer layer, int chainNumber);
  82. std::vector<AIPath> getChainInfo(const int3 & pos, bool isOnLand) const;
  83. bool isTileAccessible(const int3 & pos, const EPathfindingLayer layer) const;
  84. void setHero(HeroPtr heroPtr, const VCAI * ai);
  85. const CGHeroInstance * getHero() const
  86. {
  87. return hero;
  88. }
  89. uint64_t evaluateDanger(const int3 & tile) const
  90. {
  91. return dangerEvaluator->evaluateDanger(tile, hero, ai);
  92. }
  93. private:
  94. void calculateTownPortalTeleportations(const PathNodeInfo & source, std::vector<CGPathNode *> & neighbours);
  95. };