GeoEngine.java 50 KB

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