CellNodeBuffer.java 7.8 KB

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