PathFinding.java 5.1 KB

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