L2SiegeGuardAI.java 29 KB

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