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 = false; /**< movement is possible */
80 int value = 0; /**< cost of movement */
81 int direction = 0; /**< y-direction of movement */
82 bool updated = false; /**< this cost has ben calculated */
87 /** representation of a mapnode to be used for pathfinding */
91 /** default constructor */
92 PathGridnode() = default;
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 = false; /**< node is on surface */
117 bool target = false; /**< node is target position */
118 bool source = false; /**< node is stating position */
119 int totalcost = -1; /**< 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 = false; /**< node is element of path detected */
126 char type = 'u'; /**< type of node */
131 /** Abstract class to manage the map data */
132 class GridNodeContainer {
134 virtual PathGridnode &access(v3s16 p)=0;
135 virtual ~GridNodeContainer() = default;
140 void initNode(v3s16 ipos, PathGridnode *p_node);
143 class ArrayGridNodeContainer : public GridNodeContainer {
145 virtual ~ArrayGridNodeContainer() = default;
147 ArrayGridNodeContainer(Pathfinder *pathf, v3s16 dimensions);
148 virtual PathGridnode &access(v3s16 p);
154 std::vector<PathGridnode> m_nodes_array;
157 class MapGridNodeContainer : public GridNodeContainer {
159 virtual ~MapGridNodeContainer() = default;
161 MapGridNodeContainer(Pathfinder *pathf);
162 virtual PathGridnode &access(v3s16 p);
164 std::map<v3s16, PathGridnode> m_nodes;
167 /** class doing pathfinding */
172 * default constructor
174 Pathfinder() = default;
179 * path evaluation function
180 * @param env environment to look for path
181 * @param source origin of path
182 * @param destination end position of path
183 * @param searchdistance maximum number of nodes to look in each direction
184 * @param max_jump maximum number of blocks a path may jump up
185 * @param max_drop maximum number of blocks a path may drop
186 * @param algo Algorithm to use for finding a path
188 std::vector<v3s16> getPath(ServerEnvironment *env,
191 unsigned int searchdistance,
192 unsigned int max_jump,
193 unsigned int max_drop,
197 /* helper functions */
200 * transform index pos to mappos
201 * @param ipos a index position
202 * @return map position
204 v3s16 getRealPos(v3s16 ipos);
207 * transform mappos to index pos
208 * @param pos a real pos
209 * @return index position
211 v3s16 getIndexPos(v3s16 pos);
214 * get gridnode at a specific index position
215 * @param ipos index position
216 * @return gridnode for index
218 PathGridnode &getIndexElement(v3s16 ipos);
221 * Get gridnode at a specific index position
222 * @return gridnode for index
224 PathGridnode &getIdxElem(s16 x, s16 y, s16 z);
227 * invert a 3d position
228 * @param pos 3d position
231 v3s16 invert(v3s16 pos);
234 * check if a index is within current search area
235 * @param index position to validate
238 bool isValidIndex(v3s16 index);
241 * translate position to float position
242 * @param pos integer position
243 * @return float position
245 v3f tov3f(v3s16 pos);
248 /* algorithm functions */
251 * calculate 2d manahttan distance to target on the xz plane
252 * @param pos position to calc distance
253 * @return integer distance
255 int getXZManhattanDist(v3s16 pos);
258 * get best direction based uppon heuristics
259 * @param directions list of unchecked directions
260 * @param g_pos mapnode to start from
261 * @return direction to check
263 v3s16 getDirHeuristic(std::vector<v3s16> &directions, PathGridnode &g_pos);
266 * build internal data representation of search area
267 * @return true/false if costmap creation was successfull
272 * calculate cost of movement
273 * @param pos real world position to start movement
274 * @param dir direction to move to
275 * @return cost information
277 PathCost calcCost(v3s16 pos, v3s16 dir);
280 * recursive update whole search areas total cost information
281 * @param ipos position to check next
282 * @param srcdir positionc checked last time
283 * @param total_cost cost of moving to ipos
284 * @param level current recursion depth
285 * @return true/false path to destination has been found
287 bool updateAllCosts(v3s16 ipos, v3s16 srcdir, int current_cost, int level);
290 * recursive try to find a patrh to destionation
291 * @param ipos position to check next
292 * @param srcdir positionc checked last time
293 * @param total_cost cost of moving to ipos
294 * @param level current recursion depth
295 * @return true/false path to destination has been found
297 bool updateCostHeuristic(v3s16 ipos, v3s16 srcdir, int current_cost, int level);
300 * recursive build a vector containing all nodes from source to destination
301 * @param path vector to add nodes to
302 * @param pos pos to check next
303 * @param level recursion depth
305 void buildPath(std::vector<v3s16> &path, v3s16 pos, int level);
308 int m_max_index_x = 0; /**< max index of search area in x direction */
309 int m_max_index_y = 0; /**< max index of search area in y direction */
310 int m_max_index_z = 0; /**< max index of search area in z direction */
313 int m_searchdistance = 0; /**< max distance to search in each direction */
314 int m_maxdrop = 0; /**< maximum number of blocks a path may drop */
315 int m_maxjump = 0; /**< maximum number of blocks a path may jump */
316 int m_min_target_distance = 0; /**< current smalest path to target */
318 bool m_prefetch = true; /**< prefetch cost data */
320 v3s16 m_start; /**< source position */
321 v3s16 m_destination; /**< destination position */
323 core::aabbox3d<s16> m_limits; /**< position limits in real map coordinates */
325 /** contains all map data already collected and analyzed.
326 Access it via the getIndexElement/getIdxElem methods. */
327 friend class GridNodeContainer;
328 GridNodeContainer *m_nodes_container = nullptr;
330 ServerEnvironment *m_env = 0; /**< minetest environment pointer */
332 #ifdef PATHFINDER_DEBUG
335 * print collected cost information
340 * print collected cost information in a specific direction
341 * @param dir direction to print
343 void printCost(PathDirections dir);
346 * print type of node as evaluated
351 * print pathlenght for all nodes in search area
357 * @param path path to show
359 void printPath(std::vector<v3s16> path);
362 * print y direction for all movements
367 * print y direction for moving in a specific direction
368 * @param dir direction to show data
370 void printYdir(PathDirections dir);
373 * helper function to translate a direction to speaking text
374 * @param dir direction to translate
375 * @return textual name of direction
377 std::string dirToName(PathDirections dir);
381 /******************************************************************************/
383 /******************************************************************************/
385 std::vector<v3s16> get_path(ServerEnvironment* env,
388 unsigned int searchdistance,
389 unsigned int max_jump,
390 unsigned int max_drop,
393 Pathfinder searchclass;
395 return searchclass.getPath(env,
397 searchdistance, max_jump, max_drop, algo);
400 /******************************************************************************/
401 PathCost::PathCost(const PathCost &b)
404 direction = b.direction;
409 /******************************************************************************/
410 PathCost &PathCost::operator= (const PathCost &b)
413 direction = b.direction;
420 /******************************************************************************/
421 PathGridnode::PathGridnode(const PathGridnode &b)
425 totalcost(b.totalcost),
426 sourcedir(b.sourcedir),
428 is_element(b.is_element),
432 directions[DIR_XP] = b.directions[DIR_XP];
433 directions[DIR_XM] = b.directions[DIR_XM];
434 directions[DIR_ZP] = b.directions[DIR_ZP];
435 directions[DIR_ZM] = b.directions[DIR_ZM];
438 /******************************************************************************/
439 PathGridnode &PathGridnode::operator= (const PathGridnode &b)
444 is_element = b.is_element;
445 totalcost = b.totalcost;
446 sourcedir = b.sourcedir;
450 directions[DIR_XP] = b.directions[DIR_XP];
451 directions[DIR_XM] = b.directions[DIR_XM];
452 directions[DIR_ZP] = b.directions[DIR_ZP];
453 directions[DIR_ZM] = b.directions[DIR_ZM];
458 /******************************************************************************/
459 PathCost PathGridnode::getCost(v3s16 dir)
462 return directions[DIR_XP];
465 return directions[DIR_XM];
468 return directions[DIR_ZP];
471 return directions[DIR_ZM];
477 /******************************************************************************/
478 void PathGridnode::setCost(v3s16 dir, const PathCost &cost)
481 directions[DIR_XP] = cost;
484 directions[DIR_XM] = cost;
487 directions[DIR_ZP] = cost;
490 directions[DIR_ZM] = cost;
494 void GridNodeContainer::initNode(v3s16 ipos, PathGridnode *p_node)
496 const NodeDefManager *ndef = m_pathf->m_env->getGameDef()->ndef();
497 PathGridnode &elem = *p_node;
499 v3s16 realpos = m_pathf->getRealPos(ipos);
501 MapNode current = m_pathf->m_env->getMap().getNodeNoEx(realpos);
502 MapNode below = m_pathf->m_env->getMap().getNodeNoEx(realpos + v3s16(0, -1, 0));
505 if ((current.param0 == CONTENT_IGNORE) ||
506 (below.param0 == CONTENT_IGNORE)) {
507 DEBUG_OUT("Pathfinder: " << PP(realpos) <<
508 " current or below is invalid element" << std::endl);
509 if (current.param0 == CONTENT_IGNORE) {
511 DEBUG_OUT(PP(ipos) << ": " << 'i' << std::endl);
516 //don't add anything if it isn't an air node
517 if (ndef->get(current).walkable || !ndef->get(below).walkable) {
518 DEBUG_OUT("Pathfinder: " << PP(realpos)
519 << " not on surface" << std::endl);
520 if (ndef->get(current).walkable) {
522 DEBUG_OUT(PP(ipos) << ": " << 's' << std::endl);
525 DEBUG_OUT(PP(ipos) << ": " << '-' << std::endl);
533 DEBUG_OUT(PP(ipos) << ": " << 'a' << std::endl);
535 if (m_pathf->m_prefetch) {
536 elem.directions[DIR_XP] = m_pathf->calcCost(realpos, v3s16( 1, 0, 0));
537 elem.directions[DIR_XM] = m_pathf->calcCost(realpos, v3s16(-1, 0, 0));
538 elem.directions[DIR_ZP] = m_pathf->calcCost(realpos, v3s16( 0, 0, 1));
539 elem.directions[DIR_ZM] = m_pathf->calcCost(realpos, v3s16( 0, 0,-1));
543 ArrayGridNodeContainer::ArrayGridNodeContainer(Pathfinder *pathf, v3s16 dimensions) :
544 m_x_stride(dimensions.Y * dimensions.Z),
545 m_y_stride(dimensions.Z)
549 m_nodes_array.resize(dimensions.X * dimensions.Y * dimensions.Z);
550 INFO_TARGET << "Pathfinder ArrayGridNodeContainer constructor." << std::endl;
551 for (int x = 0; x < dimensions.X; x++) {
552 for (int y = 0; y < dimensions.Y; y++) {
553 for (int z= 0; z < dimensions.Z; z++) {
555 initNode(ipos, &access(ipos));
561 PathGridnode &ArrayGridNodeContainer::access(v3s16 p)
563 return m_nodes_array[p.X * m_x_stride + p.Y * m_y_stride + p.Z];
566 MapGridNodeContainer::MapGridNodeContainer(Pathfinder *pathf)
571 PathGridnode &MapGridNodeContainer::access(v3s16 p)
573 std::map<v3s16, PathGridnode>::iterator it = m_nodes.find(p);
574 if (it != m_nodes.end()) {
577 PathGridnode &n = m_nodes[p];
584 /******************************************************************************/
585 std::vector<v3s16> Pathfinder::getPath(ServerEnvironment *env,
588 unsigned int searchdistance,
589 unsigned int max_jump,
590 unsigned int max_drop,
593 #ifdef PATHFINDER_CALC_TIME
595 clock_gettime(CLOCK_REALTIME, &ts);
597 std::vector<v3s16> retval;
601 ERROR_TARGET << "missing environment pointer" << std::endl;
605 m_searchdistance = searchdistance;
607 m_maxjump = max_jump;
608 m_maxdrop = max_drop;
610 m_destination = destination;
611 m_min_target_distance = -1;
614 if (algo == PA_PLAIN_NP) {
618 int min_x = MYMIN(source.X, destination.X);
619 int max_x = MYMAX(source.X, destination.X);
621 int min_y = MYMIN(source.Y, destination.Y);
622 int max_y = MYMAX(source.Y, destination.Y);
624 int min_z = MYMIN(source.Z, destination.Z);
625 int max_z = MYMAX(source.Z, destination.Z);
627 m_limits.MinEdge.X = min_x - searchdistance;
628 m_limits.MinEdge.Y = min_y - searchdistance;
629 m_limits.MinEdge.Z = min_z - searchdistance;
631 m_limits.MaxEdge.X = max_x + searchdistance;
632 m_limits.MaxEdge.Y = max_y + searchdistance;
633 m_limits.MaxEdge.Z = max_z + searchdistance;
635 v3s16 diff = m_limits.MaxEdge - m_limits.MinEdge;
637 m_max_index_x = diff.X;
638 m_max_index_y = diff.Y;
639 m_max_index_z = diff.Z;
641 delete m_nodes_container;
642 if (diff.getLength() > 5) {
643 m_nodes_container = new MapGridNodeContainer(this);
645 m_nodes_container = new ArrayGridNodeContainer(this, diff);
647 #ifdef PATHFINDER_DEBUG
653 //validate and mark start and end pos
654 v3s16 StartIndex = getIndexPos(source);
655 v3s16 EndIndex = getIndexPos(destination);
657 PathGridnode &startpos = getIndexElement(StartIndex);
658 PathGridnode &endpos = getIndexElement(EndIndex);
660 if (!startpos.valid) {
661 VERBOSE_TARGET << "invalid startpos" <<
662 "Index: " << PP(StartIndex) <<
663 "Realpos: " << PP(getRealPos(StartIndex)) << std::endl;
667 VERBOSE_TARGET << "invalid stoppos" <<
668 "Index: " << PP(EndIndex) <<
669 "Realpos: " << PP(getRealPos(EndIndex)) << std::endl;
673 endpos.target = true;
674 startpos.source = true;
675 startpos.totalcost = 0;
677 bool update_cost_retval = false;
681 update_cost_retval = updateAllCosts(StartIndex, v3s16(0, 0, 0), 0, 0);
685 update_cost_retval = updateCostHeuristic(StartIndex, v3s16(0, 0, 0), 0, 0);
688 ERROR_TARGET << "missing PathAlgorithm"<< std::endl;
692 if (update_cost_retval) {
694 #ifdef PATHFINDER_DEBUG
695 std::cout << "Path to target found!" << std::endl;
700 std::vector<v3s16> path;
701 buildPath(path, EndIndex, 0);
703 #ifdef PATHFINDER_DEBUG
704 std::cout << "Full index path:" << std::endl;
709 std::vector<v3s16> full_path;
710 for (const v3s16 &i : path) {
711 full_path.push_back(getIndexElement(i).pos);
714 #ifdef PATHFINDER_DEBUG
715 std::cout << "full path:" << std::endl;
716 printPath(full_path);
718 #ifdef PATHFINDER_CALC_TIME
720 clock_gettime(CLOCK_REALTIME, &ts2);
722 int ms = (ts2.tv_nsec - ts.tv_nsec)/(1000*1000);
723 int us = ((ts2.tv_nsec - ts.tv_nsec) - (ms*1000*1000))/1000;
724 int ns = ((ts2.tv_nsec - ts.tv_nsec) - ( (ms*1000*1000) + (us*1000)));
727 std::cout << "Calculating path took: " << (ts2.tv_sec - ts.tv_sec) <<
728 "s " << ms << "ms " << us << "us " << ns << "ns " << std::endl;
733 #ifdef PATHFINDER_DEBUG
736 ERROR_TARGET << "failed to update cost map"<< std::endl;
744 Pathfinder::~Pathfinder()
746 delete m_nodes_container;
748 /******************************************************************************/
749 v3s16 Pathfinder::getRealPos(v3s16 ipos)
751 return m_limits.MinEdge + ipos;
754 /******************************************************************************/
755 PathCost Pathfinder::calcCost(v3s16 pos, v3s16 dir)
757 const NodeDefManager *ndef = m_env->getGameDef()->ndef();
760 retval.updated = true;
762 v3s16 pos2 = pos + dir;
765 if (!m_limits.isPointInside(pos2)) {
766 DEBUG_OUT("Pathfinder: " << PP(pos2) <<
767 " no cost -> out of limits" << std::endl);
771 MapNode node_at_pos2 = m_env->getMap().getNodeNoEx(pos2);
773 //did we get information about node?
774 if (node_at_pos2.param0 == CONTENT_IGNORE ) {
775 VERBOSE_TARGET << "Pathfinder: (1) area at pos: "
776 << PP(pos2) << " not loaded";
780 if (!ndef->get(node_at_pos2).walkable) {
781 MapNode node_below_pos2 =
782 m_env->getMap().getNodeNoEx(pos2 + v3s16(0, -1, 0));
784 //did we get information about node?
785 if (node_below_pos2.param0 == CONTENT_IGNORE ) {
786 VERBOSE_TARGET << "Pathfinder: (2) area at pos: "
787 << PP((pos2 + v3s16(0, -1, 0))) << " not loaded";
791 if (ndef->get(node_below_pos2).walkable) {
794 retval.direction = 0;
795 DEBUG_OUT("Pathfinder: "<< PP(pos)
796 << " cost same height found" << std::endl);
799 v3s16 testpos = pos2 - v3s16(0, -1, 0);
800 MapNode node_at_pos = m_env->getMap().getNodeNoEx(testpos);
802 while ((node_at_pos.param0 != CONTENT_IGNORE) &&
803 (!ndef->get(node_at_pos).walkable) &&
804 (testpos.Y > m_limits.MinEdge.Y)) {
805 testpos += v3s16(0, -1, 0);
806 node_at_pos = m_env->getMap().getNodeNoEx(testpos);
809 //did we find surface?
810 if ((testpos.Y >= m_limits.MinEdge.Y) &&
811 (node_at_pos.param0 != CONTENT_IGNORE) &&
812 (ndef->get(node_at_pos).walkable)) {
813 if ((pos2.Y - testpos.Y - 1) <= m_maxdrop) {
816 //difference of y-pos +1 (target node is ABOVE solid node)
817 retval.direction = ((testpos.Y - pos2.Y) +1);
818 DEBUG_OUT("Pathfinder cost below height found" << std::endl);
821 INFO_TARGET << "Pathfinder:"
822 " distance to surface below to big: "
823 << (testpos.Y - pos2.Y) << " max: " << m_maxdrop
828 DEBUG_OUT("Pathfinder: no surface below found" << std::endl);
833 v3s16 testpos = pos2;
834 MapNode node_at_pos = m_env->getMap().getNodeNoEx(testpos);
836 while ((node_at_pos.param0 != CONTENT_IGNORE) &&
837 (ndef->get(node_at_pos).walkable) &&
838 (testpos.Y < m_limits.MaxEdge.Y)) {
839 testpos += v3s16(0, 1, 0);
840 node_at_pos = m_env->getMap().getNodeNoEx(testpos);
843 //did we find surface?
844 if ((testpos.Y <= m_limits.MaxEdge.Y) &&
845 (!ndef->get(node_at_pos).walkable)) {
847 if (testpos.Y - pos2.Y <= m_maxjump) {
850 retval.direction = (testpos.Y - pos2.Y);
851 DEBUG_OUT("Pathfinder cost above found" << std::endl);
854 DEBUG_OUT("Pathfinder: distance to surface above to big: "
855 << (testpos.Y - pos2.Y) << " max: " << m_maxjump
860 DEBUG_OUT("Pathfinder: no surface above found" << std::endl);
866 /******************************************************************************/
867 v3s16 Pathfinder::getIndexPos(v3s16 pos)
869 return pos - m_limits.MinEdge;
872 /******************************************************************************/
873 PathGridnode &Pathfinder::getIndexElement(v3s16 ipos)
875 return m_nodes_container->access(ipos);
878 /******************************************************************************/
879 inline PathGridnode &Pathfinder::getIdxElem(s16 x, s16 y, s16 z)
881 return m_nodes_container->access(v3s16(x,y,z));
884 /******************************************************************************/
885 bool Pathfinder::isValidIndex(v3s16 index)
887 if ( (index.X < m_max_index_x) &&
888 (index.Y < m_max_index_y) &&
889 (index.Z < m_max_index_z) &&
898 /******************************************************************************/
899 v3s16 Pathfinder::invert(v3s16 pos)
910 /******************************************************************************/
911 bool Pathfinder::updateAllCosts(v3s16 ipos,
916 PathGridnode &g_pos = getIndexElement(ipos);
917 g_pos.totalcost = current_cost;
918 g_pos.sourcedir = srcdir;
922 //check if target has been found
924 m_min_target_distance = current_cost;
925 DEBUG_OUT(LVL " Pathfinder: target found!" << std::endl);
931 std::vector<v3s16> directions;
933 directions.emplace_back(1,0, 0);
934 directions.emplace_back(-1,0, 0);
935 directions.emplace_back(0,0, 1);
936 directions.emplace_back(0,0,-1);
938 for (v3s16 &direction : directions) {
939 if (direction != srcdir) {
940 PathCost cost = g_pos.getCost(direction);
943 direction.Y = cost.direction;
945 v3s16 ipos2 = ipos + direction;
947 if (!isValidIndex(ipos2)) {
948 DEBUG_OUT(LVL " Pathfinder: " << PP(ipos2) <<
949 " out of range, max=" << PP(m_limits.MaxEdge) << std::endl);
953 PathGridnode &g_pos2 = getIndexElement(ipos2);
956 VERBOSE_TARGET << LVL "Pathfinder: no data for new position: "
957 << PP(ipos2) << std::endl;
961 assert(cost.value > 0);
963 int new_cost = current_cost + cost.value;
965 // check if there already is a smaller path
966 if ((m_min_target_distance > 0) &&
967 (m_min_target_distance < new_cost)) {
971 if ((g_pos2.totalcost < 0) ||
972 (g_pos2.totalcost > new_cost)) {
973 DEBUG_OUT(LVL "Pathfinder: updating path at: "<<
974 PP(ipos2) << " from: " << g_pos2.totalcost << " to "<<
975 new_cost << std::endl);
976 if (updateAllCosts(ipos2, invert(direction),
982 DEBUG_OUT(LVL "Pathfinder:"
983 " already found shorter path to: "
984 << PP(ipos2) << std::endl);
988 DEBUG_OUT(LVL "Pathfinder:"
989 " not moving to invalid direction: "
990 << PP(directions[i]) << std::endl);
997 /******************************************************************************/
998 int Pathfinder::getXZManhattanDist(v3s16 pos)
1000 int min_x = MYMIN(pos.X, m_destination.X);
1001 int max_x = MYMAX(pos.X, m_destination.X);
1002 int min_z = MYMIN(pos.Z, m_destination.Z);
1003 int max_z = MYMAX(pos.Z, m_destination.Z);
1005 return (max_x - min_x) + (max_z - min_z);
1008 /******************************************************************************/
1009 v3s16 Pathfinder::getDirHeuristic(std::vector<v3s16> &directions, PathGridnode &g_pos)
1012 v3s16 retdir = v3s16(0, 0, 0);
1013 v3s16 srcpos = g_pos.pos;
1014 DEBUG_OUT("Pathfinder: remaining dirs at beginning:"
1015 << directions.size() << std::endl);
1017 for (v3s16 &direction : directions) {
1019 v3s16 pos1 = v3s16(srcpos.X + direction.X, 0, srcpos.Z+ direction.Z);
1021 int cur_manhattan = getXZManhattanDist(pos1);
1022 PathCost cost = g_pos.getCost(direction);
1024 if (!cost.updated) {
1025 cost = calcCost(g_pos.pos, direction);
1026 g_pos.setCost(direction, cost);
1030 int score = cost.value + cur_manhattan;
1032 if ((minscore < 0)|| (score < minscore)) {
1039 if (retdir != v3s16(0, 0, 0)) {
1040 for (std::vector<v3s16>::iterator iter = directions.begin();
1041 iter != directions.end();
1043 if(*iter == retdir) {
1044 DEBUG_OUT("Pathfinder: removing return direction" << std::endl);
1045 directions.erase(iter);
1051 DEBUG_OUT("Pathfinder: didn't find any valid direction clearing"
1055 DEBUG_OUT("Pathfinder: remaining dirs at end:" << directions.size()
1060 /******************************************************************************/
1061 bool Pathfinder::updateCostHeuristic( v3s16 ipos,
1067 PathGridnode &g_pos = getIndexElement(ipos);
1068 g_pos.totalcost = current_cost;
1069 g_pos.sourcedir = srcdir;
1073 //check if target has been found
1075 m_min_target_distance = current_cost;
1076 DEBUG_OUT(LVL " Pathfinder: target found!" << std::endl);
1080 bool retval = false;
1082 std::vector<v3s16> directions;
1084 directions.emplace_back(1, 0, 0);
1085 directions.emplace_back(-1, 0, 0);
1086 directions.emplace_back(0, 0, 1);
1087 directions.emplace_back(0, 0, -1);
1089 v3s16 direction = getDirHeuristic(directions, g_pos);
1091 while (direction != v3s16(0, 0, 0) && (!retval)) {
1093 if (direction != srcdir) {
1094 PathCost cost = g_pos.getCost(direction);
1097 direction.Y = cost.direction;
1099 v3s16 ipos2 = ipos + direction;
1101 if (!isValidIndex(ipos2)) {
1102 DEBUG_OUT(LVL " Pathfinder: " << PP(ipos2) <<
1103 " out of range, max=" << PP(m_limits.MaxEdge) << std::endl);
1104 direction = getDirHeuristic(directions, g_pos);
1108 PathGridnode &g_pos2 = getIndexElement(ipos2);
1110 if (!g_pos2.valid) {
1111 VERBOSE_TARGET << LVL "Pathfinder: no data for new position: "
1112 << PP(ipos2) << std::endl;
1113 direction = getDirHeuristic(directions, g_pos);
1117 assert(cost.value > 0);
1119 int new_cost = current_cost + cost.value;
1121 // check if there already is a smaller path
1122 if ((m_min_target_distance > 0) &&
1123 (m_min_target_distance < new_cost)) {
1124 DEBUG_OUT(LVL "Pathfinder:"
1125 " already longer than best already found path "
1126 << PP(ipos2) << std::endl);
1130 if ((g_pos2.totalcost < 0) ||
1131 (g_pos2.totalcost > new_cost)) {
1132 DEBUG_OUT(LVL "Pathfinder: updating path at: "<<
1133 PP(ipos2) << " from: " << g_pos2.totalcost << " to "<<
1134 new_cost << " srcdir=" <<
1135 PP(invert(direction))<< std::endl);
1136 if (updateCostHeuristic(ipos2, invert(direction),
1142 DEBUG_OUT(LVL "Pathfinder:"
1143 " already found shorter path to: "
1144 << PP(ipos2) << std::endl);
1148 DEBUG_OUT(LVL "Pathfinder:"
1149 " not moving to invalid direction: "
1150 << PP(direction) << std::endl);
1154 DEBUG_OUT(LVL "Pathfinder:"
1155 " skipping srcdir: "
1156 << PP(direction) << std::endl);
1158 direction = getDirHeuristic(directions, g_pos);
1163 /******************************************************************************/
1164 void Pathfinder::buildPath(std::vector<v3s16> &path, v3s16 pos, int level)
1169 << LVL "Pathfinder: path is too long aborting" << std::endl;
1173 PathGridnode &g_pos = getIndexElement(pos);
1176 << LVL "Pathfinder: invalid next pos detected aborting" << std::endl;
1180 g_pos.is_element = true;
1182 //check if source reached
1184 path.push_back(pos);
1188 buildPath(path, pos + g_pos.sourcedir, level);
1189 path.push_back(pos);
1192 /******************************************************************************/
1193 v3f Pathfinder::tov3f(v3s16 pos)
1195 return v3f(BS * pos.X, BS * pos.Y, BS * pos.Z);
1198 #ifdef PATHFINDER_DEBUG
1200 /******************************************************************************/
1201 void Pathfinder::printCost()
1209 /******************************************************************************/
1210 void Pathfinder::printYdir()
1218 /******************************************************************************/
1219 void Pathfinder::printCost(PathDirections dir)
1221 std::cout << "Cost in direction: " << dirToName(dir) << std::endl;
1222 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1223 std::cout << std::setfill(' ');
1224 for (int y = 0; y < m_max_index_y; y++) {
1226 std::cout << "Level: " << y << std::endl;
1228 std::cout << std::setw(4) << " " << " ";
1229 for (int x = 0; x < m_max_index_x; x++) {
1230 std::cout << std::setw(4) << x;
1232 std::cout << std::endl;
1234 for (int z = 0; z < m_max_index_z; z++) {
1235 std::cout << std::setw(4) << z <<": ";
1236 for (int x = 0; x < m_max_index_x; x++) {
1237 if (getIdxElem(x, y, z).directions[dir].valid)
1238 std::cout << std::setw(4)
1239 << getIdxElem(x, y, z).directions[dir].value;
1241 std::cout << std::setw(4) << "-";
1243 std::cout << std::endl;
1245 std::cout << std::endl;
1249 /******************************************************************************/
1250 void Pathfinder::printYdir(PathDirections dir)
1252 std::cout << "Height difference in direction: " << dirToName(dir) << std::endl;
1253 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1254 std::cout << std::setfill(' ');
1255 for (int y = 0; y < m_max_index_y; y++) {
1257 std::cout << "Level: " << y << std::endl;
1259 std::cout << std::setw(4) << " " << " ";
1260 for (int x = 0; x < m_max_index_x; x++) {
1261 std::cout << std::setw(4) << x;
1263 std::cout << std::endl;
1265 for (int z = 0; z < m_max_index_z; z++) {
1266 std::cout << std::setw(4) << z <<": ";
1267 for (int x = 0; x < m_max_index_x; x++) {
1268 if (getIdxElem(x, y, z).directions[dir].valid)
1269 std::cout << std::setw(4)
1270 << getIdxElem(x, y, z).directions[dir].direction;
1272 std::cout << std::setw(4) << "-";
1274 std::cout << std::endl;
1276 std::cout << std::endl;
1280 /******************************************************************************/
1281 void Pathfinder::printType()
1283 std::cout << "Type of node:" << std::endl;
1284 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1285 std::cout << std::setfill(' ');
1286 for (int y = 0; y < m_max_index_y; y++) {
1288 std::cout << "Level: " << y << std::endl;
1290 std::cout << std::setw(3) << " " << " ";
1291 for (int x = 0; x < m_max_index_x; x++) {
1292 std::cout << std::setw(3) << x;
1294 std::cout << std::endl;
1296 for (int z = 0; z < m_max_index_z; z++) {
1297 std::cout << std::setw(3) << z <<": ";
1298 for (int x = 0; x < m_max_index_x; x++) {
1299 char toshow = getIdxElem(x, y, z).type;
1300 std::cout << std::setw(3) << toshow;
1302 std::cout << std::endl;
1304 std::cout << std::endl;
1306 std::cout << std::endl;
1309 /******************************************************************************/
1310 void Pathfinder::printPathLen()
1312 std::cout << "Pathlen:" << std::endl;
1313 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1314 std::cout << std::setfill(' ');
1315 for (int y = 0; y < m_max_index_y; y++) {
1317 std::cout << "Level: " << y << std::endl;
1319 std::cout << std::setw(3) << " " << " ";
1320 for (int x = 0; x < m_max_index_x; x++) {
1321 std::cout << std::setw(3) << x;
1323 std::cout << std::endl;
1325 for (int z = 0; z < m_max_index_z; z++) {
1326 std::cout << std::setw(3) << z <<": ";
1327 for (int x = 0; x < m_max_index_x; x++) {
1328 std::cout << std::setw(3) << getIdxElem(x, y, z).totalcost;
1330 std::cout << std::endl;
1332 std::cout << std::endl;
1334 std::cout << std::endl;
1337 /******************************************************************************/
1338 std::string Pathfinder::dirToName(PathDirections dir)
1358 /******************************************************************************/
1359 void Pathfinder::printPath(std::vector<v3s16> path)
1361 unsigned int current = 0;
1362 for (std::vector<v3s16>::iterator i = path.begin();
1363 i != path.end(); ++i) {
1364 std::cout << std::setw(3) << current << ":" << PP((*i)) << std::endl;