Addition of RandomAnimationManager.
This commit is contained in:
@@ -255,19 +255,19 @@ LazyCache = False
|
||||
# Default: True
|
||||
CacheCharNames = True
|
||||
|
||||
# Minimum and maximum variables in seconds for npc animation delay.
|
||||
# You must keep MinNPCAnimation < = MaxNPCAnimation.
|
||||
# Default: 10
|
||||
MinNPCAnimation = 10
|
||||
# Minimum and maximum variables in seconds for NPC animation delay.
|
||||
# You must keep MinNpcAnimation < = MaxNpcAnimation.
|
||||
# Default: 5
|
||||
MinNpcAnimation = 5
|
||||
|
||||
# Default: 20
|
||||
MaxNPCAnimation = 20
|
||||
# Default: 60
|
||||
MaxNpcAnimation = 60
|
||||
|
||||
# Default: 5
|
||||
MinMonsterAnimation = 5
|
||||
|
||||
# Default: 20
|
||||
MaxMonsterAnimation = 20
|
||||
# Default: 60
|
||||
MaxMonsterAnimation = 60
|
||||
|
||||
# Grid options: Grids can turn themselves on and off. This also affects the loading and processing of all AI tasks and (in the future) geodata within this grid.
|
||||
# Turn on for a grid with a person in it is immediate, but it then turns on the 8 neighboring grids based on the specified number of seconds.
|
||||
|
@@ -1789,10 +1789,10 @@ public final class Config
|
||||
FORCE_INVENTORY_UPDATE = General.getBoolean("ForceInventoryUpdate", false);
|
||||
LAZY_CACHE = General.getBoolean("LazyCache", true);
|
||||
CACHE_CHAR_NAMES = General.getBoolean("CacheCharNames", true);
|
||||
MIN_NPC_ANIMATION = General.getInt("MinNPCAnimation", 10);
|
||||
MAX_NPC_ANIMATION = General.getInt("MaxNPCAnimation", 20);
|
||||
MIN_NPC_ANIMATION = General.getInt("MinNpcAnimation", 5);
|
||||
MAX_NPC_ANIMATION = General.getInt("MaxNpcAnimation", 60);
|
||||
MIN_MONSTER_ANIMATION = General.getInt("MinMonsterAnimation", 5);
|
||||
MAX_MONSTER_ANIMATION = General.getInt("MaxMonsterAnimation", 20);
|
||||
MAX_MONSTER_ANIMATION = General.getInt("MaxMonsterAnimation", 60);
|
||||
GRIDS_ALWAYS_ON = General.getBoolean("GridsAlwaysOn", false);
|
||||
GRID_NEIGHBOR_TURNON_TIME = General.getInt("GridNeighborTurnOnTime", 1);
|
||||
GRID_NEIGHBOR_TURNOFF_TIME = General.getInt("GridNeighborTurnOffTime", 90);
|
||||
|
@@ -188,13 +188,6 @@ public class CreatureAI extends AbstractAI
|
||||
// Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
|
||||
clientStopAutoAttack();
|
||||
|
||||
// Also enable random animations for this Creature if allowed
|
||||
// This is only for mobs - town npcs are handled in their constructor
|
||||
if (_actor.isAttackable())
|
||||
{
|
||||
((Npc) _actor).startRandomAnimationTask();
|
||||
}
|
||||
|
||||
// Launch the Think Event
|
||||
onEvtThink();
|
||||
}
|
||||
|
@@ -20,18 +20,15 @@ import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.l2jmobius.Config;
|
||||
import org.l2jmobius.commons.concurrent.ThreadPool;
|
||||
import org.l2jmobius.gameserver.model.actor.Attackable;
|
||||
import org.l2jmobius.gameserver.model.actor.Npc;
|
||||
import org.l2jmobius.gameserver.model.actor.Vehicle;
|
||||
import org.l2jmobius.gameserver.taskmanager.RandomAnimationManager;
|
||||
|
||||
public final class WorldRegion
|
||||
{
|
||||
private static final Logger LOGGER = Logger.getLogger(WorldRegion.class.getName());
|
||||
|
||||
/** Map containing visible objects in this world region. */
|
||||
private volatile Map<Integer, WorldObject> _visibleObjects = new ConcurrentHashMap<>();
|
||||
/** Map containing nearby regions forming this world region's effective area. */
|
||||
@@ -81,14 +78,12 @@ public final class WorldRegion
|
||||
return;
|
||||
}
|
||||
|
||||
int c = 0;
|
||||
if (!isOn)
|
||||
{
|
||||
for (WorldObject o : _visibleObjects.values())
|
||||
{
|
||||
if (o.isAttackable())
|
||||
{
|
||||
c++;
|
||||
final Attackable mob = (Attackable) o;
|
||||
|
||||
// Set target to null and cancel attack or cast.
|
||||
@@ -109,13 +104,14 @@ public final class WorldRegion
|
||||
mob.getAI().setIntention(org.l2jmobius.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE);
|
||||
mob.getAI().stopAITask();
|
||||
}
|
||||
|
||||
RandomAnimationManager.getInstance().remove(mob);
|
||||
}
|
||||
else if (o instanceof Vehicle)
|
||||
else if (o instanceof Npc)
|
||||
{
|
||||
c++;
|
||||
RandomAnimationManager.getInstance().remove((Npc) o);
|
||||
}
|
||||
}
|
||||
LOGGER.finer(c + " mobs were turned off");
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -123,16 +119,15 @@ public final class WorldRegion
|
||||
{
|
||||
if (o.isAttackable())
|
||||
{
|
||||
c++;
|
||||
// Start HP/MP/CP regeneration task.
|
||||
((Attackable) o).getStatus().startHpMpRegeneration();
|
||||
RandomAnimationManager.getInstance().add((Npc) o);
|
||||
}
|
||||
else if (o instanceof Npc)
|
||||
{
|
||||
((Npc) o).startRandomAnimationTask();
|
||||
RandomAnimationManager.getInstance().add((Npc) o);
|
||||
}
|
||||
}
|
||||
LOGGER.finer(c + " mobs were turned on");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,8 +156,6 @@ public final class WorldRegion
|
||||
|
||||
// Turn the AI on or off to match the region's activation.
|
||||
switchAI(value);
|
||||
|
||||
LOGGER.finer((value ? "Starting" : "Stopping") + " Grid " + this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -59,7 +59,6 @@ import org.l2jmobius.gameserver.model.actor.instance.TeleporterInstance;
|
||||
import org.l2jmobius.gameserver.model.actor.instance.WarehouseInstance;
|
||||
import org.l2jmobius.gameserver.model.actor.stat.NpcStat;
|
||||
import org.l2jmobius.gameserver.model.actor.status.NpcStatus;
|
||||
import org.l2jmobius.gameserver.model.actor.tasks.npc.RandomAnimationTask;
|
||||
import org.l2jmobius.gameserver.model.actor.templates.NpcTemplate;
|
||||
import org.l2jmobius.gameserver.model.entity.Castle;
|
||||
import org.l2jmobius.gameserver.model.entity.ClanHall;
|
||||
@@ -132,7 +131,6 @@ public class Npc extends Creature
|
||||
private final boolean _isQuestMonster = getTemplate().isQuestMonster();
|
||||
private final boolean _isFakePlayer = getTemplate().isFakePlayer();
|
||||
|
||||
protected RandomAnimationTask _rAniTask;
|
||||
private int _currentLHandId; // normally this shouldn't change from the template, but there exist exceptions
|
||||
private int _currentRHandId; // normally this shouldn't change from the template, but there exist exceptions
|
||||
private int _currentEnchant; // normally this shouldn't change from the template, but there exist exceptions
|
||||
@@ -189,37 +187,6 @@ public class Npc extends Creature
|
||||
initStatusUpdateCache();
|
||||
}
|
||||
|
||||
public void startRandomAnimationTask()
|
||||
{
|
||||
if (!hasRandomAnimation())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_rAniTask == null)
|
||||
{
|
||||
synchronized (this)
|
||||
{
|
||||
if (_rAniTask == null)
|
||||
{
|
||||
_rAniTask = new RandomAnimationTask(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_rAniTask.startRandomAnimationTimer();
|
||||
}
|
||||
|
||||
public void stopRandomAnimationTask()
|
||||
{
|
||||
final RandomAnimationTask rAniTask = _rAniTask;
|
||||
if (rAniTask != null)
|
||||
{
|
||||
rAniTask.stopRandomAnimationTimer();
|
||||
_rAniTask = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a packet SocialAction to all PlayerInstance in the _KnownPlayers of the NpcInstance and create a new RandomAnimation Task.
|
||||
* @param animationId
|
||||
|
@@ -1,104 +0,0 @@
|
||||
/*
|
||||
* 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 org.l2jmobius.gameserver.model.actor.tasks.npc;
|
||||
|
||||
import static org.l2jmobius.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.l2jmobius.Config;
|
||||
import org.l2jmobius.commons.concurrent.ThreadPool;
|
||||
import org.l2jmobius.commons.util.Rnd;
|
||||
import org.l2jmobius.gameserver.model.actor.Npc;
|
||||
|
||||
/**
|
||||
* @author Nik
|
||||
*/
|
||||
public class RandomAnimationTask implements Runnable
|
||||
{
|
||||
private static final Logger LOGGER = Logger.getLogger(RandomAnimationTask.class.getName());
|
||||
private final Npc _npc;
|
||||
private boolean _stopTask;
|
||||
|
||||
public RandomAnimationTask(Npc npc)
|
||||
{
|
||||
_npc = npc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
if (_stopTask)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (!_npc.isInActiveRegion())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Cancel further animation timers until intention is changed to ACTIVE again.
|
||||
if (_npc.isAttackable() && (_npc.getAI().getIntention() != AI_INTENTION_ACTIVE))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_npc.isDead() && !_npc.hasBlockActions())
|
||||
{
|
||||
_npc.onRandomAnimation(Rnd.get(2, 3));
|
||||
}
|
||||
|
||||
startRandomAnimationTimer();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.log(Level.SEVERE, "Execution of RandomAnimationTask has failed.", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a RandomAnimation Task that will be launched after the calculated delay.
|
||||
*/
|
||||
public void startRandomAnimationTimer()
|
||||
{
|
||||
if (!_npc.hasRandomAnimation() || _stopTask)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
final int minWait = _npc.isAttackable() ? Config.MIN_MONSTER_ANIMATION : Config.MIN_NPC_ANIMATION;
|
||||
final int maxWait = _npc.isAttackable() ? Config.MAX_MONSTER_ANIMATION : Config.MAX_NPC_ANIMATION;
|
||||
|
||||
// Calculate the delay before the next animation
|
||||
final int interval = Rnd.get(minWait, maxWait) * 1000;
|
||||
|
||||
// Create a RandomAnimation Task that will be launched after the calculated delay
|
||||
ThreadPool.schedule(this, interval);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the task from continuing and blocks it from continuing ever again. You need to create new task if you want to start it again.
|
||||
*/
|
||||
public void stopRandomAnimationTimer()
|
||||
{
|
||||
_stopTask = true;
|
||||
}
|
||||
}
|
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* 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 org.l2jmobius.gameserver.taskmanager;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.l2jmobius.Config;
|
||||
import org.l2jmobius.commons.concurrent.ThreadPool;
|
||||
import org.l2jmobius.commons.util.Rnd;
|
||||
import org.l2jmobius.gameserver.ai.CtrlIntention;
|
||||
import org.l2jmobius.gameserver.model.actor.Npc;
|
||||
|
||||
/**
|
||||
* @author Mobius
|
||||
*/
|
||||
public class RandomAnimationManager
|
||||
{
|
||||
private static final Map<Npc, Long> PENDING_ANIMATIONS = new ConcurrentHashMap<>();
|
||||
|
||||
public RandomAnimationManager()
|
||||
{
|
||||
ThreadPool.scheduleAtFixedRate(() ->
|
||||
{
|
||||
final long time = System.currentTimeMillis();
|
||||
for (Entry<Npc, Long> entry : PENDING_ANIMATIONS.entrySet())
|
||||
{
|
||||
if (time > entry.getValue())
|
||||
{
|
||||
final Npc npc = entry.getKey();
|
||||
if (!npc.isInActiveRegion())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Cancel further animation schedules until intention is changed to ACTIVE again.
|
||||
if (npc.isAttackable() && (npc.getAI().getIntention() != CtrlIntention.AI_INTENTION_ACTIVE))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!npc.isDead() && !npc.hasBlockActions())
|
||||
{
|
||||
npc.onRandomAnimation(Rnd.get(2, 3));
|
||||
}
|
||||
|
||||
PENDING_ANIMATIONS.put(npc, time + (Rnd.get((npc.isAttackable() ? Config.MIN_MONSTER_ANIMATION : Config.MIN_NPC_ANIMATION), (npc.isAttackable() ? Config.MAX_MONSTER_ANIMATION : Config.MAX_NPC_ANIMATION)) * 1000));
|
||||
}
|
||||
}
|
||||
}, 0, 1000);
|
||||
}
|
||||
|
||||
public void add(Npc npc)
|
||||
{
|
||||
if (npc.hasRandomAnimation())
|
||||
{
|
||||
PENDING_ANIMATIONS.putIfAbsent(npc, System.currentTimeMillis() + (Rnd.get((npc.isAttackable() ? Config.MIN_MONSTER_ANIMATION : Config.MIN_NPC_ANIMATION), (npc.isAttackable() ? Config.MAX_MONSTER_ANIMATION : Config.MAX_NPC_ANIMATION)) * 1000));
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(Npc npc)
|
||||
{
|
||||
PENDING_ANIMATIONS.remove(npc);
|
||||
}
|
||||
|
||||
public static RandomAnimationManager getInstance()
|
||||
{
|
||||
return SingletonHolder.INSTANCE;
|
||||
}
|
||||
|
||||
private static class SingletonHolder
|
||||
{
|
||||
protected static final RandomAnimationManager INSTANCE = new RandomAnimationManager();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user