CPathfinder.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef CPathfinder_H
  2. #define CPathfinder_H
  3. #include "global.h"
  4. #include <vector>
  5. #include <queue>
  6. #include <math.h>
  7. class CGHeroInstance;
  8. using namespace std;
  9. class Coordinate
  10. {
  11. public:
  12. int x;
  13. int y;
  14. int z;
  15. float g; //Distance from goal
  16. int h; //Movement cost
  17. Coordinate() : x(NULL), y(NULL), z(NULL), g(NULL), h(NULL) {}
  18. Coordinate(int3 xyz) : x(xyz.x), y(xyz.y), z(xyz.z){}
  19. Coordinate(int xCor, int yCor, int zCor) : x(xCor), y(yCor), z(zCor){}
  20. Coordinate(int xCor, int yCor, int zCor, int dist, int penalty) : x(xCor), y(yCor), z(zCor), g(dist), h(penalty){}
  21. void operator=(const Coordinate& other);
  22. };
  23. //Class used in priority queue to determine order.
  24. class Compare
  25. {
  26. public:
  27. bool operator()(const vector<Coordinate>& a, const vector<Coordinate>& b);
  28. };
  29. //Included for old format
  30. struct CPathNode
  31. {
  32. bool accesible; //true if a hero can be on this node
  33. int dist; //distance from the first node of searching; -1 is infinity
  34. CPathNode * theNodeBefore;
  35. int3 coord; //coordiantes
  36. bool visited;
  37. };
  38. //Included for old format
  39. struct CPath
  40. {
  41. std::vector<CPathNode> nodes; //just get node by node
  42. int3 startPos(); // start point
  43. int3 endPos(); //destination point
  44. };
  45. class CPathfinder
  46. {
  47. private:
  48. boost::logic::tribool blockLandSea; //true - blocks sea, false - blocks land, indeterminate - allows all
  49. /*
  50. * Does the actual path calculation. Don't call this directly, call GetPath instead.
  51. */
  52. vector<Coordinate>* CalcPath();
  53. /*
  54. * Determines if the given node exists in the closed list, returns true if it does. This is
  55. * used to ensure you dont check the same node twice.
  56. */
  57. bool ExistsInClosed(Coordinate node);
  58. /*
  59. * Adds the neighbors of the current node to the open cue so they can be considered in the
  60. * path creation. If the node has a cost (f = g + h) less than zero, it isn't added to Open.
  61. */
  62. void AddNeighbors(vector<Coordinate>* node);
  63. /*
  64. * Calculates the movement cost of the node. Returns -1 if it is impossible to travel on.
  65. */
  66. void CalcH(Coordinate* node);
  67. /*
  68. * Calculates distance from node to end node.
  69. */
  70. void CalcG(Coordinate* node);
  71. public:
  72. //Contains nodes to be searched
  73. priority_queue < vector<Coordinate>, vector<vector<Coordinate>>, Compare> Open;
  74. //History of nodes you have been to before
  75. vector<Coordinate> Closed;
  76. //The start, or source position.
  77. Coordinate Start;
  78. //The end, or destination position
  79. Coordinate End;
  80. //A reference to the Hero.
  81. const CGHeroInstance* Hero;
  82. /*
  83. * Does basic input checking and setup for the path calculation.
  84. */
  85. vector<Coordinate>* GetPath(const CGHeroInstance* hero);
  86. vector<Coordinate>* GetPath(Coordinate start, Coordinate end, const CGHeroInstance* hero);
  87. //Convert to oldformat
  88. CPath* ConvertToOldFormat(vector<Coordinate>* p);
  89. static void convertPath(CPath * path, unsigned int mode); //mode=0 -> from 'manifest' to 'object'
  90. };
  91. #endif //CPATHFINDER_H