CGPathNode.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * CGPathNode.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 "../GameConstants.h"
  12. #include "../int3.h"
  13. #include <boost/heap/fibonacci_heap.hpp>
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class CGHeroInstance;
  16. class CGObjectInstance;
  17. class CGameState;
  18. class CPathfinderHelper;
  19. struct TerrainTile;
  20. template<typename N>
  21. struct DLL_LINKAGE NodeComparer
  22. {
  23. STRONG_INLINE
  24. bool operator()(const N * lhs, const N * rhs) const
  25. {
  26. return lhs->getCost() > rhs->getCost();
  27. }
  28. };
  29. enum class EPathAccessibility : ui8
  30. {
  31. NOT_SET,
  32. ACCESSIBLE, //tile can be entered and passed
  33. VISITABLE, //tile can be entered as the last tile in path
  34. GUARDED, //tile can be entered, but is in zone of control of nearby monster (may also contain visitable object, if any)
  35. BLOCKVIS, //visitable from neighboring tile but not passable
  36. FLYABLE, //can only be accessed in air layer
  37. BLOCKED //tile can be neither entered nor visited
  38. };
  39. enum class EPathNodeAction : ui8
  40. {
  41. UNKNOWN,
  42. EMBARK,
  43. DISEMBARK,
  44. NORMAL,
  45. BATTLE,
  46. VISIT,
  47. BLOCKING_VISIT,
  48. TELEPORT_NORMAL,
  49. TELEPORT_BLOCKING_VISIT,
  50. TELEPORT_BATTLE
  51. };
  52. struct DLL_LINKAGE CGPathNode
  53. {
  54. using TFibHeap = boost::heap::fibonacci_heap<CGPathNode *, boost::heap::compare<NodeComparer<CGPathNode>>>;
  55. using ELayer = EPathfindingLayer;
  56. TFibHeap::handle_type pqHandle;
  57. TFibHeap * pq;
  58. CGPathNode * theNodeBefore;
  59. int3 coord; //coordinates
  60. ELayer layer;
  61. float cost; //total cost of the path to this tile measured in turns with fractions
  62. int moveRemains; //remaining movement points after hero reaches the tile
  63. ui8 turns; //how many turns we have to wait before reaching the tile - 0 means current turn
  64. EPathAccessibility accessible;
  65. EPathNodeAction action;
  66. bool locked;
  67. CGPathNode()
  68. : coord(-1),
  69. layer(ELayer::WRONG),
  70. pqHandle(nullptr)
  71. {
  72. reset();
  73. }
  74. STRONG_INLINE
  75. void reset()
  76. {
  77. locked = false;
  78. accessible = EPathAccessibility::NOT_SET;
  79. moveRemains = 0;
  80. cost = std::numeric_limits<float>::max();
  81. turns = 255;
  82. theNodeBefore = nullptr;
  83. pq = nullptr;
  84. action = EPathNodeAction::UNKNOWN;
  85. }
  86. STRONG_INLINE
  87. bool inPQ() const
  88. {
  89. return pq != nullptr;
  90. }
  91. STRONG_INLINE
  92. float getCost() const
  93. {
  94. return cost;
  95. }
  96. STRONG_INLINE
  97. void setCost(float value)
  98. {
  99. if(vstd::isAlmostEqual(value, cost))
  100. return;
  101. bool getUpNode = value < cost;
  102. cost = value;
  103. // If the node is in the heap, update the heap.
  104. if(inPQ())
  105. {
  106. if(getUpNode)
  107. {
  108. pq->increase(this->pqHandle);
  109. }
  110. else
  111. {
  112. pq->decrease(this->pqHandle);
  113. }
  114. }
  115. }
  116. STRONG_INLINE
  117. void update(const int3 & Coord, const ELayer Layer, const EPathAccessibility Accessible)
  118. {
  119. if(layer == ELayer::WRONG)
  120. {
  121. coord = Coord;
  122. layer = Layer;
  123. }
  124. else
  125. {
  126. reset();
  127. }
  128. accessible = Accessible;
  129. }
  130. STRONG_INLINE
  131. bool reachable() const
  132. {
  133. return turns < 255;
  134. }
  135. bool isTeleportAction() const
  136. {
  137. if (action != EPathNodeAction::TELEPORT_NORMAL &&
  138. action != EPathNodeAction::TELEPORT_BLOCKING_VISIT &&
  139. action != EPathNodeAction::TELEPORT_BATTLE)
  140. {
  141. return false;
  142. }
  143. return true;
  144. }
  145. };
  146. struct DLL_LINKAGE CGPath
  147. {
  148. std::vector<CGPathNode> nodes; //just get node by node
  149. /// Starting position of path, matches location of hero
  150. const CGPathNode & currNode() const;
  151. /// First node in path, this is where hero will move next
  152. const CGPathNode & nextNode() const;
  153. /// Last node in path, this is what hero wants to reach in the end
  154. const CGPathNode & lastNode() const;
  155. int3 startPos() const; // start point
  156. int3 endPos() const; //destination point
  157. };
  158. struct DLL_LINKAGE CPathsInfo
  159. {
  160. using ELayer = EPathfindingLayer;
  161. const CGHeroInstance * hero;
  162. int3 hpos;
  163. int3 sizes;
  164. /// Bonus tree version for which this information can be considered to be valid
  165. int heroBonusTreeVersion = 0;
  166. boost::multi_array<CGPathNode, 4> nodes; //[layer][level][w][h]
  167. CPathsInfo(const int3 & Sizes, const CGHeroInstance * hero_);
  168. ~CPathsInfo();
  169. const CGPathNode * getPathInfo(const int3 & tile) const;
  170. bool getPath(CGPath & out, const int3 & dst) const;
  171. const CGPathNode * getNode(const int3 & coord) const;
  172. STRONG_INLINE
  173. CGPathNode * getNode(const int3 & coord, const ELayer layer)
  174. {
  175. return &nodes[layer.getNum()][coord.z][coord.x][coord.y];
  176. }
  177. };
  178. struct DLL_LINKAGE PathNodeInfo
  179. {
  180. CGPathNode * node;
  181. const CGObjectInstance * nodeObject;
  182. const CGHeroInstance * nodeHero;
  183. const TerrainTile * tile;
  184. int3 coord;
  185. bool guarded;
  186. PlayerRelations objectRelations;
  187. PlayerRelations heroRelations;
  188. bool isInitialPosition;
  189. PathNodeInfo();
  190. virtual void setNode(const IGameInfoCallback & gameInfo, CGPathNode * n);
  191. void updateInfo(CPathfinderHelper * hlp, const IGameInfoCallback & gameInfo);
  192. bool isNodeObjectVisitable() const;
  193. };
  194. struct DLL_LINKAGE CDestinationNodeInfo : public PathNodeInfo
  195. {
  196. EPathNodeAction action;
  197. int turn;
  198. int movementLeft;
  199. float cost; //same as CGPathNode::cost
  200. bool blocked;
  201. bool isGuardianTile;
  202. CDestinationNodeInfo();
  203. void setNode(const IGameInfoCallback & gameInfo, CGPathNode * n) override;
  204. virtual bool isBetterWay() const;
  205. };
  206. VCMI_LIB_NAMESPACE_END