GeoEngine.java 43 KB

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