123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- package net.sf.l2j.gameserver.ai;
- import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
- import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_CAST;
- import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
- import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_INTERACT;
- import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_PICK_UP;
- import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_REST;
- import java.util.EmptyStackException;
- import java.util.Stack;
- import net.sf.l2j.gameserver.ThreadPoolManager;
- import net.sf.l2j.gameserver.model.L2Character;
- import net.sf.l2j.gameserver.model.L2Object;
- import net.sf.l2j.gameserver.model.L2Character.AIAccessor;
- import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2StaticObjectInstance;
- import net.sf.l2j.gameserver.model.actor.knownlist.ObjectKnownList.KnownListAsynchronousUpdateTask;
- public class L2PlayerAI extends L2CharacterAI
- {
- private boolean _thinking;
- class IntentionCommand
- {
- protected CtrlIntention _crtlIntention;
- protected Object _arg0, _arg1;
- protected IntentionCommand(CtrlIntention pIntention, Object pArg0, Object pArg1)
- {
- _crtlIntention = pIntention;
- _arg0 = pArg0;
- _arg1 = pArg1;
- }
- }
- private Stack<IntentionCommand> _interuptedIntentions = new Stack<IntentionCommand>();
- public L2PlayerAI(AIAccessor accessor)
- {
- super(accessor);
- }
-
- @Override
- synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
- {
-
-
- if (intention != AI_INTENTION_CAST)
- {
- super.changeIntention(intention, arg0, arg1);
- return;
- }
-
- if (intention == _intention && arg0 == _intentionArg0 && arg1 == _intentionArg1)
- {
- super.changeIntention(intention, arg0, arg1);
- return;
- }
-
-
- _interuptedIntentions.push(new IntentionCommand(_intention, _intentionArg0, _intentionArg1));
- super.changeIntention(intention, arg0, arg1);
- }
-
- @Override
- protected void onEvtFinishCasting()
- {
-
- if (_skill != null && _skill.isOffensive()) _interuptedIntentions.clear();
- if (getIntention() == AI_INTENTION_CAST)
- {
-
- if (!_interuptedIntentions.isEmpty())
- {
- IntentionCommand cmd = null;
- try
- {
- cmd = _interuptedIntentions.pop();
- }
- catch (EmptyStackException ese)
- {
- }
-
- if (cmd != null && cmd._crtlIntention != AI_INTENTION_CAST)
- {
- setIntention(cmd._crtlIntention, cmd._arg0, cmd._arg1);
- }
- else setIntention(AI_INTENTION_IDLE);
- }
- else
- {
-
-
- setIntention(AI_INTENTION_IDLE);
- }
- }
- }
- @Override
- protected void onIntentionRest()
- {
- if (getIntention() != AI_INTENTION_REST)
- {
- changeIntention(AI_INTENTION_REST, null, null);
- setTarget(null);
- if (getAttackTarget() != null)
- {
- setAttackTarget(null);
- }
- clientStopMoving(null);
- }
- }
- @Override
- protected void onIntentionActive()
- {
- setIntention(AI_INTENTION_IDLE);
- }
- @Override
- protected void clientNotifyDead()
- {
- _clientMovingToPawnOffset = 0;
- _clientMoving = false;
- super.clientNotifyDead();
- }
- private void thinkAttack()
- {
- L2Character target = getAttackTarget();
- if (target == null) return;
- if (checkTargetLostOrDead(target))
- {
- if (target != null)
- {
-
- setAttackTarget(null);
- }
- return;
- }
- if (maybeMoveToPawn(target, _actor.getPhysicalAttackRange())) return;
- _accessor.doAttack(target);
- return;
- }
- private void thinkCast()
- {
- L2Character target = getCastTarget();
-
- if (checkTargetLost(target))
- {
- if (_skill.isOffensive() && getAttackTarget() != null)
- {
-
- setCastTarget(null);
- }
- return;
- }
- if (target != null)
- if (maybeMoveToPawn(target, _actor.getMagicalAttackRange(_skill))) return;
- if (_skill.getHitTime() > 50) clientStopMoving(null);
- L2Object oldTarget = _actor.getTarget();
- if (oldTarget != null)
- {
-
- if (target != null && oldTarget != target) _actor.setTarget(getCastTarget());
-
- _accessor.doCast(_skill);
-
- if (target != null && oldTarget != target) _actor.setTarget(oldTarget);
- }
- else _accessor.doCast(_skill);
- return;
- }
- private void thinkPickUp()
- {
- if (_actor.isAllSkillsDisabled()) return;
- L2Object target = getTarget();
- if (checkTargetLost(target)) return;
- if (maybeMoveToPawn(target, 36)) return;
- setIntention(AI_INTENTION_IDLE);
- ((L2PcInstance.AIAccessor) _accessor).doPickupItem(target);
- return;
- }
- private void thinkInteract()
- {
- if (_actor.isAllSkillsDisabled()) return;
- L2Object target = getTarget();
- if (checkTargetLost(target)) return;
- if (maybeMoveToPawn(target, 36)) return;
- if (!(target instanceof L2StaticObjectInstance)) ((L2PcInstance.AIAccessor) _accessor).doInteract((L2Character) target);
- setIntention(AI_INTENTION_IDLE);
- return;
- }
- @Override
- protected void onEvtThink()
- {
- if (_thinking || _actor.isAllSkillsDisabled()) return;
-
- _thinking = true;
- try
- {
- if (getIntention() == AI_INTENTION_ATTACK) thinkAttack();
- else if (getIntention() == AI_INTENTION_CAST) thinkCast();
- else if (getIntention() == AI_INTENTION_PICK_UP) thinkPickUp();
- else if (getIntention() == AI_INTENTION_INTERACT) thinkInteract();
- }
- finally
- {
- _thinking = false;
- }
- }
- @Override
- protected void onEvtArrivedRevalidate()
- {
- ThreadPoolManager.getInstance().executeTask(new KnownListAsynchronousUpdateTask(_actor));
- super.onEvtArrivedRevalidate();
- }
- }
|