L2FortSiegeGuardAI.java 29 KB

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