CellPathFinding.java 11 KB

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