123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761 |
- package net.sf.l2j.gameserver.ai;
- import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
- import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
- import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
- import java.util.concurrent.Future;
- import net.sf.l2j.Config;
- import net.sf.l2j.gameserver.GameTimeController;
- import net.sf.l2j.gameserver.GeoData;
- import net.sf.l2j.gameserver.ThreadPoolManager;
- import net.sf.l2j.gameserver.model.L2Attackable;
- import net.sf.l2j.gameserver.model.L2Character;
- import net.sf.l2j.gameserver.model.L2Effect;
- import net.sf.l2j.gameserver.model.L2Object;
- import net.sf.l2j.gameserver.model.L2Skill;
- import net.sf.l2j.gameserver.model.L2Summon;
- import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2FolkInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2MonsterInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2SiegeGuardInstance;
- import net.sf.l2j.util.Rnd;
- public class L2SiegeGuardAI extends L2CharacterAI implements Runnable
- {
-
- private static final int MAX_ATTACK_TIMEOUT = 300;
-
- private Future<?> _aiTask;
-
- private int _attackTimeout;
-
- private int _globalAggro;
-
- private boolean _thinking;
- private int _attackRange;
-
- public L2SiegeGuardAI(L2Character.AIAccessor accessor)
- {
- super(accessor);
- _attackTimeout = Integer.MAX_VALUE;
- _globalAggro = -10;
- _attackRange = ((L2Attackable) _actor).getPhysicalAttackRange();
- }
- public void run()
- {
-
- onEvtThink();
- }
-
- private boolean autoAttackCondition(L2Character target)
- {
-
- if (target == null ||
- target instanceof L2SiegeGuardInstance ||
- target instanceof L2FolkInstance ||
- target instanceof L2DoorInstance ||
- target.isAlikeDead() ||
- target.isInvul()) return false;
-
- if (target instanceof L2Summon)
- {
- L2PcInstance owner = ((L2Summon)target).getOwner();
- if (_actor.isInsideRadius(owner, 1000, true, false))
- target = owner;
- }
-
- if (target instanceof L2PcInstance)
- {
-
- if (((L2PcInstance) target).isSilentMoving()
- && !_actor.isInsideRadius(target, 250, false, false)) return false;
- }
-
- return (_actor.isAutoAttackable(target) && GeoData.getInstance().canSeeTarget(_actor, target));
- }
-
- @Override
- synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
- {
- if (Config.DEBUG)
- _log.info("L2SiegeAI.changeIntention(" + intention + ", " + arg0 + ", " + arg1 + ")");
- ((L2Attackable)_actor).setisReturningToSpawnPoint(false);
-
- if (intention == AI_INTENTION_IDLE )
- {
-
- if (!_actor.isAlikeDead())
- {
- L2Attackable npc = (L2Attackable) _actor;
-
- if (npc.getKnownList().getKnownPlayers().size() > 0) intention = AI_INTENTION_ACTIVE;
- else intention = AI_INTENTION_IDLE;
- }
- if (intention == AI_INTENTION_IDLE)
- {
-
- super.changeIntention(AI_INTENTION_IDLE, null, null);
-
- if (_aiTask != null)
- {
- _aiTask.cancel(true);
- _aiTask = null;
- }
-
- _accessor.detachAI();
- return;
- }
- }
-
- super.changeIntention(intention, arg0, arg1);
-
- if (_aiTask == null)
- {
- _aiTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(this, 1000, 1000);
- }
- }
-
- @Override
- protected void onIntentionAttack(L2Character target)
- {
-
- _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
-
-
- super.onIntentionAttack(target);
- }
-
- private void thinkActive()
- {
- L2Attackable npc = (L2Attackable) _actor;
-
- if (_globalAggro != 0)
- {
- if (_globalAggro < 0) _globalAggro++;
- else _globalAggro--;
- }
-
-
- if (_globalAggro >= 0)
- {
- for (L2Character target : npc.getKnownList().getKnownCharactersInRadius(_attackRange))
- {
- if (target == null) continue;
- if (autoAttackCondition(target))
- {
-
- int hating = npc.getHating(target);
-
- if (hating == 0) npc.addDamageHate(target, 0, 1);
- }
- }
-
- L2Character hated;
- if (_actor.isConfused()) hated = _attackTarget;
- else hated = npc.getMostHated();
-
- if (hated != null)
- {
-
- int aggro = npc.getHating(hated);
- if (aggro + _globalAggro > 0)
- {
-
- if (!_actor.isRunning()) _actor.setRunning();
-
- setIntention(CtrlIntention.AI_INTENTION_ATTACK, hated, null);
- }
- return;
- }
- }
-
- ((L2SiegeGuardInstance) _actor).returnHome();
- return;
- }
- private void attackPrepare()
- {
-
- L2Skill[] skills = null;
- double dist_2 = 0;
- int range = 0;
- L2SiegeGuardInstance sGuard = (L2SiegeGuardInstance) _actor;
- try
- {
- _actor.setTarget(_attackTarget);
- skills = _actor.getAllSkills();
- dist_2 = _actor.getPlanDistanceSq(_attackTarget.getX(), _attackTarget.getY());
- range = _actor.getPhysicalAttackRange() + _actor.getTemplate().collisionRadius + _attackTarget.getTemplate().collisionRadius;
- }
- catch (NullPointerException e)
- {
-
- _actor.setTarget(null);
- setIntention(AI_INTENTION_IDLE, null, null);
- return;
- }
-
- if(_attackTarget instanceof L2PcInstance && sGuard.getCastle().getSiege().checkIsDefender(((L2PcInstance)_attackTarget).getClan()))
- {
-
- sGuard.stopHating(_attackTarget);
- _actor.setTarget(null);
- setIntention(AI_INTENTION_IDLE, null, null);
- return;
- }
- if (!GeoData.getInstance().canSeeTarget(_actor, _attackTarget))
- {
-
-
- sGuard.stopHating(_attackTarget);
- _actor.setTarget(null);
- setIntention(AI_INTENTION_IDLE, null, null);
- return;
- }
-
- if (!_actor.isMuted() && dist_2 > (range + 20) * (range + 20))
- {
-
- if (!Config.ALT_GAME_MOB_ATTACK_AI
- || (_actor instanceof L2MonsterInstance && Rnd.nextInt(100) <= 5))
- for (L2Skill sk : skills)
- {
- int castRange = sk.getCastRange();
- if (((sk.getSkillType() == L2Skill.SkillType.BUFF || sk.getSkillType() == L2Skill.SkillType.HEAL) || (dist_2 >= castRange * castRange / 9)
- && (dist_2 <= castRange * castRange) && (castRange > 70))
- && !_actor.isSkillDisabled(sk.getId())
- && _actor.getCurrentMp() >= _actor.getStat().getMpConsume(sk) && !sk.isPassive())
- {
- L2Object OldTarget = _actor.getTarget();
- if (sk.getSkillType() == L2Skill.SkillType.BUFF
- || sk.getSkillType() == L2Skill.SkillType.HEAL)
- {
- boolean useSkillSelf = true;
- if (sk.getSkillType() == L2Skill.SkillType.HEAL
- && _actor.getCurrentHp() > (int) (_actor.getMaxHp() / 1.5))
- {
- useSkillSelf = false;
- break;
- }
- if (sk.getSkillType() == L2Skill.SkillType.BUFF)
- {
- L2Effect[] effects = _actor.getAllEffects();
- for (int i = 0; effects != null && i < effects.length; i++)
- {
- L2Effect effect = effects[i];
- if (effect.getSkill() == sk)
- {
- useSkillSelf = false;
- break;
- }
- }
- }
- if (useSkillSelf) _actor.setTarget(_actor);
- }
- clientStopMoving(null);
- _accessor.doCast(sk);
- _actor.setTarget(OldTarget);
- return;
- }
- }
-
- if (!(_actor.isAttackingNow()) && (_actor.getRunSpeed() == 0)
- && (_actor.getKnownList().knowsObject(_attackTarget)))
- {
-
- _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.getHomeX();
- double homeY = _attackTarget.getY() - sGuard.getHomeY();
-
- if ((dx * dx + dy * dy > 10000) && (homeX * homeX + homeY * homeY > 3240000)
- && (_actor.getKnownList().knowsObject(_attackTarget)))
- {
-
- _actor.getKnownList().removeKnownObject(_attackTarget);
- _actor.setTarget(null);
- setIntention(AI_INTENTION_IDLE, null, null);
- }
- else
- {
-
-
- if(dz*dz < 170*170)
- moveToPawn(_attackTarget, range);
- }
- }
- return;
- }
-
- else if (_actor.isMuted() && dist_2 > (range + 20) * (range + 20))
- {
-
-
- double dz = _actor.getZ() - _attackTarget.getZ();
- if(dz*dz < 170*170)
- moveToPawn(_attackTarget, range);
- return;
- }
-
- else if (dist_2 <= (range + 20) * (range + 20))
- {
-
- 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.getGameTicks();
-
- if (!_actor.isMuted() && Rnd.nextInt(100) <= 5)
- {
- for (L2Skill sk : skills)
- {
- int castRange = sk.getCastRange();
- if (castRange * castRange >= dist_2 && castRange <= 70 && !sk.isPassive()
- && _actor.getCurrentMp() >= _actor.getStat().getMpConsume(sk)
- && !_actor.isSkillDisabled(sk.getId()))
- {
- L2Object OldTarget = _actor.getTarget();
- if (sk.getSkillType() == L2Skill.SkillType.BUFF
- || sk.getSkillType() == L2Skill.SkillType.HEAL)
- {
- boolean useSkillSelf = true;
- if (sk.getSkillType() == L2Skill.SkillType.HEAL
- && _actor.getCurrentHp() > (int) (_actor.getMaxHp() / 1.5))
- {
- useSkillSelf = false;
- break;
- }
- if (sk.getSkillType() == L2Skill.SkillType.BUFF)
- {
- L2Effect[] effects = _actor.getAllEffects();
- for (int i = 0; effects != null && i < effects.length; i++)
- {
- L2Effect effect = effects[i];
- if (effect.getSkill() == sk)
- {
- useSkillSelf = false;
- break;
- }
- }
- }
- if (useSkillSelf) _actor.setTarget(_actor);
- }
- clientStopMoving(null);
- _accessor.doCast(sk);
- _actor.setTarget(OldTarget);
- return;
- }
- }
- }
-
- _accessor.doAttack(_attackTarget);
- }
- }
-
- private void thinkAttack()
- {
- if (Config.DEBUG)
- _log.info("L2SiegeGuardAI.thinkAttack(); timeout="
- + (_attackTimeout - GameTimeController.getGameTicks()));
- if (_attackTimeout < GameTimeController.getGameTicks())
- {
-
- if (_actor.isRunning())
- {
-
- _actor.setWalking();
-
- _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
- }
- }
-
- if (_attackTarget == null || _attackTarget.isAlikeDead()
- || _attackTimeout < GameTimeController.getGameTicks())
- {
-
- if (_attackTarget != null)
- {
- L2Attackable npc = (L2Attackable) _actor;
- npc.stopHating(_attackTarget);
- }
-
- _attackTimeout = Integer.MAX_VALUE;
- _attackTarget = null;
-
- setIntention(AI_INTENTION_ACTIVE, null, null);
- _actor.setWalking();
- return;
- }
- attackPrepare();
- factionNotify();
- }
- private final void factionNotify()
- {
-
- if (((L2NpcInstance) _actor).getFactionId() == null || _attackTarget == null || _actor == null)
- return;
- if (_attackTarget.isInvul()) return;
- String faction_id = ((L2NpcInstance) _actor).getFactionId();
-
- for (L2Character cha : _actor.getKnownList().getKnownCharactersInRadius(1000))
- {
- if (cha == null) continue;
- if (!(cha instanceof L2NpcInstance)) continue;
- L2NpcInstance npc = (L2NpcInstance) cha;
- if (faction_id != npc.getFactionId()) continue;
-
- if ((npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE || npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE)
- && _actor.isInsideRadius(npc, npc.getFactionRange(), false, true)
- && npc.getTarget() == null
- && _attackTarget.isInsideRadius(npc, npc.getFactionRange(), false, true)
- )
- {
- if (Config.GEODATA > 0)
- {
- if (GeoData.getInstance().canSeeTarget(npc, _attackTarget))
-
- npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, _attackTarget, 1);
- }
- else
- {
- if (Math.abs(_attackTarget.getZ() - npc.getZ()) < 600)
-
- npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, _attackTarget, 1);
- }
- }
- }
- }
-
- @Override
- protected void onEvtThink()
- {
-
-
-
- if (_thinking || _actor.isAllSkillsDisabled()) return;
-
- _thinking = true;
- try
- {
-
- if (getIntention() == AI_INTENTION_ACTIVE) thinkActive();
- else if (getIntention() == AI_INTENTION_ATTACK) thinkAttack();
- }
- finally
- {
-
- _thinking = false;
- }
- }
-
- @Override
- protected void onEvtAttacked(L2Character attacker)
- {
-
- _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
-
- if (_globalAggro < 0) _globalAggro = 0;
-
- ((L2Attackable) _actor).addDamageHate(attacker, 0, 1);
-
- if (!_actor.isRunning()) _actor.setRunning();
-
- if (getIntention() != AI_INTENTION_ATTACK)
- {
- setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker, null);
- }
- super.onEvtAttacked(attacker);
- }
-
- @Override
- protected void onEvtAggression(L2Character target, int aggro)
- {
- if (_actor == null) return;
- L2Attackable me = (L2Attackable) _actor;
- if (target != null)
- {
-
- me.addDamageHate(target, 0, aggro);
-
- aggro = me.getHating(target);
- if (aggro <= 0)
- {
- if (me.getMostHated() == null)
- {
- _globalAggro = -25;
- me.clearAggroList();
- setIntention(AI_INTENTION_IDLE, null, null);
- }
- return;
- }
-
- if (getIntention() != CtrlIntention.AI_INTENTION_ATTACK)
- {
-
- if (!_actor.isRunning()) _actor.setRunning();
- L2SiegeGuardInstance sGuard = (L2SiegeGuardInstance) _actor;
- double homeX = target.getX() - sGuard.getHomeX();
- double homeY = target.getY() - sGuard.getHomeY();
-
- if (homeX * homeX + homeY * homeY < 3240000)
- setIntention(CtrlIntention.AI_INTENTION_ATTACK, target, null);
- }
- }
- else
- {
-
- if(aggro >= 0) return;
- L2Character mostHated = me.getMostHated();
- if (mostHated == null)
- {
- _globalAggro = -25;
- return;
- }
- else
- for(L2Character aggroed : me.getAggroListRP().keySet())
- me.addDamageHate(aggroed, 0, aggro);
- aggro = me.getHating(mostHated);
- if (aggro <= 0)
- {
- _globalAggro = -25;
- me.clearAggroList();
- setIntention(AI_INTENTION_IDLE, null, null);
- }
- }
- }
- @Override
- protected void onEvtDead()
- {
- stopAITask();
- super.onEvtDead();
- }
- public void stopAITask()
- {
- if (_aiTask != null)
- {
- _aiTask.cancel(false);
- _aiTask = null;
- }
- _accessor.detachAI();
- }
- }
|