PathFinding.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 net.sf.l2j.gameserver.pathfinding;
  16. import java.util.LinkedList;
  17. import java.util.List;
  18. import net.sf.l2j.gameserver.model.L2World;
  19. import net.sf.l2j.gameserver.pathfinding.geonodes.GeoPathFinding;
  20. import net.sf.l2j.gameserver.pathfinding.utils.BinaryNodeHeap;
  21. import net.sf.l2j.gameserver.pathfinding.utils.FastNodeList;
  22. /**
  23. *
  24. * @author -Nemesiss-
  25. */
  26. public abstract class PathFinding
  27. {
  28. private static PathFinding _instance;
  29. public static PathFinding getInstance()
  30. {
  31. if (_instance == null)
  32. {
  33. if (true /*Config.GEODATA_PATHFINDING*/)
  34. {
  35. //Smaler Memory Usage, Higher Cpu Usage (CalculatedOnTheFly)
  36. return GeoPathFinding.getInstance();
  37. }
  38. else // WORLD_PATHFINDING
  39. {
  40. //Higher Memoru Usage, Lower Cpu Usage (PreCalculated)
  41. }
  42. }
  43. return _instance;
  44. }
  45. public abstract boolean pathNodesExist(short regionoffset);
  46. public abstract List<AbstractNodeLoc> findPath(int gx, int gy, short z, int gtx, int gtz, short tz);
  47. public abstract Node[] readNeighbors(short node_x,short node_y, int idx);
  48. public List<AbstractNodeLoc> search(Node start, Node end)
  49. {
  50. // The simplest grid-based pathfinding.
  51. // Drawback is not having higher cost for diagonal movement (means funny routes)
  52. // Could be optimized e.g. not to calculate backwards as far as forwards.
  53. // List of Visited Nodes
  54. LinkedList<Node> visited = new LinkedList<Node>();
  55. // List of Nodes to Visit
  56. LinkedList<Node> to_visit = new LinkedList<Node>();
  57. to_visit.add(start);
  58. int i = 0;
  59. while (i < 800)//TODO! Add limit to cfg. Worst case max distance is 1810..
  60. {
  61. Node node;
  62. try
  63. {
  64. node = to_visit.removeFirst();
  65. }
  66. catch (Exception e)
  67. {
  68. // No Path found
  69. return null;
  70. }
  71. if (node.equals(end)) //path found!
  72. return constructPath(node);
  73. else
  74. {
  75. i++;
  76. visited.add(node);
  77. node.attacheNeighbors();
  78. Node[] neighbors = node.getNeighbors();
  79. if (neighbors == null) continue;
  80. for (Node n : neighbors)
  81. {
  82. if (!visited.contains(n) && !to_visit.contains(n))
  83. {
  84. n.setParent(node);
  85. to_visit.add(n);
  86. }
  87. }
  88. }
  89. }
  90. //No Path found
  91. return null;
  92. }
  93. public List<AbstractNodeLoc> searchByClosest(Node start, Node end)
  94. {
  95. // Always continues checking from the closest to target non-blocked
  96. // node from to_visit list. There's extra length in path if needed
  97. // to go backwards/sideways but when moving generally forwards, this is extra fast
  98. // and accurate. And can reach insane distances (try it with 800 nodes..).
  99. // Minimum required node count would be around 300-400.
  100. // Generally returns a bit (only a bit) more intelligent looking routes than
  101. // the basic version. Not a true distance image (which would increase CPU
  102. // load) level of intelligence though.
  103. // List of Visited Nodes
  104. FastNodeList visited = new FastNodeList(550);
  105. // List of Nodes to Visit
  106. LinkedList<Node> to_visit = new LinkedList<Node>();
  107. to_visit.add(start);
  108. short targetx = end.getLoc().getNodeX();
  109. short targety = end.getLoc().getNodeY();
  110. int dx, dy;
  111. boolean added;
  112. int i = 0;
  113. while (i < 550)
  114. {
  115. Node node;
  116. try
  117. {
  118. node = to_visit.removeFirst();
  119. }
  120. catch (Exception e)
  121. {
  122. // No Path found
  123. return null;
  124. }
  125. if (node.equals(end)) //path found!
  126. return constructPath(node);
  127. else
  128. {
  129. i++;
  130. visited.add(node);
  131. node.attacheNeighbors();
  132. Node[] neighbors = node.getNeighbors();
  133. if (neighbors == null) continue;
  134. for (Node n : neighbors)
  135. {
  136. if (!visited.containsRev(n) && !to_visit.contains(n))
  137. {
  138. added = false;
  139. n.setParent(node);
  140. dx = targetx - n.getLoc().getNodeX();
  141. dy = targety - n.getLoc().getNodeY();
  142. n.setCost(dx*dx+dy*dy);
  143. for (int index = 0; index < to_visit.size(); index++)
  144. {
  145. // supposed to find it quite early..
  146. if (to_visit.get(index).getCost() > n.getCost())
  147. {
  148. to_visit.add(index, n);
  149. added = true;
  150. break;
  151. }
  152. }
  153. if (!added) to_visit.addLast(n);
  154. }
  155. }
  156. }
  157. }
  158. //No Path found
  159. return null;
  160. }
  161. public List<AbstractNodeLoc> searchAStar(Node start, Node end)
  162. {
  163. // Not operational yet?
  164. int start_x = start.getLoc().getX();
  165. int start_y = start.getLoc().getY();
  166. int end_x = end.getLoc().getX();
  167. int end_y = end.getLoc().getY();
  168. //List of Visited Nodes
  169. FastNodeList visited = new FastNodeList(800);//TODO! Add limit to cfg
  170. // List of Nodes to Visit
  171. BinaryNodeHeap to_visit = new BinaryNodeHeap(800);
  172. to_visit.add(start);
  173. int i = 0;
  174. while (i < 800)//TODO! Add limit to cfg
  175. {
  176. Node node;
  177. try
  178. {
  179. node = to_visit.removeFirst();
  180. }
  181. catch (Exception e)
  182. {
  183. // No Path found
  184. return null;
  185. }
  186. if (node.equals(end)) //path found!
  187. return constructPath(node);
  188. else
  189. {
  190. visited.add(node);
  191. node.attacheNeighbors();
  192. for (Node n : node.getNeighbors())
  193. {
  194. if (!visited.contains(n) && !to_visit.contains(n))
  195. {
  196. i++;
  197. n.setParent(node);
  198. n.setCost(Math.abs(start_x - n.getLoc().getNodeX())+Math.abs(start_y - n.getLoc().getNodeY())
  199. +Math.abs(end_x - n.getLoc().getNodeX())+Math.abs(end_y - n.getLoc().getNodeY()));
  200. to_visit.add(n);
  201. }
  202. }
  203. }
  204. }
  205. //No Path found
  206. return null;
  207. }
  208. public List<AbstractNodeLoc> constructPath(Node node)
  209. {
  210. LinkedList<AbstractNodeLoc> path = new LinkedList<AbstractNodeLoc>();
  211. int previousdirectionx = -1000;
  212. int previousdirectiony = -1000;
  213. int directionx;
  214. int directiony;
  215. while (node.getParent() != null)
  216. {
  217. // only add a new route point if moving direction changes
  218. directionx = node.getLoc().getNodeX() - node.getParent().getLoc().getNodeX();
  219. directiony = node.getLoc().getNodeY() - node.getParent().getLoc().getNodeY();
  220. if(directionx != previousdirectionx || directiony != previousdirectiony)
  221. {
  222. previousdirectionx = directionx;
  223. previousdirectiony = directiony;
  224. path.addFirst(node.getLoc());
  225. }
  226. node = node.getParent();
  227. }
  228. return path;
  229. }
  230. /**
  231. * Convert geodata position to pathnode position
  232. * @param geo_pos
  233. * @return pathnode position
  234. */
  235. public short getNodePos(int geo_pos)
  236. {
  237. return (short)(geo_pos >> 3); //OK?
  238. }
  239. /**
  240. * Convert node position to pathnode block position
  241. * @param geo_pos
  242. * @return pathnode block position (0...255)
  243. */
  244. public short getNodeBlock(int node_pos)
  245. {
  246. return (short)(node_pos % 256);
  247. }
  248. public byte getRegionX(int node_pos)
  249. {
  250. return (byte)((node_pos >> 8) + 16);
  251. }
  252. public byte getRegionY(int node_pos)
  253. {
  254. return (byte)((node_pos >> 8) + 10);
  255. }
  256. public short getRegionOffset(byte rx, byte ry)
  257. {
  258. return (short)((rx << 5) + ry);
  259. }
  260. /**
  261. * Convert pathnode x to World x position
  262. * @param node_x, rx
  263. * @return
  264. */
  265. public int calculateWorldX(short node_x)
  266. {
  267. return L2World.MAP_MIN_X + node_x * 128 + 48 ;
  268. }
  269. /**
  270. * Convert pathnode y to World y position
  271. * @param node_y
  272. * @return
  273. */
  274. public int calculateWorldY(short node_y)
  275. {
  276. return L2World.MAP_MIN_Y + node_y * 128 + 48 ;
  277. }
  278. }