CellNodeBuffer.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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.cellnodes;
  16. import java.util.concurrent.locks.ReentrantLock;
  17. import javolution.util.FastList;
  18. import com.l2jserver.Config;
  19. /**
  20. *
  21. * @author DS
  22. * Credits to Diamond
  23. *
  24. */
  25. public class CellNodeBuffer
  26. {
  27. private static final byte EAST = 1;
  28. private static final byte WEST = 2;
  29. private static final byte SOUTH = 4;
  30. private static final byte NORTH = 8;
  31. private static final byte NSWE_ALL = 15;
  32. private static final byte NSWE_NONE = 0;
  33. private static final int MAX_ITERATIONS = 3500;
  34. private final ReentrantLock _lock = new ReentrantLock();
  35. private final int _mapSize;
  36. private final CellNode[][] _buffer;
  37. private int _baseX = 0;
  38. private int _baseY = 0;
  39. private int _targetX = 0;
  40. private int _targetY = 0;
  41. private short _targetZ = 0;
  42. private long _timeStamp = 0;
  43. private long _lastElapsedTime = 0;
  44. private CellNode _current = null;
  45. public CellNodeBuffer(int size)
  46. {
  47. _mapSize = size;
  48. _buffer = new CellNode[_mapSize][_mapSize];
  49. }
  50. public final boolean lock()
  51. {
  52. return _lock.tryLock();
  53. }
  54. public final CellNode findPath(int x, int y, short z, int tx, int ty, short tz)
  55. {
  56. _timeStamp = System.currentTimeMillis();
  57. _baseX = x + (tx - x - _mapSize) / 2; // middle of the line (x,y) - (tx,ty)
  58. _baseY = y + (ty - y - _mapSize) / 2; // will be in the center of the buffer
  59. _targetX = tx;
  60. _targetY = ty;
  61. _targetZ = tz;
  62. _current = getNode(x, y, z);
  63. _current.setCost(getCost(x, y, z, Config.HIGH_WEIGHT));
  64. for (int count = 0; count < MAX_ITERATIONS; count++)
  65. {
  66. if (_current.getLoc().getNodeX() == _targetX
  67. && _current.getLoc().getNodeY() == _targetY
  68. && Math.abs(_current.getLoc().getZ() - _targetZ) < 64)
  69. return _current; // found
  70. getNeighbors();
  71. if (_current.getNext() == null)
  72. return null; // no more ways
  73. _current = _current.getNext();
  74. }
  75. return null;
  76. }
  77. public final void free()
  78. {
  79. _current = null;
  80. CellNode node;
  81. for (int i = 0; i < _mapSize; i++)
  82. for (int j = 0; j < _mapSize; j++)
  83. {
  84. node = _buffer[i][j];
  85. if (node != null)
  86. node.free();
  87. }
  88. _lock.unlock();
  89. _lastElapsedTime = System.currentTimeMillis() - _timeStamp;
  90. }
  91. public final long getElapsedTime()
  92. {
  93. return _lastElapsedTime;
  94. }
  95. public final FastList<CellNode> debugPath()
  96. {
  97. FastList<CellNode> result = new FastList<CellNode>();
  98. for (CellNode n = _current; n.getParent() != null; n = (CellNode)n.getParent())
  99. {
  100. result.add(n);
  101. n.setCost(-n.getCost());
  102. }
  103. for (int i = 0; i < _mapSize; i++)
  104. for (int j = 0; j < _mapSize; j++)
  105. {
  106. CellNode n = _buffer[i][j];
  107. if (n == null || !n.isInUse() || n.getCost() <= 0)
  108. continue;
  109. result.add(n);
  110. }
  111. return result;
  112. }
  113. private final void getNeighbors()
  114. {
  115. final short NSWE = ((NodeLoc)_current.getLoc()).getNSWE();
  116. if (NSWE == NSWE_NONE)
  117. return;
  118. final int x = _current.getLoc().getNodeX();
  119. final int y = _current.getLoc().getNodeY();
  120. final short z = _current.getLoc().getZ();
  121. CellNode nodeE = null;
  122. CellNode nodeS = null;
  123. CellNode nodeW = null;
  124. CellNode nodeN = null;
  125. // East
  126. if ((NSWE & EAST) != 0)
  127. nodeE = addNode(x + 1, y, z, false);
  128. // South
  129. if ((NSWE & SOUTH) != 0)
  130. nodeS = addNode(x, y + 1, z, false);
  131. // West
  132. if ((NSWE & WEST) != 0)
  133. nodeW = addNode(x - 1, y, z, false);
  134. // North
  135. if ((NSWE & NORTH) != 0)
  136. nodeN = addNode(x, y - 1, z, false);
  137. if (Config.ADVANCED_DIAGONAL_STRATEGY)
  138. {
  139. // SouthEast
  140. if (nodeE != null && nodeS != null)
  141. {
  142. if ((((NodeLoc)nodeE.getLoc()).getNSWE() & SOUTH) != 0
  143. && (((NodeLoc)nodeS.getLoc()).getNSWE() & EAST) != 0)
  144. addNode(x + 1, y + 1, z, true);
  145. }
  146. // SouthWest
  147. if (nodeS != null && nodeW != null)
  148. {
  149. if ((((NodeLoc)nodeW.getLoc()).getNSWE() & SOUTH) != 0
  150. && (((NodeLoc)nodeS.getLoc()).getNSWE() & WEST) != 0)
  151. addNode(x - 1, y + 1, z, true);
  152. }
  153. // NorthEast
  154. if (nodeN != null && nodeE != null)
  155. {
  156. if ((((NodeLoc)nodeE.getLoc()).getNSWE() & NORTH) != 0
  157. && (((NodeLoc)nodeN.getLoc()).getNSWE() & EAST) != 0)
  158. addNode(x + 1, y - 1, z, true);
  159. }
  160. // NorthWest
  161. if (nodeN != null && nodeW != null)
  162. {
  163. if ((((NodeLoc)nodeW.getLoc()).getNSWE() & NORTH) != 0
  164. && (((NodeLoc)nodeN.getLoc()).getNSWE() & WEST) != 0)
  165. addNode(x - 1, y - 1, z, true);
  166. }
  167. }
  168. }
  169. private final CellNode getNode(int x, int y, short z)
  170. {
  171. final int aX = x - _baseX;
  172. if (aX < 0 || aX >= _mapSize)
  173. return null;
  174. final int aY = y - _baseY;
  175. if (aY < 0 || aY >= _mapSize)
  176. return null;
  177. CellNode result = _buffer[aX][aY];
  178. if (result == null)
  179. {
  180. result = new CellNode(new NodeLoc(x, y, z));
  181. _buffer[aX][aY] = result;
  182. }
  183. else if (!result.isInUse())
  184. {
  185. result.setInUse();
  186. // reinit node if needed
  187. if (result.getLoc() != null)
  188. ((NodeLoc)result.getLoc()).set(x, y, z);
  189. else
  190. result.setLoc(new NodeLoc(x, y, z));
  191. }
  192. return result;
  193. }
  194. private final CellNode addNode(int x, int y, short z, boolean diagonal)
  195. {
  196. CellNode newNode = getNode(x, y, z);
  197. if (newNode == null)
  198. return null;
  199. if (newNode.getCost() >= 0)
  200. return newNode;
  201. final short geoZ = newNode.getLoc().getZ();
  202. final int stepZ = Math.abs(geoZ - _current.getLoc().getZ());
  203. float weight = diagonal ? Config.DIAGONAL_WEIGHT : Config.LOW_WEIGHT;
  204. if (((NodeLoc)newNode.getLoc()).getNSWE() != NSWE_ALL || stepZ > 16)
  205. weight = Config.HIGH_WEIGHT;
  206. else
  207. {
  208. if (isHighWeight(x + 1, y, geoZ))
  209. weight = Config.MEDIUM_WEIGHT;
  210. else if (isHighWeight(x - 1, y, geoZ))
  211. weight = Config.MEDIUM_WEIGHT;
  212. else if (isHighWeight(x, y + 1, geoZ))
  213. weight = Config.MEDIUM_WEIGHT;
  214. else if (isHighWeight(x, y -1, geoZ))
  215. weight = Config.MEDIUM_WEIGHT;
  216. }
  217. newNode.setParent(_current);
  218. newNode.setCost(getCost(x, y, geoZ, weight));
  219. CellNode node = _current;
  220. int count = 0;
  221. while (node.getNext() != null && count < MAX_ITERATIONS * 4)
  222. {
  223. count++;
  224. if (node.getNext().getCost() > newNode.getCost())
  225. {
  226. // insert node into a chain
  227. newNode.setNext(node.getNext());
  228. break;
  229. }
  230. else
  231. node = node.getNext();
  232. }
  233. if (count == MAX_ITERATIONS * 4)
  234. System.err.println("Pathfinding: too long loop detected, cost:" + newNode.getCost());
  235. node.setNext(newNode); // add last
  236. return newNode;
  237. }
  238. private final boolean isHighWeight(int x, int y, short z)
  239. {
  240. final CellNode result = getNode(x, y, z);
  241. if (result == null)
  242. return true;
  243. if (((NodeLoc)result.getLoc()).getNSWE() != NSWE_ALL)
  244. return true;
  245. if (Math.abs(result.getLoc().getZ() - z) > 16)
  246. return true;
  247. return false;
  248. }
  249. private final double getCost(int x, int y, short z, float weight)
  250. {
  251. final int dX = x - _targetX;
  252. final int dY = y - _targetY;
  253. final int dZ = z - _targetZ;
  254. // Math.abs(dx) + Math.abs(dy) + Math.abs(dz) / 16
  255. double result = Math.sqrt(dX * dX + dY * dY + dZ * dZ /256);
  256. if (result > weight)
  257. result += weight;
  258. if (result > Float.MAX_VALUE)
  259. result = Float.MAX_VALUE;
  260. return result;
  261. }
  262. }