GeoData.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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 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 LOGGER = 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. LOGGER.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. LOGGER.info("{}: Loading {}...", getClass().getSimpleName(), geoFilePath.getFileName());
  72. _driver.loadRegion(geoFilePath, regionX, regionY);
  73. loadedRegions++;
  74. }
  75. catch (Exception e)
  76. {
  77. LOGGER.warn("{}: Failed to load {}!", getClass().getSimpleName(), geoFilePath.getFileName(), e);
  78. }
  79. }
  80. }
  81. }
  82. }
  83. catch (Exception e)
  84. {
  85. LOGGER.error("{}: Failed to load geodata!", e);
  86. System.exit(1);
  87. }
  88. LOGGER.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.isDoor())
  200. {
  201. // can always see doors :o
  202. return true;
  203. }
  204. return canSeeTarget(cha.getX(), cha.getY(), cha.getZ(), cha.getInstanceId(), target.getX(), target.getY(), target.getZ(), target.getInstanceId());
  205. }
  206. /**
  207. * Can see target. Checks doors between.
  208. * @param cha the character
  209. * @param worldPosition the world position
  210. * @return {@code true} if the character can see the target at the given world position, {@code false} otherwise
  211. */
  212. public boolean canSeeTarget(L2Object cha, ILocational worldPosition)
  213. {
  214. return canSeeTarget(cha.getX(), cha.getY(), cha.getZ(), cha.getInstanceId(), worldPosition.getX(), worldPosition.getY(), worldPosition.getZ());
  215. }
  216. /**
  217. * Can see target. Checks doors between.
  218. * @param x the x coordinate
  219. * @param y the y coordinate
  220. * @param z the z coordinate
  221. * @param instanceId
  222. * @param tx the target's x coordinate
  223. * @param ty the target's y coordinate
  224. * @param tz the target's z coordinate
  225. * @param tInstanceId the target's instanceId
  226. * @return
  227. */
  228. public boolean canSeeTarget(int x, int y, int z, int instanceId, int tx, int ty, int tz, int tInstanceId)
  229. {
  230. if ((instanceId != tInstanceId))
  231. {
  232. return false;
  233. }
  234. return canSeeTarget(x, y, z, instanceId, tx, ty, tz);
  235. }
  236. /**
  237. * Can see target. Checks doors between.
  238. * @param x the x coordinate
  239. * @param y the y coordinate
  240. * @param z the z coordinate
  241. * @param instanceId
  242. * @param tx the target's x coordinate
  243. * @param ty the target's y coordinate
  244. * @param tz the target's z coordinate
  245. * @return {@code true} if there is line of sight between the given coordinate sets, {@code false} otherwise
  246. */
  247. public boolean canSeeTarget(int x, int y, int z, int instanceId, int tx, int ty, int tz)
  248. {
  249. if (DoorData.getInstance().checkIfDoorsBetween(x, y, z, tx, ty, tz, instanceId, true))
  250. {
  251. return false;
  252. }
  253. return canSeeTarget(x, y, z, tx, ty, tz);
  254. }
  255. private int getLosGeoZ(int prevX, int prevY, int prevGeoZ, int curX, int curY, int nswe)
  256. {
  257. if ((((nswe & Cell.NSWE_NORTH) != 0) && ((nswe & Cell.NSWE_SOUTH) != 0)) || (((nswe & Cell.NSWE_WEST) != 0) && ((nswe & Cell.NSWE_EAST) != 0)))
  258. {
  259. throw new RuntimeException("Multiple directions!");
  260. }
  261. if (checkNearestNsweAntiCornerCut(prevX, prevY, prevGeoZ, nswe))
  262. {
  263. return getNearestZ(curX, curY, prevGeoZ);
  264. }
  265. return getNextHigherZ(curX, curY, prevGeoZ);
  266. }
  267. /**
  268. * Can see target. Does not check doors between.
  269. * @param x the x coordinate
  270. * @param y the y coordinate
  271. * @param z the z coordinate
  272. * @param tx the target's x coordinate
  273. * @param ty the target's y coordinate
  274. * @param tz the target's z coordinate
  275. * @return {@code true} if there is line of sight between the given coordinate sets, {@code false} otherwise
  276. */
  277. public boolean canSeeTarget(int x, int y, int z, int tx, int ty, int tz)
  278. {
  279. int geoX = getGeoX(x);
  280. int geoY = getGeoY(y);
  281. int tGeoX = getGeoX(tx);
  282. int tGeoY = getGeoY(ty);
  283. z = getNearestZ(geoX, geoY, z);
  284. tz = getNearestZ(tGeoX, tGeoY, tz);
  285. // fastpath
  286. if ((geoX == tGeoX) && (geoY == tGeoY))
  287. {
  288. if (hasGeoPos(tGeoX, tGeoY))
  289. {
  290. return z == tz;
  291. }
  292. return true;
  293. }
  294. if (tz > z)
  295. {
  296. int tmp = tx;
  297. tx = x;
  298. x = tmp;
  299. tmp = ty;
  300. ty = y;
  301. y = tmp;
  302. tmp = tz;
  303. tz = z;
  304. z = tmp;
  305. tmp = tGeoX;
  306. tGeoX = geoX;
  307. geoX = tmp;
  308. tmp = tGeoY;
  309. tGeoY = geoY;
  310. geoY = tmp;
  311. }
  312. LinePointIterator3D pointIter = new LinePointIterator3D(geoX, geoY, z, tGeoX, tGeoY, tz);
  313. // first point is guaranteed to be available, skip it, we can always see our own position
  314. pointIter.next();
  315. int prevX = pointIter.x();
  316. int prevY = pointIter.y();
  317. int prevZ = pointIter.z();
  318. int prevGeoZ = prevZ;
  319. int ptIndex = 0;
  320. while (pointIter.next())
  321. {
  322. int curX = pointIter.x();
  323. int curY = pointIter.y();
  324. if ((curX == prevX) && (curY == prevY))
  325. {
  326. continue;
  327. }
  328. int beeCurZ = pointIter.z();
  329. int curGeoZ = prevGeoZ;
  330. // check if the position has geodata
  331. if (hasGeoPos(curX, curY))
  332. {
  333. int beeCurGeoZ = getNearestZ(curX, curY, beeCurZ);
  334. int nswe = GeoUtils.computeNswe(prevX, prevY, curX, curY);// .computeDirection(prevX, prevY, curX, curY);
  335. curGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, curX, curY, nswe);
  336. int maxHeight;
  337. if (ptIndex < ELEVATED_SEE_OVER_DISTANCE)
  338. {
  339. maxHeight = z + MAX_SEE_OVER_HEIGHT;
  340. }
  341. else
  342. {
  343. maxHeight = beeCurZ + MAX_SEE_OVER_HEIGHT;
  344. }
  345. boolean canSeeThrough = false;
  346. if ((curGeoZ <= maxHeight) && (curGeoZ <= beeCurGeoZ))
  347. {
  348. if ((nswe & Cell.NSWE_NORTH_EAST) == Cell.NSWE_NORTH_EAST)
  349. {
  350. int northGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX, prevY - 1, Cell.NSWE_EAST);
  351. int eastGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX + 1, prevY, Cell.NSWE_NORTH);
  352. canSeeThrough = (northGeoZ <= maxHeight) && (eastGeoZ <= maxHeight) && (northGeoZ <= getNearestZ(prevX, prevY - 1, beeCurZ)) && (eastGeoZ <= getNearestZ(prevX + 1, prevY, beeCurZ));
  353. }
  354. else if ((nswe & Cell.NSWE_NORTH_WEST) == Cell.NSWE_NORTH_WEST)
  355. {
  356. int northGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX, prevY - 1, Cell.NSWE_WEST);
  357. int westGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX - 1, prevY, Cell.NSWE_NORTH);
  358. canSeeThrough = (northGeoZ <= maxHeight) && (westGeoZ <= maxHeight) && (northGeoZ <= getNearestZ(prevX, prevY - 1, beeCurZ)) && (westGeoZ <= getNearestZ(prevX - 1, prevY, beeCurZ));
  359. }
  360. else if ((nswe & Cell.NSWE_SOUTH_EAST) == Cell.NSWE_SOUTH_EAST)
  361. {
  362. int southGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX, prevY + 1, Cell.NSWE_EAST);
  363. int eastGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX + 1, prevY, Cell.NSWE_SOUTH);
  364. canSeeThrough = (southGeoZ <= maxHeight) && (eastGeoZ <= maxHeight) && (southGeoZ <= getNearestZ(prevX, prevY + 1, beeCurZ)) && (eastGeoZ <= getNearestZ(prevX + 1, prevY, beeCurZ));
  365. }
  366. else if ((nswe & Cell.NSWE_SOUTH_WEST) == Cell.NSWE_SOUTH_WEST)
  367. {
  368. int southGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX, prevY + 1, Cell.NSWE_WEST);
  369. int westGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX - 1, prevY, Cell.NSWE_SOUTH);
  370. canSeeThrough = (southGeoZ <= maxHeight) && (westGeoZ <= maxHeight) && (southGeoZ <= getNearestZ(prevX, prevY + 1, beeCurZ)) && (westGeoZ <= getNearestZ(prevX - 1, prevY, beeCurZ));
  371. }
  372. else
  373. {
  374. canSeeThrough = true;
  375. }
  376. }
  377. if (!canSeeThrough)
  378. {
  379. return false;
  380. }
  381. }
  382. prevX = curX;
  383. prevY = curY;
  384. prevGeoZ = curGeoZ;
  385. ++ptIndex;
  386. }
  387. return true;
  388. }
  389. /**
  390. * Verifies if the is a path between origin's location and destination, if not returns the closest location.
  391. * @param origin the origin
  392. * @param destination the destination
  393. * @return the destination if there is a path or the closes location
  394. */
  395. public Location moveCheck(ILocational origin, ILocational destination)
  396. {
  397. return moveCheck(origin.getX(), origin.getY(), origin.getZ(), destination.getX(), destination.getY(), destination.getZ(), origin.getInstanceId());
  398. }
  399. /**
  400. * Move check.
  401. * @param x the x coordinate
  402. * @param y the y coordinate
  403. * @param z the z coordinate
  404. * @param tx the target's x coordinate
  405. * @param ty the target's y coordinate
  406. * @param tz the target's z coordinate
  407. * @param instanceId the instance id
  408. * @return the last Location (x,y,z) where player can walk - just before wall
  409. */
  410. public Location moveCheck(int x, int y, int z, int tx, int ty, int tz, int instanceId)
  411. {
  412. int geoX = getGeoX(x);
  413. int geoY = getGeoY(y);
  414. z = getNearestZ(geoX, geoY, z);
  415. int tGeoX = getGeoX(tx);
  416. int tGeoY = getGeoY(ty);
  417. tz = getNearestZ(tGeoX, tGeoY, tz);
  418. if (DoorData.getInstance().checkIfDoorsBetween(x, y, z, tx, ty, tz, instanceId, false))
  419. {
  420. return new Location(x, y, getHeight(x, y, z));
  421. }
  422. LinePointIterator pointIter = new LinePointIterator(geoX, geoY, tGeoX, tGeoY);
  423. // first point is guaranteed to be available
  424. pointIter.next();
  425. int prevX = pointIter.x();
  426. int prevY = pointIter.y();
  427. int prevZ = z;
  428. while (pointIter.next())
  429. {
  430. int curX = pointIter.x();
  431. int curY = pointIter.y();
  432. int curZ = getNearestZ(curX, curY, prevZ);
  433. if (hasGeoPos(prevX, prevY))
  434. {
  435. int nswe = GeoUtils.computeNswe(prevX, prevY, curX, curY);
  436. if (!checkNearestNsweAntiCornerCut(prevX, prevY, prevZ, nswe))
  437. {
  438. // can't move, return previous location
  439. return new Location(getWorldX(prevX), getWorldY(prevY), prevZ);
  440. }
  441. }
  442. prevX = curX;
  443. prevY = curY;
  444. prevZ = curZ;
  445. }
  446. if (hasGeoPos(prevX, prevY) && (prevZ != tz))
  447. {
  448. // different floors, return start location
  449. return new Location(x, y, z);
  450. }
  451. return new Location(tx, ty, tz);
  452. }
  453. /**
  454. * Checks if its possible to move from one location to another.
  455. * @param fromX the X coordinate to start checking from
  456. * @param fromY the Y coordinate to start checking from
  457. * @param fromZ the Z coordinate to start checking from
  458. * @param toX the X coordinate to end checking at
  459. * @param toY the Y coordinate to end checking at
  460. * @param toZ the Z coordinate to end checking at
  461. * @param instanceId the instance ID
  462. * @return {@code true} if the character at start coordinates can move to end coordinates, {@code false} otherwise
  463. */
  464. public boolean canMove(int fromX, int fromY, int fromZ, int toX, int toY, int toZ, int instanceId)
  465. {
  466. int geoX = getGeoX(fromX);
  467. int geoY = getGeoY(fromY);
  468. fromZ = getNearestZ(geoX, geoY, fromZ);
  469. int tGeoX = getGeoX(toX);
  470. int tGeoY = getGeoY(toY);
  471. toZ = getNearestZ(tGeoX, tGeoY, toZ);
  472. if (DoorData.getInstance().checkIfDoorsBetween(fromX, fromY, fromZ, toX, toY, toZ, instanceId, false))
  473. {
  474. return false;
  475. }
  476. LinePointIterator pointIter = new LinePointIterator(geoX, geoY, tGeoX, tGeoY);
  477. // first point is guaranteed to be available
  478. pointIter.next();
  479. int prevX = pointIter.x();
  480. int prevY = pointIter.y();
  481. int prevZ = fromZ;
  482. while (pointIter.next())
  483. {
  484. int curX = pointIter.x();
  485. int curY = pointIter.y();
  486. int curZ = getNearestZ(curX, curY, prevZ);
  487. if (hasGeoPos(prevX, prevY))
  488. {
  489. int nswe = GeoUtils.computeNswe(prevX, prevY, curX, curY);
  490. if (!checkNearestNsweAntiCornerCut(prevX, prevY, prevZ, nswe))
  491. {
  492. return false;
  493. }
  494. }
  495. prevX = curX;
  496. prevY = curY;
  497. prevZ = curZ;
  498. }
  499. if (hasGeoPos(prevX, prevY) && (prevZ != toZ))
  500. {
  501. // different floors
  502. return false;
  503. }
  504. return true;
  505. }
  506. public int traceTerrainZ(int x, int y, int z, int tx, int ty)
  507. {
  508. int geoX = getGeoX(x);
  509. int geoY = getGeoY(y);
  510. z = getNearestZ(geoX, geoY, z);
  511. int tGeoX = getGeoX(tx);
  512. int tGeoY = getGeoY(ty);
  513. LinePointIterator pointIter = new LinePointIterator(geoX, geoY, tGeoX, tGeoY);
  514. // first point is guaranteed to be available
  515. pointIter.next();
  516. int prevZ = z;
  517. while (pointIter.next())
  518. {
  519. int curX = pointIter.x();
  520. int curY = pointIter.y();
  521. int curZ = getNearestZ(curX, curY, prevZ);
  522. prevZ = curZ;
  523. }
  524. return prevZ;
  525. }
  526. /**
  527. * Checks if its possible to move from one location to another.
  528. * @param from the {@code ILocational} to start checking from
  529. * @param toX the X coordinate to end checking at
  530. * @param toY the Y coordinate to end checking at
  531. * @param toZ the Z coordinate to end checking at
  532. * @return {@code true} if the character at start coordinates can move to end coordinates, {@code false} otherwise
  533. */
  534. public boolean canMove(ILocational from, int toX, int toY, int toZ)
  535. {
  536. return canMove(from.getX(), from.getY(), from.getZ(), toX, toY, toZ, from.getInstanceId());
  537. }
  538. /**
  539. * Checks if its possible to move from one location to another.
  540. * @param from the {@code ILocational} to start checking from
  541. * @param to the {@code ILocational} to end checking at
  542. * @return {@code true} if the character at start coordinates can move to end coordinates, {@code false} otherwise
  543. */
  544. public boolean canMove(ILocational from, ILocational to)
  545. {
  546. return canMove(from, to.getX(), to.getY(), to.getZ());
  547. }
  548. /**
  549. * Checks the specified position for available geodata.
  550. * @param x the X coordinate
  551. * @param y the Y coordinate
  552. * @return {@code true} if there is geodata for the given coordinates, {@code false} otherwise
  553. */
  554. public boolean hasGeo(int x, int y)
  555. {
  556. return hasGeoPos(getGeoX(x), getGeoY(y));
  557. }
  558. public static GeoData getInstance()
  559. {
  560. return SingletonHolder._instance;
  561. }
  562. private static class SingletonHolder
  563. {
  564. protected final static GeoData _instance = new GeoData();
  565. }
  566. }