L2FortSiegeGuardAI.java 29 KB

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