CPathfinder.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. class CPathfinderHelper;
  20. struct DLL_LINKAGE CGPathNode
  21. {
  22. typedef EPathfindingLayer ELayer;
  23. enum ENodeAction
  24. {
  25. UNKNOWN = -1,
  26. NORMAL = 0,
  27. EMBARK = 1,
  28. DISEMBARK, //2
  29. BATTLE,//3
  30. VISIT,//4
  31. BLOCKING_VISIT//5
  32. };
  33. enum EAccessibility
  34. {
  35. NOT_SET = 0,
  36. ACCESSIBLE = 1, //tile can be entered and passed
  37. VISITABLE, //tile can be entered as the last tile in path
  38. BLOCKVIS, //visitable from neighbouring tile but not passable
  39. BLOCKED //tile can't be entered nor visited
  40. };
  41. bool locked;
  42. EAccessibility accessible;
  43. ui8 turns; //how many turns we have to wait before reachng the tile - 0 means current turn
  44. ui32 moveRemains; //remaining tiles after hero reaches the tile
  45. CGPathNode * theNodeBefore;
  46. int3 coord; //coordinates
  47. ELayer layer;
  48. ENodeAction action;
  49. CGPathNode();
  50. void reset();
  51. void update(const int3 & Coord, const ELayer Layer, const EAccessibility Accessible);
  52. bool reachable() const;
  53. };
  54. struct DLL_LINKAGE CGPath
  55. {
  56. std::vector<CGPathNode> nodes; //just get node by node
  57. int3 startPos() const; // start point
  58. int3 endPos() const; //destination point
  59. void convert(ui8 mode); //mode=0 -> from 'manifest' to 'object'
  60. };
  61. struct DLL_LINKAGE CPathsInfo
  62. {
  63. typedef EPathfindingLayer ELayer;
  64. mutable boost::mutex pathMx;
  65. const CGHeroInstance * hero;
  66. int3 hpos;
  67. int3 sizes;
  68. boost::multi_array<CGPathNode, 4> nodes; //[w][h][level][layer]
  69. CPathsInfo(const int3 & Sizes);
  70. ~CPathsInfo();
  71. const CGPathNode * getPathInfo(const int3 & tile) const;
  72. bool getPath(CGPath & out, const int3 & dst) const;
  73. int getDistance(const int3 & tile) const;
  74. const CGPathNode * getNode(const int3 & coord) const;
  75. CGPathNode * getNode(const int3 & coord, const ELayer layer);
  76. };
  77. class CPathfinder : private CGameInfoCallback
  78. {
  79. public:
  80. friend class CPathfinderHelper;
  81. CPathfinder(CPathsInfo & _out, CGameState * _gs, const CGHeroInstance * _hero);
  82. 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
  83. private:
  84. typedef EPathfindingLayer ELayer;
  85. struct PathfinderOptions
  86. {
  87. bool useFlying;
  88. bool useWaterWalking;
  89. bool useEmbarkAndDisembark;
  90. bool useTeleportTwoWay; // Two-way monoliths and Subterranean Gate
  91. bool useTeleportOneWay; // One-way monoliths with one known exit only
  92. bool useTeleportOneWayRandom; // One-way monoliths with more than one known exit
  93. bool useTeleportWhirlpool; // Force enabled if hero protected or unaffected (have one stack of one creature)
  94. /// TODO: Find out with client and server code, merge with normal teleporters.
  95. /// Likely proper implementation would require some refactoring of CGTeleport.
  96. /// So for now this is unfinished and disabled by default.
  97. bool useCastleGate;
  98. /// If true transition into air layer only possible from initial node.
  99. /// This is drastically decrease path calculation complexity (and time).
  100. /// Downside is less MP effective paths calculation.
  101. bool lightweightFlyingMode;
  102. /// This option enable one turn limitation for flying and water walking.
  103. /// So if we're out of MP while cp is blocked or water tile we won't add dest tile to queue.
  104. ///
  105. /// Following imitation is default H3 mechanics, but someone may want to disable it in mods.
  106. /// After all this limit should benefit performance on maps with tons of water or blocked tiles.
  107. bool oneTurnSpecialLayersLimit;
  108. /// VCMI have different movement rules to solve flaws original engine has.
  109. /// If this option enabled you'll able to do following things in fly:
  110. /// - Move from blocked tiles to visitable one
  111. /// - Move from guarded tiles to blockvis tiles without being attacked
  112. /// - Move from guarded tiles to guarded visitable tiles with being attacked after
  113. bool originalMovementRules;
  114. PathfinderOptions();
  115. } options;
  116. CPathsInfo & out;
  117. const CGHeroInstance * hero;
  118. unique_ptr<CPathfinderHelper> hlp;
  119. struct NodeComparer
  120. {
  121. bool operator()(const CGPathNode * lhs, const CGPathNode * rhs) const
  122. {
  123. if(rhs->turns > lhs->turns)
  124. return false;
  125. else if(rhs->turns == lhs->turns && rhs->moveRemains < lhs->moveRemains)
  126. return false;
  127. return true;
  128. }
  129. };
  130. boost::heap::priority_queue<CGPathNode *, boost::heap::compare<NodeComparer> > pq;
  131. std::vector<int3> neighbours;
  132. CGPathNode * cp; //current (source) path node -> we took it from the queue
  133. CGPathNode * dp; //destination node -> it's a neighbour of cp that we consider
  134. const TerrainTile * ct, * dt; //tile info for both nodes
  135. const CGObjectInstance * ctObj, * dtObj;
  136. CGPathNode::ENodeAction destAction;
  137. void addNeighbours();
  138. void addTeleportExits();
  139. bool isLayerTransitionPossible() const;
  140. bool isMovementToDestPossible() const;
  141. bool isMovementAfterDestPossible() const;
  142. CGPathNode::ENodeAction getDestAction() const;
  143. bool isSourceInitialPosition() const;
  144. bool isSourceVisitableObj() const;
  145. bool isSourceGuarded() const;
  146. bool isDestinationGuarded(const bool ignoreAccessibility = true) const;
  147. bool isDestinationGuardian() const;
  148. void initializeGraph();
  149. CGPathNode::EAccessibility evaluateAccessibility(const int3 & pos, const TerrainTile * tinfo) const;
  150. 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)
  151. bool isAllowedTeleportEntrance(const CGTeleport * obj) const;
  152. bool addTeleportTwoWay(const CGTeleport * obj) const;
  153. bool addTeleportOneWay(const CGTeleport * obj) const;
  154. bool addTeleportOneWayRandom(const CGTeleport * obj) const;
  155. bool addTeleportWhirlpool(const CGWhirlpool * obj) const;
  156. };
  157. struct DLL_LINKAGE TurnInfo
  158. {
  159. const CGHeroInstance * hero;
  160. TBonusListPtr bonuses;
  161. mutable int maxMovePointsLand;
  162. mutable int maxMovePointsWater;
  163. TurnInfo(const CGHeroInstance * Hero, const int Turn = 0);
  164. bool isLayerAvailable(const EPathfindingLayer layer) const;
  165. bool hasBonusOfType(const Bonus::BonusType type, const int subtype = -1) const;
  166. int valOfBonuses(const Bonus::BonusType type, const int subtype = -1) const;
  167. int getMaxMovePoints(const EPathfindingLayer layer) const;
  168. };
  169. class DLL_LINKAGE CPathfinderHelper
  170. {
  171. public:
  172. CPathfinderHelper(const CGHeroInstance * Hero, const CPathfinder::PathfinderOptions & Options);
  173. void updateTurnInfo(const int turn = 0);
  174. bool isLayerAvailable(const EPathfindingLayer layer) const;
  175. const TurnInfo * getTurnInfo() const;
  176. bool hasBonusOfType(const Bonus::BonusType type, const int subtype = -1) const;
  177. int getMaxMovePoints(const EPathfindingLayer layer) const;
  178. static void getNeighbours(CGameState * gs, const TerrainTile & srct, const int3 & tile, std::vector<int3> & vec, const boost::logic::tribool & onLand, const bool limitCoastSailing);
  179. static int getMovementCost(const CGHeroInstance * h, const int3 & src, const int3 & dst, const int remainingMovePoints =- 1, const TurnInfo * ti = nullptr, const bool checkLast = true);
  180. static int getMovementCost(const CGHeroInstance * h, const int3 & dst);
  181. private:
  182. int turn;
  183. const CGHeroInstance * hero;
  184. std::vector<TurnInfo *> turnsInfo;
  185. const CPathfinder::PathfinderOptions & options;
  186. };