GeoData.java 20 KB

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