L2SiegeGuardAI.java 28 KB

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