GeoEngine.java 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver;
  16. import gnu.trove.map.hash.TShortObjectHashMap;
  17. import java.io.BufferedOutputStream;
  18. import java.io.BufferedReader;
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.FileReader;
  22. import java.io.LineNumberReader;
  23. import java.io.RandomAccessFile;
  24. import java.nio.ByteBuffer;
  25. import java.nio.ByteOrder;
  26. import java.nio.IntBuffer;
  27. import java.nio.MappedByteBuffer;
  28. import java.nio.channels.FileChannel;
  29. import java.util.StringTokenizer;
  30. import java.util.logging.Level;
  31. import java.util.logging.Logger;
  32. import com.l2jserver.Config;
  33. import com.l2jserver.gameserver.datatables.DoorTable;
  34. import com.l2jserver.gameserver.model.L2Object;
  35. import com.l2jserver.gameserver.model.L2Spawn;
  36. import com.l2jserver.gameserver.model.L2World;
  37. import com.l2jserver.gameserver.model.Location;
  38. import com.l2jserver.gameserver.model.actor.instance.L2DefenderInstance;
  39. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  40. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  41. import com.l2jserver.gameserver.util.Point3D;
  42. /**
  43. * @author -Nemesiss-
  44. */
  45. public class GeoEngine extends GeoData
  46. {
  47. private static final Logger _log = Logger.getLogger(GeoEngine.class.getName());
  48. private static final byte EAST = 1;
  49. private static final byte WEST = 2;
  50. private static final byte SOUTH = 4;
  51. private static final byte NORTH = 8;
  52. private static final byte NSWE_ALL = 15;
  53. private static TShortObjectHashMap<MappedByteBuffer> _geodata = new TShortObjectHashMap<MappedByteBuffer>();
  54. private static TShortObjectHashMap<IntBuffer> _geodataIndex = new TShortObjectHashMap<IntBuffer>();
  55. private static BufferedOutputStream _geoBugsOut;
  56. public static GeoEngine getInstance()
  57. {
  58. return SingletonHolder._instance;
  59. }
  60. private GeoEngine()
  61. {
  62. nInitGeodata();
  63. }
  64. //Public Methods
  65. /**
  66. * @see com.l2jserver.gameserver.GeoData#getType(int, int)
  67. */
  68. @Override
  69. public short getType(int x, int y)
  70. {
  71. return nGetType((x - L2World.MAP_MIN_X) >> 4, (y - L2World.MAP_MIN_Y) >> 4);
  72. }
  73. /**
  74. * @see com.l2jserver.gameserver.GeoData#getHeight(int, int, int)
  75. */
  76. @Override
  77. public short getHeight(int x, int y, int z)
  78. {
  79. return nGetHeight((x - L2World.MAP_MIN_X) >> 4, (y - L2World.MAP_MIN_Y) >> 4, z);
  80. }
  81. @Override
  82. public short getSpawnHeight(int x, int y, int zmin, int zmax, L2Spawn spawn)
  83. {
  84. return nGetSpawnHeight((x - L2World.MAP_MIN_X) >> 4, (y - L2World.MAP_MIN_Y) >> 4, zmin, zmax, spawn);
  85. }
  86. /**
  87. * @see com.l2jserver.gameserver.GeoData#geoPosition(int, int)
  88. */
  89. @Override
  90. public String geoPosition(int x, int y)
  91. {
  92. int gx = (x - L2World.MAP_MIN_X) >> 4;
  93. int gy = (y - L2World.MAP_MIN_Y) >> 4;
  94. return "bx: " + getBlock(gx) + " by: " + getBlock(gy) + " cx: " + getCell(gx) + " cy: " + getCell(gy) + " region offset: "
  95. + getRegionOffset(gx, gy);
  96. }
  97. /**
  98. * @see com.l2jserver.gameserver.GeoData#canSeeTarget(L2Object, Point3D)
  99. */
  100. @Override
  101. public boolean canSeeTarget(L2Object cha, Point3D target)
  102. {
  103. if (DoorTable.getInstance().checkIfDoorsBetween(cha.getX(), cha.getY(), cha.getZ(), target.getX(), target.getY(), target.getZ(), cha.getInstanceId()))
  104. return false;
  105. if (cha.getZ() >= target.getZ())
  106. return canSeeTarget(cha.getX(), cha.getY(), cha.getZ(), target.getX(), target.getY(), target.getZ());
  107. return canSeeTarget(target.getX(), target.getY(), target.getZ(), cha.getX(), cha.getY(), cha.getZ());
  108. }
  109. /**
  110. * @see com.l2jserver.gameserver.GeoData#canSeeTarget(com.l2jserver.gameserver.model.L2Object, com.l2jserver.gameserver.model.L2Object)
  111. */
  112. @Override
  113. public boolean canSeeTarget(L2Object cha, L2Object target)
  114. {
  115. if (cha == null || target == null)
  116. return false;
  117. // To be able to see over fences and give the player the viewpoint
  118. // game client has, all coordinates are lifted 45 from ground.
  119. // Because of layer selection in LOS algorithm (it selects -45 there
  120. // and some layers can be very close...) do not change this without
  121. // changing the LOS code.
  122. // Basically the +45 is character height. Raid bosses are naturally higher,
  123. // dwarves shorter, but this should work relatively well.
  124. // If this is going to be improved, use e.g.
  125. // ((L2Character)cha).getTemplate().collisionHeight
  126. int z = cha.getZ() + 45;
  127. if (cha instanceof L2DefenderInstance)
  128. z += 30; // well they don't move closer to balcony fence at the moment :(
  129. int z2 = target.getZ() + 45;
  130. if (!(target instanceof L2DoorInstance)
  131. && DoorTable.getInstance().checkIfDoorsBetween(cha.getX(), cha.getY(), z, target.getX(), target.getY(), z2, cha.getInstanceId()))
  132. return false;
  133. if (target instanceof L2DoorInstance)
  134. return true; // door coordinates are hinge coords..
  135. if (target instanceof L2DefenderInstance)
  136. z2 += 30; // well they don't move closer to balcony fence at the moment :(
  137. if (cha.getZ() >= target.getZ())
  138. return canSeeTarget(cha.getX(), cha.getY(), z, target.getX(), target.getY(), z2);
  139. return canSeeTarget(target.getX(), target.getY(), z2, cha.getX(), cha.getY(), z);
  140. }
  141. /**
  142. * @see com.l2jserver.gameserver.GeoData#canSeeTargetDebug(com.l2jserver.gameserver.model.actor.instance.L2PcInstance, com.l2jserver.gameserver.model.L2Object)
  143. */
  144. @Override
  145. public boolean canSeeTargetDebug(L2PcInstance gm, L2Object target)
  146. {
  147. // comments: see above
  148. int z = gm.getZ() + 45;
  149. int z2 = target.getZ() + 45;
  150. if (target instanceof L2DoorInstance)
  151. {
  152. gm.sendMessage("door always true");
  153. return true; // door coordinates are hinge coords..
  154. }
  155. if (gm.getZ() >= target.getZ())
  156. return canSeeDebug(gm, (gm.getX() - L2World.MAP_MIN_X) >> 4, (gm.getY() - L2World.MAP_MIN_Y) >> 4, z, (target.getX() - L2World.MAP_MIN_X) >> 4, (target.getY() - L2World.MAP_MIN_Y) >> 4, z2);
  157. return canSeeDebug(gm, (target.getX() - L2World.MAP_MIN_X) >> 4, (target.getY() - L2World.MAP_MIN_Y) >> 4, z2, (gm.getX() - L2World.MAP_MIN_X) >> 4, (gm.getY() - L2World.MAP_MIN_Y) >> 4, z);
  158. }
  159. /**
  160. * @see com.l2jserver.gameserver.GeoData#getNSWE(int, int, int)
  161. */
  162. @Override
  163. public short getNSWE(int x, int y, int z)
  164. {
  165. return nGetNSWE((x - L2World.MAP_MIN_X) >> 4, (y - L2World.MAP_MIN_Y) >> 4, z);
  166. }
  167. @Override
  168. public boolean canMoveFromToTarget(int x, int y, int z, int tx, int ty, int tz, int instanceId)
  169. {
  170. Location destiny = moveCheck(x, y, z, tx, ty, tz, instanceId);
  171. return (destiny.getX() == tx && destiny.getY() == ty && destiny.getZ() == tz);
  172. }
  173. /**
  174. * @see com.l2jserver.gameserver.GeoData#moveCheck(int, int, int, int, int, int, int)
  175. */
  176. @Override
  177. public Location moveCheck(int x, int y, int z, int tx, int ty, int tz, int instanceId)
  178. {
  179. Location startpoint = new Location(x, y, z);
  180. if (DoorTable.getInstance().checkIfDoorsBetween(x, y, z, tx, ty, tz, instanceId))
  181. return startpoint;
  182. Location destiny = new Location(tx, ty, tz);
  183. return moveCheck(startpoint, destiny, (x - L2World.MAP_MIN_X) >> 4, (y - L2World.MAP_MIN_Y) >> 4, z, (tx - L2World.MAP_MIN_X) >> 4, (ty - L2World.MAP_MIN_Y) >> 4, tz);
  184. }
  185. /**
  186. * @see com.l2jserver.gameserver.GeoData#addGeoDataBug(com.l2jserver.gameserver.model.actor.instance.L2PcInstance, java.lang.String)
  187. */
  188. @Override
  189. public void addGeoDataBug(L2PcInstance gm, String comment)
  190. {
  191. int gx = (gm.getX() - L2World.MAP_MIN_X) >> 4;
  192. int gy = (gm.getY() - L2World.MAP_MIN_Y) >> 4;
  193. int bx = getBlock(gx);
  194. int by = getBlock(gy);
  195. int cx = getCell(gx);
  196. int cy = getCell(gy);
  197. int rx = (gx >> 11) + Config.WORLD_X_MIN;
  198. int ry = (gy >> 11) + Config.WORLD_X_MAX;
  199. String out = rx + ";" + ry + ";" + bx + ";" + by + ";" + cx + ";" + cy + ";" + gm.getZ() + ";" + comment + "\n";
  200. try
  201. {
  202. _geoBugsOut.write(out.getBytes());
  203. _geoBugsOut.flush();
  204. gm.sendMessage("GeoData bug saved!");
  205. }
  206. catch (Exception e)
  207. {
  208. _log.log(Level.WARNING, "", e);
  209. gm.sendMessage("GeoData bug save Failed!");
  210. }
  211. }
  212. @Override
  213. public boolean canSeeTarget(int x, int y, int z, int tx, int ty, int tz)
  214. {
  215. return canSee((x - L2World.MAP_MIN_X) >> 4, (y - L2World.MAP_MIN_Y) >> 4, z, (tx - L2World.MAP_MIN_X) >> 4, (ty - L2World.MAP_MIN_Y) >> 4, tz);
  216. }
  217. @Override
  218. public boolean hasGeo(int x, int y)
  219. {
  220. int gx = (x - L2World.MAP_MIN_X) >> 4;
  221. int gy = (y - L2World.MAP_MIN_Y) >> 4;
  222. short region = getRegionOffset(gx, gy);
  223. if (_geodata.contains(region))
  224. return true;
  225. return false;
  226. }
  227. private static boolean canSee(int x, int y, double z, int tx, int ty, int tz)
  228. {
  229. int dx = (tx - x);
  230. int dy = (ty - y);
  231. final double dz = (tz - z);
  232. final int distance2 = dx * dx + dy * dy;
  233. if (distance2 > 90000) // (300*300) 300*16 = 4800 in world coord
  234. {
  235. //Avoid too long check
  236. return false;
  237. }
  238. // very short checks: 9 => 144 world distance
  239. // this ensures NLOS function has enough points to calculate,
  240. // it might not work when distance is small and path vertical
  241. else if (distance2 < 82)
  242. {
  243. // 150 should be too deep/high.
  244. if (dz * dz > 22500)
  245. {
  246. short region = getRegionOffset(x, y);
  247. // geodata is loaded for region and mobs should have correct Z coordinate...
  248. // so there would likely be a floor in between the two
  249. if (_geodata.contains(region))
  250. return false;
  251. }
  252. return true;
  253. }
  254. // Increment in Z coordinate when moving along X or Y axis
  255. // and not straight to the target. This is done because
  256. // calculation moves either in X or Y direction.
  257. final int inc_x = sign(dx);
  258. final int inc_y = sign(dy);
  259. dx = Math.abs(dx);
  260. dy = Math.abs(dy);
  261. final double inc_z_directionx = dz * dx / (distance2);
  262. final double inc_z_directiony = dz * dy / (distance2);
  263. // next_* are used in NLOS check from x,y
  264. int next_x = x;
  265. int next_y = y;
  266. // creates path to the target
  267. // calculation stops when next_* == target
  268. if (dx >= dy)// dy/dx <= 1
  269. {
  270. int delta_A = 2 * dy;
  271. int d = delta_A - dx;
  272. int delta_B = delta_A - 2 * dx;
  273. for (int i = 0; i < dx; i++)
  274. {
  275. x = next_x;
  276. y = next_y;
  277. if (d > 0)
  278. {
  279. d += delta_B;
  280. next_x += inc_x;
  281. z += inc_z_directionx;
  282. if (!nLOS(x, y, (int) z, inc_x, 0, inc_z_directionx, tz, false))
  283. return false;
  284. next_y += inc_y;
  285. z += inc_z_directiony;
  286. //_log.warning("1: next_x:"+next_x+" next_y"+next_y);
  287. if (!nLOS(next_x, y, (int) z, 0, inc_y, inc_z_directiony, tz, false))
  288. return false;
  289. }
  290. else
  291. {
  292. d += delta_A;
  293. next_x += inc_x;
  294. //_log.warning("2: next_x:"+next_x+" next_y"+next_y);
  295. z += inc_z_directionx;
  296. if (!nLOS(x, y, (int) z, inc_x, 0, inc_z_directionx, tz, false))
  297. return false;
  298. }
  299. }
  300. }
  301. else
  302. {
  303. int delta_A = 2 * dx;
  304. int d = delta_A - dy;
  305. int delta_B = delta_A - 2 * dy;
  306. for (int i = 0; i < dy; i++)
  307. {
  308. x = next_x;
  309. y = next_y;
  310. if (d > 0)
  311. {
  312. d += delta_B;
  313. next_y += inc_y;
  314. z += inc_z_directiony;
  315. if (!nLOS(x, y, (int) z, 0, inc_y, inc_z_directiony, tz, false))
  316. return false;
  317. next_x += inc_x;
  318. z += inc_z_directionx;
  319. //_log.warning("3: next_x:"+next_x+" next_y"+next_y);
  320. if (!nLOS(x, next_y, (int) z, inc_x, 0, inc_z_directionx, tz, false))
  321. return false;
  322. }
  323. else
  324. {
  325. d += delta_A;
  326. next_y += inc_y;
  327. //_log.warning("4: next_x:"+next_x+" next_y"+next_y);
  328. z += inc_z_directiony;
  329. if (!nLOS(x, y, (int) z, 0, inc_y, inc_z_directiony, tz, false))
  330. return false;
  331. }
  332. }
  333. }
  334. return true;
  335. }
  336. /*
  337. * Debug function for checking if there's a line of sight between
  338. * two coordinates.
  339. *
  340. * Creates points for line of sight check (x,y,z towards target) and
  341. * in each point, layer and movement checks are made with NLOS function.
  342. *
  343. * Coordinates here are geodata x,y but z coordinate is world coordinate
  344. */
  345. private static boolean canSeeDebug(L2PcInstance gm, int x, int y, double z, int tx, int ty, int tz)
  346. {
  347. int dx = (tx - x);
  348. int dy = (ty - y);
  349. final double dz = (tz - z);
  350. final int distance2 = dx * dx + dy * dy;
  351. if (distance2 > 90000) // (300*300) 300*16 = 4800 in world coord
  352. {
  353. //Avoid too long check
  354. gm.sendMessage("dist > 300");
  355. return false;
  356. }
  357. // very short checks: 9 => 144 world distance
  358. // this ensures NLOS function has enough points to calculate,
  359. // it might not work when distance is small and path vertical
  360. else if (distance2 < 82)
  361. {
  362. // 150 should be too deep/high.
  363. if (dz * dz > 22500)
  364. {
  365. short region = getRegionOffset(x, y);
  366. // geodata is loaded for region and mobs should have correct Z coordinate...
  367. // so there would likely be a floor in between the two
  368. if (_geodata.get(region) != null)
  369. return false;
  370. }
  371. return true;
  372. }
  373. // Increment in Z coordinate when moving along X or Y axis
  374. // and not straight to the target. This is done because
  375. // calculation moves either in X or Y direction.
  376. final int inc_x = sign(dx);
  377. final int inc_y = sign(dy);
  378. dx = Math.abs(dx);
  379. dy = Math.abs(dy);
  380. final double inc_z_directionx = dz * dx / (distance2);
  381. final double inc_z_directiony = dz * dy / (distance2);
  382. gm.sendMessage("Los: from X: " + x + "Y: " + y + "--->> X: " + tx + " Y: " + ty);
  383. // next_* are used in NLOS check from x,y
  384. int next_x = x;
  385. int next_y = y;
  386. // creates path to the target
  387. // calculation stops when next_* == target
  388. if (dx >= dy)// dy/dx <= 1
  389. {
  390. int delta_A = 2 * dy;
  391. int d = delta_A - dx;
  392. int delta_B = delta_A - 2 * dx;
  393. for (int i = 0; i < dx; i++)
  394. {
  395. x = next_x;
  396. y = next_y;
  397. if (d > 0)
  398. {
  399. d += delta_B;
  400. next_x += inc_x;
  401. z += inc_z_directionx;
  402. if (!nLOS(x, y, (int) z, inc_x, 0, inc_z_directionx, tz, true))
  403. return false;
  404. next_y += inc_y;
  405. z += inc_z_directiony;
  406. //_log.warning("1: next_x:"+next_x+" next_y"+next_y);
  407. if (!nLOS(next_x, y, (int) z, 0, inc_y, inc_z_directiony, tz, true))
  408. return false;
  409. }
  410. else
  411. {
  412. d += delta_A;
  413. next_x += inc_x;
  414. //_log.warning("2: next_x:"+next_x+" next_y"+next_y);
  415. z += inc_z_directionx;
  416. if (!nLOS(x, y, (int) z, inc_x, 0, inc_z_directionx, tz, true))
  417. return false;
  418. }
  419. }
  420. }
  421. else
  422. {
  423. int delta_A = 2 * dx;
  424. int d = delta_A - dy;
  425. int delta_B = delta_A - 2 * dy;
  426. for (int i = 0; i < dy; i++)
  427. {
  428. x = next_x;
  429. y = next_y;
  430. if (d > 0)
  431. {
  432. d += delta_B;
  433. next_y += inc_y;
  434. z += inc_z_directiony;
  435. if (!nLOS(x, y, (int) z, 0, inc_y, inc_z_directiony, tz, true))
  436. return false;
  437. next_x += inc_x;
  438. z += inc_z_directionx;
  439. //_log.warning("3: next_x:"+next_x+" next_y"+next_y);
  440. if (!nLOS(x, next_y, (int) z, inc_x, 0, inc_z_directionx, tz, true))
  441. return false;
  442. }
  443. else
  444. {
  445. d += delta_A;
  446. next_y += inc_y;
  447. //_log.warning("4: next_x:"+next_x+" next_y"+next_y);
  448. z += inc_z_directiony;
  449. if (!nLOS(x, y, (int) z, 0, inc_y, inc_z_directiony, tz, true))
  450. return false;
  451. }
  452. }
  453. }
  454. return true;
  455. }
  456. /*
  457. * MoveCheck
  458. */
  459. private static Location moveCheck(Location startpoint, Location destiny, int x, int y, double z, int tx, int ty, int tz)
  460. {
  461. int dx = (tx - x);
  462. int dy = (ty - y);
  463. final int distance2 = dx * dx + dy * dy;
  464. if (distance2 == 0)
  465. return destiny;
  466. if (distance2 > 36100) // 190*190*16 = 3040 world coord
  467. {
  468. // Avoid too long check
  469. // Currently we calculate a middle point
  470. // for wyvern users and otherwise for comfort
  471. double divider = Math.sqrt((double) 30000 / distance2);
  472. tx = x + (int) (divider * dx);
  473. ty = y + (int) (divider * dy);
  474. int dz = (tz - startpoint.getZ());
  475. tz = startpoint.getZ() + (int) (divider * dz);
  476. dx = (tx - x);
  477. dy = (ty - y);
  478. //return startpoint;
  479. }
  480. // Increment in Z coordinate when moving along X or Y axis
  481. // and not straight to the target. This is done because
  482. // calculation moves either in X or Y direction.
  483. final int inc_x = sign(dx);
  484. final int inc_y = sign(dy);
  485. dx = Math.abs(dx);
  486. dy = Math.abs(dy);
  487. //gm.sendMessage("MoveCheck: from X: "+x+ "Y: "+y+ "--->> X: "+tx+" Y: "+ty);
  488. // next_* are used in NcanMoveNext check from x,y
  489. int next_x = x;
  490. int next_y = y;
  491. double tempz = z;
  492. // creates path to the target, using only x or y direction
  493. // calculation stops when next_* == target
  494. if (dx >= dy)// dy/dx <= 1
  495. {
  496. int delta_A = 2 * dy;
  497. int d = delta_A - dx;
  498. int delta_B = delta_A - 2 * dx;
  499. for (int i = 0; i < dx; i++)
  500. {
  501. x = next_x;
  502. y = next_y;
  503. if (d > 0)
  504. {
  505. d += delta_B;
  506. next_x += inc_x;
  507. tempz = nCanMoveNext(x, y, (int) z, next_x, next_y, tz);
  508. if (tempz == Double.MIN_VALUE)
  509. return new Location((x << 4) + L2World.MAP_MIN_X, (y << 4) + L2World.MAP_MIN_Y, (int) z);
  510. z = tempz;
  511. next_y += inc_y;
  512. //_log.warning("2: next_x:"+next_x+" next_y"+next_y);
  513. tempz = nCanMoveNext(next_x, y, (int) z, next_x, next_y, tz);
  514. if (tempz == Double.MIN_VALUE)
  515. return new Location((x << 4) + L2World.MAP_MIN_X, (y << 4) + L2World.MAP_MIN_Y, (int) z);
  516. z = tempz;
  517. }
  518. else
  519. {
  520. d += delta_A;
  521. next_x += inc_x;
  522. //_log.warning("3: next_x:"+next_x+" next_y"+next_y);
  523. tempz = nCanMoveNext(x, y, (int) z, next_x, next_y, tz);
  524. if (tempz == Double.MIN_VALUE)
  525. return new Location((x << 4) + L2World.MAP_MIN_X, (y << 4) + L2World.MAP_MIN_Y, (int) z);
  526. z = tempz;
  527. }
  528. }
  529. }
  530. else
  531. {
  532. int delta_A = 2 * dx;
  533. int d = delta_A - dy;
  534. int delta_B = delta_A - 2 * dy;
  535. for (int i = 0; i < dy; i++)
  536. {
  537. x = next_x;
  538. y = next_y;
  539. if (d > 0)
  540. {
  541. d += delta_B;
  542. next_y += inc_y;
  543. tempz = nCanMoveNext(x, y, (int) z, next_x, next_y, tz);
  544. if (tempz == Double.MIN_VALUE)
  545. return new Location((x << 4) + L2World.MAP_MIN_X, (y << 4) + L2World.MAP_MIN_Y, (int) z);
  546. z = tempz;
  547. next_x += inc_x;
  548. //_log.warning("5: next_x:"+next_x+" next_y"+next_y);
  549. tempz = nCanMoveNext(x, next_y, (int) z, next_x, next_y, tz);
  550. if (tempz == Double.MIN_VALUE)
  551. return new Location((x << 4) + L2World.MAP_MIN_X, (y << 4) + L2World.MAP_MIN_Y, (int) z);
  552. z = tempz;
  553. }
  554. else
  555. {
  556. d += delta_A;
  557. next_y += inc_y;
  558. //_log.warning("6: next_x:"+next_x+" next_y"+next_y);
  559. tempz = nCanMoveNext(x, y, (int) z, next_x, next_y, tz);
  560. if (tempz == Double.MIN_VALUE)
  561. return new Location((x << 4) + L2World.MAP_MIN_X, (y << 4) + L2World.MAP_MIN_Y, (int) z);
  562. z = tempz;
  563. }
  564. }
  565. }
  566. if (z == startpoint.getZ()) // geodata hasn't modified Z in any coordinate, i.e. doesn't exist
  567. return destiny;
  568. return new Location(destiny.getX(), destiny.getY(), (int) z);
  569. }
  570. private static byte sign(int x)
  571. {
  572. if (x >= 0)
  573. return +1;
  574. return -1;
  575. }
  576. //GeoEngine
  577. private static void nInitGeodata()
  578. {
  579. LineNumberReader lnr = null;
  580. try
  581. {
  582. _log.info("Geo Engine: - Loading Geodata...");
  583. File Data = new File("./data/geodata/geo_index.txt");
  584. if (!Data.exists())
  585. return;
  586. lnr = new LineNumberReader(new BufferedReader(new FileReader(Data)));
  587. }
  588. catch (Exception e)
  589. {
  590. _log.log(Level.WARNING, "", e);
  591. throw new Error("Failed to Load geo_index File.");
  592. }
  593. String line;
  594. try
  595. {
  596. while ((line = lnr.readLine()) != null)
  597. {
  598. if (line.trim().length() == 0)
  599. continue;
  600. StringTokenizer st = new StringTokenizer(line, "_");
  601. byte rx = Byte.parseByte(st.nextToken());
  602. byte ry = Byte.parseByte(st.nextToken());
  603. loadGeodataFile(rx, ry);
  604. }
  605. }
  606. catch (Exception e)
  607. {
  608. _log.log(Level.WARNING, "", e);
  609. throw new Error("Failed to Read geo_index File.");
  610. }
  611. finally
  612. {
  613. try
  614. {
  615. lnr.close();
  616. }
  617. catch (Exception e)
  618. {
  619. }
  620. }
  621. try
  622. {
  623. File geo_bugs = new File("./data/geodata/geo_bugs.txt");
  624. _geoBugsOut = new BufferedOutputStream(new FileOutputStream(geo_bugs, true));
  625. }
  626. catch (Exception e)
  627. {
  628. _log.log(Level.WARNING, "", e);
  629. throw new Error("Failed to Load geo_bugs.txt File.");
  630. }
  631. }
  632. public static void unloadGeodata(byte rx, byte ry)
  633. {
  634. short regionoffset = (short) ((rx << 5) + ry);
  635. _geodataIndex.remove(regionoffset);
  636. _geodata.remove(regionoffset);
  637. }
  638. public static boolean loadGeodataFile(byte rx, byte ry)
  639. {
  640. if (rx < Config.WORLD_X_MIN || rx > Config.WORLD_X_MAX || ry < Config.WORLD_Y_MIN || ry > Config.WORLD_Y_MAX)
  641. {
  642. _log.warning("Failed to Load GeoFile: invalid region " + rx +","+ ry + "\n");
  643. return false;
  644. }
  645. String fname = "./data/geodata/" + rx + "_" + ry + ".l2j";
  646. short regionoffset = (short) ((rx << 5) + ry);
  647. _log.info("Geo Engine: - Loading: " + fname + " -> region offset: " + regionoffset + "X: " + rx + " Y: " + ry);
  648. File Geo = new File(fname);
  649. int size, index = 0, block = 0, flor = 0;
  650. FileChannel roChannel = null;
  651. try
  652. {
  653. // Create a read-only memory-mapped file
  654. roChannel = new RandomAccessFile(Geo, "r").getChannel();
  655. size = (int) roChannel.size();
  656. MappedByteBuffer geo;
  657. if (Config.FORCE_GEODATA) //Force O/S to Loads this buffer's content into physical memory.
  658. //it is not guarantee, because the underlying operating system may have paged out some of the buffer's data
  659. geo = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
  660. else
  661. geo = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, size);
  662. geo.order(ByteOrder.LITTLE_ENDIAN);
  663. if (size > 196608)
  664. {
  665. // Indexing geo files, so we will know where each block starts
  666. IntBuffer indexs = IntBuffer.allocate(65536);
  667. while (block < 65536)
  668. {
  669. byte type = geo.get(index);
  670. indexs.put(block, index);
  671. block++;
  672. index++;
  673. if (type == 0)
  674. index += 2; // 1x short
  675. else if (type == 1)
  676. index += 128; // 64 x short
  677. else
  678. {
  679. int b;
  680. for (b = 0; b < 64; b++)
  681. {
  682. byte layers = geo.get(index);
  683. index += (layers << 1) + 1;
  684. if (layers > flor)
  685. flor = layers;
  686. }
  687. }
  688. }
  689. _geodataIndex.put(regionoffset, indexs);
  690. }
  691. _geodata.put(regionoffset, geo);
  692. _log.info("Geo Engine: - Max Layers: " + flor + " Size: " + size + " Loaded: " + index);
  693. }
  694. catch (Exception e)
  695. {
  696. _log.log(Level.WARNING, "Failed to Load GeoFile at block: " + block, e);
  697. return false;
  698. }
  699. finally
  700. {
  701. try
  702. {
  703. roChannel.close();
  704. }
  705. catch (Exception e)
  706. {
  707. }
  708. }
  709. return true;
  710. }
  711. //Geodata Methods
  712. /**
  713. * @param x
  714. * @param y
  715. * @return Region Offset
  716. */
  717. private static short getRegionOffset(int x, int y)
  718. {
  719. int rx = x >> 11; // =/(256 * 8)
  720. int ry = y >> 11;
  721. return (short) (((rx + Config.WORLD_X_MIN) << 5) + (ry + Config.WORLD_Y_MIN));
  722. }
  723. /**
  724. * @param geo_pos
  725. * @return Block Index: 0-255
  726. */
  727. private static int getBlock(int geo_pos)
  728. {
  729. return (geo_pos >> 3) % 256;
  730. }
  731. /**
  732. * @param geo_pos
  733. * @return Cell Index: 0-7
  734. */
  735. private static int getCell(int geo_pos)
  736. {
  737. return geo_pos % 8;
  738. }
  739. //Geodata Functions
  740. /**
  741. * @param x
  742. * @param y
  743. * @return Type of geo_block: 0-2
  744. */
  745. private static short nGetType(int x, int y)
  746. {
  747. short region = getRegionOffset(x, y);
  748. int blockX = getBlock(x);
  749. int blockY = getBlock(y);
  750. int index = 0;
  751. final IntBuffer idx = _geodataIndex.get(region);
  752. //Geodata without index - it is just empty so index can be calculated on the fly
  753. if (idx == null)
  754. index = ((blockX << 8) + blockY) * 3;
  755. //Get Index for current block of current geodata region
  756. else
  757. index = idx.get((blockX << 8) + blockY);
  758. //Buffer that Contains current Region GeoData
  759. ByteBuffer geo = _geodata.get(region);
  760. if (geo == null)
  761. {
  762. if (Config.DEBUG)
  763. _log.warning("Geo Region - Region Offset: " + region + " dosnt exist!!");
  764. return 0;
  765. }
  766. return geo.get(index);
  767. }
  768. /**
  769. * @param geox
  770. * @param geoy
  771. * @param z
  772. * @return Nearest Z
  773. */
  774. private static short nGetHeight(int geox, int geoy, int z)
  775. {
  776. short region = getRegionOffset(geox, geoy);
  777. int blockX = getBlock(geox);
  778. int blockY = getBlock(geoy);
  779. int cellX, cellY, index;
  780. final IntBuffer idx = _geodataIndex.get(region);
  781. //Geodata without index - it is just empty so index can be calculated on the fly
  782. if (idx == null)
  783. index = ((blockX << 8) + blockY) * 3;
  784. //Get Index for current block of current region geodata
  785. else
  786. index = idx.get(((blockX << 8)) + (blockY));
  787. //Buffer that Contains current Region GeoData
  788. ByteBuffer geo = _geodata.get(region);
  789. if (geo == null)
  790. {
  791. if (Config.DEBUG)
  792. _log.warning("Geo Region - Region Offset: " + region + " dosnt exist!!");
  793. return (short) z;
  794. }
  795. //Read current block type: 0-flat,1-complex,2-multilevel
  796. byte type = geo.get(index);
  797. index++;
  798. if (type == 0)//flat
  799. return geo.getShort(index);
  800. else if (type == 1)//complex
  801. {
  802. cellX = getCell(geox);
  803. cellY = getCell(geoy);
  804. index += ((cellX << 3) + cellY) << 1;
  805. short height = geo.getShort(index);
  806. height = (short) (height & 0x0fff0);
  807. height = (short) (height >> 1); //height / 2
  808. return height;
  809. }
  810. else
  811. //multilevel
  812. {
  813. cellX = getCell(geox);
  814. cellY = getCell(geoy);
  815. int offset = (cellX << 3) + cellY;
  816. while (offset > 0)
  817. {
  818. byte lc = geo.get(index);
  819. index += (lc << 1) + 1;
  820. offset--;
  821. }
  822. byte layers = geo.get(index);
  823. index++;
  824. short height = -1;
  825. if (layers <= 0 || layers > 125)
  826. {
  827. _log.warning("Broken geofile (case1), region: " + region + " - invalid layer count: " + layers + " at: " + geox + " "
  828. + geoy);
  829. return (short) z;
  830. }
  831. short temph = Short.MIN_VALUE;
  832. while (layers > 0)
  833. {
  834. height = geo.getShort(index);
  835. height = (short) (height & 0x0fff0);
  836. height = (short) (height >> 1); //height / 2
  837. if ((z - temph) * (z - temph) > (z - height) * (z - height))
  838. temph = height;
  839. layers--;
  840. index += 2;
  841. }
  842. return temph;
  843. }
  844. }
  845. /**
  846. * @param geox
  847. * @param geoy
  848. * @param z
  849. * @return One layer higher Z than parameter Z
  850. */
  851. private static short nGetUpperHeight(int geox, int geoy, int z)
  852. {
  853. short region = getRegionOffset(geox, geoy);
  854. int blockX = getBlock(geox);
  855. int blockY = getBlock(geoy);
  856. int cellX, cellY, index;
  857. //Geodata without index - it is just empty so index can be calculated on the fly
  858. final IntBuffer idx = _geodataIndex.get(region);
  859. if (idx == null)
  860. index = ((blockX << 8) + blockY) * 3;
  861. //Get Index for current block of current region geodata
  862. else
  863. index = idx.get(((blockX << 8)) + (blockY));
  864. //Buffer that Contains current Region GeoData
  865. ByteBuffer geo = _geodata.get(region);
  866. if (geo == null)
  867. {
  868. if (Config.DEBUG)
  869. _log.warning("Geo Region - Region Offset: " + region + " dosnt exist!!");
  870. return (short) z;
  871. }
  872. //Read current block type: 0-flat,1-complex,2-multilevel
  873. byte type = geo.get(index);
  874. index++;
  875. if (type == 0)//flat
  876. return geo.getShort(index);
  877. else if (type == 1)//complex
  878. {
  879. cellX = getCell(geox);
  880. cellY = getCell(geoy);
  881. index += ((cellX << 3) + cellY) << 1;
  882. short height = geo.getShort(index);
  883. height = (short) (height & 0x0fff0);
  884. height = (short) (height >> 1); //height / 2
  885. return height;
  886. }
  887. else
  888. //multilevel
  889. {
  890. cellX = getCell(geox);
  891. cellY = getCell(geoy);
  892. int offset = (cellX << 3) + cellY;
  893. while (offset > 0)
  894. {
  895. byte lc = geo.get(index);
  896. index += (lc << 1) + 1;
  897. offset--;
  898. }
  899. byte layers = geo.get(index);
  900. index++;
  901. short height = -1;
  902. if (layers <= 0 || layers > 125)
  903. {
  904. _log.warning("Broken geofile (case1), region: " + region + " - invalid layer count: " + layers + " at: " + geox + " "
  905. + geoy);
  906. return (short) z;
  907. }
  908. short temph = Short.MAX_VALUE;
  909. while (layers > 0) // from higher to lower
  910. {
  911. height = geo.getShort(index);
  912. height = (short) (height & 0x0fff0);
  913. height = (short) (height >> 1); //height / 2
  914. if (height < z)
  915. return temph;
  916. temph = height;
  917. layers--;
  918. index += 2;
  919. }
  920. return temph;
  921. }
  922. }
  923. /**
  924. * @param geox
  925. * @param geoy
  926. * @param zmin
  927. * @param zmax
  928. * @param spawn
  929. * @return Z between zmin and zmax
  930. */
  931. private static short nGetSpawnHeight(int geox, int geoy, int zmin, int zmax, L2Spawn spawn)
  932. {
  933. short region = getRegionOffset(geox, geoy);
  934. int blockX = getBlock(geox);
  935. int blockY = getBlock(geoy);
  936. int cellX, cellY, index;
  937. short temph = Short.MIN_VALUE;
  938. final IntBuffer idx = _geodataIndex.get(region);
  939. //Geodata without index - it is just empty so index can be calculated on the fly
  940. if (idx == null)
  941. index = ((blockX << 8) + blockY) * 3;
  942. //Get Index for current block of current region geodata
  943. else
  944. index = idx.get(((blockX << 8)) + (blockY));
  945. //Buffer that Contains current Region GeoData
  946. ByteBuffer geo = _geodata.get(region);
  947. if (geo == null)
  948. {
  949. if (Config.DEBUG)
  950. _log.warning("Geo Region - Region Offset: " + region + " dosnt exist!!");
  951. return (short) zmin;
  952. }
  953. //Read current block type: 0-flat,1-complex,2-multilevel
  954. byte type = geo.get(index);
  955. index++;
  956. if (type == 0)//flat
  957. temph = geo.getShort(index);
  958. else if (type == 1)//complex
  959. {
  960. cellX = getCell(geox);
  961. cellY = getCell(geoy);
  962. index += ((cellX << 3) + cellY) << 1;
  963. short height = geo.getShort(index);
  964. height = (short) (height & 0x0fff0);
  965. height = (short) (height >> 1); //height / 2
  966. temph = height;
  967. }
  968. else
  969. //multilevel
  970. {
  971. cellX = getCell(geox);
  972. cellY = getCell(geoy);
  973. short height;
  974. int offset = (cellX << 3) + cellY;
  975. while (offset > 0)
  976. {
  977. byte lc = geo.get(index);
  978. index += (lc << 1) + 1;
  979. offset--;
  980. }
  981. //Read current block type: 0-flat,1-complex,2-multilevel
  982. byte layers = geo.get(index);
  983. index++;
  984. if (layers <= 0 || layers > 125)
  985. {
  986. _log.warning("Broken geofile (case2), region: " + region + " - invalid layer count: " + layers + " at: " + geox + " "
  987. + geoy);
  988. return (short) zmin;
  989. }
  990. while (layers > 0)
  991. {
  992. height = geo.getShort(index);
  993. height = (short) (height & 0x0fff0);
  994. height = (short) (height >> 1); //height / 2
  995. if ((zmin - temph) * (zmin - temph) > (zmin - height) * (zmin - height))
  996. temph = height;
  997. layers--;
  998. index += 2;
  999. }
  1000. if (temph > zmax + 200 || temph < zmin - 200)
  1001. {
  1002. if (Config.DEBUG)
  1003. _log.warning("SpawnHeight Error - Couldnt find correct layer to spawn NPC - GeoData or Spawnlist Bug!: zmin: " + zmin
  1004. + " zmax: " + zmax + " value: " + temph + " Spawn: " + spawn + " at: " + geox + " : " + geoy);
  1005. return (short) zmin;
  1006. }
  1007. }
  1008. if (temph > zmax + 1000 || temph < zmin - 1000)
  1009. {
  1010. if (Config.DEBUG)
  1011. _log.warning("SpawnHeight Error - Spawnlist z value is wrong or GeoData error: zmin: " + zmin + " zmax: " + zmax
  1012. + " value: " + temph + " Spawn: " + spawn + " at: " + geox + " : " + geoy);
  1013. return (short) zmin;
  1014. }
  1015. return temph;
  1016. }
  1017. /**
  1018. * @param x
  1019. * @param y
  1020. * @param z
  1021. * @param tx
  1022. * @param ty
  1023. * @param tz
  1024. * @return True if char can move to (tx,ty,tz)
  1025. */
  1026. private static double nCanMoveNext(int x, int y, int z, int tx, int ty, int tz)
  1027. {
  1028. short region = getRegionOffset(x, y);
  1029. int blockX = getBlock(x);
  1030. int blockY = getBlock(y);
  1031. int cellX, cellY;
  1032. short NSWE = 0;
  1033. int index = 0;
  1034. final IntBuffer idx = _geodataIndex.get(region);
  1035. //Geodata without index - it is just empty so index can be calculated on the fly
  1036. if (idx == null)
  1037. index = ((blockX << 8) + blockY) * 3;
  1038. //Get Index for current block of current region geodata
  1039. else
  1040. index = idx.get(((blockX << 8)) + (blockY));
  1041. //Buffer that Contains current Region GeoData
  1042. ByteBuffer geo = _geodata.get(region);
  1043. if (geo == null)
  1044. {
  1045. if (Config.DEBUG)
  1046. _log.warning("Geo Region - Region Offset: " + region + " dosnt exist!!");
  1047. return z;
  1048. }
  1049. //Read current block type: 0-flat,1-complex,2-multilevel
  1050. byte type = geo.get(index);
  1051. index++;
  1052. if (type == 0) //flat
  1053. return geo.getShort(index);
  1054. else if (type == 1) //complex
  1055. {
  1056. cellX = getCell(x);
  1057. cellY = getCell(y);
  1058. index += ((cellX << 3) + cellY) << 1;
  1059. short height = geo.getShort(index);
  1060. NSWE = (short) (height & 0x0F);
  1061. height = (short) (height & 0x0fff0);
  1062. height = (short) (height >> 1); //height / 2
  1063. if (checkNSWE(NSWE, x, y, tx, ty))
  1064. return height;
  1065. return Double.MIN_VALUE;
  1066. }
  1067. else
  1068. //multilevel, type == 2
  1069. {
  1070. cellX = getCell(x);
  1071. cellY = getCell(y);
  1072. int offset = (cellX << 3) + cellY;
  1073. while (offset > 0) // iterates (too many times?) to get to layer count
  1074. {
  1075. byte lc = geo.get(index);
  1076. index += (lc << 1) + 1;
  1077. offset--;
  1078. }
  1079. byte layers = geo.get(index);
  1080. //_log.warning("layers"+layers);
  1081. index++;
  1082. short height = -1;
  1083. if (layers <= 0 || layers > 125)
  1084. {
  1085. _log.warning("Broken geofile (case3), region: " + region + " - invalid layer count: " + layers + " at: " + x + " " + y);
  1086. return z;
  1087. }
  1088. short tempz = Short.MIN_VALUE;
  1089. while (layers > 0)
  1090. {
  1091. height = geo.getShort(index);
  1092. height = (short) (height & 0x0fff0);
  1093. height = (short) (height >> 1); //height / 2
  1094. // searches the closest layer to current z coordinate
  1095. if ((z - tempz) * (z - tempz) > (z - height) * (z - height))
  1096. {
  1097. //layercurr = layers;
  1098. tempz = height;
  1099. NSWE = geo.getShort(index);
  1100. NSWE = (short) (NSWE & 0x0F);
  1101. }
  1102. layers--;
  1103. index += 2;
  1104. }
  1105. if (checkNSWE(NSWE, x, y, tx, ty))
  1106. return tempz;
  1107. return Double.MIN_VALUE;
  1108. }
  1109. }
  1110. /**
  1111. * @param x
  1112. * @param y
  1113. * @param z
  1114. * @param inc_x
  1115. * @param inc_y
  1116. * @param inc_z
  1117. * @param tz
  1118. * @param debug
  1119. * @return True if Char can see target
  1120. */
  1121. private static boolean nLOS(int x, int y, int z, int inc_x, int inc_y, double inc_z, int tz, boolean debug)
  1122. {
  1123. short region = getRegionOffset(x, y);
  1124. int blockX = getBlock(x);
  1125. int blockY = getBlock(y);
  1126. int cellX, cellY;
  1127. short NSWE = 0;
  1128. int index;
  1129. final IntBuffer idx = _geodataIndex.get(region);
  1130. //Geodata without index - it is just empty so index can be calculated on the fly
  1131. if (idx == null)
  1132. index = ((blockX << 8) + blockY) * 3;
  1133. //Get Index for current block of current region geodata
  1134. else
  1135. index = idx.get(((blockX << 8)) + (blockY));
  1136. //Buffer that Contains current Region GeoData
  1137. ByteBuffer geo = _geodata.get(region);
  1138. if (geo == null)
  1139. {
  1140. if (Config.DEBUG)
  1141. _log.warning("Geo Region - Region Offset: " + region + " dosnt exist!!");
  1142. return true;
  1143. }
  1144. //Read current block type: 0-flat,1-complex,2-multilevel
  1145. byte type = geo.get(index);
  1146. index++;
  1147. if (type == 0) //flat, movement and sight always possible
  1148. {
  1149. short height = geo.getShort(index);
  1150. if (debug)
  1151. _log.warning("flatheight:" + height);
  1152. if (z > height)
  1153. return z+inc_z > height;
  1154. return z+inc_z < height;
  1155. }
  1156. else if (type == 1) //complex
  1157. {
  1158. cellX = getCell(x);
  1159. cellY = getCell(y);
  1160. index += ((cellX << 3) + cellY) << 1;
  1161. short height = geo.getShort(index);
  1162. NSWE = (short) (height & 0x0F);
  1163. height = (short) (height & 0x0fff0);
  1164. height = (short) (height >> 1); //height / 2
  1165. if (!checkNSWE(NSWE, x, y, x + inc_x, y + inc_y))
  1166. {
  1167. if (debug)
  1168. _log.warning("height:" + height + " z" + z);
  1169. if (z < nGetUpperHeight(x + inc_x, y + inc_y, height))
  1170. return false; // an obstacle high enough
  1171. }
  1172. return true;
  1173. }
  1174. else
  1175. //multilevel, type == 2
  1176. {
  1177. cellX = getCell(x);
  1178. cellY = getCell(y);
  1179. int offset = (cellX << 3) + cellY;
  1180. while (offset > 0) // iterates (too many times?) to get to layer count
  1181. {
  1182. byte lc = geo.get(index);
  1183. index += (lc << 1) + 1;
  1184. offset--;
  1185. }
  1186. byte layers = geo.get(index);
  1187. index++;
  1188. short tempZ = -1;
  1189. if (layers <= 0 || layers > 125)
  1190. {
  1191. _log.warning("Broken geofile (case4), region: " + region + " - invalid layer count: " + layers + " at: " + x + " " + y);
  1192. return false;
  1193. }
  1194. short upperHeight = Short.MAX_VALUE; // big positive value
  1195. short lowerHeight = Short.MIN_VALUE; // big negative value
  1196. byte temp_layers = layers;
  1197. boolean highestlayer = true;
  1198. while (temp_layers > 0) // from higher to lower
  1199. {
  1200. // reads tempZ for current layer, result in world z coordinate
  1201. tempZ = geo.getShort(index);
  1202. tempZ = (short) (tempZ & 0x0fff0);
  1203. tempZ = (short) (tempZ >> 1); //tempZ / 2
  1204. if (z > tempZ)
  1205. {
  1206. lowerHeight = tempZ;
  1207. NSWE = geo.getShort(index);
  1208. NSWE = (short) (NSWE & 0x0F);
  1209. break;
  1210. }
  1211. highestlayer = false;
  1212. upperHeight = tempZ;
  1213. temp_layers--;
  1214. index += 2;
  1215. }
  1216. if (debug)
  1217. _log.warning("z:" + z + " x: " + cellX + " y:" + cellY + " la " + layers + " lo:" + lowerHeight + " up:" + upperHeight);
  1218. // Check if LOS goes under a layer/floor
  1219. // clearly under layer but not too much under
  1220. // lowerheight here only for geodata bug checking, layers very close? maybe could be removed
  1221. if ((z - upperHeight) < -10 && (z - upperHeight) > inc_z - 20 && (z - lowerHeight) > 40)
  1222. {
  1223. if (debug)
  1224. _log.warning("false, incz" + inc_z);
  1225. return false;
  1226. }
  1227. // or there's a fence/wall ahead when we're not on highest layer
  1228. if (!highestlayer)
  1229. {
  1230. //a probable wall, there's movement block and layers above you
  1231. if (!checkNSWE(NSWE, x, y, x + inc_x, y + inc_y)) // cannot move
  1232. {
  1233. if (debug)
  1234. _log.warning("block and next in x" + inc_x + " y" + inc_y + " is:"
  1235. + nGetUpperHeight(x + inc_x, y + inc_y, lowerHeight));
  1236. // check one inc_x inc_y further, for the height there
  1237. if (z < nGetUpperHeight(x + inc_x, y + inc_y, lowerHeight))
  1238. return false; // a wall
  1239. }
  1240. return true;
  1241. }
  1242. if (!checkNSWE(NSWE, x, y, x + inc_x, y + inc_y))
  1243. {
  1244. // check one inc_x inc_y further, for the height there
  1245. if (z < nGetUpperHeight(x + inc_x, y + inc_y, lowerHeight))
  1246. return false; // we hit an obstacle high enough
  1247. }
  1248. return true;
  1249. }
  1250. }
  1251. /**
  1252. * @param x
  1253. * @param y
  1254. * @param z
  1255. * @return NSWE: 0-15
  1256. */
  1257. private static short nGetNSWE(int x, int y, int z)
  1258. {
  1259. short region = getRegionOffset(x, y);
  1260. int blockX = getBlock(x);
  1261. int blockY = getBlock(y);
  1262. int cellX, cellY;
  1263. short NSWE = 0;
  1264. int index = 0;
  1265. final IntBuffer idx = _geodataIndex.get(region);
  1266. //Geodata without index - it is just empty so index can be calculated on the fly
  1267. if (idx == null)
  1268. index = ((blockX << 8) + blockY) * 3;
  1269. //Get Index for current block of current region geodata
  1270. else
  1271. index = idx.get(((blockX << 8)) + (blockY));
  1272. //Buffer that Contains current Region GeoData
  1273. ByteBuffer geo = _geodata.get(region);
  1274. if (geo == null)
  1275. {
  1276. if (Config.DEBUG)
  1277. _log.warning("Geo Region - Region Offset: " + region + " dosnt exist!!");
  1278. return 15;
  1279. }
  1280. //Read current block type: 0-flat,1-complex,2-multilevel
  1281. byte type = geo.get(index);
  1282. index++;
  1283. if (type == 0)//flat
  1284. return 15;
  1285. else if (type == 1)//complex
  1286. {
  1287. cellX = getCell(x);
  1288. cellY = getCell(y);
  1289. index += ((cellX << 3) + cellY) << 1;
  1290. short height = geo.getShort(index);
  1291. NSWE = (short) (height & 0x0F);
  1292. }
  1293. else
  1294. //multilevel
  1295. {
  1296. cellX = getCell(x);
  1297. cellY = getCell(y);
  1298. int offset = (cellX << 3) + cellY;
  1299. while (offset > 0)
  1300. {
  1301. byte lc = geo.get(index);
  1302. index += (lc << 1) + 1;
  1303. offset--;
  1304. }
  1305. byte layers = geo.get(index);
  1306. index++;
  1307. short height = -1;
  1308. if (layers <= 0 || layers > 125)
  1309. {
  1310. _log.warning("Broken geofile (case5), region: " + region + " - invalid layer count: " + layers + " at: " + x + " " + y);
  1311. return 15;
  1312. }
  1313. short tempz = Short.MIN_VALUE;
  1314. while (layers > 0)
  1315. {
  1316. height = geo.getShort(index);
  1317. height = (short) (height & 0x0fff0);
  1318. height = (short) (height >> 1); //height / 2
  1319. if ((z - tempz) * (z - tempz) > (z - height) * (z - height))
  1320. {
  1321. tempz = height;
  1322. NSWE = geo.get(index);
  1323. NSWE = (short) (NSWE & 0x0F);
  1324. }
  1325. layers--;
  1326. index += 2;
  1327. }
  1328. }
  1329. return NSWE;
  1330. }
  1331. /**
  1332. * @param x
  1333. * @param y
  1334. * @param z
  1335. * @return array [0] - height, [1] - NSWE
  1336. */
  1337. @Override
  1338. public short getHeightAndNSWE(int x, int y, int z)
  1339. {
  1340. short region = getRegionOffset(x, y);
  1341. int blockX = getBlock(x);
  1342. int blockY = getBlock(y);
  1343. int cellX, cellY;
  1344. int index = 0;
  1345. final IntBuffer idx = _geodataIndex.get(region);
  1346. //Geodata without index - it is just empty so index can be calculated on the fly
  1347. if (idx == null)
  1348. index = ((blockX << 8) + blockY) * 3;
  1349. //Get Index for current block of current region geodata
  1350. else
  1351. index = idx.get(((blockX << 8)) + (blockY));
  1352. //Buffer that Contains current Region GeoData
  1353. ByteBuffer geo = _geodata.get(region);
  1354. if (geo == null)
  1355. {
  1356. if (Config.DEBUG)
  1357. _log.warning("Geo Region - Region Offset: " + region + " dosnt exist!!");
  1358. return (short)((z << 1) | NSWE_ALL);
  1359. }
  1360. //Read current block type: 0-flat,1-complex,2-multilevel
  1361. byte type = geo.get(index);
  1362. index++;
  1363. if (type == 0)//flat
  1364. return (short)((geo.getShort(index) << 1) | NSWE_ALL);
  1365. else if (type == 1)//complex
  1366. {
  1367. cellX = getCell(x);
  1368. cellY = getCell(y);
  1369. index += ((cellX << 3) + cellY) << 1;
  1370. return geo.getShort(index);
  1371. }
  1372. else
  1373. //multilevel
  1374. {
  1375. cellX = getCell(x);
  1376. cellY = getCell(y);
  1377. int offset = (cellX << 3) + cellY;
  1378. while (offset > 0)
  1379. {
  1380. byte lc = geo.get(index);
  1381. index += (lc << 1) + 1;
  1382. offset--;
  1383. }
  1384. byte layers = geo.get(index);
  1385. index++;
  1386. short height = -1;
  1387. if (layers <= 0 || layers > 125)
  1388. {
  1389. _log.warning("Broken geofile (case1), region: " + region + " - invalid layer count: " + layers + " at: " + x + " " + y);
  1390. return (short)((z << 1) | NSWE_ALL);
  1391. }
  1392. short temph = Short.MIN_VALUE;
  1393. short result = 0;
  1394. while (layers > 0)
  1395. {
  1396. short block = geo.getShort(index);
  1397. height = (short) (block & 0x0fff0);
  1398. height = (short) (height >> 1); //height / 2
  1399. if ((z - temph) * (z - temph) > (z - height) * (z - height))
  1400. {
  1401. temph = height;
  1402. result = block;
  1403. }
  1404. layers--;
  1405. index += 2;
  1406. }
  1407. return result;
  1408. }
  1409. }
  1410. /**
  1411. * @param NSWE
  1412. * @param x
  1413. * @param y
  1414. * @param tx
  1415. * @param ty
  1416. * @return True if NSWE dont block given direction
  1417. */
  1418. private static boolean checkNSWE(short NSWE, int x, int y, int tx, int ty)
  1419. {
  1420. //Check NSWE
  1421. if (NSWE == 15)
  1422. return true;
  1423. if (tx > x)//E
  1424. {
  1425. if ((NSWE & EAST) == 0)
  1426. return false;
  1427. }
  1428. else if (tx < x)//W
  1429. {
  1430. if ((NSWE & WEST) == 0)
  1431. return false;
  1432. }
  1433. if (ty > y)//S
  1434. {
  1435. if ((NSWE & SOUTH) == 0)
  1436. return false;
  1437. }
  1438. else if (ty < y)//N
  1439. {
  1440. if ((NSWE & NORTH) == 0)
  1441. return false;
  1442. }
  1443. return true;
  1444. }
  1445. @SuppressWarnings("synthetic-access")
  1446. private static class SingletonHolder
  1447. {
  1448. protected static final GeoEngine _instance = new GeoEngine();
  1449. }
  1450. }