PathFinding.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.pathfinding;
  20. import java.util.List;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.gameserver.model.L2World;
  23. import com.l2jserver.gameserver.pathfinding.cellnodes.CellPathFinding;
  24. import com.l2jserver.gameserver.pathfinding.geonodes.GeoPathFinding;
  25. /**
  26. * @author -Nemesiss-
  27. */
  28. public abstract class PathFinding
  29. {
  30. public static PathFinding getInstance()
  31. {
  32. if (Config.PATHFINDING == 1)
  33. {
  34. // Higher Memory Usage, Smaller Cpu Usage
  35. return GeoPathFinding.getInstance();
  36. }
  37. // Cell pathfinding, calculated directly from geodata files
  38. return CellPathFinding.getInstance();
  39. }
  40. public abstract boolean pathNodesExist(short regionoffset);
  41. public abstract List<AbstractNodeLoc> findPath(int x, int y, int z, int tx, int ty, int tz, int instanceId, boolean playable);
  42. // @formatter:off
  43. /*
  44. public List<AbstractNodeLoc> search(AbstractNode start, AbstractNode end, int instanceId)
  45. {
  46. // The simplest grid-based pathfinding.
  47. // Drawback is not having higher cost for diagonal movement (means funny routes)
  48. // Could be optimized e.g. not to calculate backwards as far as forwards.
  49. // List of Visited Nodes
  50. LinkedList<AbstractNode> visited = new LinkedList<AbstractNode>();
  51. // List of Nodes to Visit
  52. LinkedList<AbstractNode> to_visit = new LinkedList<AbstractNode>();
  53. to_visit.add(start);
  54. int i = 0;
  55. while (i < 800)
  56. {
  57. AbstractNode node;
  58. try
  59. {
  60. node = to_visit.removeFirst();
  61. }
  62. catch (Exception e)
  63. {
  64. // No Path found
  65. return null;
  66. }
  67. if (node.equals(end)) //path found!
  68. return constructPath(node, instanceId);
  69. else
  70. {
  71. i++;
  72. visited.add(node);
  73. node.attachNeighbors();
  74. Node[] neighbors = node.getNeighbors();
  75. if (neighbors == null)
  76. continue;
  77. for (Node n : neighbors)
  78. {
  79. if (!visited.contains(n) && !to_visit.contains(n))
  80. {
  81. n.setParent(node);
  82. to_visit.add(n);
  83. }
  84. }
  85. }
  86. }
  87. //No Path found
  88. return null;
  89. }
  90. */
  91. /*
  92. public List<AbstractNodeLoc> searchAStar(Node start, Node end, int instanceId)
  93. {
  94. // Not operational yet?
  95. int start_x = start.getLoc().getX();
  96. int start_y = start.getLoc().getY();
  97. int end_x = end.getLoc().getX();
  98. int end_y = end.getLoc().getY();
  99. //List of Visited Nodes
  100. FastNodeList visited = new FastNodeList(800);//TODO! Add limit to cfg
  101. // List of Nodes to Visit
  102. BinaryNodeHeap to_visit = new BinaryNodeHeap(800);
  103. to_visit.add(start);
  104. int i = 0;
  105. while (i < 800)//TODO! Add limit to cfg
  106. {
  107. AbstractNode node;
  108. try
  109. {
  110. node = to_visit.removeFirst();
  111. }
  112. catch (Exception e)
  113. {
  114. // No Path found
  115. return null;
  116. }
  117. if (node.equals(end)) //path found!
  118. return constructPath(node, instanceId);
  119. else
  120. {
  121. visited.add(node);
  122. node.attachNeighbors();
  123. for (Node n : node.getNeighbors())
  124. {
  125. if (!visited.contains(n) && !to_visit.contains(n))
  126. {
  127. i++;
  128. n.setParent(node);
  129. n.setCost(Math.abs(start_x - n.getLoc().getNodeX()) + Math.abs(start_y - n.getLoc().getNodeY())
  130. + Math.abs(end_x - n.getLoc().getNodeX()) + Math.abs(end_y - n.getLoc().getNodeY()));
  131. to_visit.add(n);
  132. }
  133. }
  134. }
  135. }
  136. //No Path found
  137. return null;
  138. }
  139. */
  140. // @formatter:on
  141. /**
  142. * Convert geodata position to pathnode position
  143. * @param geo_pos
  144. * @return pathnode position
  145. */
  146. public short getNodePos(int geo_pos)
  147. {
  148. return (short) (geo_pos >> 3); // OK?
  149. }
  150. /**
  151. * Convert node position to pathnode block position
  152. * @param node_pos
  153. * @return pathnode block position (0...255)
  154. */
  155. public short getNodeBlock(int node_pos)
  156. {
  157. return (short) (node_pos % 256);
  158. }
  159. public byte getRegionX(int node_pos)
  160. {
  161. return (byte) ((node_pos >> 8) + L2World.TILE_X_MIN);
  162. }
  163. public byte getRegionY(int node_pos)
  164. {
  165. return (byte) ((node_pos >> 8) + L2World.TILE_Y_MIN);
  166. }
  167. public short getRegionOffset(byte rx, byte ry)
  168. {
  169. return (short) ((rx << 5) + ry);
  170. }
  171. /**
  172. * Convert pathnode x to World x position
  173. * @param node_x rx
  174. * @return
  175. */
  176. public int calculateWorldX(short node_x)
  177. {
  178. return L2World.MAP_MIN_X + (node_x * 128) + 48;
  179. }
  180. /**
  181. * Convert pathnode y to World y position
  182. * @param node_y
  183. * @return
  184. */
  185. public int calculateWorldY(short node_y)
  186. {
  187. return L2World.MAP_MIN_Y + (node_y * 128) + 48;
  188. }
  189. public String[] getStat()
  190. {
  191. return null;
  192. }
  193. }