PathFinding.java 5.1 KB

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