123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877 |
- 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.Territory;
- import net.sf.l2j.gameserver.ThreadPoolManager;
- import net.sf.l2j.gameserver.instancemanager.DimensionalRiftManager;
- import net.sf.l2j.gameserver.model.L2Attackable;
- import net.sf.l2j.gameserver.model.L2CharPosition;
- 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.L2FestivalMonsterInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2FolkInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2FriendlyMobInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2GuardInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2MinionInstance;
- 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.L2PenaltyMonsterInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2RaidBossInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2RiftInvaderInstance;
- import net.sf.l2j.gameserver.templates.L2Weapon;
- import net.sf.l2j.gameserver.templates.L2WeaponType;
- import net.sf.l2j.util.Rnd;
- public class L2AttackableAI extends L2CharacterAI implements Runnable
- {
-
- private static final int RANDOM_WALK_RATE = 30;
-
- private static final int MAX_ATTACK_TIMEOUT = 300;
-
- private Future<?> _aiTask;
-
- private int _attackTimeout;
-
- private int _globalAggro;
-
- private boolean _thinking;
-
- public L2AttackableAI(L2Character.AIAccessor accessor)
- {
- super(accessor);
- _attackTimeout = Integer.MAX_VALUE;
- _globalAggro = -10;
- }
- public void run()
- {
-
- onEvtThink();
- }
-
- private boolean autoAttackCondition(L2Character target)
- {
- if (target == null || !(_actor instanceof L2Attackable)) return false;
- L2Attackable me = (L2Attackable) _actor;
-
- if (target.isInvul())
- {
-
- if (target instanceof L2PcInstance && ((L2PcInstance)target).isGM())
- return false;
- if (target instanceof L2Summon && ((L2Summon)target).getOwner().isGM())
- return false;
- }
-
- if (target instanceof L2FolkInstance || target instanceof L2DoorInstance) return false;
-
- if (target.isAlikeDead()
- || !me.isInsideRadius(target, me.getAggroRange(), false, false)
- || Math.abs(_actor.getZ() - target.getZ()) > 300) return false;
-
- if (target instanceof L2PcInstance)
- {
-
- if (((L2PcInstance)target).isGM() && ((L2PcInstance)target).getAccessLevel() <= Config.GM_DONT_TAKE_AGGRO)
- return false;
-
- if (!(me instanceof L2RaidBossInstance) && ((L2PcInstance)target).isSilentMoving())
- return false;
-
-
- if (me.getFactionId() == "varka" && ((L2PcInstance)target).isAlliedWithVarka())
- return false;
- if (me.getFactionId() == "ketra" && ((L2PcInstance)target).isAlliedWithKetra())
- return false;
-
- if (((L2PcInstance)target).isRecentFakeDeath())
- return false;
- if (target.isInParty() && target.getParty().isInDimensionalRift())
- {
- byte riftType = target.getParty().getDimensionalRift().getType();
- byte riftRoom = target.getParty().getDimensionalRift().getCurrentRoom();
- if (me instanceof L2RiftInvaderInstance
- && !DimensionalRiftManager.getInstance().getRoom(riftType, riftRoom).checkIfInZone(me.getX(), me.getY(), me.getZ()))
- return false;
- }
- }
-
- if (_actor instanceof L2GuardInstance)
- {
-
- if (target instanceof L2PcInstance && ((L2PcInstance) target).getKarma() > 0)
-
- return GeoData.getInstance().canSeeTarget(me, target);
-
-
-
- if (target instanceof L2MonsterInstance)
- return (((L2MonsterInstance) target).isAggressive() && GeoData.getInstance().canSeeTarget(me, target));
- return false;
- }
- else if (_actor instanceof L2FriendlyMobInstance)
- {
-
- if (target instanceof L2NpcInstance) return false;
-
- if (target instanceof L2PcInstance && ((L2PcInstance) target).getKarma() > 0)
-
- return GeoData.getInstance().canSeeTarget(me, target);
- else
- return false;
- }
- else
- {
-
- if (target instanceof L2NpcInstance) return false;
-
-
- if (!Config.ALT_MOB_AGRO_IN_PEACEZONE && target.isInsideZone(L2Character.ZONE_PEACE))
- return false;
-
- return (me.isAggressive() && GeoData.getInstance().canSeeTarget(me, target));
- }
- }
- public void startAITask()
- {
-
- if (_aiTask == null)
- {
- _aiTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(this, 1000, 1000);
- }
- }
- public void stopAITask()
- {
- if (_aiTask != null)
- {
- _aiTask.cancel(false);
- _aiTask = null;
- }
- }
- @Override
- protected void onEvtDead()
- {
- stopAITask();
- super.onEvtDead();
- }
-
- @Override
- synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
- {
- if (intention == AI_INTENTION_IDLE || intention == AI_INTENTION_ACTIVE)
- {
-
- if (!_actor.isAlikeDead())
- {
- L2Attackable npc = (L2Attackable) _actor;
-
- if (npc.getKnownList().getKnownPlayers().size() > 0) intention = AI_INTENTION_ACTIVE;
- }
- 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);
-
- startAITask();
- }
-
- @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 (L2Object obj : npc.getKnownList().getKnownObjects().values())
- {
- if (obj == null || !(obj instanceof L2Character)) continue;
- L2Character target = (L2Character) obj;
-
- if ((_actor instanceof L2FestivalMonsterInstance) && obj instanceof L2PcInstance)
- {
- L2PcInstance targetPlayer = (L2PcInstance) obj;
- if (!(targetPlayer.isFestivalParticipant())) continue;
- }
-
- if (autoAttackCondition(target))
- {
-
- int hating = npc.getHating(target);
-
- if (hating == 0) npc.addDamageHate(target, 0, 1);
- }
- }
-
- L2Character hated;
- if (_actor.isConfused()) hated = getAttackTarget();
- 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);
- }
- return;
- }
- }
-
- if (_actor instanceof L2GuardInstance)
- {
-
- ((L2GuardInstance) _actor).returnHome();
- }
-
- if (_actor instanceof L2FestivalMonsterInstance) return;
-
- if (_actor instanceof L2MinionInstance && ((L2MinionInstance)_actor).getLeader() != null)
- {
- int offset;
- if (_actor.isRaid()) offset = 500;
- else offset = 200;
- if(((L2MinionInstance)_actor).getLeader().isRunning()) _actor.setRunning();
- else _actor.setWalking();
- if (_actor.getPlanDistanceSq(((L2MinionInstance)_actor).getLeader()) > offset*offset)
- {
- int x1, y1, z1;
- x1 = ((L2MinionInstance)_actor).getLeader().getX() + Rnd.nextInt( (offset - 30) * 2 ) - ( offset - 30 );
- y1 = ((L2MinionInstance)_actor).getLeader().getY() + Rnd.nextInt( (offset - 30) * 2 ) - ( offset - 30 );
- z1 = ((L2MinionInstance)_actor).getLeader().getZ();
-
- moveTo(x1, y1, z1);
- return;
- }
- }
-
- else if (npc.getSpawn() != null && Rnd.nextInt(RANDOM_WALK_RATE) == 0)
- {
- int x1, y1, z1;
-
- if (npc.getSpawn().getLocx() == 0 && npc.getSpawn().getLocy() == 0)
- {
-
- if (Territory.getInstance().getProcMax(npc.getSpawn().getLocation()) > 0) return;
-
- int p[] = Territory.getInstance().getRandomPoint(npc.getSpawn().getLocation());
- x1 = p[0];
- y1 = p[1];
- z1 = p[2];
-
- double distance2 = _actor.getPlanDistanceSq(x1, y1);
- if (distance2 > Config.MAX_DRIFT_RANGE * Config.MAX_DRIFT_RANGE)
- {
- npc.setisReturningToSpawnPoint(true);
- float delay = (float) Math.sqrt(distance2) / Config.MAX_DRIFT_RANGE;
- x1 = _actor.getX() + (int) ((x1 - _actor.getX()) / delay);
- y1 = _actor.getY() + (int) ((y1 - _actor.getY()) / delay);
- }
- else
- npc.setisReturningToSpawnPoint(false);
- }
- else
- {
-
- x1 = npc.getSpawn().getLocx() + Rnd.nextInt(Config.MAX_DRIFT_RANGE * 2)
- - Config.MAX_DRIFT_RANGE;
- y1 = npc.getSpawn().getLocy() + Rnd.nextInt(Config.MAX_DRIFT_RANGE * 2)
- - Config.MAX_DRIFT_RANGE;
- z1 = npc.getZ();
- }
-
-
- moveTo(x1, y1, z1);
- }
- return;
- }
-
- private void thinkAttack()
- {
- if (_attackTimeout < GameTimeController.getGameTicks())
- {
-
- if (_actor.isRunning())
- {
-
- _actor.setWalking();
-
- _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
- }
- }
-
- if (getAttackTarget() == null || getAttackTarget().isAlikeDead()
- || _attackTimeout < GameTimeController.getGameTicks())
- {
-
- if (getAttackTarget() != null)
- {
- L2Attackable npc = (L2Attackable) _actor;
- npc.stopHating(getAttackTarget());
- }
-
- setIntention(AI_INTENTION_ACTIVE);
- _actor.setWalking();
- }
- else
- {
-
- if (((L2NpcInstance) _actor).getFactionId() != null)
- {
- String faction_id = ((L2NpcInstance) _actor).getFactionId();
-
- for (L2Object obj : _actor.getKnownList().getKnownObjects().values())
- {
- if (obj instanceof L2NpcInstance)
- {
- L2NpcInstance npc = (L2NpcInstance) obj;
- if (npc == null || getAttackTarget() == null || faction_id != npc.getFactionId())
- continue;
-
- if (_actor.isInsideRadius(npc, npc.getFactionRange(), true, false)
- && GeoData.getInstance().canSeeTarget(_actor, npc)
- && Math.abs(getAttackTarget().getZ() - npc.getZ()) < 600
- && npc.getAI() != null
- && _actor.getAttackByList().contains(getAttackTarget())
- && (npc.getAI()._intention == CtrlIntention.AI_INTENTION_IDLE
- || npc.getAI()._intention == CtrlIntention.AI_INTENTION_ACTIVE))
- {
- if (getAttackTarget() instanceof L2PcInstance
- && getAttackTarget().isInParty()
- && getAttackTarget().getParty().isInDimensionalRift())
- {
- byte riftType = getAttackTarget().getParty().getDimensionalRift().getType();
- byte riftRoom = getAttackTarget().getParty().getDimensionalRift().getCurrentRoom();
- if (_actor instanceof L2RiftInvaderInstance
- && !DimensionalRiftManager.getInstance().getRoom(riftType, riftRoom).checkIfInZone(npc.getX(), npc.getY(), npc.getZ()))
- continue;
- }
-
- npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, getAttackTarget(), 1);
- }
- }
- }
- }
- if(_actor.isAttackingDisabled()) return;
-
- L2Skill[] skills = null;
- double dist2 = 0;
- int range = 0;
- try
- {
- _actor.setTarget(getAttackTarget());
- skills = _actor.getAllSkills();
- dist2 = _actor.getPlanDistanceSq(getAttackTarget().getX(), getAttackTarget().getY());
- range = _actor.getPhysicalAttackRange() + _actor.getTemplate().collisionRadius + getAttackTarget().getTemplate().collisionRadius;
- }
- catch (NullPointerException e)
- {
-
- setIntention(AI_INTENTION_ACTIVE);
- return;
- }
- L2Weapon weapon = _actor.getActiveWeaponItem();
- if (weapon != null && weapon.getItemType() == L2WeaponType.BOW)
- {
-
- double distance2 = _actor.getPlanDistanceSq(getAttackTarget().getX(), getAttackTarget().getY());
- if (distance2 <= 10000)
- {
- int chance = 5;
- if (chance >= Rnd.get(100))
- {
- int posX = _actor.getX();
- int posY = _actor.getY();
- int posZ = _actor.getZ();
- double distance = Math.sqrt(distance2);
- int signx=-1;
- int signy=-1;
- if (_actor.getX()>getAttackTarget().getX())
- signx=1;
- if (_actor.getY()>getAttackTarget().getY())
- signy=1;
- posX += Math.round((float)((signx * ((range / 2) + (Rnd.get(range)))) - distance));
- posY += Math.round((float)((signy * ((range / 2) + (Rnd.get(range)))) - distance));
- setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, new L2CharPosition(posX, posY, posZ, 0));
- return;
- }
- }
- }
-
- L2Character hated;
- if (_actor.isConfused()) hated = getAttackTarget();
- else hated = ((L2Attackable) _actor).getMostHated();
- if (hated == null)
- {
- setIntention(AI_INTENTION_ACTIVE);
- return;
- }
- if (hated != getAttackTarget())
- {
- setAttackTarget(hated);
- }
-
- dist2 = _actor.getPlanDistanceSq(hated.getX(), hated.getY());
- if (hated.isMoving()) range += 50;
-
- if (dist2 > range*range)
- {
-
- if (!_actor.isMuted() &&
- (!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) || (dist2 >= castRange * castRange / 9.0)
- && (dist2 <= castRange * castRange) && (castRange > 70))
- && !_actor.isSkillDisabled(sk.getId())
- && _actor.getCurrentMp() >= _actor.getStat().getMpConsume(sk)
- && !sk.isPassive()
- && Rnd.nextInt(100) <= 5)
- {
- 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 (hated.isMoving()) range -= 100; if (range < 5) range = 5;
- moveToPawn(getAttackTarget(), range);
- return;
- }
-
- else
- {
- _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
-
- if (!_actor.isMuted() )
- {
- boolean useSkillSelf = true;;
- for (L2Skill sk : skills)
- {
- if (!sk.isPassive()
- && _actor.getCurrentMp() >= _actor.getStat().getMpConsume(sk)
- && !_actor.isSkillDisabled(sk.getId()) && (Rnd.nextInt(100) <= 8
- || (_actor instanceof L2PenaltyMonsterInstance && Rnd.nextInt(100) <= 20)))
- {
- L2Object OldTarget = _actor.getTarget();
- if (sk.getSkillType() == L2Skill.SkillType.BUFF
- || sk.getSkillType() == L2Skill.SkillType.HEAL)
- {
- 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);
- }
-
- if (!useSkillSelf && !GeoData.getInstance().canSeeTarget(_actor, _actor.getTarget()))
- return;
- clientStopMoving(null);
- _accessor.doCast(sk);
- _actor.setTarget(OldTarget);
- return;
- }
- }
- }
-
- clientStopMoving(null);
- _accessor.doAttack(hated);
- }
- }
- }
-
- @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);
- }
- else if (((L2Attackable) _actor).getMostHated() != getAttackTarget())
- {
- setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker);
- }
- super.onEvtAttacked(attacker);
- }
-
- @Override
- protected void onEvtAggression(L2Character target, int aggro)
- {
- L2Attackable me = (L2Attackable) _actor;
- if (target != null)
- {
-
- me.addDamageHate(target, 0, aggro);
-
- if (getIntention() != CtrlIntention.AI_INTENTION_ATTACK)
- {
-
- if (!_actor.isRunning()) _actor.setRunning();
- setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
- }
- }
- }
- @Override
- protected void onIntentionActive()
- {
-
- _attackTimeout = Integer.MAX_VALUE;
- super.onIntentionActive();
- }
- public void setGlobalAggro(int value)
- {
- _globalAggro = value;
- }
- }
|