L2SiegeGuardAI.java 28 KB

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