L2SiegeGuardAI.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.ai;
  20. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
  21. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  22. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  23. import java.util.Collection;
  24. import java.util.concurrent.Future;
  25. import com.l2jserver.Config;
  26. import com.l2jserver.gameserver.GameTimeController;
  27. import com.l2jserver.gameserver.GeoData;
  28. import com.l2jserver.gameserver.ThreadPoolManager;
  29. import com.l2jserver.gameserver.model.L2Object;
  30. import com.l2jserver.gameserver.model.actor.L2Attackable;
  31. import com.l2jserver.gameserver.model.actor.L2Character;
  32. import com.l2jserver.gameserver.model.actor.L2Npc;
  33. import com.l2jserver.gameserver.model.actor.L2Playable;
  34. import com.l2jserver.gameserver.model.actor.L2Summon;
  35. import com.l2jserver.gameserver.model.actor.instance.L2DefenderInstance;
  36. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  37. import com.l2jserver.gameserver.model.actor.instance.L2NpcInstance;
  38. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  39. import com.l2jserver.gameserver.model.effects.L2Effect;
  40. import com.l2jserver.gameserver.model.effects.L2EffectType;
  41. import com.l2jserver.gameserver.model.skills.L2Skill;
  42. import com.l2jserver.gameserver.model.skills.L2SkillType;
  43. import com.l2jserver.gameserver.util.Util;
  44. import com.l2jserver.util.Rnd;
  45. /**
  46. * This class manages AI of L2Attackable.
  47. */
  48. public class L2SiegeGuardAI extends L2CharacterAI implements Runnable
  49. {
  50. private static final int MAX_ATTACK_TIMEOUT = 300; // int ticks, i.e. 30 seconds
  51. /** The L2Attackable AI task executed every 1s (call onEvtThink method) */
  52. private Future<?> _aiTask;
  53. /** For attack AI, analysis of mob and its targets */
  54. private final SelfAnalysis _selfAnalysis = new SelfAnalysis();
  55. // private TargetAnalysis _mostHatedAnalysis = new TargetAnalysis();
  56. /** The delay after which the attacked is stopped */
  57. private int _attackTimeout;
  58. /** The L2Attackable aggro counter */
  59. private int _globalAggro;
  60. /** The flag used to indicate that a thinking action is in progress */
  61. private boolean _thinking; // to prevent recursive thinking
  62. private final int _attackRange;
  63. /**
  64. * Constructor of L2AttackableAI.
  65. * @param accessor The AI accessor of the L2Character
  66. */
  67. public L2SiegeGuardAI(L2Character.AIAccessor accessor)
  68. {
  69. super(accessor);
  70. _selfAnalysis.init();
  71. _attackTimeout = Integer.MAX_VALUE;
  72. _globalAggro = -10; // 10 seconds timeout of ATTACK after respawn
  73. _attackRange = ((L2Attackable) _actor).getPhysicalAttackRange();
  74. }
  75. @Override
  76. public void run()
  77. {
  78. // Launch actions corresponding to the Event Think
  79. onEvtThink();
  80. }
  81. /**
  82. * <B><U> Actor is a L2GuardInstance</U> :</B>
  83. * <ul>
  84. * <li>The target isn't a Folk or a Door</li>
  85. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  86. * <li>The target is in the actor Aggro range and is at the same height</li>
  87. * <li>The L2PcInstance target has karma (=PK)</li>
  88. * <li>The L2MonsterInstance target is aggressive</li>
  89. * </ul>
  90. * <B><U> Actor is a L2SiegeGuardInstance</U> :</B>
  91. * <ul>
  92. * <li>The target isn't a Folk or a Door</li>
  93. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  94. * <li>The target is in the actor Aggro range and is at the same height</li>
  95. * <li>A siege is in progress</li>
  96. * <li>The L2PcInstance target isn't a Defender</li>
  97. * </ul>
  98. * <B><U> Actor is a L2FriendlyMobInstance</U> :</B>
  99. * <ul>
  100. * <li>The target isn't a Folk, a Door or another L2NpcInstance</li>
  101. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  102. * <li>The target is in the actor Aggro range and is at the same height</li>
  103. * <li>The L2PcInstance target has karma (=PK)</li>
  104. * </ul>
  105. * <B><U> Actor is a L2MonsterInstance</U> :</B>
  106. * <ul>
  107. * <li>The target isn't a Folk, a Door or another L2NpcInstance</li>
  108. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  109. * <li>The target is in the actor Aggro range and is at the same height</li>
  110. * <li>The actor is Aggressive</li>
  111. * </ul>
  112. * @param target The targeted L2Object
  113. * @return True if the target is autoattackable (depends on the actor type).
  114. */
  115. protected boolean autoAttackCondition(L2Character target)
  116. {
  117. // Check if the target isn't another guard, folk or a door
  118. if ((target == null) || (target instanceof L2DefenderInstance) || (target instanceof L2NpcInstance) || (target instanceof L2DoorInstance) || target.isAlikeDead())
  119. {
  120. return false;
  121. }
  122. // Check if the target isn't invulnerable
  123. if (target.isInvul())
  124. {
  125. // However EffectInvincible requires to check GMs specially
  126. if ((target instanceof L2PcInstance) && ((L2PcInstance) target).isGM())
  127. {
  128. return false;
  129. }
  130. if ((target instanceof L2Summon) && ((L2Summon) target).getOwner().isGM())
  131. {
  132. return false;
  133. }
  134. }
  135. // Get the owner if the target is a summon
  136. if (target instanceof L2Summon)
  137. {
  138. L2PcInstance owner = ((L2Summon) target).getOwner();
  139. if (_actor.isInsideRadius(owner, 1000, true, false))
  140. {
  141. target = owner;
  142. }
  143. }
  144. // Check if the target is a L2PcInstance
  145. if (target instanceof L2Playable)
  146. {
  147. // Check if the target isn't in silent move mode AND too far (>100)
  148. if (((L2Playable) target).isSilentMoving() && !_actor.isInsideRadius(target, 250, false, false))
  149. {
  150. return false;
  151. }
  152. }
  153. // Los Check Here
  154. return (_actor.isAutoAttackable(target) && GeoData.getInstance().canSeeTarget(_actor, target));
  155. }
  156. /**
  157. * Set the Intention of this L2CharacterAI and create an AI Task executed every 1s (call onEvtThink method) for this L2Attackable.<br>
  158. * <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>
  159. * @param intention The new Intention to set to the AI
  160. * @param arg0 The first parameter of the Intention
  161. * @param arg1 The second parameter of the Intention
  162. */
  163. @Override
  164. synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
  165. {
  166. if (Config.DEBUG)
  167. {
  168. _log.info(getClass().getSimpleName() + ": changeIntention(" + intention + ", " + arg0 + ", " + arg1 + ")");
  169. }
  170. if (intention == AI_INTENTION_IDLE /* || intention == AI_INTENTION_ACTIVE */) // active becomes idle if only a summon is present
  171. {
  172. // Check if actor is not dead
  173. if (!_actor.isAlikeDead())
  174. {
  175. L2Attackable npc = (L2Attackable) _actor;
  176. // If its _knownPlayer isn't empty set the Intention to AI_INTENTION_ACTIVE
  177. if (!npc.getKnownList().getKnownPlayers().isEmpty())
  178. {
  179. intention = AI_INTENTION_ACTIVE;
  180. }
  181. else
  182. {
  183. intention = AI_INTENTION_IDLE;
  184. }
  185. }
  186. if (intention == AI_INTENTION_IDLE)
  187. {
  188. // Set the Intention of this L2AttackableAI to AI_INTENTION_IDLE
  189. super.changeIntention(AI_INTENTION_IDLE, null, null);
  190. // Stop AI task and detach AI from NPC
  191. if (_aiTask != null)
  192. {
  193. _aiTask.cancel(true);
  194. _aiTask = null;
  195. }
  196. // Cancel the AI
  197. _accessor.detachAI();
  198. return;
  199. }
  200. }
  201. // Set the Intention of this L2AttackableAI to intention
  202. super.changeIntention(intention, arg0, arg1);
  203. // If not idle - create an AI task (schedule onEvtThink repeatedly)
  204. if (_aiTask == null)
  205. {
  206. _aiTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(this, 1000, 1000);
  207. }
  208. }
  209. /**
  210. * Manage the Attack Intention : Stop current Attack (if necessary), Calculate attack timeout, Start a new Attack and Launch Think Event.
  211. * @param target The L2Character to attack
  212. */
  213. @Override
  214. protected void onIntentionAttack(L2Character target)
  215. {
  216. // Calculate the attack timeout
  217. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getInstance().getGameTicks();
  218. // Manage the Attack Intention : Stop current Attack (if necessary), Start a new Attack and Launch Think Event
  219. // if (_actor.getTarget() != null)
  220. super.onIntentionAttack(target);
  221. }
  222. /**
  223. * Manage AI standard thinks of a L2Attackable (called by onEvtThink).<br>
  224. * <B><U> Actions</U> :</B>
  225. * <ul>
  226. * <li>Update every 1s the _globalAggro counter to come close to 0</li>
  227. * <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>
  228. * <li>If the actor can't attack, order to it to return to its home location</li>
  229. * </ul>
  230. */
  231. private void thinkActive()
  232. {
  233. L2Attackable npc = (L2Attackable) _actor;
  234. // Update every 1s the _globalAggro counter to come close to 0
  235. if (_globalAggro != 0)
  236. {
  237. if (_globalAggro < 0)
  238. {
  239. _globalAggro++;
  240. }
  241. else
  242. {
  243. _globalAggro--;
  244. }
  245. }
  246. // Add all autoAttackable L2Character in L2Attackable Aggro Range to its _aggroList with 0 damage and 1 hate
  247. // A L2Attackable isn't aggressive during 10s after its spawn because _globalAggro is set to -10
  248. if (_globalAggro >= 0)
  249. {
  250. for (L2Character target : npc.getKnownList().getKnownCharactersInRadius(_attackRange))
  251. {
  252. if (target == null)
  253. {
  254. continue;
  255. }
  256. if (autoAttackCondition(target)) // check aggression
  257. {
  258. // Get the hate level of the L2Attackable against this L2Character target contained in _aggroList
  259. int hating = npc.getHating(target);
  260. // Add the attacker to the L2Attackable _aggroList with 0 damage and 1 hate
  261. if (hating == 0)
  262. {
  263. npc.addDamageHate(target, 0, 1);
  264. }
  265. }
  266. }
  267. // Chose a target from its aggroList
  268. L2Character hated;
  269. if (_actor.isConfused())
  270. {
  271. hated = getAttackTarget(); // Force mobs to attack anybody if confused
  272. }
  273. else
  274. {
  275. hated = npc.getMostHated();
  276. // _mostHatedAnalysis.Update(hated);
  277. }
  278. // Order to the L2Attackable to attack the target
  279. if (hated != null)
  280. {
  281. // Get the hate level of the L2Attackable against this L2Character target contained in _aggroList
  282. int aggro = npc.getHating(hated);
  283. if ((aggro + _globalAggro) > 0)
  284. {
  285. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  286. if (!_actor.isRunning())
  287. {
  288. _actor.setRunning();
  289. }
  290. // Set the AI Intention to AI_INTENTION_ATTACK
  291. setIntention(CtrlIntention.AI_INTENTION_ATTACK, hated, null);
  292. }
  293. return;
  294. }
  295. }
  296. // Order to the L2DefenderInstance to return to its home location because there's no target to attack
  297. ((L2DefenderInstance) _actor).returnHome();
  298. }
  299. /**
  300. * Manage AI attack thinks of a L2Attackable (called by onEvtThink).<br>
  301. * <B><U> Actions</U> :</B>
  302. * <ul>
  303. * <li>Update the attack timeout if actor is running</li>
  304. * <li>If target is dead or timeout is expired, stop this attack and set the Intention to AI_INTENTION_ACTIVE</li>
  305. * <li>Call all L2Object of its Faction inside the Faction Range</li>
  306. * <li>Chose a target and order to attack it with magic skill or physical attack</li>
  307. * </ul>
  308. * TODO: Manage casting rules to healer mobs (like Ant Nurses)
  309. */
  310. private void thinkAttack()
  311. {
  312. if (Config.DEBUG)
  313. {
  314. _log.info(getClass().getSimpleName() + ": thinkAttack(); timeout=" + (_attackTimeout - GameTimeController.getInstance().getGameTicks()));
  315. }
  316. if (_attackTimeout < GameTimeController.getInstance().getGameTicks())
  317. {
  318. // Check if the actor is running
  319. if (_actor.isRunning())
  320. {
  321. // Set the actor movement type to walk and send Server->Client packet ChangeMoveType to all others L2PcInstance
  322. _actor.setWalking();
  323. // Calculate a new attack timeout
  324. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getInstance().getGameTicks();
  325. }
  326. }
  327. L2Character attackTarget = getAttackTarget();
  328. // Check if target is dead or if timeout is expired to stop this attack
  329. if ((attackTarget == null) || attackTarget.isAlikeDead() || (_attackTimeout < GameTimeController.getInstance().getGameTicks()))
  330. {
  331. // Stop hating this target after the attack timeout or if target is dead
  332. if (attackTarget != null)
  333. {
  334. L2Attackable npc = (L2Attackable) _actor;
  335. npc.stopHating(attackTarget);
  336. }
  337. // Cancel target and timeout
  338. _attackTimeout = Integer.MAX_VALUE;
  339. setAttackTarget(null);
  340. // Set the AI Intention to AI_INTENTION_ACTIVE
  341. setIntention(AI_INTENTION_ACTIVE, null, null);
  342. _actor.setWalking();
  343. return;
  344. }
  345. factionNotifyAndSupport();
  346. attackPrepare();
  347. }
  348. private final void factionNotifyAndSupport()
  349. {
  350. L2Character target = getAttackTarget();
  351. // Call all L2Object of its Faction inside the Faction Range
  352. if ((((L2Npc) _actor).getFactionId() == null) || (target == null))
  353. {
  354. return;
  355. }
  356. if (target.isInvul())
  357. {
  358. return; // speeding it up for siege guards
  359. }
  360. String faction_id = ((L2Npc) _actor).getFactionId();
  361. // Go through all L2Character that belong to its faction
  362. // for (L2Character cha : _actor.getKnownList().getKnownCharactersInRadius(((L2NpcInstance) _actor).getFactionRange()+_actor.getTemplate().collisionRadius))
  363. for (L2Character cha : _actor.getKnownList().getKnownCharactersInRadius(1000))
  364. {
  365. if (cha == null)
  366. {
  367. continue;
  368. }
  369. if (!(cha instanceof L2Npc))
  370. {
  371. if (_selfAnalysis.hasHealOrResurrect && (cha instanceof L2PcInstance) && (((L2Npc) _actor).getCastle().getSiege().checkIsDefender(((L2PcInstance) cha).getClan())))
  372. {
  373. // heal friends
  374. if (!_actor.isAttackingDisabled() && (cha.getCurrentHp() < (cha.getMaxHp() * 0.6)) && (_actor.getCurrentHp() > (_actor.getMaxHp() / 2)) && (_actor.getCurrentMp() > (_actor.getMaxMp() / 2)) && cha.isInCombat())
  375. {
  376. for (L2Skill sk : _selfAnalysis.healSkills)
  377. {
  378. if (_actor.getCurrentMp() < sk.getMpConsume())
  379. {
  380. continue;
  381. }
  382. if (_actor.isSkillDisabled(sk))
  383. {
  384. continue;
  385. }
  386. if (!Util.checkIfInRange(sk.getCastRange(), _actor, cha, true))
  387. {
  388. continue;
  389. }
  390. int chance = 5;
  391. if (chance >= Rnd.get(100))
  392. {
  393. continue;
  394. }
  395. if (!GeoData.getInstance().canSeeTarget(_actor, cha))
  396. {
  397. break;
  398. }
  399. L2Object OldTarget = _actor.getTarget();
  400. _actor.setTarget(cha);
  401. clientStopMoving(null);
  402. _accessor.doCast(sk);
  403. _actor.setTarget(OldTarget);
  404. return;
  405. }
  406. }
  407. }
  408. continue;
  409. }
  410. L2Npc npc = (L2Npc) cha;
  411. if (!faction_id.equals(npc.getFactionId()))
  412. {
  413. continue;
  414. }
  415. if (npc.getAI() != null) // TODO: possibly check not needed
  416. {
  417. if (!npc.isDead() && (Math.abs(target.getZ() - npc.getZ()) < 600)
  418. // && _actor.getAttackByList().contains(getAttackTarget())
  419. && ((npc.getAI()._intention == CtrlIntention.AI_INTENTION_IDLE) || (npc.getAI()._intention == CtrlIntention.AI_INTENTION_ACTIVE))
  420. // limiting aggro for siege guards
  421. && target.isInsideRadius(npc, 1500, true, false) && GeoData.getInstance().canSeeTarget(npc, target))
  422. {
  423. // Notify the L2Object AI with EVT_AGGRESSION
  424. npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, getAttackTarget(), 1);
  425. return;
  426. }
  427. // heal friends
  428. if (_selfAnalysis.hasHealOrResurrect && !_actor.isAttackingDisabled() && (npc.getCurrentHp() < (npc.getMaxHp() * 0.6)) && (_actor.getCurrentHp() > (_actor.getMaxHp() / 2)) && (_actor.getCurrentMp() > (_actor.getMaxMp() / 2)) && npc.isInCombat())
  429. {
  430. for (L2Skill sk : _selfAnalysis.healSkills)
  431. {
  432. if (_actor.getCurrentMp() < sk.getMpConsume())
  433. {
  434. continue;
  435. }
  436. if (_actor.isSkillDisabled(sk))
  437. {
  438. continue;
  439. }
  440. if (!Util.checkIfInRange(sk.getCastRange(), _actor, npc, true))
  441. {
  442. continue;
  443. }
  444. int chance = 4;
  445. if (chance >= Rnd.get(100))
  446. {
  447. continue;
  448. }
  449. if (!GeoData.getInstance().canSeeTarget(_actor, npc))
  450. {
  451. break;
  452. }
  453. L2Object OldTarget = _actor.getTarget();
  454. _actor.setTarget(npc);
  455. clientStopMoving(null);
  456. _accessor.doCast(sk);
  457. _actor.setTarget(OldTarget);
  458. return;
  459. }
  460. }
  461. }
  462. }
  463. }
  464. private void attackPrepare()
  465. {
  466. // Get all information needed to choose between physical or magical attack
  467. Collection<L2Skill> skills = null;
  468. double dist_2 = 0;
  469. int range = 0;
  470. L2DefenderInstance sGuard = (L2DefenderInstance) _actor;
  471. L2Character attackTarget = getAttackTarget();
  472. try
  473. {
  474. _actor.setTarget(attackTarget);
  475. skills = _actor.getAllSkills();
  476. dist_2 = _actor.getPlanDistanceSq(attackTarget.getX(), attackTarget.getY());
  477. range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + attackTarget.getTemplate().getCollisionRadius();
  478. if (attackTarget.isMoving())
  479. {
  480. range += 50;
  481. }
  482. }
  483. catch (NullPointerException e)
  484. {
  485. _actor.setTarget(null);
  486. setIntention(AI_INTENTION_IDLE, null, null);
  487. return;
  488. }
  489. // never attack defenders
  490. if (attackTarget instanceof L2PcInstance)
  491. {
  492. if ((sGuard.getConquerableHall() == null) && sGuard.getCastle().getSiege().checkIsDefender(((L2PcInstance) attackTarget).getClan()))
  493. {
  494. // Cancel the target
  495. sGuard.stopHating(attackTarget);
  496. _actor.setTarget(null);
  497. setIntention(AI_INTENTION_IDLE, null, null);
  498. return;
  499. }
  500. }
  501. if (!GeoData.getInstance().canSeeTarget(_actor, attackTarget))
  502. {
  503. // Siege guards differ from normal mobs currently:
  504. // If target cannot seen, don't attack any more
  505. sGuard.stopHating(attackTarget);
  506. _actor.setTarget(null);
  507. setIntention(AI_INTENTION_IDLE, null, null);
  508. return;
  509. }
  510. // Check if the actor isn't muted and if it is far from target
  511. if (!_actor.isMuted() && (dist_2 > (range * range)))
  512. {
  513. // check for long ranged skills and heal/buff skills
  514. for (L2Skill sk : skills)
  515. {
  516. int castRange = sk.getCastRange();
  517. if ((dist_2 <= (castRange * castRange)) && (castRange > 70) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() >= _actor.getStat().getMpConsume(sk)) && !sk.isPassive())
  518. {
  519. L2Object OldTarget = _actor.getTarget();
  520. if ((sk.getSkillType() == L2SkillType.BUFF) || (sk.hasEffectType(L2EffectType.HEAL)))
  521. {
  522. boolean useSkillSelf = true;
  523. if ((sk.hasEffectType(L2EffectType.HEAL)) && (_actor.getCurrentHp() > (int) (_actor.getMaxHp() / 1.5)))
  524. {
  525. useSkillSelf = false;
  526. break;
  527. }
  528. if (sk.getSkillType() == L2SkillType.BUFF)
  529. {
  530. L2Effect[] effects = _actor.getAllEffects();
  531. for (int i = 0; (effects != null) && (i < effects.length); i++)
  532. {
  533. L2Effect effect = effects[i];
  534. if (effect.getSkill() == sk)
  535. {
  536. useSkillSelf = false;
  537. break;
  538. }
  539. }
  540. }
  541. if (useSkillSelf)
  542. {
  543. _actor.setTarget(_actor);
  544. }
  545. }
  546. clientStopMoving(null);
  547. _accessor.doCast(sk);
  548. _actor.setTarget(OldTarget);
  549. return;
  550. }
  551. }
  552. // Check if the L2SiegeGuardInstance is attacking, knows the target and can't run
  553. if (!(_actor.isAttackingNow()) && (_actor.getRunSpeed() == 0) && (_actor.getKnownList().knowsObject(attackTarget)))
  554. {
  555. // Cancel the target
  556. _actor.getKnownList().removeKnownObject(attackTarget);
  557. _actor.setTarget(null);
  558. setIntention(AI_INTENTION_IDLE, null, null);
  559. }
  560. else
  561. {
  562. double dx = _actor.getX() - attackTarget.getX();
  563. double dy = _actor.getY() - attackTarget.getY();
  564. double dz = _actor.getZ() - attackTarget.getZ();
  565. double homeX = attackTarget.getX() - sGuard.getSpawn().getLocx();
  566. double homeY = attackTarget.getY() - sGuard.getSpawn().getLocy();
  567. // Check if the L2SiegeGuardInstance isn't too far from it's home location
  568. if ((((dx * dx) + (dy * dy)) > 10000) && (((homeX * homeX) + (homeY * homeY)) > 3240000) // 1800 * 1800
  569. && (_actor.getKnownList().knowsObject(attackTarget)))
  570. {
  571. // Cancel the target
  572. _actor.getKnownList().removeKnownObject(attackTarget);
  573. _actor.setTarget(null);
  574. setIntention(AI_INTENTION_IDLE, null, null);
  575. }
  576. else
  577. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  578. {
  579. // Temporary hack for preventing guards jumping off towers,
  580. // before replacing this with effective geodata checks and AI modification
  581. if ((dz * dz) < (170 * 170)) // normally 130 if guard z coordinates correct
  582. {
  583. if (_selfAnalysis.isHealer)
  584. {
  585. return;
  586. }
  587. if (_selfAnalysis.isMage)
  588. {
  589. range = _selfAnalysis.maxCastRange - 50;
  590. }
  591. if (attackTarget.isMoving())
  592. {
  593. moveToPawn(attackTarget, range - 70);
  594. }
  595. else
  596. {
  597. moveToPawn(attackTarget, range);
  598. }
  599. }
  600. }
  601. }
  602. return;
  603. }
  604. // Else, if the actor is muted and far from target, just "move to pawn"
  605. else if (_actor.isMuted() && (dist_2 > (range * range)) && !_selfAnalysis.isHealer)
  606. {
  607. // Temporary hack for preventing guards jumping off towers,
  608. // before replacing this with effective geodata checks and AI modification
  609. double dz = _actor.getZ() - attackTarget.getZ();
  610. if ((dz * dz) < (170 * 170)) // normally 130 if guard z coordinates correct
  611. {
  612. if (_selfAnalysis.isMage)
  613. {
  614. range = _selfAnalysis.maxCastRange - 50;
  615. }
  616. if (attackTarget.isMoving())
  617. {
  618. moveToPawn(attackTarget, range - 70);
  619. }
  620. else
  621. {
  622. moveToPawn(attackTarget, range);
  623. }
  624. }
  625. return;
  626. }
  627. // Else, if this is close enough to attack
  628. else if (dist_2 <= (range * range))
  629. {
  630. // Force mobs to attack anybody if confused
  631. L2Character hated = null;
  632. if (_actor.isConfused())
  633. {
  634. hated = attackTarget;
  635. }
  636. else
  637. {
  638. hated = ((L2Attackable) _actor).getMostHated();
  639. }
  640. if (hated == null)
  641. {
  642. setIntention(AI_INTENTION_ACTIVE, null, null);
  643. return;
  644. }
  645. if (hated != attackTarget)
  646. {
  647. attackTarget = hated;
  648. }
  649. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getInstance().getGameTicks();
  650. // check for close combat skills && heal/buff skills
  651. if (!_actor.isMuted() && (Rnd.nextInt(100) <= 5))
  652. {
  653. for (L2Skill sk : skills)
  654. {
  655. int castRange = sk.getCastRange();
  656. if (((castRange * castRange) >= dist_2) && !sk.isPassive() && (_actor.getCurrentMp() >= _actor.getStat().getMpConsume(sk)) && !_actor.isSkillDisabled(sk))
  657. {
  658. L2Object OldTarget = _actor.getTarget();
  659. if ((sk.getSkillType() == L2SkillType.BUFF) || (sk.hasEffectType(L2EffectType.HEAL)))
  660. {
  661. boolean useSkillSelf = true;
  662. if ((sk.hasEffectType(L2EffectType.HEAL)) && (_actor.getCurrentHp() > (int) (_actor.getMaxHp() / 1.5)))
  663. {
  664. useSkillSelf = false;
  665. break;
  666. }
  667. if (sk.getSkillType() == L2SkillType.BUFF)
  668. {
  669. L2Effect[] effects = _actor.getAllEffects();
  670. for (int i = 0; (effects != null) && (i < effects.length); i++)
  671. {
  672. L2Effect effect = effects[i];
  673. if (effect.getSkill() == sk)
  674. {
  675. useSkillSelf = false;
  676. break;
  677. }
  678. }
  679. }
  680. if (useSkillSelf)
  681. {
  682. _actor.setTarget(_actor);
  683. }
  684. }
  685. clientStopMoving(null);
  686. _accessor.doCast(sk);
  687. _actor.setTarget(OldTarget);
  688. return;
  689. }
  690. }
  691. }
  692. // Finally, do the physical attack itself
  693. if (!_selfAnalysis.isHealer)
  694. {
  695. _accessor.doAttack(attackTarget);
  696. }
  697. }
  698. }
  699. /**
  700. * Manage AI thinking actions of a L2Attackable.
  701. */
  702. @Override
  703. protected void onEvtThink()
  704. {
  705. // if(getIntention() != AI_INTENTION_IDLE && (!_actor.isVisible() || !_actor.hasAI() || !_actor.isKnownPlayers()))
  706. // setIntention(AI_INTENTION_IDLE);
  707. // Check if the thinking action is already in progress
  708. if (_thinking || _actor.isCastingNow() || _actor.isAllSkillsDisabled())
  709. {
  710. return;
  711. }
  712. // Start thinking action
  713. _thinking = true;
  714. try
  715. {
  716. // Manage AI thinks of a L2Attackable
  717. if (getIntention() == AI_INTENTION_ACTIVE)
  718. {
  719. thinkActive();
  720. }
  721. else if (getIntention() == AI_INTENTION_ATTACK)
  722. {
  723. thinkAttack();
  724. }
  725. }
  726. finally
  727. {
  728. // Stop thinking action
  729. _thinking = false;
  730. }
  731. }
  732. /**
  733. * Launch actions corresponding to the Event Attacked.<br>
  734. * <B><U> Actions</U> :</B>
  735. * <ul>
  736. * <li>Init the attack : Calculate the attack timeout, Set the _globalAggro to 0, Add the attacker to the actor _aggroList</li>
  737. * <li>Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance</li>
  738. * <li>Set the Intention to AI_INTENTION_ATTACK</li>
  739. * </ul>
  740. * @param attacker The L2Character that attacks the actor
  741. */
  742. @Override
  743. protected void onEvtAttacked(L2Character attacker)
  744. {
  745. // Calculate the attack timeout
  746. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getInstance().getGameTicks();
  747. // Set the _globalAggro to 0 to permit attack even just after spawn
  748. if (_globalAggro < 0)
  749. {
  750. _globalAggro = 0;
  751. }
  752. // Add the attacker to the _aggroList of the actor
  753. ((L2Attackable) _actor).addDamageHate(attacker, 0, 1);
  754. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  755. if (!_actor.isRunning())
  756. {
  757. _actor.setRunning();
  758. }
  759. // Set the Intention to AI_INTENTION_ATTACK
  760. if (getIntention() != AI_INTENTION_ATTACK)
  761. {
  762. setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker, null);
  763. }
  764. super.onEvtAttacked(attacker);
  765. }
  766. /**
  767. * Launch actions corresponding to the Event Aggression.<br>
  768. * <B><U> Actions</U> :</B>
  769. * <ul>
  770. * <li>Add the target to the actor _aggroList or update hate if already present</li>
  771. * <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>
  772. * </ul>
  773. * @param aggro The value of hate to add to the actor against the target
  774. */
  775. @Override
  776. protected void onEvtAggression(L2Character target, int aggro)
  777. {
  778. if (_actor == null)
  779. {
  780. return;
  781. }
  782. L2Attackable me = (L2Attackable) _actor;
  783. if (target != null)
  784. {
  785. // Add the target to the actor _aggroList or update hate if already present
  786. me.addDamageHate(target, 0, aggro);
  787. // Get the hate of the actor against the target
  788. aggro = me.getHating(target);
  789. if (aggro <= 0)
  790. {
  791. if (me.getMostHated() == null)
  792. {
  793. _globalAggro = -25;
  794. me.clearAggroList();
  795. setIntention(AI_INTENTION_IDLE, null, null);
  796. }
  797. return;
  798. }
  799. // Set the actor AI Intention to AI_INTENTION_ATTACK
  800. if (getIntention() != CtrlIntention.AI_INTENTION_ATTACK)
  801. {
  802. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  803. if (!_actor.isRunning())
  804. {
  805. _actor.setRunning();
  806. }
  807. L2DefenderInstance sGuard = (L2DefenderInstance) _actor;
  808. double homeX = target.getX() - sGuard.getSpawn().getLocx();
  809. double homeY = target.getY() - sGuard.getSpawn().getLocy();
  810. // Check if the L2SiegeGuardInstance is not too far from its home location
  811. if (((homeX * homeX) + (homeY * homeY)) < 3240000)
  812. {
  813. setIntention(CtrlIntention.AI_INTENTION_ATTACK, target, null);
  814. }
  815. }
  816. }
  817. else
  818. {
  819. // currently only for setting lower general aggro
  820. if (aggro >= 0)
  821. {
  822. return;
  823. }
  824. L2Character mostHated = me.getMostHated();
  825. if (mostHated == null)
  826. {
  827. _globalAggro = -25;
  828. return;
  829. }
  830. for (L2Character aggroed : me.getAggroList().keySet())
  831. {
  832. me.addDamageHate(aggroed, 0, aggro);
  833. }
  834. aggro = me.getHating(mostHated);
  835. if (aggro <= 0)
  836. {
  837. _globalAggro = -25;
  838. me.clearAggroList();
  839. setIntention(AI_INTENTION_IDLE, null, null);
  840. }
  841. }
  842. }
  843. @Override
  844. public void stopAITask()
  845. {
  846. if (_aiTask != null)
  847. {
  848. _aiTask.cancel(false);
  849. _aiTask = null;
  850. }
  851. _accessor.detachAI();
  852. super.stopAITask();
  853. }
  854. }