GeoEngine.java 47 KB

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