CellPathFinding.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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.ArrayList;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.concurrent.CopyOnWriteArrayList;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  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 List<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 CopyOnWriteArrayList<>();
  111. }
  112. else
  113. {
  114. for (L2ItemInstance item : _debugItems)
  115. {
  116. item.decayMe();
  117. }
  118. _debugItems.clear();
  119. }
  120. }
  121. List<AbstractNodeLoc> path = null;
  122. try
  123. {
  124. CellNode result = buffer.findPath(gx, gy, gz, gtx, gty, gtz);
  125. if (debug)
  126. {
  127. for (CellNode n : buffer.debugPath())
  128. {
  129. if (n.getCost() < 0)
  130. {
  131. dropDebugItem(1831, (int) (-n.getCost() * 10), n.getLoc());
  132. }
  133. else
  134. {
  135. // known nodes
  136. dropDebugItem(Inventory.ADENA_ID, (int) (n.getCost() * 10), n.getLoc());
  137. }
  138. }
  139. }
  140. if (result == null)
  141. {
  142. _findFails++;
  143. return null;
  144. }
  145. path = constructPath(result);
  146. }
  147. catch (Exception e)
  148. {
  149. _log.log(Level.WARNING, "", e);
  150. return null;
  151. }
  152. finally
  153. {
  154. buffer.free();
  155. }
  156. if ((path.size() < 3) || (Config.MAX_POSTFILTER_PASSES <= 0))
  157. {
  158. _findSuccess++;
  159. return path;
  160. }
  161. long timeStamp = System.currentTimeMillis();
  162. _postFilterUses++;
  163. if (playable)
  164. {
  165. _postFilterPlayableUses++;
  166. }
  167. boolean remove;
  168. int pass = 0;
  169. do
  170. {
  171. pass++;
  172. _postFilterPasses++;
  173. remove = false;
  174. final Iterator<AbstractNodeLoc> endPoint = path.iterator();
  175. endPoint.next();
  176. int currentX = x;
  177. int currentY = y;
  178. int currentZ = z;
  179. int midPoint = 0;
  180. while (endPoint.hasNext())
  181. {
  182. AbstractNodeLoc locMiddle = path.get(midPoint);
  183. AbstractNodeLoc locEnd = endPoint.next();
  184. if (GeoData.getInstance().canMove(currentX, currentY, currentZ, locEnd.getX(), locEnd.getY(), locEnd.getZ(), instanceId))
  185. {
  186. path.remove(midPoint);
  187. remove = true;
  188. if (debug)
  189. {
  190. dropDebugItem(735, 1, locMiddle);
  191. }
  192. }
  193. else
  194. {
  195. currentX = locMiddle.getX();
  196. currentY = locMiddle.getY();
  197. currentZ = locMiddle.getZ();
  198. midPoint++;
  199. }
  200. }
  201. }
  202. // only one postfilter pass for AI
  203. while (playable && remove && (path.size() > 2) && (pass < Config.MAX_POSTFILTER_PASSES));
  204. if (debug)
  205. {
  206. path.forEach(n -> dropDebugItem(65, 1, n));
  207. }
  208. _findSuccess++;
  209. _postFilterElapsed += System.currentTimeMillis() - timeStamp;
  210. return path;
  211. }
  212. private List<AbstractNodeLoc> constructPath(AbstractNode<NodeLoc> node)
  213. {
  214. final List<AbstractNodeLoc> path = new CopyOnWriteArrayList<>();
  215. int previousDirectionX = Integer.MIN_VALUE;
  216. int previousDirectionY = Integer.MIN_VALUE;
  217. int directionX, directionY;
  218. while (node.getParent() != null)
  219. {
  220. if (!Config.ADVANCED_DIAGONAL_STRATEGY && (node.getParent().getParent() != null))
  221. {
  222. int tmpX = node.getLoc().getNodeX() - node.getParent().getParent().getLoc().getNodeX();
  223. int tmpY = node.getLoc().getNodeY() - node.getParent().getParent().getLoc().getNodeY();
  224. if (Math.abs(tmpX) == Math.abs(tmpY))
  225. {
  226. directionX = tmpX;
  227. directionY = tmpY;
  228. }
  229. else
  230. {
  231. directionX = node.getLoc().getNodeX() - node.getParent().getLoc().getNodeX();
  232. directionY = node.getLoc().getNodeY() - node.getParent().getLoc().getNodeY();
  233. }
  234. }
  235. else
  236. {
  237. directionX = node.getLoc().getNodeX() - node.getParent().getLoc().getNodeX();
  238. directionY = node.getLoc().getNodeY() - node.getParent().getLoc().getNodeY();
  239. }
  240. // only add a new route point if moving direction changes
  241. if ((directionX != previousDirectionX) || (directionY != previousDirectionY))
  242. {
  243. previousDirectionX = directionX;
  244. previousDirectionY = directionY;
  245. path.add(0, node.getLoc());
  246. node.setLoc(null);
  247. }
  248. node = node.getParent();
  249. }
  250. return path;
  251. }
  252. private final CellNodeBuffer alloc(int size, boolean playable)
  253. {
  254. CellNodeBuffer current = null;
  255. for (BufferInfo i : _allBuffers)
  256. {
  257. if (i.mapSize >= size)
  258. {
  259. for (CellNodeBuffer buf : i.bufs)
  260. {
  261. if (buf.lock())
  262. {
  263. i.uses++;
  264. if (playable)
  265. {
  266. i.playableUses++;
  267. }
  268. i.elapsed += buf.getElapsedTime();
  269. current = buf;
  270. break;
  271. }
  272. }
  273. if (current != null)
  274. {
  275. break;
  276. }
  277. // not found, allocate temporary buffer
  278. current = new CellNodeBuffer(i.mapSize);
  279. current.lock();
  280. if (i.bufs.size() < i.count)
  281. {
  282. i.bufs.add(current);
  283. i.uses++;
  284. if (playable)
  285. {
  286. i.playableUses++;
  287. }
  288. break;
  289. }
  290. i.overflows++;
  291. if (playable)
  292. {
  293. i.playableOverflows++;
  294. // System.err.println("Overflow, size requested: " + size + " playable:"+playable);
  295. }
  296. }
  297. }
  298. return current;
  299. }
  300. private final void dropDebugItem(int itemId, int num, AbstractNodeLoc loc)
  301. {
  302. final L2ItemInstance item = new L2ItemInstance(IdFactory.getInstance().getNextId(), itemId);
  303. item.setCount(num);
  304. item.spawnMe(loc.getX(), loc.getY(), loc.getZ());
  305. _debugItems.add(item);
  306. }
  307. private static final class BufferInfo
  308. {
  309. final int mapSize;
  310. final int count;
  311. List<CellNodeBuffer> bufs;
  312. int uses = 0;
  313. int playableUses = 0;
  314. int overflows = 0;
  315. int playableOverflows = 0;
  316. long elapsed = 0;
  317. public BufferInfo(int size, int cnt)
  318. {
  319. mapSize = size;
  320. count = cnt;
  321. bufs = new ArrayList<>(count);
  322. }
  323. @Override
  324. public String toString()
  325. {
  326. final StringBuilder stat = new StringBuilder(100);
  327. 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));
  328. if (uses > 0)
  329. {
  330. StringUtil.append(stat, " total/avg(ms):", String.valueOf(elapsed), "/", String.format("%1.2f", (double) elapsed / uses));
  331. }
  332. StringUtil.append(stat, " ovf:", String.valueOf(overflows), "/", String.valueOf(playableOverflows));
  333. return stat.toString();
  334. }
  335. }
  336. @Override
  337. public String[] getStat()
  338. {
  339. final String[] result = new String[_allBuffers.length + 1];
  340. for (int i = 0; i < _allBuffers.length; i++)
  341. {
  342. result[i] = _allBuffers[i].toString();
  343. }
  344. final StringBuilder stat = new StringBuilder(100);
  345. StringUtil.append(stat, "LOS postfilter uses:", String.valueOf(_postFilterUses), "/", String.valueOf(_postFilterPlayableUses));
  346. if (_postFilterUses > 0)
  347. {
  348. 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);
  349. }
  350. StringUtil.append(stat, "Pathfind success/fail:", String.valueOf(_findSuccess), "/", String.valueOf(_findFails));
  351. result[result.length - 1] = stat.toString();
  352. return result;
  353. }
  354. private static class SingletonHolder
  355. {
  356. protected static final CellPathFinding _instance = new CellPathFinding();
  357. }
  358. }