PathFinding.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.pathfinding;
  16. import java.util.LinkedList;
  17. import java.util.List;
  18. import com.l2jserver.Config;
  19. import com.l2jserver.gameserver.GeoData;
  20. import com.l2jserver.gameserver.model.L2World;
  21. import com.l2jserver.gameserver.pathfinding.cellnodes.CellPathFinding;
  22. import com.l2jserver.gameserver.pathfinding.geonodes.GeoPathFinding;
  23. import com.l2jserver.gameserver.pathfinding.utils.BinaryNodeHeap;
  24. import com.l2jserver.gameserver.pathfinding.utils.CellNodeMap;
  25. import com.l2jserver.gameserver.pathfinding.utils.FastNodeList;
  26. import javolution.util.FastList;
  27. /**
  28. *
  29. * @author -Nemesiss-
  30. */
  31. public abstract class PathFinding
  32. {
  33. public static PathFinding getInstance()
  34. {
  35. if (!Config.GEODATA_CELLFINDING)
  36. {
  37. //Higher Memory Usage, Smaller Cpu Usage
  38. return GeoPathFinding.getInstance();
  39. }
  40. else
  41. // Cell pathfinding, calculated directly from geodata files
  42. {
  43. return CellPathFinding.getInstance();
  44. }
  45. }
  46. public abstract boolean pathNodesExist(short regionoffset);
  47. public abstract List<AbstractNodeLoc> findPath(int x, int y, int z, int tx, int ty, int tz, int instanceId);
  48. public abstract Node[] readNeighbors(Node n, int idx);
  49. public List<AbstractNodeLoc> search(Node start, Node end)
  50. {
  51. // The simplest grid-based pathfinding.
  52. // Drawback is not having higher cost for diagonal movement (means funny routes)
  53. // Could be optimized e.g. not to calculate backwards as far as forwards.
  54. // List of Visited Nodes
  55. LinkedList<Node> visited = new LinkedList<Node>();
  56. // List of Nodes to Visit
  57. LinkedList<Node> to_visit = new LinkedList<Node>();
  58. to_visit.add(start);
  59. int i = 0;
  60. while (i < 800)
  61. {
  62. Node node;
  63. try
  64. {
  65. node = to_visit.removeFirst();
  66. }
  67. catch (Exception e)
  68. {
  69. // No Path found
  70. return null;
  71. }
  72. if (node.equals(end)) //path found!
  73. return constructPath(node);
  74. else
  75. {
  76. i++;
  77. visited.add(node);
  78. node.attachNeighbors();
  79. Node[] neighbors = node.getNeighbors();
  80. if (neighbors == null)
  81. continue;
  82. for (Node n : neighbors)
  83. {
  84. if (!visited.contains(n) && !to_visit.contains(n))
  85. {
  86. n.setParent(node);
  87. to_visit.add(n);
  88. }
  89. }
  90. }
  91. }
  92. //No Path found
  93. return null;
  94. }
  95. public List<AbstractNodeLoc> searchByClosest(Node start, Node end)
  96. {
  97. // Note: This is the version for cell-based calculation, harder
  98. // on cpu than from block-based pathnode files. However produces better routes.
  99. // Always continues checking from the closest to target non-blocked
  100. // node from to_visit list. There's extra length in path if needed
  101. // to go backwards/sideways but when moving generally forwards, this is extra fast
  102. // and accurate. And can reach insane distances (try it with 8000 nodes..).
  103. // Minimum required node count would be around 300-400.
  104. // Generally returns a bit (only a bit) more intelligent looking routes than
  105. // the basic version. Not a true distance image (which would increase CPU
  106. // load) level of intelligence though.
  107. // List of Visited Nodes
  108. CellNodeMap known = new CellNodeMap();
  109. // List of Nodes to Visit
  110. LinkedList<Node> to_visit = new LinkedList<Node>();
  111. to_visit.add(start);
  112. known.add(start);
  113. int targetx = end.getLoc().getNodeX();
  114. int targety = end.getLoc().getNodeY();
  115. int targetz = end.getLoc().getZ();
  116. int dx, dy, dz;
  117. boolean added;
  118. int i = 0;
  119. while (i < 3500)
  120. {
  121. Node node;
  122. try
  123. {
  124. node = to_visit.removeFirst();
  125. }
  126. catch (Exception e)
  127. {
  128. // No Path found
  129. return null;
  130. }
  131. i++;
  132. node.attachNeighbors();
  133. if (node.equals(end))
  134. {
  135. //path found! note that node z coordinate is updated only in attach
  136. //to improve performance (alternative: much more checks)
  137. //System.out.println("path found, i:"+i);
  138. return constructPath(node);
  139. }
  140. Node[] neighbors = node.getNeighbors();
  141. if (neighbors == null)
  142. continue;
  143. for (Node n : neighbors)
  144. {
  145. if (!known.contains(n))
  146. {
  147. added = false;
  148. n.setParent(node);
  149. dx = targetx - n.getLoc().getNodeX();
  150. dy = targety - n.getLoc().getNodeY();
  151. dz = targetz - n.getLoc().getZ();
  152. n.setCost(dx * dx + dy * dy + dz / 2 * dz/*+n.getCost()*/);
  153. for (int index = 0; index < to_visit.size(); index++)
  154. {
  155. // supposed to find it quite early..
  156. if (to_visit.get(index).getCost() > n.getCost())
  157. {
  158. to_visit.add(index, n);
  159. added = true;
  160. break;
  161. }
  162. }
  163. if (!added)
  164. to_visit.addLast(n);
  165. known.add(n);
  166. }
  167. }
  168. }
  169. //No Path found
  170. //System.out.println("no path found");
  171. return null;
  172. }
  173. public List<AbstractNodeLoc> searchByClosest2(Node start, Node end)
  174. {
  175. // Always continues checking from the closest to target non-blocked
  176. // node from to_visit list. There's extra length in path if needed
  177. // to go backwards/sideways but when moving generally forwards, this is extra fast
  178. // and accurate. And can reach insane distances (try it with 800 nodes..).
  179. // Minimum required node count would be around 300-400.
  180. // Generally returns a bit (only a bit) more intelligent looking routes than
  181. // the basic version. Not a true distance image (which would increase CPU
  182. // load) level of intelligence though.
  183. // List of Visited Nodes
  184. FastNodeList visited = new FastNodeList(550);
  185. // List of Nodes to Visit
  186. LinkedList<Node> to_visit = new LinkedList<Node>();
  187. to_visit.add(start);
  188. int targetx = end.getLoc().getNodeX();
  189. int targety = end.getLoc().getNodeY();
  190. int dx, dy;
  191. boolean added;
  192. int i = 0;
  193. while (i < 550)
  194. {
  195. Node node;
  196. try
  197. {
  198. node = to_visit.removeFirst();
  199. }
  200. catch (Exception e)
  201. {
  202. // No Path found
  203. return null;
  204. }
  205. if (node.equals(end)) //path found!
  206. return constructPath2(node);
  207. else
  208. {
  209. i++;
  210. visited.add(node);
  211. node.attachNeighbors();
  212. Node[] neighbors = node.getNeighbors();
  213. if (neighbors == null)
  214. continue;
  215. for (Node n : neighbors)
  216. {
  217. if (!visited.containsRev(n) && !to_visit.contains(n))
  218. {
  219. added = false;
  220. n.setParent(node);
  221. dx = targetx - n.getLoc().getNodeX();
  222. dy = targety - n.getLoc().getNodeY();
  223. n.setCost(dx * dx + dy * dy);
  224. for (int index = 0; index < to_visit.size(); index++)
  225. {
  226. // supposed to find it quite early..
  227. if (to_visit.get(index).getCost() > n.getCost())
  228. {
  229. to_visit.add(index, n);
  230. added = true;
  231. break;
  232. }
  233. }
  234. if (!added)
  235. to_visit.addLast(n);
  236. }
  237. }
  238. }
  239. }
  240. //No Path found
  241. return null;
  242. }
  243. public List<AbstractNodeLoc> searchAStar(Node start, Node end)
  244. {
  245. // Not operational yet?
  246. int start_x = start.getLoc().getX();
  247. int start_y = start.getLoc().getY();
  248. int end_x = end.getLoc().getX();
  249. int end_y = end.getLoc().getY();
  250. //List of Visited Nodes
  251. FastNodeList visited = new FastNodeList(800);//TODO! Add limit to cfg
  252. // List of Nodes to Visit
  253. BinaryNodeHeap to_visit = new BinaryNodeHeap(800);
  254. to_visit.add(start);
  255. int i = 0;
  256. while (i < 800)//TODO! Add limit to cfg
  257. {
  258. Node node;
  259. try
  260. {
  261. node = to_visit.removeFirst();
  262. }
  263. catch (Exception e)
  264. {
  265. // No Path found
  266. return null;
  267. }
  268. if (node.equals(end)) //path found!
  269. return constructPath(node);
  270. else
  271. {
  272. visited.add(node);
  273. node.attachNeighbors();
  274. for (Node n : node.getNeighbors())
  275. {
  276. if (!visited.contains(n) && !to_visit.contains(n))
  277. {
  278. i++;
  279. n.setParent(node);
  280. n.setCost(Math.abs(start_x - n.getLoc().getNodeX()) + Math.abs(start_y - n.getLoc().getNodeY())
  281. + Math.abs(end_x - n.getLoc().getNodeX()) + Math.abs(end_y - n.getLoc().getNodeY()));
  282. to_visit.add(n);
  283. }
  284. }
  285. }
  286. }
  287. //No Path found
  288. return null;
  289. }
  290. public List<AbstractNodeLoc> constructPath(Node node)
  291. {
  292. LinkedList<AbstractNodeLoc> path = new LinkedList<AbstractNodeLoc>();
  293. int previousdirectionx = -1000;
  294. int previousdirectiony = -1000;
  295. int directionx;
  296. int directiony;
  297. while (node.getParent() != null)
  298. {
  299. // only add a new route point if moving direction changes
  300. if (node.getParent().getParent() != null // to check and clean diagonal movement
  301. && Math.abs(node.getLoc().getNodeX() - node.getParent().getParent().getLoc().getNodeX()) == 1
  302. && Math.abs(node.getLoc().getNodeY() - node.getParent().getParent().getLoc().getNodeY()) == 1)
  303. {
  304. directionx = node.getLoc().getNodeX() - node.getParent().getParent().getLoc().getNodeX();
  305. directiony = node.getLoc().getNodeY() - node.getParent().getParent().getLoc().getNodeY();
  306. }
  307. else
  308. {
  309. directionx = node.getLoc().getNodeX() - node.getParent().getLoc().getNodeX();
  310. directiony = node.getLoc().getNodeY() - node.getParent().getLoc().getNodeY();
  311. }
  312. if (directionx != previousdirectionx || directiony != previousdirectiony)
  313. {
  314. previousdirectionx = directionx;
  315. previousdirectiony = directiony;
  316. path.addFirst(node.getLoc());
  317. }
  318. node = node.getParent();
  319. }
  320. // then LOS based filtering to reduce the number of route points
  321. if (path.size() > 4)
  322. {
  323. //System.out.println("pathsize:"+path.size());
  324. List<Integer> valueList = new FastList<Integer>();
  325. for (int index = 0; index < path.size() - 3; index = index + 3)
  326. {
  327. //System.out.println("Attempt filter");
  328. if (GeoData.getInstance().canMoveFromToTarget(path.get(index).getX(), path.get(index).getY(), path.get(index).getZ(), path.get(index + 3).getX(), path.get(index + 3).getY(), path.get(index + 3).getZ(), 0))
  329. {
  330. //System.out.println("filtering i:"+(index+1));
  331. valueList.add(index + 1);
  332. valueList.add(index + 2);
  333. }
  334. }
  335. for (int index = valueList.size() - 1; index >= 0; index--)
  336. {
  337. path.remove(valueList.get(index).intValue());
  338. }
  339. //System.out.println("pathsize:"+path.size());
  340. }
  341. return path;
  342. }
  343. public List<AbstractNodeLoc> constructPath2(Node node)
  344. {
  345. LinkedList<AbstractNodeLoc> path = new LinkedList<AbstractNodeLoc>();
  346. int previousdirectionx = -1000;
  347. int previousdirectiony = -1000;
  348. int directionx;
  349. int directiony;
  350. while (node.getParent() != null)
  351. {
  352. // only add a new route point if moving direction changes
  353. directionx = node.getLoc().getNodeX() - node.getParent().getLoc().getNodeX();
  354. directiony = node.getLoc().getNodeY() - node.getParent().getLoc().getNodeY();
  355. if (directionx != previousdirectionx || directiony != previousdirectiony)
  356. {
  357. previousdirectionx = directionx;
  358. previousdirectiony = directiony;
  359. path.addFirst(node.getLoc());
  360. }
  361. node = node.getParent();
  362. }
  363. return path;
  364. }
  365. /**
  366. * Convert geodata position to pathnode position
  367. * @param geo_pos
  368. * @return pathnode position
  369. */
  370. public short getNodePos(int geo_pos)
  371. {
  372. return (short) (geo_pos >> 3); //OK?
  373. }
  374. /**
  375. * Convert node position to pathnode block position
  376. * @param geo_pos
  377. * @return pathnode block position (0...255)
  378. */
  379. public short getNodeBlock(int node_pos)
  380. {
  381. return (short) (node_pos % 256);
  382. }
  383. public byte getRegionX(int node_pos)
  384. {
  385. return (byte) ((node_pos >> 8) + 10);
  386. }
  387. public byte getRegionY(int node_pos)
  388. {
  389. return (byte) ((node_pos >> 8) + 10);
  390. }
  391. public short getRegionOffset(byte rx, byte ry)
  392. {
  393. return (short) ((rx << 5) + ry);
  394. }
  395. /**
  396. * Convert pathnode x to World x position
  397. * @param node_x, rx
  398. * @return
  399. */
  400. public int calculateWorldX(short node_x)
  401. {
  402. return L2World.MAP_MIN_X + node_x * 128 + 48;
  403. }
  404. /**
  405. * Convert pathnode y to World y position
  406. * @param node_y
  407. * @return
  408. */
  409. public int calculateWorldY(short node_y)
  410. {
  411. return L2World.MAP_MIN_Y + node_y * 128 + 48;
  412. }
  413. }