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"
33 //#define PATHFINDER_DEBUG
34 //#define PATHFINDER_CALC_TIME
36 #ifdef PATHFINDER_DEBUG
39 #ifdef PATHFINDER_DEBUG
42 #ifdef PATHFINDER_CALC_TIME
46 /******************************************************************************/
47 /* Typedefs and macros */
48 /******************************************************************************/
50 /** shortcut to print a 3d pos */
51 #define PPOS(pos) "(" << pos.X << "," << pos.Y << "," << pos.Z << ")"
53 #define LVL "(" << level << ")" <<
55 #ifdef PATHFINDER_DEBUG
56 #define DEBUG_OUT(a) std::cout << a
57 #define INFO_TARGET std::cout
58 #define VERBOSE_TARGET std::cout
59 #define ERROR_TARGET std::cout
61 #define DEBUG_OUT(a) while(0)
62 #define INFO_TARGET infostream << "Pathfinder: "
63 #define VERBOSE_TARGET verbosestream << "Pathfinder: "
64 #define ERROR_TARGET errorstream << "Pathfinder: "
67 /******************************************************************************/
68 /* Class definitions */
69 /******************************************************************************/
72 /** representation of cost in specific direction */
76 /** default constructor */
79 /** copy constructor */
80 PathCost(const PathCost &b);
82 /** assignment operator */
83 PathCost &operator= (const PathCost &b);
85 bool valid; /**< movement is possible */
86 int value; /**< cost of movement */
87 int direction; /**< y-direction of movement */
88 bool updated; /**< this cost has ben calculated */
93 /** representation of a mapnode to be used for pathfinding */
97 /** default constructor */
100 /** copy constructor */
101 PathGridnode(const PathGridnode &b);
104 * assignment operator
105 * @param b node to copy
107 PathGridnode &operator= (const PathGridnode &b);
110 * read cost in a specific direction
111 * @param dir direction of cost to fetch
113 PathCost getCost(v3s16 dir);
116 * set cost value for movement
117 * @param dir direction to set cost for
120 void setCost(v3s16 dir, PathCost cost);
122 bool valid; /**< node is on surface */
123 bool target; /**< node is target position */
124 bool source; /**< node is stating position */
125 int totalcost; /**< cost to move here from starting point */
126 v3s16 sourcedir; /**< origin of movement for current cost */
127 v3s16 pos; /**< real position of node */
128 PathCost directions[4]; /**< cost in different directions */
131 bool is_element; /**< node is element of path detected */
132 char type; /**< type of node */
137 /** Abstract class to manage the map data */
138 class GridNodeContainer {
140 virtual PathGridnode &access(v3s16 p)=0;
141 virtual ~GridNodeContainer() {}
145 void initNode(v3s16 ipos, PathGridnode *p_node);
148 class ArrayGridNodeContainer : public GridNodeContainer {
150 virtual ~ArrayGridNodeContainer() {}
151 ArrayGridNodeContainer(Pathfinder *pathf, v3s16 dimensions);
152 virtual PathGridnode &access(v3s16 p);
158 std::vector<PathGridnode> m_nodes_array;
161 class MapGridNodeContainer : public GridNodeContainer {
163 virtual ~MapGridNodeContainer() {}
164 MapGridNodeContainer(Pathfinder *pathf);
165 virtual PathGridnode &access(v3s16 p);
167 std::map<v3s16, PathGridnode> m_nodes;
170 /** class doing pathfinding */
175 * default constructor
182 * path evaluation function
183 * @param env environment to look for path
184 * @param source origin of path
185 * @param destination end position of path
186 * @param searchdistance maximum number of nodes to look in each direction
187 * @param max_jump maximum number of blocks a path may jump up
188 * @param max_drop maximum number of blocks a path may drop
189 * @param algo Algorithm to use for finding a path
191 std::vector<v3s16> getPath(ServerEnvironment *env,
194 unsigned int searchdistance,
195 unsigned int max_jump,
196 unsigned int max_drop,
200 /* helper functions */
203 * transform index pos to mappos
204 * @param ipos a index position
205 * @return map position
207 v3s16 getRealPos(v3s16 ipos);
210 * transform mappos to index pos
211 * @param pos a real pos
212 * @return index position
214 v3s16 getIndexPos(v3s16 pos);
217 * get gridnode at a specific index position
218 * @param ipos index position
219 * @return gridnode for index
221 PathGridnode &getIndexElement(v3s16 ipos);
224 * Get gridnode at a specific index position
225 * @return gridnode for index
227 PathGridnode &getIdxElem(s16 x, s16 y, s16 z);
230 * invert a 3d position
231 * @param pos 3d position
234 v3s16 invert(v3s16 pos);
237 * check if a index is within current search area
238 * @param index position to validate
241 bool isValidIndex(v3s16 index);
244 * translate position to float position
245 * @param pos integer position
246 * @return float position
248 v3f tov3f(v3s16 pos);
251 /* algorithm functions */
254 * calculate 2d manahttan distance to target on the xz plane
255 * @param pos position to calc distance
256 * @return integer distance
258 int getXZManhattanDist(v3s16 pos);
261 * get best direction based uppon heuristics
262 * @param directions list of unchecked directions
263 * @param g_pos mapnode to start from
264 * @return direction to check
266 v3s16 getDirHeuristic(std::vector<v3s16> &directions, PathGridnode &g_pos);
269 * build internal data representation of search area
270 * @return true/false if costmap creation was successfull
275 * calculate cost of movement
276 * @param pos real world position to start movement
277 * @param dir direction to move to
278 * @return cost information
280 PathCost calcCost(v3s16 pos, v3s16 dir);
283 * recursive update whole search areas total cost information
284 * @param ipos position to check next
285 * @param srcdir positionc checked last time
286 * @param total_cost cost of moving to ipos
287 * @param level current recursion depth
288 * @return true/false path to destination has been found
290 bool updateAllCosts(v3s16 ipos, v3s16 srcdir, int total_cost, int level);
293 * recursive try to find a patrh to destionation
294 * @param ipos position to check next
295 * @param srcdir positionc checked last time
296 * @param total_cost cost of moving to ipos
297 * @param level current recursion depth
298 * @return true/false path to destination has been found
300 bool updateCostHeuristic(v3s16 ipos, v3s16 srcdir, int current_cost, int level);
303 * recursive build a vector containing all nodes from source to destination
304 * @param path vector to add nodes to
305 * @param pos pos to check next
306 * @param level recursion depth
308 void buildPath(std::vector<v3s16> &path, v3s16 pos, int level);
311 int m_max_index_x; /**< max index of search area in x direction */
312 int m_max_index_y; /**< max index of search area in y direction */
313 int m_max_index_z; /**< max index of search area in z direction */
316 int m_searchdistance; /**< max distance to search in each direction */
317 int m_maxdrop; /**< maximum number of blocks a path may drop */
318 int m_maxjump; /**< maximum number of blocks a path may jump */
319 int m_min_target_distance; /**< current smalest path to target */
321 bool m_prefetch; /**< prefetch cost data */
323 v3s16 m_start; /**< source position */
324 v3s16 m_destination; /**< destination position */
326 core::aabbox3d<s16> m_limits; /**< position limits in real map coordinates */
328 /** contains all map data already collected and analyzed.
329 Access it via the getIndexElement/getIdxElem methods. */
330 friend class GridNodeContainer;
331 GridNodeContainer *m_nodes_container;
333 ServerEnvironment *m_env; /**< minetest environment pointer */
335 #ifdef PATHFINDER_DEBUG
338 * print collected cost information
343 * print collected cost information in a specific direction
344 * @param dir direction to print
346 void printCost(PathDirections dir);
349 * print type of node as evaluated
354 * print pathlenght for all nodes in search area
360 * @param path path to show
362 void printPath(std::vector<v3s16> path);
365 * print y direction for all movements
370 * print y direction for moving in a specific direction
371 * @param dir direction to show data
373 void printYdir(PathDirections dir);
376 * helper function to translate a direction to speaking text
377 * @param dir direction to translate
378 * @return textual name of direction
380 std::string dirToName(PathDirections dir);
384 /******************************************************************************/
386 /******************************************************************************/
388 std::vector<v3s16> get_path(ServerEnvironment* env,
391 unsigned int searchdistance,
392 unsigned int max_jump,
393 unsigned int max_drop,
396 Pathfinder searchclass;
398 return searchclass.getPath(env,
400 searchdistance, max_jump, max_drop, algo);
403 /******************************************************************************/
413 /******************************************************************************/
414 PathCost::PathCost(const PathCost &b)
417 direction = b.direction;
422 /******************************************************************************/
423 PathCost &PathCost::operator= (const PathCost &b)
426 direction = b.direction;
433 /******************************************************************************/
434 PathGridnode::PathGridnode()
439 sourcedir(v3s16(0, 0, 0)),
447 /******************************************************************************/
448 PathGridnode::PathGridnode(const PathGridnode &b)
452 totalcost(b.totalcost),
453 sourcedir(b.sourcedir),
455 is_element(b.is_element),
459 directions[DIR_XP] = b.directions[DIR_XP];
460 directions[DIR_XM] = b.directions[DIR_XM];
461 directions[DIR_ZP] = b.directions[DIR_ZP];
462 directions[DIR_ZM] = b.directions[DIR_ZM];
465 /******************************************************************************/
466 PathGridnode &PathGridnode::operator= (const PathGridnode &b)
471 is_element = b.is_element;
472 totalcost = b.totalcost;
473 sourcedir = b.sourcedir;
477 directions[DIR_XP] = b.directions[DIR_XP];
478 directions[DIR_XM] = b.directions[DIR_XM];
479 directions[DIR_ZP] = b.directions[DIR_ZP];
480 directions[DIR_ZM] = b.directions[DIR_ZM];
485 /******************************************************************************/
486 PathCost PathGridnode::getCost(v3s16 dir)
489 return directions[DIR_XP];
492 return directions[DIR_XM];
495 return directions[DIR_ZP];
498 return directions[DIR_ZM];
504 /******************************************************************************/
505 void PathGridnode::setCost(v3s16 dir, PathCost cost)
508 directions[DIR_XP] = cost;
511 directions[DIR_XM] = cost;
514 directions[DIR_ZP] = cost;
517 directions[DIR_ZM] = cost;
521 void GridNodeContainer::initNode(v3s16 ipos, PathGridnode *p_node)
523 INodeDefManager *ndef = m_pathf->m_env->getGameDef()->ndef();
524 PathGridnode &elem = *p_node;
526 v3s16 realpos = m_pathf->getRealPos(ipos);
528 MapNode current = m_pathf->m_env->getMap().getNodeNoEx(realpos);
529 MapNode below = m_pathf->m_env->getMap().getNodeNoEx(realpos + v3s16(0, -1, 0));
532 if ((current.param0 == CONTENT_IGNORE) ||
533 (below.param0 == CONTENT_IGNORE)) {
534 DEBUG_OUT("Pathfinder: " << PPOS(realpos) <<
535 " current or below is invalid element" << std::endl);
536 if (current.param0 == CONTENT_IGNORE) {
538 DEBUG_OUT(PPOS(ipos) << ": " << 'i' << std::endl);
543 //don't add anything if it isn't an air node
544 if (ndef->get(current).walkable || !ndef->get(below).walkable) {
545 DEBUG_OUT("Pathfinder: " << PPOS(realpos)
546 << " not on surface" << std::endl);
547 if (ndef->get(current).walkable) {
549 DEBUG_OUT(PPOS(ipos) << ": " << 's' << std::endl);
552 DEBUG_OUT(PPOS(ipos) << ": " << '-' << std::endl);
560 DEBUG_OUT(PPOS(ipos) << ": " << 'a' << std::endl);
562 if (m_pathf->m_prefetch) {
563 elem.directions[DIR_XP] = m_pathf->calcCost(realpos, v3s16( 1, 0, 0));
564 elem.directions[DIR_XM] = m_pathf->calcCost(realpos, v3s16(-1, 0, 0));
565 elem.directions[DIR_ZP] = m_pathf->calcCost(realpos, v3s16( 0, 0, 1));
566 elem.directions[DIR_ZM] = m_pathf->calcCost(realpos, v3s16( 0, 0,-1));
570 ArrayGridNodeContainer::ArrayGridNodeContainer(Pathfinder *pathf, v3s16 dimensions) :
571 m_x_stride(dimensions.Y * dimensions.Z),
572 m_y_stride(dimensions.Z)
576 m_nodes_array.resize(dimensions.X * dimensions.Y * dimensions.Z);
577 INFO_TARGET << "Pathfinder ArrayGridNodeContainer constructor." << std::endl;
578 for (int x = 0; x < dimensions.X; x++) {
579 for (int y = 0; y < dimensions.Y; y++) {
580 for (int z= 0; z < dimensions.Z; z++) {
582 initNode(ipos, &access(ipos));
588 PathGridnode &ArrayGridNodeContainer::access(v3s16 p)
590 return m_nodes_array[p.X * m_x_stride + p.Y * m_y_stride + p.Z];
593 MapGridNodeContainer::MapGridNodeContainer(Pathfinder *pathf)
598 PathGridnode &MapGridNodeContainer::access(v3s16 p)
600 std::map<v3s16, PathGridnode>::iterator it = m_nodes.find(p);
601 if (it != m_nodes.end()) {
604 PathGridnode &n = m_nodes[p];
611 /******************************************************************************/
612 std::vector<v3s16> Pathfinder::getPath(ServerEnvironment *env,
615 unsigned int searchdistance,
616 unsigned int max_jump,
617 unsigned int max_drop,
620 #ifdef PATHFINDER_CALC_TIME
622 clock_gettime(CLOCK_REALTIME, &ts);
624 std::vector<v3s16> retval;
628 ERROR_TARGET << "missing environment pointer" << std::endl;
632 m_searchdistance = searchdistance;
634 m_maxjump = max_jump;
635 m_maxdrop = max_drop;
637 m_destination = destination;
638 m_min_target_distance = -1;
641 if (algo == PA_PLAIN_NP) {
645 int min_x = MYMIN(source.X, destination.X);
646 int max_x = MYMAX(source.X, destination.X);
648 int min_y = MYMIN(source.Y, destination.Y);
649 int max_y = MYMAX(source.Y, destination.Y);
651 int min_z = MYMIN(source.Z, destination.Z);
652 int max_z = MYMAX(source.Z, destination.Z);
654 m_limits.MinEdge.X = min_x - searchdistance;
655 m_limits.MinEdge.Y = min_y - searchdistance;
656 m_limits.MinEdge.Z = min_z - searchdistance;
658 m_limits.MaxEdge.X = max_x + searchdistance;
659 m_limits.MaxEdge.Y = max_y + searchdistance;
660 m_limits.MaxEdge.Z = max_z + searchdistance;
662 v3s16 diff = m_limits.MaxEdge - m_limits.MinEdge;
664 m_max_index_x = diff.X;
665 m_max_index_y = diff.Y;
666 m_max_index_z = diff.Z;
668 delete m_nodes_container;
669 if (diff.getLength() > 5) {
670 m_nodes_container = new MapGridNodeContainer(this);
672 m_nodes_container = new ArrayGridNodeContainer(this, diff);
674 #ifdef PATHFINDER_DEBUG
680 //validate and mark start and end pos
681 v3s16 StartIndex = getIndexPos(source);
682 v3s16 EndIndex = getIndexPos(destination);
684 PathGridnode &startpos = getIndexElement(StartIndex);
685 PathGridnode &endpos = getIndexElement(EndIndex);
687 if (!startpos.valid) {
688 VERBOSE_TARGET << "invalid startpos" <<
689 "Index: " << PPOS(StartIndex) <<
690 "Realpos: " << PPOS(getRealPos(StartIndex)) << std::endl;
694 VERBOSE_TARGET << "invalid stoppos" <<
695 "Index: " << PPOS(EndIndex) <<
696 "Realpos: " << PPOS(getRealPos(EndIndex)) << std::endl;
700 endpos.target = true;
701 startpos.source = true;
702 startpos.totalcost = 0;
704 bool update_cost_retval = false;
708 update_cost_retval = updateAllCosts(StartIndex, v3s16(0, 0, 0), 0, 0);
712 update_cost_retval = updateCostHeuristic(StartIndex, v3s16(0, 0, 0), 0, 0);
715 ERROR_TARGET << "missing PathAlgorithm"<< std::endl;
719 if (update_cost_retval) {
721 #ifdef PATHFINDER_DEBUG
722 std::cout << "Path to target found!" << std::endl;
727 std::vector<v3s16> path;
728 buildPath(path, EndIndex, 0);
730 #ifdef PATHFINDER_DEBUG
731 std::cout << "Full index path:" << std::endl;
736 std::vector<v3s16> full_path;
737 for (std::vector<v3s16>::iterator i = path.begin();
738 i != path.end(); ++i) {
739 full_path.push_back(getIndexElement(*i).pos);
742 #ifdef PATHFINDER_DEBUG
743 std::cout << "full path:" << std::endl;
744 printPath(full_path);
746 #ifdef PATHFINDER_CALC_TIME
748 clock_gettime(CLOCK_REALTIME, &ts2);
750 int ms = (ts2.tv_nsec - ts.tv_nsec)/(1000*1000);
751 int us = ((ts2.tv_nsec - ts.tv_nsec) - (ms*1000*1000))/1000;
752 int ns = ((ts2.tv_nsec - ts.tv_nsec) - ( (ms*1000*1000) + (us*1000)));
755 std::cout << "Calculating path took: " << (ts2.tv_sec - ts.tv_sec) <<
756 "s " << ms << "ms " << us << "us " << ns << "ns " << std::endl;
761 #ifdef PATHFINDER_DEBUG
764 ERROR_TARGET << "failed to update cost map"<< std::endl;
772 /******************************************************************************/
773 Pathfinder::Pathfinder() :
780 m_min_target_distance(0),
783 m_destination(0, 0, 0),
784 m_nodes_container(NULL),
790 Pathfinder::~Pathfinder()
792 delete m_nodes_container;
794 /******************************************************************************/
795 v3s16 Pathfinder::getRealPos(v3s16 ipos)
797 return m_limits.MinEdge + ipos;
800 /******************************************************************************/
801 PathCost Pathfinder::calcCost(v3s16 pos, v3s16 dir)
803 INodeDefManager *ndef = m_env->getGameDef()->ndef();
806 retval.updated = true;
808 v3s16 pos2 = pos + dir;
811 if (!m_limits.isPointInside(pos2)) {
812 DEBUG_OUT("Pathfinder: " << PPOS(pos2) <<
813 " no cost -> out of limits" << std::endl);
817 MapNode node_at_pos2 = m_env->getMap().getNodeNoEx(pos2);
819 //did we get information about node?
820 if (node_at_pos2.param0 == CONTENT_IGNORE ) {
821 VERBOSE_TARGET << "Pathfinder: (1) area at pos: "
822 << PPOS(pos2) << " not loaded";
826 if (!ndef->get(node_at_pos2).walkable) {
827 MapNode node_below_pos2 =
828 m_env->getMap().getNodeNoEx(pos2 + v3s16(0, -1, 0));
830 //did we get information about node?
831 if (node_below_pos2.param0 == CONTENT_IGNORE ) {
832 VERBOSE_TARGET << "Pathfinder: (2) area at pos: "
833 << PPOS((pos2 + v3s16(0, -1, 0))) << " not loaded";
837 if (ndef->get(node_below_pos2).walkable) {
840 retval.direction = 0;
841 DEBUG_OUT("Pathfinder: "<< PPOS(pos)
842 << " cost same height found" << std::endl);
845 v3s16 testpos = pos2 - v3s16(0, -1, 0);
846 MapNode node_at_pos = m_env->getMap().getNodeNoEx(testpos);
848 while ((node_at_pos.param0 != CONTENT_IGNORE) &&
849 (!ndef->get(node_at_pos).walkable) &&
850 (testpos.Y > m_limits.MinEdge.Y)) {
851 testpos += v3s16(0, -1, 0);
852 node_at_pos = m_env->getMap().getNodeNoEx(testpos);
855 //did we find surface?
856 if ((testpos.Y >= m_limits.MinEdge.Y) &&
857 (node_at_pos.param0 != CONTENT_IGNORE) &&
858 (ndef->get(node_at_pos).walkable)) {
859 if ((pos2.Y - testpos.Y - 1) <= m_maxdrop) {
862 //difference of y-pos +1 (target node is ABOVE solid node)
863 retval.direction = ((testpos.Y - pos2.Y) +1);
864 DEBUG_OUT("Pathfinder cost below height found" << std::endl);
867 INFO_TARGET << "Pathfinder:"
868 " distance to surface below to big: "
869 << (testpos.Y - pos2.Y) << " max: " << m_maxdrop
874 DEBUG_OUT("Pathfinder: no surface below found" << std::endl);
879 v3s16 testpos = pos2;
880 MapNode node_at_pos = m_env->getMap().getNodeNoEx(testpos);
882 while ((node_at_pos.param0 != CONTENT_IGNORE) &&
883 (ndef->get(node_at_pos).walkable) &&
884 (testpos.Y < m_limits.MaxEdge.Y)) {
885 testpos += v3s16(0, 1, 0);
886 node_at_pos = m_env->getMap().getNodeNoEx(testpos);
889 //did we find surface?
890 if ((testpos.Y <= m_limits.MaxEdge.Y) &&
891 (!ndef->get(node_at_pos).walkable)) {
893 if (testpos.Y - pos2.Y <= m_maxjump) {
896 retval.direction = (testpos.Y - pos2.Y);
897 DEBUG_OUT("Pathfinder cost above found" << std::endl);
900 DEBUG_OUT("Pathfinder: distance to surface above to big: "
901 << (testpos.Y - pos2.Y) << " max: " << m_maxjump
906 DEBUG_OUT("Pathfinder: no surface above found" << std::endl);
912 /******************************************************************************/
913 v3s16 Pathfinder::getIndexPos(v3s16 pos)
915 return pos - m_limits.MinEdge;
918 /******************************************************************************/
919 PathGridnode &Pathfinder::getIndexElement(v3s16 ipos)
921 return m_nodes_container->access(ipos);
924 /******************************************************************************/
925 inline PathGridnode &Pathfinder::getIdxElem(s16 x, s16 y, s16 z)
927 return m_nodes_container->access(v3s16(x,y,z));
930 /******************************************************************************/
931 bool Pathfinder::isValidIndex(v3s16 index)
933 if ( (index.X < m_max_index_x) &&
934 (index.Y < m_max_index_y) &&
935 (index.Z < m_max_index_z) &&
944 /******************************************************************************/
945 v3s16 Pathfinder::invert(v3s16 pos)
956 /******************************************************************************/
957 bool Pathfinder::updateAllCosts(v3s16 ipos,
962 PathGridnode &g_pos = getIndexElement(ipos);
963 g_pos.totalcost = current_cost;
964 g_pos.sourcedir = srcdir;
968 //check if target has been found
970 m_min_target_distance = current_cost;
971 DEBUG_OUT(LVL " Pathfinder: target found!" << std::endl);
977 std::vector<v3s16> directions;
979 directions.push_back(v3s16( 1,0, 0));
980 directions.push_back(v3s16(-1,0, 0));
981 directions.push_back(v3s16( 0,0, 1));
982 directions.push_back(v3s16( 0,0,-1));
984 for (unsigned int i=0; i < directions.size(); i++) {
985 if (directions[i] != srcdir) {
986 PathCost cost = g_pos.getCost(directions[i]);
989 directions[i].Y = cost.direction;
991 v3s16 ipos2 = ipos + directions[i];
993 if (!isValidIndex(ipos2)) {
994 DEBUG_OUT(LVL " Pathfinder: " << PPOS(ipos2) <<
995 " out of range, max=" << PPOS(m_limits.MaxEdge) << std::endl);
999 PathGridnode &g_pos2 = getIndexElement(ipos2);
1001 if (!g_pos2.valid) {
1002 VERBOSE_TARGET << LVL "Pathfinder: no data for new position: "
1003 << PPOS(ipos2) << std::endl;
1007 assert(cost.value > 0);
1009 int new_cost = current_cost + cost.value;
1011 // check if there already is a smaller path
1012 if ((m_min_target_distance > 0) &&
1013 (m_min_target_distance < new_cost)) {
1017 if ((g_pos2.totalcost < 0) ||
1018 (g_pos2.totalcost > new_cost)) {
1019 DEBUG_OUT(LVL "Pathfinder: updating path at: "<<
1020 PPOS(ipos2) << " from: " << g_pos2.totalcost << " to "<<
1021 new_cost << std::endl);
1022 if (updateAllCosts(ipos2, invert(directions[i]),
1028 DEBUG_OUT(LVL "Pathfinder:"
1029 " already found shorter path to: "
1030 << PPOS(ipos2) << std::endl);
1034 DEBUG_OUT(LVL "Pathfinder:"
1035 " not moving to invalid direction: "
1036 << PPOS(directions[i]) << std::endl);
1043 /******************************************************************************/
1044 int Pathfinder::getXZManhattanDist(v3s16 pos)
1046 int min_x = MYMIN(pos.X, m_destination.X);
1047 int max_x = MYMAX(pos.X, m_destination.X);
1048 int min_z = MYMIN(pos.Z, m_destination.Z);
1049 int max_z = MYMAX(pos.Z, m_destination.Z);
1051 return (max_x - min_x) + (max_z - min_z);
1054 /******************************************************************************/
1055 v3s16 Pathfinder::getDirHeuristic(std::vector<v3s16> &directions, PathGridnode &g_pos)
1058 v3s16 retdir = v3s16(0, 0, 0);
1059 v3s16 srcpos = g_pos.pos;
1060 DEBUG_OUT("Pathfinder: remaining dirs at beginning:"
1061 << directions.size() << std::endl);
1063 for (std::vector<v3s16>::iterator iter = directions.begin();
1064 iter != directions.end();
1067 v3s16 pos1 = v3s16(srcpos.X + iter->X, 0, srcpos.Z+iter->Z);
1069 int cur_manhattan = getXZManhattanDist(pos1);
1070 PathCost cost = g_pos.getCost(*iter);
1072 if (!cost.updated) {
1073 cost = calcCost(g_pos.pos, *iter);
1074 g_pos.setCost(*iter, cost);
1078 int score = cost.value + cur_manhattan;
1080 if ((minscore < 0)|| (score < minscore)) {
1087 if (retdir != v3s16(0, 0, 0)) {
1088 for (std::vector<v3s16>::iterator iter = directions.begin();
1089 iter != directions.end();
1091 if(*iter == retdir) {
1092 DEBUG_OUT("Pathfinder: removing return direction" << std::endl);
1093 directions.erase(iter);
1099 DEBUG_OUT("Pathfinder: didn't find any valid direction clearing"
1103 DEBUG_OUT("Pathfinder: remaining dirs at end:" << directions.size()
1108 /******************************************************************************/
1109 bool Pathfinder::updateCostHeuristic( v3s16 ipos,
1115 PathGridnode &g_pos = getIndexElement(ipos);
1116 g_pos.totalcost = current_cost;
1117 g_pos.sourcedir = srcdir;
1121 //check if target has been found
1123 m_min_target_distance = current_cost;
1124 DEBUG_OUT(LVL " Pathfinder: target found!" << std::endl);
1128 bool retval = false;
1130 std::vector<v3s16> directions;
1132 directions.push_back(v3s16( 1, 0, 0));
1133 directions.push_back(v3s16(-1, 0, 0));
1134 directions.push_back(v3s16( 0, 0, 1));
1135 directions.push_back(v3s16( 0, 0, -1));
1137 v3s16 direction = getDirHeuristic(directions, g_pos);
1139 while (direction != v3s16(0, 0, 0) && (!retval)) {
1141 if (direction != srcdir) {
1142 PathCost cost = g_pos.getCost(direction);
1145 direction.Y = cost.direction;
1147 v3s16 ipos2 = ipos + direction;
1149 if (!isValidIndex(ipos2)) {
1150 DEBUG_OUT(LVL " Pathfinder: " << PPOS(ipos2) <<
1151 " out of range, max=" << PPOS(m_limits.MaxEdge) << std::endl);
1152 direction = getDirHeuristic(directions, g_pos);
1156 PathGridnode &g_pos2 = getIndexElement(ipos2);
1158 if (!g_pos2.valid) {
1159 VERBOSE_TARGET << LVL "Pathfinder: no data for new position: "
1160 << PPOS(ipos2) << std::endl;
1161 direction = getDirHeuristic(directions, g_pos);
1165 assert(cost.value > 0);
1167 int new_cost = current_cost + cost.value;
1169 // check if there already is a smaller path
1170 if ((m_min_target_distance > 0) &&
1171 (m_min_target_distance < new_cost)) {
1172 DEBUG_OUT(LVL "Pathfinder:"
1173 " already longer than best already found path "
1174 << PPOS(ipos2) << std::endl);
1178 if ((g_pos2.totalcost < 0) ||
1179 (g_pos2.totalcost > new_cost)) {
1180 DEBUG_OUT(LVL "Pathfinder: updating path at: "<<
1181 PPOS(ipos2) << " from: " << g_pos2.totalcost << " to "<<
1182 new_cost << " srcdir=" <<
1183 PPOS(invert(direction))<< std::endl);
1184 if (updateCostHeuristic(ipos2, invert(direction),
1190 DEBUG_OUT(LVL "Pathfinder:"
1191 " already found shorter path to: "
1192 << PPOS(ipos2) << std::endl);
1196 DEBUG_OUT(LVL "Pathfinder:"
1197 " not moving to invalid direction: "
1198 << PPOS(direction) << std::endl);
1202 DEBUG_OUT(LVL "Pathfinder:"
1203 " skipping srcdir: "
1204 << PPOS(direction) << std::endl);
1206 direction = getDirHeuristic(directions, g_pos);
1211 /******************************************************************************/
1212 void Pathfinder::buildPath(std::vector<v3s16> &path, v3s16 pos, int level)
1217 << LVL "Pathfinder: path is too long aborting" << std::endl;
1221 PathGridnode &g_pos = getIndexElement(pos);
1224 << LVL "Pathfinder: invalid next pos detected aborting" << std::endl;
1228 g_pos.is_element = true;
1230 //check if source reached
1232 path.push_back(pos);
1236 buildPath(path, pos + g_pos.sourcedir, level);
1237 path.push_back(pos);
1240 /******************************************************************************/
1241 v3f Pathfinder::tov3f(v3s16 pos)
1243 return v3f(BS * pos.X, BS * pos.Y, BS * pos.Z);
1246 #ifdef PATHFINDER_DEBUG
1248 /******************************************************************************/
1249 void Pathfinder::printCost()
1257 /******************************************************************************/
1258 void Pathfinder::printYdir()
1266 /******************************************************************************/
1267 void Pathfinder::printCost(PathDirections dir)
1269 std::cout << "Cost in direction: " << dirToName(dir) << std::endl;
1270 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1271 std::cout << std::setfill(' ');
1272 for (int y = 0; y < m_max_index_y; y++) {
1274 std::cout << "Level: " << y << std::endl;
1276 std::cout << std::setw(4) << " " << " ";
1277 for (int x = 0; x < m_max_index_x; x++) {
1278 std::cout << std::setw(4) << x;
1280 std::cout << std::endl;
1282 for (int z = 0; z < m_max_index_z; z++) {
1283 std::cout << std::setw(4) << z <<": ";
1284 for (int x = 0; x < m_max_index_x; x++) {
1285 if (getIdxElem(x, y, z).directions[dir].valid)
1286 std::cout << std::setw(4)
1287 << getIdxElem(x, y, z).directions[dir].value;
1289 std::cout << std::setw(4) << "-";
1291 std::cout << std::endl;
1293 std::cout << std::endl;
1297 /******************************************************************************/
1298 void Pathfinder::printYdir(PathDirections dir)
1300 std::cout << "Height difference in direction: " << dirToName(dir) << std::endl;
1301 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1302 std::cout << std::setfill(' ');
1303 for (int y = 0; y < m_max_index_y; y++) {
1305 std::cout << "Level: " << y << std::endl;
1307 std::cout << std::setw(4) << " " << " ";
1308 for (int x = 0; x < m_max_index_x; x++) {
1309 std::cout << std::setw(4) << x;
1311 std::cout << std::endl;
1313 for (int z = 0; z < m_max_index_z; z++) {
1314 std::cout << std::setw(4) << z <<": ";
1315 for (int x = 0; x < m_max_index_x; x++) {
1316 if (getIdxElem(x, y, z).directions[dir].valid)
1317 std::cout << std::setw(4)
1318 << getIdxElem(x, y, z).directions[dir].direction;
1320 std::cout << std::setw(4) << "-";
1322 std::cout << std::endl;
1324 std::cout << std::endl;
1328 /******************************************************************************/
1329 void Pathfinder::printType()
1331 std::cout << "Type of node:" << std::endl;
1332 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1333 std::cout << std::setfill(' ');
1334 for (int y = 0; y < m_max_index_y; y++) {
1336 std::cout << "Level: " << y << std::endl;
1338 std::cout << std::setw(3) << " " << " ";
1339 for (int x = 0; x < m_max_index_x; x++) {
1340 std::cout << std::setw(3) << x;
1342 std::cout << std::endl;
1344 for (int z = 0; z < m_max_index_z; z++) {
1345 std::cout << std::setw(3) << z <<": ";
1346 for (int x = 0; x < m_max_index_x; x++) {
1347 char toshow = getIdxElem(x, y, z).type;
1348 std::cout << std::setw(3) << toshow;
1350 std::cout << std::endl;
1352 std::cout << std::endl;
1354 std::cout << std::endl;
1357 /******************************************************************************/
1358 void Pathfinder::printPathLen()
1360 std::cout << "Pathlen:" << std::endl;
1361 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1362 std::cout << std::setfill(' ');
1363 for (int y = 0; y < m_max_index_y; y++) {
1365 std::cout << "Level: " << y << std::endl;
1367 std::cout << std::setw(3) << " " << " ";
1368 for (int x = 0; x < m_max_index_x; x++) {
1369 std::cout << std::setw(3) << x;
1371 std::cout << std::endl;
1373 for (int z = 0; z < m_max_index_z; z++) {
1374 std::cout << std::setw(3) << z <<": ";
1375 for (int x = 0; x < m_max_index_x; x++) {
1376 std::cout << std::setw(3) << getIdxElem(x, y, z).totalcost;
1378 std::cout << std::endl;
1380 std::cout << std::endl;
1382 std::cout << std::endl;
1385 /******************************************************************************/
1386 std::string Pathfinder::dirToName(PathDirections dir)
1406 /******************************************************************************/
1407 void Pathfinder::printPath(std::vector<v3s16> path)
1409 unsigned int current = 0;
1410 for (std::vector<v3s16>::iterator i = path.begin();
1411 i != path.end(); ++i) {
1412 std::cout << std::setw(3) << current << ":" << PPOS((*i)) << std::endl;