CellPathFinding.java 11 KB

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