pathfinder.cpp 39 KB

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