3 Copyright (C) 2013 sapier, sapier at gmx dot net
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /******************************************************************************/
25 /******************************************************************************/
32 /******************************************************************************/
33 /* Typedefs and macros */
34 /******************************************************************************/
36 //#define PATHFINDER_DEBUG
45 /** List of supported algorithms */
47 DIJKSTRA, /**< Dijkstra shortest path algorithm */
48 A_PLAIN, /**< A* algorithm using heuristics to find a path */
49 A_PLAIN_NP /**< A* algorithm without prefetching of map data */
52 /******************************************************************************/
54 /******************************************************************************/
56 /** c wrapper function to use from scriptapi */
57 std::vector<v3s16> get_Path(ServerEnvironment* env,
60 unsigned int searchdistance,
61 unsigned int max_jump,
62 unsigned int max_drop,
65 /** representation of cost in specific direction */
69 /** default constructor */
72 /** copy constructor */
73 path_cost(const path_cost& b);
75 /** assignment operator */
76 path_cost& operator= (const path_cost& b);
78 bool valid; /**< movement is possible */
79 int value; /**< cost of movement */
80 int direction; /**< y-direction of movement */
81 bool updated; /**< this cost has ben calculated */
86 /** representation of a mapnode to be used for pathfinding */
90 /** default constructor */
93 /** copy constructor */
94 path_gridnode(const path_gridnode& b);
98 * @param b node to copy
100 path_gridnode& operator= (const path_gridnode& b);
103 * read cost in a specific direction
104 * @param dir direction of cost to fetch
106 path_cost get_cost(v3s16 dir);
109 * set cost value for movement
110 * @param dir direction to set cost for
113 void set_cost(v3s16 dir,path_cost cost);
115 bool valid; /**< node is on surface */
116 bool target; /**< node is target position */
117 bool source; /**< node is stating position */
118 int totalcost; /**< cost to move here from starting point */
119 v3s16 sourcedir; /**< origin of movement for current cost */
120 int surfaces; /**< number of surfaces with same x,z value*/
121 v3s16 pos; /**< real position of node */
122 path_cost directions[4]; /**< cost in different directions */
125 bool is_element; /**< node is element of path detected */
126 char type; /**< type of node */
129 /** class doing pathfinding */
134 * default constructor
139 * path evaluation function
140 * @param env environment to look for path
141 * @param source origin of path
142 * @param destination end position of path
143 * @param searchdistance maximum number of nodes to look in each direction
144 * @param max_jump maximum number of blocks a path may jump up
145 * @param max_drop maximum number of blocks a path may drop
146 * @param algo algorithm to use for finding a path
148 std::vector<v3s16> get_Path(ServerEnvironment* env,
151 unsigned int searchdistance,
152 unsigned int max_jump,
153 unsigned int max_drop,
157 /** data struct for storing internal information */
169 /* helper functions */
172 * transform index pos to mappos
173 * @param ipos a index position
174 * @return map position
176 v3s16 getRealPos(v3s16 ipos);
179 * transform mappos to index pos
180 * @param pos a real pos
181 * @return index position
183 v3s16 getIndexPos(v3s16 pos);
186 * get gridnode at a specific index position
187 * @param ipos index position
188 * @return gridnode for index
190 path_gridnode& getIndexElement(v3s16 ipos);
193 * invert a 3d position
194 * @param pos 3d position
197 v3s16 invert(v3s16 pos);
200 * check if a index is within current search area
201 * @param index position to validate
204 bool valid_index(v3s16 index);
207 * translate position to float position
208 * @param pos integer position
209 * @return float position
211 v3f tov3f(v3s16 pos);
214 /* algorithm functions */
217 * calculate 2d manahttan distance to target
218 * @param pos position to calc distance
219 * @return integer distance
221 int get_manhattandistance(v3s16 pos);
224 * get best direction based uppon heuristics
225 * @param directions list of unchecked directions
226 * @param g_pos mapnode to start from
227 * @return direction to check
229 v3s16 get_dir_heuristic(std::vector<v3s16>& directions,path_gridnode& g_pos);
232 * build internal data representation of search area
233 * @return true/false if costmap creation was successfull
235 bool build_costmap();
238 * calculate cost of movement
239 * @param pos real world position to start movement
240 * @param dir direction to move to
241 * @return cost information
243 path_cost calc_cost(v3s16 pos,v3s16 dir);
246 * recursive update whole search areas total cost information
247 * @param ipos position to check next
248 * @param srcdir positionc checked last time
249 * @param total_cost cost of moving to ipos
250 * @param level current recursion depth
251 * @return true/false path to destination has been found
253 bool update_all_costs(v3s16 ipos,v3s16 srcdir,int total_cost,int level);
256 * recursive try to find a patrh to destionation
257 * @param ipos position to check next
258 * @param srcdir positionc checked last time
259 * @param total_cost cost of moving to ipos
260 * @param level current recursion depth
261 * @return true/false path to destination has been found
263 bool update_cost_heuristic(v3s16 ipos,v3s16 srcdir,int current_cost,int level);
266 * recursive build a vector containing all nodes from source to destination
267 * @param path vector to add nodes to
268 * @param pos pos to check next
269 * @param level recursion depth
271 void build_path(std::vector<v3s16>& path,v3s16 pos, int level);
274 int m_max_index_x; /**< max index of search area in x direction */
275 int m_max_index_y; /**< max index of search area in y direction */
276 int m_max_index_z; /**< max index of search area in z direction */
279 int m_searchdistance; /**< max distance to search in each direction */
280 int m_maxdrop; /**< maximum number of blocks a path may drop */
281 int m_maxjump; /**< maximum number of blocks a path may jump */
282 int m_min_target_distance; /**< current smalest path to target */
284 bool m_prefetch; /**< prefetch cost data */
286 v3s16 m_start; /**< source position */
287 v3s16 m_destination; /**< destination position */
289 limits m_limits; /**< position limits in real map coordinates */
291 /** 3d grid containing all map data already collected and analyzed */
292 std::vector<std::vector<std::vector<path_gridnode> > > m_data;
294 ServerEnvironment* m_env; /**< minetest environment pointer */
296 #ifdef PATHFINDER_DEBUG
299 * print collected cost information
304 * print collected cost information in a specific direction
305 * @param dir direction to print
307 void print_cost(path_directions dir);
310 * print type of node as evaluated
315 * print pathlenght for all nodes in search area
317 void print_pathlen();
321 * @param path path to show
323 void print_path(std::vector<v3s16> path);
326 * print y direction for all movements
331 * print y direction for moving in a specific direction
332 * @param dir direction to show data
334 void print_ydir(path_directions dir);
337 * helper function to translate a direction to speaking text
338 * @param dir direction to translate
339 * @return textual name of direction
341 std::string dir_to_name(path_directions dir);
345 #endif /* PATHFINDER_H_ */