L2FortSiegeGuardAI.java 30 KB

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