Followed latest geodata refactorings.
This commit is contained in:
@@ -0,0 +1,578 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.l2jmobius.Config;
|
||||
import com.l2jmobius.gameserver.data.xml.impl.DoorData;
|
||||
import com.l2jmobius.gameserver.geodata.geodriver.Cell;
|
||||
import com.l2jmobius.gameserver.geodata.geodriver.GeoDriver;
|
||||
import com.l2jmobius.gameserver.model.L2Object;
|
||||
import com.l2jmobius.gameserver.model.L2World;
|
||||
import com.l2jmobius.gameserver.model.Location;
|
||||
import com.l2jmobius.gameserver.model.interfaces.ILocational;
|
||||
import com.l2jmobius.gameserver.util.GeoUtils;
|
||||
import com.l2jmobius.gameserver.util.LinePointIterator;
|
||||
import com.l2jmobius.gameserver.util.LinePointIterator3D;
|
||||
|
||||
/**
|
||||
* Geodata.
|
||||
* @author -Nemesiss-, HorridoJoho
|
||||
*/
|
||||
public class GeoData
|
||||
{
|
||||
private static final Logger LOGGER = Logger.getLogger(GeoData.class.getName());
|
||||
private static final String FILE_NAME_FORMAT = "%d_%d.l2j";
|
||||
private static final int ELEVATED_SEE_OVER_DISTANCE = 2;
|
||||
private static final int MAX_SEE_OVER_HEIGHT = 48;
|
||||
private static final int SPAWN_Z_DELTA_LIMIT = 100;
|
||||
|
||||
private final GeoDriver _driver = new GeoDriver();
|
||||
|
||||
protected GeoData()
|
||||
{
|
||||
int loadedRegions = 0;
|
||||
try
|
||||
{
|
||||
for (int regionX = L2World.TILE_X_MIN; regionX <= L2World.TILE_X_MAX; regionX++)
|
||||
{
|
||||
for (int regionY = L2World.TILE_Y_MIN; regionY <= L2World.TILE_Y_MAX; regionY++)
|
||||
{
|
||||
final Path geoFilePath = Config.GEODATA_PATH.resolve(String.format(FILE_NAME_FORMAT, regionX, regionY));
|
||||
final Boolean loadFile = Config.GEODATA_REGIONS.get(regionX + "_" + regionY);
|
||||
if (loadFile != null)
|
||||
{
|
||||
if (loadFile)
|
||||
{
|
||||
LOGGER.info(getClass().getSimpleName() + ": Loading " + geoFilePath.getFileName() + "...");
|
||||
_driver.loadRegion(geoFilePath, regionX, regionY);
|
||||
loadedRegions++;
|
||||
}
|
||||
}
|
||||
else if (Config.TRY_LOAD_UNSPECIFIED_REGIONS && Files.exists(geoFilePath))
|
||||
{
|
||||
try
|
||||
{
|
||||
LOGGER.info(getClass().getSimpleName() + ": Loading " + geoFilePath.getFileName() + "...");
|
||||
_driver.loadRegion(geoFilePath, regionX, regionY);
|
||||
loadedRegions++;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": Failed to load " + geoFilePath.getFileName() + "!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.log(Level.SEVERE, getClass().getSimpleName() + ": Failed to load geodata!", e);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
LOGGER.info(getClass().getSimpleName() + ": Loaded " + loadedRegions + " regions.");
|
||||
}
|
||||
|
||||
public boolean hasGeoPos(int geoX, int geoY)
|
||||
{
|
||||
return _driver.hasGeoPos(geoX, geoY);
|
||||
}
|
||||
|
||||
public boolean checkNearestNswe(int geoX, int geoY, int worldZ, int nswe)
|
||||
{
|
||||
return _driver.checkNearestNswe(geoX, geoY, worldZ, nswe);
|
||||
}
|
||||
|
||||
public boolean checkNearestNsweAntiCornerCut(int geoX, int geoY, int worldZ, int nswe)
|
||||
{
|
||||
boolean can = true;
|
||||
if ((nswe & Cell.NSWE_NORTH_EAST) == Cell.NSWE_NORTH_EAST)
|
||||
{
|
||||
// can = canEnterNeighbors(prevX, prevY - 1, prevGeoZ, Direction.EAST) && canEnterNeighbors(prevX + 1, prevY, prevGeoZ, Direction.NORTH);
|
||||
can = checkNearestNswe(geoX, geoY - 1, worldZ, Cell.NSWE_EAST) && checkNearestNswe(geoX + 1, geoY, worldZ, Cell.NSWE_NORTH);
|
||||
}
|
||||
|
||||
if (can && ((nswe & Cell.NSWE_NORTH_WEST) == Cell.NSWE_NORTH_WEST))
|
||||
{
|
||||
// can = canEnterNeighbors(prevX, prevY - 1, prevGeoZ, Direction.WEST) && canEnterNeighbors(prevX - 1, prevY, prevGeoZ, Direction.NORTH);
|
||||
can = checkNearestNswe(geoX, geoY - 1, worldZ, Cell.NSWE_WEST) && checkNearestNswe(geoX, geoY - 1, worldZ, Cell.NSWE_NORTH);
|
||||
}
|
||||
|
||||
if (can && ((nswe & Cell.NSWE_SOUTH_EAST) == Cell.NSWE_SOUTH_EAST))
|
||||
{
|
||||
// can = canEnterNeighbors(prevX, prevY + 1, prevGeoZ, Direction.EAST) && canEnterNeighbors(prevX + 1, prevY, prevGeoZ, Direction.SOUTH);
|
||||
can = checkNearestNswe(geoX, geoY + 1, worldZ, Cell.NSWE_EAST) && checkNearestNswe(geoX + 1, geoY, worldZ, Cell.NSWE_SOUTH);
|
||||
}
|
||||
|
||||
if (can && ((nswe & Cell.NSWE_SOUTH_WEST) == Cell.NSWE_SOUTH_WEST))
|
||||
{
|
||||
// can = canEnterNeighbors(prevX, prevY + 1, prevGeoZ, Direction.WEST) && canEnterNeighbors(prevX - 1, prevY, prevGeoZ, Direction.SOUTH);
|
||||
can = checkNearestNswe(geoX, geoY + 1, worldZ, Cell.NSWE_WEST) && checkNearestNswe(geoX - 1, geoY, worldZ, Cell.NSWE_SOUTH);
|
||||
}
|
||||
|
||||
return can && checkNearestNswe(geoX, geoY, worldZ, nswe);
|
||||
}
|
||||
|
||||
public int getNearestZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return _driver.getNearestZ(geoX, geoY, worldZ);
|
||||
}
|
||||
|
||||
public int getNextLowerZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return _driver.getNextLowerZ(geoX, geoY, worldZ);
|
||||
}
|
||||
|
||||
public int getNextHigherZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return _driver.getNextHigherZ(geoX, geoY, worldZ);
|
||||
}
|
||||
|
||||
public int getGeoX(int worldX)
|
||||
{
|
||||
return _driver.getGeoX(worldX);
|
||||
}
|
||||
|
||||
public int getGeoY(int worldY)
|
||||
{
|
||||
return _driver.getGeoY(worldY);
|
||||
}
|
||||
|
||||
public int getWorldX(int geoX)
|
||||
{
|
||||
return _driver.getWorldX(geoX);
|
||||
}
|
||||
|
||||
public int getWorldY(int geoY)
|
||||
{
|
||||
return _driver.getWorldY(geoY);
|
||||
}
|
||||
|
||||
// ///////////////////
|
||||
// L2J METHODS
|
||||
/**
|
||||
* Gets the height.
|
||||
* @param x the x coordinate
|
||||
* @param y the y coordinate
|
||||
* @param z the z coordinate
|
||||
* @return the height
|
||||
*/
|
||||
public int getHeight(int x, int y, int z)
|
||||
{
|
||||
return getNearestZ(getGeoX(x), getGeoY(y), z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the spawn height.
|
||||
* @param x the x coordinate
|
||||
* @param y the y coordinate
|
||||
* @param z the the z coordinate
|
||||
* @return the spawn height
|
||||
*/
|
||||
public int getSpawnHeight(int x, int y, int z)
|
||||
{
|
||||
final int geoX = getGeoX(x);
|
||||
final int geoY = getGeoY(y);
|
||||
|
||||
if (!hasGeoPos(geoX, geoY))
|
||||
{
|
||||
return z;
|
||||
}
|
||||
|
||||
final int nextLowerZ = getNextLowerZ(geoX, geoY, z + 20);
|
||||
return Math.abs(nextLowerZ - z) <= SPAWN_Z_DELTA_LIMIT ? nextLowerZ : z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the spawn height.
|
||||
* @param location the location
|
||||
* @return the spawn height
|
||||
*/
|
||||
public int getSpawnHeight(Location location)
|
||||
{
|
||||
return getSpawnHeight(location.getX(), location.getY(), location.getZ());
|
||||
}
|
||||
|
||||
/**
|
||||
* Can see target. Doors as target always return true. Checks doors between.
|
||||
* @param cha the character
|
||||
* @param target the target
|
||||
* @return {@code true} if the character can see the target (LOS), {@code false} otherwise
|
||||
*/
|
||||
public boolean canSeeTarget(L2Object cha, L2Object target)
|
||||
{
|
||||
return (target != null) && (target.isDoor() || canSeeTarget(cha.getX(), cha.getY(), cha.getZ(), cha.getInstanceId(), target.getX(), target.getY(), target.getZ(), target.getInstanceId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Can see target. Checks doors between.
|
||||
* @param cha the character
|
||||
* @param worldPosition the world position
|
||||
* @return {@code true} if the character can see the target at the given world position, {@code false} otherwise
|
||||
*/
|
||||
public boolean canSeeTarget(L2Object cha, ILocational worldPosition)
|
||||
{
|
||||
return canSeeTarget(cha.getX(), cha.getY(), cha.getZ(), cha.getInstanceId(), worldPosition.getX(), worldPosition.getY(), worldPosition.getZ());
|
||||
}
|
||||
|
||||
/**
|
||||
* Can see target. Checks doors between.
|
||||
* @param x the x coordinate
|
||||
* @param y the y coordinate
|
||||
* @param z the z coordinate
|
||||
* @param instanceId
|
||||
* @param tx the target's x coordinate
|
||||
* @param ty the target's y coordinate
|
||||
* @param tz the target's z coordinate
|
||||
* @param tInstanceId the target's instanceId
|
||||
* @return
|
||||
*/
|
||||
public boolean canSeeTarget(int x, int y, int z, int instanceId, int tx, int ty, int tz, int tInstanceId)
|
||||
{
|
||||
return (instanceId == tInstanceId) && canSeeTarget(x, y, z, instanceId, tx, ty, tz);
|
||||
}
|
||||
|
||||
/**
|
||||
* Can see target. Checks doors between.
|
||||
* @param x the x coordinate
|
||||
* @param y the y coordinate
|
||||
* @param z the z coordinate
|
||||
* @param instanceId
|
||||
* @param tx the target's x coordinate
|
||||
* @param ty the target's y coordinate
|
||||
* @param tz the target's z coordinate
|
||||
* @return {@code true} if there is line of sight between the given coordinate sets, {@code false} otherwise
|
||||
*/
|
||||
public boolean canSeeTarget(int x, int y, int z, int instanceId, int tx, int ty, int tz)
|
||||
{
|
||||
return !DoorData.getInstance().checkIfDoorsBetween(x, y, z, tx, ty, tz, instanceId, true);
|
||||
}
|
||||
|
||||
private int getLosGeoZ(int prevX, int prevY, int prevGeoZ, int curX, int curY, int nswe)
|
||||
{
|
||||
if ((((nswe & Cell.NSWE_NORTH) != 0) && ((nswe & Cell.NSWE_SOUTH) != 0)) || (((nswe & Cell.NSWE_WEST) != 0) && ((nswe & Cell.NSWE_EAST) != 0)))
|
||||
{
|
||||
throw new RuntimeException("Multiple directions!");
|
||||
}
|
||||
return checkNearestNsweAntiCornerCut(prevX, prevY, prevGeoZ, nswe) ? getNearestZ(curX, curY, prevGeoZ) : getNextHigherZ(curX, curY, prevGeoZ);
|
||||
}
|
||||
|
||||
/**
|
||||
* Can see target. Does not check doors between.
|
||||
* @param x the x coordinate
|
||||
* @param y the y coordinate
|
||||
* @param z the z coordinate
|
||||
* @param tx the target's x coordinate
|
||||
* @param ty the target's y coordinate
|
||||
* @param tz the target's z coordinate
|
||||
* @return {@code true} if there is line of sight between the given coordinate sets, {@code false} otherwise
|
||||
*/
|
||||
public boolean canSeeTarget(int x, int y, int z, int tx, int ty, int tz)
|
||||
{
|
||||
int geoX = getGeoX(x);
|
||||
int geoY = getGeoY(y);
|
||||
int tGeoX = getGeoX(tx);
|
||||
int tGeoY = getGeoY(ty);
|
||||
|
||||
z = getNearestZ(geoX, geoY, z);
|
||||
tz = getNearestZ(tGeoX, tGeoY, tz);
|
||||
|
||||
// fastpath
|
||||
if ((geoX == tGeoX) && (geoY == tGeoY))
|
||||
{
|
||||
return !hasGeoPos(tGeoX, tGeoY) || (z == tz);
|
||||
}
|
||||
|
||||
if (tz > z)
|
||||
{
|
||||
int tmp = tx;
|
||||
tx = x;
|
||||
x = tmp;
|
||||
|
||||
tmp = ty;
|
||||
ty = y;
|
||||
y = tmp;
|
||||
|
||||
tmp = tz;
|
||||
tz = z;
|
||||
z = tmp;
|
||||
|
||||
tmp = tGeoX;
|
||||
tGeoX = geoX;
|
||||
geoX = tmp;
|
||||
|
||||
tmp = tGeoY;
|
||||
tGeoY = geoY;
|
||||
geoY = tmp;
|
||||
}
|
||||
|
||||
final LinePointIterator3D pointIter = new LinePointIterator3D(geoX, geoY, z, tGeoX, tGeoY, tz);
|
||||
// first point is guaranteed to be available, skip it, we can always see our own position
|
||||
pointIter.next();
|
||||
int prevX = pointIter.x();
|
||||
int prevY = pointIter.y();
|
||||
final int prevZ = pointIter.z();
|
||||
int prevGeoZ = prevZ;
|
||||
int ptIndex = 0;
|
||||
while (pointIter.next())
|
||||
{
|
||||
final int curX = pointIter.x();
|
||||
final int curY = pointIter.y();
|
||||
|
||||
if ((curX == prevX) && (curY == prevY))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
final int beeCurZ = pointIter.z();
|
||||
int curGeoZ = prevGeoZ;
|
||||
|
||||
// check if the position has geodata
|
||||
if (hasGeoPos(curX, curY))
|
||||
{
|
||||
final int nswe = GeoUtils.computeNswe(prevX, prevY, curX, curY);
|
||||
curGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, curX, curY, nswe);
|
||||
final int maxHeight = ptIndex < ELEVATED_SEE_OVER_DISTANCE ? z + MAX_SEE_OVER_HEIGHT : beeCurZ + MAX_SEE_OVER_HEIGHT;
|
||||
boolean canSeeThrough = false;
|
||||
if (curGeoZ <= maxHeight)
|
||||
{
|
||||
if ((nswe & Cell.NSWE_NORTH_EAST) == Cell.NSWE_NORTH_EAST)
|
||||
{
|
||||
final int northGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX, prevY - 1, Cell.NSWE_EAST);
|
||||
final int eastGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX + 1, prevY, Cell.NSWE_NORTH);
|
||||
canSeeThrough = (northGeoZ <= maxHeight) && (eastGeoZ <= maxHeight) && (northGeoZ <= getNearestZ(prevX, prevY - 1, beeCurZ)) && (eastGeoZ <= getNearestZ(prevX + 1, prevY, beeCurZ));
|
||||
}
|
||||
else if ((nswe & Cell.NSWE_NORTH_WEST) == Cell.NSWE_NORTH_WEST)
|
||||
{
|
||||
final int northGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX, prevY - 1, Cell.NSWE_WEST);
|
||||
final int westGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX - 1, prevY, Cell.NSWE_NORTH);
|
||||
canSeeThrough = (northGeoZ <= maxHeight) && (westGeoZ <= maxHeight) && (northGeoZ <= getNearestZ(prevX, prevY - 1, beeCurZ)) && (westGeoZ <= getNearestZ(prevX - 1, prevY, beeCurZ));
|
||||
}
|
||||
else if ((nswe & Cell.NSWE_SOUTH_EAST) == Cell.NSWE_SOUTH_EAST)
|
||||
{
|
||||
final int southGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX, prevY + 1, Cell.NSWE_EAST);
|
||||
final int eastGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX + 1, prevY, Cell.NSWE_SOUTH);
|
||||
canSeeThrough = (southGeoZ <= maxHeight) && (eastGeoZ <= maxHeight) && (southGeoZ <= getNearestZ(prevX, prevY + 1, beeCurZ)) && (eastGeoZ <= getNearestZ(prevX + 1, prevY, beeCurZ));
|
||||
}
|
||||
else if ((nswe & Cell.NSWE_SOUTH_WEST) == Cell.NSWE_SOUTH_WEST)
|
||||
{
|
||||
final int southGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX, prevY + 1, Cell.NSWE_WEST);
|
||||
final int westGeoZ = getLosGeoZ(prevX, prevY, prevGeoZ, prevX - 1, prevY, Cell.NSWE_SOUTH);
|
||||
canSeeThrough = (southGeoZ <= maxHeight) && (westGeoZ <= maxHeight) && (southGeoZ <= getNearestZ(prevX, prevY + 1, beeCurZ)) && (westGeoZ <= getNearestZ(prevX - 1, prevY, beeCurZ));
|
||||
}
|
||||
else
|
||||
{
|
||||
canSeeThrough = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!canSeeThrough)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
prevX = curX;
|
||||
prevY = curY;
|
||||
prevGeoZ = curGeoZ;
|
||||
++ptIndex;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies if the is a path between origin's location and destination, if not returns the closest location.
|
||||
* @param origin the origin
|
||||
* @param destination the destination
|
||||
* @return the destination if there is a path or the closes location
|
||||
*/
|
||||
public Location moveCheck(ILocational origin, ILocational destination)
|
||||
{
|
||||
return moveCheck(origin.getX(), origin.getY(), origin.getZ(), destination.getX(), destination.getY(), destination.getZ(), origin.getInstanceId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Move check.
|
||||
* @param x the x coordinate
|
||||
* @param y the y coordinate
|
||||
* @param z the z coordinate
|
||||
* @param tx the target's x coordinate
|
||||
* @param ty the target's y coordinate
|
||||
* @param tz the target's z coordinate
|
||||
* @param instanceId the instance id
|
||||
* @return the last Location (x,y,z) where player can walk - just before wall
|
||||
*/
|
||||
public Location moveCheck(int x, int y, int z, int tx, int ty, int tz, int instanceId)
|
||||
{
|
||||
final int geoX = getGeoX(x);
|
||||
final int geoY = getGeoY(y);
|
||||
z = getNearestZ(geoX, geoY, z);
|
||||
final int tGeoX = getGeoX(tx);
|
||||
final int tGeoY = getGeoY(ty);
|
||||
tz = getNearestZ(tGeoX, tGeoY, tz);
|
||||
|
||||
if (DoorData.getInstance().checkIfDoorsBetween(x, y, z, tx, ty, tz, instanceId, false))
|
||||
{
|
||||
return new Location(x, y, getHeight(x, y, z));
|
||||
}
|
||||
|
||||
final LinePointIterator pointIter = new LinePointIterator(geoX, geoY, tGeoX, tGeoY);
|
||||
// first point is guaranteed to be available
|
||||
pointIter.next();
|
||||
int prevX = pointIter.x();
|
||||
int prevY = pointIter.y();
|
||||
int prevZ = z;
|
||||
|
||||
while (pointIter.next())
|
||||
{
|
||||
final int curX = pointIter.x();
|
||||
final int curY = pointIter.y();
|
||||
final int curZ = getNearestZ(curX, curY, prevZ);
|
||||
if (hasGeoPos(prevX, prevY) && !checkNearestNsweAntiCornerCut(prevX, prevY, prevZ, GeoUtils.computeNswe(prevX, prevY, curX, curY)))
|
||||
{
|
||||
// can't move, return previous location
|
||||
return new Location(getWorldX(prevX), getWorldY(prevY), prevZ);
|
||||
}
|
||||
prevX = curX;
|
||||
prevY = curY;
|
||||
prevZ = curZ;
|
||||
}
|
||||
return hasGeoPos(prevX, prevY) && (prevZ != tz) ? new Location(x, y, z) : new Location(tx, ty, tz);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if its possible to move from one location to another.
|
||||
* @param fromX the X coordinate to start checking from
|
||||
* @param fromY the Y coordinate to start checking from
|
||||
* @param fromZ the Z coordinate to start checking from
|
||||
* @param toX the X coordinate to end checking at
|
||||
* @param toY the Y coordinate to end checking at
|
||||
* @param toZ the Z coordinate to end checking at
|
||||
* @param instanceId the instance ID
|
||||
* @return {@code true} if the character at start coordinates can move to end coordinates, {@code false} otherwise
|
||||
*/
|
||||
public boolean canMove(int fromX, int fromY, int fromZ, int toX, int toY, int toZ, int instanceId)
|
||||
{
|
||||
final int geoX = getGeoX(fromX);
|
||||
final int geoY = getGeoY(fromY);
|
||||
fromZ = getNearestZ(geoX, geoY, fromZ);
|
||||
final int tGeoX = getGeoX(toX);
|
||||
final int tGeoY = getGeoY(toY);
|
||||
toZ = getNearestZ(tGeoX, tGeoY, toZ);
|
||||
|
||||
if (DoorData.getInstance().checkIfDoorsBetween(fromX, fromY, fromZ, toX, toY, toZ, instanceId, false))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
final LinePointIterator pointIter = new LinePointIterator(geoX, geoY, tGeoX, tGeoY);
|
||||
// first point is guaranteed to be available
|
||||
pointIter.next();
|
||||
int prevX = pointIter.x();
|
||||
int prevY = pointIter.y();
|
||||
int prevZ = fromZ;
|
||||
|
||||
while (pointIter.next())
|
||||
{
|
||||
final int curX = pointIter.x();
|
||||
final int curY = pointIter.y();
|
||||
final int curZ = getNearestZ(curX, curY, prevZ);
|
||||
if (hasGeoPos(prevX, prevY) && !checkNearestNsweAntiCornerCut(prevX, prevY, prevZ, GeoUtils.computeNswe(prevX, prevY, curX, curY)))
|
||||
{
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
prevX = curX;
|
||||
prevY = curY;
|
||||
prevZ = curZ;
|
||||
}
|
||||
return !hasGeoPos(prevX, prevY) || (prevZ == toZ);
|
||||
}
|
||||
|
||||
public int traceTerrainZ(int x, int y, int z, int tx, int ty)
|
||||
{
|
||||
final int geoX = getGeoX(x);
|
||||
final int geoY = getGeoY(y);
|
||||
z = getNearestZ(geoX, geoY, z);
|
||||
final int tGeoX = getGeoX(tx);
|
||||
final int tGeoY = getGeoY(ty);
|
||||
|
||||
final LinePointIterator pointIter = new LinePointIterator(geoX, geoY, tGeoX, tGeoY);
|
||||
// first point is guaranteed to be available
|
||||
pointIter.next();
|
||||
int prevZ = z;
|
||||
|
||||
while (pointIter.next())
|
||||
{
|
||||
prevZ = getNearestZ(pointIter.x(), pointIter.y(), prevZ);
|
||||
}
|
||||
|
||||
return prevZ;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if its possible to move from one location to another.
|
||||
* @param from the {@code ILocational} to start checking from
|
||||
* @param toX the X coordinate to end checking at
|
||||
* @param toY the Y coordinate to end checking at
|
||||
* @param toZ the Z coordinate to end checking at
|
||||
* @return {@code true} if the character at start coordinates can move to end coordinates, {@code false} otherwise
|
||||
*/
|
||||
public boolean canMove(ILocational from, int toX, int toY, int toZ)
|
||||
{
|
||||
return canMove(from.getX(), from.getY(), from.getZ(), toX, toY, toZ, from.getInstanceId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if its possible to move from one location to another.
|
||||
* @param from the {@code ILocational} to start checking from
|
||||
* @param to the {@code ILocational} to end checking at
|
||||
* @return {@code true} if the character at start coordinates can move to end coordinates, {@code false} otherwise
|
||||
*/
|
||||
public boolean canMove(ILocational from, ILocational to)
|
||||
{
|
||||
return canMove(from, to.getX(), to.getY(), to.getZ());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the specified position for available geodata.
|
||||
* @param x the X coordinate
|
||||
* @param y the Y coordinate
|
||||
* @return {@code true} if there is geodata for the given coordinates, {@code false} otherwise
|
||||
*/
|
||||
public boolean hasGeo(int x, int y)
|
||||
{
|
||||
return hasGeoPos(getGeoX(x), getGeoY(y));
|
||||
}
|
||||
|
||||
public static GeoData getInstance()
|
||||
{
|
||||
return SingletonHolder._instance;
|
||||
}
|
||||
|
||||
private static class SingletonHolder
|
||||
{
|
||||
protected static final GeoData _instance = new GeoData();
|
||||
}
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.geodriver;
|
||||
|
||||
/**
|
||||
* @author HorridoJoho
|
||||
*/
|
||||
public final class Cell
|
||||
{
|
||||
/** East NSWE flag */
|
||||
public static final byte NSWE_EAST = 1 << 0;
|
||||
/** West NSWE flag */
|
||||
public static final byte NSWE_WEST = 1 << 1;
|
||||
/** South NSWE flag */
|
||||
public static final byte NSWE_SOUTH = 1 << 2;
|
||||
/** North NSWE flag */
|
||||
public static final byte NSWE_NORTH = 1 << 3;
|
||||
|
||||
/** North-East NSWE flags */
|
||||
public static final byte NSWE_NORTH_EAST = NSWE_NORTH | NSWE_EAST;
|
||||
/** North-West NSWE flags */
|
||||
public static final byte NSWE_NORTH_WEST = NSWE_NORTH | NSWE_WEST;
|
||||
/** South-East NSWE flags */
|
||||
public static final byte NSWE_SOUTH_EAST = NSWE_SOUTH | NSWE_EAST;
|
||||
/** South-West NSWE flags */
|
||||
public static final byte NSWE_SOUTH_WEST = NSWE_SOUTH | NSWE_WEST;
|
||||
|
||||
/** All directions NSWE flags */
|
||||
public static final byte NSWE_ALL = NSWE_EAST | NSWE_WEST | NSWE_SOUTH | NSWE_NORTH;
|
||||
|
||||
private Cell()
|
||||
{
|
||||
}
|
||||
}
|
@@ -0,0 +1,162 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.geodriver;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.channels.FileChannel.MapMode;
|
||||
import java.nio.file.Path;
|
||||
import java.util.concurrent.atomic.AtomicReferenceArray;
|
||||
|
||||
import com.l2jmobius.gameserver.geodata.geodriver.regions.NullRegion;
|
||||
import com.l2jmobius.gameserver.geodata.geodriver.regions.Region;
|
||||
|
||||
/**
|
||||
* @author HorridoJoho
|
||||
*/
|
||||
public final class GeoDriver
|
||||
{
|
||||
// world dimensions: 1048576 * 1048576 = 1099511627776
|
||||
private static final int WORLD_MIN_X = -655360;
|
||||
private static final int WORLD_MAX_X = 393215;
|
||||
private static final int WORLD_MIN_Y = -589824;
|
||||
private static final int WORLD_MAX_Y = 458751;
|
||||
|
||||
/** Regions in the world on the x axis */
|
||||
public static final int GEO_REGIONS_X = 32;
|
||||
/** Regions in the world on the y axis */
|
||||
public static final int GEO_REGIONS_Y = 32;
|
||||
/** Region in the world */
|
||||
public static final int GEO_REGIONS = GEO_REGIONS_X * GEO_REGIONS_Y;
|
||||
|
||||
/** Blocks in the world on the x axis */
|
||||
public static final int GEO_BLOCKS_X = GEO_REGIONS_X * IRegion.REGION_BLOCKS_X;
|
||||
/** Blocks in the world on the y axis */
|
||||
public static final int GEO_BLOCKS_Y = GEO_REGIONS_Y * IRegion.REGION_BLOCKS_Y;
|
||||
/** Blocks in the world */
|
||||
public static final int GEO_BLOCKS = GEO_REGIONS * IRegion.REGION_BLOCKS;
|
||||
|
||||
/** Cells in the world on the x axis */
|
||||
public static final int GEO_CELLS_X = GEO_BLOCKS_X * IBlock.BLOCK_CELLS_X;
|
||||
/** Cells in the world in the y axis */
|
||||
public static final int GEO_CELLS_Y = GEO_BLOCKS_Y * IBlock.BLOCK_CELLS_Y;
|
||||
|
||||
/** The regions array */
|
||||
private final AtomicReferenceArray<IRegion> _regions = new AtomicReferenceArray<>(GEO_REGIONS);
|
||||
|
||||
public GeoDriver()
|
||||
{
|
||||
for (int i = 0; i < _regions.length(); i++)
|
||||
{
|
||||
_regions.set(i, NullRegion.INSTANCE);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkGeoX(int geoX)
|
||||
{
|
||||
if ((geoX < 0) || (geoX >= GEO_CELLS_X))
|
||||
{
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
private void checkGeoY(int geoY)
|
||||
{
|
||||
if ((geoY < 0) || (geoY >= GEO_CELLS_Y))
|
||||
{
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
private IRegion getRegion(int geoX, int geoY)
|
||||
{
|
||||
checkGeoX(geoX);
|
||||
checkGeoY(geoY);
|
||||
return _regions.get(((geoX / IRegion.REGION_CELLS_X) * GEO_REGIONS_Y) + (geoY / IRegion.REGION_CELLS_Y));
|
||||
}
|
||||
|
||||
public void loadRegion(Path filePath, int regionX, int regionY) throws IOException
|
||||
{
|
||||
final int regionOffset = (regionX * GEO_REGIONS_Y) + regionY;
|
||||
|
||||
try (RandomAccessFile raf = new RandomAccessFile(filePath.toFile(), "r"))
|
||||
{
|
||||
_regions.set(regionOffset, new Region(raf.getChannel().map(MapMode.READ_ONLY, 0, raf.length()).order(ByteOrder.LITTLE_ENDIAN)));
|
||||
}
|
||||
}
|
||||
|
||||
public void unloadRegion(int regionX, int regionY)
|
||||
{
|
||||
_regions.set((regionX * GEO_REGIONS_Y) + regionY, NullRegion.INSTANCE);
|
||||
}
|
||||
|
||||
public boolean hasGeoPos(int geoX, int geoY)
|
||||
{
|
||||
return getRegion(geoX, geoY).hasGeo();
|
||||
}
|
||||
|
||||
public boolean checkNearestNswe(int geoX, int geoY, int worldZ, int nswe)
|
||||
{
|
||||
return getRegion(geoX, geoY).checkNearestNswe(geoX, geoY, worldZ, nswe);
|
||||
}
|
||||
|
||||
public int getNearestZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return getRegion(geoX, geoY).getNearestZ(geoX, geoY, worldZ);
|
||||
}
|
||||
|
||||
public int getNextLowerZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return getRegion(geoX, geoY).getNextLowerZ(geoX, geoY, worldZ);
|
||||
}
|
||||
|
||||
public int getNextHigherZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return getRegion(geoX, geoY).getNextHigherZ(geoX, geoY, worldZ);
|
||||
}
|
||||
|
||||
public int getGeoX(int worldX)
|
||||
{
|
||||
if ((worldX < WORLD_MIN_X) || (worldX > WORLD_MAX_X))
|
||||
{
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
return (worldX - WORLD_MIN_X) / 16;
|
||||
}
|
||||
|
||||
public int getGeoY(int worldY)
|
||||
{
|
||||
if ((worldY < WORLD_MIN_Y) || (worldY > WORLD_MAX_Y))
|
||||
{
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
return (worldY - WORLD_MIN_Y) / 16;
|
||||
}
|
||||
|
||||
public int getWorldX(int geoX)
|
||||
{
|
||||
checkGeoX(geoX);
|
||||
return (geoX * 16) + WORLD_MIN_X + 8;
|
||||
}
|
||||
|
||||
public int getWorldY(int geoY)
|
||||
{
|
||||
checkGeoY(geoY);
|
||||
return (geoY * 16) + WORLD_MIN_Y + 8;
|
||||
}
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.geodriver;
|
||||
|
||||
/**
|
||||
* @author HorridoJoho
|
||||
*/
|
||||
public interface IBlock
|
||||
{
|
||||
int TYPE_FLAT = 0;
|
||||
int TYPE_COMPLEX = 1;
|
||||
int TYPE_MULTILAYER = 2;
|
||||
|
||||
/** Cells in a block on the x axis */
|
||||
int BLOCK_CELLS_X = 8;
|
||||
/** Cells in a block on the y axis */
|
||||
int BLOCK_CELLS_Y = 8;
|
||||
/** Cells in a block */
|
||||
int BLOCK_CELLS = BLOCK_CELLS_X * BLOCK_CELLS_Y;
|
||||
|
||||
boolean checkNearestNswe(int geoX, int geoY, int worldZ, int nswe);
|
||||
|
||||
int getNearestZ(int geoX, int geoY, int worldZ);
|
||||
|
||||
int getNextLowerZ(int geoX, int geoY, int worldZ);
|
||||
|
||||
int getNextHigherZ(int geoX, int geoY, int worldZ);
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.geodriver;
|
||||
|
||||
/**
|
||||
* @author HorridoJoho
|
||||
*/
|
||||
public interface IRegion
|
||||
{
|
||||
/** Blocks in a region on the x axis. */
|
||||
int REGION_BLOCKS_X = 256;
|
||||
/** Blocks in a region on the y axis. */
|
||||
int REGION_BLOCKS_Y = 256;
|
||||
/** Blocks in a region. */
|
||||
int REGION_BLOCKS = REGION_BLOCKS_X * REGION_BLOCKS_Y;
|
||||
|
||||
/** Cells in a region on the x axis. */
|
||||
int REGION_CELLS_X = REGION_BLOCKS_X * IBlock.BLOCK_CELLS_X;
|
||||
/** Cells in a regioin on the y axis. */
|
||||
int REGION_CELLS_Y = REGION_BLOCKS_Y * IBlock.BLOCK_CELLS_Y;
|
||||
/** Cells in a region. */
|
||||
int REGION_CELLS = REGION_CELLS_X * REGION_CELLS_Y;
|
||||
|
||||
boolean checkNearestNswe(int geoX, int geoY, int worldZ, int nswe);
|
||||
|
||||
int getNearestZ(int geoX, int geoY, int worldZ);
|
||||
|
||||
int getNextLowerZ(int geoX, int geoY, int worldZ);
|
||||
|
||||
int getNextHigherZ(int geoX, int geoY, int worldZ);
|
||||
|
||||
boolean hasGeo();
|
||||
}
|
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.geodriver.blocks;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import com.l2jmobius.gameserver.geodata.geodriver.IBlock;
|
||||
|
||||
/**
|
||||
* @author HorridoJoho
|
||||
*/
|
||||
public final class ComplexBlock implements IBlock
|
||||
{
|
||||
private final short[] _data;
|
||||
|
||||
public ComplexBlock(ByteBuffer bb)
|
||||
{
|
||||
_data = new short[IBlock.BLOCK_CELLS];
|
||||
for (int cellOffset = 0; cellOffset < IBlock.BLOCK_CELLS; cellOffset++)
|
||||
{
|
||||
_data[cellOffset] = bb.getShort();
|
||||
}
|
||||
}
|
||||
|
||||
private short _getCellData(int geoX, int geoY)
|
||||
{
|
||||
return _data[((geoX % IBlock.BLOCK_CELLS_X) * IBlock.BLOCK_CELLS_Y) + (geoY % IBlock.BLOCK_CELLS_Y)];
|
||||
}
|
||||
|
||||
private byte _getCellNSWE(int geoX, int geoY)
|
||||
{
|
||||
return (byte) (_getCellData(geoX, geoY) & 0x000F);
|
||||
}
|
||||
|
||||
private int _getCellHeight(int geoX, int geoY)
|
||||
{
|
||||
return (short) (_getCellData(geoX, geoY) & 0x0FFF0) >> 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkNearestNswe(int geoX, int geoY, int worldZ, int nswe)
|
||||
{
|
||||
return (_getCellNSWE(geoX, geoY) & nswe) == nswe;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNearestZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return _getCellHeight(geoX, geoY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNextLowerZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
final int cellHeight = _getCellHeight(geoX, geoY);
|
||||
return cellHeight <= worldZ ? cellHeight : worldZ;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNextHigherZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
final int cellHeight = _getCellHeight(geoX, geoY);
|
||||
return cellHeight >= worldZ ? cellHeight : worldZ;
|
||||
}
|
||||
}
|
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.geodriver.blocks;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import com.l2jmobius.gameserver.geodata.geodriver.IBlock;
|
||||
|
||||
/**
|
||||
* @author HorridoJoho
|
||||
*/
|
||||
public class FlatBlock implements IBlock
|
||||
{
|
||||
private final short _height;
|
||||
|
||||
public FlatBlock(ByteBuffer bb)
|
||||
{
|
||||
_height = bb.getShort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkNearestNswe(int geoX, int geoY, int worldZ, int nswe)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNearestZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return _height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNextLowerZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return _height <= worldZ ? _height : worldZ;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNextHigherZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return _height >= worldZ ? _height : worldZ;
|
||||
}
|
||||
}
|
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.geodriver.blocks;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import com.l2jmobius.gameserver.geodata.geodriver.IBlock;
|
||||
|
||||
/**
|
||||
* @author HorridoJoho
|
||||
*/
|
||||
public class MultilayerBlock implements IBlock
|
||||
{
|
||||
private final byte[] _data;
|
||||
|
||||
/**
|
||||
* Initializes a new instance of this block reading the specified buffer.
|
||||
* @param bb the buffer
|
||||
*/
|
||||
public MultilayerBlock(ByteBuffer bb)
|
||||
{
|
||||
final int start = bb.position();
|
||||
|
||||
for (int blockCellOffset = 0; blockCellOffset < IBlock.BLOCK_CELLS; blockCellOffset++)
|
||||
{
|
||||
final byte nLayers = bb.get();
|
||||
if ((nLayers <= 0) || (nLayers > 125))
|
||||
{
|
||||
throw new RuntimeException("L2JGeoDriver: Geo file corrupted! Invalid layers count!");
|
||||
}
|
||||
|
||||
bb.position(bb.position() + (nLayers * 2));
|
||||
}
|
||||
|
||||
_data = new byte[bb.position() - start];
|
||||
bb.position(start);
|
||||
bb.get(_data);
|
||||
}
|
||||
|
||||
private short _getNearestLayer(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
final int startOffset = _getCellDataOffset(geoX, geoY);
|
||||
final byte nLayers = _data[startOffset];
|
||||
final int endOffset = startOffset + 1 + (nLayers * 2);
|
||||
|
||||
// 1 layer at least was required on loading so this is set at least once on the loop below
|
||||
int nearestDZ = 0;
|
||||
short nearestData = 0;
|
||||
for (int offset = startOffset + 1; offset < endOffset; offset += 2)
|
||||
{
|
||||
final short layerData = _extractLayerData(offset);
|
||||
final int layerZ = _extractLayerHeight(layerData);
|
||||
if (layerZ == worldZ)
|
||||
{
|
||||
// exact z
|
||||
return layerData;
|
||||
}
|
||||
|
||||
final int layerDZ = Math.abs(layerZ - worldZ);
|
||||
if ((offset == (startOffset + 1)) || (layerDZ < nearestDZ))
|
||||
{
|
||||
nearestDZ = layerDZ;
|
||||
nearestData = layerData;
|
||||
}
|
||||
}
|
||||
|
||||
return nearestData;
|
||||
}
|
||||
|
||||
private int _getCellDataOffset(int geoX, int geoY)
|
||||
{
|
||||
final int cellLocalOffset = ((geoX % IBlock.BLOCK_CELLS_X) * IBlock.BLOCK_CELLS_Y) + (geoY % IBlock.BLOCK_CELLS_Y);
|
||||
int cellDataOffset = 0;
|
||||
// move index to cell, we need to parse on each request, OR we parse on creation and save indexes
|
||||
for (int i = 0; i < cellLocalOffset; i++)
|
||||
{
|
||||
cellDataOffset += 1 + (_data[cellDataOffset] * 2);
|
||||
}
|
||||
// now the index points to the cell we need
|
||||
|
||||
return cellDataOffset;
|
||||
}
|
||||
|
||||
private short _extractLayerData(int dataOffset)
|
||||
{
|
||||
return (short) ((_data[dataOffset] & 0xFF) | (_data[dataOffset + 1] << 8));
|
||||
}
|
||||
|
||||
private int _getNearestNSWE(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return _extractLayerNswe(_getNearestLayer(geoX, geoY, worldZ));
|
||||
}
|
||||
|
||||
private int _extractLayerNswe(short layer)
|
||||
{
|
||||
return (byte) (layer & 0x000F);
|
||||
}
|
||||
|
||||
private int _extractLayerHeight(short layer)
|
||||
{
|
||||
layer = (short) (layer & 0x0fff0);
|
||||
return layer >> 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkNearestNswe(int geoX, int geoY, int worldZ, int nswe)
|
||||
{
|
||||
return (_getNearestNSWE(geoX, geoY, worldZ) & nswe) == nswe;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNearestZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return _extractLayerHeight(_getNearestLayer(geoX, geoY, worldZ));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNextLowerZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
final int startOffset = _getCellDataOffset(geoX, geoY);
|
||||
final byte nLayers = _data[startOffset];
|
||||
final int endOffset = startOffset + 1 + (nLayers * 2);
|
||||
|
||||
int lowerZ = Integer.MIN_VALUE;
|
||||
for (int offset = startOffset + 1; offset < endOffset; offset += 2)
|
||||
{
|
||||
final short layerData = _extractLayerData(offset);
|
||||
|
||||
final int layerZ = _extractLayerHeight(layerData);
|
||||
if (layerZ == worldZ)
|
||||
{
|
||||
// exact z
|
||||
return layerZ;
|
||||
}
|
||||
|
||||
if ((layerZ < worldZ) && (layerZ > lowerZ))
|
||||
{
|
||||
lowerZ = layerZ;
|
||||
}
|
||||
}
|
||||
|
||||
return lowerZ == Integer.MIN_VALUE ? worldZ : lowerZ;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNextHigherZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
final int startOffset = _getCellDataOffset(geoX, geoY);
|
||||
final byte nLayers = _data[startOffset];
|
||||
final int endOffset = startOffset + 1 + (nLayers * 2);
|
||||
|
||||
int higherZ = Integer.MAX_VALUE;
|
||||
for (int offset = startOffset + 1; offset < endOffset; offset += 2)
|
||||
{
|
||||
final short layerData = _extractLayerData(offset);
|
||||
|
||||
final int layerZ = _extractLayerHeight(layerData);
|
||||
if (layerZ == worldZ)
|
||||
{
|
||||
// exact z
|
||||
return layerZ;
|
||||
}
|
||||
|
||||
if ((layerZ > worldZ) && (layerZ < higherZ))
|
||||
{
|
||||
higherZ = layerZ;
|
||||
}
|
||||
}
|
||||
|
||||
return higherZ == Integer.MAX_VALUE ? worldZ : higherZ;
|
||||
}
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.geodriver.regions;
|
||||
|
||||
import com.l2jmobius.gameserver.geodata.geodriver.IRegion;
|
||||
|
||||
/**
|
||||
* @author HorridoJoho
|
||||
*/
|
||||
public final class NullRegion implements IRegion
|
||||
{
|
||||
public static final NullRegion INSTANCE = new NullRegion();
|
||||
|
||||
@Override
|
||||
public boolean checkNearestNswe(int geoX, int geoY, int worldZ, int nswe)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNearestZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return worldZ;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNextLowerZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return worldZ;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNextHigherZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return worldZ;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasGeo()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.geodriver.regions;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import com.l2jmobius.gameserver.geodata.geodriver.IBlock;
|
||||
import com.l2jmobius.gameserver.geodata.geodriver.IRegion;
|
||||
import com.l2jmobius.gameserver.geodata.geodriver.blocks.ComplexBlock;
|
||||
import com.l2jmobius.gameserver.geodata.geodriver.blocks.FlatBlock;
|
||||
import com.l2jmobius.gameserver.geodata.geodriver.blocks.MultilayerBlock;
|
||||
|
||||
/**
|
||||
* @author HorridoJoho
|
||||
*/
|
||||
public final class Region implements IRegion
|
||||
{
|
||||
private final IBlock[] _blocks = new IBlock[IRegion.REGION_BLOCKS];
|
||||
|
||||
public Region(ByteBuffer bb)
|
||||
{
|
||||
for (int blockOffset = 0; blockOffset < IRegion.REGION_BLOCKS; blockOffset++)
|
||||
{
|
||||
final int blockType = bb.get();
|
||||
switch (blockType)
|
||||
{
|
||||
case IBlock.TYPE_FLAT:
|
||||
{
|
||||
_blocks[blockOffset] = new FlatBlock(bb);
|
||||
break;
|
||||
}
|
||||
case IBlock.TYPE_COMPLEX:
|
||||
{
|
||||
_blocks[blockOffset] = new ComplexBlock(bb);
|
||||
break;
|
||||
}
|
||||
case IBlock.TYPE_MULTILAYER:
|
||||
{
|
||||
_blocks[blockOffset] = new MultilayerBlock(bb);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new RuntimeException("Invalid block type " + blockType + "!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IBlock getBlock(int geoX, int geoY)
|
||||
{
|
||||
return _blocks[(((geoX / IBlock.BLOCK_CELLS_X) % IRegion.REGION_BLOCKS_X) * IRegion.REGION_BLOCKS_Y) + ((geoY / IBlock.BLOCK_CELLS_Y) % IRegion.REGION_BLOCKS_Y)];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkNearestNswe(int geoX, int geoY, int worldZ, int nswe)
|
||||
{
|
||||
return getBlock(geoX, geoY).checkNearestNswe(geoX, geoY, worldZ, nswe);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNearestZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return getBlock(geoX, geoY).getNearestZ(geoX, geoY, worldZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNextLowerZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return getBlock(geoX, geoY).getNextLowerZ(geoX, geoY, worldZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNextHigherZ(int geoX, int geoY, int worldZ)
|
||||
{
|
||||
return getBlock(geoX, geoY).getNextHigherZ(geoX, geoY, worldZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasGeo()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.pathfinding;
|
||||
|
||||
public abstract class AbstractNode<Loc extends AbstractNodeLoc>
|
||||
{
|
||||
private Loc _loc;
|
||||
private AbstractNode<Loc> _parent;
|
||||
|
||||
public AbstractNode(Loc loc)
|
||||
{
|
||||
_loc = loc;
|
||||
}
|
||||
|
||||
public void setParent(AbstractNode<Loc> p)
|
||||
{
|
||||
_parent = p;
|
||||
}
|
||||
|
||||
public AbstractNode<Loc> getParent()
|
||||
{
|
||||
return _parent;
|
||||
}
|
||||
|
||||
public Loc getLoc()
|
||||
{
|
||||
return _loc;
|
||||
}
|
||||
|
||||
public void setLoc(Loc l)
|
||||
{
|
||||
_loc = l;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return (31 * 1) + ((_loc == null) ? 0 : _loc.hashCode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (this == obj)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (obj == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!(obj instanceof AbstractNode))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
final AbstractNode<?> other = (AbstractNode<?>) obj;
|
||||
if (_loc == null)
|
||||
{
|
||||
if (other._loc != null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (!_loc.equals(other._loc))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.pathfinding;
|
||||
|
||||
/**
|
||||
* @author -Nemesiss-
|
||||
*/
|
||||
public abstract class AbstractNodeLoc
|
||||
{
|
||||
public abstract int getX();
|
||||
|
||||
public abstract int getY();
|
||||
|
||||
public abstract int getZ();
|
||||
|
||||
public abstract void setZ(short z);
|
||||
|
||||
public abstract int getNodeX();
|
||||
|
||||
public abstract int getNodeY();
|
||||
}
|
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.pathfinding;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.l2jmobius.Config;
|
||||
import com.l2jmobius.gameserver.geodata.pathfinding.cellnodes.CellPathFinding;
|
||||
import com.l2jmobius.gameserver.geodata.pathfinding.geonodes.GeoPathFinding;
|
||||
import com.l2jmobius.gameserver.model.L2World;
|
||||
|
||||
/**
|
||||
* @author -Nemesiss-
|
||||
*/
|
||||
public abstract class PathFinding
|
||||
{
|
||||
public static PathFinding getInstance()
|
||||
{
|
||||
return Config.PATHFINDING == 1 ? GeoPathFinding.getInstance() : CellPathFinding.getInstance();
|
||||
}
|
||||
|
||||
public abstract boolean pathNodesExist(short regionoffset);
|
||||
|
||||
public abstract List<AbstractNodeLoc> findPath(int x, int y, int z, int tx, int ty, int tz, int instanceId, boolean playable);
|
||||
|
||||
// @formatter:off
|
||||
/*
|
||||
public List<AbstractNodeLoc> search(AbstractNode start, AbstractNode end, int instanceId)
|
||||
{
|
||||
// The simplest grid-based pathfinding.
|
||||
// Drawback is not having higher cost for diagonal movement (means funny routes)
|
||||
// Could be optimized e.g. not to calculate backwards as far as forwards.
|
||||
|
||||
// List of Visited Nodes
|
||||
LinkedList<AbstractNode> visited = new LinkedList<AbstractNode>();
|
||||
|
||||
// List of Nodes to Visit
|
||||
LinkedList<AbstractNode> to_visit = new LinkedList<AbstractNode>();
|
||||
to_visit.add(start);
|
||||
|
||||
int i = 0;
|
||||
while (i < 800)
|
||||
{
|
||||
AbstractNode node;
|
||||
try
|
||||
{
|
||||
node = to_visit.removeFirst();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// No Path found
|
||||
return null;
|
||||
}
|
||||
if (node.equals(end)) //path found!
|
||||
return constructPath(node, instanceId);
|
||||
else
|
||||
{
|
||||
i++;
|
||||
visited.add(node);
|
||||
node.attachNeighbors();
|
||||
Node[] neighbors = node.getNeighbors();
|
||||
if (neighbors == null)
|
||||
continue;
|
||||
for (Node n : neighbors)
|
||||
{
|
||||
if (!visited.contains(n) && !to_visit.contains(n))
|
||||
{
|
||||
n.setParent(node);
|
||||
to_visit.add(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//No Path found
|
||||
return null;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
public List<AbstractNodeLoc> searchAStar(Node start, Node end, int instanceId)
|
||||
{
|
||||
// Not operational yet?
|
||||
int start_x = start.getLoc().getX();
|
||||
int start_y = start.getLoc().getY();
|
||||
int end_x = end.getLoc().getX();
|
||||
int end_y = end.getLoc().getY();
|
||||
//List of Visited Nodes
|
||||
FastNodeList visited = new FastNodeList(800); // TODO! Add limit to cfg
|
||||
|
||||
// List of Nodes to Visit
|
||||
BinaryNodeHeap to_visit = new BinaryNodeHeap(800);
|
||||
to_visit.add(start);
|
||||
|
||||
int i = 0;
|
||||
while (i < 800)//TODO! Add limit to cfg
|
||||
{
|
||||
AbstractNode node;
|
||||
try
|
||||
{
|
||||
node = to_visit.removeFirst();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// No Path found
|
||||
return null;
|
||||
}
|
||||
if (node.equals(end)) //path found!
|
||||
return constructPath(node, instanceId);
|
||||
else
|
||||
{
|
||||
visited.add(node);
|
||||
node.attachNeighbors();
|
||||
for (Node n : node.getNeighbors())
|
||||
{
|
||||
if (!visited.contains(n) && !to_visit.contains(n))
|
||||
{
|
||||
i++;
|
||||
n.setParent(node);
|
||||
n.setCost(Math.abs(start_x - n.getLoc().getNodeX()) + Math.abs(start_y - n.getLoc().getNodeY())
|
||||
+ Math.abs(end_x - n.getLoc().getNodeX()) + Math.abs(end_y - n.getLoc().getNodeY()));
|
||||
to_visit.add(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//No Path found
|
||||
return null;
|
||||
}
|
||||
*/
|
||||
// @formatter:on
|
||||
|
||||
/**
|
||||
* Convert geodata position to pathnode position
|
||||
* @param geo_pos
|
||||
* @return pathnode position
|
||||
*/
|
||||
public short getNodePos(int geo_pos)
|
||||
{
|
||||
return (short) (geo_pos >> 3); // OK?
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert node position to pathnode block position
|
||||
* @param node_pos
|
||||
* @return pathnode block position (0...255)
|
||||
*/
|
||||
public short getNodeBlock(int node_pos)
|
||||
{
|
||||
return (short) (node_pos % 256);
|
||||
}
|
||||
|
||||
public byte getRegionX(int node_pos)
|
||||
{
|
||||
return (byte) ((node_pos >> 8) + L2World.TILE_X_MIN);
|
||||
}
|
||||
|
||||
public byte getRegionY(int node_pos)
|
||||
{
|
||||
return (byte) ((node_pos >> 8) + L2World.TILE_Y_MIN);
|
||||
}
|
||||
|
||||
public short getRegionOffset(byte rx, byte ry)
|
||||
{
|
||||
return (short) ((rx << 5) + ry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert pathnode x to World x position
|
||||
* @param node_x rx
|
||||
* @return
|
||||
*/
|
||||
public int calculateWorldX(short node_x)
|
||||
{
|
||||
return L2World.MAP_MIN_X + (node_x * 128) + 48;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert pathnode y to World y position
|
||||
* @param node_y
|
||||
* @return
|
||||
*/
|
||||
public int calculateWorldY(short node_y)
|
||||
{
|
||||
return L2World.MAP_MIN_Y + (node_y * 128) + 48;
|
||||
}
|
||||
|
||||
public String[] getStat()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.pathfinding.cellnodes;
|
||||
|
||||
import com.l2jmobius.gameserver.geodata.pathfinding.AbstractNode;
|
||||
|
||||
public class CellNode extends AbstractNode<NodeLoc>
|
||||
{
|
||||
private CellNode _next = null;
|
||||
private boolean _isInUse = true;
|
||||
private float _cost = -1000;
|
||||
|
||||
public CellNode(NodeLoc loc)
|
||||
{
|
||||
super(loc);
|
||||
}
|
||||
|
||||
public boolean isInUse()
|
||||
{
|
||||
return _isInUse;
|
||||
}
|
||||
|
||||
public void setInUse()
|
||||
{
|
||||
_isInUse = true;
|
||||
}
|
||||
|
||||
public CellNode getNext()
|
||||
{
|
||||
return _next;
|
||||
}
|
||||
|
||||
public void setNext(CellNode next)
|
||||
{
|
||||
_next = next;
|
||||
}
|
||||
|
||||
public float getCost()
|
||||
{
|
||||
return _cost;
|
||||
}
|
||||
|
||||
public void setCost(double cost)
|
||||
{
|
||||
_cost = (float) cost;
|
||||
}
|
||||
|
||||
public void free()
|
||||
{
|
||||
setParent(null);
|
||||
_cost = -1000;
|
||||
_isInUse = false;
|
||||
_next = null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,335 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.pathfinding.cellnodes;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import com.l2jmobius.Config;
|
||||
|
||||
/**
|
||||
* @author DS Credits to Diamond
|
||||
*/
|
||||
public class CellNodeBuffer
|
||||
{
|
||||
private static final int MAX_ITERATIONS = 3500;
|
||||
|
||||
private final ReentrantLock _lock = new ReentrantLock();
|
||||
private final int _mapSize;
|
||||
private final CellNode[][] _buffer;
|
||||
|
||||
private int _baseX = 0;
|
||||
private int _baseY = 0;
|
||||
|
||||
private int _targetX = 0;
|
||||
private int _targetY = 0;
|
||||
private int _targetZ = 0;
|
||||
|
||||
private long _timeStamp = 0;
|
||||
private long _lastElapsedTime = 0;
|
||||
|
||||
private CellNode _current = null;
|
||||
|
||||
public CellNodeBuffer(int size)
|
||||
{
|
||||
_mapSize = size;
|
||||
_buffer = new CellNode[_mapSize][_mapSize];
|
||||
}
|
||||
|
||||
public final boolean lock()
|
||||
{
|
||||
return _lock.tryLock();
|
||||
}
|
||||
|
||||
public final CellNode findPath(int x, int y, int z, int tx, int ty, int tz)
|
||||
{
|
||||
_timeStamp = System.currentTimeMillis();
|
||||
_baseX = x + ((tx - x - _mapSize) / 2); // middle of the line (x,y) - (tx,ty)
|
||||
_baseY = y + ((ty - y - _mapSize) / 2); // will be in the center of the buffer
|
||||
_targetX = tx;
|
||||
_targetY = ty;
|
||||
_targetZ = tz;
|
||||
_current = getNode(x, y, z);
|
||||
_current.setCost(getCost(x, y, z, Config.HIGH_WEIGHT));
|
||||
|
||||
for (int count = 0; count < MAX_ITERATIONS; count++)
|
||||
{
|
||||
if ((_current.getLoc().getNodeX() == _targetX) && (_current.getLoc().getNodeY() == _targetY) && (Math.abs(_current.getLoc().getZ() - _targetZ) < 64))
|
||||
{
|
||||
return _current; // found
|
||||
}
|
||||
|
||||
getNeighbors();
|
||||
if (_current.getNext() == null)
|
||||
{
|
||||
return null; // no more ways
|
||||
}
|
||||
|
||||
_current = _current.getNext();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public final void free()
|
||||
{
|
||||
_current = null;
|
||||
|
||||
CellNode node;
|
||||
for (int i = 0; i < _mapSize; i++)
|
||||
{
|
||||
for (int j = 0; j < _mapSize; j++)
|
||||
{
|
||||
node = _buffer[i][j];
|
||||
if (node != null)
|
||||
{
|
||||
node.free();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_lock.unlock();
|
||||
_lastElapsedTime = System.currentTimeMillis() - _timeStamp;
|
||||
}
|
||||
|
||||
public final long getElapsedTime()
|
||||
{
|
||||
return _lastElapsedTime;
|
||||
}
|
||||
|
||||
public final List<CellNode> debugPath()
|
||||
{
|
||||
final List<CellNode> result = new LinkedList<>();
|
||||
for (CellNode n = _current; n.getParent() != null; n = (CellNode) n.getParent())
|
||||
{
|
||||
result.add(n);
|
||||
n.setCost(-n.getCost());
|
||||
}
|
||||
|
||||
for (int i = 0; i < _mapSize; i++)
|
||||
{
|
||||
for (int j = 0; j < _mapSize; j++)
|
||||
{
|
||||
final CellNode n = _buffer[i][j];
|
||||
if ((n == null) || !n.isInUse() || (n.getCost() <= 0))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
result.add(n);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private final void getNeighbors()
|
||||
{
|
||||
if (_current.getLoc().canGoNone())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
final int x = _current.getLoc().getNodeX();
|
||||
final int y = _current.getLoc().getNodeY();
|
||||
final int z = _current.getLoc().getZ();
|
||||
|
||||
CellNode nodeE = null;
|
||||
CellNode nodeS = null;
|
||||
CellNode nodeW = null;
|
||||
CellNode nodeN = null;
|
||||
|
||||
// East
|
||||
if (_current.getLoc().canGoEast())
|
||||
{
|
||||
nodeE = addNode(x + 1, y, z, false);
|
||||
}
|
||||
|
||||
// South
|
||||
if (_current.getLoc().canGoSouth())
|
||||
{
|
||||
nodeS = addNode(x, y + 1, z, false);
|
||||
}
|
||||
|
||||
// West
|
||||
if (_current.getLoc().canGoWest())
|
||||
{
|
||||
nodeW = addNode(x - 1, y, z, false);
|
||||
}
|
||||
|
||||
// North
|
||||
if (_current.getLoc().canGoNorth())
|
||||
{
|
||||
nodeN = addNode(x, y - 1, z, false);
|
||||
}
|
||||
|
||||
if (!Config.ADVANCED_DIAGONAL_STRATEGY)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// SouthEast
|
||||
if ((nodeE != null) && (nodeS != null) && nodeE.getLoc().canGoSouth() && nodeS.getLoc().canGoEast())
|
||||
{
|
||||
addNode(x + 1, y + 1, z, true);
|
||||
}
|
||||
|
||||
// SouthWest
|
||||
if ((nodeS != null) && (nodeW != null) && nodeW.getLoc().canGoSouth() && nodeS.getLoc().canGoWest())
|
||||
{
|
||||
addNode(x - 1, y + 1, z, true);
|
||||
}
|
||||
|
||||
// NorthEast
|
||||
if ((nodeN != null) && (nodeE != null) && nodeE.getLoc().canGoNorth() && nodeN.getLoc().canGoEast())
|
||||
{
|
||||
addNode(x + 1, y - 1, z, true);
|
||||
}
|
||||
|
||||
// NorthWest
|
||||
if ((nodeN != null) && (nodeW != null) && nodeW.getLoc().canGoNorth() && nodeN.getLoc().canGoWest())
|
||||
{
|
||||
addNode(x - 1, y - 1, z, true);
|
||||
}
|
||||
}
|
||||
|
||||
private final CellNode getNode(int x, int y, int z)
|
||||
{
|
||||
final int aX = x - _baseX;
|
||||
if ((aX < 0) || (aX >= _mapSize))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
final int aY = y - _baseY;
|
||||
if ((aY < 0) || (aY >= _mapSize))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
CellNode result = _buffer[aX][aY];
|
||||
if (result == null)
|
||||
{
|
||||
result = new CellNode(new NodeLoc(x, y, z));
|
||||
_buffer[aX][aY] = result;
|
||||
}
|
||||
else if (!result.isInUse())
|
||||
{
|
||||
result.setInUse();
|
||||
// reinit node if needed
|
||||
if (result.getLoc() != null)
|
||||
{
|
||||
result.getLoc().set(x, y, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.setLoc(new NodeLoc(x, y, z));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private final CellNode addNode(int x, int y, int z, boolean diagonal)
|
||||
{
|
||||
final CellNode newNode = getNode(x, y, z);
|
||||
if (newNode == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (newNode.getCost() >= 0)
|
||||
{
|
||||
return newNode;
|
||||
}
|
||||
|
||||
final int geoZ = newNode.getLoc().getZ();
|
||||
|
||||
final int stepZ = Math.abs(geoZ - _current.getLoc().getZ());
|
||||
float weight = diagonal ? Config.DIAGONAL_WEIGHT : Config.LOW_WEIGHT;
|
||||
|
||||
if (!newNode.getLoc().canGoAll() || (stepZ > 16))
|
||||
{
|
||||
weight = Config.HIGH_WEIGHT;
|
||||
}
|
||||
else if (isHighWeight(x + 1, y, geoZ))
|
||||
{
|
||||
weight = Config.MEDIUM_WEIGHT;
|
||||
}
|
||||
else if (isHighWeight(x - 1, y, geoZ))
|
||||
{
|
||||
weight = Config.MEDIUM_WEIGHT;
|
||||
}
|
||||
else if (isHighWeight(x, y + 1, geoZ))
|
||||
{
|
||||
weight = Config.MEDIUM_WEIGHT;
|
||||
}
|
||||
else if (isHighWeight(x, y - 1, geoZ))
|
||||
{
|
||||
weight = Config.MEDIUM_WEIGHT;
|
||||
}
|
||||
|
||||
newNode.setParent(_current);
|
||||
newNode.setCost(getCost(x, y, geoZ, weight));
|
||||
|
||||
CellNode node = _current;
|
||||
int count = 0;
|
||||
while ((node.getNext() != null) && (count < (MAX_ITERATIONS * 4)))
|
||||
{
|
||||
count++;
|
||||
if (node.getNext().getCost() > newNode.getCost())
|
||||
{
|
||||
// insert node into a chain
|
||||
newNode.setNext(node.getNext());
|
||||
break;
|
||||
}
|
||||
node = node.getNext();
|
||||
}
|
||||
if (count == (MAX_ITERATIONS * 4))
|
||||
{
|
||||
System.err.println("Pathfinding: too long loop detected, cost:" + newNode.getCost());
|
||||
}
|
||||
|
||||
node.setNext(newNode); // add last
|
||||
|
||||
return newNode;
|
||||
}
|
||||
|
||||
private final boolean isHighWeight(int x, int y, int z)
|
||||
{
|
||||
final CellNode result = getNode(x, y, z);
|
||||
return (result == null) || !result.getLoc().canGoAll() || (Math.abs(result.getLoc().getZ() - z) > 16);
|
||||
}
|
||||
|
||||
private final double getCost(int x, int y, int z, float weight)
|
||||
{
|
||||
final int dX = x - _targetX;
|
||||
final int dY = y - _targetY;
|
||||
final int dZ = z - _targetZ;
|
||||
// Math.abs(dx) + Math.abs(dy) + Math.abs(dz) / 16
|
||||
double result = Math.sqrt((dX * dX) + (dY * dY) + ((dZ * dZ) / 256.0));
|
||||
if (result > weight)
|
||||
{
|
||||
result += weight;
|
||||
}
|
||||
|
||||
if (result > Float.MAX_VALUE)
|
||||
{
|
||||
result = Float.MAX_VALUE;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,429 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.pathfinding.cellnodes;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.l2jmobius.Config;
|
||||
import com.l2jmobius.gameserver.geodata.GeoData;
|
||||
import com.l2jmobius.gameserver.geodata.pathfinding.AbstractNode;
|
||||
import com.l2jmobius.gameserver.geodata.pathfinding.AbstractNodeLoc;
|
||||
import com.l2jmobius.gameserver.geodata.pathfinding.PathFinding;
|
||||
import com.l2jmobius.gameserver.idfactory.IdFactory;
|
||||
import com.l2jmobius.gameserver.model.itemcontainer.Inventory;
|
||||
import com.l2jmobius.gameserver.model.items.instance.L2ItemInstance;
|
||||
|
||||
/**
|
||||
* @author Sami, DS Credits to Diamond
|
||||
*/
|
||||
public class CellPathFinding extends PathFinding
|
||||
{
|
||||
private static final Logger _log = Logger.getLogger(CellPathFinding.class.getName());
|
||||
private BufferInfo[] _allBuffers;
|
||||
private int _findSuccess = 0;
|
||||
private int _findFails = 0;
|
||||
private int _postFilterUses = 0;
|
||||
private int _postFilterPlayableUses = 0;
|
||||
private int _postFilterPasses = 0;
|
||||
private long _postFilterElapsed = 0;
|
||||
|
||||
private List<L2ItemInstance> _debugItems = null;
|
||||
|
||||
public static CellPathFinding getInstance()
|
||||
{
|
||||
return SingletonHolder._instance;
|
||||
}
|
||||
|
||||
protected CellPathFinding()
|
||||
{
|
||||
try
|
||||
{
|
||||
final String[] array = Config.PATHFIND_BUFFERS.split(";");
|
||||
|
||||
_allBuffers = new BufferInfo[array.length];
|
||||
|
||||
String buf;
|
||||
String[] args;
|
||||
for (int i = 0; i < array.length; i++)
|
||||
{
|
||||
buf = array[i];
|
||||
args = buf.split("x");
|
||||
if (args.length != 2)
|
||||
{
|
||||
throw new Exception("Invalid buffer definition: " + buf);
|
||||
}
|
||||
|
||||
_allBuffers[i] = new BufferInfo(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, "CellPathFinding: Problem during buffer init: " + e.getMessage(), e);
|
||||
throw new Error("CellPathFinding: load aborted");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean pathNodesExist(short regionoffset)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AbstractNodeLoc> findPath(int x, int y, int z, int tx, int ty, int tz, int instanceId, boolean playable)
|
||||
{
|
||||
final int gx = GeoData.getInstance().getGeoX(x);
|
||||
final int gy = GeoData.getInstance().getGeoY(y);
|
||||
if (!GeoData.getInstance().hasGeo(x, y))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
final int gz = GeoData.getInstance().getHeight(x, y, z);
|
||||
final int gtx = GeoData.getInstance().getGeoX(tx);
|
||||
final int gty = GeoData.getInstance().getGeoY(ty);
|
||||
if (!GeoData.getInstance().hasGeo(tx, ty))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
final int gtz = GeoData.getInstance().getHeight(tx, ty, tz);
|
||||
final CellNodeBuffer buffer = alloc(64 + (2 * Math.max(Math.abs(gx - gtx), Math.abs(gy - gty))), playable);
|
||||
if (buffer == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
final boolean debug = playable && Config.DEBUG_PATH;
|
||||
|
||||
if (debug)
|
||||
{
|
||||
if (_debugItems == null)
|
||||
{
|
||||
_debugItems = new CopyOnWriteArrayList<>();
|
||||
}
|
||||
else
|
||||
{
|
||||
for (L2ItemInstance item : _debugItems)
|
||||
{
|
||||
item.decayMe();
|
||||
}
|
||||
|
||||
_debugItems.clear();
|
||||
}
|
||||
}
|
||||
|
||||
List<AbstractNodeLoc> path = null;
|
||||
try
|
||||
{
|
||||
final CellNode result = buffer.findPath(gx, gy, gz, gtx, gty, gtz);
|
||||
|
||||
if (debug)
|
||||
{
|
||||
for (CellNode n : buffer.debugPath())
|
||||
{
|
||||
if (n.getCost() < 0)
|
||||
{
|
||||
dropDebugItem(1831, (int) (-n.getCost() * 10), n.getLoc());
|
||||
}
|
||||
else
|
||||
{
|
||||
// known nodes
|
||||
dropDebugItem(Inventory.ADENA_ID, (int) (n.getCost() * 10), n.getLoc());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
_findFails++;
|
||||
return null;
|
||||
}
|
||||
|
||||
path = constructPath(result);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, "", e);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
buffer.free();
|
||||
}
|
||||
|
||||
if ((path.size() < 3) || (Config.MAX_POSTFILTER_PASSES <= 0))
|
||||
{
|
||||
_findSuccess++;
|
||||
return path;
|
||||
}
|
||||
|
||||
final long timeStamp = System.currentTimeMillis();
|
||||
_postFilterUses++;
|
||||
if (playable)
|
||||
{
|
||||
_postFilterPlayableUses++;
|
||||
}
|
||||
|
||||
boolean remove;
|
||||
int pass = 0;
|
||||
do
|
||||
{
|
||||
pass++;
|
||||
_postFilterPasses++;
|
||||
|
||||
remove = false;
|
||||
final Iterator<AbstractNodeLoc> endPoint = path.iterator();
|
||||
endPoint.next();
|
||||
int currentX = x;
|
||||
int currentY = y;
|
||||
int currentZ = z;
|
||||
|
||||
int midPoint = 0;
|
||||
while (endPoint.hasNext())
|
||||
{
|
||||
final AbstractNodeLoc locMiddle = path.get(midPoint);
|
||||
final AbstractNodeLoc locEnd = endPoint.next();
|
||||
if (GeoData.getInstance().canMove(currentX, currentY, currentZ, locEnd.getX(), locEnd.getY(), locEnd.getZ(), instanceId))
|
||||
{
|
||||
path.remove(midPoint);
|
||||
remove = true;
|
||||
if (debug)
|
||||
{
|
||||
dropDebugItem(735, 1, locMiddle);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
currentX = locMiddle.getX();
|
||||
currentY = locMiddle.getY();
|
||||
currentZ = locMiddle.getZ();
|
||||
midPoint++;
|
||||
}
|
||||
}
|
||||
}
|
||||
// only one postfilter pass for AI
|
||||
while (playable && remove && (path.size() > 2) && (pass < Config.MAX_POSTFILTER_PASSES));
|
||||
|
||||
if (debug)
|
||||
{
|
||||
path.forEach(n -> dropDebugItem(65, 1, n));
|
||||
}
|
||||
|
||||
_findSuccess++;
|
||||
_postFilterElapsed += System.currentTimeMillis() - timeStamp;
|
||||
return path;
|
||||
}
|
||||
|
||||
private List<AbstractNodeLoc> constructPath(AbstractNode<NodeLoc> node)
|
||||
{
|
||||
final List<AbstractNodeLoc> path = new CopyOnWriteArrayList<>();
|
||||
int previousDirectionX = Integer.MIN_VALUE;
|
||||
int previousDirectionY = Integer.MIN_VALUE;
|
||||
int directionX, directionY;
|
||||
|
||||
while (node.getParent() != null)
|
||||
{
|
||||
if (!Config.ADVANCED_DIAGONAL_STRATEGY && (node.getParent().getParent() != null))
|
||||
{
|
||||
final int tmpX = node.getLoc().getNodeX() - node.getParent().getParent().getLoc().getNodeX();
|
||||
final int tmpY = node.getLoc().getNodeY() - node.getParent().getParent().getLoc().getNodeY();
|
||||
if (Math.abs(tmpX) == Math.abs(tmpY))
|
||||
{
|
||||
directionX = tmpX;
|
||||
directionY = tmpY;
|
||||
}
|
||||
else
|
||||
{
|
||||
directionX = node.getLoc().getNodeX() - node.getParent().getLoc().getNodeX();
|
||||
directionY = node.getLoc().getNodeY() - node.getParent().getLoc().getNodeY();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
directionX = node.getLoc().getNodeX() - node.getParent().getLoc().getNodeX();
|
||||
directionY = node.getLoc().getNodeY() - node.getParent().getLoc().getNodeY();
|
||||
}
|
||||
|
||||
// only add a new route point if moving direction changes
|
||||
if ((directionX != previousDirectionX) || (directionY != previousDirectionY))
|
||||
{
|
||||
previousDirectionX = directionX;
|
||||
previousDirectionY = directionY;
|
||||
|
||||
path.add(0, node.getLoc());
|
||||
node.setLoc(null);
|
||||
}
|
||||
|
||||
node = node.getParent();
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
private final CellNodeBuffer alloc(int size, boolean playable)
|
||||
{
|
||||
CellNodeBuffer current = null;
|
||||
for (BufferInfo i : _allBuffers)
|
||||
{
|
||||
if (i.mapSize >= size)
|
||||
{
|
||||
for (CellNodeBuffer buf : i.bufs)
|
||||
{
|
||||
if (buf.lock())
|
||||
{
|
||||
i.uses++;
|
||||
if (playable)
|
||||
{
|
||||
i.playableUses++;
|
||||
}
|
||||
i.elapsed += buf.getElapsedTime();
|
||||
current = buf;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (current != null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// not found, allocate temporary buffer
|
||||
current = new CellNodeBuffer(i.mapSize);
|
||||
current.lock();
|
||||
if (i.bufs.size() < i.count)
|
||||
{
|
||||
i.bufs.add(current);
|
||||
i.uses++;
|
||||
if (playable)
|
||||
{
|
||||
i.playableUses++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
i.overflows++;
|
||||
if (playable)
|
||||
{
|
||||
i.playableOverflows++;
|
||||
// System.err.println("Overflow, size requested: " + size + " playable:"+playable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
private final void dropDebugItem(int itemId, int num, AbstractNodeLoc loc)
|
||||
{
|
||||
final L2ItemInstance item = new L2ItemInstance(IdFactory.getInstance().getNextId(), itemId);
|
||||
item.setCount(num);
|
||||
item.spawnMe(loc.getX(), loc.getY(), loc.getZ());
|
||||
_debugItems.add(item);
|
||||
}
|
||||
|
||||
private static final class BufferInfo
|
||||
{
|
||||
final int mapSize;
|
||||
final int count;
|
||||
List<CellNodeBuffer> bufs;
|
||||
int uses = 0;
|
||||
int playableUses = 0;
|
||||
int overflows = 0;
|
||||
int playableOverflows = 0;
|
||||
long elapsed = 0;
|
||||
|
||||
public BufferInfo(int size, int cnt)
|
||||
{
|
||||
mapSize = size;
|
||||
count = cnt;
|
||||
bufs = new ArrayList<>(count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
final StringBuilder sb = new StringBuilder(100);
|
||||
sb.append(mapSize);
|
||||
sb.append("x");
|
||||
sb.append(mapSize);
|
||||
sb.append(" num:");
|
||||
sb.append(bufs.size());
|
||||
sb.append("/");
|
||||
sb.append(count);
|
||||
sb.append(" uses:");
|
||||
sb.append(uses);
|
||||
sb.append("/");
|
||||
sb.append(playableUses);
|
||||
if (uses > 0)
|
||||
{
|
||||
sb.append(" total/avg(ms):");
|
||||
sb.append(elapsed);
|
||||
sb.append("/");
|
||||
sb.append(String.format("%1.2f", (double) elapsed / uses));
|
||||
}
|
||||
|
||||
sb.append(" ovf:");
|
||||
sb.append(overflows);
|
||||
sb.append("/");
|
||||
sb.append(playableOverflows);
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getStat()
|
||||
{
|
||||
final String[] result = new String[_allBuffers.length + 1];
|
||||
for (int i = 0; i < _allBuffers.length; i++)
|
||||
{
|
||||
result[i] = _allBuffers[i].toString();
|
||||
}
|
||||
|
||||
final StringBuilder sb = new StringBuilder(128);
|
||||
sb.append("LOS postfilter uses:");
|
||||
sb.append(_postFilterUses);
|
||||
sb.append("/");
|
||||
sb.append(_postFilterPlayableUses);
|
||||
if (_postFilterUses > 0)
|
||||
{
|
||||
sb.append(" total/avg(ms):");
|
||||
sb.append(_postFilterElapsed);
|
||||
sb.append("/");
|
||||
sb.append(String.format("%1.2f", (double) _postFilterElapsed / _postFilterUses));
|
||||
sb.append(" passes total/avg:");
|
||||
sb.append(_postFilterPasses);
|
||||
sb.append("/");
|
||||
sb.append(String.format("%1.1f", (double) _postFilterPasses / _postFilterUses));
|
||||
sb.append(Config.EOL);
|
||||
}
|
||||
sb.append("Pathfind success/fail:");
|
||||
sb.append(_findSuccess);
|
||||
sb.append("/");
|
||||
sb.append(_findFails);
|
||||
result[result.length - 1] = sb.toString();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static class SingletonHolder
|
||||
{
|
||||
protected static final CellPathFinding _instance = new CellPathFinding();
|
||||
}
|
||||
}
|
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.pathfinding.cellnodes;
|
||||
|
||||
import com.l2jmobius.gameserver.geodata.GeoData;
|
||||
import com.l2jmobius.gameserver.geodata.geodriver.Cell;
|
||||
import com.l2jmobius.gameserver.geodata.pathfinding.AbstractNodeLoc;
|
||||
|
||||
/**
|
||||
* @author -Nemesiss-, HorridoJoho
|
||||
*/
|
||||
public class NodeLoc extends AbstractNodeLoc
|
||||
{
|
||||
private int _x;
|
||||
private int _y;
|
||||
private boolean _goNorth;
|
||||
private boolean _goEast;
|
||||
private boolean _goSouth;
|
||||
private boolean _goWest;
|
||||
private int _geoHeight;
|
||||
|
||||
public NodeLoc(int x, int y, int z)
|
||||
{
|
||||
set(x, y, z);
|
||||
}
|
||||
|
||||
public void set(int x, int y, int z)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_goNorth = GeoData.getInstance().checkNearestNswe(x, y, z, Cell.NSWE_NORTH);
|
||||
_goEast = GeoData.getInstance().checkNearestNswe(x, y, z, Cell.NSWE_EAST);
|
||||
_goSouth = GeoData.getInstance().checkNearestNswe(x, y, z, Cell.NSWE_SOUTH);
|
||||
_goWest = GeoData.getInstance().checkNearestNswe(x, y, z, Cell.NSWE_WEST);
|
||||
_geoHeight = GeoData.getInstance().getNearestZ(x, y, z);
|
||||
}
|
||||
|
||||
public boolean canGoNorth()
|
||||
{
|
||||
return _goNorth;
|
||||
}
|
||||
|
||||
public boolean canGoEast()
|
||||
{
|
||||
return _goEast;
|
||||
}
|
||||
|
||||
public boolean canGoSouth()
|
||||
{
|
||||
return _goSouth;
|
||||
}
|
||||
|
||||
public boolean canGoWest()
|
||||
{
|
||||
return _goWest;
|
||||
}
|
||||
|
||||
public boolean canGoNone()
|
||||
{
|
||||
return !canGoNorth() && !canGoEast() && !canGoSouth() && !canGoWest();
|
||||
}
|
||||
|
||||
public boolean canGoAll()
|
||||
{
|
||||
return canGoNorth() && canGoEast() && canGoSouth() && canGoWest();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getX()
|
||||
{
|
||||
return GeoData.getInstance().getWorldX(_x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getY()
|
||||
{
|
||||
return GeoData.getInstance().getWorldY(_y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getZ()
|
||||
{
|
||||
return _geoHeight;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setZ(short z)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNodeX()
|
||||
{
|
||||
return _x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNodeY()
|
||||
{
|
||||
return _y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = (prime * result) + _x;
|
||||
result = (prime * result) + _y;
|
||||
|
||||
int nswe = 0;
|
||||
if (canGoNorth())
|
||||
{
|
||||
nswe |= Cell.NSWE_NORTH;
|
||||
}
|
||||
if (canGoEast())
|
||||
{
|
||||
nswe |= Cell.NSWE_EAST;
|
||||
}
|
||||
if (canGoSouth())
|
||||
{
|
||||
nswe |= Cell.NSWE_SOUTH;
|
||||
}
|
||||
if (canGoWest())
|
||||
{
|
||||
nswe |= Cell.NSWE_WEST;
|
||||
}
|
||||
|
||||
result = (prime * result) + (((_geoHeight & 0xFFFF) << 1) | nswe);
|
||||
return result;
|
||||
// return super.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (this == obj)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (obj == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!(obj instanceof NodeLoc))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
final NodeLoc other = (NodeLoc) obj;
|
||||
return (_x == other._x) && (_y == other._y) && (!_goNorth == !other._goNorth) && (!_goEast == !other._goEast) && (!_goSouth == !other._goSouth) && (!_goWest == !other._goWest) && (_geoHeight == other._geoHeight);
|
||||
}
|
||||
}
|
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.pathfinding.geonodes;
|
||||
|
||||
import com.l2jmobius.gameserver.geodata.pathfinding.AbstractNode;
|
||||
|
||||
/**
|
||||
* @author -Nemesiss-
|
||||
*/
|
||||
public class GeoNode extends AbstractNode<GeoNodeLoc>
|
||||
{
|
||||
private final int _neighborsIdx;
|
||||
private short _cost;
|
||||
private GeoNode[] _neighbors;
|
||||
|
||||
public GeoNode(GeoNodeLoc Loc, int Neighbors_idx)
|
||||
{
|
||||
super(Loc);
|
||||
_neighborsIdx = Neighbors_idx;
|
||||
}
|
||||
|
||||
public short getCost()
|
||||
{
|
||||
return _cost;
|
||||
}
|
||||
|
||||
public void setCost(int cost)
|
||||
{
|
||||
_cost = (short) cost;
|
||||
}
|
||||
|
||||
public GeoNode[] getNeighbors()
|
||||
{
|
||||
return _neighbors;
|
||||
}
|
||||
|
||||
public void attachNeighbors(GeoNode[] neighbors)
|
||||
{
|
||||
_neighbors = neighbors;
|
||||
}
|
||||
|
||||
public int getNeighborsIdx()
|
||||
{
|
||||
return _neighborsIdx;
|
||||
}
|
||||
}
|
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.pathfinding.geonodes;
|
||||
|
||||
import com.l2jmobius.gameserver.geodata.pathfinding.AbstractNodeLoc;
|
||||
import com.l2jmobius.gameserver.model.L2World;
|
||||
|
||||
/**
|
||||
* @author -Nemesiss-
|
||||
*/
|
||||
public class GeoNodeLoc extends AbstractNodeLoc
|
||||
{
|
||||
private final short _x;
|
||||
private final short _y;
|
||||
private final short _z;
|
||||
|
||||
public GeoNodeLoc(short x, short y, short z)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_z = z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getX()
|
||||
{
|
||||
return L2World.MAP_MIN_X + (_x * 128) + 48;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getY()
|
||||
{
|
||||
return L2World.MAP_MIN_Y + (_y * 128) + 48;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getZ()
|
||||
{
|
||||
return _z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setZ(short z)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNodeX()
|
||||
{
|
||||
return _x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNodeY()
|
||||
{
|
||||
return _y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = (prime * result) + _x;
|
||||
result = (prime * result) + _y;
|
||||
result = (prime * result) + _z;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (this == obj)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (obj == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!(obj instanceof GeoNodeLoc))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
final GeoNodeLoc other = (GeoNodeLoc) obj;
|
||||
return (_x == other._x) && (_y == other._y) && (_z == other._z);
|
||||
}
|
||||
}
|
@@ -0,0 +1,473 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.pathfinding.geonodes;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
import java.nio.MappedByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.l2jmobius.Config;
|
||||
import com.l2jmobius.gameserver.geodata.GeoData;
|
||||
import com.l2jmobius.gameserver.geodata.pathfinding.AbstractNode;
|
||||
import com.l2jmobius.gameserver.geodata.pathfinding.AbstractNodeLoc;
|
||||
import com.l2jmobius.gameserver.geodata.pathfinding.PathFinding;
|
||||
import com.l2jmobius.gameserver.geodata.pathfinding.utils.FastNodeList;
|
||||
import com.l2jmobius.gameserver.model.L2World;
|
||||
import com.l2jmobius.gameserver.model.Location;
|
||||
import com.l2jmobius.gameserver.util.Util;
|
||||
|
||||
/**
|
||||
* @author -Nemesiss-
|
||||
*/
|
||||
public class GeoPathFinding extends PathFinding
|
||||
{
|
||||
private static Logger _log = Logger.getLogger(GeoPathFinding.class.getName());
|
||||
private static Map<Short, ByteBuffer> _pathNodes = new HashMap<>();
|
||||
private static Map<Short, IntBuffer> _pathNodesIndex = new HashMap<>();
|
||||
|
||||
public static GeoPathFinding getInstance()
|
||||
{
|
||||
return SingletonHolder._instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean pathNodesExist(short regionoffset)
|
||||
{
|
||||
return _pathNodesIndex.containsKey(regionoffset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AbstractNodeLoc> findPath(int x, int y, int z, int tx, int ty, int tz, int instanceId, boolean playable)
|
||||
{
|
||||
final int gx = (x - L2World.MAP_MIN_X) >> 4;
|
||||
final int gy = (y - L2World.MAP_MIN_Y) >> 4;
|
||||
final short gz = (short) z;
|
||||
final int gtx = (tx - L2World.MAP_MIN_X) >> 4;
|
||||
final int gty = (ty - L2World.MAP_MIN_Y) >> 4;
|
||||
final short gtz = (short) tz;
|
||||
|
||||
final GeoNode start = readNode(gx, gy, gz);
|
||||
final GeoNode end = readNode(gtx, gty, gtz);
|
||||
if ((start == null) || (end == null))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (Math.abs(start.getLoc().getZ() - z) > 55)
|
||||
{
|
||||
return null; // not correct layer
|
||||
}
|
||||
if (Math.abs(end.getLoc().getZ() - tz) > 55)
|
||||
{
|
||||
return null; // not correct layer
|
||||
}
|
||||
if (start == end)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// TODO: Find closest path node we CAN access. Now only checks if we can not reach the closest
|
||||
Location temp = GeoData.getInstance().moveCheck(x, y, z, start.getLoc().getX(), start.getLoc().getY(), start.getLoc().getZ(), instanceId);
|
||||
if ((temp.getX() != start.getLoc().getX()) || (temp.getY() != start.getLoc().getY()))
|
||||
{
|
||||
return null; // cannot reach closest...
|
||||
}
|
||||
|
||||
// TODO: Find closest path node around target, now only checks if final location can be reached
|
||||
temp = GeoData.getInstance().moveCheck(tx, ty, tz, end.getLoc().getX(), end.getLoc().getY(), end.getLoc().getZ(), instanceId);
|
||||
if ((temp.getX() != end.getLoc().getX()) || (temp.getY() != end.getLoc().getY()))
|
||||
{
|
||||
return null; // cannot reach closest...
|
||||
}
|
||||
|
||||
// return searchAStar(start, end);
|
||||
return searchByClosest2(start, end);
|
||||
}
|
||||
|
||||
public List<AbstractNodeLoc> searchByClosest2(GeoNode start, GeoNode end)
|
||||
{
|
||||
// Always continues checking from the closest to target non-blocked
|
||||
// node from to_visit list. There's extra length in path if needed
|
||||
// to go backwards/sideways but when moving generally forwards, this is extra fast
|
||||
// and accurate. And can reach insane distances (try it with 800 nodes..).
|
||||
// Minimum required node count would be around 300-400.
|
||||
// Generally returns a bit (only a bit) more intelligent looking routes than
|
||||
// the basic version. Not a true distance image (which would increase CPU
|
||||
// load) level of intelligence though.
|
||||
|
||||
// List of Visited Nodes
|
||||
final FastNodeList visited = new FastNodeList(550);
|
||||
|
||||
// List of Nodes to Visit
|
||||
final LinkedList<GeoNode> to_visit = new LinkedList<>();
|
||||
to_visit.add(start);
|
||||
final int targetX = end.getLoc().getNodeX();
|
||||
final int targetY = end.getLoc().getNodeY();
|
||||
|
||||
int dx, dy;
|
||||
boolean added;
|
||||
int i = 0;
|
||||
while (i < 550)
|
||||
{
|
||||
GeoNode node;
|
||||
try
|
||||
{
|
||||
node = to_visit.removeFirst();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// No Path found
|
||||
return null;
|
||||
}
|
||||
if (node.equals(end))
|
||||
{
|
||||
return constructPath2(node);
|
||||
}
|
||||
|
||||
i++;
|
||||
visited.add(node);
|
||||
node.attachNeighbors(readNeighbors(node));
|
||||
final GeoNode[] neighbors = node.getNeighbors();
|
||||
if (neighbors == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
for (GeoNode n : neighbors)
|
||||
{
|
||||
if (!visited.containsRev(n) && !to_visit.contains(n))
|
||||
{
|
||||
added = false;
|
||||
n.setParent(node);
|
||||
dx = targetX - n.getLoc().getNodeX();
|
||||
dy = targetY - n.getLoc().getNodeY();
|
||||
n.setCost((dx * dx) + (dy * dy));
|
||||
for (int index = 0; index < to_visit.size(); index++)
|
||||
{
|
||||
// supposed to find it quite early..
|
||||
if (to_visit.get(index).getCost() > n.getCost())
|
||||
{
|
||||
to_visit.add(index, n);
|
||||
added = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!added)
|
||||
{
|
||||
to_visit.addLast(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// No Path found
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<AbstractNodeLoc> constructPath2(AbstractNode<GeoNodeLoc> node)
|
||||
{
|
||||
final LinkedList<AbstractNodeLoc> path = new LinkedList<>();
|
||||
int previousDirectionX = -1000;
|
||||
int previousDirectionY = -1000;
|
||||
int directionX;
|
||||
int directionY;
|
||||
|
||||
while (node.getParent() != null)
|
||||
{
|
||||
// only add a new route point if moving direction changes
|
||||
directionX = node.getLoc().getNodeX() - node.getParent().getLoc().getNodeX();
|
||||
directionY = node.getLoc().getNodeY() - node.getParent().getLoc().getNodeY();
|
||||
|
||||
if ((directionX != previousDirectionX) || (directionY != previousDirectionY))
|
||||
{
|
||||
previousDirectionX = directionX;
|
||||
previousDirectionY = directionY;
|
||||
path.addFirst(node.getLoc());
|
||||
}
|
||||
node = node.getParent();
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
private GeoNode[] readNeighbors(GeoNode n)
|
||||
{
|
||||
if (n.getLoc() == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int idx = n.getNeighborsIdx();
|
||||
|
||||
final int node_x = n.getLoc().getNodeX();
|
||||
final int node_y = n.getLoc().getNodeY();
|
||||
// short node_z = n.getLoc().getZ();
|
||||
|
||||
final short regoffset = getRegionOffset(getRegionX(node_x), getRegionY(node_y));
|
||||
final ByteBuffer pn = _pathNodes.get(regoffset);
|
||||
|
||||
final List<AbstractNode<GeoNodeLoc>> neighbors = new ArrayList<>(8);
|
||||
GeoNode newNode;
|
||||
short new_node_x, new_node_y;
|
||||
|
||||
// Region for sure will change, we must read from correct file
|
||||
byte neighbor = pn.get(idx++); // N
|
||||
if (neighbor > 0)
|
||||
{
|
||||
neighbor--;
|
||||
new_node_x = (short) node_x;
|
||||
new_node_y = (short) (node_y - 1);
|
||||
newNode = readNode(new_node_x, new_node_y, neighbor);
|
||||
if (newNode != null)
|
||||
{
|
||||
neighbors.add(newNode);
|
||||
}
|
||||
}
|
||||
neighbor = pn.get(idx++); // NE
|
||||
if (neighbor > 0)
|
||||
{
|
||||
neighbor--;
|
||||
new_node_x = (short) (node_x + 1);
|
||||
new_node_y = (short) (node_y - 1);
|
||||
newNode = readNode(new_node_x, new_node_y, neighbor);
|
||||
if (newNode != null)
|
||||
{
|
||||
neighbors.add(newNode);
|
||||
}
|
||||
}
|
||||
neighbor = pn.get(idx++); // E
|
||||
if (neighbor > 0)
|
||||
{
|
||||
neighbor--;
|
||||
new_node_x = (short) (node_x + 1);
|
||||
new_node_y = (short) node_y;
|
||||
newNode = readNode(new_node_x, new_node_y, neighbor);
|
||||
if (newNode != null)
|
||||
{
|
||||
neighbors.add(newNode);
|
||||
}
|
||||
}
|
||||
neighbor = pn.get(idx++); // SE
|
||||
if (neighbor > 0)
|
||||
{
|
||||
neighbor--;
|
||||
new_node_x = (short) (node_x + 1);
|
||||
new_node_y = (short) (node_y + 1);
|
||||
newNode = readNode(new_node_x, new_node_y, neighbor);
|
||||
if (newNode != null)
|
||||
{
|
||||
neighbors.add(newNode);
|
||||
}
|
||||
}
|
||||
neighbor = pn.get(idx++); // S
|
||||
if (neighbor > 0)
|
||||
{
|
||||
neighbor--;
|
||||
new_node_x = (short) node_x;
|
||||
new_node_y = (short) (node_y + 1);
|
||||
newNode = readNode(new_node_x, new_node_y, neighbor);
|
||||
if (newNode != null)
|
||||
{
|
||||
neighbors.add(newNode);
|
||||
}
|
||||
}
|
||||
neighbor = pn.get(idx++); // SW
|
||||
if (neighbor > 0)
|
||||
{
|
||||
neighbor--;
|
||||
new_node_x = (short) (node_x - 1);
|
||||
new_node_y = (short) (node_y + 1);
|
||||
newNode = readNode(new_node_x, new_node_y, neighbor);
|
||||
if (newNode != null)
|
||||
{
|
||||
neighbors.add(newNode);
|
||||
}
|
||||
}
|
||||
neighbor = pn.get(idx++); // W
|
||||
if (neighbor > 0)
|
||||
{
|
||||
neighbor--;
|
||||
new_node_x = (short) (node_x - 1);
|
||||
new_node_y = (short) node_y;
|
||||
newNode = readNode(new_node_x, new_node_y, neighbor);
|
||||
if (newNode != null)
|
||||
{
|
||||
neighbors.add(newNode);
|
||||
}
|
||||
}
|
||||
neighbor = pn.get(idx++); // NW
|
||||
if (neighbor > 0)
|
||||
{
|
||||
neighbor--;
|
||||
new_node_x = (short) (node_x - 1);
|
||||
new_node_y = (short) (node_y - 1);
|
||||
newNode = readNode(new_node_x, new_node_y, neighbor);
|
||||
if (newNode != null)
|
||||
{
|
||||
neighbors.add(newNode);
|
||||
}
|
||||
}
|
||||
return neighbors.toArray(new GeoNode[neighbors.size()]);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
private GeoNode readNode(short node_x, short node_y, byte layer)
|
||||
{
|
||||
final short regoffset = getRegionOffset(getRegionX(node_x), getRegionY(node_y));
|
||||
if (!pathNodesExist(regoffset))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
final short nbx = getNodeBlock(node_x);
|
||||
final short nby = getNodeBlock(node_y);
|
||||
int idx = _pathNodesIndex.get(regoffset).get((nby << 8) + nbx);
|
||||
final ByteBuffer pn = _pathNodes.get(regoffset);
|
||||
// reading
|
||||
final byte nodes = pn.get(idx);
|
||||
idx += (layer * 10) + 1; // byte + layer*10byte
|
||||
if (nodes < layer)
|
||||
{
|
||||
_log.warning("SmthWrong!");
|
||||
}
|
||||
final short node_z = pn.getShort(idx);
|
||||
idx += 2;
|
||||
return new GeoNode(new GeoNodeLoc(node_x, node_y, node_z), idx);
|
||||
}
|
||||
|
||||
private GeoNode readNode(int gx, int gy, short z)
|
||||
{
|
||||
final short node_x = getNodePos(gx);
|
||||
final short node_y = getNodePos(gy);
|
||||
final short regoffset = getRegionOffset(getRegionX(node_x), getRegionY(node_y));
|
||||
if (!pathNodesExist(regoffset))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
final short nbx = getNodeBlock(node_x);
|
||||
final short nby = getNodeBlock(node_y);
|
||||
int idx = _pathNodesIndex.get(regoffset).get((nby << 8) + nbx);
|
||||
final ByteBuffer pn = _pathNodes.get(regoffset);
|
||||
// reading
|
||||
byte nodes = pn.get(idx++);
|
||||
int idx2 = 0; // create index to nearlest node by z
|
||||
short last_z = Short.MIN_VALUE;
|
||||
while (nodes > 0)
|
||||
{
|
||||
final short node_z = pn.getShort(idx);
|
||||
if (Math.abs(last_z - z) > Math.abs(node_z - z))
|
||||
{
|
||||
last_z = node_z;
|
||||
idx2 = idx + 2;
|
||||
}
|
||||
idx += 10; // short + 8 byte
|
||||
nodes--;
|
||||
}
|
||||
return new GeoNode(new GeoNodeLoc(node_x, node_y, last_z), idx2);
|
||||
}
|
||||
|
||||
protected GeoPathFinding()
|
||||
{
|
||||
try
|
||||
{
|
||||
_log.info("Path Engine: - Loading Path Nodes...");
|
||||
//@formatter:off
|
||||
Files.lines(Config.PATHNODE_PATH.resolve("pn_index.txt"), StandardCharsets.UTF_8)
|
||||
.map(String::trim)
|
||||
.filter(l -> !l.isEmpty())
|
||||
.forEach(line -> {
|
||||
final String[] parts = line.split("_");
|
||||
|
||||
if ((parts.length < 2)
|
||||
|| !Util.isDigit(parts[0])
|
||||
|| !Util.isDigit(parts[1]))
|
||||
{
|
||||
_log.warning("Invalid pathnode entry: '" + line + "', must be in format 'XX_YY', where X and Y - integers");
|
||||
return;
|
||||
}
|
||||
|
||||
LoadPathNodeFile(Byte.parseByte(parts[0]), Byte.parseByte(parts[1]));
|
||||
});
|
||||
//@formatter:on
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
_log.log(Level.WARNING, "", e);
|
||||
throw new Error("Failed to read pn_index file.");
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadPathNodeFile(byte rx, byte ry)
|
||||
{
|
||||
if ((rx < L2World.TILE_X_MIN) || (rx > L2World.TILE_X_MAX) || (ry < L2World.TILE_Y_MIN) || (ry > L2World.TILE_Y_MAX))
|
||||
{
|
||||
_log.warning("Failed to Load PathNode File: invalid region " + rx + "," + ry + Config.EOL);
|
||||
return;
|
||||
}
|
||||
final short regionoffset = getRegionOffset(rx, ry);
|
||||
final File file = new File(Config.PATHNODE_PATH.toString(), rx + "_" + ry + ".pn");
|
||||
_log.info("Path Engine: - Loading: " + file.getName() + " -> region offset: " + regionoffset + " X: " + rx + " Y: " + ry);
|
||||
int node = 0, size, index = 0;
|
||||
|
||||
// Create a read-only memory-mapped file
|
||||
try (RandomAccessFile raf = new RandomAccessFile(file, "r");
|
||||
FileChannel roChannel = raf.getChannel())
|
||||
{
|
||||
size = (int) roChannel.size();
|
||||
MappedByteBuffer nodes;
|
||||
if (Config.FORCE_GEODATA)
|
||||
{
|
||||
// it is not guarantee, because the underlying operating system may have paged out some of the buffer's data
|
||||
nodes = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
|
||||
}
|
||||
else
|
||||
{
|
||||
nodes = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, size);
|
||||
}
|
||||
|
||||
// Indexing pathnode files, so we will know where each block starts
|
||||
final IntBuffer indexs = IntBuffer.allocate(65536);
|
||||
|
||||
while (node < 65536)
|
||||
{
|
||||
final byte layer = nodes.get(index);
|
||||
indexs.put(node++, index);
|
||||
index += (layer * 10) + 1;
|
||||
}
|
||||
_pathNodesIndex.put(regionoffset, indexs);
|
||||
_pathNodes.put(regionoffset, nodes);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, "Failed to Load PathNode File: " + file.getAbsolutePath() + " : " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private static class SingletonHolder
|
||||
{
|
||||
protected static final GeoPathFinding _instance = new GeoPathFinding();
|
||||
}
|
||||
}
|
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.pathfinding.utils;
|
||||
|
||||
import com.l2jmobius.gameserver.geodata.pathfinding.geonodes.GeoNode;
|
||||
|
||||
/**
|
||||
* @author -Nemesiss-
|
||||
*/
|
||||
public class BinaryNodeHeap
|
||||
{
|
||||
private final GeoNode[] _list;
|
||||
private int _size;
|
||||
|
||||
public BinaryNodeHeap(int size)
|
||||
{
|
||||
_list = new GeoNode[size + 1];
|
||||
_size = 0;
|
||||
}
|
||||
|
||||
public void add(GeoNode n)
|
||||
{
|
||||
_size++;
|
||||
int pos = _size;
|
||||
_list[pos] = n;
|
||||
while (pos != 1)
|
||||
{
|
||||
final int p2 = pos / 2;
|
||||
if (_list[pos].getCost() <= _list[p2].getCost())
|
||||
{
|
||||
final GeoNode temp = _list[p2];
|
||||
_list[p2] = _list[pos];
|
||||
_list[pos] = temp;
|
||||
pos = p2;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public GeoNode removeFirst()
|
||||
{
|
||||
final GeoNode first = _list[1];
|
||||
_list[1] = _list[_size];
|
||||
_list[_size] = null;
|
||||
_size--;
|
||||
int pos = 1;
|
||||
int cpos;
|
||||
int dblcpos;
|
||||
GeoNode temp;
|
||||
while (true)
|
||||
{
|
||||
cpos = pos;
|
||||
dblcpos = cpos * 2;
|
||||
if ((dblcpos + 1) <= _size)
|
||||
{
|
||||
if (_list[cpos].getCost() >= _list[dblcpos].getCost())
|
||||
{
|
||||
pos = dblcpos;
|
||||
}
|
||||
if (_list[pos].getCost() >= _list[dblcpos + 1].getCost())
|
||||
{
|
||||
pos = dblcpos + 1;
|
||||
}
|
||||
}
|
||||
else if (dblcpos <= _size)
|
||||
{
|
||||
if (_list[cpos].getCost() >= _list[dblcpos].getCost())
|
||||
{
|
||||
pos = dblcpos;
|
||||
}
|
||||
}
|
||||
|
||||
if (cpos != pos)
|
||||
{
|
||||
temp = _list[cpos];
|
||||
_list[cpos] = _list[pos];
|
||||
_list[pos] = temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
public boolean contains(GeoNode n)
|
||||
{
|
||||
if (_size == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
for (int i = 1; i <= _size; i++)
|
||||
{
|
||||
if (_list[i].equals(n))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return _size == 0;
|
||||
}
|
||||
}
|
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.geodata.pathfinding.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.l2jmobius.gameserver.geodata.pathfinding.AbstractNode;
|
||||
|
||||
/**
|
||||
* @author -Nemesiss-
|
||||
*/
|
||||
public class FastNodeList
|
||||
{
|
||||
private final ArrayList<AbstractNode<?>> _list;
|
||||
|
||||
public FastNodeList(int size)
|
||||
{
|
||||
_list = new ArrayList<>(size);
|
||||
}
|
||||
|
||||
public void add(AbstractNode<?> n)
|
||||
{
|
||||
_list.add(n);
|
||||
}
|
||||
|
||||
public boolean contains(AbstractNode<?> n)
|
||||
{
|
||||
return _list.contains(n);
|
||||
}
|
||||
|
||||
public boolean containsRev(AbstractNode<?> n)
|
||||
{
|
||||
return _list.lastIndexOf(n) != -1;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user