L2SiegeGuardAI.java 28 KB

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