GeoEngine.java 47 KB

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