3 Copyright (C) 2013 sapier, sapier at gmx dot net
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 /******************************************************************************/
22 /******************************************************************************/
24 #include "pathfinder.h"
25 #include "environment.h"
29 #ifdef PATHFINDER_DEBUG
32 #ifdef PATHFINDER_CALC_TIME
36 /******************************************************************************/
37 /* Typedefs and macros */
38 /******************************************************************************/
40 //#define PATHFINDER_CALC_TIME
42 /** shortcut to print a 3d pos */
43 #define PPOS(pos) "(" << pos.X << "," << pos.Y << "," << pos.Z << ")"
45 #define LVL "(" << level << ")" <<
47 #ifdef PATHFINDER_DEBUG
48 #define DEBUG_OUT(a) std::cout << a
49 #define INFO_TARGET std::cout
50 #define VERBOSE_TARGET std::cout
51 #define ERROR_TARGET std::cout
53 #define DEBUG_OUT(a) while(0)
54 #define INFO_TARGET infostream << "pathfinder: "
55 #define VERBOSE_TARGET verbosestream << "pathfinder: "
56 #define ERROR_TARGET errorstream << "pathfinder: "
59 /******************************************************************************/
61 /******************************************************************************/
63 std::vector<v3s16> get_Path(ServerEnvironment* env,
66 unsigned int searchdistance,
67 unsigned int max_jump,
68 unsigned int max_drop,
71 pathfinder searchclass;
73 return searchclass.get_Path(env,
75 searchdistance,max_jump,max_drop,algo);
78 /******************************************************************************/
79 path_cost::path_cost()
88 /******************************************************************************/
89 path_cost::path_cost(const path_cost& b) {
91 direction = b.direction;
96 /******************************************************************************/
97 path_cost& path_cost::operator= (const path_cost& b) {
99 direction = b.direction;
106 /******************************************************************************/
107 path_gridnode::path_gridnode()
112 sourcedir(v3s16(0,0,0)),
121 /******************************************************************************/
122 path_gridnode::path_gridnode(const path_gridnode& b)
126 totalcost(b.totalcost),
127 sourcedir(b.sourcedir),
128 surfaces(b.surfaces),
130 is_element(b.is_element),
134 directions[DIR_XP] = b.directions[DIR_XP];
135 directions[DIR_XM] = b.directions[DIR_XM];
136 directions[DIR_ZP] = b.directions[DIR_ZP];
137 directions[DIR_ZM] = b.directions[DIR_ZM];
140 /******************************************************************************/
141 path_gridnode& path_gridnode::operator= (const path_gridnode& b) {
145 is_element = b.is_element;
146 totalcost = b.totalcost;
147 sourcedir = b.sourcedir;
148 surfaces = b.surfaces;
152 directions[DIR_XP] = b.directions[DIR_XP];
153 directions[DIR_XM] = b.directions[DIR_XM];
154 directions[DIR_ZP] = b.directions[DIR_ZP];
155 directions[DIR_ZM] = b.directions[DIR_ZM];
160 /******************************************************************************/
161 path_cost path_gridnode::get_cost(v3s16 dir) {
163 return directions[DIR_XP];
166 return directions[DIR_XM];
169 return directions[DIR_ZP];
172 return directions[DIR_ZM];
178 /******************************************************************************/
179 void path_gridnode::set_cost(v3s16 dir,path_cost cost) {
181 directions[DIR_XP] = cost;
184 directions[DIR_XM] = cost;
187 directions[DIR_ZP] = cost;
190 directions[DIR_ZM] = cost;
194 /******************************************************************************/
195 std::vector<v3s16> pathfinder::get_Path(ServerEnvironment* env,
198 unsigned int searchdistance,
199 unsigned int max_jump,
200 unsigned int max_drop,
202 #ifdef PATHFINDER_CALC_TIME
204 clock_gettime(CLOCK_REALTIME, &ts);
206 std::vector<v3s16> retval;
210 ERROR_TARGET << "missing environment pointer" << std::endl;
214 m_searchdistance = searchdistance;
216 m_maxjump = max_jump;
217 m_maxdrop = max_drop;
219 m_destination = destination;
220 m_min_target_distance = -1;
223 if (algo == A_PLAIN_NP) {
227 int min_x = MYMIN(source.X,destination.X);
228 int max_x = MYMAX(source.X,destination.X);
230 int min_y = MYMIN(source.Y,destination.Y);
231 int max_y = MYMAX(source.Y,destination.Y);
233 int min_z = MYMIN(source.Z,destination.Z);
234 int max_z = MYMAX(source.Z,destination.Z);
236 m_limits.X.min = min_x - searchdistance;
237 m_limits.X.max = max_x + searchdistance;
238 m_limits.Y.min = min_y - searchdistance;
239 m_limits.Y.max = max_y + searchdistance;
240 m_limits.Z.min = min_z - searchdistance;
241 m_limits.Z.max = max_z + searchdistance;
243 m_max_index_x = m_limits.X.max - m_limits.X.min;
244 m_max_index_y = m_limits.Y.max - m_limits.Y.min;
245 m_max_index_z = m_limits.Z.max - m_limits.Z.min;
248 if (!build_costmap()) {
249 ERROR_TARGET << "failed to build costmap" << std::endl;
252 #ifdef PATHFINDER_DEBUG
258 //validate and mark start and end pos
259 v3s16 StartIndex = getIndexPos(source);
260 v3s16 EndIndex = getIndexPos(destination);
262 path_gridnode& startpos = getIndexElement(StartIndex);
263 path_gridnode& endpos = getIndexElement(EndIndex);
265 if (!startpos.valid) {
266 VERBOSE_TARGET << "invalid startpos" <<
267 "Index: " << PPOS(StartIndex) <<
268 "Realpos: " << PPOS(getRealPos(StartIndex)) << std::endl;
272 VERBOSE_TARGET << "invalid stoppos" <<
273 "Index: " << PPOS(EndIndex) <<
274 "Realpos: " << PPOS(getRealPos(EndIndex)) << std::endl;
278 endpos.target = true;
279 startpos.source = true;
280 startpos.totalcost = 0;
282 bool update_cost_retval = false;
286 update_cost_retval = update_all_costs(StartIndex,v3s16(0,0,0),0,0);
290 update_cost_retval = update_cost_heuristic(StartIndex,v3s16(0,0,0),0,0);
293 ERROR_TARGET << "missing algorithm"<< std::endl;
297 if (update_cost_retval) {
299 #ifdef PATHFINDER_DEBUG
300 std::cout << "Path to target found!" << std::endl;
305 std::vector<v3s16> path;
306 build_path(path,EndIndex,0);
308 #ifdef PATHFINDER_DEBUG
309 std::cout << "Full index path:" << std::endl;
314 std::vector<v3s16> optimized_path;
316 std::vector<v3s16>::iterator startpos = path.begin();
317 optimized_path.push_back(source);
319 for (std::vector<v3s16>::iterator i = path.begin();
320 i != path.end(); i++) {
321 if (!m_env->line_of_sight(
322 tov3f(getIndexElement(*startpos).pos),
323 tov3f(getIndexElement(*i).pos))) {
324 optimized_path.push_back(getIndexElement(*(i-1)).pos);
329 optimized_path.push_back(destination);
331 #ifdef PATHFINDER_DEBUG
332 std::cout << "Optimized path:" << std::endl;
333 print_path(optimized_path);
335 #ifdef PATHFINDER_CALC_TIME
337 clock_gettime(CLOCK_REALTIME, &ts2);
339 int ms = (ts2.tv_nsec - ts.tv_nsec)/(1000*1000);
340 int us = ((ts2.tv_nsec - ts.tv_nsec) - (ms*1000*1000))/1000;
341 int ns = ((ts2.tv_nsec - ts.tv_nsec) - ( (ms*1000*1000) + (us*1000)));
344 std::cout << "Calculating path took: " << (ts2.tv_sec - ts.tv_sec) <<
345 "s " << ms << "ms " << us << "us " << ns << "ns " << std::endl;
347 return optimized_path;
350 #ifdef PATHFINDER_DEBUG
353 ERROR_TARGET << "failed to update cost map"<< std::endl;
361 /******************************************************************************/
362 pathfinder::pathfinder() :
369 m_min_target_distance(0),
372 m_destination(0,0,0),
380 /******************************************************************************/
381 v3s16 pathfinder::getRealPos(v3s16 ipos) {
385 retval.X += m_limits.X.min;
386 retval.Y += m_limits.Y.min;
387 retval.Z += m_limits.Z.min;
392 /******************************************************************************/
393 bool pathfinder::build_costmap()
395 INFO_TARGET << "Pathfinder build costmap: (" << m_limits.X.min << ","
396 << m_limits.Z.min << ") ("
397 << m_limits.X.max << ","
398 << m_limits.Z.max << ")"
400 m_data.resize(m_max_index_x);
401 for (int x = 0; x < m_max_index_x; x++) {
402 m_data[x].resize(m_max_index_z);
403 for (int z = 0; z < m_max_index_z; z++) {
404 m_data[x][z].resize(m_max_index_y);
407 for (int y = 0; y < m_max_index_y; y++) {
410 v3s16 realpos = getRealPos(ipos);
412 MapNode current = m_env->getMap().getNodeNoEx(realpos);
413 MapNode below = m_env->getMap().getNodeNoEx(realpos + v3s16(0,-1,0));
416 if ((current.param0 == CONTENT_IGNORE) ||
417 (below.param0 == CONTENT_IGNORE)) {
418 DEBUG_OUT("Pathfinder: " << PPOS(realpos) <<
419 " current or below is invalid element" << std::endl);
420 if (current.param0 == CONTENT_IGNORE) {
421 m_data[x][z][y].type = 'i';
422 DEBUG_OUT(x << "," << y << "," << z << ": " << 'i' << std::endl);
427 //don't add anything if it isn't an air node
428 if ((current.param0 != CONTENT_AIR) ||
429 (below.param0 == CONTENT_AIR )) {
430 DEBUG_OUT("Pathfinder: " << PPOS(realpos)
431 << " not on surface" << std::endl);
432 if (current.param0 != CONTENT_AIR) {
433 m_data[x][z][y].type = 's';
434 DEBUG_OUT(x << "," << y << "," << z << ": " << 's' << std::endl);
437 m_data[x][z][y].type = '-';
438 DEBUG_OUT(x << "," << y << "," << z << ": " << '-' << std::endl);
445 m_data[x][z][y].valid = true;
446 m_data[x][z][y].pos = realpos;
447 m_data[x][z][y].type = 'g';
448 DEBUG_OUT(x << "," << y << "," << z << ": " << 'a' << std::endl);
451 m_data[x][z][y].directions[DIR_XP] =
452 calc_cost(realpos,v3s16( 1,0, 0));
453 m_data[x][z][y].directions[DIR_XM] =
454 calc_cost(realpos,v3s16(-1,0, 0));
455 m_data[x][z][y].directions[DIR_ZP] =
456 calc_cost(realpos,v3s16( 0,0, 1));
457 m_data[x][z][y].directions[DIR_ZM] =
458 calc_cost(realpos,v3s16( 0,0,-1));
463 if (surfaces >= 1 ) {
464 for (int y = 0; y < m_max_index_y; y++) {
465 if (m_data[x][z][y].valid) {
466 m_data[x][z][y].surfaces = surfaces;
475 /******************************************************************************/
476 path_cost pathfinder::calc_cost(v3s16 pos,v3s16 dir) {
479 retval.updated = true;
481 v3s16 pos2 = pos + dir;
484 if ( (pos2.X < m_limits.X.min) ||
485 (pos2.X >= m_limits.X.max) ||
486 (pos2.Z < m_limits.Z.min) ||
487 (pos2.Z >= m_limits.Z.max)) {
488 DEBUG_OUT("Pathfinder: " << PPOS(pos2) <<
489 " no cost -> out of limits" << std::endl);
493 MapNode node_at_pos2 = m_env->getMap().getNodeNoEx(pos2);
495 //did we get information about node?
496 if (node_at_pos2.param0 == CONTENT_IGNORE ) {
497 VERBOSE_TARGET << "Pathfinder: (1) area at pos: "
498 << PPOS(pos2) << " not loaded";
502 if (node_at_pos2.param0 == CONTENT_AIR) {
503 MapNode node_below_pos2 =
504 m_env->getMap().getNodeNoEx(pos2 + v3s16(0,-1,0));
506 //did we get information about node?
507 if (node_below_pos2.param0 == CONTENT_IGNORE ) {
508 VERBOSE_TARGET << "Pathfinder: (2) area at pos: "
509 << PPOS((pos2 + v3s16(0,-1,0))) << " not loaded";
513 if (node_below_pos2.param0 != CONTENT_AIR) {
516 retval.direction = 0;
517 DEBUG_OUT("Pathfinder: "<< PPOS(pos)
518 << " cost same height found" << std::endl);
521 v3s16 testpos = pos2 - v3s16(0,-1,0);
522 MapNode node_at_pos = m_env->getMap().getNodeNoEx(testpos);
524 while ((node_at_pos.param0 != CONTENT_IGNORE) &&
525 (node_at_pos.param0 == CONTENT_AIR) &&
526 (testpos.Y > m_limits.Y.min)) {
527 testpos += v3s16(0,-1,0);
528 node_at_pos = m_env->getMap().getNodeNoEx(testpos);
531 //did we find surface?
532 if ((testpos.Y >= m_limits.Y.min) &&
533 (node_at_pos.param0 != CONTENT_IGNORE) &&
534 (node_at_pos.param0 != CONTENT_AIR)) {
535 if (((pos2.Y - testpos.Y)*-1) <= m_maxdrop) {
538 //difference of y-pos +1 (target node is ABOVE solid node)
539 retval.direction = ((testpos.Y - pos2.Y) +1);
540 DEBUG_OUT("Pathfinder cost below height found" << std::endl);
543 INFO_TARGET << "Pathfinder:"
544 " distance to surface below to big: "
545 << (testpos.Y - pos2.Y) << " max: " << m_maxdrop
550 DEBUG_OUT("Pathfinder: no surface below found" << std::endl);
555 v3s16 testpos = pos2;
556 MapNode node_at_pos = m_env->getMap().getNodeNoEx(testpos);
558 while ((node_at_pos.param0 != CONTENT_IGNORE) &&
559 (node_at_pos.param0 != CONTENT_AIR) &&
560 (testpos.Y < m_limits.Y.max)) {
561 testpos += v3s16(0,1,0);
562 node_at_pos = m_env->getMap().getNodeNoEx(testpos);
565 //did we find surface?
566 if ((testpos.Y <= m_limits.Y.max) &&
567 (node_at_pos.param0 == CONTENT_AIR)) {
569 if (testpos.Y - pos2.Y <= m_maxjump) {
572 retval.direction = (testpos.Y - pos2.Y);
573 DEBUG_OUT("Pathfinder cost above found" << std::endl);
576 DEBUG_OUT("Pathfinder: distance to surface above to big: "
577 << (testpos.Y - pos2.Y) << " max: " << m_maxjump
582 DEBUG_OUT("Pathfinder: no surface above found" << std::endl);
588 /******************************************************************************/
589 v3s16 pathfinder::getIndexPos(v3s16 pos) {
592 retval.X -= m_limits.X.min;
593 retval.Y -= m_limits.Y.min;
594 retval.Z -= m_limits.Z.min;
599 /******************************************************************************/
600 path_gridnode& pathfinder::getIndexElement(v3s16 ipos) {
601 return m_data[ipos.X][ipos.Z][ipos.Y];
604 /******************************************************************************/
605 bool pathfinder::valid_index(v3s16 index) {
606 if ( (index.X < m_max_index_x) &&
607 (index.Y < m_max_index_y) &&
608 (index.Z < m_max_index_z) &&
617 /******************************************************************************/
618 v3s16 pathfinder::invert(v3s16 pos) {
628 /******************************************************************************/
629 bool pathfinder::update_all_costs( v3s16 ipos,
634 path_gridnode& g_pos = getIndexElement(ipos);
635 g_pos.totalcost = current_cost;
636 g_pos.sourcedir = srcdir;
640 //check if target has been found
642 m_min_target_distance = current_cost;
643 DEBUG_OUT(LVL " Pathfinder: target found!" << std::endl);
649 std::vector<v3s16> directions;
651 directions.push_back(v3s16( 1,0, 0));
652 directions.push_back(v3s16(-1,0, 0));
653 directions.push_back(v3s16( 0,0, 1));
654 directions.push_back(v3s16( 0,0,-1));
656 for (unsigned int i=0; i < directions.size(); i++) {
657 if (directions[i] != srcdir) {
658 path_cost cost = g_pos.get_cost(directions[i]);
661 directions[i].Y = cost.direction;
663 v3s16 ipos2 = ipos + directions[i];
665 if (!valid_index(ipos2)) {
666 DEBUG_OUT(LVL " Pathfinder: " << PPOS(ipos2) <<
667 " out of range (" << m_limits.X.max << "," <<
668 m_limits.Y.max << "," << m_limits.Z.max
673 path_gridnode& g_pos2 = getIndexElement(ipos2);
676 VERBOSE_TARGET << LVL "Pathfinder: no data for new position: "
677 << PPOS(ipos2) << std::endl;
681 assert(cost.value > 0);
683 int new_cost = current_cost + cost.value;
685 // check if there already is a smaller path
686 if ((m_min_target_distance > 0) &&
687 (m_min_target_distance < new_cost)) {
691 if ((g_pos2.totalcost < 0) ||
692 (g_pos2.totalcost > new_cost)) {
693 DEBUG_OUT(LVL "Pathfinder: updating path at: "<<
694 PPOS(ipos2) << " from: " << g_pos2.totalcost << " to "<<
695 new_cost << std::endl);
696 if (update_all_costs(ipos2,invert(directions[i]),
702 DEBUG_OUT(LVL "Pathfinder:"
703 " already found shorter path to: "
704 << PPOS(ipos2) << std::endl);
708 DEBUG_OUT(LVL "Pathfinder:"
709 " not moving to invalid direction: "
710 << PPOS(directions[i]) << std::endl);
717 /******************************************************************************/
718 int pathfinder::get_manhattandistance(v3s16 pos) {
720 int min_x = MYMIN(pos.X,m_destination.X);
721 int max_x = MYMAX(pos.X,m_destination.X);
722 int min_z = MYMIN(pos.Z,m_destination.Z);
723 int max_z = MYMAX(pos.Z,m_destination.Z);
725 return (max_x - min_x) + (max_z - min_z);
728 /******************************************************************************/
729 v3s16 pathfinder::get_dir_heuristic(std::vector<v3s16>& directions,path_gridnode& g_pos) {
731 v3s16 retdir = v3s16(0,0,0);
732 v3s16 srcpos = g_pos.pos;
733 DEBUG_OUT("Pathfinder: remaining dirs at beginning:"
734 << directions.size() << std::endl);
736 for (std::vector<v3s16>::iterator iter = directions.begin();
737 iter != directions.end();
740 v3s16 pos1 = v3s16(srcpos.X + iter->X,0,srcpos.Z+iter->Z);
742 int cur_manhattan = get_manhattandistance(pos1);
743 path_cost cost = g_pos.get_cost(*iter);
746 cost = calc_cost(g_pos.pos,*iter);
747 g_pos.set_cost(*iter,cost);
751 int score = cost.value + cur_manhattan;
753 if ((minscore < 0)|| (score < minscore)) {
760 if (retdir != v3s16(0,0,0)) {
761 for (std::vector<v3s16>::iterator iter = directions.begin();
762 iter != directions.end();
764 if(*iter == retdir) {
765 DEBUG_OUT("Pathfinder: removing return direction" << std::endl);
766 directions.erase(iter);
772 DEBUG_OUT("Pathfinder: didn't find any valid direction clearing"
776 DEBUG_OUT("Pathfinder: remaining dirs at end:" << directions.size()
781 /******************************************************************************/
782 bool pathfinder::update_cost_heuristic( v3s16 ipos,
787 path_gridnode& g_pos = getIndexElement(ipos);
788 g_pos.totalcost = current_cost;
789 g_pos.sourcedir = srcdir;
793 //check if target has been found
795 m_min_target_distance = current_cost;
796 DEBUG_OUT(LVL " Pathfinder: target found!" << std::endl);
802 std::vector<v3s16> directions;
804 directions.push_back(v3s16( 1,0, 0));
805 directions.push_back(v3s16(-1,0, 0));
806 directions.push_back(v3s16( 0,0, 1));
807 directions.push_back(v3s16( 0,0,-1));
809 v3s16 direction = get_dir_heuristic(directions,g_pos);
811 while (direction != v3s16(0,0,0) && (!retval)) {
813 if (direction != srcdir) {
814 path_cost cost = g_pos.get_cost(direction);
817 direction.Y = cost.direction;
819 v3s16 ipos2 = ipos + direction;
821 if (!valid_index(ipos2)) {
822 DEBUG_OUT(LVL " Pathfinder: " << PPOS(ipos2) <<
823 " out of range (" << m_limits.X.max << "," <<
824 m_limits.Y.max << "," << m_limits.Z.max
826 direction = get_dir_heuristic(directions,g_pos);
830 path_gridnode& g_pos2 = getIndexElement(ipos2);
833 VERBOSE_TARGET << LVL "Pathfinder: no data for new position: "
834 << PPOS(ipos2) << std::endl;
835 direction = get_dir_heuristic(directions,g_pos);
839 assert(cost.value > 0);
841 int new_cost = current_cost + cost.value;
843 // check if there already is a smaller path
844 if ((m_min_target_distance > 0) &&
845 (m_min_target_distance < new_cost)) {
846 DEBUG_OUT(LVL "Pathfinder:"
847 " already longer than best already found path "
848 << PPOS(ipos2) << std::endl);
852 if ((g_pos2.totalcost < 0) ||
853 (g_pos2.totalcost > new_cost)) {
854 DEBUG_OUT(LVL "Pathfinder: updating path at: "<<
855 PPOS(ipos2) << " from: " << g_pos2.totalcost << " to "<<
856 new_cost << " srcdir=" <<
857 PPOS(invert(direction))<< std::endl);
858 if (update_cost_heuristic(ipos2,invert(direction),
864 DEBUG_OUT(LVL "Pathfinder:"
865 " already found shorter path to: "
866 << PPOS(ipos2) << std::endl);
870 DEBUG_OUT(LVL "Pathfinder:"
871 " not moving to invalid direction: "
872 << PPOS(direction) << std::endl);
876 DEBUG_OUT(LVL "Pathfinder:"
878 << PPOS(direction) << std::endl);
880 direction = get_dir_heuristic(directions,g_pos);
885 /******************************************************************************/
886 void pathfinder::build_path(std::vector<v3s16>& path,v3s16 pos, int level) {
890 << LVL "Pathfinder: path is too long aborting" << std::endl;
894 path_gridnode& g_pos = getIndexElement(pos);
897 << LVL "Pathfinder: invalid next pos detected aborting" << std::endl;
901 g_pos.is_element = true;
903 //check if source reached
909 build_path(path,pos + g_pos.sourcedir,level);
913 /******************************************************************************/
914 v3f pathfinder::tov3f(v3s16 pos) {
915 return v3f(BS*pos.X,BS*pos.Y,BS*pos.Z);
918 #ifdef PATHFINDER_DEBUG
920 /******************************************************************************/
921 void pathfinder::print_cost() {
928 /******************************************************************************/
929 void pathfinder::print_ydir() {
936 /******************************************************************************/
937 void pathfinder::print_cost(path_directions dir) {
939 std::cout << "Cost in direction: " << dir_to_name(dir) << std::endl;
940 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
941 std::cout << std::setfill(' ');
942 for (int y = 0; y < m_max_index_y; y++) {
944 std::cout << "Level: " << y << std::endl;
946 std::cout << std::setw(4) << " " << " ";
947 for (int x = 0; x < m_max_index_x; x++) {
948 std::cout << std::setw(4) << x;
950 std::cout << std::endl;
952 for (int z = 0; z < m_max_index_z; z++) {
953 std::cout << std::setw(4) << z <<": ";
954 for (int x = 0; x < m_max_index_x; x++) {
955 if (m_data[x][z][y].directions[dir].valid)
956 std::cout << std::setw(4)
957 << m_data[x][z][y].directions[dir].value;
959 std::cout << std::setw(4) << "-";
961 std::cout << std::endl;
963 std::cout << std::endl;
967 /******************************************************************************/
968 void pathfinder::print_ydir(path_directions dir) {
970 std::cout << "Height difference in direction: " << dir_to_name(dir) << std::endl;
971 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
972 std::cout << std::setfill(' ');
973 for (int y = 0; y < m_max_index_y; y++) {
975 std::cout << "Level: " << y << std::endl;
977 std::cout << std::setw(4) << " " << " ";
978 for (int x = 0; x < m_max_index_x; x++) {
979 std::cout << std::setw(4) << x;
981 std::cout << std::endl;
983 for (int z = 0; z < m_max_index_z; z++) {
984 std::cout << std::setw(4) << z <<": ";
985 for (int x = 0; x < m_max_index_x; x++) {
986 if (m_data[x][z][y].directions[dir].valid)
987 std::cout << std::setw(4)
988 << m_data[x][z][y].directions[dir].direction;
990 std::cout << std::setw(4) << "-";
992 std::cout << std::endl;
994 std::cout << std::endl;
998 /******************************************************************************/
999 void pathfinder::print_type() {
1000 std::cout << "Type of node:" << std::endl;
1001 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1002 std::cout << std::setfill(' ');
1003 for (int y = 0; y < m_max_index_y; y++) {
1005 std::cout << "Level: " << y << std::endl;
1007 std::cout << std::setw(3) << " " << " ";
1008 for (int x = 0; x < m_max_index_x; x++) {
1009 std::cout << std::setw(3) << x;
1011 std::cout << std::endl;
1013 for (int z = 0; z < m_max_index_z; z++) {
1014 std::cout << std::setw(3) << z <<": ";
1015 for (int x = 0; x < m_max_index_x; x++) {
1016 char toshow = m_data[x][z][y].type;
1017 std::cout << std::setw(3) << toshow;
1019 std::cout << std::endl;
1021 std::cout << std::endl;
1023 std::cout << std::endl;
1026 /******************************************************************************/
1027 void pathfinder::print_pathlen() {
1028 std::cout << "Pathlen:" << std::endl;
1029 std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
1030 std::cout << std::setfill(' ');
1031 for (int y = 0; y < m_max_index_y; y++) {
1033 std::cout << "Level: " << y << std::endl;
1035 std::cout << std::setw(3) << " " << " ";
1036 for (int x = 0; x < m_max_index_x; x++) {
1037 std::cout << std::setw(3) << x;
1039 std::cout << std::endl;
1041 for (int z = 0; z < m_max_index_z; z++) {
1042 std::cout << std::setw(3) << z <<": ";
1043 for (int x = 0; x < m_max_index_x; x++) {
1044 std::cout << std::setw(3) << m_data[x][z][y].totalcost;
1046 std::cout << std::endl;
1048 std::cout << std::endl;
1050 std::cout << std::endl;
1053 /******************************************************************************/
1054 std::string pathfinder::dir_to_name(path_directions dir) {
1073 /******************************************************************************/
1074 void pathfinder::print_path(std::vector<v3s16> path) {
1076 unsigned int current = 0;
1077 for (std::vector<v3s16>::iterator i = path.begin();
1078 i != path.end(); i++) {
1079 std::cout << std::setw(3) << current << ":" << PPOS((*i)) << std::endl;