GeoEngine.java 44 KB

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