AINodeStorage.h 3.0 KB

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