123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221 |
- package net.sf.l2j.gameserver;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.LineNumberReader;
- import java.io.RandomAccessFile;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.IntBuffer;
- import java.nio.MappedByteBuffer;
- import java.nio.channels.FileChannel;
- import java.util.Map;
- import java.util.StringTokenizer;
- import java.util.logging.Logger;
- import javolution.util.FastMap;
- import net.sf.l2j.Config;
- import net.sf.l2j.gameserver.datatables.DoorTable;
- import net.sf.l2j.gameserver.model.L2Object;
- import net.sf.l2j.gameserver.model.L2World;
- import net.sf.l2j.gameserver.model.Location;
- import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2SiegeGuardInstance;
- public class GeoEngine extends GeoData
- {
- private static Logger _log = Logger.getLogger(GeoData.class.getName());
- private static GeoEngine _instance;
- private final static byte _e = 1;
- private final static byte _w = 2;
- private final static byte _s = 4;
- private final static byte _n = 8;
- private static Map<Short, MappedByteBuffer> _geodata = new FastMap<Short, MappedByteBuffer>();
- private static Map<Short, IntBuffer> _geodataIndex = new FastMap<Short, IntBuffer>();
- private static BufferedOutputStream _geoBugsOut;
- public static GeoEngine getInstance()
- {
- if(_instance == null)
- _instance = new GeoEngine();
- return _instance;
- }
- public GeoEngine()
- {
- nInitGeodata();
- }
-
-
- @Override
- public short getType(int x, int y)
- {
- return nGetType((x - L2World.MAP_MIN_X) >> 4, (y - L2World.MAP_MIN_Y) >> 4);
- }
-
- @Override
- public short getHeight(int x, int y, int z)
- {
- return nGetHeight((x - L2World.MAP_MIN_X) >> 4,(y - L2World.MAP_MIN_Y) >> 4,z);
- }
-
- @Override
- public short getSpawnHeight(int x, int y, int zmin, int zmax, int spawnid)
- {
- return nGetSpawnHeight((x - L2World.MAP_MIN_X) >> 4,(y - L2World.MAP_MIN_Y) >> 4,zmin,zmax,spawnid);
- }
-
- @Override
- public String geoPosition(int x, int y)
- {
- int gx = (x - L2World.MAP_MIN_X) >> 4;
- int gy = (y - L2World.MAP_MIN_Y) >> 4;
- return "bx: "+getBlock(gx)+" by: "+getBlock(gy)+" cx: "+getCell(gx)+" cy: "+getCell(gy)+" region offset: "+getRegionOffset(gx,gy);
- }
-
- @Override
- public boolean canSeeTarget(L2Object cha, L2Object target)
- {
-
-
-
-
-
-
-
-
-
- int z = cha.getZ()+45;
- if(cha instanceof L2SiegeGuardInstance) z += 30;
- int z2 = target.getZ()+45;
- if (!(target instanceof L2DoorInstance)
- && DoorTable.getInstance().checkIfDoorsBetween(cha.getX(),cha.getY(),z,target.getX(),target.getY(),z2))
- return false;
- if(target instanceof L2DoorInstance) return true;
- if(target instanceof L2SiegeGuardInstance) z2 += 30;
- if(cha.getZ() >= target.getZ())
- return canSeeTarget(cha.getX(),cha.getY(),z,target.getX(),target.getY(),z2);
- else
- return canSeeTarget(target.getX(),target.getY(),z2, cha.getX(),cha.getY(),z);
- }
-
- @Override
- public boolean canSeeTargetDebug(L2PcInstance gm, L2Object target)
- {
-
- int z = gm.getZ()+45;
- int z2 = target.getZ()+45;
- if(target instanceof L2DoorInstance)
- {
- gm.sendMessage("door always true");
- return true;
- }
- if(gm.getZ() >= target.getZ())
- 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);
- else
- 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);
- }
-
- @Override
- public short getNSWE(int x, int y, int z)
- {
- return nGetNSWE((x - L2World.MAP_MIN_X) >> 4,(y - L2World.MAP_MIN_Y) >> 4,z);
- }
-
- @Override
- public Location moveCheck(int x, int y, int z, int tx, int ty, int tz)
- {
- Location startpoint = new Location(x,y,z);
- if (DoorTable.getInstance().checkIfDoorsBetween(x,y,z,tx,ty,tz))
- return startpoint;
- Location destiny = new Location(tx,ty,tz);
- 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);
- }
-
- @Override
- public void addGeoDataBug(L2PcInstance gm, String comment)
- {
- int gx = (gm.getX() - L2World.MAP_MIN_X) >> 4;
- int gy = (gm.getY() - L2World.MAP_MIN_Y) >> 4;
- int bx = getBlock(gx);
- int by = getBlock(gy);
- int cx = getCell(gx);
- int cy = getCell(gy);
- int rx = (gx >> 11) + 16;
- int ry = (gy >> 11) + 10;
- String out = rx+";"+ry+";"+bx+";"+by+";"+cx+";"+cy+";"+gm.getZ()+";"+comment+"\n";
- try
- {
- _geoBugsOut.write(out.getBytes());
- _geoBugsOut.flush();
- gm.sendMessage("GeoData bug saved!");
- } catch (Exception e) {
- e.printStackTrace();
- gm.sendMessage("GeoData bug save Failed!");
- }
- }
-
- private boolean canSeeTarget(int x, int y, int z, int tx, int ty, int tz)
- {
- 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);
- }
- private static boolean canSee(int x, int y, double z, int tx, int ty, int tz)
- {
- int dx = (tx - x);
- int dy = (ty - y);
- final double dz = (tz - z);
- final int distance2 = dx*dx+dy*dy;
- if (distance2 > 90000)
- {
-
- return false;
- }
-
-
-
- else if (distance2 < 82)
- {
-
- if(dz*dz > 40000)
- {
- short region = getRegionOffset(x,y);
-
-
- if (_geodata.get(region) != null)
- return false;
- }
- return true;
- }
-
-
-
- final int inc_x = sign(dx);
- final int inc_y = sign(dy);
- dx = Math.abs(dx);
- dy = Math.abs(dy);
- final double inc_z_directionx = dz*dx / (distance2);
- final double inc_z_directiony = dz*dy / (distance2);
-
- int next_x = x;
- int next_y = y;
-
-
- if (dx >= dy)
- {
- int delta_A = 2*dy;
- int d = delta_A - dx;
- int delta_B = delta_A - 2*dx;
- for (int i = 0; i < dx; i++)
- {
- x = next_x;
- y = next_y;
- if (d > 0)
- {
- d += delta_B;
- next_x += inc_x;
- z += inc_z_directionx;
- next_y += inc_y;
- z += inc_z_directiony;
-
- if (!nLOS(x,y,(int)z,inc_x,inc_y,tz,false))
- return false;
- }
- else
- {
- d += delta_A;
- next_x += inc_x;
-
- z += inc_z_directionx;
- if (!nLOS(x,y,(int)z,inc_x,0,tz,false))
- return false;
- }
- }
- }
- else
- {
- int delta_A = 2*dx;
- int d = delta_A - dy;
- int delta_B = delta_A - 2*dy;
- for (int i = 0; i < dy; i++)
- {
- x = next_x;
- y = next_y;
- if (d > 0)
- {
- d += delta_B;
- next_y += inc_y;
- z += inc_z_directiony;
- next_x += inc_x;
- z += inc_z_directionx;
-
- if (!nLOS(x,y,(int)z,inc_x,inc_y,tz,false))
- return false;
- }
- else
- {
- d += delta_A;
- next_y += inc_y;
-
- z += inc_z_directiony;
- if (!nLOS(x,y,(int)z,0,inc_y,tz,false))
- return false;
- }
- }
- }
- return true;
- }
-
- private static boolean canSeeDebug(L2PcInstance gm, int x, int y, double z, int tx, int ty, int tz)
- {
- int dx = (tx - x);
- int dy = (ty - y);
- final double dz = (tz - z);
- final int distance2 = dx*dx+dy*dy;
- if (distance2 > 90000)
- {
-
- gm.sendMessage("dist > 300");
- return false;
- }
-
-
-
- else if (distance2 < 82)
- {
-
- if(dz*dz > 40000)
- {
- short region = getRegionOffset(x,y);
-
-
- if (_geodata.get(region) != null)
- return false;
- }
- return true;
- }
-
-
-
- final int inc_x = sign(dx);
- final int inc_y = sign(dy);
- dx = Math.abs(dx);
- dy = Math.abs(dy);
- final double inc_z_directionx = dz*dx / (distance2);
- final double inc_z_directiony = dz*dy / (distance2);
- gm.sendMessage("Los: from X: "+x+ "Y: "+y+ "--->> X: "+tx+" Y: "+ty);
-
- int next_x = x;
- int next_y = y;
-
-
- if (dx >= dy)
- {
- int delta_A = 2*dy;
- int d = delta_A - dx;
- int delta_B = delta_A - 2*dx;
- for (int i = 0; i < dx; i++)
- {
- x = next_x;
- y = next_y;
- if (d > 0)
- {
- d += delta_B;
- next_x += inc_x;
- z += inc_z_directionx;
- next_y += inc_y;
- z += inc_z_directiony;
-
- if (!nLOS(x,y,(int)z,inc_x,inc_y,tz,true))
- return false;
- }
- else
- {
- d += delta_A;
- next_x += inc_x;
-
- z += inc_z_directionx;
- if (!nLOS(x,y,(int)z,inc_x,0,tz,true))
- return false;
- }
- }
- }
- else
- {
- int delta_A = 2*dx;
- int d = delta_A - dy;
- int delta_B = delta_A - 2*dy;
- for (int i = 0; i < dy; i++)
- {
- x = next_x;
- y = next_y;
- if (d > 0)
- {
- d += delta_B;
- next_y += inc_y;
- z += inc_z_directiony;
- next_x += inc_x;
- z += inc_z_directionx;
-
- if (!nLOS(x,y,(int)z,inc_x,inc_y,tz,true))
- return false;
- }
- else
- {
- d += delta_A;
- next_y += inc_y;
-
- z += inc_z_directiony;
- if (!nLOS(x,y,(int)z,0,inc_y,tz,true))
- return false;
- }
- }
- }
- return true;
- }
-
- private static Location moveCheck(Location startpoint, Location destiny, int x, int y, double z, int tx, int ty, int tz)
- {
- int dx = (tx - x);
- int dy = (ty - y);
- final int distance2 = dx*dx+dy*dy;
- if (distance2 == 0)
- return destiny;
- if (distance2 > 36100)
- {
-
-
-
- double divider = Math.sqrt((double)30000/distance2);
- tx = x + (int)(divider * dx);
- ty = y + (int)(divider * dy);
- int dz = (tz - startpoint.getZ());
- tz = startpoint.getZ() + (int)(divider * dz);
- dx = (tx - x);
- dy = (ty - y);
-
- }
-
-
-
- final int inc_x = sign(dx);
- final int inc_y = sign(dy);
- dx = Math.abs(dx);
- dy = Math.abs(dy);
-
-
- int next_x = x;
- int next_y = y;
- double tempz = z;
-
-
- if (dx >= dy)
- {
- int delta_A = 2*dy;
- int d = delta_A - dx;
- int delta_B = delta_A - 2*dx;
- for (int i = 0; i < dx; i++)
- {
- x = next_x;
- y = next_y;
- if (d > 0)
- {
- d += delta_B;
- next_x += inc_x;
- next_y += inc_y;
-
- tempz = nCanMoveNext(x,y,(int)z,next_x,next_y,tz);
- if (tempz == Double.MIN_VALUE)
- return new Location((x << 4) + L2World.MAP_MIN_X,(y << 4) + L2World.MAP_MIN_Y,(int)z);
- else z = tempz;
- }
- else
- {
- d += delta_A;
- next_x += inc_x;
-
- tempz = nCanMoveNext(x,y,(int)z,next_x,next_y,tz);
- if (tempz == Double.MIN_VALUE)
- return new Location((x << 4) + L2World.MAP_MIN_X,(y << 4) + L2World.MAP_MIN_Y,(int)z);
- else z = tempz;
- }
- }
- }
- else
- {
- int delta_A = 2*dx;
- int d = delta_A - dy;
- int delta_B = delta_A - 2*dy;
- for (int i = 0; i < dy; i++)
- {
- x = next_x;
- y = next_y;
- if (d > 0)
- {
- d += delta_B;
- next_y += inc_y;
- next_x += inc_x;
-
- tempz = nCanMoveNext(x,y,(int)z,next_x,next_y,tz);
- if (tempz == Double.MIN_VALUE)
- return new Location((x << 4) + L2World.MAP_MIN_X,(y << 4) + L2World.MAP_MIN_Y,(int)z);
- else z = tempz;
- }
- else
- {
- d += delta_A;
- next_y += inc_y;
-
- tempz = nCanMoveNext(x,y,(int)z,next_x,next_y,tz);
- if (tempz == Double.MIN_VALUE)
- return new Location((x << 4) + L2World.MAP_MIN_X,(y << 4) + L2World.MAP_MIN_Y,(int)z);
- else z = tempz;
- }
- }
- }
- return destiny;
- }
- private static byte sign(int x)
- {
- if (x >= 0)
- return +1;
- else
- return -1;
- }
-
- private static void nInitGeodata()
- {
- LineNumberReader lnr = null;
- try
- {
- _log.info("Geo Engine: - Loading Geodata...");
- File Data = new File("./data/geodata/geo_index.txt");
- if (!Data.exists())
- return;
- lnr = new LineNumberReader(new BufferedReader(new FileReader(Data)));
- } catch (Exception e) {
- e.printStackTrace();
- throw new Error("Failed to Load geo_index File.");
- }
- String line;
- try
- {
- while ((line = lnr.readLine()) != null) {
- if (line.trim().length() == 0)
- continue;
- StringTokenizer st = new StringTokenizer(line, "_");
- byte rx = Byte.parseByte(st.nextToken());
- byte ry = Byte.parseByte(st.nextToken());
- loadGeodataFile(rx,ry);
- }
- } catch (Exception e) {
- e.printStackTrace();
- throw new Error("Failed to Read geo_index File.");
- }
- try
- {
- File geo_bugs = new File("./data/geodata/geo_bugs.txt");
- _geoBugsOut = new BufferedOutputStream(new FileOutputStream(geo_bugs,true));
- } catch (Exception e) {
- e.printStackTrace();
- throw new Error("Failed to Load geo_bugs.txt File.");
- }
- }
- public static void unloadGeodata(byte rx, byte ry)
- {
- short regionoffset = (short)((rx << 5) + ry);
- _geodataIndex.remove(regionoffset);
- _geodata.remove(regionoffset);
- }
- public static boolean loadGeodataFile(byte rx, byte ry)
- {
- String fname = "./data/geodata/"+rx+"_"+ry+".l2j";
- short regionoffset = (short)((rx << 5) + ry);
- _log.info("Geo Engine: - Loading: "+fname+" -> region offset: "+regionoffset+"X: "+rx+" Y: "+ry);
- File Geo = new File(fname);
- int size, index = 0, block = 0, flor = 0;
- try {
-
- FileChannel roChannel = new RandomAccessFile(Geo, "r").getChannel();
- size = (int)roChannel.size();
- MappedByteBuffer geo;
- if (Config.FORCE_GEODATA)
-
- geo = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
- else
- geo = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, size);
- geo.order(ByteOrder.LITTLE_ENDIAN);
- if (size > 196608)
- {
-
- IntBuffer indexs = IntBuffer.allocate(65536);
- while(block < 65536)
- {
- byte type = geo.get(index);
- indexs.put(block,index);
- block++;
- index++;
- if(type == 0)
- index += 2;
- else if(type == 1)
- index += 128;
- else
- {
- int b;
- for(b=0;b<64;b++)
- {
- byte layers = geo.get(index);
- index += (layers << 1) + 1;
- if (layers > flor)
- flor = layers;
- }
- }
- }
- _geodataIndex.put(regionoffset, indexs);
- }
- _geodata.put(regionoffset,geo);
- _log.info("Geo Engine: - Max Layers: "+flor+" Size: "+size+" Loaded: "+index);
- } catch (Exception e)
- {
- e.printStackTrace();
- _log.warning("Failed to Load GeoFile at block: "+block+"\n");
- return false;
- }
- return true;
- }
-
-
- private static short getRegionOffset(int x, int y)
- {
- int rx = x >> 11;
- int ry = y >> 11;
- return (short)(((rx+16) << 5) + (ry+10));
- }
-
- private static int getBlock(int geo_pos)
- {
- return (geo_pos >> 3) % 256;
- }
-
- private static int getCell(int geo_pos)
- {
- return geo_pos % 8;
- }
-
-
- private static short nGetType(int x, int y)
- {
- short region = getRegionOffset(x,y);
- int blockX = getBlock(x);
- int blockY = getBlock(y);
- int index = 0;
-
- if(_geodataIndex.get(region) == null) index = ((blockX << 8) + blockY)*3;
-
- else index = _geodataIndex.get(region).get((blockX << 8) + blockY);
-
- ByteBuffer geo = _geodata.get(region);
- if(geo == null)
- {
- if(Config.DEBUG)
- _log.warning("Geo Region - Region Offset: "+region+" dosnt exist!!");
- return 0;
- }
- return geo.get(index);
- }
-
- private static short nGetHeight(int geox, int geoy, int z)
- {
- short region = getRegionOffset(geox,geoy);
- int blockX = getBlock(geox);
- int blockY = getBlock(geoy);
- int cellX, cellY, index;
-
- if(_geodataIndex.get(region) == null) index = ((blockX << 8) + blockY)*3;
-
- else index = _geodataIndex.get(region).get(((blockX << 8))+(blockY));
-
- ByteBuffer geo = _geodata.get(region);
- if(geo == null)
- {
- if(Config.DEBUG)
- _log.warning("Geo Region - Region Offset: "+region+" dosnt exist!!");
- return (short)z;
- }
-
- byte type = geo.get(index);
- index++;
- if(type == 0)
- return geo.getShort(index);
- else if(type == 1)
- {
- cellX = getCell(geox);
- cellY = getCell(geoy);
- index += ((cellX << 3) + cellY) << 1;
- short height = geo.getShort(index);
- height = (short)(height&0x0fff0);
- height = (short)(height >> 1);
- return height;
- }
- else
- {
- cellX = getCell(geox);
- cellY = getCell(geoy);
- int offset = (cellX << 3) + cellY;
- while(offset > 0)
- {
- byte lc = geo.get(index);
- index += (lc << 1) + 1;
- offset--;
- }
- byte layers = geo.get(index);
- index++;
- short height=-1;
- if(layers <= 0 || layers > 125)
- {
- _log.warning("Broken geofile (case1), region: "+region+" - invalid layer count: "+layers+" at: "+geox+" "+geoy);
- return (short)z;
- }
- short temph = Short.MIN_VALUE;
- while(layers > 0)
- {
- height = geo.getShort(index);
- height = (short)(height&0x0fff0);
- height = (short)(height >> 1);
- if ((z-temph)*(z-temph) > (z-height)*(z-height))
- temph = height;
- layers--;
- index += 2;
- }
- return temph;
- }
- }
-
- private static short nGetSpawnHeight(int geox, int geoy, int zmin, int zmax, int spawnid)
- {
- short region = getRegionOffset(geox,geoy);
- int blockX = getBlock(geox);
- int blockY = getBlock(geoy);
- int cellX, cellY, index;
- short temph = Short.MIN_VALUE;
-
- if(_geodataIndex.get(region) == null) index = ((blockX << 8) + blockY)*3;
-
- else index = _geodataIndex.get(region).get(((blockX << 8))+(blockY));
-
- ByteBuffer geo = _geodata.get(region);
- if(geo == null)
- {
- if(Config.DEBUG)
- _log.warning("Geo Region - Region Offset: "+region+" dosnt exist!!");
- return (short)zmin;
- }
-
- byte type = geo.get(index);
- index++;
- if(type == 0)
- temph = geo.getShort(index);
- else if(type == 1)
- {
- cellX = getCell(geox);
- cellY = getCell(geoy);
- index += ((cellX << 3) + cellY) << 1;
- short height = geo.getShort(index);
- height = (short)(height&0x0fff0);
- height = (short)(height >> 1);
- temph = height;
- }
- else
- {
- cellX = getCell(geox);
- cellY = getCell(geoy);
- short height;
- int offset = (cellX << 3) + cellY;
- while(offset > 0)
- {
- byte lc = geo.get(index);
- index += (lc << 1) + 1;
- offset--;
- }
-
- byte layers = geo.get(index);
- index++;
- if(layers <= 0 || layers > 125)
- {
- _log.warning("Broken geofile (case2), region: "+region+" - invalid layer count: "+layers+" at: "+geox+" "+geoy);
- return (short)zmin;
- }
- while(layers > 0)
- {
- height = geo.getShort(index);
- height = (short)(height&0x0fff0);
- height = (short)(height >> 1);
- if ((zmin-temph)*(zmin-temph) > (zmin-height)*(zmin-height))
- temph = height;
- layers--;
- index += 2;
- }
- if (temph > zmax + 200 || temph < zmin - 200)
- {
- if(Config.DEBUG)
- _log.warning("SpawnHeight Error - Couldnt find correct layer to spawn NPC - GeoData or Spawnlist Bug!: zmin: "+zmin+" zmax: "+zmax+" value: "+temph+" SpawnId: "+spawnid+" at: "+geox+" : "+geoy);
- return (short)zmin;
- }
- }
- if (temph > zmax + 1000 || temph < zmin - 1000)
- {
- if(Config.DEBUG)
- _log.warning("SpawnHeight Error - Spawnlist z value is wrong or GeoData error: zmin: "+zmin+" zmax: "+zmax+" value: "+temph+" SpawnId: "+spawnid+" at: "+geox+" : "+geoy);
- return (short)zmin;
- }
- return temph;
- }
-
- private static double nCanMoveNext(int x, int y, int z, int tx, int ty, int tz)
- {
- short region = getRegionOffset(x,y);
- int blockX = getBlock(x);
- int blockY = getBlock(y);
- int cellX, cellY;
- short NSWE = 0;
- int index = 0;
-
- if(_geodataIndex.get(region) == null) index = ((blockX << 8) + blockY)*3;
-
- else index = _geodataIndex.get(region).get(((blockX << 8))+(blockY));
-
- ByteBuffer geo = _geodata.get(region);
- if(geo == null)
- {
- if(Config.DEBUG)
- _log.warning("Geo Region - Region Offset: "+region+" dosnt exist!!");
- return z;
- }
-
- byte type = geo.get(index);
- index++;
- if(type == 0)
- return z;
- else if(type == 1)
- {
- cellX = getCell(x);
- cellY = getCell(y);
- index += ((cellX << 3) + cellY) << 1;
- short height = geo.getShort(index);
- NSWE = (short)(height&0x0F);
- height = (short)(height&0x0fff0);
- height = (short)(height >> 1);
- if(checkNSWE(NSWE,x,y,tx,ty)) return height;
- else return Double.MIN_VALUE;
- }
- else
- {
- cellX = getCell(x);
- cellY = getCell(y);
- int offset = (cellX << 3) + cellY;
- while(offset > 0)
- {
- byte lc = geo.get(index);
- index += (lc << 1) + 1;
- offset--;
- }
- byte layers = geo.get(index);
-
- index++;
- short height=-1;
- if(layers <= 0 || layers > 125)
- {
- _log.warning("Broken geofile (case3), region: "+region+" - invalid layer count: "+layers+" at: "+x+" "+y);
- return z;
- }
- short tempz = Short.MIN_VALUE;
- while(layers > 0)
- {
- height = geo.getShort(index);
- height = (short)(height&0x0fff0);
- height = (short)(height >> 1);
-
- if ((z-tempz)*(z-tempz) > (z-height)*(z-height))
- {
-
- tempz = height;
- NSWE = geo.getShort(index);
- NSWE = (short)(NSWE&0x0F);
- }
- layers--;
- index += 2;
- }
- if(checkNSWE(NSWE,x,y,tx,ty)) return tempz;
- else return Double.MIN_VALUE;
- }
- }
-
- private static boolean nLOS(int x, int y, int z, int inc_x, int inc_y, int tz, boolean debug)
- {
- short region = getRegionOffset(x,y);
- int blockX = getBlock(x);
- int blockY = getBlock(y);
- int cellX, cellY;
- short NSWE = 0;
- int index;
-
- if(_geodataIndex.get(region) == null) index = ((blockX << 8) + blockY)*3;
-
- else index = _geodataIndex.get(region).get(((blockX << 8))+(blockY));
-
- ByteBuffer geo = _geodata.get(region);
- if(geo == null)
- {
- if(Config.DEBUG)
- _log.warning("Geo Region - Region Offset: "+region+" dosnt exist!!");
- return true;
- }
-
- byte type = geo.get(index);
- index++;
- if(type == 0)
- {
- if(debug) _log.warning("flatheight:"+geo.getShort(index));
- return true;
- }
- else if(type == 1)
- {
- cellX = getCell(x);
- cellY = getCell(y);
- index += ((cellX << 3) + cellY) << 1;
- short height = geo.getShort(index);
- NSWE = (short)(height&0x0F);
- height = (short)(height&0x0fff0);
- height = (short)(height >> 1);
- if(debug) {
- _log.warning("height:"+height+" z"+z);
- if(!checkNSWE(NSWE,x,y,x+inc_x,y+inc_y)) _log.warning("would block");
- }
- if(z - height > 50) return true;
- }
- else
- {
- cellX = getCell(x);
- cellY = getCell(y);
- int offset = (cellX << 3) + cellY;
- while(offset > 0)
- {
- byte lc = geo.get(index);
- index += (lc << 1) + 1;
- offset--;
- }
- byte layers = geo.get(index);
- if (debug) _log.warning("layers"+layers);
- index++;
- short height=-1;
- if(layers <= 0 || layers > 125)
- {
- _log.warning("Broken geofile (case4), region: "+region+" - invalid layer count: "+layers+" at: "+x+" "+y);
- return false;
- }
- short tempz = Short.MIN_VALUE;
- byte temp_layers = layers;
- boolean highestlayer = true;
- z -= 25;
- while(temp_layers > 0)
- {
-
- height = geo.getShort(index);
- height = (short)(height&0x0fff0);
- height = (short)(height >> 1);
-
-
- if ((z-tempz)*(z-tempz) > (z-height)*(z-height))
- {
- if(tempz > Short.MIN_VALUE) highestlayer = false;
- tempz = height;
- if (debug) _log.warning("z"+(z+45)+" tempz"+tempz+" dz"+(z-tempz));
- NSWE = geo.getShort(index);
- NSWE = (short)(NSWE&0x0F);
- }
- temp_layers--;
- index += 2;
- }
- z += 25;
-
- if((z-tempz) < -20) return false;
-
-
- if((z-tempz) > 250) return true;
-
-
- if(!highestlayer)
- {
-
- if(!checkNSWE(NSWE,x,y,x+inc_x,y+inc_y))
- {
-
- short nextheight = nGetHeight(x+2*inc_x,y+2*inc_y,z-50);
- if(debug)
- {
- _log.warning("0: z:"+z+" tz"+nGetHeight(x,y,z-60));
- _log.warning("1: z:"+z+" tz"+nGetHeight(x+inc_x,y+inc_y,z-60));
- _log.warning("2: z:"+z+" tz"+nGetHeight(x+2*inc_x,y+2*inc_y,z-60));
- _log.warning("3: z:"+z+" tz"+nGetHeight(x+3*inc_x,y+3*inc_y,z-60));
- }
-
-
- if(z-nextheight>100) return true;
-
- if(nextheight-tempz>5 && nextheight-tempz<20) return true;
- return false;
- }
- else return true;
- }
- else return true;
- }
- return checkNSWE(NSWE,x,y,x+inc_x,y+inc_y);
- }
-
- private short nGetNSWE(int x, int y, int z)
- {
- short region = getRegionOffset(x,y);
- int blockX = getBlock(x);
- int blockY = getBlock(y);
- int cellX, cellY;
- short NSWE = 0;
- int index = 0;
-
- if(_geodataIndex.get(region) == null) index = ((blockX << 8) + blockY)*3;
-
- else index = _geodataIndex.get(region).get(((blockX << 8))+(blockY));
-
- ByteBuffer geo = _geodata.get(region);
- if(geo == null)
- {
- if(Config.DEBUG)
- _log.warning("Geo Region - Region Offset: "+region+" dosnt exist!!");
- return 15;
- }
-
- byte type = geo.get(index);
- index++;
- if(type == 0)
- return 15;
- else if(type == 1)
- {
- cellX = getCell(x);
- cellY = getCell(y);
- index += ((cellX << 3) + cellY) << 1;
- short height = geo.getShort(index);
- NSWE = (short)(height&0x0F);
- }
- else
- {
- cellX = getCell(x);
- cellY = getCell(y);
- int offset = (cellX << 3) + cellY;
- while(offset > 0)
- {
- byte lc = geo.get(index);
- index += (lc << 1) + 1;
- offset--;
- }
- byte layers = geo.get(index);
- index++;
- short height=-1;
- if(layers <= 0 || layers > 125)
- {
- _log.warning("Broken geofile (case5), region: "+region+" - invalid layer count: "+layers+" at: "+x+" "+y);
- return 15;
- }
- short tempz = Short.MIN_VALUE;
- while(layers > 0)
- {
- height = geo.getShort(index);
- height = (short)(height&0x0fff0);
- height = (short)(height >> 1);
- if ((z-tempz)*(z-tempz) > (z-height)*(z-height))
- {
- tempz = height;
- NSWE = geo.get(index);
- NSWE = (short)(NSWE&0x0F);
- }
- layers--;
- index += 2;
- }
- }
- return NSWE;
- }
-
- private static boolean checkNSWE(short NSWE, int x, int y, int tx, int ty)
- {
-
- if(NSWE == 15)
- return true;
- if(tx > x)
- {
- if ((NSWE & _e) == 0)
- return false;
- }
- else if (tx < x)
- {
- if ((NSWE & _w) == 0)
- return false;
- }
- if (ty > y)
- {
- if ((NSWE & _s) == 0)
- return false;
- }
- else if (ty < y)
- {
- if ((NSWE & _n) == 0)
- return false;
- }
- return true;
- }
- }
|