pathfinder.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. /*
  2. Minetest
  3. Copyright (C) 2013 sapier, sapier at gmx dot net
  4. Copyright (C) 2016 est31, <MTest31@outlook.com>
  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.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. /******************************************************************************/
  18. /* Includes */
  19. /******************************************************************************/
  20. #include "pathfinder.h"
  21. #include "map.h"
  22. #include "nodedef.h"
  23. //#define PATHFINDER_DEBUG
  24. //#define PATHFINDER_CALC_TIME
  25. #ifdef PATHFINDER_DEBUG
  26. #include <string>
  27. #endif
  28. #ifdef PATHFINDER_DEBUG
  29. #include <iomanip>
  30. #endif
  31. #ifdef PATHFINDER_CALC_TIME
  32. #include <sys/time.h>
  33. #endif
  34. /******************************************************************************/
  35. /* Typedefs and macros */
  36. /******************************************************************************/
  37. #define LVL "(" << level << ")" <<
  38. #ifdef PATHFINDER_DEBUG
  39. #define DEBUG_OUT(a) std::cout << a
  40. #define INFO_TARGET std::cout
  41. #define VERBOSE_TARGET std::cout
  42. #define ERROR_TARGET std::cout
  43. #else
  44. #define DEBUG_OUT(a) while(0)
  45. #define INFO_TARGET infostream << "Pathfinder: "
  46. #define VERBOSE_TARGET verbosestream << "Pathfinder: "
  47. #define ERROR_TARGET warningstream << "Pathfinder: "
  48. #endif
  49. #define PATHFINDER_MAX_WAYPOINTS 700
  50. /******************************************************************************/
  51. /* Class definitions */
  52. /******************************************************************************/
  53. /** representation of cost in specific direction */
  54. class PathCost {
  55. public:
  56. /** default constructor */
  57. PathCost() = default;
  58. /** copy constructor */
  59. PathCost(const PathCost &b);
  60. /** assignment operator */
  61. PathCost &operator= (const PathCost &b);
  62. bool valid = false; /**< movement is possible */
  63. int value = 0; /**< cost of movement */
  64. int y_change = 0; /**< change of y position of movement */
  65. bool updated = false; /**< this cost has ben calculated */
  66. };
  67. /** representation of a mapnode to be used for pathfinding */
  68. class PathGridnode {
  69. public:
  70. /** default constructor */
  71. PathGridnode() = default;
  72. /** copy constructor */
  73. PathGridnode(const PathGridnode &b);
  74. /**
  75. * assignment operator
  76. * @param b node to copy
  77. */
  78. PathGridnode &operator= (const PathGridnode &b);
  79. /**
  80. * read cost in a specific direction
  81. * @param dir direction of cost to fetch
  82. */
  83. PathCost getCost(v3s16 dir);
  84. /**
  85. * set cost value for movement
  86. * @param dir direction to set cost for
  87. * @cost cost to set
  88. */
  89. void setCost(v3s16 dir, const PathCost &cost);
  90. bool valid = false; /**< node is on surface */
  91. bool target = false; /**< node is target position */
  92. bool source = false; /**< node is stating position */
  93. int totalcost = -1; /**< cost to move here from starting point */
  94. int estimated_cost = -1; /**< totalcost + heuristic cost to end */
  95. v3s16 sourcedir; /**< origin of movement for current cost */
  96. v3s16 pos; /**< real position of node */
  97. PathCost directions[4]; /**< cost in different directions */
  98. bool is_closed = false; /**< for A* search: if true, is in closed list */
  99. bool is_open = false; /**< for A* search: if true, is in open list */
  100. /* debug values */
  101. bool is_element = false; /**< node is element of path detected */
  102. char type = 'u'; /**< Type of pathfinding node.
  103. * u = unknown
  104. * i = invalid
  105. * s = surface (walkable node)
  106. * - = non-walkable node (e.g. air) above surface
  107. * g = other non-walkable node
  108. */
  109. };
  110. class Pathfinder;
  111. class PathfinderCompareHeuristic;
  112. /** Abstract class to manage the map data */
  113. class GridNodeContainer {
  114. public:
  115. virtual PathGridnode &access(v3s16 p)=0;
  116. virtual ~GridNodeContainer() = default;
  117. protected:
  118. Pathfinder *m_pathf;
  119. void initNode(v3s16 ipos, PathGridnode *p_node);
  120. };
  121. class ArrayGridNodeContainer : public GridNodeContainer {
  122. public:
  123. virtual ~ArrayGridNodeContainer() = default;
  124. ArrayGridNodeContainer(Pathfinder *pathf, v3s16 dimensions);
  125. virtual PathGridnode &access(v3s16 p);
  126. private:
  127. int m_x_stride;
  128. int m_y_stride;
  129. std::vector<PathGridnode> m_nodes_array;
  130. };
  131. class MapGridNodeContainer : public GridNodeContainer {
  132. public:
  133. virtual ~MapGridNodeContainer() = default;
  134. MapGridNodeContainer(Pathfinder *pathf);
  135. virtual PathGridnode &access(v3s16 p);
  136. private:
  137. std::map<v3s16, PathGridnode> m_nodes;
  138. };
  139. /** class doing pathfinding */
  140. class Pathfinder {
  141. public:
  142. Pathfinder() = delete;
  143. Pathfinder(Map *map, const NodeDefManager *ndef) : m_map(map), m_ndef(ndef) {}
  144. ~Pathfinder();
  145. /**
  146. * path evaluation function
  147. * @param env environment to look for path
  148. * @param source origin of path
  149. * @param destination end position of path
  150. * @param searchdistance maximum number of nodes to look in each direction
  151. * @param max_jump maximum number of blocks a path may jump up
  152. * @param max_drop maximum number of blocks a path may drop
  153. * @param algo Algorithm to use for finding a path
  154. */
  155. std::vector<v3s16> getPath(v3s16 source,
  156. v3s16 destination,
  157. unsigned int searchdistance,
  158. unsigned int max_jump,
  159. unsigned int max_drop,
  160. PathAlgorithm algo);
  161. private:
  162. /* helper functions */
  163. /**
  164. * transform index pos to mappos
  165. * @param ipos a index position
  166. * @return map position
  167. */
  168. v3s16 getRealPos(v3s16 ipos);
  169. /**
  170. * transform mappos to index pos
  171. * @param pos a real pos
  172. * @return index position
  173. */
  174. v3s16 getIndexPos(v3s16 pos);
  175. /**
  176. * get gridnode at a specific index position
  177. * @param ipos index position
  178. * @return gridnode for index
  179. */
  180. PathGridnode &getIndexElement(v3s16 ipos);
  181. /**
  182. * Get gridnode at a specific index position
  183. * @return gridnode for index
  184. */
  185. PathGridnode &getIdxElem(s16 x, s16 y, s16 z);
  186. /**
  187. * invert a 3D position (change sign of coordinates)
  188. * @param pos 3D position
  189. * @return pos *-1
  190. */
  191. v3s16 invert(v3s16 pos);
  192. /**
  193. * check if a index is within current search area
  194. * @param index position to validate
  195. * @return true/false
  196. */
  197. bool isValidIndex(v3s16 index);
  198. /* algorithm functions */
  199. /**
  200. * calculate 2D Manhattan distance to target
  201. * @param pos position to calc distance
  202. * @return integer distance
  203. */
  204. int getXZManhattanDist(v3s16 pos);
  205. /**
  206. * calculate cost of movement
  207. * @param pos real world position to start movement
  208. * @param dir direction to move to
  209. * @return cost information
  210. */
  211. PathCost calcCost(v3s16 pos, v3s16 dir);
  212. /**
  213. * recursive update whole search areas total cost information
  214. * @param ipos position to check next
  215. * @param srcdir positionc checked last time
  216. * @param total_cost cost of moving to ipos
  217. * @param level current recursion depth
  218. * @return true/false path to destination has been found
  219. */
  220. bool updateAllCosts(v3s16 ipos, v3s16 srcdir, int current_cost, int level);
  221. /**
  222. * try to find a path to destination using a heuristic function
  223. * to estimate distance to target (A* search algorithm)
  224. * @param isource start position (index pos)
  225. * @param idestination end position (index pos)
  226. * @return true/false path to destination has been found
  227. */
  228. bool updateCostHeuristic(v3s16 isource, v3s16 idestination);
  229. /**
  230. * build a vector containing all nodes from destination to source;
  231. * to be called after the node costs have been processed
  232. * @param path vector to add nodes to
  233. * @param ipos initial pos to check (index pos)
  234. * @return true/false path has been fully built
  235. */
  236. bool buildPath(std::vector<v3s16> &path, v3s16 ipos);
  237. /**
  238. * go downwards from a position until some barrier
  239. * is hit.
  240. * @param pos position from which to go downwards
  241. * @param max_down maximum distance to go downwards
  242. * @return new position after movement; if too far down,
  243. * pos is returned
  244. */
  245. v3s16 walkDownwards(v3s16 pos, unsigned int max_down);
  246. /* variables */
  247. int m_max_index_x = 0; /**< max index of search area in x direction */
  248. int m_max_index_y = 0; /**< max index of search area in y direction */
  249. int m_max_index_z = 0; /**< max index of search area in z direction */
  250. int m_maxdrop = 0; /**< maximum number of blocks a path may drop */
  251. int m_maxjump = 0; /**< maximum number of blocks a path may jump */
  252. int m_min_target_distance = 0; /**< current smalest path to target */
  253. bool m_prefetch = true; /**< prefetch cost data */
  254. v3s16 m_start; /**< source position */
  255. v3s16 m_destination; /**< destination position */
  256. core::aabbox3d<s16> m_limits; /**< position limits in real map coordinates */
  257. /** contains all map data already collected and analyzed.
  258. Access it via the getIndexElement/getIdxElem methods. */
  259. friend class GridNodeContainer;
  260. GridNodeContainer *m_nodes_container = nullptr;
  261. Map *m_map = nullptr;
  262. const NodeDefManager *m_ndef = nullptr;
  263. friend class PathfinderCompareHeuristic;
  264. #ifdef PATHFINDER_DEBUG
  265. /**
  266. * print collected cost information
  267. */
  268. void printCost();
  269. /**
  270. * print collected cost information in a specific direction
  271. * @param dir direction to print
  272. */
  273. void printCost(PathDirections dir);
  274. /**
  275. * print type of node as evaluated
  276. */
  277. void printType();
  278. /**
  279. * print pathlenght for all nodes in search area
  280. */
  281. void printPathLen();
  282. /**
  283. * print a path
  284. * @param path path to show
  285. */
  286. void printPath(std::vector<v3s16> path);
  287. /**
  288. * print y direction for all movements
  289. */
  290. void printYdir();
  291. /**
  292. * print y direction for moving in a specific direction
  293. * @param dir direction to show data
  294. */
  295. void printYdir(PathDirections dir);
  296. /**
  297. * helper function to translate a direction to speaking text
  298. * @param dir direction to translate
  299. * @return textual name of direction
  300. */
  301. std::string dirToName(PathDirections dir);
  302. #endif
  303. };
  304. /** Helper class for the open list priority queue in the A* pathfinder
  305. * to sort the pathfinder nodes by cost.
  306. */
  307. class PathfinderCompareHeuristic
  308. {
  309. private:
  310. Pathfinder *myPathfinder;
  311. public:
  312. PathfinderCompareHeuristic(Pathfinder *pf)
  313. {
  314. myPathfinder = pf;
  315. }
  316. bool operator() (v3s16 pos1, v3s16 pos2) {
  317. v3s16 ipos1 = myPathfinder->getIndexPos(pos1);
  318. v3s16 ipos2 = myPathfinder->getIndexPos(pos2);
  319. PathGridnode &g_pos1 = myPathfinder->getIndexElement(ipos1);
  320. PathGridnode &g_pos2 = myPathfinder->getIndexElement(ipos2);
  321. if (!g_pos1.valid)
  322. return false;
  323. if (!g_pos2.valid)
  324. return false;
  325. return g_pos1.estimated_cost > g_pos2.estimated_cost;
  326. }
  327. };
  328. /******************************************************************************/
  329. /* implementation */
  330. /******************************************************************************/
  331. std::vector<v3s16> get_path(Map* map, const NodeDefManager *ndef,
  332. v3s16 source,
  333. v3s16 destination,
  334. unsigned int searchdistance,
  335. unsigned int max_jump,
  336. unsigned int max_drop,
  337. PathAlgorithm algo)
  338. {
  339. return Pathfinder(map, ndef).getPath(source, destination,
  340. searchdistance, max_jump, max_drop, algo);
  341. }
  342. /******************************************************************************/
  343. PathCost::PathCost(const PathCost &b)
  344. {
  345. valid = b.valid;
  346. y_change = b.y_change;
  347. value = b.value;
  348. updated = b.updated;
  349. }
  350. /******************************************************************************/
  351. PathCost &PathCost::operator= (const PathCost &b)
  352. {
  353. valid = b.valid;
  354. y_change = b.y_change;
  355. value = b.value;
  356. updated = b.updated;
  357. return *this;
  358. }
  359. /******************************************************************************/
  360. PathGridnode::PathGridnode(const PathGridnode &b)
  361. : valid(b.valid),
  362. target(b.target),
  363. source(b.source),
  364. totalcost(b.totalcost),
  365. sourcedir(b.sourcedir),
  366. pos(b.pos),
  367. is_element(b.is_element),
  368. type(b.type)
  369. {
  370. directions[DIR_XP] = b.directions[DIR_XP];
  371. directions[DIR_XM] = b.directions[DIR_XM];
  372. directions[DIR_ZP] = b.directions[DIR_ZP];
  373. directions[DIR_ZM] = b.directions[DIR_ZM];
  374. }
  375. /******************************************************************************/
  376. PathGridnode &PathGridnode::operator= (const PathGridnode &b)
  377. {
  378. valid = b.valid;
  379. target = b.target;
  380. source = b.source;
  381. is_element = b.is_element;
  382. totalcost = b.totalcost;
  383. sourcedir = b.sourcedir;
  384. pos = b.pos;
  385. type = b.type;
  386. directions[DIR_XP] = b.directions[DIR_XP];
  387. directions[DIR_XM] = b.directions[DIR_XM];
  388. directions[DIR_ZP] = b.directions[DIR_ZP];
  389. directions[DIR_ZM] = b.directions[DIR_ZM];
  390. return *this;
  391. }
  392. /******************************************************************************/
  393. PathCost PathGridnode::getCost(v3s16 dir)
  394. {
  395. if (dir.X > 0) {
  396. return directions[DIR_XP];
  397. }
  398. if (dir.X < 0) {
  399. return directions[DIR_XM];
  400. }
  401. if (dir.Z > 0) {
  402. return directions[DIR_ZP];
  403. }
  404. if (dir.Z < 0) {
  405. return directions[DIR_ZM];
  406. }
  407. PathCost retval;
  408. return retval;
  409. }
  410. /******************************************************************************/
  411. void PathGridnode::setCost(v3s16 dir, const PathCost &cost)
  412. {
  413. if (dir.X > 0) {
  414. directions[DIR_XP] = cost;
  415. }
  416. if (dir.X < 0) {
  417. directions[DIR_XM] = cost;
  418. }
  419. if (dir.Z > 0) {
  420. directions[DIR_ZP] = cost;
  421. }
  422. if (dir.Z < 0) {
  423. directions[DIR_ZM] = cost;
  424. }
  425. }
  426. void GridNodeContainer::initNode(v3s16 ipos, PathGridnode *p_node)
  427. {
  428. const NodeDefManager *ndef = m_pathf->m_ndef;
  429. PathGridnode &elem = *p_node;
  430. v3s16 realpos = m_pathf->getRealPos(ipos);
  431. MapNode current = m_pathf->m_map->getNode(realpos);
  432. MapNode below = m_pathf->m_map->getNode(realpos + v3s16(0, -1, 0));
  433. if ((current.param0 == CONTENT_IGNORE) ||
  434. (below.param0 == CONTENT_IGNORE)) {
  435. DEBUG_OUT("Pathfinder: " << PP(realpos) <<
  436. " current or below is invalid element" << std::endl);
  437. if (current.param0 == CONTENT_IGNORE) {
  438. elem.type = 'i';
  439. DEBUG_OUT(PP(ipos) << ": " << 'i' << std::endl);
  440. }
  441. return;
  442. }
  443. //don't add anything if it isn't an air node
  444. if (ndef->get(current).walkable || !ndef->get(below).walkable) {
  445. DEBUG_OUT("Pathfinder: " << PP(realpos)
  446. << " not on surface" << std::endl);
  447. if (ndef->get(current).walkable) {
  448. elem.type = 's';
  449. DEBUG_OUT(PP(ipos) << ": " << 's' << std::endl);
  450. } else {
  451. elem.type = '-';
  452. DEBUG_OUT(PP(ipos) << ": " << '-' << std::endl);
  453. }
  454. return;
  455. }
  456. elem.valid = true;
  457. elem.pos = realpos;
  458. elem.type = 'g';
  459. DEBUG_OUT(PP(ipos) << ": " << 'a' << std::endl);
  460. if (m_pathf->m_prefetch) {
  461. elem.directions[DIR_XP] = m_pathf->calcCost(realpos, v3s16( 1, 0, 0));
  462. elem.directions[DIR_XM] = m_pathf->calcCost(realpos, v3s16(-1, 0, 0));
  463. elem.directions[DIR_ZP] = m_pathf->calcCost(realpos, v3s16( 0, 0, 1));
  464. elem.directions[DIR_ZM] = m_pathf->calcCost(realpos, v3s16( 0, 0,-1));
  465. }
  466. }
  467. ArrayGridNodeContainer::ArrayGridNodeContainer(Pathfinder *pathf, v3s16 dimensions) :
  468. m_x_stride(dimensions.Y * dimensions.Z),
  469. m_y_stride(dimensions.Z)
  470. {
  471. m_pathf = pathf;
  472. m_nodes_array.resize(dimensions.X * dimensions.Y * dimensions.Z);
  473. INFO_TARGET << "Pathfinder ArrayGridNodeContainer constructor." << std::endl;
  474. for (int x = 0; x < dimensions.X; x++) {
  475. for (int y = 0; y < dimensions.Y; y++) {
  476. for (int z= 0; z < dimensions.Z; z++) {
  477. v3s16 ipos(x, y, z);
  478. initNode(ipos, &access(ipos));
  479. }
  480. }
  481. }
  482. }
  483. PathGridnode &ArrayGridNodeContainer::access(v3s16 p)
  484. {
  485. return m_nodes_array[p.X * m_x_stride + p.Y * m_y_stride + p.Z];
  486. }
  487. MapGridNodeContainer::MapGridNodeContainer(Pathfinder *pathf)
  488. {
  489. m_pathf = pathf;
  490. }
  491. PathGridnode &MapGridNodeContainer::access(v3s16 p)
  492. {
  493. std::map<v3s16, PathGridnode>::iterator it = m_nodes.find(p);
  494. if (it != m_nodes.end()) {
  495. return it->second;
  496. }
  497. PathGridnode &n = m_nodes[p];
  498. initNode(p, &n);
  499. return n;
  500. }
  501. /******************************************************************************/
  502. std::vector<v3s16> Pathfinder::getPath(v3s16 source,
  503. v3s16 destination,
  504. unsigned int searchdistance,
  505. unsigned int max_jump,
  506. unsigned int max_drop,
  507. PathAlgorithm algo)
  508. {
  509. #ifdef PATHFINDER_CALC_TIME
  510. timespec ts;
  511. clock_gettime(CLOCK_REALTIME, &ts);
  512. #endif
  513. std::vector<v3s16> retval;
  514. //initialization
  515. m_maxjump = max_jump;
  516. m_maxdrop = max_drop;
  517. m_start = source;
  518. m_destination = destination;
  519. m_min_target_distance = -1;
  520. m_prefetch = true;
  521. if (algo == PA_PLAIN_NP) {
  522. m_prefetch = false;
  523. }
  524. //calculate boundaries within we're allowed to search
  525. int min_x = MYMIN(source.X, destination.X);
  526. int max_x = MYMAX(source.X, destination.X);
  527. int min_y = MYMIN(source.Y, destination.Y);
  528. int max_y = MYMAX(source.Y, destination.Y);
  529. int min_z = MYMIN(source.Z, destination.Z);
  530. int max_z = MYMAX(source.Z, destination.Z);
  531. m_limits.MinEdge.X = min_x - searchdistance;
  532. m_limits.MinEdge.Y = min_y - searchdistance;
  533. m_limits.MinEdge.Z = min_z - searchdistance;
  534. m_limits.MaxEdge.X = max_x + searchdistance;
  535. m_limits.MaxEdge.Y = max_y + searchdistance;
  536. m_limits.MaxEdge.Z = max_z + searchdistance;
  537. v3s16 diff = m_limits.MaxEdge - m_limits.MinEdge;
  538. m_max_index_x = diff.X;
  539. m_max_index_y = diff.Y;
  540. m_max_index_z = diff.Z;
  541. delete m_nodes_container;
  542. if (diff.getLength() > 5) {
  543. m_nodes_container = new MapGridNodeContainer(this);
  544. } else {
  545. m_nodes_container = new ArrayGridNodeContainer(this, diff);
  546. }
  547. #ifdef PATHFINDER_DEBUG
  548. printType();
  549. printCost();
  550. printYdir();
  551. #endif
  552. //fail if source or destination is walkable
  553. MapNode node_at_pos = m_map->getNode(destination);
  554. if (m_ndef->get(node_at_pos).walkable) {
  555. VERBOSE_TARGET << "Destination is walkable. " <<
  556. "Pos: " << PP(destination) << std::endl;
  557. return retval;
  558. }
  559. node_at_pos = m_map->getNode(source);
  560. if (m_ndef->get(node_at_pos).walkable) {
  561. VERBOSE_TARGET << "Source is walkable. " <<
  562. "Pos: " << PP(source) << std::endl;
  563. return retval;
  564. }
  565. //If source pos is hovering above air, drop
  566. //to the first walkable node (up to m_maxdrop).
  567. //All algorithms expect the source pos to be *directly* above
  568. //a walkable node.
  569. v3s16 true_source = v3s16(source);
  570. source = walkDownwards(source, m_maxdrop);
  571. //If destination pos is hovering above air, go downwards
  572. //to the first walkable node (up to m_maxjump).
  573. //This means a hovering destination pos could be reached
  574. //by a final upwards jump.
  575. v3s16 true_destination = v3s16(destination);
  576. destination = walkDownwards(destination, m_maxjump);
  577. //validate and mark start and end pos
  578. v3s16 StartIndex = getIndexPos(source);
  579. v3s16 EndIndex = getIndexPos(destination);
  580. PathGridnode &startpos = getIndexElement(StartIndex);
  581. PathGridnode &endpos = getIndexElement(EndIndex);
  582. if (!startpos.valid) {
  583. VERBOSE_TARGET << "Invalid startpos " <<
  584. "Index: " << PP(StartIndex) <<
  585. "Realpos: " << PP(getRealPos(StartIndex)) << std::endl;
  586. return retval;
  587. }
  588. if (!endpos.valid) {
  589. VERBOSE_TARGET << "Invalid stoppos " <<
  590. "Index: " << PP(EndIndex) <<
  591. "Realpos: " << PP(getRealPos(EndIndex)) << std::endl;
  592. return retval;
  593. }
  594. endpos.target = true;
  595. startpos.source = true;
  596. startpos.totalcost = 0;
  597. bool update_cost_retval = false;
  598. //calculate node costs
  599. switch (algo) {
  600. case PA_DIJKSTRA:
  601. update_cost_retval = updateAllCosts(StartIndex, v3s16(0, 0, 0), 0, 0);
  602. break;
  603. case PA_PLAIN_NP:
  604. case PA_PLAIN:
  605. update_cost_retval = updateCostHeuristic(StartIndex, EndIndex);
  606. break;
  607. default:
  608. ERROR_TARGET << "Missing PathAlgorithm" << std::endl;
  609. break;
  610. }
  611. if (update_cost_retval) {
  612. #ifdef PATHFINDER_DEBUG
  613. std::cout << "Path to target found!" << std::endl;
  614. printPathLen();
  615. #endif
  616. //find path
  617. std::vector<v3s16> index_path;
  618. buildPath(index_path, EndIndex);
  619. //Now we have a path of index positions,
  620. //and it's in reverse.
  621. //The "true" start or end position might be missing
  622. //since those have been given special treatment.
  623. #ifdef PATHFINDER_DEBUG
  624. std::cout << "Index path:" << std::endl;
  625. printPath(index_path);
  626. #endif
  627. //from here we'll make the final changes to the path
  628. std::vector<v3s16> full_path;
  629. //calculate required size
  630. int full_path_size = index_path.size();
  631. if (source != true_source) {
  632. full_path_size++;
  633. }
  634. if (destination != true_destination) {
  635. full_path_size++;
  636. }
  637. full_path.reserve(full_path_size);
  638. //manually add true_source to start of path, if needed
  639. if (source != true_source) {
  640. full_path.push_back(true_source);
  641. }
  642. //convert all index positions to "normal" positions and insert
  643. //them into full_path in reverse
  644. std::vector<v3s16>::reverse_iterator rit = index_path.rbegin();
  645. for (; rit != index_path.rend(); ++rit) {
  646. full_path.push_back(getIndexElement(*rit).pos);
  647. }
  648. //manually add true_destination to end of path, if needed
  649. if (destination != true_destination) {
  650. full_path.push_back(true_destination);
  651. }
  652. //Done! We now have a complete path of normal positions.
  653. #ifdef PATHFINDER_DEBUG
  654. std::cout << "Full path:" << std::endl;
  655. printPath(full_path);
  656. #endif
  657. #ifdef PATHFINDER_CALC_TIME
  658. timespec ts2;
  659. clock_gettime(CLOCK_REALTIME, &ts2);
  660. int ms = (ts2.tv_nsec - ts.tv_nsec)/(1000*1000);
  661. int us = ((ts2.tv_nsec - ts.tv_nsec) - (ms*1000*1000))/1000;
  662. int ns = ((ts2.tv_nsec - ts.tv_nsec) - ( (ms*1000*1000) + (us*1000)));
  663. std::cout << "Calculating path took: " << (ts2.tv_sec - ts.tv_sec) <<
  664. "s " << ms << "ms " << us << "us " << ns << "ns " << std::endl;
  665. #endif
  666. return full_path;
  667. }
  668. else {
  669. #ifdef PATHFINDER_DEBUG
  670. printPathLen();
  671. #endif
  672. INFO_TARGET << "No path found" << std::endl;
  673. }
  674. //return
  675. return retval;
  676. }
  677. Pathfinder::~Pathfinder()
  678. {
  679. delete m_nodes_container;
  680. }
  681. /******************************************************************************/
  682. v3s16 Pathfinder::getRealPos(v3s16 ipos)
  683. {
  684. return m_limits.MinEdge + ipos;
  685. }
  686. /******************************************************************************/
  687. PathCost Pathfinder::calcCost(v3s16 pos, v3s16 dir)
  688. {
  689. PathCost retval;
  690. retval.updated = true;
  691. v3s16 pos2 = pos + dir;
  692. //check limits
  693. if (!m_limits.isPointInside(pos2)) {
  694. DEBUG_OUT("Pathfinder: " << PP(pos2) <<
  695. " no cost -> out of limits" << std::endl);
  696. return retval;
  697. }
  698. MapNode node_at_pos2 = m_map->getNode(pos2);
  699. //did we get information about node?
  700. if (node_at_pos2.param0 == CONTENT_IGNORE ) {
  701. VERBOSE_TARGET << "Pathfinder: (1) area at pos: "
  702. << PP(pos2) << " not loaded";
  703. return retval;
  704. }
  705. if (!m_ndef->get(node_at_pos2).walkable) {
  706. MapNode node_below_pos2 =
  707. m_map->getNode(pos2 + v3s16(0, -1, 0));
  708. //did we get information about node?
  709. if (node_below_pos2.param0 == CONTENT_IGNORE ) {
  710. VERBOSE_TARGET << "Pathfinder: (2) area at pos: "
  711. << PP((pos2 + v3s16(0, -1, 0))) << " not loaded";
  712. return retval;
  713. }
  714. //test if the same-height neighbor is suitable
  715. if (m_ndef->get(node_below_pos2).walkable) {
  716. //SUCCESS!
  717. retval.valid = true;
  718. retval.value = 1;
  719. retval.y_change = 0;
  720. DEBUG_OUT("Pathfinder: "<< PP(pos)
  721. << " cost same height found" << std::endl);
  722. }
  723. else {
  724. //test if we can fall a couple of nodes (m_maxdrop)
  725. v3s16 testpos = pos2 + v3s16(0, -1, 0);
  726. MapNode node_at_pos = m_map->getNode(testpos);
  727. while ((node_at_pos.param0 != CONTENT_IGNORE) &&
  728. (!m_ndef->get(node_at_pos).walkable) &&
  729. (testpos.Y > m_limits.MinEdge.Y)) {
  730. testpos += v3s16(0, -1, 0);
  731. node_at_pos = m_map->getNode(testpos);
  732. }
  733. //did we find surface?
  734. if ((testpos.Y >= m_limits.MinEdge.Y) &&
  735. (node_at_pos.param0 != CONTENT_IGNORE) &&
  736. (m_ndef->get(node_at_pos).walkable)) {
  737. if ((pos2.Y - testpos.Y - 1) <= m_maxdrop) {
  738. //SUCCESS!
  739. retval.valid = true;
  740. retval.value = 2;
  741. //difference of y-pos +1 (target node is ABOVE solid node)
  742. retval.y_change = ((testpos.Y - pos2.Y) +1);
  743. DEBUG_OUT("Pathfinder cost below height found" << std::endl);
  744. }
  745. else {
  746. INFO_TARGET << "Pathfinder:"
  747. " distance to surface below too big: "
  748. << (testpos.Y - pos2.Y) << " max: " << m_maxdrop
  749. << std::endl;
  750. }
  751. }
  752. else {
  753. DEBUG_OUT("Pathfinder: no surface below found" << std::endl);
  754. }
  755. }
  756. }
  757. else {
  758. //test if we can jump upwards (m_maxjump)
  759. v3s16 targetpos = pos2; // position for jump target
  760. v3s16 jumppos = pos; // position for checking if jumping space is free
  761. MapNode node_target = m_map->getNode(targetpos);
  762. MapNode node_jump = m_map->getNode(jumppos);
  763. bool headbanger = false; // true if anything blocks jumppath
  764. while ((node_target.param0 != CONTENT_IGNORE) &&
  765. (m_ndef->get(node_target).walkable) &&
  766. (targetpos.Y < m_limits.MaxEdge.Y)) {
  767. //if the jump would hit any solid node, discard
  768. if ((node_jump.param0 == CONTENT_IGNORE) ||
  769. (m_ndef->get(node_jump).walkable)) {
  770. headbanger = true;
  771. break;
  772. }
  773. targetpos += v3s16(0, 1, 0);
  774. jumppos += v3s16(0, 1, 0);
  775. node_target = m_map->getNode(targetpos);
  776. node_jump = m_map->getNode(jumppos);
  777. }
  778. //check headbanger one last time
  779. if ((node_jump.param0 == CONTENT_IGNORE) ||
  780. (m_ndef->get(node_jump).walkable)) {
  781. headbanger = true;
  782. }
  783. //did we find surface without banging our head?
  784. if ((!headbanger) && (targetpos.Y <= m_limits.MaxEdge.Y) &&
  785. (!m_ndef->get(node_target).walkable)) {
  786. if (targetpos.Y - pos2.Y <= m_maxjump) {
  787. //SUCCESS!
  788. retval.valid = true;
  789. retval.value = 2;
  790. retval.y_change = (targetpos.Y - pos2.Y);
  791. DEBUG_OUT("Pathfinder cost above found" << std::endl);
  792. }
  793. else {
  794. DEBUG_OUT("Pathfinder: distance to surface above too big: "
  795. << (targetpos.Y - pos2.Y) << " max: " << m_maxjump
  796. << std::endl);
  797. }
  798. }
  799. else {
  800. DEBUG_OUT("Pathfinder: no surface above found" << std::endl);
  801. }
  802. }
  803. return retval;
  804. }
  805. /******************************************************************************/
  806. v3s16 Pathfinder::getIndexPos(v3s16 pos)
  807. {
  808. return pos - m_limits.MinEdge;
  809. }
  810. /******************************************************************************/
  811. PathGridnode &Pathfinder::getIndexElement(v3s16 ipos)
  812. {
  813. return m_nodes_container->access(ipos);
  814. }
  815. /******************************************************************************/
  816. inline PathGridnode &Pathfinder::getIdxElem(s16 x, s16 y, s16 z)
  817. {
  818. return m_nodes_container->access(v3s16(x,y,z));
  819. }
  820. /******************************************************************************/
  821. bool Pathfinder::isValidIndex(v3s16 index)
  822. {
  823. if ( (index.X < m_max_index_x) &&
  824. (index.Y < m_max_index_y) &&
  825. (index.Z < m_max_index_z) &&
  826. (index.X >= 0) &&
  827. (index.Y >= 0) &&
  828. (index.Z >= 0))
  829. return true;
  830. return false;
  831. }
  832. /******************************************************************************/
  833. v3s16 Pathfinder::invert(v3s16 pos)
  834. {
  835. v3s16 retval = pos;
  836. retval.X *=-1;
  837. retval.Y *=-1;
  838. retval.Z *=-1;
  839. return retval;
  840. }
  841. /******************************************************************************/
  842. bool Pathfinder::updateAllCosts(v3s16 ipos,
  843. v3s16 srcdir,
  844. int current_cost,
  845. int level)
  846. {
  847. PathGridnode &g_pos = getIndexElement(ipos);
  848. g_pos.totalcost = current_cost;
  849. g_pos.sourcedir = srcdir;
  850. level ++;
  851. //check if target has been found
  852. if (g_pos.target) {
  853. m_min_target_distance = current_cost;
  854. DEBUG_OUT(LVL " Pathfinder: target found!" << std::endl);
  855. return true;
  856. }
  857. bool retval = false;
  858. // the 4 cardinal directions
  859. const static v3s16 directions[4] = {
  860. v3s16(1,0, 0),
  861. v3s16(-1,0, 0),
  862. v3s16(0,0, 1),
  863. v3s16(0,0,-1)
  864. };
  865. for (v3s16 direction : directions) {
  866. if (direction != srcdir) {
  867. PathCost cost = g_pos.getCost(direction);
  868. if (cost.valid) {
  869. direction.Y = cost.y_change;
  870. v3s16 ipos2 = ipos + direction;
  871. if (!isValidIndex(ipos2)) {
  872. DEBUG_OUT(LVL " Pathfinder: " << PP(ipos2) <<
  873. " out of range, max=" << PP(m_limits.MaxEdge) << std::endl);
  874. continue;
  875. }
  876. PathGridnode &g_pos2 = getIndexElement(ipos2);
  877. if (!g_pos2.valid) {
  878. VERBOSE_TARGET << LVL "Pathfinder: no data for new position: "
  879. << PP(ipos2) << std::endl;
  880. continue;
  881. }
  882. assert(cost.value > 0);
  883. int new_cost = current_cost + cost.value;
  884. // check if there already is a smaller path
  885. if ((m_min_target_distance > 0) &&
  886. (m_min_target_distance < new_cost)) {
  887. return false;
  888. }
  889. if ((g_pos2.totalcost < 0) ||
  890. (g_pos2.totalcost > new_cost)) {
  891. DEBUG_OUT(LVL "Pathfinder: updating path at: "<<
  892. PP(ipos2) << " from: " << g_pos2.totalcost << " to "<<
  893. new_cost << std::endl);
  894. if (updateAllCosts(ipos2, invert(direction),
  895. new_cost, level)) {
  896. retval = true;
  897. }
  898. }
  899. else {
  900. DEBUG_OUT(LVL "Pathfinder:"
  901. " already found shorter path to: "
  902. << PP(ipos2) << std::endl);
  903. }
  904. }
  905. else {
  906. DEBUG_OUT(LVL "Pathfinder:"
  907. " not moving to invalid direction: "
  908. << PP(directions[i]) << std::endl);
  909. }
  910. }
  911. }
  912. return retval;
  913. }
  914. /******************************************************************************/
  915. int Pathfinder::getXZManhattanDist(v3s16 pos)
  916. {
  917. int min_x = MYMIN(pos.X, m_destination.X);
  918. int max_x = MYMAX(pos.X, m_destination.X);
  919. int min_z = MYMIN(pos.Z, m_destination.Z);
  920. int max_z = MYMAX(pos.Z, m_destination.Z);
  921. return (max_x - min_x) + (max_z - min_z);
  922. }
  923. /******************************************************************************/
  924. bool Pathfinder::updateCostHeuristic(v3s16 isource, v3s16 idestination)
  925. {
  926. // A* search algorithm.
  927. // The open list contains the pathfinder nodes that still need to be
  928. // checked. The priority queue sorts the pathfinder nodes by
  929. // estimated cost, with lowest cost on the top.
  930. std::priority_queue<v3s16, std::vector<v3s16>, PathfinderCompareHeuristic>
  931. openList(PathfinderCompareHeuristic(this));
  932. v3s16 source = getRealPos(isource);
  933. v3s16 destination = getRealPos(idestination);
  934. // initial position
  935. openList.push(source);
  936. // the 4 cardinal directions
  937. const static v3s16 directions[4] = {
  938. v3s16(1,0, 0),
  939. v3s16(-1,0, 0),
  940. v3s16(0,0, 1),
  941. v3s16(0,0,-1)
  942. };
  943. v3s16 current_pos;
  944. PathGridnode& s_pos = getIndexElement(isource);
  945. s_pos.source = true;
  946. s_pos.totalcost = 0;
  947. // estimated cost from start to finish
  948. int cur_manhattan = getXZManhattanDist(destination);
  949. s_pos.estimated_cost = cur_manhattan;
  950. while (!openList.empty()) {
  951. // Pick node with lowest total cost estimate.
  952. // The "cheapest" node is always on top.
  953. current_pos = openList.top();
  954. openList.pop();
  955. v3s16 ipos = getIndexPos(current_pos);
  956. // check if node is inside searchdistance and valid
  957. if (!isValidIndex(ipos)) {
  958. DEBUG_OUT(LVL " Pathfinder: " << PP(current_pos) <<
  959. " out of search distance, max=" << PP(m_limits.MaxEdge) << std::endl);
  960. continue;
  961. }
  962. PathGridnode& g_pos = getIndexElement(ipos);
  963. g_pos.is_closed = true;
  964. g_pos.is_open = false;
  965. if (!g_pos.valid) {
  966. continue;
  967. }
  968. if (current_pos == destination) {
  969. // destination found, terminate
  970. g_pos.target = true;
  971. return true;
  972. }
  973. // for this node, check the 4 cardinal directions
  974. for (v3s16 direction_flat : directions) {
  975. int current_totalcost = g_pos.totalcost;
  976. // get cost from current node to currently checked direction
  977. PathCost cost = g_pos.getCost(direction_flat);
  978. if (!cost.updated) {
  979. cost = calcCost(current_pos, direction_flat);
  980. g_pos.setCost(direction_flat, cost);
  981. }
  982. // update Y component of direction if neighbor requires jump or fall
  983. v3s16 direction_3d = v3s16(direction_flat);
  984. direction_3d.Y = cost.y_change;
  985. // get position of true neighbor
  986. v3s16 neighbor = current_pos + direction_3d;
  987. v3s16 ineighbor = getIndexPos(neighbor);
  988. PathGridnode &n_pos = getIndexElement(ineighbor);
  989. if (cost.valid && !n_pos.is_closed && !n_pos.is_open) {
  990. // heuristic function; estimate cost from neighbor to destination
  991. cur_manhattan = getXZManhattanDist(neighbor);
  992. // add neighbor to open list
  993. n_pos.sourcedir = invert(direction_3d);
  994. n_pos.totalcost = current_totalcost + cost.value;
  995. n_pos.estimated_cost = current_totalcost + cost.value + cur_manhattan;
  996. n_pos.is_open = true;
  997. openList.push(neighbor);
  998. }
  999. }
  1000. }
  1001. // no path found; all possible nodes within searchdistance have been exhausted
  1002. return false;
  1003. }
  1004. /******************************************************************************/
  1005. bool Pathfinder::buildPath(std::vector<v3s16> &path, v3s16 ipos)
  1006. {
  1007. // The cost calculation should have set a source direction for all relevant nodes.
  1008. // To build the path, we go backwards from the destination until we reach the start.
  1009. for(u32 waypoints = 1; waypoints++; ) {
  1010. if (waypoints > PATHFINDER_MAX_WAYPOINTS) {
  1011. ERROR_TARGET << "Pathfinder: buildPath: path is too long (too many waypoints), aborting" << std::endl;
  1012. return false;
  1013. }
  1014. // Insert node into path
  1015. PathGridnode &g_pos = getIndexElement(ipos);
  1016. if (!g_pos.valid) {
  1017. ERROR_TARGET << "Pathfinder: buildPath: invalid next pos detected, aborting" << std::endl;
  1018. return false;
  1019. }
  1020. g_pos.is_element = true;
  1021. path.push_back(ipos);
  1022. if (g_pos.source)
  1023. // start node found, terminate
  1024. return true;
  1025. // go to the node from which the pathfinder came
  1026. ipos += g_pos.sourcedir;
  1027. }
  1028. ERROR_TARGET << "Pathfinder: buildPath: no source node found" << std::endl;
  1029. return false;
  1030. }
  1031. /******************************************************************************/
  1032. v3s16 Pathfinder::walkDownwards(v3s16 pos, unsigned int max_down) {
  1033. if (max_down == 0)
  1034. return pos;
  1035. v3s16 testpos = v3s16(pos);
  1036. MapNode node_at_pos = m_map->getNode(testpos);
  1037. unsigned int down = 0;
  1038. while ((node_at_pos.param0 != CONTENT_IGNORE) &&
  1039. (!m_ndef->get(node_at_pos).walkable) &&
  1040. (testpos.Y > m_limits.MinEdge.Y) &&
  1041. (down <= max_down)) {
  1042. testpos += v3s16(0, -1, 0);
  1043. down++;
  1044. node_at_pos = m_map->getNode(testpos);
  1045. }
  1046. //did we find surface?
  1047. if ((testpos.Y >= m_limits.MinEdge.Y) &&
  1048. (node_at_pos.param0 != CONTENT_IGNORE) &&
  1049. (m_ndef->get(node_at_pos).walkable)) {
  1050. if (down == 0) {
  1051. pos = testpos;
  1052. } else if ((down - 1) <= max_down) {
  1053. //difference of y-pos +1 (target node is ABOVE solid node)
  1054. testpos += v3s16(0, 1, 0);
  1055. pos = testpos;
  1056. }
  1057. else {
  1058. VERBOSE_TARGET << "Pos too far above ground: " <<
  1059. "Index: " << PP(getIndexPos(pos)) <<
  1060. "Realpos: " << PP(getRealPos(getIndexPos(pos))) << std::endl;
  1061. }
  1062. } else {
  1063. DEBUG_OUT("Pathfinder: no surface found below pos" << std::endl);
  1064. }
  1065. return pos;
  1066. }
  1067. #ifdef PATHFINDER_DEBUG
  1068. /******************************************************************************/
  1069. void Pathfinder::printCost()
  1070. {
  1071. printCost(DIR_XP);
  1072. printCost(DIR_XM);
  1073. printCost(DIR_ZP);
  1074. printCost(DIR_ZM);
  1075. }
  1076. /******************************************************************************/
  1077. void Pathfinder::printYdir()
  1078. {
  1079. printYdir(DIR_XP);
  1080. printYdir(DIR_XM);
  1081. printYdir(DIR_ZP);
  1082. printYdir(DIR_ZM);
  1083. }
  1084. /******************************************************************************/
  1085. void Pathfinder::printCost(PathDirections dir)
  1086. {
  1087. std::cout << "Cost in direction: " << dirToName(dir) << std::endl;
  1088. std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
  1089. std::cout << std::setfill(' ');
  1090. for (int y = 0; y < m_max_index_y; y++) {
  1091. std::cout << "Level: " << y << std::endl;
  1092. std::cout << std::setw(4) << " " << " ";
  1093. for (int x = 0; x < m_max_index_x; x++) {
  1094. std::cout << std::setw(4) << x;
  1095. }
  1096. std::cout << std::endl;
  1097. for (int z = 0; z < m_max_index_z; z++) {
  1098. std::cout << std::setw(4) << z <<": ";
  1099. for (int x = 0; x < m_max_index_x; x++) {
  1100. if (getIdxElem(x, y, z).directions[dir].valid)
  1101. std::cout << std::setw(4)
  1102. << getIdxElem(x, y, z).directions[dir].value;
  1103. else
  1104. std::cout << std::setw(4) << "-";
  1105. }
  1106. std::cout << std::endl;
  1107. }
  1108. std::cout << std::endl;
  1109. }
  1110. }
  1111. /******************************************************************************/
  1112. void Pathfinder::printYdir(PathDirections dir)
  1113. {
  1114. std::cout << "Height difference in direction: " << dirToName(dir) << std::endl;
  1115. std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
  1116. std::cout << std::setfill(' ');
  1117. for (int y = 0; y < m_max_index_y; y++) {
  1118. std::cout << "Level: " << y << std::endl;
  1119. std::cout << std::setw(4) << " " << " ";
  1120. for (int x = 0; x < m_max_index_x; x++) {
  1121. std::cout << std::setw(4) << x;
  1122. }
  1123. std::cout << std::endl;
  1124. for (int z = 0; z < m_max_index_z; z++) {
  1125. std::cout << std::setw(4) << z <<": ";
  1126. for (int x = 0; x < m_max_index_x; x++) {
  1127. if (getIdxElem(x, y, z).directions[dir].valid)
  1128. std::cout << std::setw(4)
  1129. << getIdxElem(x, y, z).directions[dir].y_change;
  1130. else
  1131. std::cout << std::setw(4) << "-";
  1132. }
  1133. std::cout << std::endl;
  1134. }
  1135. std::cout << std::endl;
  1136. }
  1137. }
  1138. /******************************************************************************/
  1139. void Pathfinder::printType()
  1140. {
  1141. std::cout << "Type of node:" << std::endl;
  1142. std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
  1143. std::cout << std::setfill(' ');
  1144. for (int y = 0; y < m_max_index_y; y++) {
  1145. std::cout << "Level: " << y << std::endl;
  1146. std::cout << std::setw(3) << " " << " ";
  1147. for (int x = 0; x < m_max_index_x; x++) {
  1148. std::cout << std::setw(3) << x;
  1149. }
  1150. std::cout << std::endl;
  1151. for (int z = 0; z < m_max_index_z; z++) {
  1152. std::cout << std::setw(3) << z <<": ";
  1153. for (int x = 0; x < m_max_index_x; x++) {
  1154. char toshow = getIdxElem(x, y, z).type;
  1155. std::cout << std::setw(3) << toshow;
  1156. }
  1157. std::cout << std::endl;
  1158. }
  1159. std::cout << std::endl;
  1160. }
  1161. std::cout << std::endl;
  1162. }
  1163. /******************************************************************************/
  1164. void Pathfinder::printPathLen()
  1165. {
  1166. std::cout << "Pathlen:" << std::endl;
  1167. std::cout << std::setfill('-') << std::setw(80) << "-" << std::endl;
  1168. std::cout << std::setfill(' ');
  1169. for (int y = 0; y < m_max_index_y; y++) {
  1170. std::cout << "Level: " << y << std::endl;
  1171. std::cout << std::setw(3) << " " << " ";
  1172. for (int x = 0; x < m_max_index_x; x++) {
  1173. std::cout << std::setw(3) << x;
  1174. }
  1175. std::cout << std::endl;
  1176. for (int z = 0; z < m_max_index_z; z++) {
  1177. std::cout << std::setw(3) << z <<": ";
  1178. for (int x = 0; x < m_max_index_x; x++) {
  1179. std::cout << std::setw(3) << getIdxElem(x, y, z).totalcost;
  1180. }
  1181. std::cout << std::endl;
  1182. }
  1183. std::cout << std::endl;
  1184. }
  1185. std::cout << std::endl;
  1186. }
  1187. /******************************************************************************/
  1188. std::string Pathfinder::dirToName(PathDirections dir)
  1189. {
  1190. switch (dir) {
  1191. case DIR_XP:
  1192. return "XP";
  1193. break;
  1194. case DIR_XM:
  1195. return "XM";
  1196. break;
  1197. case DIR_ZP:
  1198. return "ZP";
  1199. break;
  1200. case DIR_ZM:
  1201. return "ZM";
  1202. break;
  1203. default:
  1204. return "UKN";
  1205. }
  1206. }
  1207. /******************************************************************************/
  1208. void Pathfinder::printPath(const std::vector<v3s16> &path)
  1209. {
  1210. unsigned int current = 0;
  1211. for (std::vector<v3s16>::iterator i = path.begin();
  1212. i != path.end(); ++i) {
  1213. std::cout << std::setw(3) << current << ":" << PP((*i)) << std::endl;
  1214. current++;
  1215. }
  1216. }
  1217. #endif