CellNodeBuffer.java 8.0 KB

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