L2FortSiegeGuardAI.java 29 KB

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