AINodeStorage.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "../Goals/AbstractGoal.h"
  15. class AIPathNode;
  16. class ISpecialAction
  17. {
  18. public:
  19. virtual Goals::TSubgoal whatToDo(HeroPtr hero) const = 0;
  20. virtual void applyOnDestination(
  21. HeroPtr hero,
  22. CDestinationNodeInfo & destination,
  23. const PathNodeInfo & source,
  24. AIPathNode * dstMode,
  25. const AIPathNode * srcNode) const
  26. {
  27. }
  28. };
  29. struct AIPathNode : public CGPathNode
  30. {
  31. uint32_t chainMask;
  32. uint64_t danger;
  33. uint32_t manaCost;
  34. std::shared_ptr<const ISpecialAction> specialAction;
  35. };
  36. struct AIPathNodeInfo
  37. {
  38. uint32_t movementPointsLeft;
  39. uint32_t movementPointsUsed;
  40. int turns;
  41. int3 coord;
  42. uint64_t danger;
  43. };
  44. struct AIPath
  45. {
  46. std::vector<AIPathNodeInfo> nodes;
  47. std::shared_ptr<const ISpecialAction> specialAction;
  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. uint32_t movementCost() const;
  55. };
  56. class AINodeStorage : public INodeStorage
  57. {
  58. private:
  59. int3 sizes;
  60. /// 1-3 - position on map, 4 - layer (air, water, land), 5 - chain (normal, battle, spellcast and combinations)
  61. boost::multi_array<AIPathNode, 5> nodes;
  62. const CGHeroInstance * hero;
  63. public:
  64. /// more than 1 chain layer allows us to have more than 1 path to each tile so we can chose more optimal one.
  65. static const int NUM_CHAINS = 3;
  66. // chain flags, can be combined
  67. static const int NORMAL_CHAIN = 1;
  68. static const int BATTLE_CHAIN = 2;
  69. static const int CAST_CHAIN = 4;
  70. static const int RESOURCE_CHAIN = 8;
  71. AINodeStorage(const int3 & sizes);
  72. ~AINodeStorage();
  73. virtual CGPathNode * getInitialNode() override;
  74. virtual void resetTile(const int3 & tile, EPathfindingLayer layer, CGPathNode::EAccessibility accessibility) override;
  75. virtual std::vector<CGPathNode *> calculateNeighbours(
  76. const PathNodeInfo & source,
  77. const PathfinderConfig * pathfinderConfig,
  78. const CPathfinderHelper * pathfinderHelper) override;
  79. virtual std::vector<CGPathNode *> calculateTeleportations(
  80. const PathNodeInfo & source,
  81. const PathfinderConfig * pathfinderConfig,
  82. const CPathfinderHelper * pathfinderHelper) override;
  83. virtual void commit(CDestinationNodeInfo & destination, const PathNodeInfo & source) override;
  84. const AIPathNode * getAINode(const CGPathNode * node) const;
  85. void updateAINode(CGPathNode * node, std::function<void (AIPathNode *)> updater);
  86. bool isBattleNode(const CGPathNode * node) const;
  87. bool hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) const;
  88. boost::optional<AIPathNode *> getOrCreateNode(const int3 & coord, const EPathfindingLayer layer, int chainNumber);
  89. std::vector<AIPath> getChainInfo(int3 pos, bool isOnLand) const;
  90. bool isTileAccessible(int3 pos, const EPathfindingLayer layer) const;
  91. void setHero(HeroPtr heroPtr)
  92. {
  93. hero = heroPtr.get();
  94. }
  95. const CGHeroInstance * getHero() const
  96. {
  97. return hero;
  98. }
  99. };