L2SiegeGuardAI.java 28 KB

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