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 "serverenvironment.h"
30 //#define PATHFINDER_DEBUG
31 //#define PATHFINDER_CALC_TIME
33 #ifdef PATHFINDER_DEBUG
36 #ifdef PATHFINDER_DEBUG
39 #ifdef PATHFINDER_CALC_TIME
43 /******************************************************************************/
44 /* Typedefs and macros */
45 /******************************************************************************/
47 #define LVL "(" << level << ")" <<
49 #ifdef PATHFINDER_DEBUG
50 #define DEBUG_OUT(a) std::cout << a
51 #define INFO_TARGET std::cout
52 #define VERBOSE_TARGET std::cout
53 #define ERROR_TARGET std::cout
55 #define DEBUG_OUT(a) while(0)
56 #define INFO_TARGET infostream << "Pathfinder: "
57 #define VERBOSE_TARGET verbosestream << "Pathfinder: "
58 #define ERROR_TARGET warningstream << "Pathfinder: "
61 /******************************************************************************/
62 /* Class definitions */
63 /******************************************************************************/
66 /** representation of cost in specific direction */
70 /** default constructor */
73 /** copy constructor */
74 PathCost(const PathCost &b);
76 /** assignment operator */
77 PathCost &operator= (const PathCost &b);
79 bool valid; /**< movement is possible */
80 int value; /**< cost of movement */
81 int direction; /**< y-direction of movement */
82 bool updated; /**< this cost has ben calculated */
87 /** representation of a mapnode to be used for pathfinding */
91 /** default constructor */
94 /** copy constructor */
95 PathGridnode(const PathGridnode &b);
99 * @param b node to copy
101 PathGridnode &operator= (const PathGridnode &b);
104 * read cost in a specific direction
105 * @param dir direction of cost to fetch
107 PathCost getCost(v3s16 dir);
110 * set cost value for movement
111 * @param dir direction to set cost for
114 void setCost(v3s16 dir, const PathCost &cost);
116 bool valid; /**< node is on surface */
117 bool target; /**< node is target position */
118 bool source; /**< node is stating position */
119 int totalcost; /**< cost to move here from starting point */
120 v3s16 sourcedir; /**< origin of movement for current cost */
121 v3s16 pos; /**< real position of node */
122 PathCost directions[4]; /**< cost in different directions */
125 bool is_element; /**< node is element of path detected */
126 char type; /**< type of node */
131 /** Abstract class to manage the map data */
132 class GridNodeContainer {
134 virtual PathGridnode &access(v3s16 p)=0;
135 virtual ~GridNodeContainer() {}
139 void initNode(v3s16 ipos, PathGridnode *p_node);
142 class ArrayGridNodeContainer : public GridNodeContainer {
144 virtual ~ArrayGridNodeContainer() {}
145 ArrayGridNodeContainer(Pathfinder *pathf, v3s16 dimensions);
146 virtual PathGridnode &access(v3s16 p);
152 std::vector<PathGridnode> m_nodes_array;
155 class MapGridNodeContainer : public GridNodeContainer {
157 virtual ~MapGridNodeContainer() {}
158 MapGridNodeContainer(Pathfinder *pathf);
159 virtual PathGridnode &access(v3s16 p);
161 std::map<v3s16, PathGridnode> m_nodes;
164 /** class doing pathfinding */
169 * default constructor
176 * path evaluation function
177 * @param env environment to look for path
178 * @param source origin of path
179 * @param destination end position of path
180 * @param searchdistance maximum number of nodes to look in each direction
181 * @param max_jump maximum number of blocks a path may jump up
182 * @param max_drop maximum number of blocks a path may drop
183 * @param algo Algorithm to use for finding a path
185 std::vector<v3s16> getPath(ServerEnvironment *env,
188 unsigned int searchdistance,
189 unsigned int max_jump,
190 unsigned int max_drop,
194 /* helper functions */
197 * transform index pos to mappos
198 * @param ipos a index position
199 * @return map position
201 v3s16 getRealPos(v3s16 ipos);
204 * transform mappos to index pos
205 * @param pos a real pos
206 * @return index position
208 v3s16 getIndexPos(v3s16 pos);
211 * get gridnode at a specific index position
212 * @param ipos index position
213 * @return gridnode for index
215 PathGridnode &getIndexElement(v3s16 ipos);
218 * Get gridnode at a specific index position
219 * @return gridnode for index
221 PathGridnode &getIdxElem(s16 x, s16 y, s16 z);
224 * invert a 3d position
225 * @param pos 3d position
228 v3s16 invert(v3s16 pos);
231 * check if a index is within current search area
232 * @param index position to validate
235 bool isValidIndex(v3s16 index);
238 * translate position to float position
239 * @param pos integer position
240 * @return float position
242 v3f tov3f(v3s16 pos);
245 /* algorithm functions */
248 * calculate 2d manahttan distance to target on the xz plane
249 * @param pos position to calc distance
250 * @return integer distance
252 int getXZManhattanDist(v3s16 pos);
255 * get best direction based uppon heuristics
256 * @param directions list of unchecked directions
257 * @param g_pos mapnode to start from
258 * @return direction to check
260 v3s16 getDirHeuristic(std::vector<v3s16> &directions, PathGridnode &g_pos);
263 * build internal data representation of search area
264 * @return true/false if costmap creation was successfull
269 * calculate cost of movement
270 * @param pos real world position to start movement
271 * @param dir direction to move to
272 * @return cost information
274 PathCost calcCost(v3s16 pos, v3s16 dir);
277 * recursive update whole search areas total cost information
278 * @param ipos position to check next
279 * @param srcdir positionc checked last time
280 * @param total_cost cost of moving to ipos
281 * @param level current recursion depth
282 * @return true/false path to destination has been found
284 bool updateAllCosts(v3s16 ipos, v3s16 srcdir, int total_cost, int level);
287 * recursive try to find a patrh to destionation
288 * @param ipos position to check next
289 * @param srcdir positionc checked last time
290 * @param total_cost cost of moving to ipos
291 * @param level current recursion depth
292 * @return true/false path to destination has been found
294 bool updateCostHeuristic(v3s16 ipos, v3s16 srcdir, int current_cost, int level);
297 * recursive build a vector containing all nodes from source to destination
298 * @param path vector to add nodes to
299 * @param pos pos to check next
300 * @param level recursion depth
302 void buildPath(std::vector<v3s16> &path, v3s16 pos, int level);
305 int m_max_index_x; /**< max index of search area in x direction */
306 int m_max_index_y; /**< max index of search area in y direction */
307 int m_max_index_z; /**< max index of search area in z direction */
310 int m_searchdistance; /**< max distance to search in each direction */
311 int m_maxdrop; /**< maximum number of blocks a path may drop */
312 int m_maxjump; /**< maximum number of blocks a path may jump */
313 int m_min_target_distance; /**< current smalest path to target */
315 bool m_prefetch; /**< prefetch cost data */
317 v3s16 m_start; /**< source position */
318 v3s16 m_destination; /**< destination position */
320 core::aabbox3d<s16> m_limits; /**< position limits in real map coordinates */
322 /** contains all map data already collected and analyzed.
323 Access it via the getIndexElement/getIdxElem methods. */
324 friend class GridNodeContainer;
325 GridNodeContainer *m_nodes_container;
327 ServerEnvironment *m_env; /**< minetest environment pointer */
329 #ifdef PATHFINDER_DEBUG
332 * print collected cost information
337 * print collected cost information in a specific direction
338 * @param dir direction to print
340 void printCost(PathDirections dir);
343 * print type of node as evaluated
348 * print pathlenght for all nodes in search area
354 * @param path path to show
356 void printPath(std::vector<v3s16> path);
359 * print y direction for all movements
364 * print y direction for moving in a specific direction
365 * @param dir direction to show data
367 void printYdir(PathDirections dir);
370 * helper function to translate a direction to speaking text
371 * @param dir direction to translate
372 * @return textual name of direction
374 std::string dirToName(PathDirections dir);
378 /******************************************************************************/
380 /******************************************************************************/
382 std::vector<v3s16> get_path(ServerEnvironment* env,
385 unsigned int searchdistance,
386 unsigned int max_jump,
387 unsigned int max_drop,
390 Pathfinder searchclass;
392 return searchclass.getPath(env,
394 searchdistance, max_jump, max_drop, algo);
397 /******************************************************************************/
407 /******************************************************************************/
408 PathCost::PathCost(const PathCost &b)
411 direction = b.direction;
416 /******************************************************************************/
417 PathCost &PathCost::operator= (const PathCost &b)
420 direction = b.direction;
427 /******************************************************************************/
428 PathGridnode::PathGridnode()
433 sourcedir(v3s16(0, 0, 0)),
441 /******************************************************************************/
442 PathGridnode::PathGridnode(const PathGridnode &b)
446 totalcost(b.totalcost),
447 sourcedir(b.sourcedir),
449 is_element(b.is_element),
453 directions[DIR_XP] = b.directions[DIR_XP];
454 directions[DIR_XM] = b.directions[DIR_XM];
455 directions[DIR_ZP] = b.directions[DIR_ZP];
456 directions[DIR_ZM] = b.directions[DIR_ZM];
459 /******************************************************************************/
460 PathGridnode &PathGridnode::operator= (const PathGridnode &b)
465 is_element = b.is_element;
466 totalcost = b.totalcost;
467 sourcedir = b.sourcedir;
471 directions[DIR_XP] = b.directions[DIR_XP];
472 directions[DIR_XM] = b.directions[DIR_XM];
473 directions[DIR_ZP] = b.directions[DIR_ZP];
474 directions[DIR_ZM] = b.directions[DIR_ZM];
479 /******************************************************************************/
480 PathCost PathGridnode::getCost(v3s16 dir)
483 return directions[DIR_XP];
486 return directions[DIR_XM];
489 return directions[DIR_ZP];
492 return directions[DIR_ZM];
498 /******************************************************************************/
499 void PathGridnode::setCost(v3s16 dir, const PathCost &cost)
502 directions[DIR_XP] = cost;
505 directions[DIR_XM] = cost;
508 directions[DIR_ZP] = cost;
511 directions[DIR_ZM] = cost;
515 void GridNodeContainer::initNode(v3s16 ipos, PathGridnode *p_node)
517 INodeDefManager *ndef = m_pathf->m_env->getGameDef()->ndef();
518 PathGridnode &elem = *p_node;
520 v3s16 realpos = m_pathf->getRealPos(ipos);
522 MapNode current = m_pathf->m_env->getMap().getNodeNoEx(realpos);
523 MapNode below = m_pathf->m_env->getMap().getNodeNoEx(realpos + v3s16(0, -1, 0));
526 if ((current.param0 == CONTENT_IGNORE) ||
527 (below.param0 == CONTENT_IGNORE)) {
528 DEBUG_OUT("Pathfinder: " << PP(realpos) <<
529 " current or below is invalid element" << std::endl);
530 if (current.param0 == CONTENT_IGNORE) {
532 DEBUG_OUT(PP(ipos) << ": " << 'i' << std::endl);
537 //don't add anything if it isn't an air node
538 if (ndef->get(current).walkable || !ndef->get(below).walkable) {
539 DEBUG_OUT("Pathfinder: " << PP(realpos)
540 << " not on surface" << std::endl);
541 if (ndef->get(current).walkable) {
543 DEBUG_OUT(PP(ipos) << ": " << 's' << std::endl);
546 DEBUG_OUT(PP(ipos) << ": " << '-' << std::endl);
554 DEBUG_OUT(PP(ipos) << ": " << 'a' << std::endl);
556 if (m_pathf->m_prefetch) {
557 elem.directions[DIR_XP] = m_pathf->calcCost(realpos, v3s16( 1, 0, 0));
558 elem.directions[DIR_XM] = m_pathf->calcCost(realpos, v3s16(-1, 0, 0));
559 elem.directions[DIR_ZP] = m_pathf->calcCost(realpos, v3s16( 0, 0, 1));
560 elem.directions[DIR_ZM] = m_pathf->calcCost(realpos, v3s16( 0, 0,-1));
564 ArrayGridNodeContainer::ArrayGridNodeContainer(Pathfinder *pathf, v3s16 dimensions) :
565 m_x_stride(dimensions.Y * dimensions.Z),
566 m_y_stride(dimensions.Z)
570 m_nodes_array.resize(dimensions.X * dimensions.Y * dimensions.Z);
571 INFO_TARGET << "Pathfinder ArrayGridNodeContainer constructor." << std::endl;
572 for (int x = 0; x < dimensions.X; x++) {
573 for (int y = 0; y < dimensions.Y; y++) {
574 for (int z= 0; z < dimensions.Z; z++) {
576 initNode(ipos, &access(ipos));
582 PathGridnode &ArrayGridNodeContainer::access(v3s16 p)
584 return m_nodes_array[p.X * m_x_stride + p.Y * m_y_stride + p.Z];
587 MapGridNodeContainer::MapGridNodeContainer(Pathfinder *pathf)
592 PathGridnode &MapGridNodeContainer::access(v3s16 p)
594 std::map<v3s16, PathGridnode>::iterator it = m_nodes.find(p);
595 if (it != m_nodes.end()) {
598 PathGridnode &n = m_nodes[p];
605 /******************************************************************************/
606 std::vector<v3s16> Pathfinder::getPath(ServerEnvironment *env,
609 unsigned int searchdistance,
610 unsigned int max_jump,
611 unsigned int max_drop,
614 #ifdef PATHFINDER_CALC_TIME
616 clock_gettime(CLOCK_REALTIME, &ts);
618 std::vector<v3s16> retval;
622 ERROR_TARGET << "missing environment pointer" << std::endl;
626 m_searchdistance = searchdistance;
628 m_maxjump = max_jump;
629 m_maxdrop = max_drop;
631 m_destination = destination;
632 m_min_target_distance = -1;
635 if (algo == PA_PLAIN_NP) {
639 int min_x = MYMIN(source.X, destination.X);
640 int max_x = MYMAX(source.X, destination.X);
642 int min_y = MYMIN(source.Y, destination.Y);
643 int max_y = MYMAX(source.Y, destination.Y);
645 int min_z = MYMIN(source.Z, destination.Z);
646 int max_z = MYMAX(source.Z, destination.Z);
648 m_limits.MinEdge.X = min_x - searchdistance;
649 m_limits.MinEdge.Y = min_y - searchdistance;
650 m_limits.MinEdge.Z = min_z - searchdistance;
652 m_limits.MaxEdge.X = max_x + searchdistance;
653 m_limits.MaxEdge.Y = max_y + searchdistance;
654 m_limits.MaxEdge.Z = max_z + searchdistance;
656 v3s16 diff = m_limits.MaxEdge - m_limits.MinEdge;
658 m_max_index_x = diff.X;
659 m_max_index_y = diff.Y;
660 m_max_index_z = diff.Z;
662 delete m_nodes_container;
663 if (diff.getLength() > 5) {
664 m_nodes_container = new MapGridNodeContainer(this);
666 m_nodes_container = new ArrayGridNodeContainer(this, diff);
668 #ifdef PATHFINDER_DEBUG
674 //validate and mark start and end pos
675 v3s16 StartIndex = getIndexPos(source);
676 v3s16 EndIndex = getIndexPos(destination);
678 PathGridnode &startpos = getIndexElement(StartIndex);
679 PathGridnode &endpos = getIndexElement(EndIndex);
681 if (!startpos.valid) {
682 VERBOSE_TARGET << "invalid startpos" <<
683 "Index: " << PP(StartIndex) <<
684 "Realpos: " << PP(getRealPos(StartIndex)) << std::endl;
688 VERBOSE_TARGET << "invalid stoppos" <<
689 "Index: " << PP(EndIndex) <<
690 "Realpos: " << PP(getRealPos(EndIndex)) << std::endl;
694 endpos.target = true;
695 startpos.source = true;
696 startpos.totalcost = 0;
698 bool update_cost_retval = false;
702 update_cost_retval = updateAllCosts(StartIndex, v3s16(0, 0, 0), 0, 0);
706 update_cost_retval = updateCostHeuristic(StartIndex, v3s16(0, 0, 0), 0, 0);
709 ERROR_TARGET << "missing PathAlgorithm"<< std::endl;
713 if (update_cost_retval) {
715 #ifdef PATHFINDER_DEBUG
716 std::cout << "Path to target found!" << std::endl;
721 std::vector<v3s16> path;
722 buildPath(path, EndIndex, 0);
724 #ifdef PATHFINDER_DEBUG
725 std::cout << "Full index path:" << std::endl;
730 std::vector<v3s16> full_path;
731 for (std::vector<v3s16>::iterator i = path.begin();
732 i != path.end(); ++i) {
733 full_path.push_back(getIndexElement(*i).pos);
736 #ifdef PATHFINDER_DEBUG
737 std::cout << "full path:" << std::endl;
738 printPath(full_path);
740 #ifdef PATHFINDER_CALC_TIME
742 clock_gettime(CLOCK_REALTIME, &ts2);
744 int ms = (ts2.tv_nsec - ts.tv_nsec)/(1000*1000);
745 int us = ((ts2.tv_nsec - ts.tv_nsec) - (ms*1000*1000))/1000;
746 int ns = ((ts2.tv_nsec - ts.tv_nsec) - ( (ms*1000*1000) + (us*1000)));
749 std::cout << "Calculating path took: " << (ts2.tv_sec - ts.tv_sec) <<
750 "s " << ms << "ms " << us << "us " << ns << "ns " << std::endl;
755 #ifdef PATHFINDER_DEBUG
758 ERROR_TARGET << "failed to update cost map"<< std::endl;
766 /******************************************************************************/
767 Pathfinder::Pathfinder() :
774 m_min_target_distance(0),
777 m_destination(0, 0, 0),
778 m_nodes_container(NULL),
784 Pathfinder::~Pathfinder()
786 delete m_nodes_container;
788 /******************************************************************************/
789 v3s16 Pathfinder::getRealPos(v3s16 ipos)
791 return m_limits.MinEdge + ipos;
794 /******************************************************************************/
795 PathCost Pathfinder::calcCost(v3s16 pos, v3s16 dir)
797 INodeDefManager *ndef = m_env->getGameDef()->ndef();
800 retval.updated = true;
802 v3s16 pos2 = pos + dir;
805 if (!m_limits.isPointInside(pos2)) {
806 DEBUG_OUT("Pathfinder: " << PP(pos2) <<
807 " no cost -> out of limits" << std::endl);
811 MapNode node_at_pos2 = m_env->getMap().getNodeNoEx(pos2);
813 //did we get information about node?
814 if (node_at_pos2.param0 == CONTENT_IGNORE ) {
815 VERBOSE_TARGET << "Pathfinder: (1) area at pos: "
816 << PP(pos2) << " not loaded";
820 if (!ndef->get(node_at_pos2).walkable) {
821 MapNode node_below_pos2 =
822 m_env->getMap().getNodeNoEx(pos2 + v3s16(0, -1, 0));
824 //did we get information about node?
825 if (node_below_pos2.param0 == CONTENT_IGNORE ) {
826 VERBOSE_TARGET << "Pathfinder: (2) area at pos: "
827 << PP((pos2 + v3s16(0, -1, 0))) << " not loaded";
831 if (ndef->get(node_below_pos2).walkable) {
834 retval.direction = 0;
835 DEBUG_OUT("Pathfinder: "<< PP(pos)
836 << " cost same height found" << std::endl);
839 v3s16 testpos = pos2 - v3s16(0, -1, 0);
840 MapNode node_at_pos = m_env->getMap().getNodeNoEx(testpos);
842 while ((node_at_pos.param0 != CONTENT_IGNORE) &&
843 (!ndef->get(node_at_pos).walkable) &&
844 (testpos.Y > m_limits.MinEdge.Y)) {
845 testpos += v3s16(0, -1, 0);
846 node_at_pos = m_env->getMap().getNodeNoEx(testpos);
849 //did we find surface?
850 if ((testpos.Y >= m_limits.MinEdge.Y) &&
851 (node_at_pos.param0 != CONTENT_IGNORE) &&
852 (ndef->get(node_at_pos).walkable)) {
853 if ((pos2.Y - testpos.Y - 1) <= m_maxdrop) {
856 //difference of y-pos +1 (target node is ABOVE solid node)
857 retval.direction = ((testpos.Y - pos2.Y) +1);
858 DEBUG_OUT("Pathfinder cost below height found" << std::endl);
861 INFO_TARGET << "Pathfinder:"
862 " distance to surface below to big: "
863 << (testpos.Y - pos2.Y) << " max: " << m_maxdrop
868 DEBUG_OUT("Pathfinder: no surface below found" << std::endl);
873 v3s16 testpos = pos2;
874 MapNode node_at_pos = m_env->getMap().getNodeNoEx(testpos);
876 while ((node_at_pos.param0 != CONTENT_IGNORE) &&
877 (ndef->get(node_at_pos).walkable) &&
878 (testpos.Y < m_limits.MaxEdge.Y)) {
879 testpos += v3s16(0, 1, 0);
880 node_at_pos = m_env->getMap().getNodeNoEx(testpos);
883 //did we find surface?
884 if ((testpos.Y <= m_limits.MaxEdge.Y) &&
885 (!ndef->get(node_at_pos).walkable)) {
887 if (testpos.Y - pos2.Y <= m_maxjump) {
890 retval.direction = (testpos.Y - pos2.Y);
891 DEBUG_OUT("Pathfinder cost above found" << std::endl);
894 DEBUG_OUT("Pathfinder: distance to surface above to big: "
895 << (testpos.Y - pos2.Y) << " max: " << m_maxjump
900 DEBUG_OUT("Pathfinder: no surface above found" << std::endl);
906 /******************************************************************************/
907 v3s16 Pathfinder::getIndexPos(v3s16 pos)
909 return pos - m_limits.MinEdge;
912 /******************************************************************************/
913 PathGridnode &Pathfinder::getIndexElement(v3s16 ipos)
915 return m_nodes_container->access(ipos);
918 /******************************************************************************/
919 inline PathGridnode &Pathfinder::getIdxElem(s16 x, s16 y, s16 z)
921 return m_nodes_container->access(v3s16(x,y,z));
924 /******************************************************************************/
925 bool Pathfinder::isValidIndex(v3s16 index)
927 if ( (index.X < m_max_index_x) &&
928 (index.Y < m_max_index_y) &&
929 (index.Z < m_max_index_z) &&
938 /******************************************************************************/
939 v3s16 Pathfinder::invert(v3s16 pos)
950 /******************************************************************************/
951 bool Pathfinder::updateAllCosts(v3s16 ipos,
956 PathGridnode &g_pos = getIndexElement(ipos);
957 g_pos.totalcost = current_cost;
958 g_pos.sourcedir = srcdir;
962 //check if target has been found
964 m_min_target_distance = current_cost;
965 DEBUG_OUT(LVL " Pathfinder: target found!" << std::endl);
971 std::vector<v3s16> directions;
973 directions.push_back(v3s16( 1,0, 0));
974 directions.push_back(v3s16(-1,0, 0));
975 directions.push_back(v3s16( 0,0, 1));
976 directions.push_back(v3s16( 0,0,-1));
978 for (unsigned int i=0; i < directions.size(); i++) {
979 if (directions[i] != srcdir) {
980 PathCost cost = g_pos.getCost(directions[i]);
983 directions[i].Y = cost.direction;
985 v3s16 ipos2 = ipos + directions[i];
987 if (!isValidIndex(ipos2)) {
988 DEBUG_OUT(LVL " Pathfinder: " << PP(ipos2) <<
989 " out of range, max=" << PP(m_limits.MaxEdge) << std::endl);
993 PathGridnode &g_pos2 = getIndexElement(ipos2);
996 VERBOSE_TARGET << LVL "Pathfinder: no data for new position: "
997 << PP(ipos2) << std::endl;
1001 assert(cost.value > 0);
1003 int new_cost = current_cost + cost.value;
1005 // check if there already is a smaller path
1006 if ((m_min_target_distance > 0) &&
1007 (m_min_target_distance < new_cost)) {
1011 if ((g_pos2.totalcost < 0) ||
1012 (g_pos2.totalcost > new_cost)) {
1013 DEBUG_OUT(LVL "Pathfinder: updating path at: "<<
1014 PP(ipos2) << " from: " << g_pos2.totalcost << " to "<<
1015 new_cost << std::endl);
1016 if (updateAllCosts(ipos2, invert(directions[i]),
1022 DEBUG_OUT(LVL "Pathfinder:"
1023 " already found shorter path to: "
1024 << PP(ipos2) << std::endl);
1028 DEBUG_OUT(LVL "Pathfinder:"
1029 " not moving to invalid direction: "
1030 << PP(directions[i]) << std::endl);
1037 /******************************************************************************/
1038 int Pathfinder::getXZManhattanDist(v3s16 pos)
1040 int min_x = MYMIN(pos.X, m_destination.X);
1041 int max_x = MYMAX(pos.X, m_destination.X);
1042 int min_z = MYMIN(pos.Z, m_destination.Z);
1043 int max_z = MYMAX(pos.Z, m_destination.Z);
1045 return (max_x - min_x) + (max_z - min_z);
1048 /******************************************************************************/
1049 v3s16 Pathfinder::getDirHeuristic(std::vector<v3s16> &directions, PathGridnode &g_pos)
1052 v3s16 retdir = v3s16(0, 0, 0);
1053 v3s16 srcpos = g_pos.pos;
1054 DEBUG_OUT("Pathfinder: remaining dirs at beginning:"
1055 << directions.size() << std::endl);
1057 for (std::vector<v3s16>::iterator iter = directions.begin();
1058 iter != directions.end();
1061 v3s16 pos1 = v3s16(srcpos.X + iter->X, 0, srcpos.Z+iter->Z);
1063 int cur_manhattan = getXZManhattanDist(pos1);
1064 PathCost cost = g_pos.getCost(*iter);
1066 if (!cost.updated) {
1067 cost = calcCost(g_pos.pos, *iter);
1068 g_pos.setCost(*iter, cost);
1072 int score = cost.value + cur_manhattan;
1074 if ((minscore < 0)|| (score < minscore)) {
1081 if (retdir != v3s16(0, 0, 0)) {
1082 for (std::vector<v3s16>::iterator iter = directions.begin();
1083 iter != directions.end();
1085 if(*iter == retdir) {
1086 DEBUG_OUT("Pathfinder: removing return direction" << std::endl);
1087 directions.erase(iter);
1093 DEBUG_OUT("Pathfinder: didn't find any valid direction clearing"
1097 DEBUG_OUT("Pathfinder: remaining dirs at end:" << directions.size()
1102 /******************************************************************************/
1103 bool Pathfinder::updateCostHeuristic( v3s16 ipos,
1109 PathGridnode &g_pos = getIndexElement(ipos);
1110 g_pos.totalcost = current_cost;
1111 g_pos.sourcedir = srcdir;
1115 //check if target has been found
1117 m_min_target_distance = current_cost;
1118 DEBUG_OUT(LVL " Pathfinder: target found!" << std::endl);
1122 bool retval = false;
1124 std::vector<v3s16> directions;
1126 directions.push_back(v3s16( 1, 0, 0));
1127 directions.push_back(v3s16(-1, 0, 0));
1128 directions.push_back(v3s16( 0, 0, 1));
1129 directions.push_back(v3s16( 0, 0, -1));
1131 v3s16 direction = getDirHeuristic(directions, g_pos);
1133 while (direction != v3s16(0, 0, 0) && (!retval)) {
1135 if (direction != srcdir) {
1136 PathCost cost = g_pos.getCost(direction);
1139 direction.Y = cost.direction;
1141 v3s16 ipos2 = ipos + direction;
1143 if (!isValidIndex(ipos2)) {
1144 DEBUG_OUT(LVL " Pathfinder: " << PP(ipos2) <<
1145 " out of range, max=" << PP(m_limits.MaxEdge) << std::endl);
1146 direction = getDirHeuristic(directions, g_pos);
1150 PathGridnode &g_pos2 = getIndexElement(ipos2);
1152 if (!g_pos2.valid) {
1153 VERBOSE_TARGET << LVL "Pathfinder: no data for new position: "
1154 << PP(ipos2) << std::endl;
1155 direction = getDirHeuristic(directions, g_pos);
1159 assert(cost.value > 0);
1161 int new_cost = current_cost + cost.value;
1163 // check if there already is a smaller path
1164 if ((m_min_target_distance > 0) &&
1165 (m_min_target_distance < new_cost)) {
1166 DEBUG_OUT(LVL "Pathfinder:"
1167 " already longer than best already found path "
1168 << PP(ipos2) << std::endl);
1172 if ((g_pos2.totalcost < 0) ||
1173 (g_pos2.totalcost > new_cost)) {
1174 DEBUG_OUT(LVL "Pathfinder: updating path at: "<<
1175 PP(ipos2) << " from: " << g_pos2.totalcost << " to "<<
1176 new_cost << " srcdir=" <<
1177 PP(invert(direction))<< std::endl);
1178 if (updateCostHeuristic(ipos2, invert(direction),
1184 DEBUG_OUT(LVL "Pathfinder:"
1185 " already found shorter path to: "
1186 << PP(ipos2) << std::endl);
1190 DEBUG_OUT(LVL "Pathfinder:"
1191 " not moving to invalid direction: "
1192 << PP(direction) << std::endl);
1196 DEBUG_OUT(LVL "Pathfinder:"
1197 " skipping srcdir: "
1198 << PP(direction) << std::endl);
1200 direction = getDirHeuristic(directions, g_pos);
1205 /******************************************************************************/
1206 void Pathfinder::buildPath(std::vector<v3s16> &path, v3s16 pos, int level)
1211 << LVL "Pathfinder: path is too long aborting" << std::endl;
1215 PathGridnode &g_pos = getIndexElement(pos);
1218 << LVL "Pathfinder: invalid next pos detected aborting" << std::endl;
1222 g_pos.is_element = true;
1224 //check if source reached
1226 path.push_back(pos);
1230 buildPath(path, pos + g_pos.sourcedir, level);
1231 path.push_back(pos);
1234 /******************************************************************************/
1235 v3f Pathfinder::tov3f(v3s16 pos)
1237 return v3f(BS * pos.X, BS * pos.Y, BS * pos.Z);
1240 #ifdef PATHFINDER_DEBUG
1242 /******************************************************************************/
1243 void Pathfinder::printCost()
1251 /******************************************************************************/
1252 void Pathfinder::printYdir()
1260 /******************************************************************************/
1261 void Pathfinder::printCost(PathDirections dir)
1263 std::cout << "Cost in direction: " << dirToName(dir) << std::endl;
1264 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1265 std::cout << std::setfill(' ');
1266 for (int y = 0; y < m_max_index_y; y++) {
1268 std::cout << "Level: " << y << std::endl;
1270 std::cout << std::setw(4) << " " << " ";
1271 for (int x = 0; x < m_max_index_x; x++) {
1272 std::cout << std::setw(4) << x;
1274 std::cout << std::endl;
1276 for (int z = 0; z < m_max_index_z; z++) {
1277 std::cout << std::setw(4) << z <<": ";
1278 for (int x = 0; x < m_max_index_x; x++) {
1279 if (getIdxElem(x, y, z).directions[dir].valid)
1280 std::cout << std::setw(4)
1281 << getIdxElem(x, y, z).directions[dir].value;
1283 std::cout << std::setw(4) << "-";
1285 std::cout << std::endl;
1287 std::cout << std::endl;
1291 /******************************************************************************/
1292 void Pathfinder::printYdir(PathDirections dir)
1294 std::cout << "Height difference in direction: " << dirToName(dir) << std::endl;
1295 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1296 std::cout << std::setfill(' ');
1297 for (int y = 0; y < m_max_index_y; y++) {
1299 std::cout << "Level: " << y << std::endl;
1301 std::cout << std::setw(4) << " " << " ";
1302 for (int x = 0; x < m_max_index_x; x++) {
1303 std::cout << std::setw(4) << x;
1305 std::cout << std::endl;
1307 for (int z = 0; z < m_max_index_z; z++) {
1308 std::cout << std::setw(4) << z <<": ";
1309 for (int x = 0; x < m_max_index_x; x++) {
1310 if (getIdxElem(x, y, z).directions[dir].valid)
1311 std::cout << std::setw(4)
1312 << getIdxElem(x, y, z).directions[dir].direction;
1314 std::cout << std::setw(4) << "-";
1316 std::cout << std::endl;
1318 std::cout << std::endl;
1322 /******************************************************************************/
1323 void Pathfinder::printType()
1325 std::cout << "Type of node:" << std::endl;
1326 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1327 std::cout << std::setfill(' ');
1328 for (int y = 0; y < m_max_index_y; y++) {
1330 std::cout << "Level: " << y << std::endl;
1332 std::cout << std::setw(3) << " " << " ";
1333 for (int x = 0; x < m_max_index_x; x++) {
1334 std::cout << std::setw(3) << x;
1336 std::cout << std::endl;
1338 for (int z = 0; z < m_max_index_z; z++) {
1339 std::cout << std::setw(3) << z <<": ";
1340 for (int x = 0; x < m_max_index_x; x++) {
1341 char toshow = getIdxElem(x, y, z).type;
1342 std::cout << std::setw(3) << toshow;
1344 std::cout << std::endl;
1346 std::cout << std::endl;
1348 std::cout << std::endl;
1351 /******************************************************************************/
1352 void Pathfinder::printPathLen()
1354 std::cout << "Pathlen:" << std::endl;
1355 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1356 std::cout << std::setfill(' ');
1357 for (int y = 0; y < m_max_index_y; y++) {
1359 std::cout << "Level: " << y << std::endl;
1361 std::cout << std::setw(3) << " " << " ";
1362 for (int x = 0; x < m_max_index_x; x++) {
1363 std::cout << std::setw(3) << x;
1365 std::cout << std::endl;
1367 for (int z = 0; z < m_max_index_z; z++) {
1368 std::cout << std::setw(3) << z <<": ";
1369 for (int x = 0; x < m_max_index_x; x++) {
1370 std::cout << std::setw(3) << getIdxElem(x, y, z).totalcost;
1372 std::cout << std::endl;
1374 std::cout << std::endl;
1376 std::cout << std::endl;
1379 /******************************************************************************/
1380 std::string Pathfinder::dirToName(PathDirections dir)
1400 /******************************************************************************/
1401 void Pathfinder::printPath(std::vector<v3s16> path)
1403 unsigned int current = 0;
1404 for (std::vector<v3s16>::iterator i = path.begin();
1405 i != path.end(); ++i) {
1406 std::cout << std::setw(3) << current << ":" << PP((*i)) << std::endl;