123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926 |
- /*
- * Copyright (C) 2004-2015 L2J Server
- *
- * This file is part of L2J Server.
- *
- * L2J Server 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.
- *
- * L2J Server 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 com.l2jserver.gameserver.ai;
- import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
- import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
- import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
- import java.util.Collection;
- import java.util.concurrent.Future;
- import com.l2jserver.gameserver.GameTimeController;
- import com.l2jserver.gameserver.GeoData;
- import com.l2jserver.gameserver.ThreadPoolManager;
- import com.l2jserver.gameserver.model.L2Object;
- import com.l2jserver.gameserver.model.actor.L2Attackable;
- import com.l2jserver.gameserver.model.actor.L2Character;
- import com.l2jserver.gameserver.model.actor.L2Npc;
- import com.l2jserver.gameserver.model.actor.L2Playable;
- import com.l2jserver.gameserver.model.actor.L2Summon;
- import com.l2jserver.gameserver.model.actor.instance.L2DefenderInstance;
- import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
- import com.l2jserver.gameserver.model.actor.instance.L2NpcInstance;
- import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
- import com.l2jserver.gameserver.model.effects.L2EffectType;
- import com.l2jserver.gameserver.model.skills.Skill;
- import com.l2jserver.gameserver.util.Util;
- import com.l2jserver.util.Rnd;
- /**
- * This class manages AI of L2Attackable.
- */
- public class L2SiegeGuardAI extends L2CharacterAI implements Runnable
- {
- private static final int MAX_ATTACK_TIMEOUT = 300; // int ticks, i.e. 30 seconds
-
- /** The L2Attackable AI task executed every 1s (call onEvtThink method) */
- private Future<?> _aiTask;
-
- /** For attack AI, analysis of mob and its targets */
- private final SelfAnalysis _selfAnalysis = new SelfAnalysis();
- // private TargetAnalysis _mostHatedAnalysis = new TargetAnalysis();
-
- /** The delay after which the attacked is stopped */
- private int _attackTimeout;
-
- /** The L2Attackable aggro counter */
- private int _globalAggro;
-
- /** The flag used to indicate that a thinking action is in progress */
- private boolean _thinking; // to prevent recursive thinking
-
- private final int _attackRange;
-
- /**
- * Constructor of L2AttackableAI.
- * @param creature the creature
- */
- public L2SiegeGuardAI(L2DefenderInstance creature)
- {
- super(creature);
- _selfAnalysis.init();
- _attackTimeout = Integer.MAX_VALUE;
- _globalAggro = -10; // 10 seconds timeout of ATTACK after respawn
- _attackRange = _actor.getPhysicalAttackRange();
- }
-
- @Override
- public void run()
- {
- // Launch actions corresponding to the Event Think
- onEvtThink();
-
- }
-
- /**
- * <B><U> Actor is a L2GuardInstance</U> :</B>
- * <ul>
- * <li>The target isn't a Folk or a Door</li>
- * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
- * <li>The target is in the actor Aggro range and is at the same height</li>
- * <li>The L2PcInstance target has karma (=PK)</li>
- * <li>The L2MonsterInstance target is aggressive</li>
- * </ul>
- * <B><U> Actor is a L2SiegeGuardInstance</U> :</B>
- * <ul>
- * <li>The target isn't a Folk or a Door</li>
- * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
- * <li>The target is in the actor Aggro range and is at the same height</li>
- * <li>A siege is in progress</li>
- * <li>The L2PcInstance target isn't a Defender</li>
- * </ul>
- * <B><U> Actor is a L2FriendlyMobInstance</U> :</B>
- * <ul>
- * <li>The target isn't a Folk, a Door or another L2NpcInstance</li>
- * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
- * <li>The target is in the actor Aggro range and is at the same height</li>
- * <li>The L2PcInstance target has karma (=PK)</li>
- * </ul>
- * <B><U> Actor is a L2MonsterInstance</U> :</B>
- * <ul>
- * <li>The target isn't a Folk, a Door or another L2NpcInstance</li>
- * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
- * <li>The target is in the actor Aggro range and is at the same height</li>
- * <li>The actor is Aggressive</li>
- * </ul>
- * @param target The targeted L2Object
- * @return True if the target is autoattackable (depends on the actor type).
- */
- protected boolean autoAttackCondition(L2Character target)
- {
- // Check if the target isn't another guard, folk or a door
- if ((target == null) || (target instanceof L2DefenderInstance) || (target instanceof L2NpcInstance) || (target instanceof L2DoorInstance) || target.isAlikeDead())
- {
- return false;
- }
-
- // Check if the target isn't invulnerable
- if (target.isInvul())
- {
- // However EffectInvincible requires to check GMs specially
- if ((target instanceof L2PcInstance) && target.isGM())
- {
- return false;
- }
- if ((target instanceof L2Summon) && ((L2Summon) target).getOwner().isGM())
- {
- return false;
- }
- }
-
- // Get the owner if the target is a summon
- if (target instanceof L2Summon)
- {
- L2PcInstance owner = ((L2Summon) target).getOwner();
- if (_actor.isInsideRadius(owner, 1000, true, false))
- {
- target = owner;
- }
- }
-
- // Check if the target is a L2PcInstance
- if (target instanceof L2Playable)
- {
- // Check if the target isn't in silent move mode AND too far (>100)
- if (((L2Playable) target).isSilentMovingAffected() && !_actor.isInsideRadius(target, 250, false, false))
- {
- return false;
- }
- }
- // Los Check Here
- return (_actor.isAutoAttackable(target) && GeoData.getInstance().canSeeTarget(_actor, target));
-
- }
-
- /**
- * Set the Intention of this L2CharacterAI and create an AI Task executed every 1s (call onEvtThink method) for this L2Attackable.<br>
- * <FONT COLOR=#FF0000><B> <U>Caution</U> : If actor _knowPlayer isn't EMPTY, AI_INTENTION_IDLE will be change in AI_INTENTION_ACTIVE</B></FONT>
- * @param intention The new Intention to set to the AI
- * @param arg0 The first parameter of the Intention
- * @param arg1 The second parameter of the Intention
- */
- @Override
- synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
- {
- _log.debug("{}: changeIntention({}, {}, {})", getClass().getSimpleName(), intention, arg0, arg1);
-
- if (intention == AI_INTENTION_IDLE /* || intention == AI_INTENTION_ACTIVE */) // active becomes idle if only a summon is present
- {
- // Check if actor is not dead
- if (!_actor.isAlikeDead())
- {
- L2Attackable npc = (L2Attackable) _actor;
-
- // If its _knownPlayer isn't empty set the Intention to AI_INTENTION_ACTIVE
- if (!npc.getKnownList().getKnownPlayers().isEmpty())
- {
- intention = AI_INTENTION_ACTIVE;
- }
- else
- {
- intention = AI_INTENTION_IDLE;
- }
- }
-
- if (intention == AI_INTENTION_IDLE)
- {
- // Set the Intention of this L2AttackableAI to AI_INTENTION_IDLE
- super.changeIntention(AI_INTENTION_IDLE, null, null);
-
- // Stop AI task and detach AI from NPC
- if (_aiTask != null)
- {
- _aiTask.cancel(true);
- _aiTask = null;
- }
-
- // Cancel the AI
- _actor.detachAI();
-
- return;
- }
- }
-
- // Set the Intention of this L2AttackableAI to intention
- super.changeIntention(intention, arg0, arg1);
-
- // If not idle - create an AI task (schedule onEvtThink repeatedly)
- if (_aiTask == null)
- {
- _aiTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(this, 1000, 1000);
- }
- }
-
- /**
- * Manage the Attack Intention : Stop current Attack (if necessary), Calculate attack timeout, Start a new Attack and Launch Think Event.
- * @param target The L2Character to attack
- */
- @Override
- protected void onIntentionAttack(L2Character target)
- {
- // Calculate the attack timeout
- _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getInstance().getGameTicks();
-
- // Manage the Attack Intention : Stop current Attack (if necessary), Start a new Attack and Launch Think Event
- // if (_actor.getTarget() != null)
- super.onIntentionAttack(target);
- }
-
- /**
- * Manage AI standard thinks of a L2Attackable (called by onEvtThink).<br>
- * <B><U> Actions</U> :</B>
- * <ul>
- * <li>Update every 1s the _globalAggro counter to come close to 0</li>
- * <li>If the actor is Aggressive and can attack, add all autoAttackable L2Character in its Aggro Range to its _aggroList, chose a target and order to attack it</li>
- * <li>If the actor can't attack, order to it to return to its home location</li>
- * </ul>
- */
- private void thinkActive()
- {
- L2Attackable npc = (L2Attackable) _actor;
-
- // Update every 1s the _globalAggro counter to come close to 0
- if (_globalAggro != 0)
- {
- if (_globalAggro < 0)
- {
- _globalAggro++;
- }
- else
- {
- _globalAggro--;
- }
- }
-
- // Add all autoAttackable L2Character in L2Attackable Aggro Range to its _aggroList with 0 damage and 1 hate
- // A L2Attackable isn't aggressive during 10s after its spawn because _globalAggro is set to -10
- if (_globalAggro >= 0)
- {
- for (L2Character target : npc.getKnownList().getKnownCharactersInRadius(_attackRange))
- {
- if (target == null)
- {
- continue;
- }
- if (autoAttackCondition(target)) // check aggression
- {
- // Get the hate level of the L2Attackable against this L2Character target contained in _aggroList
- int hating = npc.getHating(target);
-
- // Add the attacker to the L2Attackable _aggroList with 0 damage and 1 hate
- if (hating == 0)
- {
- npc.addDamageHate(target, 0, 1);
- }
- }
- }
-
- // Chose a target from its aggroList
- L2Character hated;
- if (_actor.isConfused())
- {
- hated = getAttackTarget(); // Force mobs to attack anybody if confused
- }
- else
- {
- hated = npc.getMostHated();
- // _mostHatedAnalysis.Update(hated);
- }
-
- // Order to the L2Attackable to attack the target
- if (hated != null)
- {
- // Get the hate level of the L2Attackable against this L2Character target contained in _aggroList
- int aggro = npc.getHating(hated);
-
- if ((aggro + _globalAggro) > 0)
- {
- // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
- if (!_actor.isRunning())
- {
- _actor.setRunning();
- }
-
- // Set the AI Intention to AI_INTENTION_ATTACK
- setIntention(CtrlIntention.AI_INTENTION_ATTACK, hated, null);
- }
-
- return;
- }
-
- }
- // Order to the L2DefenderInstance to return to its home location because there's no target to attack
- ((L2DefenderInstance) _actor).returnHome();
- }
-
- /**
- * Manage AI attack thinks of a L2Attackable (called by onEvtThink).<br>
- * <B><U> Actions</U> :</B>
- * <ul>
- * <li>Update the attack timeout if actor is running</li>
- * <li>If target is dead or timeout is expired, stop this attack and set the Intention to AI_INTENTION_ACTIVE</li>
- * <li>Call all L2Object of its Faction inside the Faction Range</li>
- * <li>Chose a target and order to attack it with magic skill or physical attack</li>
- * </ul>
- * TODO: Manage casting rules to healer mobs (like Ant Nurses)
- */
- private void thinkAttack()
- {
- _log.debug("{}: thinkAttack(); timeout={}", getClass().getSimpleName(), (_attackTimeout - GameTimeController.getInstance().getGameTicks()));
-
- if (_attackTimeout < GameTimeController.getInstance().getGameTicks())
- {
- // Check if the actor is running
- if (_actor.isRunning())
- {
- // Set the actor movement type to walk and send Server->Client packet ChangeMoveType to all others L2PcInstance
- _actor.setWalking();
-
- // Calculate a new attack timeout
- _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getInstance().getGameTicks();
- }
- }
-
- L2Character attackTarget = getAttackTarget();
- // Check if target is dead or if timeout is expired to stop this attack
- if ((attackTarget == null) || attackTarget.isAlikeDead() || (_attackTimeout < GameTimeController.getInstance().getGameTicks()))
- {
- // Stop hating this target after the attack timeout or if target is dead
- if (attackTarget != null)
- {
- L2Attackable npc = (L2Attackable) _actor;
- npc.stopHating(attackTarget);
- }
-
- // Cancel target and timeout
- _attackTimeout = Integer.MAX_VALUE;
- setAttackTarget(null);
-
- // Set the AI Intention to AI_INTENTION_ACTIVE
- setIntention(AI_INTENTION_ACTIVE, null, null);
-
- _actor.setWalking();
- return;
- }
-
- factionNotifyAndSupport();
- attackPrepare();
- }
-
- private final void factionNotifyAndSupport()
- {
- L2Character target = getAttackTarget();
- // Call all L2Object of its Faction inside the Faction Range
- if ((((L2Npc) _actor).getTemplate().getClans() == null) || (target == null))
- {
- return;
- }
-
- if (target.isInvul())
- {
- return; // speeding it up for siege guards
- }
-
- // Go through all L2Character that belong to its faction
- // for (L2Character cha : _actor.getKnownList().getKnownCharactersInRadius(((L2NpcInstance) _actor).getFactionRange()+_actor.getTemplate().collisionRadius))
- for (L2Character cha : _actor.getKnownList().getKnownCharactersInRadius(1000))
- {
- if (cha == null)
- {
- continue;
- }
-
- if (!(cha instanceof L2Npc))
- {
- if (_selfAnalysis.hasHealOrResurrect && (cha instanceof L2PcInstance) && (((L2Npc) _actor).getCastle().getSiege().checkIsDefender(((L2PcInstance) cha).getClan())))
- {
- // heal friends
- if (!_actor.isAttackingDisabled() && (cha.getCurrentHp() < (cha.getMaxHp() * 0.6)) && (_actor.getCurrentHp() > (_actor.getMaxHp() / 2)) && (_actor.getCurrentMp() > (_actor.getMaxMp() / 2)) && cha.isInCombat())
- {
- for (Skill sk : _selfAnalysis.healSkills)
- {
- if (_actor.getCurrentMp() < sk.getMpConsume())
- {
- continue;
- }
- if (_actor.isSkillDisabled(sk))
- {
- continue;
- }
- if (!Util.checkIfInRange(sk.getCastRange(), _actor, cha, true))
- {
- continue;
- }
-
- int chance = 5;
- if (chance >= Rnd.get(100))
- {
- continue;
- }
- if (!GeoData.getInstance().canSeeTarget(_actor, cha))
- {
- break;
- }
-
- L2Object OldTarget = _actor.getTarget();
- _actor.setTarget(cha);
- clientStopMoving(null);
- _actor.doCast(sk);
- _actor.setTarget(OldTarget);
- return;
- }
- }
- }
- continue;
- }
-
- L2Npc npc = (L2Npc) cha;
-
- if (!npc.isInMyClan((L2Npc) _actor))
- {
- continue;
- }
-
- if (npc.getAI() != null) // TODO: possibly check not needed
- {
- if (!npc.isDead() && (Math.abs(target.getZ() - npc.getZ()) < 600)
- // && _actor.getAttackByList().contains(getAttackTarget())
- && ((npc.getAI()._intention == CtrlIntention.AI_INTENTION_IDLE) || (npc.getAI()._intention == CtrlIntention.AI_INTENTION_ACTIVE))
- // limiting aggro for siege guards
- && target.isInsideRadius(npc, 1500, true, false) && GeoData.getInstance().canSeeTarget(npc, target))
- {
- // Notify the L2Object AI with EVT_AGGRESSION
- npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, getAttackTarget(), 1);
- return;
- }
- // heal friends
- if (_selfAnalysis.hasHealOrResurrect && !_actor.isAttackingDisabled() && (npc.getCurrentHp() < (npc.getMaxHp() * 0.6)) && (_actor.getCurrentHp() > (_actor.getMaxHp() / 2)) && (_actor.getCurrentMp() > (_actor.getMaxMp() / 2)) && npc.isInCombat())
- {
- for (Skill sk : _selfAnalysis.healSkills)
- {
- if (_actor.getCurrentMp() < sk.getMpConsume())
- {
- continue;
- }
- if (_actor.isSkillDisabled(sk))
- {
- continue;
- }
- if (!Util.checkIfInRange(sk.getCastRange(), _actor, npc, true))
- {
- continue;
- }
-
- int chance = 4;
- if (chance >= Rnd.get(100))
- {
- continue;
- }
- if (!GeoData.getInstance().canSeeTarget(_actor, npc))
- {
- break;
- }
-
- L2Object OldTarget = _actor.getTarget();
- _actor.setTarget(npc);
- clientStopMoving(null);
- _actor.doCast(sk);
- _actor.setTarget(OldTarget);
- return;
- }
- }
- }
- }
- }
-
- private void attackPrepare()
- {
- // Get all information needed to choose between physical or magical attack
- Collection<Skill> skills = null;
- double dist_2 = 0;
- int range = 0;
- L2DefenderInstance sGuard = (L2DefenderInstance) _actor;
- L2Character attackTarget = getAttackTarget();
-
- try
- {
- _actor.setTarget(attackTarget);
- skills = _actor.getAllSkills();
- dist_2 = _actor.calculateDistance(attackTarget, false, true);
- range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + attackTarget.getTemplate().getCollisionRadius();
- if (attackTarget.isMoving())
- {
- range += 50;
- }
- }
- catch (NullPointerException e)
- {
- _actor.setTarget(null);
- setIntention(AI_INTENTION_IDLE, null, null);
- return;
- }
-
- // never attack defenders
- if (attackTarget instanceof L2PcInstance)
- {
- if ((sGuard.getConquerableHall() == null) && sGuard.getCastle().getSiege().checkIsDefender(((L2PcInstance) attackTarget).getClan()))
- {
- // Cancel the target
- sGuard.stopHating(attackTarget);
- _actor.setTarget(null);
- setIntention(AI_INTENTION_IDLE, null, null);
- return;
- }
- }
-
- if (!GeoData.getInstance().canSeeTarget(_actor, attackTarget))
- {
- // Siege guards differ from normal mobs currently:
- // If target cannot seen, don't attack any more
- sGuard.stopHating(attackTarget);
- _actor.setTarget(null);
- setIntention(AI_INTENTION_IDLE, null, null);
- return;
- }
-
- // Check if the actor isn't muted and if it is far from target
- if (!_actor.isMuted() && (dist_2 > (range * range)))
- {
- // check for long ranged skills and heal/buff skills
- for (Skill sk : skills)
- {
- int castRange = sk.getCastRange();
-
- if ((dist_2 <= (castRange * castRange)) && (castRange > 70) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() >= _actor.getStat().getMpConsume(sk)) && !sk.isPassive())
- {
-
- L2Object OldTarget = _actor.getTarget();
- if ((sk.isContinuous() && !sk.isDebuff()) || (sk.hasEffectType(L2EffectType.HEAL)))
- {
- boolean useSkillSelf = true;
- if ((sk.hasEffectType(L2EffectType.HEAL)) && (_actor.getCurrentHp() > (int) (_actor.getMaxHp() / 1.5)))
- {
- useSkillSelf = false;
- break;
- }
-
- if ((sk.isContinuous() && !sk.isDebuff()) && _actor.isAffectedBySkill(sk.getId()))
- {
- useSkillSelf = false;
- }
-
- if (useSkillSelf)
- {
- _actor.setTarget(_actor);
- }
- }
-
- clientStopMoving(null);
- _actor.doCast(sk);
- _actor.setTarget(OldTarget);
- return;
- }
- }
-
- // Check if the L2SiegeGuardInstance is attacking, knows the target and can't run
- if (!(_actor.isAttackingNow()) && (_actor.getRunSpeed() == 0) && (_actor.getKnownList().knowsObject(attackTarget)))
- {
- // Cancel the target
- _actor.getKnownList().removeKnownObject(attackTarget);
- _actor.setTarget(null);
- setIntention(AI_INTENTION_IDLE, null, null);
- }
- else
- {
- double dx = _actor.getX() - attackTarget.getX();
- double dy = _actor.getY() - attackTarget.getY();
- double dz = _actor.getZ() - attackTarget.getZ();
- double homeX = attackTarget.getX() - sGuard.getSpawn().getX();
- double homeY = attackTarget.getY() - sGuard.getSpawn().getY();
-
- // Check if the L2SiegeGuardInstance isn't too far from it's home location
- if ((((dx * dx) + (dy * dy)) > 10000) && (((homeX * homeX) + (homeY * homeY)) > 3240000) // 1800 * 1800
- && (_actor.getKnownList().knowsObject(attackTarget)))
- {
- // Cancel the target
- _actor.getKnownList().removeKnownObject(attackTarget);
- _actor.setTarget(null);
- setIntention(AI_INTENTION_IDLE, null, null);
- }
- else
- // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
- {
- // Temporary hack for preventing guards jumping off towers,
- // before replacing this with effective geodata checks and AI modification
- if ((dz * dz) < (170 * 170)) // normally 130 if guard z coordinates correct
- {
- if (_selfAnalysis.isHealer)
- {
- return;
- }
- if (_selfAnalysis.isMage)
- {
- range = _selfAnalysis.maxCastRange - 50;
- }
- if (attackTarget.isMoving())
- {
- moveToPawn(attackTarget, range - 70);
- }
- else
- {
- moveToPawn(attackTarget, range);
- }
- }
- }
- }
-
- return;
-
- }
- // Else, if the actor is muted and far from target, just "move to pawn"
- else if (_actor.isMuted() && (dist_2 > (range * range)) && !_selfAnalysis.isHealer)
- {
- // Temporary hack for preventing guards jumping off towers,
- // before replacing this with effective geodata checks and AI modification
- double dz = _actor.getZ() - attackTarget.getZ();
- if ((dz * dz) < (170 * 170)) // normally 130 if guard z coordinates correct
- {
- if (_selfAnalysis.isMage)
- {
- range = _selfAnalysis.maxCastRange - 50;
- }
- if (attackTarget.isMoving())
- {
- moveToPawn(attackTarget, range - 70);
- }
- else
- {
- moveToPawn(attackTarget, range);
- }
- }
- return;
- }
- // Else, if this is close enough to attack
- else if (dist_2 <= (range * range))
- {
- // Force mobs to attack anybody if confused
- L2Character hated = null;
- if (_actor.isConfused())
- {
- hated = attackTarget;
- }
- else
- {
- hated = ((L2Attackable) _actor).getMostHated();
- }
-
- if (hated == null)
- {
- setIntention(AI_INTENTION_ACTIVE, null, null);
- return;
- }
- if (hated != attackTarget)
- {
- attackTarget = hated;
- }
-
- _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getInstance().getGameTicks();
-
- // check for close combat skills && heal/buff skills
- if (!_actor.isMuted() && (Rnd.nextInt(100) <= 5))
- {
- for (Skill sk : skills)
- {
- int castRange = sk.getCastRange();
-
- if (((castRange * castRange) >= dist_2) && !sk.isPassive() && (_actor.getCurrentMp() >= _actor.getStat().getMpConsume(sk)) && !_actor.isSkillDisabled(sk))
- {
- L2Object OldTarget = _actor.getTarget();
- if ((sk.isContinuous() && !sk.isDebuff()) || (sk.hasEffectType(L2EffectType.HEAL)))
- {
- boolean useSkillSelf = true;
- if ((sk.hasEffectType(L2EffectType.HEAL)) && (_actor.getCurrentHp() > (int) (_actor.getMaxHp() / 1.5)))
- {
- useSkillSelf = false;
- break;
- }
-
- if ((sk.isContinuous() && !sk.isDebuff()) && _actor.isAffectedBySkill(sk.getId()))
- {
- useSkillSelf = false;
- }
-
- if (useSkillSelf)
- {
- _actor.setTarget(_actor);
- }
- }
-
- clientStopMoving(null);
- _actor.doCast(sk);
- _actor.setTarget(OldTarget);
- return;
- }
- }
- }
- // Finally, do the physical attack itself
- if (!_selfAnalysis.isHealer)
- {
- _actor.doAttack(attackTarget);
- }
- }
- }
-
- /**
- * Manage AI thinking actions of a L2Attackable.
- */
- @Override
- protected void onEvtThink()
- {
- // if(getIntention() != AI_INTENTION_IDLE && (!_actor.isVisible() || !_actor.hasAI() || !_actor.isKnownPlayers()))
- // setIntention(AI_INTENTION_IDLE);
-
- // Check if the thinking action is already in progress
- if (_thinking || _actor.isCastingNow() || _actor.isAllSkillsDisabled())
- {
- return;
- }
-
- // Start thinking action
- _thinking = true;
-
- try
- {
- // Manage AI thinks of a L2Attackable
- if (getIntention() == AI_INTENTION_ACTIVE)
- {
- thinkActive();
- }
- else if (getIntention() == AI_INTENTION_ATTACK)
- {
- thinkAttack();
- }
- }
- finally
- {
- // Stop thinking action
- _thinking = false;
- }
- }
-
- /**
- * Launch actions corresponding to the Event Attacked.<br>
- * <B><U> Actions</U> :</B>
- * <ul>
- * <li>Init the attack : Calculate the attack timeout, Set the _globalAggro to 0, Add the attacker to the actor _aggroList</li>
- * <li>Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance</li>
- * <li>Set the Intention to AI_INTENTION_ATTACK</li>
- * </ul>
- * @param attacker The L2Character that attacks the actor
- */
- @Override
- protected void onEvtAttacked(L2Character attacker)
- {
- // Calculate the attack timeout
- _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getInstance().getGameTicks();
-
- // Set the _globalAggro to 0 to permit attack even just after spawn
- if (_globalAggro < 0)
- {
- _globalAggro = 0;
- }
-
- // Add the attacker to the _aggroList of the actor
- ((L2Attackable) _actor).addDamageHate(attacker, 0, 1);
-
- // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
- if (!_actor.isRunning())
- {
- _actor.setRunning();
- }
-
- // Set the Intention to AI_INTENTION_ATTACK
- if (getIntention() != AI_INTENTION_ATTACK)
- {
- setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker, null);
- }
-
- super.onEvtAttacked(attacker);
- }
-
- /**
- * Launch actions corresponding to the Event Aggression.<br>
- * <B><U> Actions</U> :</B>
- * <ul>
- * <li>Add the target to the actor _aggroList or update hate if already present</li>
- * <li>Set the actor Intention to AI_INTENTION_ATTACK (if actor is L2GuardInstance check if it isn't too far from its home location)</li>
- * </ul>
- * @param aggro The value of hate to add to the actor against the target
- */
- @Override
- protected void onEvtAggression(L2Character target, int aggro)
- {
- if (_actor == null)
- {
- return;
- }
- L2Attackable me = (L2Attackable) _actor;
-
- if (target != null)
- {
- // Add the target to the actor _aggroList or update hate if already present
- me.addDamageHate(target, 0, aggro);
-
- // Get the hate of the actor against the target
- aggro = me.getHating(target);
-
- if (aggro <= 0)
- {
- if (me.getMostHated() == null)
- {
- _globalAggro = -25;
- me.clearAggroList();
- setIntention(AI_INTENTION_IDLE, null, null);
- }
- return;
- }
-
- // Set the actor AI Intention to AI_INTENTION_ATTACK
- if (getIntention() != CtrlIntention.AI_INTENTION_ATTACK)
- {
- // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
- if (!_actor.isRunning())
- {
- _actor.setRunning();
- }
-
- L2DefenderInstance sGuard = (L2DefenderInstance) _actor;
- double homeX = target.getX() - sGuard.getSpawn().getX();
- double homeY = target.getY() - sGuard.getSpawn().getY();
-
- // Check if the L2SiegeGuardInstance is not too far from its home location
- if (((homeX * homeX) + (homeY * homeY)) < 3240000)
- {
- setIntention(CtrlIntention.AI_INTENTION_ATTACK, target, null);
- }
- }
- }
- else
- {
- // currently only for setting lower general aggro
- if (aggro >= 0)
- {
- return;
- }
-
- L2Character mostHated = me.getMostHated();
- if (mostHated == null)
- {
- _globalAggro = -25;
- return;
- }
-
- for (L2Character aggroed : me.getAggroList().keySet())
- {
- me.addDamageHate(aggroed, 0, aggro);
- }
-
- aggro = me.getHating(mostHated);
- if (aggro <= 0)
- {
- _globalAggro = -25;
- me.clearAggroList();
- setIntention(AI_INTENTION_IDLE, null, null);
- }
- }
- }
-
- @Override
- public void stopAITask()
- {
- if (_aiTask != null)
- {
- _aiTask.cancel(false);
- _aiTask = null;
- }
- _actor.detachAI();
- super.stopAITask();
- }
- }
|