CellNodeBuffer.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.pathfinding.cellnodes;
  20. import java.util.concurrent.locks.ReentrantLock;
  21. import javolution.util.FastList;
  22. import com.l2jserver.Config;
  23. /**
  24. * @author DS Credits to Diamond
  25. */
  26. public class CellNodeBuffer
  27. {
  28. private static final byte EAST = 1;
  29. private static final byte WEST = 2;
  30. private static final byte SOUTH = 4;
  31. private static final byte NORTH = 8;
  32. private static final byte NSWE_ALL = 15;
  33. private static final byte NSWE_NONE = 0;
  34. private static final int MAX_ITERATIONS = 3500;
  35. private final ReentrantLock _lock = new ReentrantLock();
  36. private final int _mapSize;
  37. private final CellNode[][] _buffer;
  38. private int _baseX = 0;
  39. private int _baseY = 0;
  40. private int _targetX = 0;
  41. private int _targetY = 0;
  42. private short _targetZ = 0;
  43. private long _timeStamp = 0;
  44. private long _lastElapsedTime = 0;
  45. private CellNode _current = null;
  46. public CellNodeBuffer(int size)
  47. {
  48. _mapSize = size;
  49. _buffer = new CellNode[_mapSize][_mapSize];
  50. }
  51. public final boolean lock()
  52. {
  53. return _lock.tryLock();
  54. }
  55. public final CellNode findPath(int x, int y, short z, int tx, int ty, short tz)
  56. {
  57. _timeStamp = System.currentTimeMillis();
  58. _baseX = x + ((tx - x - _mapSize) / 2); // middle of the line (x,y) - (tx,ty)
  59. _baseY = y + ((ty - y - _mapSize) / 2); // will be in the center of the buffer
  60. _targetX = tx;
  61. _targetY = ty;
  62. _targetZ = tz;
  63. _current = getNode(x, y, z);
  64. _current.setCost(getCost(x, y, z, Config.HIGH_WEIGHT));
  65. for (int count = 0; count < MAX_ITERATIONS; count++)
  66. {
  67. if ((_current.getLoc().getNodeX() == _targetX) && (_current.getLoc().getNodeY() == _targetY) && (Math.abs(_current.getLoc().getZ() - _targetZ) < 64))
  68. {
  69. return _current; // found
  70. }
  71. getNeighbors();
  72. if (_current.getNext() == null)
  73. {
  74. return null; // no more ways
  75. }
  76. _current = _current.getNext();
  77. }
  78. return null;
  79. }
  80. public final void free()
  81. {
  82. _current = null;
  83. CellNode node;
  84. for (int i = 0; i < _mapSize; i++)
  85. {
  86. for (int j = 0; j < _mapSize; j++)
  87. {
  88. node = _buffer[i][j];
  89. if (node != null)
  90. {
  91. node.free();
  92. }
  93. }
  94. }
  95. _lock.unlock();
  96. _lastElapsedTime = System.currentTimeMillis() - _timeStamp;
  97. }
  98. public final long getElapsedTime()
  99. {
  100. return _lastElapsedTime;
  101. }
  102. public final FastList<CellNode> debugPath()
  103. {
  104. FastList<CellNode> result = new FastList<>();
  105. for (CellNode n = _current; n.getParent() != null; n = (CellNode) n.getParent())
  106. {
  107. result.add(n);
  108. n.setCost(-n.getCost());
  109. }
  110. for (int i = 0; i < _mapSize; i++)
  111. {
  112. for (int j = 0; j < _mapSize; j++)
  113. {
  114. CellNode n = _buffer[i][j];
  115. if ((n == null) || !n.isInUse() || (n.getCost() <= 0))
  116. {
  117. continue;
  118. }
  119. result.add(n);
  120. }
  121. }
  122. return result;
  123. }
  124. private final void getNeighbors()
  125. {
  126. final short NSWE = ((NodeLoc) _current.getLoc()).getNSWE();
  127. if (NSWE == NSWE_NONE)
  128. {
  129. return;
  130. }
  131. final int x = _current.getLoc().getNodeX();
  132. final int y = _current.getLoc().getNodeY();
  133. final short z = _current.getLoc().getZ();
  134. CellNode nodeE = null;
  135. CellNode nodeS = null;
  136. CellNode nodeW = null;
  137. CellNode nodeN = null;
  138. // East
  139. if ((NSWE & EAST) != 0)
  140. {
  141. nodeE = addNode(x + 1, y, z, false);
  142. }
  143. // South
  144. if ((NSWE & SOUTH) != 0)
  145. {
  146. nodeS = addNode(x, y + 1, z, false);
  147. }
  148. // West
  149. if ((NSWE & WEST) != 0)
  150. {
  151. nodeW = addNode(x - 1, y, z, false);
  152. }
  153. // North
  154. if ((NSWE & NORTH) != 0)
  155. {
  156. nodeN = addNode(x, y - 1, z, false);
  157. }
  158. if (Config.ADVANCED_DIAGONAL_STRATEGY)
  159. {
  160. // SouthEast
  161. if ((nodeE != null) && (nodeS != null))
  162. {
  163. if (((((NodeLoc) nodeE.getLoc()).getNSWE() & SOUTH) != 0) && ((((NodeLoc) nodeS.getLoc()).getNSWE() & EAST) != 0))
  164. {
  165. addNode(x + 1, y + 1, z, true);
  166. }
  167. }
  168. // SouthWest
  169. if ((nodeS != null) && (nodeW != null))
  170. {
  171. if (((((NodeLoc) nodeW.getLoc()).getNSWE() & SOUTH) != 0) && ((((NodeLoc) nodeS.getLoc()).getNSWE() & WEST) != 0))
  172. {
  173. addNode(x - 1, y + 1, z, true);
  174. }
  175. }
  176. // NorthEast
  177. if ((nodeN != null) && (nodeE != null))
  178. {
  179. if (((((NodeLoc) nodeE.getLoc()).getNSWE() & NORTH) != 0) && ((((NodeLoc) nodeN.getLoc()).getNSWE() & EAST) != 0))
  180. {
  181. addNode(x + 1, y - 1, z, true);
  182. }
  183. }
  184. // NorthWest
  185. if ((nodeN != null) && (nodeW != null))
  186. {
  187. if (((((NodeLoc) nodeW.getLoc()).getNSWE() & NORTH) != 0) && ((((NodeLoc) nodeN.getLoc()).getNSWE() & WEST) != 0))
  188. {
  189. addNode(x - 1, y - 1, z, true);
  190. }
  191. }
  192. }
  193. }
  194. private final CellNode getNode(int x, int y, short z)
  195. {
  196. final int aX = x - _baseX;
  197. if ((aX < 0) || (aX >= _mapSize))
  198. {
  199. return null;
  200. }
  201. final int aY = y - _baseY;
  202. if ((aY < 0) || (aY >= _mapSize))
  203. {
  204. return null;
  205. }
  206. CellNode result = _buffer[aX][aY];
  207. if (result == null)
  208. {
  209. result = new CellNode(new NodeLoc(x, y, z));
  210. _buffer[aX][aY] = result;
  211. }
  212. else if (!result.isInUse())
  213. {
  214. result.setInUse();
  215. // reinit node if needed
  216. if (result.getLoc() != null)
  217. {
  218. ((NodeLoc) result.getLoc()).set(x, y, z);
  219. }
  220. else
  221. {
  222. result.setLoc(new NodeLoc(x, y, z));
  223. }
  224. }
  225. return result;
  226. }
  227. private final CellNode addNode(int x, int y, short z, boolean diagonal)
  228. {
  229. CellNode newNode = getNode(x, y, z);
  230. if (newNode == null)
  231. {
  232. return null;
  233. }
  234. if (newNode.getCost() >= 0)
  235. {
  236. return newNode;
  237. }
  238. final short geoZ = newNode.getLoc().getZ();
  239. final int stepZ = Math.abs(geoZ - _current.getLoc().getZ());
  240. float weight = diagonal ? Config.DIAGONAL_WEIGHT : Config.LOW_WEIGHT;
  241. if ((((NodeLoc) newNode.getLoc()).getNSWE() != NSWE_ALL) || (stepZ > 16))
  242. {
  243. weight = Config.HIGH_WEIGHT;
  244. }
  245. else
  246. {
  247. if (isHighWeight(x + 1, y, geoZ))
  248. {
  249. weight = Config.MEDIUM_WEIGHT;
  250. }
  251. else if (isHighWeight(x - 1, y, geoZ))
  252. {
  253. weight = Config.MEDIUM_WEIGHT;
  254. }
  255. else if (isHighWeight(x, y + 1, geoZ))
  256. {
  257. weight = Config.MEDIUM_WEIGHT;
  258. }
  259. else if (isHighWeight(x, y - 1, geoZ))
  260. {
  261. weight = Config.MEDIUM_WEIGHT;
  262. }
  263. }
  264. newNode.setParent(_current);
  265. newNode.setCost(getCost(x, y, geoZ, weight));
  266. CellNode node = _current;
  267. int count = 0;
  268. while ((node.getNext() != null) && (count < (MAX_ITERATIONS * 4)))
  269. {
  270. count++;
  271. if (node.getNext().getCost() > newNode.getCost())
  272. {
  273. // insert node into a chain
  274. newNode.setNext(node.getNext());
  275. break;
  276. }
  277. node = node.getNext();
  278. }
  279. if (count == (MAX_ITERATIONS * 4))
  280. {
  281. System.err.println("Pathfinding: too long loop detected, cost:" + newNode.getCost());
  282. }
  283. node.setNext(newNode); // add last
  284. return newNode;
  285. }
  286. private final boolean isHighWeight(int x, int y, short z)
  287. {
  288. final CellNode result = getNode(x, y, z);
  289. if (result == null)
  290. {
  291. return true;
  292. }
  293. if (((NodeLoc) result.getLoc()).getNSWE() != NSWE_ALL)
  294. {
  295. return true;
  296. }
  297. if (Math.abs(result.getLoc().getZ() - z) > 16)
  298. {
  299. return true;
  300. }
  301. return false;
  302. }
  303. private final double getCost(int x, int y, short z, float weight)
  304. {
  305. final int dX = x - _targetX;
  306. final int dY = y - _targetY;
  307. final int dZ = z - _targetZ;
  308. // Math.abs(dx) + Math.abs(dy) + Math.abs(dz) / 16
  309. double result = Math.sqrt((dX * dX) + (dY * dY) + ((dZ * dZ) / 256.0));
  310. if (result > weight)
  311. {
  312. result += weight;
  313. }
  314. if (result > Float.MAX_VALUE)
  315. {
  316. result = Float.MAX_VALUE;
  317. }
  318. return result;
  319. }
  320. }