3 Copyright (C) 2013 sapier, sapier at gmx dot net
4 Copyright (C) 2016 est31, <MTest31@outlook.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 /******************************************************************************/
23 /******************************************************************************/
25 #include "pathfinder.h"
26 #include "environment.h"
31 #include "irr_aabb3d.h"
32 #include "util/basic_macros.h"
34 //#define PATHFINDER_DEBUG
35 //#define PATHFINDER_CALC_TIME
37 #ifdef PATHFINDER_DEBUG
40 #ifdef PATHFINDER_DEBUG
43 #ifdef PATHFINDER_CALC_TIME
47 /******************************************************************************/
48 /* Typedefs and macros */
49 /******************************************************************************/
51 #define LVL "(" << level << ")" <<
53 #ifdef PATHFINDER_DEBUG
54 #define DEBUG_OUT(a) std::cout << a
55 #define INFO_TARGET std::cout
56 #define VERBOSE_TARGET std::cout
57 #define ERROR_TARGET std::cout
59 #define DEBUG_OUT(a) while(0)
60 #define INFO_TARGET infostream << "Pathfinder: "
61 #define VERBOSE_TARGET verbosestream << "Pathfinder: "
62 #define ERROR_TARGET errorstream << "Pathfinder: "
65 /******************************************************************************/
66 /* Class definitions */
67 /******************************************************************************/
70 /** representation of cost in specific direction */
74 /** default constructor */
77 /** copy constructor */
78 PathCost(const PathCost &b);
80 /** assignment operator */
81 PathCost &operator= (const PathCost &b);
83 bool valid; /**< movement is possible */
84 int value; /**< cost of movement */
85 int direction; /**< y-direction of movement */
86 bool updated; /**< this cost has ben calculated */
91 /** representation of a mapnode to be used for pathfinding */
95 /** default constructor */
98 /** copy constructor */
99 PathGridnode(const PathGridnode &b);
102 * assignment operator
103 * @param b node to copy
105 PathGridnode &operator= (const PathGridnode &b);
108 * read cost in a specific direction
109 * @param dir direction of cost to fetch
111 PathCost getCost(v3s16 dir);
114 * set cost value for movement
115 * @param dir direction to set cost for
118 void setCost(v3s16 dir, PathCost cost);
120 bool valid; /**< node is on surface */
121 bool target; /**< node is target position */
122 bool source; /**< node is stating position */
123 int totalcost; /**< cost to move here from starting point */
124 v3s16 sourcedir; /**< origin of movement for current cost */
125 v3s16 pos; /**< real position of node */
126 PathCost directions[4]; /**< cost in different directions */
129 bool is_element; /**< node is element of path detected */
130 char type; /**< type of node */
135 /** Abstract class to manage the map data */
136 class GridNodeContainer {
138 virtual PathGridnode &access(v3s16 p)=0;
139 virtual ~GridNodeContainer() {}
143 void initNode(v3s16 ipos, PathGridnode *p_node);
146 class ArrayGridNodeContainer : public GridNodeContainer {
148 virtual ~ArrayGridNodeContainer() {}
149 ArrayGridNodeContainer(Pathfinder *pathf, v3s16 dimensions);
150 virtual PathGridnode &access(v3s16 p);
156 std::vector<PathGridnode> m_nodes_array;
159 class MapGridNodeContainer : public GridNodeContainer {
161 virtual ~MapGridNodeContainer() {}
162 MapGridNodeContainer(Pathfinder *pathf);
163 virtual PathGridnode &access(v3s16 p);
165 std::map<v3s16, PathGridnode> m_nodes;
168 /** class doing pathfinding */
173 * default constructor
180 * path evaluation function
181 * @param env environment to look for path
182 * @param source origin of path
183 * @param destination end position of path
184 * @param searchdistance maximum number of nodes to look in each direction
185 * @param max_jump maximum number of blocks a path may jump up
186 * @param max_drop maximum number of blocks a path may drop
187 * @param algo Algorithm to use for finding a path
189 std::vector<v3s16> getPath(ServerEnvironment *env,
192 unsigned int searchdistance,
193 unsigned int max_jump,
194 unsigned int max_drop,
198 /* helper functions */
201 * transform index pos to mappos
202 * @param ipos a index position
203 * @return map position
205 v3s16 getRealPos(v3s16 ipos);
208 * transform mappos to index pos
209 * @param pos a real pos
210 * @return index position
212 v3s16 getIndexPos(v3s16 pos);
215 * get gridnode at a specific index position
216 * @param ipos index position
217 * @return gridnode for index
219 PathGridnode &getIndexElement(v3s16 ipos);
222 * Get gridnode at a specific index position
223 * @return gridnode for index
225 PathGridnode &getIdxElem(s16 x, s16 y, s16 z);
228 * invert a 3d position
229 * @param pos 3d position
232 v3s16 invert(v3s16 pos);
235 * check if a index is within current search area
236 * @param index position to validate
239 bool isValidIndex(v3s16 index);
242 * translate position to float position
243 * @param pos integer position
244 * @return float position
246 v3f tov3f(v3s16 pos);
249 /* algorithm functions */
252 * calculate 2d manahttan distance to target on the xz plane
253 * @param pos position to calc distance
254 * @return integer distance
256 int getXZManhattanDist(v3s16 pos);
259 * get best direction based uppon heuristics
260 * @param directions list of unchecked directions
261 * @param g_pos mapnode to start from
262 * @return direction to check
264 v3s16 getDirHeuristic(std::vector<v3s16> &directions, PathGridnode &g_pos);
267 * build internal data representation of search area
268 * @return true/false if costmap creation was successfull
273 * calculate cost of movement
274 * @param pos real world position to start movement
275 * @param dir direction to move to
276 * @return cost information
278 PathCost calcCost(v3s16 pos, v3s16 dir);
281 * recursive update whole search areas total cost information
282 * @param ipos position to check next
283 * @param srcdir positionc checked last time
284 * @param total_cost cost of moving to ipos
285 * @param level current recursion depth
286 * @return true/false path to destination has been found
288 bool updateAllCosts(v3s16 ipos, v3s16 srcdir, int total_cost, int level);
291 * recursive try to find a patrh to destionation
292 * @param ipos position to check next
293 * @param srcdir positionc checked last time
294 * @param total_cost cost of moving to ipos
295 * @param level current recursion depth
296 * @return true/false path to destination has been found
298 bool updateCostHeuristic(v3s16 ipos, v3s16 srcdir, int current_cost, int level);
301 * recursive build a vector containing all nodes from source to destination
302 * @param path vector to add nodes to
303 * @param pos pos to check next
304 * @param level recursion depth
306 void buildPath(std::vector<v3s16> &path, v3s16 pos, int level);
309 int m_max_index_x; /**< max index of search area in x direction */
310 int m_max_index_y; /**< max index of search area in y direction */
311 int m_max_index_z; /**< max index of search area in z direction */
314 int m_searchdistance; /**< max distance to search in each direction */
315 int m_maxdrop; /**< maximum number of blocks a path may drop */
316 int m_maxjump; /**< maximum number of blocks a path may jump */
317 int m_min_target_distance; /**< current smalest path to target */
319 bool m_prefetch; /**< prefetch cost data */
321 v3s16 m_start; /**< source position */
322 v3s16 m_destination; /**< destination position */
324 core::aabbox3d<s16> m_limits; /**< position limits in real map coordinates */
326 /** contains all map data already collected and analyzed.
327 Access it via the getIndexElement/getIdxElem methods. */
328 friend class GridNodeContainer;
329 GridNodeContainer *m_nodes_container;
331 ServerEnvironment *m_env; /**< minetest environment pointer */
333 #ifdef PATHFINDER_DEBUG
336 * print collected cost information
341 * print collected cost information in a specific direction
342 * @param dir direction to print
344 void printCost(PathDirections dir);
347 * print type of node as evaluated
352 * print pathlenght for all nodes in search area
358 * @param path path to show
360 void printPath(std::vector<v3s16> path);
363 * print y direction for all movements
368 * print y direction for moving in a specific direction
369 * @param dir direction to show data
371 void printYdir(PathDirections dir);
374 * helper function to translate a direction to speaking text
375 * @param dir direction to translate
376 * @return textual name of direction
378 std::string dirToName(PathDirections dir);
382 /******************************************************************************/
384 /******************************************************************************/
386 std::vector<v3s16> get_path(ServerEnvironment* env,
389 unsigned int searchdistance,
390 unsigned int max_jump,
391 unsigned int max_drop,
394 Pathfinder searchclass;
396 return searchclass.getPath(env,
398 searchdistance, max_jump, max_drop, algo);
401 /******************************************************************************/
411 /******************************************************************************/
412 PathCost::PathCost(const PathCost &b)
415 direction = b.direction;
420 /******************************************************************************/
421 PathCost &PathCost::operator= (const PathCost &b)
424 direction = b.direction;
431 /******************************************************************************/
432 PathGridnode::PathGridnode()
437 sourcedir(v3s16(0, 0, 0)),
445 /******************************************************************************/
446 PathGridnode::PathGridnode(const PathGridnode &b)
450 totalcost(b.totalcost),
451 sourcedir(b.sourcedir),
453 is_element(b.is_element),
457 directions[DIR_XP] = b.directions[DIR_XP];
458 directions[DIR_XM] = b.directions[DIR_XM];
459 directions[DIR_ZP] = b.directions[DIR_ZP];
460 directions[DIR_ZM] = b.directions[DIR_ZM];
463 /******************************************************************************/
464 PathGridnode &PathGridnode::operator= (const PathGridnode &b)
469 is_element = b.is_element;
470 totalcost = b.totalcost;
471 sourcedir = b.sourcedir;
475 directions[DIR_XP] = b.directions[DIR_XP];
476 directions[DIR_XM] = b.directions[DIR_XM];
477 directions[DIR_ZP] = b.directions[DIR_ZP];
478 directions[DIR_ZM] = b.directions[DIR_ZM];
483 /******************************************************************************/
484 PathCost PathGridnode::getCost(v3s16 dir)
487 return directions[DIR_XP];
490 return directions[DIR_XM];
493 return directions[DIR_ZP];
496 return directions[DIR_ZM];
502 /******************************************************************************/
503 void PathGridnode::setCost(v3s16 dir, PathCost cost)
506 directions[DIR_XP] = cost;
509 directions[DIR_XM] = cost;
512 directions[DIR_ZP] = cost;
515 directions[DIR_ZM] = cost;
519 void GridNodeContainer::initNode(v3s16 ipos, PathGridnode *p_node)
521 INodeDefManager *ndef = m_pathf->m_env->getGameDef()->ndef();
522 PathGridnode &elem = *p_node;
524 v3s16 realpos = m_pathf->getRealPos(ipos);
526 MapNode current = m_pathf->m_env->getMap().getNodeNoEx(realpos);
527 MapNode below = m_pathf->m_env->getMap().getNodeNoEx(realpos + v3s16(0, -1, 0));
530 if ((current.param0 == CONTENT_IGNORE) ||
531 (below.param0 == CONTENT_IGNORE)) {
532 DEBUG_OUT("Pathfinder: " << PP(realpos) <<
533 " current or below is invalid element" << std::endl);
534 if (current.param0 == CONTENT_IGNORE) {
536 DEBUG_OUT(PP(ipos) << ": " << 'i' << std::endl);
541 //don't add anything if it isn't an air node
542 if (ndef->get(current).walkable || !ndef->get(below).walkable) {
543 DEBUG_OUT("Pathfinder: " << PP(realpos)
544 << " not on surface" << std::endl);
545 if (ndef->get(current).walkable) {
547 DEBUG_OUT(PP(ipos) << ": " << 's' << std::endl);
550 DEBUG_OUT(PP(ipos) << ": " << '-' << std::endl);
558 DEBUG_OUT(PP(ipos) << ": " << 'a' << std::endl);
560 if (m_pathf->m_prefetch) {
561 elem.directions[DIR_XP] = m_pathf->calcCost(realpos, v3s16( 1, 0, 0));
562 elem.directions[DIR_XM] = m_pathf->calcCost(realpos, v3s16(-1, 0, 0));
563 elem.directions[DIR_ZP] = m_pathf->calcCost(realpos, v3s16( 0, 0, 1));
564 elem.directions[DIR_ZM] = m_pathf->calcCost(realpos, v3s16( 0, 0,-1));
568 ArrayGridNodeContainer::ArrayGridNodeContainer(Pathfinder *pathf, v3s16 dimensions) :
569 m_x_stride(dimensions.Y * dimensions.Z),
570 m_y_stride(dimensions.Z)
574 m_nodes_array.resize(dimensions.X * dimensions.Y * dimensions.Z);
575 INFO_TARGET << "Pathfinder ArrayGridNodeContainer constructor." << std::endl;
576 for (int x = 0; x < dimensions.X; x++) {
577 for (int y = 0; y < dimensions.Y; y++) {
578 for (int z= 0; z < dimensions.Z; z++) {
580 initNode(ipos, &access(ipos));
586 PathGridnode &ArrayGridNodeContainer::access(v3s16 p)
588 return m_nodes_array[p.X * m_x_stride + p.Y * m_y_stride + p.Z];
591 MapGridNodeContainer::MapGridNodeContainer(Pathfinder *pathf)
596 PathGridnode &MapGridNodeContainer::access(v3s16 p)
598 std::map<v3s16, PathGridnode>::iterator it = m_nodes.find(p);
599 if (it != m_nodes.end()) {
602 PathGridnode &n = m_nodes[p];
609 /******************************************************************************/
610 std::vector<v3s16> Pathfinder::getPath(ServerEnvironment *env,
613 unsigned int searchdistance,
614 unsigned int max_jump,
615 unsigned int max_drop,
618 #ifdef PATHFINDER_CALC_TIME
620 clock_gettime(CLOCK_REALTIME, &ts);
622 std::vector<v3s16> retval;
626 ERROR_TARGET << "missing environment pointer" << std::endl;
630 m_searchdistance = searchdistance;
632 m_maxjump = max_jump;
633 m_maxdrop = max_drop;
635 m_destination = destination;
636 m_min_target_distance = -1;
639 if (algo == PA_PLAIN_NP) {
643 int min_x = MYMIN(source.X, destination.X);
644 int max_x = MYMAX(source.X, destination.X);
646 int min_y = MYMIN(source.Y, destination.Y);
647 int max_y = MYMAX(source.Y, destination.Y);
649 int min_z = MYMIN(source.Z, destination.Z);
650 int max_z = MYMAX(source.Z, destination.Z);
652 m_limits.MinEdge.X = min_x - searchdistance;
653 m_limits.MinEdge.Y = min_y - searchdistance;
654 m_limits.MinEdge.Z = min_z - searchdistance;
656 m_limits.MaxEdge.X = max_x + searchdistance;
657 m_limits.MaxEdge.Y = max_y + searchdistance;
658 m_limits.MaxEdge.Z = max_z + searchdistance;
660 v3s16 diff = m_limits.MaxEdge - m_limits.MinEdge;
662 m_max_index_x = diff.X;
663 m_max_index_y = diff.Y;
664 m_max_index_z = diff.Z;
666 delete m_nodes_container;
667 if (diff.getLength() > 5) {
668 m_nodes_container = new MapGridNodeContainer(this);
670 m_nodes_container = new ArrayGridNodeContainer(this, diff);
672 #ifdef PATHFINDER_DEBUG
678 //validate and mark start and end pos
679 v3s16 StartIndex = getIndexPos(source);
680 v3s16 EndIndex = getIndexPos(destination);
682 PathGridnode &startpos = getIndexElement(StartIndex);
683 PathGridnode &endpos = getIndexElement(EndIndex);
685 if (!startpos.valid) {
686 VERBOSE_TARGET << "invalid startpos" <<
687 "Index: " << PP(StartIndex) <<
688 "Realpos: " << PP(getRealPos(StartIndex)) << std::endl;
692 VERBOSE_TARGET << "invalid stoppos" <<
693 "Index: " << PP(EndIndex) <<
694 "Realpos: " << PP(getRealPos(EndIndex)) << std::endl;
698 endpos.target = true;
699 startpos.source = true;
700 startpos.totalcost = 0;
702 bool update_cost_retval = false;
706 update_cost_retval = updateAllCosts(StartIndex, v3s16(0, 0, 0), 0, 0);
710 update_cost_retval = updateCostHeuristic(StartIndex, v3s16(0, 0, 0), 0, 0);
713 ERROR_TARGET << "missing PathAlgorithm"<< std::endl;
717 if (update_cost_retval) {
719 #ifdef PATHFINDER_DEBUG
720 std::cout << "Path to target found!" << std::endl;
725 std::vector<v3s16> path;
726 buildPath(path, EndIndex, 0);
728 #ifdef PATHFINDER_DEBUG
729 std::cout << "Full index path:" << std::endl;
734 std::vector<v3s16> full_path;
735 for (std::vector<v3s16>::iterator i = path.begin();
736 i != path.end(); ++i) {
737 full_path.push_back(getIndexElement(*i).pos);
740 #ifdef PATHFINDER_DEBUG
741 std::cout << "full path:" << std::endl;
742 printPath(full_path);
744 #ifdef PATHFINDER_CALC_TIME
746 clock_gettime(CLOCK_REALTIME, &ts2);
748 int ms = (ts2.tv_nsec - ts.tv_nsec)/(1000*1000);
749 int us = ((ts2.tv_nsec - ts.tv_nsec) - (ms*1000*1000))/1000;
750 int ns = ((ts2.tv_nsec - ts.tv_nsec) - ( (ms*1000*1000) + (us*1000)));
753 std::cout << "Calculating path took: " << (ts2.tv_sec - ts.tv_sec) <<
754 "s " << ms << "ms " << us << "us " << ns << "ns " << std::endl;
759 #ifdef PATHFINDER_DEBUG
762 ERROR_TARGET << "failed to update cost map"<< std::endl;
770 /******************************************************************************/
771 Pathfinder::Pathfinder() :
778 m_min_target_distance(0),
781 m_destination(0, 0, 0),
782 m_nodes_container(NULL),
788 Pathfinder::~Pathfinder()
790 delete m_nodes_container;
792 /******************************************************************************/
793 v3s16 Pathfinder::getRealPos(v3s16 ipos)
795 return m_limits.MinEdge + ipos;
798 /******************************************************************************/
799 PathCost Pathfinder::calcCost(v3s16 pos, v3s16 dir)
801 INodeDefManager *ndef = m_env->getGameDef()->ndef();
804 retval.updated = true;
806 v3s16 pos2 = pos + dir;
809 if (!m_limits.isPointInside(pos2)) {
810 DEBUG_OUT("Pathfinder: " << PP(pos2) <<
811 " no cost -> out of limits" << std::endl);
815 MapNode node_at_pos2 = m_env->getMap().getNodeNoEx(pos2);
817 //did we get information about node?
818 if (node_at_pos2.param0 == CONTENT_IGNORE ) {
819 VERBOSE_TARGET << "Pathfinder: (1) area at pos: "
820 << PP(pos2) << " not loaded";
824 if (!ndef->get(node_at_pos2).walkable) {
825 MapNode node_below_pos2 =
826 m_env->getMap().getNodeNoEx(pos2 + v3s16(0, -1, 0));
828 //did we get information about node?
829 if (node_below_pos2.param0 == CONTENT_IGNORE ) {
830 VERBOSE_TARGET << "Pathfinder: (2) area at pos: "
831 << PP((pos2 + v3s16(0, -1, 0))) << " not loaded";
835 if (ndef->get(node_below_pos2).walkable) {
838 retval.direction = 0;
839 DEBUG_OUT("Pathfinder: "<< PP(pos)
840 << " cost same height found" << std::endl);
843 v3s16 testpos = pos2 - v3s16(0, -1, 0);
844 MapNode node_at_pos = m_env->getMap().getNodeNoEx(testpos);
846 while ((node_at_pos.param0 != CONTENT_IGNORE) &&
847 (!ndef->get(node_at_pos).walkable) &&
848 (testpos.Y > m_limits.MinEdge.Y)) {
849 testpos += v3s16(0, -1, 0);
850 node_at_pos = m_env->getMap().getNodeNoEx(testpos);
853 //did we find surface?
854 if ((testpos.Y >= m_limits.MinEdge.Y) &&
855 (node_at_pos.param0 != CONTENT_IGNORE) &&
856 (ndef->get(node_at_pos).walkable)) {
857 if ((pos2.Y - testpos.Y - 1) <= m_maxdrop) {
860 //difference of y-pos +1 (target node is ABOVE solid node)
861 retval.direction = ((testpos.Y - pos2.Y) +1);
862 DEBUG_OUT("Pathfinder cost below height found" << std::endl);
865 INFO_TARGET << "Pathfinder:"
866 " distance to surface below to big: "
867 << (testpos.Y - pos2.Y) << " max: " << m_maxdrop
872 DEBUG_OUT("Pathfinder: no surface below found" << std::endl);
877 v3s16 testpos = pos2;
878 MapNode node_at_pos = m_env->getMap().getNodeNoEx(testpos);
880 while ((node_at_pos.param0 != CONTENT_IGNORE) &&
881 (ndef->get(node_at_pos).walkable) &&
882 (testpos.Y < m_limits.MaxEdge.Y)) {
883 testpos += v3s16(0, 1, 0);
884 node_at_pos = m_env->getMap().getNodeNoEx(testpos);
887 //did we find surface?
888 if ((testpos.Y <= m_limits.MaxEdge.Y) &&
889 (!ndef->get(node_at_pos).walkable)) {
891 if (testpos.Y - pos2.Y <= m_maxjump) {
894 retval.direction = (testpos.Y - pos2.Y);
895 DEBUG_OUT("Pathfinder cost above found" << std::endl);
898 DEBUG_OUT("Pathfinder: distance to surface above to big: "
899 << (testpos.Y - pos2.Y) << " max: " << m_maxjump
904 DEBUG_OUT("Pathfinder: no surface above found" << std::endl);
910 /******************************************************************************/
911 v3s16 Pathfinder::getIndexPos(v3s16 pos)
913 return pos - m_limits.MinEdge;
916 /******************************************************************************/
917 PathGridnode &Pathfinder::getIndexElement(v3s16 ipos)
919 return m_nodes_container->access(ipos);
922 /******************************************************************************/
923 inline PathGridnode &Pathfinder::getIdxElem(s16 x, s16 y, s16 z)
925 return m_nodes_container->access(v3s16(x,y,z));
928 /******************************************************************************/
929 bool Pathfinder::isValidIndex(v3s16 index)
931 if ( (index.X < m_max_index_x) &&
932 (index.Y < m_max_index_y) &&
933 (index.Z < m_max_index_z) &&
942 /******************************************************************************/
943 v3s16 Pathfinder::invert(v3s16 pos)
954 /******************************************************************************/
955 bool Pathfinder::updateAllCosts(v3s16 ipos,
960 PathGridnode &g_pos = getIndexElement(ipos);
961 g_pos.totalcost = current_cost;
962 g_pos.sourcedir = srcdir;
966 //check if target has been found
968 m_min_target_distance = current_cost;
969 DEBUG_OUT(LVL " Pathfinder: target found!" << std::endl);
975 std::vector<v3s16> directions;
977 directions.push_back(v3s16( 1,0, 0));
978 directions.push_back(v3s16(-1,0, 0));
979 directions.push_back(v3s16( 0,0, 1));
980 directions.push_back(v3s16( 0,0,-1));
982 for (unsigned int i=0; i < directions.size(); i++) {
983 if (directions[i] != srcdir) {
984 PathCost cost = g_pos.getCost(directions[i]);
987 directions[i].Y = cost.direction;
989 v3s16 ipos2 = ipos + directions[i];
991 if (!isValidIndex(ipos2)) {
992 DEBUG_OUT(LVL " Pathfinder: " << PP(ipos2) <<
993 " out of range, max=" << PP(m_limits.MaxEdge) << std::endl);
997 PathGridnode &g_pos2 = getIndexElement(ipos2);
1000 VERBOSE_TARGET << LVL "Pathfinder: no data for new position: "
1001 << PP(ipos2) << std::endl;
1005 assert(cost.value > 0);
1007 int new_cost = current_cost + cost.value;
1009 // check if there already is a smaller path
1010 if ((m_min_target_distance > 0) &&
1011 (m_min_target_distance < new_cost)) {
1015 if ((g_pos2.totalcost < 0) ||
1016 (g_pos2.totalcost > new_cost)) {
1017 DEBUG_OUT(LVL "Pathfinder: updating path at: "<<
1018 PP(ipos2) << " from: " << g_pos2.totalcost << " to "<<
1019 new_cost << std::endl);
1020 if (updateAllCosts(ipos2, invert(directions[i]),
1026 DEBUG_OUT(LVL "Pathfinder:"
1027 " already found shorter path to: "
1028 << PP(ipos2) << std::endl);
1032 DEBUG_OUT(LVL "Pathfinder:"
1033 " not moving to invalid direction: "
1034 << PP(directions[i]) << std::endl);
1041 /******************************************************************************/
1042 int Pathfinder::getXZManhattanDist(v3s16 pos)
1044 int min_x = MYMIN(pos.X, m_destination.X);
1045 int max_x = MYMAX(pos.X, m_destination.X);
1046 int min_z = MYMIN(pos.Z, m_destination.Z);
1047 int max_z = MYMAX(pos.Z, m_destination.Z);
1049 return (max_x - min_x) + (max_z - min_z);
1052 /******************************************************************************/
1053 v3s16 Pathfinder::getDirHeuristic(std::vector<v3s16> &directions, PathGridnode &g_pos)
1056 v3s16 retdir = v3s16(0, 0, 0);
1057 v3s16 srcpos = g_pos.pos;
1058 DEBUG_OUT("Pathfinder: remaining dirs at beginning:"
1059 << directions.size() << std::endl);
1061 for (std::vector<v3s16>::iterator iter = directions.begin();
1062 iter != directions.end();
1065 v3s16 pos1 = v3s16(srcpos.X + iter->X, 0, srcpos.Z+iter->Z);
1067 int cur_manhattan = getXZManhattanDist(pos1);
1068 PathCost cost = g_pos.getCost(*iter);
1070 if (!cost.updated) {
1071 cost = calcCost(g_pos.pos, *iter);
1072 g_pos.setCost(*iter, cost);
1076 int score = cost.value + cur_manhattan;
1078 if ((minscore < 0)|| (score < minscore)) {
1085 if (retdir != v3s16(0, 0, 0)) {
1086 for (std::vector<v3s16>::iterator iter = directions.begin();
1087 iter != directions.end();
1089 if(*iter == retdir) {
1090 DEBUG_OUT("Pathfinder: removing return direction" << std::endl);
1091 directions.erase(iter);
1097 DEBUG_OUT("Pathfinder: didn't find any valid direction clearing"
1101 DEBUG_OUT("Pathfinder: remaining dirs at end:" << directions.size()
1106 /******************************************************************************/
1107 bool Pathfinder::updateCostHeuristic( v3s16 ipos,
1113 PathGridnode &g_pos = getIndexElement(ipos);
1114 g_pos.totalcost = current_cost;
1115 g_pos.sourcedir = srcdir;
1119 //check if target has been found
1121 m_min_target_distance = current_cost;
1122 DEBUG_OUT(LVL " Pathfinder: target found!" << std::endl);
1126 bool retval = false;
1128 std::vector<v3s16> directions;
1130 directions.push_back(v3s16( 1, 0, 0));
1131 directions.push_back(v3s16(-1, 0, 0));
1132 directions.push_back(v3s16( 0, 0, 1));
1133 directions.push_back(v3s16( 0, 0, -1));
1135 v3s16 direction = getDirHeuristic(directions, g_pos);
1137 while (direction != v3s16(0, 0, 0) && (!retval)) {
1139 if (direction != srcdir) {
1140 PathCost cost = g_pos.getCost(direction);
1143 direction.Y = cost.direction;
1145 v3s16 ipos2 = ipos + direction;
1147 if (!isValidIndex(ipos2)) {
1148 DEBUG_OUT(LVL " Pathfinder: " << PP(ipos2) <<
1149 " out of range, max=" << PP(m_limits.MaxEdge) << std::endl);
1150 direction = getDirHeuristic(directions, g_pos);
1154 PathGridnode &g_pos2 = getIndexElement(ipos2);
1156 if (!g_pos2.valid) {
1157 VERBOSE_TARGET << LVL "Pathfinder: no data for new position: "
1158 << PP(ipos2) << std::endl;
1159 direction = getDirHeuristic(directions, g_pos);
1163 assert(cost.value > 0);
1165 int new_cost = current_cost + cost.value;
1167 // check if there already is a smaller path
1168 if ((m_min_target_distance > 0) &&
1169 (m_min_target_distance < new_cost)) {
1170 DEBUG_OUT(LVL "Pathfinder:"
1171 " already longer than best already found path "
1172 << PP(ipos2) << std::endl);
1176 if ((g_pos2.totalcost < 0) ||
1177 (g_pos2.totalcost > new_cost)) {
1178 DEBUG_OUT(LVL "Pathfinder: updating path at: "<<
1179 PP(ipos2) << " from: " << g_pos2.totalcost << " to "<<
1180 new_cost << " srcdir=" <<
1181 PP(invert(direction))<< std::endl);
1182 if (updateCostHeuristic(ipos2, invert(direction),
1188 DEBUG_OUT(LVL "Pathfinder:"
1189 " already found shorter path to: "
1190 << PP(ipos2) << std::endl);
1194 DEBUG_OUT(LVL "Pathfinder:"
1195 " not moving to invalid direction: "
1196 << PP(direction) << std::endl);
1200 DEBUG_OUT(LVL "Pathfinder:"
1201 " skipping srcdir: "
1202 << PP(direction) << std::endl);
1204 direction = getDirHeuristic(directions, g_pos);
1209 /******************************************************************************/
1210 void Pathfinder::buildPath(std::vector<v3s16> &path, v3s16 pos, int level)
1215 << LVL "Pathfinder: path is too long aborting" << std::endl;
1219 PathGridnode &g_pos = getIndexElement(pos);
1222 << LVL "Pathfinder: invalid next pos detected aborting" << std::endl;
1226 g_pos.is_element = true;
1228 //check if source reached
1230 path.push_back(pos);
1234 buildPath(path, pos + g_pos.sourcedir, level);
1235 path.push_back(pos);
1238 /******************************************************************************/
1239 v3f Pathfinder::tov3f(v3s16 pos)
1241 return v3f(BS * pos.X, BS * pos.Y, BS * pos.Z);
1244 #ifdef PATHFINDER_DEBUG
1246 /******************************************************************************/
1247 void Pathfinder::printCost()
1255 /******************************************************************************/
1256 void Pathfinder::printYdir()
1264 /******************************************************************************/
1265 void Pathfinder::printCost(PathDirections dir)
1267 std::cout << "Cost in direction: " << dirToName(dir) << std::endl;
1268 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1269 std::cout << std::setfill(' ');
1270 for (int y = 0; y < m_max_index_y; y++) {
1272 std::cout << "Level: " << y << std::endl;
1274 std::cout << std::setw(4) << " " << " ";
1275 for (int x = 0; x < m_max_index_x; x++) {
1276 std::cout << std::setw(4) << x;
1278 std::cout << std::endl;
1280 for (int z = 0; z < m_max_index_z; z++) {
1281 std::cout << std::setw(4) << z <<": ";
1282 for (int x = 0; x < m_max_index_x; x++) {
1283 if (getIdxElem(x, y, z).directions[dir].valid)
1284 std::cout << std::setw(4)
1285 << getIdxElem(x, y, z).directions[dir].value;
1287 std::cout << std::setw(4) << "-";
1289 std::cout << std::endl;
1291 std::cout << std::endl;
1295 /******************************************************************************/
1296 void Pathfinder::printYdir(PathDirections dir)
1298 std::cout << "Height difference in direction: " << dirToName(dir) << std::endl;
1299 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1300 std::cout << std::setfill(' ');
1301 for (int y = 0; y < m_max_index_y; y++) {
1303 std::cout << "Level: " << y << std::endl;
1305 std::cout << std::setw(4) << " " << " ";
1306 for (int x = 0; x < m_max_index_x; x++) {
1307 std::cout << std::setw(4) << x;
1309 std::cout << std::endl;
1311 for (int z = 0; z < m_max_index_z; z++) {
1312 std::cout << std::setw(4) << z <<": ";
1313 for (int x = 0; x < m_max_index_x; x++) {
1314 if (getIdxElem(x, y, z).directions[dir].valid)
1315 std::cout << std::setw(4)
1316 << getIdxElem(x, y, z).directions[dir].direction;
1318 std::cout << std::setw(4) << "-";
1320 std::cout << std::endl;
1322 std::cout << std::endl;
1326 /******************************************************************************/
1327 void Pathfinder::printType()
1329 std::cout << "Type of node:" << std::endl;
1330 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1331 std::cout << std::setfill(' ');
1332 for (int y = 0; y < m_max_index_y; y++) {
1334 std::cout << "Level: " << y << std::endl;
1336 std::cout << std::setw(3) << " " << " ";
1337 for (int x = 0; x < m_max_index_x; x++) {
1338 std::cout << std::setw(3) << x;
1340 std::cout << std::endl;
1342 for (int z = 0; z < m_max_index_z; z++) {
1343 std::cout << std::setw(3) << z <<": ";
1344 for (int x = 0; x < m_max_index_x; x++) {
1345 char toshow = getIdxElem(x, y, z).type;
1346 std::cout << std::setw(3) << toshow;
1348 std::cout << std::endl;
1350 std::cout << std::endl;
1352 std::cout << std::endl;
1355 /******************************************************************************/
1356 void Pathfinder::printPathLen()
1358 std::cout << "Pathlen:" << std::endl;
1359 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1360 std::cout << std::setfill(' ');
1361 for (int y = 0; y < m_max_index_y; y++) {
1363 std::cout << "Level: " << y << std::endl;
1365 std::cout << std::setw(3) << " " << " ";
1366 for (int x = 0; x < m_max_index_x; x++) {
1367 std::cout << std::setw(3) << x;
1369 std::cout << std::endl;
1371 for (int z = 0; z < m_max_index_z; z++) {
1372 std::cout << std::setw(3) << z <<": ";
1373 for (int x = 0; x < m_max_index_x; x++) {
1374 std::cout << std::setw(3) << getIdxElem(x, y, z).totalcost;
1376 std::cout << std::endl;
1378 std::cout << std::endl;
1380 std::cout << std::endl;
1383 /******************************************************************************/
1384 std::string Pathfinder::dirToName(PathDirections dir)
1404 /******************************************************************************/
1405 void Pathfinder::printPath(std::vector<v3s16> path)
1407 unsigned int current = 0;
1408 for (std::vector<v3s16>::iterator i = path.begin();
1409 i != path.end(); ++i) {
1410 std::cout << std::setw(3) << current << ":" << PP((*i)) << std::endl;