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