Addition of AI related task managers.
This commit is contained in:
@ -20,11 +20,6 @@ import static org.l2jmobius.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
|
||||
import static org.l2jmobius.gameserver.ai.CtrlIntention.AI_INTENTION_FOLLOW;
|
||||
import static org.l2jmobius.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.l2jmobius.commons.concurrent.ThreadPool;
|
||||
import org.l2jmobius.commons.util.Rnd;
|
||||
import org.l2jmobius.gameserver.GameTimeController;
|
||||
import org.l2jmobius.gameserver.model.Location;
|
||||
import org.l2jmobius.gameserver.model.WorldObject;
|
||||
@ -42,6 +37,7 @@ import org.l2jmobius.gameserver.network.serverpackets.MoveToLocation;
|
||||
import org.l2jmobius.gameserver.network.serverpackets.MoveToPawn;
|
||||
import org.l2jmobius.gameserver.network.serverpackets.StopMove;
|
||||
import org.l2jmobius.gameserver.taskmanager.AttackStanceTaskManager;
|
||||
import org.l2jmobius.gameserver.taskmanager.CreatureFollowTaskManager;
|
||||
|
||||
/**
|
||||
* Mother class of all objects AI in the world.<br>
|
||||
@ -50,26 +46,6 @@ import org.l2jmobius.gameserver.taskmanager.AttackStanceTaskManager;
|
||||
*/
|
||||
public abstract class AbstractAI implements Ctrl
|
||||
{
|
||||
private static final Logger LOGGER = Logger.getLogger(AbstractAI.class.getName());
|
||||
|
||||
private NextAction _nextAction;
|
||||
|
||||
/**
|
||||
* @return the _nextAction
|
||||
*/
|
||||
public NextAction getNextAction()
|
||||
{
|
||||
return _nextAction;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param nextAction the next action to set.
|
||||
*/
|
||||
public void setNextAction(NextAction nextAction)
|
||||
{
|
||||
_nextAction = nextAction;
|
||||
}
|
||||
|
||||
/** The creature that this AI manages */
|
||||
protected final Creature _actor;
|
||||
|
||||
@ -97,9 +73,23 @@ public abstract class AbstractAI implements Ctrl
|
||||
/** Different internal state flags */
|
||||
protected int _moveToPawnTimeout;
|
||||
|
||||
private Future<?> _followTask = null;
|
||||
private static final int FOLLOW_INTERVAL = 1000;
|
||||
private static final int ATTACK_FOLLOW_INTERVAL = 500;
|
||||
private NextAction _nextAction;
|
||||
|
||||
/**
|
||||
* @return the _nextAction
|
||||
*/
|
||||
public NextAction getNextAction()
|
||||
{
|
||||
return _nextAction;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param nextAction the next action to set.
|
||||
*/
|
||||
public void setNextAction(NextAction nextAction)
|
||||
{
|
||||
_nextAction = nextAction;
|
||||
}
|
||||
|
||||
protected AbstractAI(Creature creature)
|
||||
{
|
||||
@ -435,7 +425,7 @@ public abstract class AbstractAI implements Ctrl
|
||||
* @param pawn
|
||||
* @param offset
|
||||
*/
|
||||
protected void moveToPawn(WorldObject pawn, int offset)
|
||||
public void moveToPawn(WorldObject pawn, int offset)
|
||||
{
|
||||
// Check if actor can move
|
||||
if (!_actor.isMovementDisabled() && !_actor.isAttackingNow() && !_actor.isCastingNow())
|
||||
@ -720,57 +710,16 @@ public abstract class AbstractAI implements Ctrl
|
||||
*/
|
||||
public synchronized void startFollow(Creature target, int range)
|
||||
{
|
||||
if (_followTask != null)
|
||||
{
|
||||
_followTask.cancel(false);
|
||||
_followTask = null;
|
||||
}
|
||||
|
||||
stopFollow();
|
||||
setTarget(target);
|
||||
|
||||
final int followRange = range == -1 ? Rnd.get(50, 100) : range;
|
||||
_followTask = ThreadPool.scheduleAtFixedRate(() ->
|
||||
if (range == -1)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_followTask == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
final WorldObject followTarget = getTarget(); // copy to prevent NPE
|
||||
if (followTarget == null)
|
||||
{
|
||||
if (_actor.isSummon())
|
||||
{
|
||||
((Summon) _actor).setFollowStatus(false);
|
||||
}
|
||||
setIntention(AI_INTENTION_IDLE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_actor.isInsideRadius3D(followTarget, followRange))
|
||||
{
|
||||
if (!_actor.isInsideRadius3D(followTarget, 3000))
|
||||
{
|
||||
// if the target is too far (maybe also teleported)
|
||||
if (_actor.isSummon())
|
||||
{
|
||||
((Summon) _actor).setFollowStatus(false);
|
||||
}
|
||||
|
||||
setIntention(AI_INTENTION_IDLE);
|
||||
return;
|
||||
}
|
||||
|
||||
moveToPawn(followTarget, followRange);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.warning("Error: " + e.getMessage());
|
||||
}
|
||||
}, 5, range == -1 ? FOLLOW_INTERVAL : ATTACK_FOLLOW_INTERVAL);
|
||||
CreatureFollowTaskManager.getInstance().addNormalFollow(_actor, range);
|
||||
}
|
||||
else
|
||||
{
|
||||
CreatureFollowTaskManager.getInstance().addAttackFollow(_actor, range);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -778,12 +727,7 @@ public abstract class AbstractAI implements Ctrl
|
||||
*/
|
||||
public synchronized void stopFollow()
|
||||
{
|
||||
if (_followTask != null)
|
||||
{
|
||||
// Stop the Follow Task
|
||||
_followTask.cancel(false);
|
||||
_followTask = null;
|
||||
}
|
||||
CreatureFollowTaskManager.getInstance().remove(_actor);
|
||||
}
|
||||
|
||||
public void setTarget(WorldObject target)
|
||||
|
@ -23,12 +23,10 @@ import static org.l2jmobius.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.l2jmobius.Config;
|
||||
import org.l2jmobius.commons.concurrent.ThreadPool;
|
||||
import org.l2jmobius.commons.util.Rnd;
|
||||
import org.l2jmobius.gameserver.GameTimeController;
|
||||
import org.l2jmobius.gameserver.enums.AISkillScope;
|
||||
@ -59,6 +57,7 @@ import org.l2jmobius.gameserver.model.items.instance.ItemInstance;
|
||||
import org.l2jmobius.gameserver.model.skills.Skill;
|
||||
import org.l2jmobius.gameserver.model.skills.SkillCaster;
|
||||
import org.l2jmobius.gameserver.model.zone.ZoneId;
|
||||
import org.l2jmobius.gameserver.taskmanager.AttackableThinkTaskManager;
|
||||
import org.l2jmobius.gameserver.util.Util;
|
||||
|
||||
/**
|
||||
@ -69,12 +68,7 @@ public class AttackableAI extends CreatureAI
|
||||
private static final Logger LOGGER = Logger.getLogger(AttackableAI.class.getName());
|
||||
|
||||
private static final int RANDOM_WALK_RATE = 30; // confirmed
|
||||
// private static final int MAX_DRIFT_RANGE = 300;
|
||||
private static final int MAX_ATTACK_TIMEOUT = 1200; // int ticks, i.e. 2min
|
||||
/**
|
||||
* The Attackable AI task executed every 1s (call onEvtThink method).
|
||||
*/
|
||||
private Future<?> _aiTask;
|
||||
/**
|
||||
* The delay after which the attacked is stopped.
|
||||
*/
|
||||
@ -192,21 +186,13 @@ public class AttackableAI extends CreatureAI
|
||||
|
||||
public void startAITask()
|
||||
{
|
||||
// If not idle - create an AI task (schedule onEvtThink repeatedly)
|
||||
if (_aiTask == null)
|
||||
{
|
||||
_aiTask = ThreadPool.scheduleAtFixedRate(this::onEvtThink, 1000, 1000);
|
||||
}
|
||||
AttackableThinkTaskManager.getInstance().add(getActiveChar());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopAITask()
|
||||
{
|
||||
if (_aiTask != null)
|
||||
{
|
||||
_aiTask.cancel(false);
|
||||
_aiTask = null;
|
||||
}
|
||||
AttackableThinkTaskManager.getInstance().remove(getActiveChar());
|
||||
super.stopAITask();
|
||||
}
|
||||
|
||||
@ -1191,7 +1177,7 @@ public class AttackableAI extends CreatureAI
|
||||
* Manage AI thinking actions of a Attackable.
|
||||
*/
|
||||
@Override
|
||||
protected void onEvtThink()
|
||||
public void onEvtThink()
|
||||
{
|
||||
// Check if the actor can't use skills and if a thinking action isn't already in progress
|
||||
if (_thinking || getActiveChar().isAllSkillsDisabled())
|
||||
|
@ -71,7 +71,7 @@ public class ControllableMobAI extends AttackableAI
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onEvtThink()
|
||||
public void onEvtThink()
|
||||
{
|
||||
if (_isThinking)
|
||||
{
|
||||
|
@ -531,7 +531,7 @@ public class CreatureAI extends AbstractAI
|
||||
* Do nothing.
|
||||
*/
|
||||
@Override
|
||||
protected void onEvtThink()
|
||||
public void onEvtThink()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ public class DoorAI extends CreatureAI
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onEvtThink()
|
||||
public void onEvtThink()
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -119,7 +119,7 @@ public class DoppelgangerAI extends CreatureAI
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onEvtThink()
|
||||
public void onEvtThink()
|
||||
{
|
||||
if (_thinking || _actor.isCastingNow() || _actor.isAllSkillsDisabled())
|
||||
{
|
||||
@ -203,7 +203,7 @@ public class DoppelgangerAI extends CreatureAI
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void moveToPawn(WorldObject pawn, int offset)
|
||||
public void moveToPawn(WorldObject pawn, int offset)
|
||||
{
|
||||
// Check if actor can move
|
||||
if (!_actor.isMovementDisabled() && (_actor.getMoveSpeed() > 0))
|
||||
|
@ -339,7 +339,7 @@ public class PlayerAI extends PlayableAI
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onEvtThink()
|
||||
public void onEvtThink()
|
||||
{
|
||||
if (_thinking && (getIntention() != AI_INTENTION_CAST))
|
||||
{
|
||||
|
@ -165,7 +165,7 @@ public class SummonAI extends PlayableAI implements Runnable
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onEvtThink()
|
||||
public void onEvtThink()
|
||||
{
|
||||
if (_thinking || _actor.isCastingNow() || _actor.isAllSkillsDisabled())
|
||||
{
|
||||
|
@ -112,7 +112,7 @@ public abstract class VehicleAI extends CreatureAI
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void moveToPawn(WorldObject pawn, int offset)
|
||||
public void moveToPawn(WorldObject pawn, int offset)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -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.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.l2jmobius.commons.concurrent.ThreadPool;
|
||||
import org.l2jmobius.gameserver.ai.CreatureAI;
|
||||
import org.l2jmobius.gameserver.model.actor.Attackable;
|
||||
|
||||
/**
|
||||
* @author Mobius
|
||||
*/
|
||||
public class AttackableThinkTaskManager
|
||||
{
|
||||
private static final Set<Attackable> ATTACKABLES = ConcurrentHashMap.newKeySet();
|
||||
private static boolean _working = false;
|
||||
|
||||
public AttackableThinkTaskManager()
|
||||
{
|
||||
ThreadPool.scheduleAtFixedRate(() ->
|
||||
{
|
||||
if (_working)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_working = true;
|
||||
|
||||
CreatureAI ai;
|
||||
for (Attackable attackable : ATTACKABLES)
|
||||
{
|
||||
if (attackable.hasAI())
|
||||
{
|
||||
ai = attackable.getAI();
|
||||
if (ai != null)
|
||||
{
|
||||
ai.onEvtThink();
|
||||
}
|
||||
else
|
||||
{
|
||||
remove(attackable);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
remove(attackable);
|
||||
}
|
||||
}
|
||||
|
||||
_working = false;
|
||||
}, 1000, 1000);
|
||||
}
|
||||
|
||||
public void add(Attackable attackable)
|
||||
{
|
||||
if (!ATTACKABLES.contains(attackable))
|
||||
{
|
||||
ATTACKABLES.add(attackable);
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(Attackable attackable)
|
||||
{
|
||||
ATTACKABLES.remove(attackable);
|
||||
}
|
||||
|
||||
public static AttackableThinkTaskManager getInstance()
|
||||
{
|
||||
return SingletonHolder.INSTANCE;
|
||||
}
|
||||
|
||||
private static class SingletonHolder
|
||||
{
|
||||
protected static final AttackableThinkTaskManager INSTANCE = new AttackableThinkTaskManager();
|
||||
}
|
||||
}
|
@ -0,0 +1,147 @@
|
||||
/*
|
||||
* 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 static org.l2jmobius.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.l2jmobius.commons.concurrent.ThreadPool;
|
||||
import org.l2jmobius.commons.util.Rnd;
|
||||
import org.l2jmobius.gameserver.ai.CreatureAI;
|
||||
import org.l2jmobius.gameserver.model.WorldObject;
|
||||
import org.l2jmobius.gameserver.model.actor.Creature;
|
||||
import org.l2jmobius.gameserver.model.actor.Summon;
|
||||
|
||||
/**
|
||||
* @author Mobius
|
||||
*/
|
||||
public class CreatureFollowTaskManager
|
||||
{
|
||||
private static final Map<Creature, Integer> NORMAL_FOLLOW_CREATURES = new ConcurrentHashMap<>();
|
||||
private static final Map<Creature, Integer> ATTACK_FOLLOW_CREATURES = new ConcurrentHashMap<>();
|
||||
private static boolean _workingNormal = false;
|
||||
private static boolean _workingAttack = false;
|
||||
|
||||
public CreatureFollowTaskManager()
|
||||
{
|
||||
ThreadPool.scheduleAtFixedRate(() ->
|
||||
{
|
||||
if (_workingNormal)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_workingNormal = true;
|
||||
|
||||
for (Entry<Creature, Integer> entry : NORMAL_FOLLOW_CREATURES.entrySet())
|
||||
{
|
||||
follow(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
_workingNormal = false;
|
||||
}, 1000, 1000);
|
||||
|
||||
ThreadPool.scheduleAtFixedRate(() ->
|
||||
{
|
||||
if (_workingAttack)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_workingAttack = true;
|
||||
|
||||
for (Entry<Creature, Integer> entry : ATTACK_FOLLOW_CREATURES.entrySet())
|
||||
{
|
||||
follow(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
_workingAttack = false;
|
||||
}, 500, 500);
|
||||
}
|
||||
|
||||
private void follow(Creature creature, int range)
|
||||
{
|
||||
if (creature.hasAI())
|
||||
{
|
||||
final CreatureAI ai = creature.getAI();
|
||||
if (ai != null)
|
||||
{
|
||||
final WorldObject followTarget = ai.getTarget();
|
||||
if (followTarget == null)
|
||||
{
|
||||
if (creature.isSummon())
|
||||
{
|
||||
((Summon) creature).setFollowStatus(false);
|
||||
}
|
||||
ai.setIntention(AI_INTENTION_IDLE);
|
||||
return;
|
||||
}
|
||||
|
||||
final int followRange = range == -1 ? Rnd.get(50, 100) : range;
|
||||
if (!creature.isInsideRadius3D(followTarget, followRange))
|
||||
{
|
||||
if (!creature.isInsideRadius3D(followTarget, 3000))
|
||||
{
|
||||
// If the target is too far (maybe also teleported).
|
||||
if (creature.isSummon())
|
||||
{
|
||||
((Summon) creature).setFollowStatus(false);
|
||||
}
|
||||
ai.setIntention(AI_INTENTION_IDLE);
|
||||
return;
|
||||
}
|
||||
ai.moveToPawn(followTarget, followRange);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
remove(creature);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
remove(creature);
|
||||
}
|
||||
}
|
||||
|
||||
public void addNormalFollow(Creature creature, int range)
|
||||
{
|
||||
NORMAL_FOLLOW_CREATURES.putIfAbsent(creature, range);
|
||||
}
|
||||
|
||||
public void addAttackFollow(Creature creature, int range)
|
||||
{
|
||||
ATTACK_FOLLOW_CREATURES.putIfAbsent(creature, range);
|
||||
}
|
||||
|
||||
public void remove(Creature creature)
|
||||
{
|
||||
NORMAL_FOLLOW_CREATURES.remove(creature);
|
||||
ATTACK_FOLLOW_CREATURES.remove(creature);
|
||||
}
|
||||
|
||||
public static CreatureFollowTaskManager getInstance()
|
||||
{
|
||||
return SingletonHolder.INSTANCE;
|
||||
}
|
||||
|
||||
private static class SingletonHolder
|
||||
{
|
||||
protected static final CreatureFollowTaskManager INSTANCE = new CreatureFollowTaskManager();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user