CPathfinder.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #pragma once
  2. #include "VCMI_Lib.h"
  3. #include "mapping/CMap.h"
  4. #include "IGameCallback.h"
  5. #include "int3.h"
  6. #include <boost/heap/priority_queue.hpp>
  7. /*
  8. * CPathfinder.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. class CGHeroInstance;
  17. class CGObjectInstance;
  18. struct TerrainTile;
  19. struct DLL_LINKAGE CGPathNode
  20. {
  21. typedef EPathfindingLayer ELayer;
  22. enum ENodeAction
  23. {
  24. UNKNOWN = -1,
  25. NORMAL = 0,
  26. EMBARK = 1,
  27. DISEMBARK, //2
  28. BATTLE,//3
  29. VISIT,//4
  30. BLOCKING_VISIT//5
  31. };
  32. enum EAccessibility
  33. {
  34. NOT_SET = 0,
  35. ACCESSIBLE = 1, //tile can be entered and passed
  36. VISITABLE, //tile can be entered as the last tile in path
  37. BLOCKVIS, //visitable from neighbouring tile but not passable
  38. BLOCKED //tile can't be entered nor visited
  39. };
  40. bool locked;
  41. EAccessibility accessible;
  42. ui8 turns; //how many turns we have to wait before reachng the tile - 0 means current turn
  43. ui32 moveRemains; //remaining tiles after hero reaches the tile
  44. CGPathNode * theNodeBefore;
  45. int3 coord; //coordinates
  46. ELayer layer;
  47. ENodeAction action;
  48. CGPathNode(int3 Coord, ELayer Layer);
  49. void reset();
  50. bool reachable() const;
  51. };
  52. struct DLL_LINKAGE CGPath
  53. {
  54. std::vector<CGPathNode> nodes; //just get node by node
  55. int3 startPos() const; // start point
  56. int3 endPos() const; //destination point
  57. void convert(ui8 mode); //mode=0 -> from 'manifest' to 'object'
  58. };
  59. struct DLL_LINKAGE CPathsInfo
  60. {
  61. typedef EPathfindingLayer ELayer;
  62. mutable boost::mutex pathMx;
  63. const CGHeroInstance *hero;
  64. int3 hpos;
  65. int3 sizes;
  66. boost::multi_array<CGPathNode *, 4> nodes; //[w][h][level][layer]
  67. CPathsInfo(const int3 &Sizes);
  68. ~CPathsInfo();
  69. const CGPathNode * getPathInfo(const int3 &tile, const ELayer &layer = ELayer::AUTO) const;
  70. bool getPath(CGPath &out, const int3 &dst, const ELayer &layer = ELayer::AUTO) const;
  71. int getDistance(const int3 &tile, const ELayer &layer = ELayer::AUTO) const;
  72. CGPathNode *getNode(const int3 &coord, const ELayer &layer) const;
  73. };
  74. class CPathfinder : private CGameInfoCallback
  75. {
  76. public:
  77. CPathfinder(CPathsInfo &_out, CGameState *_gs, const CGHeroInstance *_hero);
  78. void calculatePaths(); //calculates possible paths for hero, uses current hero position and movement left; returns pointer to newly allocated CPath or nullptr if path does not exists
  79. private:
  80. typedef EPathfindingLayer ELayer;
  81. struct PathfinderOptions
  82. {
  83. bool useFlying;
  84. bool useWaterWalking;
  85. bool useEmbarkAndDisembark;
  86. bool useTeleportTwoWay; // Two-way monoliths and Subterranean Gate
  87. bool useTeleportOneWay; // One-way monoliths with one known exit only
  88. bool useTeleportOneWayRandom; // One-way monoliths with more than one known exit
  89. bool useTeleportWhirlpool; // Force enabled if hero protected or unaffected (have one stack of one creature)
  90. /// If true transition into air layer only possible from initial node.
  91. /// This is drastically decrease path calculation complexity (and time).
  92. /// Downside is less MP effective paths calculation.
  93. bool lightweightFlyingMode;
  94. /// This option enable one turn limitation for flying and water walking.
  95. /// So if we're out of MP while cp is blocked or water tile we won't add dest tile to queue.
  96. ///
  97. /// Following imitation is default H3 mechanics, but someone may want to disable it in mods.
  98. /// After all this limit should benefit performance on maps with tons of water or blocked tiles.
  99. bool oneTurnSpecialLayersLimit;
  100. PathfinderOptions();
  101. } options;
  102. CPathsInfo &out;
  103. const CGHeroInstance *hero;
  104. struct NodeComparer
  105. {
  106. bool operator()(const CGPathNode * lhs, const CGPathNode * rhs) const
  107. {
  108. if(rhs->turns > lhs->turns)
  109. return false;
  110. else if(rhs->turns == lhs->turns && rhs->moveRemains < lhs->moveRemains)
  111. return false;
  112. return true;
  113. }
  114. };
  115. boost::heap::priority_queue<CGPathNode *, boost::heap::compare<NodeComparer> > pq;
  116. std::vector<int3> neighbours;
  117. CGPathNode *cp; //current (source) path node -> we took it from the queue
  118. CGPathNode *dp; //destination node -> it's a neighbour of cp that we consider
  119. const TerrainTile *ct, *dt; //tile info for both nodes
  120. const CGObjectInstance *sTileObj;
  121. CGPathNode::ENodeAction destAction;
  122. void addNeighbours(const int3 &coord);
  123. void addTeleportExits(bool noTeleportExcludes = false);
  124. bool isLayerTransitionPossible() const;
  125. bool isMovementToDestPossible();
  126. bool isMovementAfterDestPossible() const;
  127. bool isSourceInitialPosition() const;
  128. int3 getSourceGuardPosition() const;
  129. bool isSourceGuarded() const;
  130. bool isDestinationGuarded(const bool ignoreAccessibility = true) const;
  131. bool isDestinationGuardian() const;
  132. void initializeGraph();
  133. CGPathNode::EAccessibility evaluateAccessibility(const int3 &pos, const TerrainTile *tinfo) const;
  134. bool canMoveBetween(const int3 &a, const int3 &b) const; //checks only for visitable objects that may make moving between tiles impossible, not other conditions (like tiles itself accessibility)
  135. bool addTeleportTwoWay(const CGTeleport * obj) const;
  136. bool addTeleportOneWay(const CGTeleport * obj) const;
  137. bool addTeleportOneWayRandom(const CGTeleport * obj) const;
  138. bool addTeleportWhirlpool(const CGWhirlpool * obj) const;
  139. bool canVisitObject() const;
  140. };