L2FortSiegeGuardAI.java 29 KB

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