GeoData.java 20 KB

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