L2FortSiegeGuardAI.java 30 KB

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