CPathfinder.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. CGPathNode();
  35. bool reachable() const;
  36. };
  37. struct DLL_LINKAGE CGPath
  38. {
  39. std::vector<CGPathNode> nodes; //just get node by node
  40. int3 startPos() const; // start point
  41. int3 endPos() const; //destination point
  42. void convert(ui8 mode); //mode=0 -> from 'manifest' to 'object'
  43. };
  44. struct DLL_LINKAGE CPathsInfo
  45. {
  46. mutable boost::mutex pathMx;
  47. const CGHeroInstance *hero;
  48. int3 hpos;
  49. int3 sizes;
  50. CGPathNode ***nodes; //[w][h][level]
  51. CPathsInfo(const int3 &Sizes);
  52. ~CPathsInfo();
  53. const CGPathNode * getPathInfo( int3 tile ) const;
  54. bool getPath(const int3 &dst, CGPath &out) const;
  55. int getDistance( int3 tile ) const;
  56. };
  57. class CPathfinder : private CGameInfoCallback
  58. {
  59. public:
  60. CPathfinder(CPathsInfo &_out, CGameState *_gs, const CGHeroInstance *_hero);
  61. 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
  62. private:
  63. struct PathfinderOptions
  64. {
  65. bool useFlying;
  66. bool useWaterWalking;
  67. bool useEmbarkAndDisembark;
  68. bool useTeleportTwoWay; // Two-way monoliths and Subterranean Gate
  69. bool useTeleportOneWay; // One-way monoliths with one known exit only
  70. bool useTeleportOneWayRandom; // One-way monoliths with more than one known exit
  71. bool useTeleportWhirlpool; // Force enabled if hero protected or unaffected (have one stack of one creature)
  72. PathfinderOptions();
  73. } options;
  74. CPathsInfo &out;
  75. const CGHeroInstance *hero;
  76. const std::vector<std::vector<std::vector<ui8> > > &FoW;
  77. std::list<CGPathNode*> mq; //BFS queue -> nodes to be checked
  78. std::vector<int3> neighbours;
  79. CGPathNode *cp; //current (source) path node -> we took it from the queue
  80. CGPathNode *dp; //destination node -> it's a neighbour of cp that we consider
  81. const TerrainTile *ct, *dt; //tile info for both nodes
  82. const CGObjectInstance *sTileObj;
  83. ui8 useEmbarkCost; //0 - usual movement; 1 - embark; 2 - disembark
  84. void addNeighbours(const int3 &coord);
  85. void addTeleportExits(bool noTeleportExcludes = false);
  86. 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
  87. bool checkDestinationTile();
  88. int3 getSourceGuardPosition();
  89. bool isSourceGuarded();
  90. bool isDestinationGuarded();
  91. bool isDestinationGuardian();
  92. void initializeGraph();
  93. CGPathNode *getNode(const int3 &coord);
  94. CGPathNode::EAccessibility evaluateAccessibility(const int3 &pos, const TerrainTile *tinfo) const;
  95. 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)
  96. bool addTeleportTwoWay(const CGTeleport * obj) const;
  97. bool addTeleportOneWay(const CGTeleport * obj) const;
  98. bool addTeleportOneWayRandom(const CGTeleport * obj) const;
  99. bool addTeleportWhirlpool(const CGWhirlpool * obj) const;
  100. };