L2SiegeGuardAI.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. /*
  2. * Copyright (C) 2004-2014 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.L2Skill;
  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 accessor The AI accessor of the L2Character
  64. */
  65. public L2SiegeGuardAI(L2Character.AIAccessor accessor)
  66. {
  67. super(accessor);
  68. _selfAnalysis.init();
  69. _attackTimeout = Integer.MAX_VALUE;
  70. _globalAggro = -10; // 10 seconds timeout of ATTACK after respawn
  71. _attackRange = ((L2Attackable) _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) && ((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).isSilentMoving() && !_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. _accessor.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).getFactionId() == null) || (target == null))
  351. {
  352. return;
  353. }
  354. if (target.isInvul())
  355. {
  356. return; // speeding it up for siege guards
  357. }
  358. String faction_id = ((L2Npc) _actor).getFactionId();
  359. // Go through all L2Character that belong to its faction
  360. // for (L2Character cha : _actor.getKnownList().getKnownCharactersInRadius(((L2NpcInstance) _actor).getFactionRange()+_actor.getTemplate().collisionRadius))
  361. for (L2Character cha : _actor.getKnownList().getKnownCharactersInRadius(1000))
  362. {
  363. if (cha == null)
  364. {
  365. continue;
  366. }
  367. if (!(cha instanceof L2Npc))
  368. {
  369. if (_selfAnalysis.hasHealOrResurrect && (cha instanceof L2PcInstance) && (((L2Npc) _actor).getCastle().getSiege().checkIsDefender(((L2PcInstance) cha).getClan())))
  370. {
  371. // heal friends
  372. if (!_actor.isAttackingDisabled() && (cha.getCurrentHp() < (cha.getMaxHp() * 0.6)) && (_actor.getCurrentHp() > (_actor.getMaxHp() / 2)) && (_actor.getCurrentMp() > (_actor.getMaxMp() / 2)) && cha.isInCombat())
  373. {
  374. for (L2Skill sk : _selfAnalysis.healSkills)
  375. {
  376. if (_actor.getCurrentMp() < sk.getMpConsume())
  377. {
  378. continue;
  379. }
  380. if (_actor.isSkillDisabled(sk))
  381. {
  382. continue;
  383. }
  384. if (!Util.checkIfInRange(sk.getCastRange(), _actor, cha, true))
  385. {
  386. continue;
  387. }
  388. int chance = 5;
  389. if (chance >= Rnd.get(100))
  390. {
  391. continue;
  392. }
  393. if (!GeoData.getInstance().canSeeTarget(_actor, cha))
  394. {
  395. break;
  396. }
  397. L2Object OldTarget = _actor.getTarget();
  398. _actor.setTarget(cha);
  399. clientStopMoving(null);
  400. _accessor.doCast(sk);
  401. _actor.setTarget(OldTarget);
  402. return;
  403. }
  404. }
  405. }
  406. continue;
  407. }
  408. L2Npc npc = (L2Npc) cha;
  409. if (!faction_id.equals(npc.getFactionId()))
  410. {
  411. continue;
  412. }
  413. if (npc.getAI() != null) // TODO: possibly check not needed
  414. {
  415. if (!npc.isDead() && (Math.abs(target.getZ() - npc.getZ()) < 600)
  416. // && _actor.getAttackByList().contains(getAttackTarget())
  417. && ((npc.getAI()._intention == CtrlIntention.AI_INTENTION_IDLE) || (npc.getAI()._intention == CtrlIntention.AI_INTENTION_ACTIVE))
  418. // limiting aggro for siege guards
  419. && target.isInsideRadius(npc, 1500, true, false) && GeoData.getInstance().canSeeTarget(npc, target))
  420. {
  421. // Notify the L2Object AI with EVT_AGGRESSION
  422. npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, getAttackTarget(), 1);
  423. return;
  424. }
  425. // heal friends
  426. if (_selfAnalysis.hasHealOrResurrect && !_actor.isAttackingDisabled() && (npc.getCurrentHp() < (npc.getMaxHp() * 0.6)) && (_actor.getCurrentHp() > (_actor.getMaxHp() / 2)) && (_actor.getCurrentMp() > (_actor.getMaxMp() / 2)) && npc.isInCombat())
  427. {
  428. for (L2Skill sk : _selfAnalysis.healSkills)
  429. {
  430. if (_actor.getCurrentMp() < sk.getMpConsume())
  431. {
  432. continue;
  433. }
  434. if (_actor.isSkillDisabled(sk))
  435. {
  436. continue;
  437. }
  438. if (!Util.checkIfInRange(sk.getCastRange(), _actor, npc, true))
  439. {
  440. continue;
  441. }
  442. int chance = 4;
  443. if (chance >= Rnd.get(100))
  444. {
  445. continue;
  446. }
  447. if (!GeoData.getInstance().canSeeTarget(_actor, npc))
  448. {
  449. break;
  450. }
  451. L2Object OldTarget = _actor.getTarget();
  452. _actor.setTarget(npc);
  453. clientStopMoving(null);
  454. _accessor.doCast(sk);
  455. _actor.setTarget(OldTarget);
  456. return;
  457. }
  458. }
  459. }
  460. }
  461. }
  462. private void attackPrepare()
  463. {
  464. // Get all information needed to choose between physical or magical attack
  465. Collection<L2Skill> skills = null;
  466. double dist_2 = 0;
  467. int range = 0;
  468. L2DefenderInstance sGuard = (L2DefenderInstance) _actor;
  469. L2Character attackTarget = getAttackTarget();
  470. try
  471. {
  472. _actor.setTarget(attackTarget);
  473. skills = _actor.getAllSkills();
  474. dist_2 = _actor.calculateDistance(attackTarget, false, true);
  475. range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + attackTarget.getTemplate().getCollisionRadius();
  476. if (attackTarget.isMoving())
  477. {
  478. range += 50;
  479. }
  480. }
  481. catch (NullPointerException e)
  482. {
  483. _actor.setTarget(null);
  484. setIntention(AI_INTENTION_IDLE, null, null);
  485. return;
  486. }
  487. // never attack defenders
  488. if (attackTarget instanceof L2PcInstance)
  489. {
  490. if ((sGuard.getConquerableHall() == null) && sGuard.getCastle().getSiege().checkIsDefender(((L2PcInstance) attackTarget).getClan()))
  491. {
  492. // Cancel the target
  493. sGuard.stopHating(attackTarget);
  494. _actor.setTarget(null);
  495. setIntention(AI_INTENTION_IDLE, null, null);
  496. return;
  497. }
  498. }
  499. if (!GeoData.getInstance().canSeeTarget(_actor, attackTarget))
  500. {
  501. // Siege guards differ from normal mobs currently:
  502. // If target cannot seen, don't attack any more
  503. sGuard.stopHating(attackTarget);
  504. _actor.setTarget(null);
  505. setIntention(AI_INTENTION_IDLE, null, null);
  506. return;
  507. }
  508. // Check if the actor isn't muted and if it is far from target
  509. if (!_actor.isMuted() && (dist_2 > (range * range)))
  510. {
  511. // check for long ranged skills and heal/buff skills
  512. for (L2Skill sk : skills)
  513. {
  514. int castRange = sk.getCastRange();
  515. if ((dist_2 <= (castRange * castRange)) && (castRange > 70) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() >= _actor.getStat().getMpConsume(sk)) && !sk.isPassive())
  516. {
  517. L2Object OldTarget = _actor.getTarget();
  518. if ((sk.isContinuous() && !sk.isDebuff()) || (sk.hasEffectType(L2EffectType.HEAL)))
  519. {
  520. boolean useSkillSelf = true;
  521. if ((sk.hasEffectType(L2EffectType.HEAL)) && (_actor.getCurrentHp() > (int) (_actor.getMaxHp() / 1.5)))
  522. {
  523. useSkillSelf = false;
  524. break;
  525. }
  526. if ((sk.isContinuous() && !sk.isDebuff()) && _actor.isAffectedBySkill(sk.getId()))
  527. {
  528. useSkillSelf = false;
  529. }
  530. if (useSkillSelf)
  531. {
  532. _actor.setTarget(_actor);
  533. }
  534. }
  535. clientStopMoving(null);
  536. _accessor.doCast(sk);
  537. _actor.setTarget(OldTarget);
  538. return;
  539. }
  540. }
  541. // Check if the L2SiegeGuardInstance is attacking, knows the target and can't run
  542. if (!(_actor.isAttackingNow()) && (_actor.getRunSpeed() == 0) && (_actor.getKnownList().knowsObject(attackTarget)))
  543. {
  544. // Cancel the target
  545. _actor.getKnownList().removeKnownObject(attackTarget);
  546. _actor.setTarget(null);
  547. setIntention(AI_INTENTION_IDLE, null, null);
  548. }
  549. else
  550. {
  551. double dx = _actor.getX() - attackTarget.getX();
  552. double dy = _actor.getY() - attackTarget.getY();
  553. double dz = _actor.getZ() - attackTarget.getZ();
  554. double homeX = attackTarget.getX() - sGuard.getSpawn().getX();
  555. double homeY = attackTarget.getY() - sGuard.getSpawn().getY();
  556. // Check if the L2SiegeGuardInstance isn't too far from it's home location
  557. if ((((dx * dx) + (dy * dy)) > 10000) && (((homeX * homeX) + (homeY * homeY)) > 3240000) // 1800 * 1800
  558. && (_actor.getKnownList().knowsObject(attackTarget)))
  559. {
  560. // Cancel the target
  561. _actor.getKnownList().removeKnownObject(attackTarget);
  562. _actor.setTarget(null);
  563. setIntention(AI_INTENTION_IDLE, null, null);
  564. }
  565. else
  566. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  567. {
  568. // Temporary hack for preventing guards jumping off towers,
  569. // before replacing this with effective geodata checks and AI modification
  570. if ((dz * dz) < (170 * 170)) // normally 130 if guard z coordinates correct
  571. {
  572. if (_selfAnalysis.isHealer)
  573. {
  574. return;
  575. }
  576. if (_selfAnalysis.isMage)
  577. {
  578. range = _selfAnalysis.maxCastRange - 50;
  579. }
  580. if (attackTarget.isMoving())
  581. {
  582. moveToPawn(attackTarget, range - 70);
  583. }
  584. else
  585. {
  586. moveToPawn(attackTarget, range);
  587. }
  588. }
  589. }
  590. }
  591. return;
  592. }
  593. // Else, if the actor is muted and far from target, just "move to pawn"
  594. else if (_actor.isMuted() && (dist_2 > (range * range)) && !_selfAnalysis.isHealer)
  595. {
  596. // Temporary hack for preventing guards jumping off towers,
  597. // before replacing this with effective geodata checks and AI modification
  598. double dz = _actor.getZ() - attackTarget.getZ();
  599. if ((dz * dz) < (170 * 170)) // normally 130 if guard z coordinates correct
  600. {
  601. if (_selfAnalysis.isMage)
  602. {
  603. range = _selfAnalysis.maxCastRange - 50;
  604. }
  605. if (attackTarget.isMoving())
  606. {
  607. moveToPawn(attackTarget, range - 70);
  608. }
  609. else
  610. {
  611. moveToPawn(attackTarget, range);
  612. }
  613. }
  614. return;
  615. }
  616. // Else, if this is close enough to attack
  617. else if (dist_2 <= (range * range))
  618. {
  619. // Force mobs to attack anybody if confused
  620. L2Character hated = null;
  621. if (_actor.isConfused())
  622. {
  623. hated = attackTarget;
  624. }
  625. else
  626. {
  627. hated = ((L2Attackable) _actor).getMostHated();
  628. }
  629. if (hated == null)
  630. {
  631. setIntention(AI_INTENTION_ACTIVE, null, null);
  632. return;
  633. }
  634. if (hated != attackTarget)
  635. {
  636. attackTarget = hated;
  637. }
  638. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getInstance().getGameTicks();
  639. // check for close combat skills && heal/buff skills
  640. if (!_actor.isMuted() && (Rnd.nextInt(100) <= 5))
  641. {
  642. for (L2Skill sk : skills)
  643. {
  644. int castRange = sk.getCastRange();
  645. if (((castRange * castRange) >= dist_2) && !sk.isPassive() && (_actor.getCurrentMp() >= _actor.getStat().getMpConsume(sk)) && !_actor.isSkillDisabled(sk))
  646. {
  647. L2Object OldTarget = _actor.getTarget();
  648. if ((sk.isContinuous() && !sk.isDebuff()) || (sk.hasEffectType(L2EffectType.HEAL)))
  649. {
  650. boolean useSkillSelf = true;
  651. if ((sk.hasEffectType(L2EffectType.HEAL)) && (_actor.getCurrentHp() > (int) (_actor.getMaxHp() / 1.5)))
  652. {
  653. useSkillSelf = false;
  654. break;
  655. }
  656. if ((sk.isContinuous() && !sk.isDebuff()) && _actor.isAffectedBySkill(sk.getId()))
  657. {
  658. useSkillSelf = false;
  659. }
  660. if (useSkillSelf)
  661. {
  662. _actor.setTarget(_actor);
  663. }
  664. }
  665. clientStopMoving(null);
  666. _accessor.doCast(sk);
  667. _actor.setTarget(OldTarget);
  668. return;
  669. }
  670. }
  671. }
  672. // Finally, do the physical attack itself
  673. if (!_selfAnalysis.isHealer)
  674. {
  675. _accessor.doAttack(attackTarget);
  676. }
  677. }
  678. }
  679. /**
  680. * Manage AI thinking actions of a L2Attackable.
  681. */
  682. @Override
  683. protected void onEvtThink()
  684. {
  685. // if(getIntention() != AI_INTENTION_IDLE && (!_actor.isVisible() || !_actor.hasAI() || !_actor.isKnownPlayers()))
  686. // setIntention(AI_INTENTION_IDLE);
  687. // Check if the thinking action is already in progress
  688. if (_thinking || _actor.isCastingNow() || _actor.isAllSkillsDisabled())
  689. {
  690. return;
  691. }
  692. // Start thinking action
  693. _thinking = true;
  694. try
  695. {
  696. // Manage AI thinks of a L2Attackable
  697. if (getIntention() == AI_INTENTION_ACTIVE)
  698. {
  699. thinkActive();
  700. }
  701. else if (getIntention() == AI_INTENTION_ATTACK)
  702. {
  703. thinkAttack();
  704. }
  705. }
  706. finally
  707. {
  708. // Stop thinking action
  709. _thinking = false;
  710. }
  711. }
  712. /**
  713. * Launch actions corresponding to the Event Attacked.<br>
  714. * <B><U> Actions</U> :</B>
  715. * <ul>
  716. * <li>Init the attack : Calculate the attack timeout, Set the _globalAggro to 0, Add the attacker to the actor _aggroList</li>
  717. * <li>Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance</li>
  718. * <li>Set the Intention to AI_INTENTION_ATTACK</li>
  719. * </ul>
  720. * @param attacker The L2Character that attacks the actor
  721. */
  722. @Override
  723. protected void onEvtAttacked(L2Character attacker)
  724. {
  725. // Calculate the attack timeout
  726. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getInstance().getGameTicks();
  727. // Set the _globalAggro to 0 to permit attack even just after spawn
  728. if (_globalAggro < 0)
  729. {
  730. _globalAggro = 0;
  731. }
  732. // Add the attacker to the _aggroList of the actor
  733. ((L2Attackable) _actor).addDamageHate(attacker, 0, 1);
  734. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  735. if (!_actor.isRunning())
  736. {
  737. _actor.setRunning();
  738. }
  739. // Set the Intention to AI_INTENTION_ATTACK
  740. if (getIntention() != AI_INTENTION_ATTACK)
  741. {
  742. setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker, null);
  743. }
  744. super.onEvtAttacked(attacker);
  745. }
  746. /**
  747. * Launch actions corresponding to the Event Aggression.<br>
  748. * <B><U> Actions</U> :</B>
  749. * <ul>
  750. * <li>Add the target to the actor _aggroList or update hate if already present</li>
  751. * <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>
  752. * </ul>
  753. * @param aggro The value of hate to add to the actor against the target
  754. */
  755. @Override
  756. protected void onEvtAggression(L2Character target, int aggro)
  757. {
  758. if (_actor == null)
  759. {
  760. return;
  761. }
  762. L2Attackable me = (L2Attackable) _actor;
  763. if (target != null)
  764. {
  765. // Add the target to the actor _aggroList or update hate if already present
  766. me.addDamageHate(target, 0, aggro);
  767. // Get the hate of the actor against the target
  768. aggro = me.getHating(target);
  769. if (aggro <= 0)
  770. {
  771. if (me.getMostHated() == null)
  772. {
  773. _globalAggro = -25;
  774. me.clearAggroList();
  775. setIntention(AI_INTENTION_IDLE, null, null);
  776. }
  777. return;
  778. }
  779. // Set the actor AI Intention to AI_INTENTION_ATTACK
  780. if (getIntention() != CtrlIntention.AI_INTENTION_ATTACK)
  781. {
  782. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  783. if (!_actor.isRunning())
  784. {
  785. _actor.setRunning();
  786. }
  787. L2DefenderInstance sGuard = (L2DefenderInstance) _actor;
  788. double homeX = target.getX() - sGuard.getSpawn().getX();
  789. double homeY = target.getY() - sGuard.getSpawn().getY();
  790. // Check if the L2SiegeGuardInstance is not too far from its home location
  791. if (((homeX * homeX) + (homeY * homeY)) < 3240000)
  792. {
  793. setIntention(CtrlIntention.AI_INTENTION_ATTACK, target, null);
  794. }
  795. }
  796. }
  797. else
  798. {
  799. // currently only for setting lower general aggro
  800. if (aggro >= 0)
  801. {
  802. return;
  803. }
  804. L2Character mostHated = me.getMostHated();
  805. if (mostHated == null)
  806. {
  807. _globalAggro = -25;
  808. return;
  809. }
  810. for (L2Character aggroed : me.getAggroList().keySet())
  811. {
  812. me.addDamageHate(aggroed, 0, aggro);
  813. }
  814. aggro = me.getHating(mostHated);
  815. if (aggro <= 0)
  816. {
  817. _globalAggro = -25;
  818. me.clearAggroList();
  819. setIntention(AI_INTENTION_IDLE, null, null);
  820. }
  821. }
  822. }
  823. @Override
  824. public void stopAITask()
  825. {
  826. if (_aiTask != null)
  827. {
  828. _aiTask.cancel(false);
  829. _aiTask = null;
  830. }
  831. _accessor.detachAI();
  832. super.stopAITask();
  833. }
  834. }