PathFinding.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.List;
  17. import com.l2jserver.Config;
  18. import com.l2jserver.gameserver.model.L2World;
  19. import com.l2jserver.gameserver.pathfinding.cellnodes.CellPathFinding;
  20. import com.l2jserver.gameserver.pathfinding.geonodes.GeoPathFinding;
  21. /**
  22. * @author -Nemesiss-
  23. */
  24. public abstract class PathFinding
  25. {
  26. public static PathFinding getInstance()
  27. {
  28. if (!Config.GEODATA_CELLFINDING)
  29. {
  30. // Higher Memory Usage, Smaller Cpu Usage
  31. return GeoPathFinding.getInstance();
  32. }
  33. // Cell pathfinding, calculated directly from geodata files
  34. return CellPathFinding.getInstance();
  35. }
  36. public abstract boolean pathNodesExist(short regionoffset);
  37. public abstract List<AbstractNodeLoc> findPath(int x, int y, int z, int tx, int ty, int tz, int instanceId, boolean playable);
  38. // @formatter:off
  39. /*
  40. public List<AbstractNodeLoc> search(AbstractNode start, AbstractNode end, int instanceId)
  41. {
  42. // The simplest grid-based pathfinding.
  43. // Drawback is not having higher cost for diagonal movement (means funny routes)
  44. // Could be optimized e.g. not to calculate backwards as far as forwards.
  45. // List of Visited Nodes
  46. LinkedList<AbstractNode> visited = new LinkedList<AbstractNode>();
  47. // List of Nodes to Visit
  48. LinkedList<AbstractNode> to_visit = new LinkedList<AbstractNode>();
  49. to_visit.add(start);
  50. int i = 0;
  51. while (i < 800)
  52. {
  53. AbstractNode node;
  54. try
  55. {
  56. node = to_visit.removeFirst();
  57. }
  58. catch (Exception e)
  59. {
  60. // No Path found
  61. return null;
  62. }
  63. if (node.equals(end)) //path found!
  64. return constructPath(node, instanceId);
  65. else
  66. {
  67. i++;
  68. visited.add(node);
  69. node.attachNeighbors();
  70. Node[] neighbors = node.getNeighbors();
  71. if (neighbors == null)
  72. continue;
  73. for (Node n : neighbors)
  74. {
  75. if (!visited.contains(n) && !to_visit.contains(n))
  76. {
  77. n.setParent(node);
  78. to_visit.add(n);
  79. }
  80. }
  81. }
  82. }
  83. //No Path found
  84. return null;
  85. }
  86. */
  87. /*
  88. public List<AbstractNodeLoc> searchAStar(Node start, Node end, int instanceId)
  89. {
  90. // Not operational yet?
  91. int start_x = start.getLoc().getX();
  92. int start_y = start.getLoc().getY();
  93. int end_x = end.getLoc().getX();
  94. int end_y = end.getLoc().getY();
  95. //List of Visited Nodes
  96. FastNodeList visited = new FastNodeList(800);//TODO! Add limit to cfg
  97. // List of Nodes to Visit
  98. BinaryNodeHeap to_visit = new BinaryNodeHeap(800);
  99. to_visit.add(start);
  100. int i = 0;
  101. while (i < 800)//TODO! Add limit to cfg
  102. {
  103. AbstractNode node;
  104. try
  105. {
  106. node = to_visit.removeFirst();
  107. }
  108. catch (Exception e)
  109. {
  110. // No Path found
  111. return null;
  112. }
  113. if (node.equals(end)) //path found!
  114. return constructPath(node, instanceId);
  115. else
  116. {
  117. visited.add(node);
  118. node.attachNeighbors();
  119. for (Node n : node.getNeighbors())
  120. {
  121. if (!visited.contains(n) && !to_visit.contains(n))
  122. {
  123. i++;
  124. n.setParent(node);
  125. n.setCost(Math.abs(start_x - n.getLoc().getNodeX()) + Math.abs(start_y - n.getLoc().getNodeY())
  126. + Math.abs(end_x - n.getLoc().getNodeX()) + Math.abs(end_y - n.getLoc().getNodeY()));
  127. to_visit.add(n);
  128. }
  129. }
  130. }
  131. }
  132. //No Path found
  133. return null;
  134. }
  135. */
  136. // @formatter:on
  137. /**
  138. * Convert geodata position to pathnode position
  139. * @param geo_pos
  140. * @return pathnode position
  141. */
  142. public short getNodePos(int geo_pos)
  143. {
  144. return (short) (geo_pos >> 3); // OK?
  145. }
  146. /**
  147. * Convert node position to pathnode block position
  148. * @param node_pos
  149. * @return pathnode block position (0...255)
  150. */
  151. public short getNodeBlock(int node_pos)
  152. {
  153. return (short) (node_pos % 256);
  154. }
  155. public byte getRegionX(int node_pos)
  156. {
  157. return (byte) ((node_pos >> 8) + Config.WORLD_X_MIN);
  158. }
  159. public byte getRegionY(int node_pos)
  160. {
  161. return (byte) ((node_pos >> 8) + Config.WORLD_Y_MIN);
  162. }
  163. public short getRegionOffset(byte rx, byte ry)
  164. {
  165. return (short) ((rx << 5) + ry);
  166. }
  167. /**
  168. * Convert pathnode x to World x position
  169. * @param node_x rx
  170. * @return
  171. */
  172. public int calculateWorldX(short node_x)
  173. {
  174. return L2World.MAP_MIN_X + (node_x * 128) + 48;
  175. }
  176. /**
  177. * Convert pathnode y to World y position
  178. * @param node_y
  179. * @return
  180. */
  181. public int calculateWorldY(short node_y)
  182. {
  183. return L2World.MAP_MIN_Y + (node_y * 128) + 48;
  184. }
  185. public String[] getStat()
  186. {
  187. return null;
  188. }
  189. }