GeoEngine.java 47 KB

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