L2FortSiegeGuardAI.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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.L2Effect;
  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.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.getGameTicks()));
  337. }
  338. if (_attackTimeout < GameTimeController.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.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.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.getPlanDistanceSq(attackTarget.getX(), attackTarget.getY());
  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.getSkillType() == L2SkillType.HEAL))
  549. {
  550. boolean useSkillSelf = true;
  551. if ((sk.getSkillType() == L2SkillType.HEAL) && (_actor.getCurrentHp() > (int) (_actor.getMaxHp() / 1.5)))
  552. {
  553. useSkillSelf = false;
  554. break;
  555. }
  556. if (sk.getSkillType() == L2SkillType.BUFF)
  557. {
  558. L2Effect[] effects = _actor.getAllEffects();
  559. for (int i = 0; (effects != null) && (i < effects.length); i++)
  560. {
  561. L2Effect effect = effects[i];
  562. if (effect.getSkill() == sk)
  563. {
  564. useSkillSelf = false;
  565. break;
  566. }
  567. }
  568. }
  569. if (useSkillSelf)
  570. {
  571. _actor.setTarget(_actor);
  572. }
  573. }
  574. clientStopMoving(null);
  575. _accessor.doCast(sk);
  576. _actor.setTarget(OldTarget);
  577. return;
  578. }
  579. }
  580. // Check if the L2SiegeGuardInstance is attacking, knows the target and can't run
  581. if (!(_actor.isAttackingNow()) && (_actor.getRunSpeed() == 0) && (_actor.getKnownList().knowsObject(attackTarget)))
  582. {
  583. // Cancel the target
  584. _actor.getKnownList().removeKnownObject(attackTarget);
  585. _actor.setTarget(null);
  586. setIntention(AI_INTENTION_IDLE, null, null);
  587. }
  588. else
  589. {
  590. double dx = _actor.getX() - attackTarget.getX();
  591. double dy = _actor.getY() - attackTarget.getY();
  592. double dz = _actor.getZ() - attackTarget.getZ();
  593. double homeX = attackTarget.getX() - sGuard.getSpawn().getLocx();
  594. double homeY = attackTarget.getY() - sGuard.getSpawn().getLocy();
  595. // Check if the L2SiegeGuardInstance isn't too far from it's home location
  596. if ((((dx * dx) + (dy * dy)) > 10000) && (((homeX * homeX) + (homeY * homeY)) > 3240000) // 1800 * 1800
  597. && (_actor.getKnownList().knowsObject(attackTarget)))
  598. {
  599. // Cancel the target
  600. _actor.getKnownList().removeKnownObject(attackTarget);
  601. _actor.setTarget(null);
  602. setIntention(AI_INTENTION_IDLE, null, null);
  603. }
  604. else
  605. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  606. {
  607. // Temporary hack for preventing guards jumping off towers,
  608. // before replacing this with effective geodata checks and AI modification
  609. if ((dz * dz) < (170 * 170)) // normally 130 if guard z coordinates correct
  610. {
  611. if (_selfAnalysis.isMage)
  612. {
  613. range = _selfAnalysis.maxCastRange - 50;
  614. }
  615. if (_actor.getWalkSpeed() <= 0)
  616. {
  617. return;
  618. }
  619. if (attackTarget.isMoving())
  620. {
  621. moveToPawn(attackTarget, range - 70);
  622. }
  623. else
  624. {
  625. moveToPawn(attackTarget, range);
  626. }
  627. }
  628. }
  629. }
  630. return;
  631. }
  632. // Else, if the actor is muted and far from target, just "move to pawn"
  633. else if (_actor.isMuted() && (dist_2 > (range * range)))
  634. {
  635. // Temporary hack for preventing guards jumping off towers,
  636. // before replacing this with effective geodata checks and AI modification
  637. double dz = _actor.getZ() - attackTarget.getZ();
  638. if ((dz * dz) < (170 * 170)) // normally 130 if guard z coordinates correct
  639. {
  640. if (_selfAnalysis.isMage)
  641. {
  642. range = _selfAnalysis.maxCastRange - 50;
  643. }
  644. if (_actor.getWalkSpeed() <= 0)
  645. {
  646. return;
  647. }
  648. if (attackTarget.isMoving())
  649. {
  650. moveToPawn(attackTarget, range - 70);
  651. }
  652. else
  653. {
  654. moveToPawn(attackTarget, range);
  655. }
  656. }
  657. return;
  658. }
  659. // Else, if this is close enough to attack
  660. else if (dist_2 <= (range * range))
  661. {
  662. // Force mobs to attack anybody if confused
  663. L2Character hated = null;
  664. if (_actor.isConfused())
  665. {
  666. hated = attackTarget;
  667. }
  668. else
  669. {
  670. hated = ((L2Attackable) _actor).getMostHated();
  671. }
  672. if (hated == null)
  673. {
  674. setIntention(AI_INTENTION_ACTIVE, null, null);
  675. return;
  676. }
  677. if (hated != attackTarget)
  678. {
  679. attackTarget = hated;
  680. }
  681. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  682. // check for close combat skills && heal/buff skills
  683. if (!_actor.isMuted() && (Rnd.nextInt(100) <= 5))
  684. {
  685. for (L2Skill sk : skills)
  686. {
  687. int castRange = sk.getCastRange();
  688. if (((castRange * castRange) >= dist_2) && !sk.isPassive() && (_actor.getCurrentMp() >= _actor.getStat().getMpConsume(sk)) && !_actor.isSkillDisabled(sk))
  689. {
  690. L2Object OldTarget = _actor.getTarget();
  691. if ((sk.getSkillType() == L2SkillType.BUFF) || (sk.getSkillType() == L2SkillType.HEAL))
  692. {
  693. boolean useSkillSelf = true;
  694. if ((sk.getSkillType() == L2SkillType.HEAL) && (_actor.getCurrentHp() > (int) (_actor.getMaxHp() / 1.5)))
  695. {
  696. useSkillSelf = false;
  697. break;
  698. }
  699. if (sk.getSkillType() == L2SkillType.BUFF)
  700. {
  701. L2Effect[] effects = _actor.getAllEffects();
  702. for (int i = 0; (effects != null) && (i < effects.length); i++)
  703. {
  704. L2Effect effect = effects[i];
  705. if (effect.getSkill() == sk)
  706. {
  707. useSkillSelf = false;
  708. break;
  709. }
  710. }
  711. }
  712. if (useSkillSelf)
  713. {
  714. _actor.setTarget(_actor);
  715. }
  716. }
  717. clientStopMoving(null);
  718. _accessor.doCast(sk);
  719. _actor.setTarget(OldTarget);
  720. return;
  721. }
  722. }
  723. }
  724. // Finally, do the physical attack itself
  725. _accessor.doAttack(attackTarget);
  726. }
  727. }
  728. /**
  729. * Manage AI thinking actions of a L2Attackable.
  730. */
  731. @Override
  732. protected void onEvtThink()
  733. {
  734. // if(getIntention() != AI_INTENTION_IDLE && (!_actor.isVisible() || !_actor.hasAI() || !_actor.isKnownPlayers()))
  735. // setIntention(AI_INTENTION_IDLE);
  736. // Check if the actor can't use skills and if a thinking action isn't already in progress
  737. if (_thinking || _actor.isCastingNow() || _actor.isAllSkillsDisabled())
  738. {
  739. return;
  740. }
  741. // Start thinking action
  742. _thinking = true;
  743. try
  744. {
  745. // Manage AI thinks of a L2Attackable
  746. if (getIntention() == AI_INTENTION_ACTIVE)
  747. {
  748. thinkActive();
  749. }
  750. else if (getIntention() == AI_INTENTION_ATTACK)
  751. {
  752. thinkAttack();
  753. }
  754. }
  755. finally
  756. {
  757. // Stop thinking action
  758. _thinking = false;
  759. }
  760. }
  761. /**
  762. * Launch actions corresponding to the Event Attacked.<br>
  763. * <B><U> Actions</U> :</B>
  764. * <ul>
  765. * <li>Init the attack : Calculate the attack timeout, Set the _globalAggro to 0, Add the attacker to the actor _aggroList</li>
  766. * <li>Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance</li>
  767. * <li>Set the Intention to AI_INTENTION_ATTACK</li>
  768. * </ul>
  769. * @param attacker The L2Character that attacks the actor
  770. */
  771. @Override
  772. protected void onEvtAttacked(L2Character attacker)
  773. {
  774. // Calculate the attack timeout
  775. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  776. // Set the _globalAggro to 0 to permit attack even just after spawn
  777. if (_globalAggro < 0)
  778. {
  779. _globalAggro = 0;
  780. }
  781. // Add the attacker to the _aggroList of the actor
  782. ((L2Attackable) _actor).addDamageHate(attacker, 0, 1);
  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. // Set the Intention to AI_INTENTION_ATTACK
  789. if (getIntention() != AI_INTENTION_ATTACK)
  790. {
  791. setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker, null);
  792. }
  793. super.onEvtAttacked(attacker);
  794. }
  795. /**
  796. * Launch actions corresponding to the Event Aggression.<br>
  797. * <B><U> Actions</U> :</B>
  798. * <ul>
  799. * <li>Add the target to the actor _aggroList or update hate if already present</li>
  800. * <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>
  801. * </ul>
  802. * @param aggro The value of hate to add to the actor against the target
  803. */
  804. @Override
  805. protected void onEvtAggression(L2Character target, int aggro)
  806. {
  807. if (_actor == null)
  808. {
  809. return;
  810. }
  811. L2Attackable me = (L2Attackable) _actor;
  812. if (target != null)
  813. {
  814. // Add the target to the actor _aggroList or update hate if already present
  815. me.addDamageHate(target, 0, aggro);
  816. // Get the hate of the actor against the target
  817. aggro = me.getHating(target);
  818. if (aggro <= 0)
  819. {
  820. if (me.getMostHated() == null)
  821. {
  822. _globalAggro = -25;
  823. me.clearAggroList();
  824. setIntention(AI_INTENTION_IDLE, null, null);
  825. }
  826. return;
  827. }
  828. // Set the actor AI Intention to AI_INTENTION_ATTACK
  829. if (getIntention() != CtrlIntention.AI_INTENTION_ATTACK)
  830. {
  831. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  832. if (!_actor.isRunning())
  833. {
  834. _actor.setRunning();
  835. }
  836. L2DefenderInstance sGuard;
  837. if (_actor instanceof L2FortCommanderInstance)
  838. {
  839. sGuard = (L2FortCommanderInstance) _actor;
  840. }
  841. else
  842. {
  843. sGuard = (L2DefenderInstance) _actor;
  844. }
  845. double homeX = target.getX() - sGuard.getSpawn().getLocx();
  846. double homeY = target.getY() - sGuard.getSpawn().getLocy();
  847. // Check if the L2SiegeGuardInstance is not too far from its home location
  848. if (((homeX * homeX) + (homeY * homeY)) < 3240000)
  849. {
  850. setIntention(CtrlIntention.AI_INTENTION_ATTACK, target, null);
  851. }
  852. }
  853. }
  854. else
  855. {
  856. // currently only for setting lower general aggro
  857. if (aggro >= 0)
  858. {
  859. return;
  860. }
  861. L2Character mostHated = me.getMostHated();
  862. if (mostHated == null)
  863. {
  864. _globalAggro = -25;
  865. return;
  866. }
  867. for (L2Character aggroed : me.getAggroList().keySet())
  868. {
  869. me.addDamageHate(aggroed, 0, aggro);
  870. }
  871. aggro = me.getHating(mostHated);
  872. if (aggro <= 0)
  873. {
  874. _globalAggro = -25;
  875. me.clearAggroList();
  876. setIntention(AI_INTENTION_IDLE, null, null);
  877. }
  878. }
  879. }
  880. @Override
  881. public void stopAITask()
  882. {
  883. if (_aiTask != null)
  884. {
  885. _aiTask.cancel(false);
  886. _aiTask = null;
  887. }
  888. _accessor.detachAI();
  889. super.stopAITask();
  890. }
  891. }