CellPathFinding.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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.ArrayList;
  17. import java.util.List;
  18. import java.util.ListIterator;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import javolution.util.FastList;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.gameserver.GeoData;
  24. import com.l2jserver.gameserver.idfactory.IdFactory;
  25. import com.l2jserver.gameserver.model.L2ItemInstance;
  26. import com.l2jserver.gameserver.model.L2World;
  27. import com.l2jserver.gameserver.pathfinding.AbstractNode;
  28. import com.l2jserver.gameserver.pathfinding.AbstractNodeLoc;
  29. import com.l2jserver.gameserver.pathfinding.PathFinding;
  30. import com.l2jserver.util.StringUtil;
  31. /**
  32. *
  33. * @author Sami, DS
  34. * Credits to Diamond
  35. */
  36. public class CellPathFinding extends PathFinding
  37. {
  38. private static final Logger _log = Logger.getLogger(CellPathFinding.class.getName());
  39. private BufferInfo[] _allBuffers;
  40. private int _findSuccess = 0;
  41. private int _findFails = 0;
  42. private int _postFilterUses = 0;
  43. private int _postFilterPlayableUses = 0;
  44. private int _postFilterPasses = 0;
  45. private long _postFilterElapsed = 0;
  46. private FastList<L2ItemInstance> _debugItems = null;
  47. public static CellPathFinding getInstance()
  48. {
  49. return SingletonHolder._instance;
  50. }
  51. private CellPathFinding()
  52. {
  53. try
  54. {
  55. String[] array = Config.PATHFIND_BUFFERS.split(";");
  56. _allBuffers = new BufferInfo[array.length];
  57. String buf;
  58. String[] args;
  59. for (int i = 0; i < array.length; i++)
  60. {
  61. buf = array[i];
  62. args = buf.split("x");
  63. if (args.length != 2)
  64. throw new Exception("Invalid buffer definition: " + buf);
  65. _allBuffers[i] = new BufferInfo(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
  66. }
  67. }
  68. catch (Exception e)
  69. {
  70. _log.log(Level.WARNING, "CellPathFinding: Problem during buffer init: " + e.getMessage(), e);
  71. throw new Error("CellPathFinding: load aborted");
  72. }
  73. }
  74. /**
  75. * @see com.l2jserver.gameserver.pathfinding.PathFinding#PathNodesExist(short)
  76. */
  77. @Override
  78. public boolean pathNodesExist(short regionoffset)
  79. {
  80. return false;
  81. }
  82. /**
  83. * @see com.l2jserver.gameserver.pathfinding.PathFinding#FindPath(int, int, short, int, int, short)
  84. */
  85. @Override
  86. public List<AbstractNodeLoc> findPath(int x, int y, int z, int tx, int ty, int tz, int instanceId, boolean playable)
  87. {
  88. int gx = (x - L2World.MAP_MIN_X) >> 4;
  89. int gy = (y - L2World.MAP_MIN_Y) >> 4;
  90. if (!GeoData.getInstance().hasGeo(x, y))
  91. return null;
  92. short gz = GeoData.getInstance().getHeight(x, y, z);
  93. int gtx = (tx - L2World.MAP_MIN_X) >> 4;
  94. int gty = (ty - L2World.MAP_MIN_Y) >> 4;
  95. if (!GeoData.getInstance().hasGeo(tx, ty))
  96. return null;
  97. short gtz = GeoData.getInstance().getHeight(tx, ty, tz);
  98. CellNodeBuffer buffer = alloc(64 + 2 * Math.max(Math.abs(gx - gtx), Math.abs(gy - gty)), playable);
  99. if (buffer == null)
  100. return null;
  101. boolean debug = playable && Config.DEBUG_PATH;
  102. if (debug)
  103. {
  104. if (_debugItems == null)
  105. _debugItems = new FastList<L2ItemInstance>();
  106. else
  107. {
  108. for (L2ItemInstance item : _debugItems)
  109. {
  110. if (item == null)
  111. continue;
  112. item.decayMe();
  113. }
  114. _debugItems.clear();
  115. }
  116. }
  117. FastList<AbstractNodeLoc> path = null;
  118. try
  119. {
  120. CellNode result = buffer.findPath(gx, gy, gz, gtx, gty, gtz);
  121. if (debug)
  122. {
  123. for (CellNode n : buffer.debugPath())
  124. {
  125. if (n.getCost() < 0) // calculated path
  126. dropDebugItem(1831, (int) (-n.getCost() * 10), n.getLoc());
  127. else
  128. // known nodes
  129. dropDebugItem(57, (int) (n.getCost() * 10), n.getLoc());
  130. }
  131. }
  132. if (result == null)
  133. {
  134. _findFails++;
  135. return null;
  136. }
  137. path = constructPath(result);
  138. }
  139. catch (Exception e)
  140. {
  141. _log.log(Level.WARNING, "", e);
  142. return null;
  143. }
  144. finally
  145. {
  146. buffer.free();
  147. }
  148. if (path.size() < 3 || Config.MAX_POSTFILTER_PASSES <= 0)
  149. {
  150. _findSuccess++;
  151. return path;
  152. }
  153. long timeStamp = System.currentTimeMillis();
  154. _postFilterUses++;
  155. if (playable)
  156. _postFilterPlayableUses++;
  157. int currentX, currentY, currentZ;
  158. ListIterator<AbstractNodeLoc> middlePoint, endPoint;
  159. AbstractNodeLoc locMiddle, locEnd;
  160. boolean remove;
  161. int pass = 0;
  162. do
  163. {
  164. pass++;
  165. _postFilterPasses++;
  166. remove = false;
  167. middlePoint = path.listIterator();
  168. endPoint = path.listIterator(1);
  169. locEnd = null;
  170. currentX = x;
  171. currentY = y;
  172. currentZ = z;
  173. while (endPoint.hasNext())
  174. {
  175. locEnd = endPoint.next();
  176. locMiddle = middlePoint.next();
  177. if (GeoData.getInstance().canMoveFromToTarget(currentX, currentY, currentZ, locEnd.getX(), locEnd.getY(), locEnd.getZ(), instanceId))
  178. {
  179. middlePoint.remove();
  180. remove = true;
  181. if (debug)
  182. dropDebugItem(735, 1, locMiddle);
  183. }
  184. else
  185. {
  186. currentX = locMiddle.getX();
  187. currentY = locMiddle.getY();
  188. currentZ = locMiddle.getZ();
  189. }
  190. }
  191. }
  192. // only one postfilter pass for AI
  193. while (playable && remove && path.size() > 2 && pass < Config.MAX_POSTFILTER_PASSES);
  194. if (debug)
  195. {
  196. middlePoint = path.listIterator();
  197. while (middlePoint.hasNext())
  198. {
  199. locMiddle = middlePoint.next();
  200. dropDebugItem(65, 1, locMiddle);
  201. }
  202. }
  203. _findSuccess++;
  204. _postFilterElapsed += System.currentTimeMillis() - timeStamp;
  205. return path;
  206. }
  207. private FastList<AbstractNodeLoc> constructPath(AbstractNode node)
  208. {
  209. FastList<AbstractNodeLoc> path = new FastList<AbstractNodeLoc>();
  210. int previousDirectionX = Integer.MIN_VALUE;
  211. int previousDirectionY = Integer.MIN_VALUE;
  212. int directionX, directionY;
  213. while (node.getParent() != null)
  214. {
  215. if (!Config.ADVANCED_DIAGONAL_STRATEGY && node.getParent().getParent() != null)
  216. {
  217. int tmpX = node.getLoc().getNodeX() - node.getParent().getParent().getLoc().getNodeX();
  218. int tmpY = node.getLoc().getNodeY() - node.getParent().getParent().getLoc().getNodeY();
  219. if (Math.abs(tmpX) == Math.abs(tmpY))
  220. {
  221. directionX = tmpX;
  222. directionY = tmpY;
  223. }
  224. else
  225. {
  226. directionX = node.getLoc().getNodeX() - node.getParent().getLoc().getNodeX();
  227. directionY = node.getLoc().getNodeY() - node.getParent().getLoc().getNodeY();
  228. }
  229. }
  230. else
  231. {
  232. directionX = node.getLoc().getNodeX() - node.getParent().getLoc().getNodeX();
  233. directionY = node.getLoc().getNodeY() - node.getParent().getLoc().getNodeY();
  234. }
  235. // only add a new route point if moving direction changes
  236. if (directionX != previousDirectionX || directionY != previousDirectionY)
  237. {
  238. previousDirectionX = directionX;
  239. previousDirectionY = directionY;
  240. path.addFirst(node.getLoc());
  241. node.setLoc(null);
  242. }
  243. node = node.getParent();
  244. }
  245. return path;
  246. }
  247. private final CellNodeBuffer alloc(int size, boolean playable)
  248. {
  249. CellNodeBuffer current = null;
  250. for (BufferInfo i : _allBuffers)
  251. {
  252. if (i.mapSize >= size)
  253. {
  254. for (CellNodeBuffer buf : i.bufs)
  255. {
  256. if (buf.lock())
  257. {
  258. i.uses++;
  259. if (playable)
  260. i.playableUses++;
  261. i.elapsed += buf.getElapsedTime();
  262. current = buf;
  263. break;
  264. }
  265. }
  266. if (current != null)
  267. break;
  268. // not found, allocate temporary buffer
  269. current = new CellNodeBuffer(i.mapSize);
  270. current.lock();
  271. if (i.bufs.size() < i.count)
  272. {
  273. i.bufs.add(current);
  274. i.uses++;
  275. if (playable)
  276. i.playableUses++;
  277. break;
  278. }
  279. else
  280. {
  281. i.overflows++;
  282. if (playable)
  283. i.playableOverflows++;
  284. //System.err.println("Overflow, size requested: " + size + " playable:"+playable);
  285. }
  286. }
  287. }
  288. return current;
  289. }
  290. private final void dropDebugItem(int itemId, int num, AbstractNodeLoc loc)
  291. {
  292. final L2ItemInstance item = new L2ItemInstance(IdFactory.getInstance().getNextId(), itemId);
  293. item.setCount(num);
  294. item.spawnMe(loc.getX(), loc.getY(), loc.getZ());
  295. _debugItems.add(item);
  296. }
  297. private static final class BufferInfo
  298. {
  299. final int mapSize;
  300. final int count;
  301. ArrayList<CellNodeBuffer> bufs;
  302. int uses = 0;
  303. int playableUses = 0;
  304. int overflows = 0;
  305. int playableOverflows = 0;
  306. long elapsed = 0;
  307. public BufferInfo(int size, int cnt)
  308. {
  309. mapSize = size;
  310. count = cnt;
  311. bufs = new ArrayList<CellNodeBuffer>(count);
  312. }
  313. @Override
  314. public String toString()
  315. {
  316. final StringBuilder stat = new StringBuilder(100);
  317. StringUtil.append(stat,
  318. String.valueOf(mapSize), "x", String.valueOf(mapSize),
  319. " num:", String.valueOf(bufs.size()), "/" ,String.valueOf(count),
  320. " uses:", String.valueOf(uses), "/", String.valueOf(playableUses));
  321. if (uses > 0)
  322. StringUtil.append(stat, " total/avg(ms):",
  323. String.valueOf(elapsed), "/", String.format("%1.2f", (double)elapsed/uses));
  324. StringUtil.append(stat, " ovf:", String.valueOf(overflows), "/",
  325. String.valueOf(playableOverflows));
  326. return stat.toString();
  327. }
  328. }
  329. @Override
  330. public String[] getStat()
  331. {
  332. final String[] result = new String[_allBuffers.length + 1];
  333. for (int i = 0; i < _allBuffers.length; i++)
  334. result[i] = _allBuffers[i].toString();
  335. final StringBuilder stat = new StringBuilder(100);
  336. StringUtil.append(stat,
  337. "LOS postfilter uses:", String.valueOf(_postFilterUses),
  338. "/", String.valueOf(_postFilterPlayableUses));
  339. if (_postFilterUses > 0)
  340. StringUtil.append(stat, " total/avg(ms):",
  341. String.valueOf(_postFilterElapsed), "/",
  342. String.format("%1.2f", (double)_postFilterElapsed/_postFilterUses),
  343. " passes total/avg:", String.valueOf(_postFilterPasses), "/",
  344. String.format("%1.1f", (double)_postFilterPasses/_postFilterUses),"\r\n");
  345. StringUtil.append(stat, "Pathfind success/fail:", String.valueOf(_findSuccess),
  346. "/", String.valueOf(_findFails));
  347. result[result.length - 1] = stat.toString();
  348. return result;
  349. }
  350. @SuppressWarnings("synthetic-access")
  351. private static class SingletonHolder
  352. {
  353. protected static final CellPathFinding _instance = new CellPathFinding();
  354. }
  355. }