123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814 |
- package com.l2jserver.gameserver.model;
- import java.lang.reflect.Constructor;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javolution.util.FastList;
- import com.l2jserver.Config;
- import com.l2jserver.gameserver.GeoData;
- import com.l2jserver.gameserver.ThreadPoolManager;
- import com.l2jserver.gameserver.datatables.NpcPersonalAIData;
- import com.l2jserver.gameserver.datatables.TerritoryTable;
- import com.l2jserver.gameserver.idfactory.IdFactory;
- import com.l2jserver.gameserver.model.actor.L2Attackable;
- import com.l2jserver.gameserver.model.actor.L2Npc;
- import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
- import com.l2jserver.gameserver.model.interfaces.IIdentifiable;
- import com.l2jserver.gameserver.model.interfaces.ILocational;
- import com.l2jserver.gameserver.model.interfaces.INamable;
- import com.l2jserver.gameserver.model.interfaces.IPositionable;
- import com.l2jserver.gameserver.model.zone.type.NpcSpawnTerritory;
- import com.l2jserver.util.Rnd;
- public class L2Spawn implements IPositionable, IIdentifiable, INamable
- {
- protected static final Logger _log = Logger.getLogger(L2Spawn.class.getName());
-
-
- private String _name;
-
- private L2NpcTemplate _template;
-
- private int _maximumCount;
-
- private int _currentCount;
-
- protected int _scheduledCount;
-
- private int _locationId;
-
- private Location _location = new Location(0, 0, 0, 0, 0);
-
- private NpcSpawnTerritory _spawnTerritory = null;
-
- private int _respawnMinDelay;
-
- private int _respawnMaxDelay;
-
- private Constructor<? extends L2Npc> _constructor;
-
- private boolean _doRespawn;
-
- private boolean _customSpawn;
- private static List<SpawnListener> _spawnListeners = new FastList<>();
- private final FastList<L2Npc> _spawnedNpcs = new FastList<>();
- private Map<Integer, Location> _lastSpawnPoints;
- private boolean _isNoRndWalk = false;
-
-
- class SpawnTask implements Runnable
- {
- private final L2Npc _oldNpc;
-
- public SpawnTask(L2Npc pOldNpc)
- {
- _oldNpc = pOldNpc;
- }
-
- @Override
- public void run()
- {
- try
- {
-
- respawnNpc(_oldNpc);
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "", e);
- }
-
- _scheduledCount--;
- }
- }
-
-
- public L2Spawn(L2NpcTemplate template) throws SecurityException, ClassNotFoundException, NoSuchMethodException, ClassCastException
- {
-
- _template = template;
-
- if (_template == null)
- {
- return;
- }
-
- String className = "com.l2jserver.gameserver.model.actor.instance." + _template.getType() + "Instance";
-
-
- _constructor = Class.forName(className).asSubclass(L2Npc.class).getConstructor(int.class, L2NpcTemplate.class);
- }
-
-
- public int getAmount()
- {
- return _maximumCount;
- }
-
-
- @Override
- public String getName()
- {
- return _name;
- }
-
-
- public void setName(String name)
- {
- _name = name;
- }
-
-
- public int getLocationId()
- {
- return _locationId;
- }
-
- @Override
- public Location getLocation()
- {
- return _location;
- }
-
- public Location getLocation(L2Object obj)
- {
- return ((_lastSpawnPoints == null) || (obj == null) || !_lastSpawnPoints.containsKey(obj.getObjectId())) ? _location : _lastSpawnPoints.get(obj.getObjectId());
- }
-
- @Override
- public int getX()
- {
- return _location.getX();
- }
-
-
- public int getX(L2Object obj)
- {
- return getLocation(obj).getX();
- }
-
-
- @Override
- public void setX(int x)
- {
- _location.setX(x);
- }
-
- @Override
- public int getY()
- {
- return _location.getY();
- }
-
-
- public int getY(L2Object obj)
- {
- return getLocation(obj).getY();
- }
-
-
- @Override
- public void setY(int y)
- {
- _location.setY(y);
- }
-
- @Override
- public int getZ()
- {
- return _location.getZ();
- }
-
-
- public int getZ(L2Object obj)
- {
- return getLocation(obj).getZ();
- }
-
-
- @Override
- public void setZ(int z)
- {
- _location.setZ(z);
- }
-
-
- @Override
- public void setXYZ(int x, int y, int z)
- {
- setX(x);
- setY(y);
- setZ(z);
- }
-
-
- @Override
- public void setXYZ(ILocational loc)
- {
- setXYZ(loc.getX(), loc.getY(), loc.getZ());
-
- }
-
-
- @Override
- public int getHeading()
- {
- return _location.getHeading();
- }
-
-
- @Override
- public void setHeading(int heading)
- {
- _location.setHeading(heading);
- }
-
-
- @Override
- public void setLocation(Location loc)
- {
- _location = loc;
- }
-
-
- @Override
- public int getId()
- {
- return _template.getId();
- }
-
-
- public int getRespawnMinDelay()
- {
- return _respawnMinDelay;
- }
-
-
- public int getRespawnMaxDelay()
- {
- return _respawnMaxDelay;
- }
-
-
- public void setAmount(int amount)
- {
- _maximumCount = amount;
- }
-
-
- public void setLocationId(int id)
- {
- _locationId = id;
- }
-
-
- public void setRespawnMinDelay(int date)
- {
- _respawnMinDelay = date;
- }
-
-
- public void setRespawnMaxDelay(int date)
- {
- _respawnMaxDelay = date;
- }
-
-
- public void setCustom(boolean custom)
- {
- _customSpawn = custom;
- }
-
-
- public boolean isCustom()
- {
- return _customSpawn;
- }
-
-
- public void decreaseCount(L2Npc oldNpc)
- {
-
- if (_currentCount <= 0)
- {
- return;
- }
-
-
- _currentCount--;
-
-
- _spawnedNpcs.remove(oldNpc);
-
-
- if (_lastSpawnPoints != null)
- {
- _lastSpawnPoints.remove(oldNpc.getObjectId());
- }
-
-
- if (_doRespawn && ((_scheduledCount + _currentCount) < _maximumCount))
- {
-
- _scheduledCount++;
-
-
-
- ThreadPoolManager.getInstance().scheduleGeneral(new SpawnTask(oldNpc), hasRespawnRandom() ? Rnd.get(_respawnMinDelay, _respawnMaxDelay) : _respawnMinDelay);
- }
- }
-
-
- public int init()
- {
- while (_currentCount < _maximumCount)
- {
- doSpawn();
- }
- _doRespawn = _respawnMinDelay != 0;
-
- return _currentCount;
- }
-
-
- public L2Npc spawnOne(boolean val)
- {
- return doSpawn(val);
- }
-
-
- public boolean isRespawnEnabled()
- {
- return _doRespawn;
- }
-
-
- public void stopRespawn()
- {
- _doRespawn = false;
- }
-
-
- public void startRespawn()
- {
- _doRespawn = true;
- }
-
- public L2Npc doSpawn()
- {
- return doSpawn(false);
- }
-
-
- public L2Npc doSpawn(boolean isSummonSpawn)
- {
- try
- {
-
- if (_template.isType("L2Pet") || _template.isType("L2Decoy") || _template.isType("L2Trap"))
- {
- _currentCount++;
-
- return null;
- }
-
-
- L2Npc npc = _constructor.newInstance(IdFactory.getInstance().getNextId(), _template);
- npc.setInstanceId(getInstanceId());
- if (isSummonSpawn)
- {
- npc.setShowSummonAnimation(isSummonSpawn);
- }
-
-
- if (_name != null)
- {
- NpcPersonalAIData.getInstance().initializeNpcParameters(npc, this, _name);
- }
-
- return initializeNpcInstance(npc);
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "NPC " + _template.getId() + " class not found", e);
- }
- return null;
- }
-
-
- private L2Npc initializeNpcInstance(L2Npc mob)
- {
- int newlocx = 0;
- int newlocy = 0;
- int newlocz = 0;
-
-
-
- if (isTerritoryBased())
- {
- int[] p = _spawnTerritory.getRandomPoint();
- newlocx = p[0];
- newlocy = p[1];
- newlocz = p[2];
- }
-
- else if ((getX() == 0) && (getY() == 0))
- {
- if (getLocationId() == 0)
- {
- return mob;
- }
-
-
- final Location location = TerritoryTable.getInstance().getRandomPoint(getLocationId());
-
-
- if (location != null)
- {
- newlocx = location.getX();
- newlocy = location.getY();
- newlocz = location.getZ();
- }
- }
- else
- {
-
- newlocx = getX();
- newlocy = getY();
- newlocz = getZ();
- }
-
-
- if (!mob.isFlying())
- {
- newlocz = GeoData.getInstance().getSpawnHeight(newlocx, newlocy, newlocz);
- }
-
- mob.stopAllEffects();
-
- mob.setIsDead(false);
-
- mob.setDecayed(false);
-
- mob.setCurrentHpMp(mob.getMaxHp(), mob.getMaxMp());
-
- if (mob.hasVariables())
- {
- mob.getVariables().getSet().clear();
- }
-
- mob.setIsNoRndWalk(isNoRndWalk());
-
-
- if (getHeading() == -1)
- {
- mob.setHeading(Rnd.nextInt(61794));
- }
- else
- {
- mob.setHeading(getHeading());
- }
-
- if (mob instanceof L2Attackable)
- {
- ((L2Attackable) mob).setChampion(false);
- }
-
- if (Config.L2JMOD_CHAMPION_ENABLE)
- {
-
- if (mob.isMonster() && !getTemplate().isUndying() && !mob.isRaid() && !mob.isRaidMinion() && (Config.L2JMOD_CHAMPION_FREQUENCY > 0) && (mob.getLevel() >= Config.L2JMOD_CHAMP_MIN_LVL) && (mob.getLevel() <= Config.L2JMOD_CHAMP_MAX_LVL) && (Config.L2JMOD_CHAMPION_ENABLE_IN_INSTANCES || (getInstanceId() == 0)))
- {
- if (Rnd.get(100) < Config.L2JMOD_CHAMPION_FREQUENCY)
- {
- ((L2Attackable) mob).setChampion(true);
- }
- }
- }
-
-
- mob.setSpawn(this);
-
-
- mob.spawnMe(newlocx, newlocy, newlocz);
-
- L2Spawn.notifyNpcSpawned(mob);
-
- _spawnedNpcs.add(mob);
- if (_lastSpawnPoints != null)
- {
- _lastSpawnPoints.put(mob.getObjectId(), new Location(newlocx, newlocy, newlocz));
- }
-
- if (Config.DEBUG)
- {
- _log.finest("Spawned Mob Id: " + _template.getId() + " , at: X: " + mob.getX() + " Y: " + mob.getY() + " Z: " + mob.getZ());
- }
-
- _currentCount++;
- return mob;
- }
-
- public static void addSpawnListener(SpawnListener listener)
- {
- synchronized (_spawnListeners)
- {
- _spawnListeners.add(listener);
- }
- }
-
- public static void removeSpawnListener(SpawnListener listener)
- {
- synchronized (_spawnListeners)
- {
- _spawnListeners.remove(listener);
- }
- }
-
- public static void notifyNpcSpawned(L2Npc npc)
- {
- synchronized (_spawnListeners)
- {
- for (SpawnListener listener : _spawnListeners)
- {
- listener.npcSpawned(npc);
- }
- }
- }
-
-
- public void setRespawnDelay(int delay, int randomInterval)
- {
- if (delay != 0)
- {
- if (delay < 0)
- {
- _log.warning("respawn delay is negative for spawn:" + this);
- }
-
- int minDelay = delay - randomInterval;
- int maxDelay = delay + randomInterval;
-
- _respawnMinDelay = Math.max(10, minDelay) * 1000;
- _respawnMaxDelay = Math.max(10, maxDelay) * 1000;
- }
-
- else
- {
- _respawnMinDelay = 0;
- _respawnMaxDelay = 0;
- }
- }
-
- public void setRespawnDelay(int delay)
- {
- setRespawnDelay(delay, 0);
- }
-
- public int getRespawnDelay()
- {
- return (_respawnMinDelay + _respawnMaxDelay) / 2;
- }
-
- public boolean hasRespawnRandom()
- {
- return _respawnMinDelay != _respawnMaxDelay;
- }
-
- public void setSpawnTerritory(NpcSpawnTerritory territory)
- {
- _spawnTerritory = territory;
- _lastSpawnPoints = new ConcurrentHashMap<>();
- }
-
- public NpcSpawnTerritory getSpawnTerritory()
- {
- return _spawnTerritory;
- }
-
- public boolean isTerritoryBased()
- {
- return (_spawnTerritory != null) && (_location.getX() == 0) && (_location.getY() == 0);
- }
-
- public L2Npc getLastSpawn()
- {
- if (!_spawnedNpcs.isEmpty())
- {
- return _spawnedNpcs.getLast();
- }
-
- return null;
- }
-
- public final FastList<L2Npc> getSpawnedNpcs()
- {
- return _spawnedNpcs;
- }
-
-
- public void respawnNpc(L2Npc oldNpc)
- {
- if (_doRespawn)
- {
- oldNpc.refreshID();
- initializeNpcInstance(oldNpc);
- }
- }
-
- public L2NpcTemplate getTemplate()
- {
- return _template;
- }
-
- @Override
- public int getInstanceId()
- {
- return _location.getInstanceId();
- }
-
- @Override
- public void setInstanceId(int instanceId)
- {
- _location.setInstanceId(instanceId);
- }
-
- @Override
- public String toString()
- {
- return "L2Spawn ID: " + getId() + " " + getLocation();
- }
-
- public final boolean isNoRndWalk()
- {
- return _isNoRndWalk;
- }
-
- public final void setIsNoRndWalk(boolean value)
- {
- _isNoRndWalk = value;
- }
- }
|