GeoEngine.java 47 KB

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