L2FortSiegeGuardAI.java 29 KB

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