CPathfinder.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #pragma once
  2. #include "VCMI_Lib.h"
  3. #include "mapping/CMap.h"
  4. #include "IGameCallback.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. struct DLL_LINKAGE CGPathNode
  20. {
  21. enum ENodeAction
  22. {
  23. UNKNOWN = -1,
  24. NORMAL = 0,
  25. EMBARK = 1,
  26. DISEMBARK, //2
  27. BATTLE,//3
  28. VISIT,//4
  29. BLOCKING_VISIT//5
  30. };
  31. enum EAccessibility
  32. {
  33. NOT_SET = 0,
  34. ACCESSIBLE = 1, //tile can be entered and passed
  35. VISITABLE, //tile can be entered as the last tile in path
  36. BLOCKVIS, //visitable from neighbouring tile but not passable
  37. BLOCKED //tile can't be entered nor visited
  38. };
  39. bool locked;
  40. EAccessibility accessible;
  41. ui8 land;
  42. ui8 turns; //how many turns we have to wait before reachng the tile - 0 means current turn
  43. ui32 moveRemains; //remaining tiles after hero reaches the tile
  44. CGPathNode * theNodeBefore;
  45. int3 coord; //coordinates
  46. EPathfindingLayer layer;
  47. ENodeAction action;
  48. CGPathNode(int3 Coord, EPathfindingLayer Layer);
  49. void reset();
  50. bool reachable() const;
  51. };
  52. struct DLL_LINKAGE CGPath
  53. {
  54. std::vector<CGPathNode> nodes; //just get node by node
  55. int3 startPos() const; // start point
  56. int3 endPos() const; //destination point
  57. void convert(ui8 mode); //mode=0 -> from 'manifest' to 'object'
  58. };
  59. struct DLL_LINKAGE CPathsInfo
  60. {
  61. mutable boost::mutex pathMx;
  62. const CGHeroInstance *hero;
  63. int3 hpos;
  64. int3 sizes;
  65. boost::multi_array<CGPathNode *, 4> nodes; //[w][h][level][layer]
  66. CPathsInfo(const int3 &Sizes);
  67. ~CPathsInfo();
  68. const CGPathNode * getPathInfo(const int3 &tile, const EPathfindingLayer &layer = EPathfindingLayer::AUTO) const;
  69. bool getPath(CGPath &out, const int3 &dst, const EPathfindingLayer &layer = EPathfindingLayer::AUTO) const;
  70. int getDistance(const int3 &tile, const EPathfindingLayer &layer = EPathfindingLayer::AUTO) const;
  71. CGPathNode *getNode(const int3 &coord, const EPathfindingLayer &layer) const;
  72. };
  73. class CPathfinder : private CGameInfoCallback
  74. {
  75. public:
  76. CPathfinder(CPathsInfo &_out, CGameState *_gs, const CGHeroInstance *_hero);
  77. 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
  78. private:
  79. struct PathfinderOptions
  80. {
  81. bool useFlying;
  82. bool useWaterWalking;
  83. bool useEmbarkAndDisembark;
  84. bool useTeleportTwoWay; // Two-way monoliths and Subterranean Gate
  85. bool useTeleportOneWay; // One-way monoliths with one known exit only
  86. bool useTeleportOneWayRandom; // One-way monoliths with more than one known exit
  87. bool useTeleportWhirlpool; // Force enabled if hero protected or unaffected (have one stack of one creature)
  88. /// If true transition into air layer only possible from initial node.
  89. /// This is drastically decrease path calculation complexity (and time).
  90. /// Downside is less MP effective paths calculation.
  91. bool lightweightFlyingMode;
  92. PathfinderOptions();
  93. } options;
  94. CPathsInfo &out;
  95. const CGHeroInstance *hero;
  96. struct NodeComparer
  97. {
  98. bool operator()(const CGPathNode * lhs, const CGPathNode * rhs) const
  99. {
  100. if(rhs->turns > lhs->turns)
  101. return false;
  102. else if(rhs->turns == lhs->turns && rhs->moveRemains < lhs->moveRemains)
  103. return false;
  104. return true;
  105. }
  106. };
  107. boost::heap::priority_queue<CGPathNode *, boost::heap::compare<NodeComparer> > pq;
  108. std::vector<int3> neighbours;
  109. CGPathNode *cp; //current (source) path node -> we took it from the queue
  110. CGPathNode *dp; //destination node -> it's a neighbour of cp that we consider
  111. const TerrainTile *ct, *dt; //tile info for both nodes
  112. const CGObjectInstance *sTileObj;
  113. CGPathNode::ENodeAction destAction;
  114. void addNeighbours(const int3 &coord);
  115. void addTeleportExits(bool noTeleportExcludes = false);
  116. bool isLayerTransitionPossible();
  117. bool isMovementToDestPossible();
  118. bool isMovementAfterDestPossible();
  119. bool isSourceInitialPosition();
  120. int3 getSourceGuardPosition();
  121. bool isSourceGuarded();
  122. bool isDestinationGuarded(bool ignoreAccessibility = true);
  123. bool isDestinationGuardian();
  124. void initializeGraph();
  125. CGPathNode::EAccessibility evaluateAccessibility(const int3 &pos, const TerrainTile *tinfo) const;
  126. 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)
  127. bool addTeleportTwoWay(const CGTeleport * obj) const;
  128. bool addTeleportOneWay(const CGTeleport * obj) const;
  129. bool addTeleportOneWayRandom(const CGTeleport * obj) const;
  130. bool addTeleportWhirlpool(const CGWhirlpool * obj) const;
  131. bool canVisitObject() const;
  132. };