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