GeoEngine.java 47 KB

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