Reuse dead NPC objects.

This commit is contained in:
MobiusDevelopment
2019-06-09 03:14:29 +00:00
parent 6514604c4a
commit ea06ddbade
37 changed files with 500 additions and 210 deletions

View File

@ -30,6 +30,7 @@ import org.l2jmobius.gameserver.geoengine.GeoEngine;
import org.l2jmobius.gameserver.model.actor.Npc;
import org.l2jmobius.gameserver.model.actor.instance.MonsterInstance;
import org.l2jmobius.gameserver.model.actor.templates.NpcTemplate;
import org.l2jmobius.gameserver.model.instancezone.Instance;
import org.l2jmobius.gameserver.model.interfaces.IIdentifiable;
import org.l2jmobius.gameserver.model.interfaces.INamable;
import org.l2jmobius.gameserver.model.spawns.NpcSpawnTemplate;
@ -254,7 +255,7 @@ public class Spawn extends Location implements IIdentifiable, INamable
_scheduledCount++;
// Schedule the next respawn.
RespawnTaskManager.getInstance().add(this, System.currentTimeMillis() + (hasRespawnRandom() ? Rnd.get(_respawnMinDelay, _respawnMaxDelay) : _respawnMinDelay));
RespawnTaskManager.getInstance().add(oldNpc, System.currentTimeMillis() + (hasRespawnRandom() ? Rnd.get(_respawnMinDelay, _respawnMaxDelay) : _respawnMinDelay));
}
}
@ -468,7 +469,6 @@ public class Spawn extends Location implements IIdentifiable, INamable
_respawnMinDelay = Math.max(10, minDelay) * 1000;
_respawnMaxDelay = Math.max(10, maxDelay) * 1000;
}
else
{
_respawnMinDelay = 0;
@ -511,6 +511,22 @@ public class Spawn extends Location implements IIdentifiable, INamable
return _spawnedNpcs;
}
public void respawnNpc(Npc oldNpc)
{
if (_doRespawn)
{
oldNpc.refreshID();
initializeNpcInstance(oldNpc);
// Register NPC back to instance world.
final Instance instance = oldNpc.getInstanceWorld();
if (instance != null)
{
instance.addNpc(oldNpc);
}
}
}
public NpcTemplate getTemplate()
{
return _template;

View File

@ -24,22 +24,23 @@ import java.util.concurrent.CopyOnWriteArrayList;
import org.l2jmobius.commons.concurrent.ThreadPool;
import org.l2jmobius.gameserver.model.Spawn;
import org.l2jmobius.gameserver.model.actor.Npc;
/**
* @author Mobius
*/
public class RespawnTaskManager
{
private static final Map<Spawn, List<Long>> PENDING_RESPAWNS = new ConcurrentHashMap<>();
private static final Map<Npc, List<Long>> PENDING_RESPAWNS = new ConcurrentHashMap<>();
public RespawnTaskManager()
{
ThreadPool.scheduleAtFixedRate(() ->
{
final long time = System.currentTimeMillis();
for (Entry<Spawn, List<Long>> entry : PENDING_RESPAWNS.entrySet())
for (Entry<Npc, List<Long>> entry : PENDING_RESPAWNS.entrySet())
{
final Spawn spawn = entry.getKey();
final Npc npc = entry.getKey();
final List<Long> schedules = entry.getValue();
for (Long respawnTime : schedules)
{
@ -48,23 +49,27 @@ public class RespawnTaskManager
schedules.remove(respawnTime);
if (schedules.isEmpty())
{
PENDING_RESPAWNS.remove(spawn);
PENDING_RESPAWNS.remove(npc);
}
final Spawn spawn = npc.getSpawn();
if (spawn != null)
{
spawn.respawnNpc(npc);
spawn._scheduledCount--;
}
spawn.doSpawn();
spawn._scheduledCount--;
}
}
}
}, 0, 1000);
}
public void add(Spawn spawn, Long time)
public void add(Npc npc, Long time)
{
if (!PENDING_RESPAWNS.containsKey(spawn))
if (!PENDING_RESPAWNS.containsKey(npc))
{
PENDING_RESPAWNS.put(spawn, new CopyOnWriteArrayList<>());
PENDING_RESPAWNS.put(npc, new CopyOnWriteArrayList<>());
}
PENDING_RESPAWNS.get(spawn).add(time);
PENDING_RESPAWNS.get(npc).add(time);
}
public static RespawnTaskManager getInstance()