L2FortSiegeGuardAI.java 29 KB

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