GeoEngine.java 44 KB

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