GeoData.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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;
  20. import java.io.FileInputStream;
  21. import java.lang.reflect.Constructor;
  22. import java.nio.file.Paths;
  23. import java.util.Properties;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.gameserver.datatables.DoorTable;
  28. import com.l2jserver.gameserver.geoengine.Direction;
  29. import com.l2jserver.gameserver.geoengine.NullDriver;
  30. import com.l2jserver.gameserver.geoengine.abstraction.IGeoDriver;
  31. import com.l2jserver.gameserver.model.L2Object;
  32. import com.l2jserver.gameserver.model.Location;
  33. import com.l2jserver.gameserver.model.interfaces.ILocational;
  34. import com.l2jserver.gameserver.util.GeoUtils;
  35. import com.l2jserver.gameserver.util.LinePointIterator;
  36. import com.l2jserver.gameserver.util.Util;
  37. /**
  38. * @author -Nemesiss-, FBIagent
  39. */
  40. public class GeoData implements IGeoDriver
  41. {
  42. private static class SingletonHolder
  43. {
  44. protected final static GeoData _instance;
  45. static
  46. {
  47. _instance = new GeoData();
  48. }
  49. }
  50. private static final Logger _LOGGER = Logger.getLogger(GeoData.class.getName());
  51. private static final int _ELEVATED_SEE_OVER_DISTANCE = 2;
  52. private static final int _MAX_SEE_OVER_HEIGHT = 32;
  53. public static GeoData getInstance()
  54. {
  55. return SingletonHolder._instance;
  56. }
  57. private final IGeoDriver _driver;
  58. protected GeoData()
  59. {
  60. if (Config.GEODATA > 0)
  61. {
  62. IGeoDriver driver = null;
  63. try
  64. {
  65. Class<?> cls = Class.forName(Config.GEODATA_DRIVER);
  66. if (!IGeoDriver.class.isAssignableFrom(cls))
  67. {
  68. throw new ClassCastException("Geodata driver class needs to implement IGeoDriver!");
  69. }
  70. Constructor<?> ctor = cls.getConstructor(Properties.class);
  71. Properties props = new Properties();
  72. try (FileInputStream fis = new FileInputStream(Paths.get("config", "GeoDriver.properties").toString()))
  73. {
  74. props.load(fis);
  75. }
  76. driver = (IGeoDriver) ctor.newInstance(props);
  77. }
  78. catch (Exception ex)
  79. {
  80. _LOGGER.log(Level.SEVERE, "Failed to load geodata driver!", ex);
  81. System.exit(1);
  82. }
  83. // we do it this way so it's predictable for the compiler
  84. _driver = driver;
  85. }
  86. else
  87. {
  88. _driver = new NullDriver(null);
  89. }
  90. }
  91. @Override
  92. public int getGeoX(int worldX)
  93. {
  94. return _driver.getGeoX(worldX);
  95. }
  96. @Override
  97. public int getGeoY(int worldY)
  98. {
  99. return _driver.getGeoY(worldY);
  100. }
  101. @Override
  102. public int getWorldX(int geoX)
  103. {
  104. return _driver.getWorldX(geoX);
  105. }
  106. @Override
  107. public int getWorldY(int geoY)
  108. {
  109. return _driver.getWorldY(geoY);
  110. }
  111. @Override
  112. public boolean hasGeoPos(int geoX, int geoY)
  113. {
  114. return _driver.hasGeoPos(geoX, geoY);
  115. }
  116. @Override
  117. public int getNearestZ(int geoX, int geoY, int worldZ)
  118. {
  119. return _driver.getNearestZ(geoX, geoY, worldZ);
  120. }
  121. @Override
  122. public int getNextLowerZ(int geoX, int geoY, int worldZ)
  123. {
  124. return _driver.getNextLowerZ(geoX, geoY, worldZ);
  125. }
  126. @Override
  127. public int getNextHigherZ(int geoX, int geoY, int worldZ)
  128. {
  129. return _driver.getNextHigherZ(geoX, geoY, worldZ);
  130. }
  131. @Override
  132. public boolean canEnterNeighbors(int geoX, int geoY, int worldZ, Direction first, Direction... more)
  133. {
  134. return _driver.canEnterNeighbors(geoX, geoY, worldZ, first, more);
  135. }
  136. @Override
  137. public boolean canEnterAllNeighbors(int geoX, int geoY, int worldZ)
  138. {
  139. return _driver.canEnterAllNeighbors(geoX, geoY, worldZ);
  140. }
  141. // ///////////////////
  142. // L2J METHODS
  143. /**
  144. * Gets the height.
  145. * @param x the x coordinate
  146. * @param y the y coordinate
  147. * @param z the z coordinate
  148. * @return the height
  149. */
  150. public int getHeight(int x, int y, int z)
  151. {
  152. return getNearestZ(getGeoX(x), getGeoY(y), z);
  153. }
  154. /**
  155. * Gets the spawn height.
  156. * @param x the x coordinate
  157. * @param y the y coordinate
  158. * @param zmin the minimum z coordinate
  159. * @param zmax the the maximum z coordinate
  160. * @return the spawn height
  161. */
  162. public int getSpawnHeight(int x, int y, int zmin, int zmax)
  163. {
  164. // + 30, defend against defective geodata and invalid spawn z :(
  165. return getNextLowerZ(getGeoX(x), getGeoY(y), zmax + 30);
  166. }
  167. /**
  168. * Can see target. Doors as target always return true. Checks doors between.
  169. * @param cha the character
  170. * @param target the target
  171. * @return {@code true} if the character can see the target (LOS), {@code false} otherwise
  172. */
  173. public boolean canSeeTarget(L2Object cha, L2Object target)
  174. {
  175. if (target.isDoor())
  176. {
  177. // can always see doors :o
  178. return true;
  179. }
  180. return canSeeTarget(cha.getX(), cha.getY(), cha.getZ(), cha.getInstanceId(), target.getX(), target.getY(), target.getZ(), target.getInstanceId());
  181. }
  182. /**
  183. * Can see target. Checks doors between.
  184. * @param cha the character
  185. * @param worldPosition the world position
  186. * @return {@code true} if the character can see the target at the given world position, {@code false} otherwise
  187. */
  188. public boolean canSeeTarget(L2Object cha, ILocational worldPosition)
  189. {
  190. return canSeeTarget(cha.getX(), cha.getY(), cha.getZ(), cha.getInstanceId(), worldPosition.getX(), worldPosition.getY(), worldPosition.getZ());
  191. }
  192. /**
  193. * Can see target. Checks doors between.
  194. * @param x the x coordinate
  195. * @param y the y coordinate
  196. * @param z the z coordinate
  197. * @param instanceId
  198. * @param tx the target's x coordinate
  199. * @param ty the target's y coordinate
  200. * @param tz the target's z coordinate
  201. * @param tInstanceId the target's instanceId
  202. * @return
  203. */
  204. public boolean canSeeTarget(int x, int y, int z, int instanceId, int tx, int ty, int tz, int tInstanceId)
  205. {
  206. if ((instanceId != tInstanceId))
  207. {
  208. return false;
  209. }
  210. return canSeeTarget(x, y, z, instanceId, tx, ty, tz);
  211. }
  212. /**
  213. * Can see target. Checks doors between.
  214. * @param x the x coordinate
  215. * @param y the y coordinate
  216. * @param z the z coordinate
  217. * @param instanceId
  218. * @param tx the target's x coordinate
  219. * @param ty the target's y coordinate
  220. * @param tz the target's z coordinate
  221. * @return {@code true} if there is line of sight between the given coordinate sets, {@code false} otherwise
  222. */
  223. public boolean canSeeTarget(int x, int y, int z, int instanceId, int tx, int ty, int tz)
  224. {
  225. if (DoorTable.getInstance().checkIfDoorsBetween(x, y, z, tx, ty, tz, instanceId, true))
  226. {
  227. return false;
  228. }
  229. return canSeeTarget(x, y, z, tx, ty, tz);
  230. }
  231. /**
  232. * Can see target. Does not check doors between.
  233. * @param x the x coordinate
  234. * @param y the y coordinate
  235. * @param z the z coordinate
  236. * @param tx the target's x coordinate
  237. * @param ty the target's y coordinate
  238. * @param tz the target's z coordinate
  239. * @return {@code true} if there is line of sight between the given coordinate sets, {@code false} otherwise
  240. */
  241. public boolean canSeeTarget(int x, int y, int z, int tx, int ty, int tz)
  242. {
  243. int geoX = getGeoX(x);
  244. int geoY = getGeoY(y);
  245. int tGeoX = getGeoX(tx);
  246. int tGeoY = getGeoY(ty);
  247. z = getNearestZ(geoX, geoY, z);
  248. tz = getNearestZ(tGeoX, tGeoY, tz);
  249. if ((geoX == tGeoX) && (geoY == tGeoY))
  250. {
  251. if (hasGeoPos(tGeoX, tGeoY))
  252. {
  253. return z == tz;
  254. }
  255. return true;
  256. }
  257. double fullDist = Util.calculateDistance(geoX, geoY, 0, tGeoX, tGeoY, 0, false, false);
  258. if (tz > z)
  259. {
  260. int tmp = tx;
  261. tx = x;
  262. x = tmp;
  263. tmp = ty;
  264. ty = y;
  265. y = tmp;
  266. tmp = tz;
  267. tz = z;
  268. z = tmp;
  269. tmp = tGeoX;
  270. tGeoX = geoX;
  271. geoX = tmp;
  272. tmp = tGeoY;
  273. tGeoY = geoY;
  274. geoY = tmp;
  275. }
  276. int fullZDiff = tz - z;
  277. LinePointIterator pointIter = new LinePointIterator(geoX, geoY, tGeoX, tGeoY);
  278. // first point is guaranteed to be available, skip it, we can always see our own position
  279. pointIter.next();
  280. int prevX = pointIter.x();
  281. int prevY = pointIter.y();
  282. int prevMoveNearestZ = z;
  283. int ptIndex = 0;
  284. while (pointIter.next())
  285. {
  286. int curX = pointIter.x();
  287. int curY = pointIter.y();
  288. // check only when it's not the last point & the current position has geodata
  289. if (/* ((curX != tGeoX) || (curY != tGeoY)) && */hasGeoPos(curX, curY))
  290. {
  291. double percentageDist = Util.calculateDistance(geoX, geoY, 0, curX, curY, 0, false, false) / fullDist;
  292. int beeCurZ = (int) (z + (fullZDiff * percentageDist));
  293. int beeCurNearestZ = getNearestZ(curX, curY, beeCurZ);
  294. int moveCurNearestZ;
  295. if (canEnterNeighbors(prevX, prevY, prevMoveNearestZ, GeoUtils.computeDirection(prevX, prevY, curX, curY)))
  296. {
  297. moveCurNearestZ = getNearestZ(curX, curY, prevMoveNearestZ);
  298. }
  299. else
  300. {
  301. moveCurNearestZ = beeCurNearestZ;
  302. }
  303. int maxHeight;
  304. if ((ptIndex < _ELEVATED_SEE_OVER_DISTANCE) && (fullDist >= _ELEVATED_SEE_OVER_DISTANCE))
  305. {
  306. maxHeight = z + _MAX_SEE_OVER_HEIGHT;
  307. ++ptIndex;
  308. }
  309. else
  310. {
  311. maxHeight = beeCurZ + _MAX_SEE_OVER_HEIGHT;
  312. }
  313. boolean canSeeThrough = false;
  314. if ((beeCurNearestZ <= maxHeight) && (moveCurNearestZ <= beeCurNearestZ))
  315. {
  316. Direction dir = GeoUtils.computeDirection(prevX, prevY, curX, curY);
  317. // check diagonal step
  318. switch (dir)
  319. {
  320. case NORTH_EAST:
  321. canSeeThrough = (getNearestZ(prevX, prevY - 1, beeCurZ) <= maxHeight) || (getNearestZ(prevX + 1, prevY, beeCurZ) <= maxHeight);
  322. break;
  323. case NORTH_WEST:
  324. canSeeThrough = (getNearestZ(prevX, prevY - 1, beeCurZ) <= maxHeight) || (getNearestZ(prevX - 1, prevY, beeCurZ) <= maxHeight);
  325. break;
  326. case SOUTH_EAST:
  327. canSeeThrough = (getNearestZ(prevX, prevY + 1, beeCurZ) <= maxHeight) || (getNearestZ(prevX + 1, prevY, beeCurZ) <= maxHeight);
  328. break;
  329. case SOUTH_WEST:
  330. canSeeThrough = (getNearestZ(prevX, prevY + 1, beeCurZ) <= maxHeight) || (getNearestZ(prevX - 1, prevY, beeCurZ) <= maxHeight);
  331. break;
  332. default:
  333. canSeeThrough = true;
  334. break;
  335. }
  336. }
  337. if (!canSeeThrough)
  338. {
  339. return false;
  340. }
  341. prevMoveNearestZ = moveCurNearestZ;
  342. }
  343. prevX = curX;
  344. prevY = curY;
  345. }
  346. return true;
  347. }
  348. /**
  349. * Move check.
  350. * @param x the x coordinate
  351. * @param y the y coordinate
  352. * @param z the z coordinate
  353. * @param tx the target's x coordinate
  354. * @param ty the target's y coordinate
  355. * @param tz the target's z coordinate
  356. * @param instanceId the instance id
  357. * @return the last Location (x,y,z) where player can walk - just before wall
  358. */
  359. public Location moveCheck(int x, int y, int z, int tx, int ty, int tz, int instanceId)
  360. {
  361. int geoX = getGeoX(x);
  362. int geoY = getGeoY(y);
  363. z = getNearestZ(geoX, geoY, z);
  364. int tGeoX = getGeoX(tx);
  365. int tGeoY = getGeoY(ty);
  366. tz = getNearestZ(tGeoX, tGeoY, tz);
  367. if (DoorTable.getInstance().checkIfDoorsBetween(x, y, z, tx, ty, tz, instanceId, false))
  368. {
  369. return new Location(x, y, getHeight(x, y, z));
  370. }
  371. LinePointIterator pointIter = new LinePointIterator(geoX, geoY, tGeoX, tGeoY);
  372. // first point is guaranteed to be available
  373. pointIter.next();
  374. int prevX = pointIter.x();
  375. int prevY = pointIter.y();
  376. int prevZ = z;
  377. while (pointIter.next())
  378. {
  379. int curX = pointIter.x();
  380. int curY = pointIter.y();
  381. int curZ = getNearestZ(curX, curY, prevZ);
  382. if (hasGeoPos(prevX, prevY))
  383. {
  384. Direction dir = GeoUtils.computeDirection(prevX, prevY, curX, curY);
  385. boolean canEnter = false;
  386. if (canEnterNeighbors(prevX, prevY, prevZ, dir))
  387. {
  388. // check diagonal movement
  389. switch (dir)
  390. {
  391. case NORTH_EAST:
  392. canEnter = canEnterNeighbors(prevX, prevY - 1, prevZ, Direction.EAST) && canEnterNeighbors(prevX + 1, prevY, prevZ, Direction.NORTH);
  393. break;
  394. case NORTH_WEST:
  395. canEnter = canEnterNeighbors(prevX, prevY - 1, prevZ, Direction.WEST) && canEnterNeighbors(prevX - 1, prevY, prevZ, Direction.NORTH);
  396. break;
  397. case SOUTH_EAST:
  398. canEnter = canEnterNeighbors(prevX, prevY + 1, prevZ, Direction.EAST) && canEnterNeighbors(prevX + 1, prevY, prevZ, Direction.SOUTH);
  399. break;
  400. case SOUTH_WEST:
  401. canEnter = canEnterNeighbors(prevX, prevY + 1, prevZ, Direction.WEST) && canEnterNeighbors(prevX - 1, prevY, prevZ, Direction.SOUTH);
  402. break;
  403. default:
  404. canEnter = true;
  405. break;
  406. }
  407. }
  408. if (!canEnter)
  409. {
  410. // can't move, return previous location
  411. return new Location(getWorldX(prevX), getWorldY(prevY), prevZ);
  412. }
  413. }
  414. prevX = curX;
  415. prevY = curY;
  416. prevZ = curZ;
  417. }
  418. if (hasGeoPos(prevX, prevY) && (prevZ != tz))
  419. {
  420. // different floors, return start location
  421. return new Location(x, y, z);
  422. }
  423. return new Location(tx, ty, tz);
  424. }
  425. /**
  426. * Can move from to target.
  427. * @param x the x coordinate
  428. * @param y the y coordinate
  429. * @param z the z coordinate
  430. * @param tx the target's x coordinate
  431. * @param ty the target's y coordinate
  432. * @param tz the target's z coordinate
  433. * @param instanceId the instance id
  434. * @return {@code true} if the character at x,y,z can move to tx,ty,tz, {@code false} otherwise
  435. */
  436. public boolean canMoveFromToTarget(int x, int y, int z, int tx, int ty, int tz, int instanceId)
  437. {
  438. int geoX = getGeoX(x);
  439. int geoY = getGeoY(y);
  440. z = getNearestZ(geoX, geoY, z);
  441. int tGeoX = getGeoX(tx);
  442. int tGeoY = getGeoY(ty);
  443. tz = getNearestZ(tGeoX, tGeoY, tz);
  444. if (DoorTable.getInstance().checkIfDoorsBetween(x, y, z, tx, ty, tz, instanceId, false))
  445. {
  446. return false;
  447. }
  448. LinePointIterator pointIter = new LinePointIterator(geoX, geoY, tGeoX, tGeoY);
  449. // first point is guaranteed to be available
  450. pointIter.next();
  451. int prevX = pointIter.x();
  452. int prevY = pointIter.y();
  453. int prevZ = z;
  454. while (pointIter.next())
  455. {
  456. int curX = pointIter.x();
  457. int curY = pointIter.y();
  458. int curZ = getNearestZ(curX, curY, prevZ);
  459. if (hasGeoPos(prevX, prevY))
  460. {
  461. Direction dir = GeoUtils.computeDirection(prevX, prevY, curX, curY);
  462. boolean canEnter = false;
  463. if (canEnterNeighbors(prevX, prevY, prevZ, dir))
  464. {
  465. // check diagonal movement
  466. switch (dir)
  467. {
  468. case NORTH_EAST:
  469. canEnter = canEnterNeighbors(prevX, prevY - 1, prevZ, Direction.EAST) && canEnterNeighbors(prevX + 1, prevY, prevZ, Direction.NORTH);
  470. break;
  471. case NORTH_WEST:
  472. canEnter = canEnterNeighbors(prevX, prevY - 1, prevZ, Direction.WEST) && canEnterNeighbors(prevX - 1, prevY, prevZ, Direction.NORTH);
  473. break;
  474. case SOUTH_EAST:
  475. canEnter = canEnterNeighbors(prevX, prevY + 1, prevZ, Direction.EAST) && canEnterNeighbors(prevX + 1, prevY, prevZ, Direction.SOUTH);
  476. break;
  477. case SOUTH_WEST:
  478. canEnter = canEnterNeighbors(prevX, prevY + 1, prevZ, Direction.WEST) && canEnterNeighbors(prevX - 1, prevY, prevZ, Direction.SOUTH);
  479. break;
  480. default:
  481. canEnter = true;
  482. break;
  483. }
  484. }
  485. if (!canEnter)
  486. {
  487. return false;
  488. }
  489. }
  490. prevX = curX;
  491. prevY = curY;
  492. prevZ = curZ;
  493. }
  494. if (hasGeoPos(prevX, prevY) && (prevZ != tz))
  495. {
  496. // different floors
  497. return false;
  498. }
  499. return true;
  500. }
  501. /**
  502. * Checks for geodata.
  503. * @param x the x coordinate
  504. * @param y the y coordinate
  505. * @return {@code true} if there is geodata for the given coordinates, {@code false} otherwise
  506. */
  507. public boolean hasGeo(int x, int y)
  508. {
  509. return hasGeoPos(getGeoX(x), getGeoY(y));
  510. }
  511. }