pathfinder.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. Minetest
  3. Copyright (C) 2013 sapier, sapier at gmx dot net
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #ifndef PATHFINDER_H_
  17. #define PATHFINDER_H_
  18. /******************************************************************************/
  19. /* Includes */
  20. /******************************************************************************/
  21. #include <vector>
  22. #include "irr_v3d.h"
  23. /******************************************************************************/
  24. /* Forward declarations */
  25. /******************************************************************************/
  26. class ServerEnvironment;
  27. /******************************************************************************/
  28. /* Typedefs and macros */
  29. /******************************************************************************/
  30. //#define PATHFINDER_DEBUG
  31. typedef enum {
  32. DIR_XP,
  33. DIR_XM,
  34. DIR_ZP,
  35. DIR_ZM
  36. } path_directions;
  37. /** List of supported algorithms */
  38. typedef enum {
  39. DIJKSTRA, /**< Dijkstra shortest path algorithm */
  40. A_PLAIN, /**< A* algorithm using heuristics to find a path */
  41. A_PLAIN_NP /**< A* algorithm without prefetching of map data */
  42. } algorithm;
  43. /******************************************************************************/
  44. /* declarations */
  45. /******************************************************************************/
  46. /** c wrapper function to use from scriptapi */
  47. std::vector<v3s16> get_Path(ServerEnvironment* env,
  48. v3s16 source,
  49. v3s16 destination,
  50. unsigned int searchdistance,
  51. unsigned int max_jump,
  52. unsigned int max_drop,
  53. algorithm algo);
  54. /** representation of cost in specific direction */
  55. class path_cost {
  56. public:
  57. /** default constructor */
  58. path_cost();
  59. /** copy constructor */
  60. path_cost(const path_cost& b);
  61. /** assignment operator */
  62. path_cost& operator= (const path_cost& b);
  63. bool valid; /**< movement is possible */
  64. int value; /**< cost of movement */
  65. int direction; /**< y-direction of movement */
  66. bool updated; /**< this cost has ben calculated */
  67. };
  68. /** representation of a mapnode to be used for pathfinding */
  69. class path_gridnode {
  70. public:
  71. /** default constructor */
  72. path_gridnode();
  73. /** copy constructor */
  74. path_gridnode(const path_gridnode& b);
  75. /**
  76. * assignment operator
  77. * @param b node to copy
  78. */
  79. path_gridnode& operator= (const path_gridnode& b);
  80. /**
  81. * read cost in a specific direction
  82. * @param dir direction of cost to fetch
  83. */
  84. path_cost get_cost(v3s16 dir);
  85. /**
  86. * set cost value for movement
  87. * @param dir direction to set cost for
  88. * @cost cost to set
  89. */
  90. void set_cost(v3s16 dir,path_cost cost);
  91. bool valid; /**< node is on surface */
  92. bool target; /**< node is target position */
  93. bool source; /**< node is stating position */
  94. int totalcost; /**< cost to move here from starting point */
  95. v3s16 sourcedir; /**< origin of movement for current cost */
  96. int surfaces; /**< number of surfaces with same x,z value*/
  97. v3s16 pos; /**< real position of node */
  98. path_cost directions[4]; /**< cost in different directions */
  99. /* debug values */
  100. bool is_element; /**< node is element of path detected */
  101. char type; /**< type of node */
  102. };
  103. /** class doing pathfinding */
  104. class pathfinder {
  105. public:
  106. /**
  107. * default constructor
  108. */
  109. pathfinder();
  110. /**
  111. * path evaluation function
  112. * @param env environment to look for path
  113. * @param source origin of path
  114. * @param destination end position of path
  115. * @param searchdistance maximum number of nodes to look in each direction
  116. * @param max_jump maximum number of blocks a path may jump up
  117. * @param max_drop maximum number of blocks a path may drop
  118. * @param algo algorithm to use for finding a path
  119. */
  120. std::vector<v3s16> get_Path(ServerEnvironment* env,
  121. v3s16 source,
  122. v3s16 destination,
  123. unsigned int searchdistance,
  124. unsigned int max_jump,
  125. unsigned int max_drop,
  126. algorithm algo);
  127. private:
  128. /** data struct for storing internal information */
  129. struct limits {
  130. struct limit {
  131. int min;
  132. int max;
  133. };
  134. limit X;
  135. limit Y;
  136. limit Z;
  137. };
  138. /* helper functions */
  139. /**
  140. * transform index pos to mappos
  141. * @param ipos a index position
  142. * @return map position
  143. */
  144. v3s16 getRealPos(v3s16 ipos);
  145. /**
  146. * transform mappos to index pos
  147. * @param pos a real pos
  148. * @return index position
  149. */
  150. v3s16 getIndexPos(v3s16 pos);
  151. /**
  152. * get gridnode at a specific index position
  153. * @param ipos index position
  154. * @return gridnode for index
  155. */
  156. path_gridnode& getIndexElement(v3s16 ipos);
  157. /**
  158. * invert a 3d position
  159. * @param pos 3d position
  160. * @return pos *-1
  161. */
  162. v3s16 invert(v3s16 pos);
  163. /**
  164. * check if a index is within current search area
  165. * @param index position to validate
  166. * @return true/false
  167. */
  168. bool valid_index(v3s16 index);
  169. /**
  170. * translate position to float position
  171. * @param pos integer position
  172. * @return float position
  173. */
  174. v3f tov3f(v3s16 pos);
  175. /* algorithm functions */
  176. /**
  177. * calculate 2d manahttan distance to target
  178. * @param pos position to calc distance
  179. * @return integer distance
  180. */
  181. int get_manhattandistance(v3s16 pos);
  182. /**
  183. * get best direction based uppon heuristics
  184. * @param directions list of unchecked directions
  185. * @param g_pos mapnode to start from
  186. * @return direction to check
  187. */
  188. v3s16 get_dir_heuristic(std::vector<v3s16>& directions,path_gridnode& g_pos);
  189. /**
  190. * build internal data representation of search area
  191. * @return true/false if costmap creation was successfull
  192. */
  193. bool build_costmap();
  194. /**
  195. * calculate cost of movement
  196. * @param pos real world position to start movement
  197. * @param dir direction to move to
  198. * @return cost information
  199. */
  200. path_cost calc_cost(v3s16 pos,v3s16 dir);
  201. /**
  202. * recursive update whole search areas total cost information
  203. * @param ipos position to check next
  204. * @param srcdir positionc checked last time
  205. * @param total_cost cost of moving to ipos
  206. * @param level current recursion depth
  207. * @return true/false path to destination has been found
  208. */
  209. bool update_all_costs(v3s16 ipos,v3s16 srcdir,int total_cost,int level);
  210. /**
  211. * recursive try to find a patrh to destionation
  212. * @param ipos position to check next
  213. * @param srcdir positionc checked last time
  214. * @param total_cost cost of moving to ipos
  215. * @param level current recursion depth
  216. * @return true/false path to destination has been found
  217. */
  218. bool update_cost_heuristic(v3s16 ipos,v3s16 srcdir,int current_cost,int level);
  219. /**
  220. * recursive build a vector containing all nodes from source to destination
  221. * @param path vector to add nodes to
  222. * @param pos pos to check next
  223. * @param level recursion depth
  224. */
  225. void build_path(std::vector<v3s16>& path,v3s16 pos, int level);
  226. /* variables */
  227. int m_max_index_x; /**< max index of search area in x direction */
  228. int m_max_index_y; /**< max index of search area in y direction */
  229. int m_max_index_z; /**< max index of search area in z direction */
  230. int m_searchdistance; /**< max distance to search in each direction */
  231. int m_maxdrop; /**< maximum number of blocks a path may drop */
  232. int m_maxjump; /**< maximum number of blocks a path may jump */
  233. int m_min_target_distance; /**< current smalest path to target */
  234. bool m_prefetch; /**< prefetch cost data */
  235. v3s16 m_start; /**< source position */
  236. v3s16 m_destination; /**< destination position */
  237. limits m_limits; /**< position limits in real map coordinates */
  238. /** 3d grid containing all map data already collected and analyzed */
  239. std::vector<std::vector<std::vector<path_gridnode> > > m_data;
  240. ServerEnvironment* m_env; /**< minetest environment pointer */
  241. #ifdef PATHFINDER_DEBUG
  242. /**
  243. * print collected cost information
  244. */
  245. void print_cost();
  246. /**
  247. * print collected cost information in a specific direction
  248. * @param dir direction to print
  249. */
  250. void print_cost(path_directions dir);
  251. /**
  252. * print type of node as evaluated
  253. */
  254. void print_type();
  255. /**
  256. * print pathlenght for all nodes in search area
  257. */
  258. void print_pathlen();
  259. /**
  260. * print a path
  261. * @param path path to show
  262. */
  263. void print_path(std::vector<v3s16> path);
  264. /**
  265. * print y direction for all movements
  266. */
  267. void print_ydir();
  268. /**
  269. * print y direction for moving in a specific direction
  270. * @param dir direction to show data
  271. */
  272. void print_ydir(path_directions dir);
  273. /**
  274. * helper function to translate a direction to speaking text
  275. * @param dir direction to translate
  276. * @return textual name of direction
  277. */
  278. std::string dir_to_name(path_directions dir);
  279. #endif
  280. };
  281. #endif /* PATHFINDER_H_ */