CGPathNode.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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, //visitable, but in zone of control of nearby monster
  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 ELayer = EPathfindingLayer;
  55. CGPathNode * theNodeBefore;
  56. int3 coord; //coordinates
  57. ELayer layer;
  58. int moveRemains; //remaining movement points after hero reaches the tile
  59. ui8 turns; //how many turns we have to wait before reaching the tile - 0 means current turn
  60. EPathAccessibility accessible;
  61. EPathNodeAction action;
  62. bool locked;
  63. bool inPQ;
  64. CGPathNode()
  65. : coord(-1),
  66. layer(ELayer::WRONG),
  67. pqHandle(nullptr)
  68. {
  69. reset();
  70. }
  71. STRONG_INLINE
  72. void reset()
  73. {
  74. locked = false;
  75. accessible = EPathAccessibility::NOT_SET;
  76. moveRemains = 0;
  77. cost = std::numeric_limits<float>::max();
  78. turns = 255;
  79. theNodeBefore = nullptr;
  80. action = EPathNodeAction::UNKNOWN;
  81. inPQ = false;
  82. pq = nullptr;
  83. }
  84. STRONG_INLINE
  85. float getCost() const
  86. {
  87. return cost;
  88. }
  89. STRONG_INLINE
  90. void setCost(float value)
  91. {
  92. if(vstd::isAlmostEqual(value, cost))
  93. return;
  94. bool getUpNode = value < cost;
  95. cost = value;
  96. // If the node is in the heap, update the heap.
  97. if(inPQ && pq != nullptr)
  98. {
  99. if(getUpNode)
  100. {
  101. pq->increase(this->pqHandle);
  102. }
  103. else
  104. {
  105. pq->decrease(this->pqHandle);
  106. }
  107. }
  108. }
  109. STRONG_INLINE
  110. void update(const int3 & Coord, const ELayer Layer, const EPathAccessibility Accessible)
  111. {
  112. if(layer == ELayer::WRONG)
  113. {
  114. coord = Coord;
  115. layer = Layer;
  116. }
  117. else
  118. {
  119. reset();
  120. }
  121. accessible = Accessible;
  122. }
  123. STRONG_INLINE
  124. bool reachable() const
  125. {
  126. return turns < 255;
  127. }
  128. bool isTeleportAction() const
  129. {
  130. if (action != EPathNodeAction::TELEPORT_NORMAL &&
  131. action != EPathNodeAction::TELEPORT_BLOCKING_VISIT &&
  132. action != EPathNodeAction::TELEPORT_BATTLE)
  133. {
  134. return false;
  135. }
  136. return true;
  137. }
  138. using TFibHeap = boost::heap::fibonacci_heap<CGPathNode *, boost::heap::compare<NodeComparer<CGPathNode>>>;
  139. TFibHeap::handle_type pqHandle;
  140. TFibHeap* pq;
  141. private:
  142. float cost; //total cost of the path to this tile measured in turns with fractions
  143. };
  144. struct DLL_LINKAGE CGPath
  145. {
  146. std::vector<CGPathNode> nodes; //just get node by node
  147. /// Starting position of path, matches location of hero
  148. const CGPathNode & currNode() const;
  149. /// First node in path, this is where hero will move next
  150. const CGPathNode & nextNode() const;
  151. /// Last node in path, this is what hero wants to reach in the end
  152. const CGPathNode & lastNode() const;
  153. int3 startPos() const; // start point
  154. int3 endPos() const; //destination point
  155. };
  156. struct DLL_LINKAGE CPathsInfo
  157. {
  158. using ELayer = EPathfindingLayer;
  159. const CGHeroInstance * hero;
  160. int3 hpos;
  161. int3 sizes;
  162. boost::multi_array<CGPathNode, 4> nodes; //[layer][level][w][h]
  163. CPathsInfo(const int3 & Sizes, const CGHeroInstance * hero_);
  164. ~CPathsInfo();
  165. const CGPathNode * getPathInfo(const int3 & tile) const;
  166. bool getPath(CGPath & out, const int3 & dst) const;
  167. const CGPathNode * getNode(const int3 & coord) const;
  168. STRONG_INLINE
  169. CGPathNode * getNode(const int3 & coord, const ELayer layer)
  170. {
  171. return &nodes[layer.getNum()][coord.z][coord.x][coord.y];
  172. }
  173. };
  174. struct DLL_LINKAGE PathNodeInfo
  175. {
  176. CGPathNode * node;
  177. const CGObjectInstance * nodeObject;
  178. const CGHeroInstance * nodeHero;
  179. const TerrainTile * tile;
  180. int3 coord;
  181. bool guarded;
  182. PlayerRelations objectRelations;
  183. PlayerRelations heroRelations;
  184. bool isInitialPosition;
  185. PathNodeInfo();
  186. virtual void setNode(CGameState * gs, CGPathNode * n);
  187. void updateInfo(CPathfinderHelper * hlp, CGameState * gs);
  188. bool isNodeObjectVisitable() const;
  189. };
  190. struct DLL_LINKAGE CDestinationNodeInfo : public PathNodeInfo
  191. {
  192. EPathNodeAction action;
  193. int turn;
  194. int movementLeft;
  195. float cost; //same as CGPathNode::cost
  196. bool blocked;
  197. bool isGuardianTile;
  198. CDestinationNodeInfo();
  199. void setNode(CGameState * gs, CGPathNode * n) override;
  200. virtual bool isBetterWay() const;
  201. };
  202. VCMI_LIB_NAMESPACE_END