CGPathNode.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. ui32 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. using TFibHeap = boost::heap::fibonacci_heap<CGPathNode *, boost::heap::compare<NodeComparer<CGPathNode>>>;
  128. TFibHeap::handle_type pqHandle;
  129. TFibHeap* pq;
  130. private:
  131. float cost; //total cost of the path to this tile measured in turns with fractions
  132. };
  133. struct DLL_LINKAGE CGPath
  134. {
  135. std::vector<CGPathNode> nodes; //just get node by node
  136. int3 startPos() const; // start point
  137. int3 endPos() const; //destination point
  138. };
  139. struct DLL_LINKAGE CPathsInfo
  140. {
  141. using ELayer = EPathfindingLayer;
  142. const CGHeroInstance * hero;
  143. int3 hpos;
  144. int3 sizes;
  145. boost::multi_array<CGPathNode, 4> nodes; //[layer][level][w][h]
  146. CPathsInfo(const int3 & Sizes, const CGHeroInstance * hero_);
  147. ~CPathsInfo();
  148. const CGPathNode * getPathInfo(const int3 & tile) const;
  149. bool getPath(CGPath & out, const int3 & dst) const;
  150. const CGPathNode * getNode(const int3 & coord) const;
  151. STRONG_INLINE
  152. CGPathNode * getNode(const int3 & coord, const ELayer layer)
  153. {
  154. return &nodes[layer][coord.z][coord.x][coord.y];
  155. }
  156. };
  157. struct DLL_LINKAGE PathNodeInfo
  158. {
  159. CGPathNode * node;
  160. const CGObjectInstance * nodeObject;
  161. const CGHeroInstance * nodeHero;
  162. const TerrainTile * tile;
  163. int3 coord;
  164. bool guarded;
  165. PlayerRelations::PlayerRelations objectRelations;
  166. PlayerRelations::PlayerRelations heroRelations;
  167. bool isInitialPosition;
  168. PathNodeInfo();
  169. virtual void setNode(CGameState * gs, CGPathNode * n);
  170. void updateInfo(CPathfinderHelper * hlp, CGameState * gs);
  171. bool isNodeObjectVisitable() const;
  172. };
  173. struct DLL_LINKAGE CDestinationNodeInfo : public PathNodeInfo
  174. {
  175. EPathNodeAction action;
  176. int turn;
  177. int movementLeft;
  178. float cost; //same as CGPathNode::cost
  179. bool blocked;
  180. bool isGuardianTile;
  181. CDestinationNodeInfo();
  182. virtual void setNode(CGameState * gs, CGPathNode * n) override;
  183. virtual bool isBetterWay() const;
  184. };
  185. VCMI_LIB_NAMESPACE_END