2
0

GeoEngine.java 44 KB

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