Addition of RandomAnimationManager.

This commit is contained in:
MobiusDevelopment
2019-06-09 12:03:03 +00:00
parent b0f6f357fd
commit 415667cbaa
97 changed files with 1513 additions and 2333 deletions

View File

@@ -1287,10 +1287,10 @@ public final class Config
MAX_DRIFT_RANGE = Integer.parseInt(optionsSettings.getProperty("MaxDriftRange", "300"));
MIN_NPC_ANIMATION = Integer.parseInt(optionsSettings.getProperty("MinNPCAnimation", "10"));
MAX_NPC_ANIMATION = Integer.parseInt(optionsSettings.getProperty("MaxNPCAnimation", "20"));
MIN_NPC_ANIMATION = Integer.parseInt(optionsSettings.getProperty("MinNpcAnimation", "5"));
MAX_NPC_ANIMATION = Integer.parseInt(optionsSettings.getProperty("MaxNpcAnimation", "60"));
MIN_MONSTER_ANIMATION = Integer.parseInt(optionsSettings.getProperty("MinMonsterAnimation", "5"));
MAX_MONSTER_ANIMATION = Integer.parseInt(optionsSettings.getProperty("MaxMonsterAnimation", "20"));
MAX_MONSTER_ANIMATION = Integer.parseInt(optionsSettings.getProperty("MaxMonsterAnimation", "60"));
SHOW_NPC_LVL = Boolean.valueOf(optionsSettings.getProperty("ShowNpcLevel", "false"));

View File

@@ -30,14 +30,12 @@ import org.l2jmobius.gameserver.ai.PlayerAI.IntentionCommand;
import org.l2jmobius.gameserver.model.Inventory;
import org.l2jmobius.gameserver.model.Skill;
import org.l2jmobius.gameserver.model.WorldObject;
import org.l2jmobius.gameserver.model.actor.Attackable;
import org.l2jmobius.gameserver.model.actor.Creature;
import org.l2jmobius.gameserver.model.actor.Playable;
import org.l2jmobius.gameserver.model.actor.instance.BoatInstance;
import org.l2jmobius.gameserver.model.actor.instance.DoorInstance;
import org.l2jmobius.gameserver.model.actor.instance.ItemInstance;
import org.l2jmobius.gameserver.model.actor.instance.ItemInstance.ItemLocation;
import org.l2jmobius.gameserver.model.actor.instance.NpcInstance;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.actor.position.Location;
import org.l2jmobius.gameserver.model.zone.ZoneId;
@@ -147,13 +145,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 instanceof Attackable)
{
((NpcInstance) _actor).startRandomAnimationTask();
}
// Launch the Think Event
onEvtThink();
}
@@ -1215,13 +1206,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 instanceof Attackable)
{
((NpcInstance) _actor).startRandomAnimationTask();
}
// Launch the Think Event
onEvtThink();
}

View File

@@ -37,6 +37,7 @@ import org.l2jmobius.gameserver.model.spawn.Spawn;
import org.l2jmobius.gameserver.model.zone.ZoneManager;
import org.l2jmobius.gameserver.model.zone.ZoneType;
import org.l2jmobius.gameserver.model.zone.type.PeaceZone;
import org.l2jmobius.gameserver.taskmanager.RandomAnimationManager;
public final class WorldRegion
{
@@ -222,6 +223,12 @@ public final class WorldRegion
((SiegeGuardAI) mob.getAI()).stopAITask();
}
}
RandomAnimationManager.getInstance().remove(mob);
}
else if (o instanceof NpcInstance)
{
RandomAnimationManager.getInstance().remove((NpcInstance) o);
}
}
}
@@ -233,12 +240,11 @@ public final class WorldRegion
{
// Start HP/MP/CP Regeneration task
((Attackable) o).getStatus().startHpMpRegeneration();
RandomAnimationManager.getInstance().add((NpcInstance) o);
}
else if (o instanceof NpcInstance)
{
// Create a RandomAnimation Task that will be launched after the calculated delay if the server allow it
// Monsterinstance/Attackable socials are handled by AI (TODO: check the instances)
((NpcInstance) o).startRandomAnimationTask();
RandomAnimationManager.getInstance().add((NpcInstance) o);
}
}
}

View File

@@ -16,14 +16,11 @@
*/
package org.l2jmobius.gameserver.model.actor.instance;
import static org.l2jmobius.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;
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.cache.HtmCache;
@@ -131,116 +128,12 @@ public class NpcInstance extends Creature
private int _isSpoiledBy = 0;
private long _lastSocialBroadcast = 0;
private static final int MINIMUM_SOCIAL_INTERVAL = 6000;
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 _currentCollisionHeight; // used for npc grow effect skills
private int _currentCollisionRadius; // used for npc grow effect skills
private int _scriptValue = 0;
public class RandomAnimationTask implements Runnable
{
private final NpcInstance _npc;
private boolean _stopTask;
public RandomAnimationTask(NpcInstance 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.isStunned() || _npc.isSleeping() || _npc.isParalyzed()))
{
_npc.onRandomAnimation(Rnd.get(2, 3));
}
startRandomAnimationTimer();
}
catch (Exception 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;
}
}
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

View File

@@ -0,0 +1,91 @@
/*
* 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.Attackable;
import org.l2jmobius.gameserver.model.actor.instance.NpcInstance;
/**
* @author Mobius
*/
public class RandomAnimationManager
{
private static final Map<NpcInstance, Long> PENDING_ANIMATIONS = new ConcurrentHashMap<>();
public RandomAnimationManager()
{
ThreadPool.scheduleAtFixedRate(() ->
{
final long time = System.currentTimeMillis();
for (Entry<NpcInstance, Long> entry : PENDING_ANIMATIONS.entrySet())
{
if (time > entry.getValue())
{
final NpcInstance npc = entry.getKey();
if (!npc.isInActiveRegion())
{
continue;
}
// Cancel further animation schedules until intention is changed to ACTIVE again.
if ((npc instanceof Attackable) && (npc.getAI().getIntention() != CtrlIntention.AI_INTENTION_ACTIVE))
{
continue;
}
if (!npc.isDead() && !npc.isStunned() && !npc.isSleeping() && !npc.isParalyzed())
{
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(NpcInstance 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(NpcInstance npc)
{
PENDING_ANIMATIONS.remove(npc);
}
public static RandomAnimationManager getInstance()
{
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder
{
protected static final RandomAnimationManager INSTANCE = new RandomAnimationManager();
}
}