L2SiegeGuardAI.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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 net.sf.l2j.gameserver.ai;
  16. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
  17. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  18. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  19. import java.util.concurrent.Future;
  20. import net.sf.l2j.Config;
  21. import net.sf.l2j.gameserver.GameTimeController;
  22. import net.sf.l2j.gameserver.GeoData;
  23. import net.sf.l2j.gameserver.ThreadPoolManager;
  24. import net.sf.l2j.gameserver.model.L2Attackable;
  25. import net.sf.l2j.gameserver.model.L2Character;
  26. import net.sf.l2j.gameserver.model.L2Effect;
  27. import net.sf.l2j.gameserver.model.L2Object;
  28. import net.sf.l2j.gameserver.model.L2Skill;
  29. import net.sf.l2j.gameserver.model.L2Summon;
  30. import net.sf.l2j.gameserver.model.actor.instance.L2CommanderInstance;
  31. import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
  32. import net.sf.l2j.gameserver.model.actor.instance.L2FolkInstance;
  33. import net.sf.l2j.gameserver.model.actor.instance.L2FortMerchantInstance;
  34. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  35. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  36. import net.sf.l2j.gameserver.model.actor.instance.L2PlayableInstance;
  37. import net.sf.l2j.gameserver.model.actor.instance.L2SiegeGuardInstance;
  38. import net.sf.l2j.gameserver.templates.L2SkillType;
  39. import net.sf.l2j.gameserver.util.Util;
  40. import net.sf.l2j.util.Rnd;
  41. /**
  42. * This class manages AI of L2Attackable.<BR><BR>
  43. *
  44. */
  45. public class L2SiegeGuardAI extends L2CharacterAI implements Runnable
  46. {
  47. //protected static final Logger _log = Logger.getLogger(L2SiegeGuardAI.class.getName());
  48. private static final int MAX_ATTACK_TIMEOUT = 300; // int ticks, i.e. 30 seconds
  49. /** The L2Attackable AI task executed every 1s (call onEvtThink method)*/
  50. private Future<?> _aiTask;
  51. /** For attack AI, analysis of mob and its targets */
  52. private SelfAnalysis _selfAnalysis = new SelfAnalysis();
  53. //private TargetAnalysis _mostHatedAnalysis = new TargetAnalysis();
  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 L2SiegeGuardAI(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 L2SiegeGuardInstance || target instanceof L2CommanderInstance || target instanceof L2FortMerchantInstance || target instanceof L2FolkInstance || target instanceof L2DoorInstance
  116. || target.isAlikeDead())
  117. return false;
  118. // Check if the target isn't invulnerable
  119. if (target.isInvul())
  120. {
  121. // However EffectInvincible requires to check GMs specially
  122. if (target instanceof L2PcInstance && ((L2PcInstance) target).isGM())
  123. return false;
  124. if (target instanceof L2Summon && ((L2Summon) target).getOwner().isGM())
  125. return false;
  126. }
  127. // Get the owner if the target is a summon
  128. if (target instanceof L2Summon)
  129. {
  130. L2PcInstance owner = ((L2Summon) target).getOwner();
  131. if (_actor.isInsideRadius(owner, 1000, true, false))
  132. target = owner;
  133. }
  134. // Check if the target is a L2PcInstance
  135. if (target instanceof L2PlayableInstance)
  136. {
  137. // Check if the target isn't in silent move mode AND too far (>100)
  138. if (((L2PlayableInstance) target).isSilentMoving() && !_actor.isInsideRadius(target, 250, false, false))
  139. return false;
  140. }
  141. // Los Check Here
  142. return (_actor.isAutoAttackable(target) && GeoData.getInstance().canSeeTarget(_actor, target));
  143. }
  144. /**
  145. * Set the Intention of this L2CharacterAI and create an AI Task executed every 1s (call onEvtThink method) for this L2Attackable.<BR><BR>
  146. *
  147. * <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>
  148. *
  149. * @param intention The new Intention to set to the AI
  150. * @param arg0 The first parameter of the Intention
  151. * @param arg1 The second parameter of the Intention
  152. *
  153. */
  154. @Override
  155. synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
  156. {
  157. if (Config.DEBUG)
  158. _log.info("L2SiegeAI.changeIntention(" + intention + ", " + arg0 + ", " + arg1 + ")");
  159. if (intention == AI_INTENTION_IDLE /*|| intention == AI_INTENTION_ACTIVE*/) // active becomes idle if only a summon is present
  160. {
  161. // Check if actor is not dead
  162. if (!_actor.isAlikeDead())
  163. {
  164. L2Attackable npc = (L2Attackable) _actor;
  165. // If its _knownPlayer isn't empty set the Intention to AI_INTENTION_ACTIVE
  166. if (npc.getKnownList().getKnownPlayers().size() > 0)
  167. intention = AI_INTENTION_ACTIVE;
  168. else
  169. intention = AI_INTENTION_IDLE;
  170. }
  171. if (intention == AI_INTENTION_IDLE)
  172. {
  173. // Set the Intention of this L2AttackableAI to AI_INTENTION_IDLE
  174. super.changeIntention(AI_INTENTION_IDLE, null, null);
  175. // Stop AI task and detach AI from NPC
  176. if (_aiTask != null)
  177. {
  178. _aiTask.cancel(true);
  179. _aiTask = null;
  180. }
  181. // Cancel the AI
  182. _accessor.detachAI();
  183. return;
  184. }
  185. }
  186. // Set the Intention of this L2AttackableAI to intention
  187. super.changeIntention(intention, arg0, arg1);
  188. // If not idle - create an AI task (schedule onEvtThink repeatedly)
  189. if (_aiTask == null)
  190. {
  191. _aiTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(this, 1000, 1000);
  192. }
  193. }
  194. /**
  195. * Manage the Attack Intention : Stop current Attack (if necessary), Calculate attack timeout, Start a new Attack and Launch Think Event.<BR><BR>
  196. *
  197. * @param target The L2Character to attack
  198. *
  199. */
  200. @Override
  201. protected void onIntentionAttack(L2Character target)
  202. {
  203. // Calculate the attack timeout
  204. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  205. // Manage the Attack Intention : Stop current Attack (if necessary), Start a new Attack and Launch Think Event
  206. //if (_actor.getTarget() != null)
  207. super.onIntentionAttack(target);
  208. }
  209. /**
  210. * Manage AI standard thinks of a L2Attackable (called by onEvtThink).<BR><BR>
  211. *
  212. * <B><U> Actions</U> :</B><BR><BR>
  213. * <li>Update every 1s the _globalAggro counter to come close to 0</li>
  214. * <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>
  215. * <li>If the actor can't attack, order to it to return to its home location</li>
  216. *
  217. */
  218. private void thinkActive()
  219. {
  220. L2Attackable npc = (L2Attackable) _actor;
  221. // Update every 1s the _globalAggro counter to come close to 0
  222. if (_globalAggro != 0)
  223. {
  224. if (_globalAggro < 0)
  225. _globalAggro++;
  226. else
  227. _globalAggro--;
  228. }
  229. // Add all autoAttackable L2Character in L2Attackable Aggro Range to its _aggroList with 0 damage and 1 hate
  230. // A L2Attackable isn't aggressive during 10s after its spawn because _globalAggro is set to -10
  231. if (_globalAggro >= 0)
  232. {
  233. for (L2Character target : npc.getKnownList().getKnownCharactersInRadius(_attackRange))
  234. {
  235. if (target == null)
  236. continue;
  237. if (autoAttackCondition(target)) // check aggression
  238. {
  239. // Get the hate level of the L2Attackable against this L2Character target contained in _aggroList
  240. int hating = npc.getHating(target);
  241. // Add the attacker to the L2Attackable _aggroList with 0 damage and 1 hate
  242. if (hating == 0)
  243. npc.addDamageHate(target, 0, 1);
  244. }
  245. }
  246. // Chose a target from its aggroList
  247. L2Character hated;
  248. if (_actor.isConfused())
  249. hated = getAttackTarget(); // Force mobs to attack anybody if confused
  250. else
  251. hated = npc.getMostHated();
  252. //_mostHatedAnalysis.Update(hated);
  253. // Order to the L2Attackable to attack the target
  254. if (hated != null)
  255. {
  256. // Get the hate level of the L2Attackable against this L2Character target contained in _aggroList
  257. int aggro = npc.getHating(hated);
  258. if (aggro + _globalAggro > 0)
  259. {
  260. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  261. if (!_actor.isRunning())
  262. _actor.setRunning();
  263. // Set the AI Intention to AI_INTENTION_ATTACK
  264. setIntention(CtrlIntention.AI_INTENTION_ATTACK, hated, null);
  265. }
  266. return;
  267. }
  268. }
  269. // Order to the L2SiegeGuardInstance to return to its home location because there's no target to attack
  270. ((L2SiegeGuardInstance) _actor).returnHome();
  271. return;
  272. }
  273. /**
  274. * Manage AI attack thinks of a L2Attackable (called by onEvtThink).<BR><BR>
  275. *
  276. * <B><U> Actions</U> :</B><BR><BR>
  277. * <li>Update the attack timeout if actor is running</li>
  278. * <li>If target is dead or timeout is expired, stop this attack and set the Intention to AI_INTENTION_ACTIVE</li>
  279. * <li>Call all L2Object of its Faction inside the Faction Range</li>
  280. * <li>Chose a target and order to attack it with magic skill or physical attack</li><BR><BR>
  281. *
  282. * TODO: Manage casting rules to healer mobs (like Ant Nurses)
  283. *
  284. */
  285. private void thinkAttack()
  286. {
  287. if (Config.DEBUG)
  288. _log.info("L2SiegeGuardAI.thinkAttack(); timeout=" + (_attackTimeout - GameTimeController.getGameTicks()));
  289. if (_attackTimeout < GameTimeController.getGameTicks())
  290. {
  291. // Check if the actor is running
  292. if (_actor.isRunning())
  293. {
  294. // Set the actor movement type to walk and send Server->Client packet ChangeMoveType to all others L2PcInstance
  295. _actor.setWalking();
  296. // Calculate a new attack timeout
  297. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  298. }
  299. }
  300. L2Character attackTarget = getAttackTarget();
  301. // Check if target is dead or if timeout is expired to stop this attack
  302. if (attackTarget == null || attackTarget.isAlikeDead() || _attackTimeout < GameTimeController.getGameTicks())
  303. {
  304. // Stop hating this target after the attack timeout or if target is dead
  305. if (attackTarget != null)
  306. {
  307. L2Attackable npc = (L2Attackable) _actor;
  308. npc.stopHating(attackTarget);
  309. }
  310. // Cancel target and timeout
  311. _attackTimeout = Integer.MAX_VALUE;
  312. setAttackTarget(null);
  313. // Set the AI Intention to AI_INTENTION_ACTIVE
  314. setIntention(AI_INTENTION_ACTIVE, null, null);
  315. _actor.setWalking();
  316. return;
  317. }
  318. factionNotifyAndSupport();
  319. attackPrepare();
  320. }
  321. private final void factionNotifyAndSupport()
  322. {
  323. L2Character target = getAttackTarget();
  324. // Call all L2Object of its Faction inside the Faction Range
  325. if (((L2NpcInstance) _actor).getFactionId() == null || target == null)
  326. return;
  327. if (target.isInvul())
  328. return; // speeding it up for siege guards
  329. if (Rnd.get(10) > 4) return; // test for reducing CPU load
  330. String faction_id = ((L2NpcInstance) _actor).getFactionId();
  331. // Go through all L2Character that belong to its faction
  332. //for (L2Character cha : _actor.getKnownList().getKnownCharactersInRadius(((L2NpcInstance) _actor).getFactionRange()+_actor.getTemplate().collisionRadius))
  333. for (L2Character cha : _actor.getKnownList().getKnownCharactersInRadius(1000))
  334. {
  335. if (cha == null)
  336. continue;
  337. if (!(cha instanceof L2NpcInstance))
  338. {
  339. if (_selfAnalysis.hasHealOrResurrect && cha instanceof L2PcInstance && ((L2NpcInstance) _actor).getCastle().getSiege().checkIsDefender(((L2PcInstance) cha).getClan()))
  340. {
  341. // heal friends
  342. if (!_actor.isAttackingDisabled() && cha.getCurrentHp() < cha.getMaxHp() * 0.6 && _actor.getCurrentHp() > _actor.getMaxHp() / 2 && _actor.getCurrentMp() > _actor.getMaxMp() / 2 && cha.isInCombat())
  343. {
  344. for (L2Skill sk : _selfAnalysis.healSkills)
  345. {
  346. if (_actor.getCurrentMp() < sk.getMpConsume())
  347. continue;
  348. if (_actor.isSkillDisabled(sk.getId()))
  349. continue;
  350. if (!Util.checkIfInRange(sk.getCastRange(), _actor, cha, true))
  351. continue;
  352. int chance = 5;
  353. if (chance >= Rnd.get(100)) // chance
  354. continue;
  355. if (!GeoData.getInstance().canSeeTarget(_actor, cha))
  356. break;
  357. L2Object OldTarget = _actor.getTarget();
  358. _actor.setTarget(cha);
  359. clientStopMoving(null);
  360. _accessor.doCast(sk);
  361. _actor.setTarget(OldTarget);
  362. return;
  363. }
  364. }
  365. }
  366. continue;
  367. }
  368. L2NpcInstance npc = (L2NpcInstance) cha;
  369. if (faction_id != npc.getFactionId())
  370. continue;
  371. if (npc.getAI() != null) // TODO: possibly check not needed
  372. {
  373. if (!npc.isDead() && Math.abs(target.getZ() - npc.getZ()) < 600
  374. //&& _actor.getAttackByList().contains(getAttackTarget())
  375. && (npc.getAI()._intention == CtrlIntention.AI_INTENTION_IDLE || npc.getAI()._intention == CtrlIntention.AI_INTENTION_ACTIVE)
  376. //limiting aggro for siege guards
  377. && target.isInsideRadius(npc, 1500, true, false) && GeoData.getInstance().canSeeTarget(npc, target))
  378. {
  379. // Notify the L2Object AI with EVT_AGGRESSION
  380. npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, getAttackTarget(), 1);
  381. }
  382. // heal friends
  383. if (_selfAnalysis.hasHealOrResurrect && !_actor.isAttackingDisabled() && npc.getCurrentHp() < npc.getMaxHp() * 0.6 && _actor.getCurrentHp() > _actor.getMaxHp() / 2 && _actor.getCurrentMp() > _actor.getMaxMp() / 2
  384. && npc.isInCombat())
  385. {
  386. for (L2Skill sk : _selfAnalysis.healSkills)
  387. {
  388. if (_actor.getCurrentMp() < sk.getMpConsume())
  389. continue;
  390. if (_actor.isSkillDisabled(sk.getId()))
  391. continue;
  392. if (!Util.checkIfInRange(sk.getCastRange(), _actor, npc, true))
  393. continue;
  394. int chance = 4;
  395. if (chance >= Rnd.get(100)) // chance
  396. continue;
  397. if (!GeoData.getInstance().canSeeTarget(_actor, npc))
  398. break;
  399. L2Object OldTarget = _actor.getTarget();
  400. _actor.setTarget(npc);
  401. clientStopMoving(null);
  402. _accessor.doCast(sk);
  403. _actor.setTarget(OldTarget);
  404. return;
  405. }
  406. }
  407. }
  408. }
  409. }
  410. private void attackPrepare()
  411. {
  412. // Get all information needed to choose between physical or magical attack
  413. L2Skill[] skills = null;
  414. double dist_2 = 0;
  415. int range = 0;
  416. L2SiegeGuardInstance sGuard = (L2SiegeGuardInstance) _actor;
  417. L2Character attackTarget = getAttackTarget();
  418. try
  419. {
  420. _actor.setTarget(attackTarget);
  421. skills = _actor.getAllSkills();
  422. dist_2 = _actor.getPlanDistanceSq(attackTarget.getX(), attackTarget.getY());
  423. range = _actor.getPhysicalAttackRange() + _actor.getTemplate().collisionRadius + attackTarget.getTemplate().collisionRadius;
  424. if (attackTarget.isMoving())
  425. range += 50;
  426. }
  427. catch (NullPointerException e)
  428. {
  429. //_log.warning("AttackableAI: Attack target is NULL.");
  430. _actor.setTarget(null);
  431. setIntention(AI_INTENTION_IDLE, null, null);
  432. return;
  433. }
  434. // never attack defenders
  435. if (attackTarget instanceof L2PcInstance && sGuard.getCastle().getSiege().checkIsDefender(((L2PcInstance) attackTarget).getClan()))
  436. {
  437. // Cancel the target
  438. sGuard.stopHating(attackTarget);
  439. _actor.setTarget(null);
  440. setIntention(AI_INTENTION_IDLE, null, null);
  441. return;
  442. }
  443. if (!GeoData.getInstance().canSeeTarget(_actor, attackTarget))
  444. {
  445. // Siege guards differ from normal mobs currently:
  446. // If target cannot seen, don't attack any more
  447. sGuard.stopHating(attackTarget);
  448. _actor.setTarget(null);
  449. setIntention(AI_INTENTION_IDLE, null, null);
  450. return;
  451. }
  452. // Check if the actor isn't muted and if it is far from target
  453. if (!_actor.isMuted() && dist_2 > range * range)
  454. {
  455. // check for long ranged skills and heal/buff skills
  456. for (L2Skill sk : skills)
  457. {
  458. int castRange = sk.getCastRange();
  459. if ((dist_2 <= castRange * castRange) && castRange > 70 && !_actor.isSkillDisabled(sk.getId()) && _actor.getCurrentMp() >= _actor.getStat().getMpConsume(sk) && !sk.isPassive())
  460. {
  461. L2Object OldTarget = _actor.getTarget();
  462. if (sk.getSkillType() == L2SkillType.BUFF || sk.getSkillType() == L2SkillType.HEAL)
  463. {
  464. boolean useSkillSelf = true;
  465. if (sk.getSkillType() == L2SkillType.HEAL && _actor.getCurrentHp() > (int) (_actor.getMaxHp() / 1.5))
  466. {
  467. useSkillSelf = false;
  468. break;
  469. }
  470. if (sk.getSkillType() == L2SkillType.BUFF)
  471. {
  472. L2Effect[] effects = _actor.getAllEffects();
  473. for (int i = 0; effects != null && i < effects.length; i++)
  474. {
  475. L2Effect effect = effects[i];
  476. if (effect.getSkill() == sk)
  477. {
  478. useSkillSelf = false;
  479. break;
  480. }
  481. }
  482. }
  483. if (useSkillSelf)
  484. _actor.setTarget(_actor);
  485. }
  486. clientStopMoving(null);
  487. _accessor.doCast(sk);
  488. _actor.setTarget(OldTarget);
  489. return;
  490. }
  491. }
  492. // Check if the L2SiegeGuardInstance is attacking, knows the target and can't run
  493. if (!(_actor.isAttackingNow()) && (_actor.getRunSpeed() == 0) && (_actor.getKnownList().knowsObject(attackTarget)))
  494. {
  495. // Cancel the target
  496. _actor.getKnownList().removeKnownObject(attackTarget);
  497. _actor.setTarget(null);
  498. setIntention(AI_INTENTION_IDLE, null, null);
  499. }
  500. else
  501. {
  502. double dx = _actor.getX() - attackTarget.getX();
  503. double dy = _actor.getY() - attackTarget.getY();
  504. double dz = _actor.getZ() - attackTarget.getZ();
  505. double homeX = attackTarget.getX() - sGuard.getSpawn().getLocx();
  506. double homeY = attackTarget.getY() - sGuard.getSpawn().getLocy();
  507. // Check if the L2SiegeGuardInstance isn't too far from it's home location
  508. if ((dx * dx + dy * dy > 10000) && (homeX * homeX + homeY * homeY > 3240000) // 1800 * 1800
  509. && (_actor.getKnownList().knowsObject(attackTarget)))
  510. {
  511. // Cancel the target
  512. _actor.getKnownList().removeKnownObject(attackTarget);
  513. _actor.setTarget(null);
  514. setIntention(AI_INTENTION_IDLE, null, null);
  515. }
  516. else
  517. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  518. {
  519. // Temporary hack for preventing guards jumping off towers,
  520. // before replacing this with effective geodata checks and AI modification
  521. if (dz * dz < 170 * 170) // normally 130 if guard z coordinates correct
  522. {
  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)
  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.getId()))
  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. _accessor.doAttack(attackTarget);
  610. }
  611. }
  612. /**
  613. * Manage AI thinking actions of a L2Attackable.<BR><BR>
  614. */
  615. @Override
  616. protected void onEvtThink()
  617. {
  618. // if(getIntention() != AI_INTENTION_IDLE && (!_actor.isVisible() || !_actor.hasAI() || !_actor.isKnownPlayers()))
  619. // setIntention(AI_INTENTION_IDLE);
  620. // Check if the thinking action is already in progress
  621. if (_thinking || _actor.isCastingNow() || _actor.isAllSkillsDisabled())
  622. return;
  623. // Start thinking action
  624. _thinking = true;
  625. try
  626. {
  627. // Manage AI thinks of a L2Attackable
  628. if (getIntention() == AI_INTENTION_ACTIVE)
  629. thinkActive();
  630. else if (getIntention() == AI_INTENTION_ATTACK)
  631. thinkAttack();
  632. }
  633. finally
  634. {
  635. // Stop thinking action
  636. _thinking = false;
  637. }
  638. }
  639. /**
  640. * Launch actions corresponding to the Event Attacked.<BR><BR>
  641. *
  642. * <B><U> Actions</U> :</B><BR><BR>
  643. * <li>Init the attack : Calculate the attack timeout, Set the _globalAggro to 0, Add the attacker to the actor _aggroList</li>
  644. * <li>Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance</li>
  645. * <li>Set the Intention to AI_INTENTION_ATTACK</li><BR><BR>
  646. *
  647. * @param attacker The L2Character that attacks the actor
  648. *
  649. */
  650. @Override
  651. protected void onEvtAttacked(L2Character attacker)
  652. {
  653. // Calculate the attack timeout
  654. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  655. // Set the _globalAggro to 0 to permit attack even just after spawn
  656. if (_globalAggro < 0)
  657. _globalAggro = 0;
  658. // Add the attacker to the _aggroList of the actor
  659. ((L2Attackable) _actor).addDamageHate(attacker, 0, 1);
  660. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  661. if (!_actor.isRunning())
  662. _actor.setRunning();
  663. // Set the Intention to AI_INTENTION_ATTACK
  664. if (getIntention() != AI_INTENTION_ATTACK)
  665. {
  666. setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker, null);
  667. }
  668. super.onEvtAttacked(attacker);
  669. }
  670. /**
  671. * Launch actions corresponding to the Event Aggression.<BR><BR>
  672. *
  673. * <B><U> Actions</U> :</B><BR><BR>
  674. * <li>Add the target to the actor _aggroList or update hate if already present </li>
  675. * <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>
  676. *
  677. * @param attacker The L2Character that attacks
  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. L2SiegeGuardInstance sGuard = (L2SiegeGuardInstance) _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. else
  729. for (L2Character aggroed : me.getAggroListRP().keySet())
  730. me.addDamageHate(aggroed, 0, aggro);
  731. aggro = me.getHating(mostHated);
  732. if (aggro <= 0)
  733. {
  734. _globalAggro = -25;
  735. me.clearAggroList();
  736. setIntention(AI_INTENTION_IDLE, null, null);
  737. }
  738. }
  739. }
  740. @Override
  741. protected void onEvtDead()
  742. {
  743. stopAITask();
  744. super.onEvtDead();
  745. }
  746. public void stopAITask()
  747. {
  748. if (_aiTask != null)
  749. {
  750. _aiTask.cancel(false);
  751. _aiTask = null;
  752. }
  753. _accessor.detachAI();
  754. }
  755. }