CellPathFinding.java 11 KB

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