L2SiegeGuardAI.java 29 KB

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