CellNodeBuffer.java 7.9 KB

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