Addition of fences support.
Contributed by Sahar.
This commit is contained in:
@ -54,6 +54,7 @@ import com.l2jmobius.gameserver.data.xml.impl.EnchantItemOptionsData;
|
||||
import com.l2jmobius.gameserver.data.xml.impl.EnchantSkillGroupsData;
|
||||
import com.l2jmobius.gameserver.data.xml.impl.ExperienceData;
|
||||
import com.l2jmobius.gameserver.data.xml.impl.FakePlayerData;
|
||||
import com.l2jmobius.gameserver.data.xml.impl.FenceData;
|
||||
import com.l2jmobius.gameserver.data.xml.impl.FishData;
|
||||
import com.l2jmobius.gameserver.data.xml.impl.FishingMonstersData;
|
||||
import com.l2jmobius.gameserver.data.xml.impl.FishingRodsData;
|
||||
@ -292,6 +293,7 @@ public class GameServer
|
||||
|
||||
printSection("NPCs");
|
||||
DoorData.getInstance();
|
||||
FenceData.getInstance();
|
||||
SkillLearnData.getInstance();
|
||||
NpcData.getInstance();
|
||||
FakePlayerData.getInstance();
|
||||
|
@ -0,0 +1,237 @@
|
||||
/*
|
||||
* 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.data.xml.impl;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import com.l2jmobius.commons.util.IGameXmlReader;
|
||||
import com.l2jmobius.gameserver.enums.FenceState;
|
||||
import com.l2jmobius.gameserver.model.L2World;
|
||||
import com.l2jmobius.gameserver.model.L2WorldRegion;
|
||||
import com.l2jmobius.gameserver.model.StatsSet;
|
||||
import com.l2jmobius.gameserver.model.actor.instance.L2FenceInstance;
|
||||
|
||||
/**
|
||||
* @author HoridoJoho / FBIagent
|
||||
*/
|
||||
public final class FenceData implements IGameXmlReader
|
||||
{
|
||||
private static final Logger LOGGER = Logger.getLogger(FenceData.class.getSimpleName());
|
||||
|
||||
private static final int MAX_Z_DIFF = 100;
|
||||
|
||||
private final Map<L2WorldRegion, List<L2FenceInstance>> _regions = new ConcurrentHashMap<>();
|
||||
private final Map<Integer, L2FenceInstance> _fences = new ConcurrentHashMap<>();
|
||||
|
||||
protected FenceData()
|
||||
{
|
||||
load();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load()
|
||||
{
|
||||
if (!_fences.isEmpty())
|
||||
{
|
||||
// Remove old fences when reloading
|
||||
_fences.values().forEach(this::removeFence);
|
||||
}
|
||||
|
||||
parseDatapackFile("data/FenceData.xml");
|
||||
LOGGER.info("Loaded " + _fences.size() + " Fences.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parseDocument(Document doc, File f)
|
||||
{
|
||||
forEach(doc, "list", listNode -> forEach(listNode, "fence", this::spawnFence));
|
||||
}
|
||||
|
||||
public int getLoadedElementsCount()
|
||||
{
|
||||
return _fences.size();
|
||||
}
|
||||
|
||||
private void spawnFence(Node fenceNode)
|
||||
{
|
||||
final StatsSet set = new StatsSet(parseAttributes(fenceNode));
|
||||
spawnFence(set.getInt("x"), set.getInt("y"), set.getInt("z"), set.getString("name"), set.getInt("width"), set.getInt("length"), set.getInt("height"), 0, set.getEnum("state", FenceState.class, FenceState.CLOSED));
|
||||
}
|
||||
|
||||
public L2FenceInstance spawnFence(int x, int y, int z, int width, int length, int height, int instanceId, FenceState state)
|
||||
{
|
||||
return spawnFence(x, y, z, null, width, length, height, instanceId, state);
|
||||
}
|
||||
|
||||
public L2FenceInstance spawnFence(int x, int y, int z, String name, int width, int length, int height, int instanceId, FenceState state)
|
||||
{
|
||||
final L2FenceInstance fence = new L2FenceInstance(x, y, name, width, length, height, state);
|
||||
if (instanceId > 0)
|
||||
{
|
||||
fence.setInstanceId(instanceId);
|
||||
}
|
||||
fence.spawnMe(x, y, z);
|
||||
addFence(fence);
|
||||
|
||||
return fence;
|
||||
}
|
||||
|
||||
private void addFence(L2FenceInstance fence)
|
||||
{
|
||||
_fences.put(fence.getObjectId(), fence);
|
||||
_regions.computeIfAbsent(L2World.getInstance().getRegion(fence), key -> new ArrayList<>()).add(fence);
|
||||
}
|
||||
|
||||
public void removeFence(L2FenceInstance fence)
|
||||
{
|
||||
_fences.remove(fence.getObjectId());
|
||||
|
||||
final List<L2FenceInstance> fencesInRegion = _regions.get(L2World.getInstance().getRegion(fence));
|
||||
if (fencesInRegion != null)
|
||||
{
|
||||
fencesInRegion.remove(fence);
|
||||
}
|
||||
}
|
||||
|
||||
public Map<Integer, L2FenceInstance> getFences()
|
||||
{
|
||||
return _fences;
|
||||
}
|
||||
|
||||
public L2FenceInstance getFence(int objectId)
|
||||
{
|
||||
return _fences.get(objectId);
|
||||
}
|
||||
|
||||
public boolean checkIfFenceBetween(int x, int y, int z, int tx, int ty, int tz, int instanceId)
|
||||
{
|
||||
final Predicate<L2FenceInstance> filter = fence ->
|
||||
{
|
||||
// Check if fence is geodata enabled.
|
||||
if (!fence.getState().isGeodataEnabled())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if fence is within the instance we search for.
|
||||
if (fence.getInstanceId() != instanceId)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
final int xMin = fence.getXMin();
|
||||
final int xMax = fence.getXMax();
|
||||
final int yMin = fence.getYMin();
|
||||
final int yMax = fence.getYMax();
|
||||
if ((x < xMin) && (tx < xMin))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if ((x > xMax) && (tx > xMax))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if ((y < yMin) && (ty < yMin))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if ((y > yMax) && (ty > yMax))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if ((x > xMin) && (tx > xMin) && (x < xMax) && (tx < xMax))
|
||||
{
|
||||
if ((y > yMin) && (ty > yMin) && (y < yMax) && (ty < yMax))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (crossLinePart(xMin, yMin, xMax, yMin, x, y, tx, ty, xMin, yMin, xMax, yMax) || crossLinePart(xMax, yMin, xMax, yMax, x, y, tx, ty, xMin, yMin, xMax, yMax) || crossLinePart(xMax, yMax, xMin, yMax, x, y, tx, ty, xMin, yMin, xMax, yMax) || crossLinePart(xMin, yMax, xMin, yMin, x, y, tx, ty, xMin, yMin, xMax, yMax))
|
||||
{
|
||||
if ((z > (fence.getZ() - MAX_Z_DIFF)) && (z < (fence.getZ() + MAX_Z_DIFF)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
return _regions.getOrDefault(L2World.getInstance().getRegion(x, y, z), Collections.emptyList()).stream().anyMatch(filter);
|
||||
}
|
||||
|
||||
private boolean crossLinePart(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double xMin, double yMin, double xMax, double yMax)
|
||||
{
|
||||
final double[] result = intersection(x1, y1, x2, y2, x3, y3, x4, y4);
|
||||
if (result == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
final double xCross = result[0];
|
||||
final double yCross = result[1];
|
||||
if ((xCross <= xMax) && (xCross >= xMin))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if ((yCross <= yMax) && (yCross >= yMin))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private double[] intersection(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
|
||||
{
|
||||
final double d = ((x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4));
|
||||
if (d == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
final double xi = (((x3 - x4) * ((x1 * y2) - (y1 * x2))) - ((x1 - x2) * ((x3 * y4) - (y3 * x4)))) / d;
|
||||
final double yi = (((y3 - y4) * ((x1 * y2) - (y1 * x2))) - ((y1 - y2) * ((x3 * y4) - (y3 * x4)))) / d;
|
||||
|
||||
return new double[]
|
||||
{
|
||||
xi,
|
||||
yi
|
||||
};
|
||||
}
|
||||
|
||||
public static FenceData getInstance()
|
||||
{
|
||||
return SingletonHolder.INSTANCE;
|
||||
}
|
||||
|
||||
private static class SingletonHolder
|
||||
{
|
||||
protected static final FenceData INSTANCE = new FenceData();
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.enums;
|
||||
|
||||
/**
|
||||
* @author Nik64
|
||||
*/
|
||||
public enum FenceState
|
||||
{
|
||||
HIDDEN(0),
|
||||
OPENED(1),
|
||||
CLOSED(2),
|
||||
CLOSED_HIDDEN(0);
|
||||
|
||||
final int _clientId;
|
||||
|
||||
private FenceState(int clientId)
|
||||
{
|
||||
_clientId = clientId;
|
||||
}
|
||||
|
||||
public int getClientId()
|
||||
{
|
||||
return _clientId;
|
||||
}
|
||||
|
||||
public boolean isGeodataEnabled()
|
||||
{
|
||||
return (this == CLOSED_HIDDEN) || (this == CLOSED);
|
||||
}
|
||||
}
|
@ -26,6 +26,7 @@ import java.util.logging.Logger;
|
||||
|
||||
import com.l2jmobius.Config;
|
||||
import com.l2jmobius.gameserver.data.xml.impl.DoorData;
|
||||
import com.l2jmobius.gameserver.data.xml.impl.FenceData;
|
||||
import com.l2jmobius.gameserver.geoengine.geodata.ABlock;
|
||||
import com.l2jmobius.gameserver.geoengine.geodata.BlockComplex;
|
||||
import com.l2jmobius.gameserver.geoengine.geodata.BlockComplexDynamic;
|
||||
@ -597,6 +598,10 @@ public class GeoEngine
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (FenceData.getInstance().checkIfFenceBetween(ox, oy, oz, tx, ty, tz, origin.getInstanceId()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// get origin and check existing geo coordinates
|
||||
final int gox = getGeoX(ox);
|
||||
@ -662,6 +667,10 @@ public class GeoEngine
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (FenceData.getInstance().checkIfFenceBetween(ox, oy, oz, tx, ty, tz, origin.getInstanceId()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// get origin and check existing geo coordinates
|
||||
final int gox = getGeoX(ox);
|
||||
@ -1126,6 +1135,10 @@ public class GeoEngine
|
||||
{
|
||||
return new Location(ox, oy, oz);
|
||||
}
|
||||
if (FenceData.getInstance().checkIfFenceBetween(ox, oy, oz, tx, ty, tz, instanceId))
|
||||
{
|
||||
return new Location(ox, oy, oz);
|
||||
}
|
||||
|
||||
// get origin and check existing geo coordinates
|
||||
final int gox = getGeoX(ox);
|
||||
@ -1186,6 +1199,10 @@ public class GeoEngine
|
||||
{
|
||||
return new GeoLocation(gox, goy, goz);
|
||||
}
|
||||
if (FenceData.getInstance().checkIfFenceBetween(gox, goy, goz, gtx, gty, gtz, instanceId))
|
||||
{
|
||||
return new GeoLocation(gox, goy, goz);
|
||||
}
|
||||
|
||||
// get X delta, signum and direction flag
|
||||
final int dx = Math.abs(gtx - gox);
|
||||
|
@ -0,0 +1,168 @@
|
||||
/*
|
||||
* 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.model.actor.instance;
|
||||
|
||||
import com.l2jmobius.gameserver.data.xml.impl.FenceData;
|
||||
import com.l2jmobius.gameserver.enums.FenceState;
|
||||
import com.l2jmobius.gameserver.idfactory.IdFactory;
|
||||
import com.l2jmobius.gameserver.model.L2Object;
|
||||
import com.l2jmobius.gameserver.model.L2World;
|
||||
import com.l2jmobius.gameserver.model.actor.L2Character;
|
||||
import com.l2jmobius.gameserver.network.serverpackets.DeleteObject;
|
||||
import com.l2jmobius.gameserver.network.serverpackets.ExColosseumFenceInfo;
|
||||
|
||||
/**
|
||||
* @author HoridoJoho / FBIagent
|
||||
*/
|
||||
public final class L2FenceInstance extends L2Object
|
||||
{
|
||||
private final int _xMin;
|
||||
private final int _xMax;
|
||||
private final int _yMin;
|
||||
private final int _yMax;
|
||||
|
||||
private final String _name;
|
||||
private final int _width;
|
||||
private final int _length;
|
||||
|
||||
private FenceState _state;
|
||||
private int[] _heightFences;
|
||||
|
||||
public L2FenceInstance(int x, int y, String name, int width, int length, int height, FenceState state)
|
||||
{
|
||||
super(IdFactory.getInstance().getNextId());
|
||||
|
||||
_xMin = x - (width / 2);
|
||||
_xMax = x + (width / 2);
|
||||
_yMin = y - (length / 2);
|
||||
_yMax = y + (length / 2);
|
||||
|
||||
_name = name;
|
||||
_width = width;
|
||||
_length = length;
|
||||
|
||||
_state = state;
|
||||
|
||||
if (height > 1)
|
||||
{
|
||||
_heightFences = new int[height - 1];
|
||||
for (int i = 0; i < _heightFences.length; i++)
|
||||
{
|
||||
_heightFences[i] = IdFactory.getInstance().getNextId();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId()
|
||||
{
|
||||
return getObjectId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName()
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAutoAttackable(L2Character attacker)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendInfo(L2PcInstance activeChar)
|
||||
{
|
||||
activeChar.sendPacket(new ExColosseumFenceInfo(this));
|
||||
|
||||
if (_heightFences != null)
|
||||
{
|
||||
for (int objId : _heightFences)
|
||||
{
|
||||
activeChar.sendPacket(new ExColosseumFenceInfo(objId, getX(), getY(), getZ(), _width, _length, _state));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean decayMe()
|
||||
{
|
||||
if (_heightFences != null)
|
||||
{
|
||||
final DeleteObject[] deleteObjects = new DeleteObject[_heightFences.length];
|
||||
for (int i = 0; i < _heightFences.length; i++)
|
||||
{
|
||||
deleteObjects[i] = new DeleteObject(_heightFences[i]);
|
||||
}
|
||||
|
||||
L2World.getInstance().forEachVisibleObject(this, L2PcInstance.class, player -> player.sendPacket(deleteObjects));
|
||||
}
|
||||
|
||||
return super.decayMe();
|
||||
}
|
||||
|
||||
public boolean deleteMe()
|
||||
{
|
||||
decayMe();
|
||||
|
||||
FenceData.getInstance().removeFence(this);
|
||||
return false;
|
||||
}
|
||||
|
||||
public FenceState getState()
|
||||
{
|
||||
return _state;
|
||||
}
|
||||
|
||||
public void setState(FenceState type)
|
||||
{
|
||||
_state = type;
|
||||
|
||||
broadcastInfo();
|
||||
}
|
||||
|
||||
public int getWidth()
|
||||
{
|
||||
return _width;
|
||||
}
|
||||
|
||||
public int getLength()
|
||||
{
|
||||
return _length;
|
||||
}
|
||||
|
||||
public int getXMin()
|
||||
{
|
||||
return _xMin;
|
||||
}
|
||||
|
||||
public int getYMin()
|
||||
{
|
||||
return _yMin;
|
||||
}
|
||||
|
||||
public int getXMax()
|
||||
{
|
||||
return _xMax;
|
||||
}
|
||||
|
||||
public int getYMax()
|
||||
{
|
||||
return _yMax;
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.network.serverpackets;
|
||||
|
||||
import com.l2jmobius.commons.network.PacketWriter;
|
||||
import com.l2jmobius.gameserver.enums.FenceState;
|
||||
import com.l2jmobius.gameserver.model.actor.instance.L2FenceInstance;
|
||||
import com.l2jmobius.gameserver.network.OutgoingPackets;
|
||||
|
||||
/**
|
||||
* @author HoridoJoho / FBIagent
|
||||
*/
|
||||
public class ExColosseumFenceInfo implements IClientOutgoingPacket
|
||||
{
|
||||
private final int _objId;
|
||||
private final int _x;
|
||||
private final int _y;
|
||||
private final int _z;
|
||||
private final int _width;
|
||||
private final int _length;
|
||||
private final int _clientState;
|
||||
|
||||
public ExColosseumFenceInfo(L2FenceInstance fence)
|
||||
{
|
||||
this(fence.getObjectId(), fence.getX(), fence.getY(), fence.getZ(), fence.getWidth(), fence.getLength(), fence.getState());
|
||||
}
|
||||
|
||||
public ExColosseumFenceInfo(int objId, double x, double y, double z, int width, int length, FenceState state)
|
||||
{
|
||||
_objId = objId;
|
||||
_x = (int) x;
|
||||
_y = (int) y;
|
||||
_z = (int) z;
|
||||
_width = width;
|
||||
_length = length;
|
||||
_clientState = state.getClientId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean write(PacketWriter packet)
|
||||
{
|
||||
OutgoingPackets.EX_COLOSSEUM_FENCE_INFO.writeId(packet);
|
||||
|
||||
packet.writeD(_objId);
|
||||
packet.writeD(_clientState);
|
||||
packet.writeD(_x);
|
||||
packet.writeD(_y);
|
||||
packet.writeD(_z);
|
||||
packet.writeD(_width);
|
||||
packet.writeD(_length);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user