GeoEngine.java 46 KB

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