CPathfinder.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #pragma once
  2. #include "VCMI_Lib.h"
  3. #include "mapping/CMap.h"
  4. #include "IGameCallback.h"
  5. #include "int3.h"
  6. /*
  7. * CPathfinder.h, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. class CGHeroInstance;
  16. class CGObjectInstance;
  17. struct TerrainTile;
  18. struct DLL_LINKAGE CGPathNode
  19. {
  20. enum EAccessibility
  21. {
  22. NOT_SET = 0,
  23. ACCESSIBLE = 1, //tile can be entered and passed
  24. VISITABLE, //tile can be entered as the last tile in path
  25. BLOCKVIS, //visitable from neighbouring tile but not passable
  26. BLOCKED //tile can't be entered nor visited
  27. };
  28. EAccessibility accessible;
  29. ui8 land;
  30. ui8 turns; //how many turns we have to wait before reachng the tile - 0 means current turn
  31. ui32 moveRemains; //remaining tiles after hero reaches the tile
  32. CGPathNode * theNodeBefore;
  33. int3 coord; //coordinates
  34. EPathfindingLayer layer;
  35. CGPathNode();
  36. bool reachable() const;
  37. };
  38. struct DLL_LINKAGE CGPath
  39. {
  40. std::vector<CGPathNode> nodes; //just get node by node
  41. int3 startPos() const; // start point
  42. int3 endPos() const; //destination point
  43. void convert(ui8 mode); //mode=0 -> from 'manifest' to 'object'
  44. };
  45. struct DLL_LINKAGE CPathsInfo
  46. {
  47. mutable boost::mutex pathMx;
  48. const CGHeroInstance *hero;
  49. int3 hpos;
  50. int3 sizes;
  51. CGPathNode ****nodes; //[w][h][level][layer]
  52. CPathsInfo(const int3 &Sizes);
  53. ~CPathsInfo();
  54. const CGPathNode * getPathInfo(const int3 &tile, const EPathfindingLayer &layer = EPathfindingLayer::AUTO) const;
  55. bool getPath(CGPath &out, const int3 &dst, const EPathfindingLayer &layer = EPathfindingLayer::AUTO) const;
  56. int getDistance(const int3 &tile, const EPathfindingLayer &layer = EPathfindingLayer::AUTO) const;
  57. };
  58. class CPathfinder : private CGameInfoCallback
  59. {
  60. public:
  61. CPathfinder(CPathsInfo &_out, CGameState *_gs, const CGHeroInstance *_hero);
  62. 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
  63. private:
  64. struct PathfinderOptions
  65. {
  66. bool useFlying;
  67. bool useWaterWalking;
  68. bool useEmbarkAndDisembark;
  69. bool useTeleportTwoWay; // Two-way monoliths and Subterranean Gate
  70. bool useTeleportOneWay; // One-way monoliths with one known exit only
  71. bool useTeleportOneWayRandom; // One-way monoliths with more than one known exit
  72. bool useTeleportWhirlpool; // Force enabled if hero protected or unaffected (have one stack of one creature)
  73. PathfinderOptions();
  74. } options;
  75. CPathsInfo &out;
  76. const CGHeroInstance *hero;
  77. const std::vector<std::vector<std::vector<ui8> > > &FoW;
  78. std::list<CGPathNode*> mq; //BFS queue -> nodes to be checked
  79. std::vector<int3> neighbours;
  80. CGPathNode *cp; //current (source) path node -> we took it from the queue
  81. CGPathNode *dp; //destination node -> it's a neighbour of cp that we consider
  82. const TerrainTile *ct, *dt; //tile info for both nodes
  83. const CGObjectInstance *sTileObj;
  84. ui8 useEmbarkCost; //0 - usual movement; 1 - embark; 2 - disembark
  85. void addNeighbours(const int3 &coord);
  86. void addTeleportExits(bool noTeleportExcludes = false);
  87. bool isMovementPossible(); //checks if current move will be between sea<->land. If so, checks it legality (returns false if movement is not possible) and sets useEmbarkCost
  88. bool checkDestinationTile();
  89. int3 getSourceGuardPosition();
  90. bool isSourceGuarded();
  91. bool isDestinationGuarded();
  92. bool isDestinationGuardian();
  93. void initializeGraph();
  94. CGPathNode *getNode(const int3 &coord, const EPathfindingLayer &layer);
  95. CGPathNode::EAccessibility evaluateAccessibility(const int3 &pos, const TerrainTile *tinfo) const;
  96. 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)
  97. bool addTeleportTwoWay(const CGTeleport * obj) const;
  98. bool addTeleportOneWay(const CGTeleport * obj) const;
  99. bool addTeleportOneWayRandom(const CGTeleport * obj) const;
  100. bool addTeleportWhirlpool(const CGWhirlpool * obj) const;
  101. bool canVisitObject() const;
  102. };