CGPathNode.h 4.8 KB

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