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 full_path.reserve(path.size());
711 for (const v3s16 &i : path) {
712 full_path.push_back(getIndexElement(i).pos);
715 #ifdef PATHFINDER_DEBUG
716 std::cout << "full path:" << std::endl;
717 printPath(full_path);
719 #ifdef PATHFINDER_CALC_TIME
721 clock_gettime(CLOCK_REALTIME, &ts2);
723 int ms = (ts2.tv_nsec - ts.tv_nsec)/(1000*1000);
724 int us = ((ts2.tv_nsec - ts.tv_nsec) - (ms*1000*1000))/1000;
725 int ns = ((ts2.tv_nsec - ts.tv_nsec) - ( (ms*1000*1000) + (us*1000)));
728 std::cout << "Calculating path took: " << (ts2.tv_sec - ts.tv_sec) <<
729 "s " << ms << "ms " << us << "us " << ns << "ns " << std::endl;
734 #ifdef PATHFINDER_DEBUG
737 ERROR_TARGET << "failed to update cost map"<< std::endl;
745 Pathfinder::~Pathfinder()
747 delete m_nodes_container;
749 /******************************************************************************/
750 v3s16 Pathfinder::getRealPos(v3s16 ipos)
752 return m_limits.MinEdge + ipos;
755 /******************************************************************************/
756 PathCost Pathfinder::calcCost(v3s16 pos, v3s16 dir)
758 const NodeDefManager *ndef = m_env->getGameDef()->ndef();
761 retval.updated = true;
763 v3s16 pos2 = pos + dir;
766 if (!m_limits.isPointInside(pos2)) {
767 DEBUG_OUT("Pathfinder: " << PP(pos2) <<
768 " no cost -> out of limits" << std::endl);
772 MapNode node_at_pos2 = m_env->getMap().getNodeNoEx(pos2);
774 //did we get information about node?
775 if (node_at_pos2.param0 == CONTENT_IGNORE ) {
776 VERBOSE_TARGET << "Pathfinder: (1) area at pos: "
777 << PP(pos2) << " not loaded";
781 if (!ndef->get(node_at_pos2).walkable) {
782 MapNode node_below_pos2 =
783 m_env->getMap().getNodeNoEx(pos2 + v3s16(0, -1, 0));
785 //did we get information about node?
786 if (node_below_pos2.param0 == CONTENT_IGNORE ) {
787 VERBOSE_TARGET << "Pathfinder: (2) area at pos: "
788 << PP((pos2 + v3s16(0, -1, 0))) << " not loaded";
792 if (ndef->get(node_below_pos2).walkable) {
795 retval.direction = 0;
796 DEBUG_OUT("Pathfinder: "<< PP(pos)
797 << " cost same height found" << std::endl);
800 v3s16 testpos = pos2 - v3s16(0, -1, 0);
801 MapNode node_at_pos = m_env->getMap().getNodeNoEx(testpos);
803 while ((node_at_pos.param0 != CONTENT_IGNORE) &&
804 (!ndef->get(node_at_pos).walkable) &&
805 (testpos.Y > m_limits.MinEdge.Y)) {
806 testpos += v3s16(0, -1, 0);
807 node_at_pos = m_env->getMap().getNodeNoEx(testpos);
810 //did we find surface?
811 if ((testpos.Y >= m_limits.MinEdge.Y) &&
812 (node_at_pos.param0 != CONTENT_IGNORE) &&
813 (ndef->get(node_at_pos).walkable)) {
814 if ((pos2.Y - testpos.Y - 1) <= m_maxdrop) {
817 //difference of y-pos +1 (target node is ABOVE solid node)
818 retval.direction = ((testpos.Y - pos2.Y) +1);
819 DEBUG_OUT("Pathfinder cost below height found" << std::endl);
822 INFO_TARGET << "Pathfinder:"
823 " distance to surface below to big: "
824 << (testpos.Y - pos2.Y) << " max: " << m_maxdrop
829 DEBUG_OUT("Pathfinder: no surface below found" << std::endl);
834 v3s16 testpos = pos2;
835 MapNode node_at_pos = m_env->getMap().getNodeNoEx(testpos);
837 while ((node_at_pos.param0 != CONTENT_IGNORE) &&
838 (ndef->get(node_at_pos).walkable) &&
839 (testpos.Y < m_limits.MaxEdge.Y)) {
840 testpos += v3s16(0, 1, 0);
841 node_at_pos = m_env->getMap().getNodeNoEx(testpos);
844 //did we find surface?
845 if ((testpos.Y <= m_limits.MaxEdge.Y) &&
846 (!ndef->get(node_at_pos).walkable)) {
848 if (testpos.Y - pos2.Y <= m_maxjump) {
851 retval.direction = (testpos.Y - pos2.Y);
852 DEBUG_OUT("Pathfinder cost above found" << std::endl);
855 DEBUG_OUT("Pathfinder: distance to surface above to big: "
856 << (testpos.Y - pos2.Y) << " max: " << m_maxjump
861 DEBUG_OUT("Pathfinder: no surface above found" << std::endl);
867 /******************************************************************************/
868 v3s16 Pathfinder::getIndexPos(v3s16 pos)
870 return pos - m_limits.MinEdge;
873 /******************************************************************************/
874 PathGridnode &Pathfinder::getIndexElement(v3s16 ipos)
876 return m_nodes_container->access(ipos);
879 /******************************************************************************/
880 inline PathGridnode &Pathfinder::getIdxElem(s16 x, s16 y, s16 z)
882 return m_nodes_container->access(v3s16(x,y,z));
885 /******************************************************************************/
886 bool Pathfinder::isValidIndex(v3s16 index)
888 if ( (index.X < m_max_index_x) &&
889 (index.Y < m_max_index_y) &&
890 (index.Z < m_max_index_z) &&
899 /******************************************************************************/
900 v3s16 Pathfinder::invert(v3s16 pos)
911 /******************************************************************************/
912 bool Pathfinder::updateAllCosts(v3s16 ipos,
917 PathGridnode &g_pos = getIndexElement(ipos);
918 g_pos.totalcost = current_cost;
919 g_pos.sourcedir = srcdir;
923 //check if target has been found
925 m_min_target_distance = current_cost;
926 DEBUG_OUT(LVL " Pathfinder: target found!" << std::endl);
932 std::vector<v3s16> directions;
934 directions.emplace_back(1,0, 0);
935 directions.emplace_back(-1,0, 0);
936 directions.emplace_back(0,0, 1);
937 directions.emplace_back(0,0,-1);
939 for (v3s16 &direction : directions) {
940 if (direction != srcdir) {
941 PathCost cost = g_pos.getCost(direction);
944 direction.Y = cost.direction;
946 v3s16 ipos2 = ipos + direction;
948 if (!isValidIndex(ipos2)) {
949 DEBUG_OUT(LVL " Pathfinder: " << PP(ipos2) <<
950 " out of range, max=" << PP(m_limits.MaxEdge) << std::endl);
954 PathGridnode &g_pos2 = getIndexElement(ipos2);
957 VERBOSE_TARGET << LVL "Pathfinder: no data for new position: "
958 << PP(ipos2) << std::endl;
962 assert(cost.value > 0);
964 int new_cost = current_cost + cost.value;
966 // check if there already is a smaller path
967 if ((m_min_target_distance > 0) &&
968 (m_min_target_distance < new_cost)) {
972 if ((g_pos2.totalcost < 0) ||
973 (g_pos2.totalcost > new_cost)) {
974 DEBUG_OUT(LVL "Pathfinder: updating path at: "<<
975 PP(ipos2) << " from: " << g_pos2.totalcost << " to "<<
976 new_cost << std::endl);
977 if (updateAllCosts(ipos2, invert(direction),
983 DEBUG_OUT(LVL "Pathfinder:"
984 " already found shorter path to: "
985 << PP(ipos2) << std::endl);
989 DEBUG_OUT(LVL "Pathfinder:"
990 " not moving to invalid direction: "
991 << PP(directions[i]) << std::endl);
998 /******************************************************************************/
999 int Pathfinder::getXZManhattanDist(v3s16 pos)
1001 int min_x = MYMIN(pos.X, m_destination.X);
1002 int max_x = MYMAX(pos.X, m_destination.X);
1003 int min_z = MYMIN(pos.Z, m_destination.Z);
1004 int max_z = MYMAX(pos.Z, m_destination.Z);
1006 return (max_x - min_x) + (max_z - min_z);
1009 /******************************************************************************/
1010 v3s16 Pathfinder::getDirHeuristic(std::vector<v3s16> &directions, PathGridnode &g_pos)
1013 v3s16 retdir = v3s16(0, 0, 0);
1014 v3s16 srcpos = g_pos.pos;
1015 DEBUG_OUT("Pathfinder: remaining dirs at beginning:"
1016 << directions.size() << std::endl);
1018 for (v3s16 &direction : directions) {
1020 v3s16 pos1 = v3s16(srcpos.X + direction.X, 0, srcpos.Z+ direction.Z);
1022 int cur_manhattan = getXZManhattanDist(pos1);
1023 PathCost cost = g_pos.getCost(direction);
1025 if (!cost.updated) {
1026 cost = calcCost(g_pos.pos, direction);
1027 g_pos.setCost(direction, cost);
1031 int score = cost.value + cur_manhattan;
1033 if ((minscore < 0)|| (score < minscore)) {
1040 if (retdir != v3s16(0, 0, 0)) {
1041 for (std::vector<v3s16>::iterator iter = directions.begin();
1042 iter != directions.end();
1044 if(*iter == retdir) {
1045 DEBUG_OUT("Pathfinder: removing return direction" << std::endl);
1046 directions.erase(iter);
1052 DEBUG_OUT("Pathfinder: didn't find any valid direction clearing"
1056 DEBUG_OUT("Pathfinder: remaining dirs at end:" << directions.size()
1061 /******************************************************************************/
1062 bool Pathfinder::updateCostHeuristic( v3s16 ipos,
1068 PathGridnode &g_pos = getIndexElement(ipos);
1069 g_pos.totalcost = current_cost;
1070 g_pos.sourcedir = srcdir;
1074 //check if target has been found
1076 m_min_target_distance = current_cost;
1077 DEBUG_OUT(LVL " Pathfinder: target found!" << std::endl);
1081 bool retval = false;
1083 std::vector<v3s16> directions;
1085 directions.emplace_back(1, 0, 0);
1086 directions.emplace_back(-1, 0, 0);
1087 directions.emplace_back(0, 0, 1);
1088 directions.emplace_back(0, 0, -1);
1090 v3s16 direction = getDirHeuristic(directions, g_pos);
1092 while (direction != v3s16(0, 0, 0) && (!retval)) {
1094 if (direction != srcdir) {
1095 PathCost cost = g_pos.getCost(direction);
1098 direction.Y = cost.direction;
1100 v3s16 ipos2 = ipos + direction;
1102 if (!isValidIndex(ipos2)) {
1103 DEBUG_OUT(LVL " Pathfinder: " << PP(ipos2) <<
1104 " out of range, max=" << PP(m_limits.MaxEdge) << std::endl);
1105 direction = getDirHeuristic(directions, g_pos);
1109 PathGridnode &g_pos2 = getIndexElement(ipos2);
1111 if (!g_pos2.valid) {
1112 VERBOSE_TARGET << LVL "Pathfinder: no data for new position: "
1113 << PP(ipos2) << std::endl;
1114 direction = getDirHeuristic(directions, g_pos);
1118 assert(cost.value > 0);
1120 int new_cost = current_cost + cost.value;
1122 // check if there already is a smaller path
1123 if ((m_min_target_distance > 0) &&
1124 (m_min_target_distance < new_cost)) {
1125 DEBUG_OUT(LVL "Pathfinder:"
1126 " already longer than best already found path "
1127 << PP(ipos2) << std::endl);
1131 if ((g_pos2.totalcost < 0) ||
1132 (g_pos2.totalcost > new_cost)) {
1133 DEBUG_OUT(LVL "Pathfinder: updating path at: "<<
1134 PP(ipos2) << " from: " << g_pos2.totalcost << " to "<<
1135 new_cost << " srcdir=" <<
1136 PP(invert(direction))<< std::endl);
1137 if (updateCostHeuristic(ipos2, invert(direction),
1143 DEBUG_OUT(LVL "Pathfinder:"
1144 " already found shorter path to: "
1145 << PP(ipos2) << std::endl);
1149 DEBUG_OUT(LVL "Pathfinder:"
1150 " not moving to invalid direction: "
1151 << PP(direction) << std::endl);
1155 DEBUG_OUT(LVL "Pathfinder:"
1156 " skipping srcdir: "
1157 << PP(direction) << std::endl);
1159 direction = getDirHeuristic(directions, g_pos);
1164 /******************************************************************************/
1165 void Pathfinder::buildPath(std::vector<v3s16> &path, v3s16 pos, int level)
1170 << LVL "Pathfinder: path is too long aborting" << std::endl;
1174 PathGridnode &g_pos = getIndexElement(pos);
1177 << LVL "Pathfinder: invalid next pos detected aborting" << std::endl;
1181 g_pos.is_element = true;
1183 //check if source reached
1185 path.push_back(pos);
1189 buildPath(path, pos + g_pos.sourcedir, level);
1190 path.push_back(pos);
1193 /******************************************************************************/
1194 v3f Pathfinder::tov3f(v3s16 pos)
1196 return v3f(BS * pos.X, BS * pos.Y, BS * pos.Z);
1199 #ifdef PATHFINDER_DEBUG
1201 /******************************************************************************/
1202 void Pathfinder::printCost()
1210 /******************************************************************************/
1211 void Pathfinder::printYdir()
1219 /******************************************************************************/
1220 void Pathfinder::printCost(PathDirections dir)
1222 std::cout << "Cost in direction: " << dirToName(dir) << std::endl;
1223 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1224 std::cout << std::setfill(' ');
1225 for (int y = 0; y < m_max_index_y; y++) {
1227 std::cout << "Level: " << y << std::endl;
1229 std::cout << std::setw(4) << " " << " ";
1230 for (int x = 0; x < m_max_index_x; x++) {
1231 std::cout << std::setw(4) << x;
1233 std::cout << std::endl;
1235 for (int z = 0; z < m_max_index_z; z++) {
1236 std::cout << std::setw(4) << z <<": ";
1237 for (int x = 0; x < m_max_index_x; x++) {
1238 if (getIdxElem(x, y, z).directions[dir].valid)
1239 std::cout << std::setw(4)
1240 << getIdxElem(x, y, z).directions[dir].value;
1242 std::cout << std::setw(4) << "-";
1244 std::cout << std::endl;
1246 std::cout << std::endl;
1250 /******************************************************************************/
1251 void Pathfinder::printYdir(PathDirections dir)
1253 std::cout << "Height difference in direction: " << dirToName(dir) << std::endl;
1254 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1255 std::cout << std::setfill(' ');
1256 for (int y = 0; y < m_max_index_y; y++) {
1258 std::cout << "Level: " << y << std::endl;
1260 std::cout << std::setw(4) << " " << " ";
1261 for (int x = 0; x < m_max_index_x; x++) {
1262 std::cout << std::setw(4) << x;
1264 std::cout << std::endl;
1266 for (int z = 0; z < m_max_index_z; z++) {
1267 std::cout << std::setw(4) << z <<": ";
1268 for (int x = 0; x < m_max_index_x; x++) {
1269 if (getIdxElem(x, y, z).directions[dir].valid)
1270 std::cout << std::setw(4)
1271 << getIdxElem(x, y, z).directions[dir].direction;
1273 std::cout << std::setw(4) << "-";
1275 std::cout << std::endl;
1277 std::cout << std::endl;
1281 /******************************************************************************/
1282 void Pathfinder::printType()
1284 std::cout << "Type of node:" << std::endl;
1285 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1286 std::cout << std::setfill(' ');
1287 for (int y = 0; y < m_max_index_y; y++) {
1289 std::cout << "Level: " << y << std::endl;
1291 std::cout << std::setw(3) << " " << " ";
1292 for (int x = 0; x < m_max_index_x; x++) {
1293 std::cout << std::setw(3) << x;
1295 std::cout << std::endl;
1297 for (int z = 0; z < m_max_index_z; z++) {
1298 std::cout << std::setw(3) << z <<": ";
1299 for (int x = 0; x < m_max_index_x; x++) {
1300 char toshow = getIdxElem(x, y, z).type;
1301 std::cout << std::setw(3) << toshow;
1303 std::cout << std::endl;
1305 std::cout << std::endl;
1307 std::cout << std::endl;
1310 /******************************************************************************/
1311 void Pathfinder::printPathLen()
1313 std::cout << "Pathlen:" << std::endl;
1314 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1315 std::cout << std::setfill(' ');
1316 for (int y = 0; y < m_max_index_y; y++) {
1318 std::cout << "Level: " << y << std::endl;
1320 std::cout << std::setw(3) << " " << " ";
1321 for (int x = 0; x < m_max_index_x; x++) {
1322 std::cout << std::setw(3) << x;
1324 std::cout << std::endl;
1326 for (int z = 0; z < m_max_index_z; z++) {
1327 std::cout << std::setw(3) << z <<": ";
1328 for (int x = 0; x < m_max_index_x; x++) {
1329 std::cout << std::setw(3) << getIdxElem(x, y, z).totalcost;
1331 std::cout << std::endl;
1333 std::cout << std::endl;
1335 std::cout << std::endl;
1338 /******************************************************************************/
1339 std::string Pathfinder::dirToName(PathDirections dir)
1359 /******************************************************************************/
1360 void Pathfinder::printPath(std::vector<v3s16> path)
1362 unsigned int current = 0;
1363 for (std::vector<v3s16>::iterator i = path.begin();
1364 i != path.end(); ++i) {
1365 std::cout << std::setw(3) << current << ":" << PP((*i)) << std::endl;