L2SiegeGuardAI.java 35 KB

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