CPathfinder.h 9.2 KB

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