L2FortSiegeGuardAI.java 30 KB

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