AINodeStorage.h 3.6 KB

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