2
0

GeoData.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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;
  20. import java.io.FileNotFoundException;
  21. import java.nio.file.Path;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import com.l2jserver.Config;
  25. import com.l2jserver.gameserver.datatables.DoorTable;
  26. import com.l2jserver.gameserver.model.L2Object;
  27. import com.l2jserver.gameserver.model.L2World;
  28. import com.l2jserver.gameserver.model.Location;
  29. import com.l2jserver.gameserver.model.interfaces.ILocational;
  30. import com.l2jserver.gameserver.util.GeoUtils;
  31. import com.l2jserver.gameserver.util.LinePointIterator;
  32. import com.l2jserver.gameserver.util.LinePointIterator3D;
  33. import com.l2jserver.geodriver.Cell;
  34. import com.l2jserver.geodriver.GeoDriver;
  35. /**
  36. * @author -Nemesiss-, HorridoJoho
  37. */
  38. public class GeoData
  39. {
  40. private static final Logger LOGGER = Logger.getLogger(GeoData.class.getName());
  41. private static final String FILE_NAME_FORMAT = "%d_%d.l2j";
  42. private static final int ELEVATED_SEE_OVER_DISTANCE = 2;
  43. private static final int MAX_SEE_OVER_HEIGHT = 48;
  44. private static final int Z_DELTA_LIMIT = 100;
  45. private final GeoDriver _driver = new GeoDriver();
  46. protected GeoData()
  47. {
  48. if (Config.GEODATA == 0)
  49. {
  50. LOGGER.info(getClass().getSimpleName() + ": Disabled.");
  51. return;
  52. }
  53. int loadedRegions = 0;
  54. try
  55. {
  56. for (int regionX = L2World.TILE_X_MIN; regionX <= L2World.TILE_X_MAX; regionX++)
  57. {
  58. for (int regionY = L2World.TILE_Y_MIN; regionY <= L2World.TILE_Y_MAX; regionY++)
  59. {
  60. final Path geoFilePath = Config.GEODATA_PATH.resolve(String.format(FILE_NAME_FORMAT, regionX, regionY));
  61. final Boolean loadFile = Config.GEODATA_REGIONS.get(regionX + "_" + regionY);
  62. if (loadFile != null)
  63. {
  64. if (loadFile)
  65. {
  66. LOGGER.info(getClass().getSimpleName() + ": Loading " + geoFilePath.getFileName() + "...");
  67. _driver.loadRegion(geoFilePath, regionX, regionY);
  68. loadedRegions++;
  69. }
  70. }
  71. else if (Config.TRY_LOAD_UNSPECIFIED_REGIONS)
  72. {
  73. try
  74. {
  75. LOGGER.info(getClass().getSimpleName() + ": Loading " + geoFilePath.getFileName() + "...");
  76. _driver.loadRegion(geoFilePath, regionX, regionY);
  77. loadedRegions++;
  78. }
  79. catch (Exception e)
  80. {
  81. // ignore file not found errors
  82. if (!(e instanceof FileNotFoundException))
  83. {
  84. LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": Failed to load " + geoFilePath.getFileName() + "!", e);
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. catch (Exception e)
  92. {
  93. LOGGER.log(Level.SEVERE, getClass().getSimpleName() + ": Failed to load geodata!", e);
  94. System.exit(1);
  95. }
  96. LOGGER.info(getClass().getSimpleName() + ": Loaded " + loadedRegions + " regions.");
  97. }
  98. public boolean hasGeoPos(int geoX, int geoY)
  99. {
  100. return _driver.hasGeoPos(geoX, geoY);
  101. }
  102. public boolean checkNearestNswe(int geoX, int geoY, int worldZ, int nswe)
  103. {
  104. return _driver.checkNearestNswe(geoX, geoY, worldZ, nswe);
  105. }
  106. public boolean checkNearestNswe(int geoX, int geoY, int worldZ, int nswe, int zDeltaLimit)
  107. {
  108. return _driver.checkNearestNswe(geoX, geoY, worldZ, nswe, zDeltaLimit);
  109. }
  110. public boolean checkNearestNsweAntiCornerCut(int geoX, int geoY, int worldZ, int nswe)
  111. {
  112. boolean can = true;
  113. if ((nswe & Cell.NSWE_NORTH_EAST) != 0)
  114. {
  115. // can = canEnterNeighbors(prevX, prevY - 1, prevGeoZ, Direction.EAST) && canEnterNeighbors(prevX + 1, prevY, prevGeoZ, Direction.NORTH);
  116. can = checkNearestNswe(geoX, geoY - 1, worldZ, Cell.NSWE_EAST) && checkNearestNswe(geoX + 1, geoY, worldZ, Cell.NSWE_NORTH);
  117. }
  118. if (can && ((nswe & Cell.NSWE_NORTH_WEST) != 0))
  119. {
  120. // can = canEnterNeighbors(prevX, prevY - 1, prevGeoZ, Direction.WEST) && canEnterNeighbors(prevX - 1, prevY, prevGeoZ, Direction.NORTH);
  121. can = checkNearestNswe(geoX, geoY - 1, worldZ, Cell.NSWE_WEST) && checkNearestNswe(geoX, geoY - 1, worldZ, Cell.NSWE_NORTH);
  122. }
  123. if (can && ((nswe & Cell.NSWE_SOUTH_EAST) != 0))
  124. {
  125. // can = canEnterNeighbors(prevX, prevY + 1, prevGeoZ, Direction.EAST) && canEnterNeighbors(prevX + 1, prevY, prevGeoZ, Direction.SOUTH);
  126. can = checkNearestNswe(geoX, geoY + 1, worldZ, Cell.NSWE_EAST) && checkNearestNswe(geoX + 1, geoY, worldZ, Cell.NSWE_SOUTH);
  127. }
  128. if (can && ((nswe & Cell.NSWE_SOUTH_WEST) != 0))
  129. {
  130. // can = canEnterNeighbors(prevX, prevY + 1, prevGeoZ, Direction.WEST) && canEnterNeighbors(prevX - 1, prevY, prevGeoZ, Direction.SOUTH);
  131. can = checkNearestNswe(geoX, geoY + 1, worldZ, Cell.NSWE_WEST) && checkNearestNswe(geoX - 1, geoY, worldZ, Cell.NSWE_SOUTH);
  132. }
  133. return can && checkNearestNswe(geoX, geoY, worldZ, nswe);
  134. }
  135. public boolean checkNearestNsweAntiCornerCut(int geoX, int geoY, int worldZ, int nswe, int zDeltaLimit)
  136. {
  137. boolean can = true;
  138. if ((nswe & Cell.NSWE_NORTH_EAST) != 0)
  139. {
  140. // can = canEnterNeighbors(prevX, prevY - 1, prevGeoZ, Direction.EAST) && canEnterNeighbors(prevX + 1, prevY, prevGeoZ, Direction.NORTH);
  141. can = checkNearestNswe(geoX, geoY - 1, worldZ, Cell.NSWE_EAST, zDeltaLimit) && checkNearestNswe(geoX + 1, geoY, worldZ, Cell.NSWE_NORTH, zDeltaLimit);
  142. }
  143. if (can && ((nswe & Cell.NSWE_NORTH_WEST) != 0))
  144. {
  145. // can = canEnterNeighbors(prevX, prevY - 1, prevGeoZ, Direction.WEST) && canEnterNeighbors(prevX - 1, prevY, prevGeoZ, Direction.NORTH);
  146. can = checkNearestNswe(geoX, geoY - 1, worldZ, Cell.NSWE_WEST, zDeltaLimit) && checkNearestNswe(geoX, geoY - 1, worldZ, Cell.NSWE_NORTH, zDeltaLimit);
  147. }
  148. if (can && ((nswe & Cell.NSWE_SOUTH_EAST) != 0))
  149. {
  150. // can = canEnterNeighbors(prevX, prevY + 1, prevGeoZ, Direction.EAST) && canEnterNeighbors(prevX + 1, prevY, prevGeoZ, Direction.SOUTH);
  151. can = checkNearestNswe(geoX, geoY + 1, worldZ, Cell.NSWE_EAST, zDeltaLimit) && checkNearestNswe(geoX + 1, geoY, worldZ, Cell.NSWE_SOUTH, zDeltaLimit);
  152. }
  153. if (can && ((nswe & Cell.NSWE_SOUTH_WEST) != 0))
  154. {
  155. // can = canEnterNeighbors(prevX, prevY + 1, prevGeoZ, Direction.WEST) && canEnterNeighbors(prevX - 1, prevY, prevGeoZ, Direction.SOUTH);
  156. can = checkNearestNswe(geoX, geoY + 1, worldZ, Cell.NSWE_WEST, zDeltaLimit) && checkNearestNswe(geoX - 1, geoY, worldZ, Cell.NSWE_SOUTH, zDeltaLimit);
  157. }
  158. return can && checkNearestNswe(geoX, geoY, worldZ, nswe, zDeltaLimit);
  159. }
  160. public int getNearestZ(int geoX, int geoY, int worldZ)
  161. {
  162. return _driver.getNearestZ(geoX, geoY, worldZ);
  163. }
  164. public int getNearestZ(int geoX, int geoY, int worldZ, int zDeltaLimit)
  165. {
  166. return _driver.getNearestZ(geoX, geoY, worldZ, zDeltaLimit);
  167. }
  168. public int getNextLowerZ(int geoX, int geoY, int worldZ)
  169. {
  170. return _driver.getNextLowerZ(geoX, geoY, worldZ);
  171. }
  172. public int getNextLowerZ(int geoX, int geoY, int worldZ, int zDeltaLimit)
  173. {
  174. return _driver.getNextLowerZ(geoX, geoY, worldZ, zDeltaLimit);
  175. }
  176. public int getNextHigherZ(int geoX, int geoY, int worldZ)
  177. {
  178. return _driver.getNextHigherZ(geoX, geoY, worldZ);
  179. }
  180. public int getNextHigherZ(int geoX, int geoY, int worldZ, int zDeltaLimit)
  181. {
  182. return _driver.getNextHigherZ(geoX, geoY, worldZ, zDeltaLimit);
  183. }
  184. public int getGeoX(int worldX)
  185. {
  186. return _driver.getGeoX(worldX);
  187. }
  188. public int getGeoY(int worldY)
  189. {
  190. return _driver.getGeoY(worldY);
  191. }
  192. public int getWorldX(int geoX)
  193. {
  194. return _driver.getWorldX(geoX);
  195. }
  196. public int getWorldY(int geoY)
  197. {
  198. return _driver.getWorldY(geoY);
  199. }
  200. // ///////////////////
  201. // L2J METHODS
  202. /**
  203. * Gets the height.
  204. * @param x the x coordinate
  205. * @param y the y coordinate
  206. * @param z the z coordinate
  207. * @return the height
  208. */
  209. public int getHeight(int x, int y, int z)
  210. {
  211. return getNearestZ(getGeoX(x), getGeoY(y), z);
  212. }
  213. /**
  214. * Gets the spawn height.
  215. * @param x the x coordinate
  216. * @param y the y coordinate
  217. * @param z the the z coordinate
  218. * @return the spawn height
  219. */
  220. public int getSpawnHeight(int x, int y, int z)
  221. {
  222. return getNearestZ(getGeoX(x), getGeoY(y), z, Z_DELTA_LIMIT);
  223. }
  224. /**
  225. * Gets the spawn height.
  226. * @param location the location
  227. * @return the spawn height
  228. */
  229. public int getSpawnHeight(Location location)
  230. {
  231. return getSpawnHeight(location.getX(), location.getY(), location.getZ());
  232. }
  233. /**
  234. * Can see target. Doors as target always return true. Checks doors between.
  235. * @param cha the character
  236. * @param target the target
  237. * @return {@code true} if the character can see the target (LOS), {@code false} otherwise
  238. */
  239. public boolean canSeeTarget(L2Object cha, L2Object target)
  240. {
  241. if (target.isDoor())
  242. {
  243. // can always see doors :o
  244. return true;
  245. }
  246. return canSeeTarget(cha.getX(), cha.getY(), cha.getZ(), cha.getInstanceId(), target.getX(), target.getY(), target.getZ(), target.getInstanceId());
  247. }
  248. /**
  249. * Can see target. Checks doors between.
  250. * @param cha the character
  251. * @param worldPosition the world position
  252. * @return {@code true} if the character can see the target at the given world position, {@code false} otherwise
  253. */
  254. public boolean canSeeTarget(L2Object cha, ILocational worldPosition)
  255. {
  256. return canSeeTarget(cha.getX(), cha.getY(), cha.getZ(), cha.getInstanceId(), worldPosition.getX(), worldPosition.getY(), worldPosition.getZ());
  257. }
  258. /**
  259. * Can see target. Checks doors between.
  260. * @param x the x coordinate
  261. * @param y the y coordinate
  262. * @param z the z coordinate
  263. * @param instanceId
  264. * @param tx the target's x coordinate
  265. * @param ty the target's y coordinate
  266. * @param tz the target's z coordinate
  267. * @param tInstanceId the target's instanceId
  268. * @return
  269. */
  270. public boolean canSeeTarget(int x, int y, int z, int instanceId, int tx, int ty, int tz, int tInstanceId)
  271. {
  272. if ((instanceId != tInstanceId))
  273. {
  274. return false;
  275. }
  276. return canSeeTarget(x, y, z, instanceId, tx, ty, tz);
  277. }
  278. /**
  279. * Can see target. Checks doors between.
  280. * @param x the x coordinate
  281. * @param y the y coordinate
  282. * @param z the z coordinate
  283. * @param instanceId
  284. * @param tx the target's x coordinate
  285. * @param ty the target's y coordinate
  286. * @param tz the target's z coordinate
  287. * @return {@code true} if there is line of sight between the given coordinate sets, {@code false} otherwise
  288. */
  289. public boolean canSeeTarget(int x, int y, int z, int instanceId, int tx, int ty, int tz)
  290. {
  291. if (DoorTable.getInstance().checkIfDoorsBetween(x, y, z, tx, ty, tz, instanceId, true))
  292. {
  293. return false;
  294. }
  295. return canSeeTarget(x, y, z, tx, ty, tz);
  296. }
  297. private int getLosGeoZ(int prevX, int prevY, int prevGeoZ, int curX, int curY, int nswe)
  298. {
  299. if ((((nswe & Cell.NSWE_NORTH) != 0) && ((nswe & Cell.NSWE_SOUTH) != 0)) || (((nswe & Cell.NSWE_WEST) != 0) && ((nswe & Cell.NSWE_EAST) != 0)))
  300. {
  301. throw new RuntimeException("Multiple directions!");
  302. }
  303. if (checkNearestNsweAntiCornerCut(prevX, prevY, prevGeoZ, nswe, Z_DELTA_LIMIT))
  304. {
  305. return getNearestZ(curX, curY, prevGeoZ, Z_DELTA_LIMIT);
  306. }
  307. return getNextHigherZ(curX, curY, prevGeoZ);
  308. }
  309. /**
  310. * Can see target. Does not check doors between.
  311. * @param x the x coordinate
  312. * @param y the y coordinate
  313. * @param z the z coordinate
  314. * @param tx the target's x coordinate
  315. * @param ty the target's y coordinate
  316. * @param tz the target's z coordinate
  317. * @return {@code true} if there is line of sight between the given coordinate sets, {@code false} otherwise
  318. */
  319. public boolean canSeeTarget(int x, int y, int z, int tx, int ty, int tz)
  320. {
  321. int geoX = getGeoX(x);
  322. int geoY = getGeoY(y);
  323. int tGeoX = getGeoX(tx);
  324. int tGeoY = getGeoY(ty);
  325. z = getNearestZ(geoX, geoY, z, Z_DELTA_LIMIT);
  326. tz = getNearestZ(tGeoX, tGeoY, tz, Z_DELTA_LIMIT);
  327. // fastpath
  328. if ((geoX == tGeoX) && (geoY == tGeoY))
  329. {
  330. if (hasGeoPos(tGeoX, tGeoY))
  331. {
  332. return z == tz;
  333. }
  334. return true;
  335. }
  336. if (tz > z)
  337. {
  338. int tmp = tx;
  339. tx = x;
  340. x = tmp;
  341. tmp = ty;
  342. ty = y;
  343. y = tmp;
  344. tmp = tz;
  345. tz = z;
  346. z = tmp;
  347. tmp = tGeoX;
  348. tGeoX = geoX;
  349. geoX = tmp;
  350. tmp = tGeoY;
  351. tGeoY = geoY;
  352. geoY = tmp;
  353. }
  354. LinePointIterator3D pointIter = new LinePointIterator3D(geoX, geoY, z, tGeoX, tGeoY, tz);
  355. // first point is guaranteed to be available, skip it, we can always see our own position
  356. pointIter.next();
  357. int prevX = pointIter.x();
  358. int prevY = pointIter.y();
  359. int prevZ = pointIter.z();
  360. int prevGeoZ = prevZ;
  361. int ptIndex = 0;
  362. while (pointIter.next())
  363. {
  364. int curX = pointIter.x();
  365. int curY = pointIter.y();
  366. if ((curX == prevX) && (curY == prevY))
  367. {
  368. continue;
  369. }
  370. int beeCurZ = pointIter.z();
  371. int curGeoZ = prevGeoZ;
  372. // check if the position has geodata
  373. if (hasGeoPos(curX, curY))
  374. {
  375. int beeCurGeoZ = getNearestZ(curX, curY, beeCurZ);
  376. int nswe = GeoUtils.computeNswe(prevX, prevY, curX, curY);// .computeDirection(prevX, prevY, curX, curY);
  377. curGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, curX, curY, nswe);
  378. int maxHeight;
  379. if (ptIndex < ELEVATED_SEE_OVER_DISTANCE)
  380. {
  381. maxHeight = z + MAX_SEE_OVER_HEIGHT;
  382. }
  383. else
  384. {
  385. maxHeight = beeCurZ + MAX_SEE_OVER_HEIGHT;
  386. }
  387. boolean canSeeThrough = false;
  388. if ((curGeoZ <= maxHeight) && (curGeoZ <= beeCurGeoZ))
  389. {
  390. if ((nswe & Cell.NSWE_NORTH_EAST) != 0)
  391. {
  392. int northGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX, prevY - 1, Cell.NSWE_EAST);
  393. int eastGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX + 1, prevY, Cell.NSWE_NORTH);
  394. canSeeThrough = (northGeoZ <= maxHeight) && (eastGeoZ <= maxHeight) && (northGeoZ <= getNearestZ(prevX, prevY - 1, beeCurZ)) && (eastGeoZ <= getNearestZ(prevX + 1, prevY, beeCurZ));
  395. }
  396. else if ((nswe & Cell.NSWE_NORTH_WEST) != 0)
  397. {
  398. int northGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX, prevY - 1, Cell.NSWE_WEST);
  399. int westGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX - 1, prevY, Cell.NSWE_NORTH);
  400. canSeeThrough = (northGeoZ <= maxHeight) && (westGeoZ <= maxHeight) && (northGeoZ <= getNearestZ(prevX, prevY - 1, beeCurZ)) && (westGeoZ <= getNearestZ(prevX - 1, prevY, beeCurZ));
  401. }
  402. else if ((nswe & Cell.NSWE_SOUTH_EAST) != 0)
  403. {
  404. int southGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX, prevY + 1, Cell.NSWE_EAST);
  405. int eastGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX + 1, prevY, Cell.NSWE_SOUTH);
  406. canSeeThrough = (southGeoZ <= maxHeight) && (eastGeoZ <= maxHeight) && (southGeoZ <= getNearestZ(prevX, prevY + 1, beeCurZ)) && (eastGeoZ <= getNearestZ(prevX + 1, prevY, beeCurZ));
  407. }
  408. else if ((nswe & Cell.NSWE_SOUTH_WEST) != 0)
  409. {
  410. int southGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX, prevY + 1, Cell.NSWE_WEST);
  411. int westGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX - 1, prevY, Cell.NSWE_SOUTH);
  412. canSeeThrough = (southGeoZ <= maxHeight) && (westGeoZ <= maxHeight) && (southGeoZ <= getNearestZ(prevX, prevY + 1, beeCurZ)) && (westGeoZ <= getNearestZ(prevX - 1, prevY, beeCurZ));
  413. }
  414. else
  415. {
  416. canSeeThrough = true;
  417. }
  418. }
  419. if (!canSeeThrough)
  420. {
  421. return false;
  422. }
  423. }
  424. prevX = curX;
  425. prevY = curY;
  426. prevGeoZ = curGeoZ;
  427. ++ptIndex;
  428. }
  429. return true;
  430. }
  431. /**
  432. * Move check.
  433. * @param x the x coordinate
  434. * @param y the y coordinate
  435. * @param z the z coordinate
  436. * @param tx the target's x coordinate
  437. * @param ty the target's y coordinate
  438. * @param tz the target's z coordinate
  439. * @param instanceId the instance id
  440. * @return the last Location (x,y,z) where player can walk - just before wall
  441. */
  442. public Location moveCheck(int x, int y, int z, int tx, int ty, int tz, int instanceId)
  443. {
  444. int geoX = getGeoX(x);
  445. int geoY = getGeoY(y);
  446. z = getNearestZ(geoX, geoY, z, Z_DELTA_LIMIT);
  447. int tGeoX = getGeoX(tx);
  448. int tGeoY = getGeoY(ty);
  449. tz = getNearestZ(tGeoX, tGeoY, tz, Z_DELTA_LIMIT);
  450. if (DoorTable.getInstance().checkIfDoorsBetween(x, y, z, tx, ty, tz, instanceId, false))
  451. {
  452. return new Location(x, y, getHeight(x, y, z));
  453. }
  454. LinePointIterator pointIter = new LinePointIterator(geoX, geoY, tGeoX, tGeoY);
  455. // first point is guaranteed to be available
  456. pointIter.next();
  457. int prevX = pointIter.x();
  458. int prevY = pointIter.y();
  459. int prevZ = z;
  460. while (pointIter.next())
  461. {
  462. int curX = pointIter.x();
  463. int curY = pointIter.y();
  464. int curZ = getNearestZ(curX, curY, prevZ, Z_DELTA_LIMIT);
  465. if (hasGeoPos(prevX, prevY))
  466. {
  467. int nswe = GeoUtils.computeNswe(prevX, prevY, curX, curY);
  468. if (!checkNearestNsweAntiCornerCut(prevX, prevY, prevZ, nswe, Z_DELTA_LIMIT))
  469. {
  470. // can't move, return previous location
  471. return new Location(getWorldX(prevX), getWorldY(prevY), prevZ);
  472. }
  473. }
  474. prevX = curX;
  475. prevY = curY;
  476. prevZ = curZ;
  477. }
  478. if (hasGeoPos(prevX, prevY) && (prevZ != tz))
  479. {
  480. // different floors, return start location
  481. return new Location(x, y, z);
  482. }
  483. return new Location(tx, ty, tz);
  484. }
  485. /**
  486. * Checks if its possible to move from one location to another.
  487. * @param fromX the X coordinate to start checking from
  488. * @param fromY the Y coordinate to start checking from
  489. * @param fromZ the Z coordinate to start checking from
  490. * @param toX the X coordinate to end checking at
  491. * @param toY the Y coordinate to end checking at
  492. * @param toZ the Z coordinate to end checking at
  493. * @param instanceId the instance ID
  494. * @return {@code true} if the character at start coordinates can move to end coordinates, {@code false} otherwise
  495. */
  496. public boolean canMove(int fromX, int fromY, int fromZ, int toX, int toY, int toZ, int instanceId)
  497. {
  498. int geoX = getGeoX(fromX);
  499. int geoY = getGeoY(fromY);
  500. fromZ = getNearestZ(geoX, geoY, fromZ, Z_DELTA_LIMIT);
  501. int tGeoX = getGeoX(toX);
  502. int tGeoY = getGeoY(toY);
  503. toZ = getNearestZ(tGeoX, tGeoY, toZ, Z_DELTA_LIMIT);
  504. if (DoorTable.getInstance().checkIfDoorsBetween(fromX, fromY, fromZ, toX, toY, toZ, instanceId, false))
  505. {
  506. return false;
  507. }
  508. LinePointIterator pointIter = new LinePointIterator(geoX, geoY, tGeoX, tGeoY);
  509. // first point is guaranteed to be available
  510. pointIter.next();
  511. int prevX = pointIter.x();
  512. int prevY = pointIter.y();
  513. int prevZ = fromZ;
  514. while (pointIter.next())
  515. {
  516. int curX = pointIter.x();
  517. int curY = pointIter.y();
  518. int curZ = getNearestZ(curX, curY, prevZ, Z_DELTA_LIMIT);
  519. if (hasGeoPos(prevX, prevY))
  520. {
  521. int nswe = GeoUtils.computeNswe(prevX, prevY, curX, curY);
  522. if (!checkNearestNsweAntiCornerCut(prevX, prevY, prevZ, nswe, Z_DELTA_LIMIT))
  523. {
  524. return false;
  525. }
  526. }
  527. prevX = curX;
  528. prevY = curY;
  529. prevZ = curZ;
  530. }
  531. if (hasGeoPos(prevX, prevY) && (prevZ != toZ))
  532. {
  533. // different floors
  534. return false;
  535. }
  536. return true;
  537. }
  538. public int traceTerrainZ(int x, int y, int z, int tx, int ty)
  539. {
  540. int geoX = getGeoX(x);
  541. int geoY = getGeoY(y);
  542. z = getNearestZ(geoX, geoY, z, Z_DELTA_LIMIT);
  543. int tGeoX = getGeoX(tx);
  544. int tGeoY = getGeoY(ty);
  545. LinePointIterator pointIter = new LinePointIterator(geoX, geoY, tGeoX, tGeoY);
  546. // first point is guaranteed to be available
  547. pointIter.next();
  548. int prevZ = z;
  549. while (pointIter.next())
  550. {
  551. int curX = pointIter.x();
  552. int curY = pointIter.y();
  553. int curZ = getNearestZ(curX, curY, prevZ, Z_DELTA_LIMIT);
  554. prevZ = curZ;
  555. }
  556. return prevZ;
  557. }
  558. /**
  559. * Checks if its possible to move from one location to another.
  560. * @param from the {@code ILocational} to start checking from
  561. * @param toX the X coordinate to end checking at
  562. * @param toY the Y coordinate to end checking at
  563. * @param toZ the Z coordinate to end checking at
  564. * @return {@code true} if the character at start coordinates can move to end coordinates, {@code false} otherwise
  565. */
  566. public boolean canMove(ILocational from, int toX, int toY, int toZ)
  567. {
  568. return canMove(from.getX(), from.getY(), from.getZ(), toX, toY, toZ, from.getInstanceId());
  569. }
  570. /**
  571. * Checks if its possible to move from one location to another.
  572. * @param from the {@code ILocational} to start checking from
  573. * @param to the {@code ILocational} to end checking at
  574. * @return {@code true} if the character at start coordinates can move to end coordinates, {@code false} otherwise
  575. */
  576. public boolean canMove(ILocational from, ILocational to)
  577. {
  578. return canMove(from, to.getX(), to.getY(), to.getZ());
  579. }
  580. /**
  581. * Checks the specified position for available geodata.
  582. * @param x the X coordinate
  583. * @param y the Y coordinate
  584. * @return {@code true} if there is geodata for the given coordinates, {@code false} otherwise
  585. */
  586. public boolean hasGeo(int x, int y)
  587. {
  588. return hasGeoPos(getGeoX(x), getGeoY(y));
  589. }
  590. public static GeoData getInstance()
  591. {
  592. return SingletonHolder._instance;
  593. }
  594. private static class SingletonHolder
  595. {
  596. protected final static GeoData _instance = new GeoData();
  597. }
  598. }