Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 68.51 KB | None | 0 0
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2, or (at your option)
  5. * any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. * 02111-1307, USA.
  16. *
  17. * http://www.gnu.org/copyleft/gpl.html
  18. */
  19. package net.sf.l2j.gameserver.ai;
  20.  
  21. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
  22. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  23. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  24.  
  25. import java.util.List;
  26. import java.util.concurrent.Future;
  27.  
  28. import javolution.util.FastList;
  29.  
  30. import net.sf.l2j.Config;
  31. import net.sf.l2j.gameserver.GameTimeController;
  32. import net.sf.l2j.gameserver.GeoData;
  33. import net.sf.l2j.gameserver.Territory;
  34. import net.sf.l2j.gameserver.ThreadPoolManager;
  35. import net.sf.l2j.gameserver.instancemanager.DimensionalRiftManager;
  36. import net.sf.l2j.gameserver.model.L2Attackable;
  37. import net.sf.l2j.gameserver.model.L2CharPosition;
  38. import net.sf.l2j.gameserver.model.L2Character;
  39. import net.sf.l2j.gameserver.model.L2Object;
  40. import net.sf.l2j.gameserver.model.L2Skill;
  41. import net.sf.l2j.gameserver.model.L2Summon;
  42. import net.sf.l2j.gameserver.model.actor.instance.L2ChestInstance;
  43. import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
  44. import net.sf.l2j.gameserver.model.actor.instance.L2FestivalMonsterInstance;
  45. import net.sf.l2j.gameserver.model.actor.instance.L2FolkInstance;
  46. import net.sf.l2j.gameserver.model.actor.instance.L2FriendlyMobInstance;
  47. import net.sf.l2j.gameserver.model.actor.instance.L2GrandBossInstance;
  48. import net.sf.l2j.gameserver.model.actor.instance.L2GuardInstance;
  49. import net.sf.l2j.gameserver.model.actor.instance.L2MinionInstance;
  50. import net.sf.l2j.gameserver.model.actor.instance.L2MonsterInstance;
  51. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  52. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  53. import net.sf.l2j.gameserver.model.actor.instance.L2PlayableInstance;
  54. import net.sf.l2j.gameserver.model.actor.instance.L2RaidBossInstance;
  55. import net.sf.l2j.gameserver.model.actor.instance.L2RiftInvaderInstance;
  56. import net.sf.l2j.gameserver.model.actor.instance.L2SepulcherMonsterInstance;
  57. import net.sf.l2j.gameserver.taskmanager.DecayTaskManager;
  58. import net.sf.l2j.gameserver.model.quest.Quest;
  59. import net.sf.l2j.gameserver.util.Util;
  60. import net.sf.l2j.util.Rnd;
  61.  
  62. /**
  63. * This class manages AI of L2Attackable.<BR><BR>
  64. *
  65. */
  66. public class L2AttackableAI extends L2CharacterAI implements Runnable
  67. {
  68. //protected static final Logger _log = Logger.getLogger(L2AttackableAI.class.getName());
  69.  
  70. private static final int RANDOM_WALK_RATE = 30;
  71. // private static final int MAX_DRIFT_RANGE = 300;
  72. private static final int MAX_ATTACK_TIMEOUT = 1200; // int ticks, i.e. 2 minutes
  73.  
  74. /** The L2Attackable AI task executed every 1s (call onEvtThink method)*/
  75. private Future aiTask;
  76.  
  77. /** The delay after wich the attacked is stopped */
  78. private int _attackTimeout;
  79.  
  80. /** The L2Attackable aggro counter */
  81. private int _globalAggro;
  82.  
  83. /** The flag used to indicate that a thinking action is in progress */
  84. private boolean thinking; // to prevent recursive thinking
  85.  
  86. /** For attack AI, analysis of mob and its targets */
  87. private SelfAnalysis _selfAnalysis = new SelfAnalysis();
  88. private TargetAnalysis _mostHatedAnalysis = new TargetAnalysis();
  89. private TargetAnalysis _secondMostHatedAnalysis = new TargetAnalysis();
  90.  
  91. /**
  92. * Constructor of L2AttackableAI.<BR><BR>
  93. *
  94. * @param accessor The AI accessor of the L2Character
  95. *
  96. */
  97. public L2AttackableAI(L2Character.AIAccessor accessor)
  98. {
  99. super(accessor);
  100.  
  101. _selfAnalysis.Init();
  102. _attackTimeout = Integer.MAX_VALUE;
  103. _globalAggro = -10; // 10 seconds timeout of ATTACK after respawn
  104. }
  105.  
  106. public void run()
  107. {
  108. // Launch actions corresponding to the Event Think
  109. onEvtThink();
  110. }
  111.  
  112. /**
  113. * Return True if the target is autoattackable (depends on the actor type).<BR><BR>
  114. *
  115. * <B><U> Actor is a L2GuardInstance</U> :</B><BR><BR>
  116. * <li>The target isn't a Folk or a Door</li>
  117. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  118. * <li>The target is in the actor Aggro range and is at the same height</li>
  119. * <li>The L2PcInstance target has karma (=PK)</li>
  120. * <li>The L2MonsterInstance target is aggressive</li><BR><BR>
  121. *
  122. * <B><U> Actor is a L2SiegeGuardInstance</U> :</B><BR><BR>
  123. * <li>The target isn't a Folk or a Door</li>
  124. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  125. * <li>The target is in the actor Aggro range and is at the same height</li>
  126. * <li>A siege is in progress</li>
  127. * <li>The L2PcInstance target isn't a Defender</li><BR><BR>
  128. *
  129. * <B><U> Actor is a L2FriendlyMobInstance</U> :</B><BR><BR>
  130. * <li>The target isn't a Folk, a Door or another L2NpcInstance</li>
  131. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  132. * <li>The target is in the actor Aggro range and is at the same height</li>
  133. * <li>The L2PcInstance target has karma (=PK)</li><BR><BR>
  134. *
  135. * <B><U> Actor is a L2MonsterInstance</U> :</B><BR><BR>
  136. * <li>The target isn't a Folk, a Door or another L2NpcInstance</li>
  137. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  138. * <li>The target is in the actor Aggro range and is at the same height</li>
  139. * <li>The actor is Aggressive</li><BR><BR>
  140. *
  141. * @param target The targeted L2Object
  142. *
  143. */
  144. private boolean autoAttackCondition(L2Character target)
  145. {
  146. if (target == null || !(_actor instanceof L2Attackable))
  147. return false;
  148.  
  149. L2Attackable me = (L2Attackable) _actor;
  150.  
  151. // Check if target is invulnerable
  152. if (target.isInvul())
  153. return false;
  154.  
  155. // Check if the target isn't a Folk or a Door
  156. if (target instanceof L2FolkInstance || target instanceof L2DoorInstance)
  157. return false;
  158.  
  159. // Check if the target isn't dead, is in the Aggro range and is at the same height
  160. if (target.isAlikeDead() || !me.isInsideRadius(target, me.getAggroRange(), false, false)
  161. || Math.abs(me.getZ() - target.getZ()) > 400)
  162. return false;
  163.  
  164. if (_selfAnalysis.cannotMoveOnLand && !target.isInsideZone(L2Character.ZONE_WATER))
  165. return false;
  166.  
  167. if (target instanceof L2PlayableInstance)
  168. {
  169. // Check if the AI isn't a Raid Boss/Town guard and the target isn't in silent move mode
  170. if (!(me.isRaid() || me instanceof L2GuardInstance) && ((L2PlayableInstance)target).isSilentMoving())
  171. return false;
  172. }
  173.  
  174. // Check if the target is a L2PcInstance
  175. if (target instanceof L2PcInstance)
  176. {
  177. if (me.getFactionId() != null)
  178. {
  179.  
  180. // Check if player is an ally
  181. if (me.getFactionId().equals("varka_silenos_clan") && ((L2PcInstance)target).isAlliedWithVarka())
  182. return false;
  183.  
  184. if (me.getFactionId().equals("ketra_orc_clan") && ((L2PcInstance)target).isAlliedWithKetra())
  185. return false;
  186. }
  187.  
  188. // check if the target is within the grace period for JUST getting up from fake death
  189. if (((L2PcInstance)target).isRecentFakeDeath())
  190. return false;
  191.  
  192. if (target.isInParty() && target.getParty().isInDimensionalRift())
  193. {
  194. byte riftType = target.getParty().getDimensionalRift().getType();
  195. byte riftRoom = target.getParty().getDimensionalRift().getCurrentRoom();
  196. if (me instanceof L2RiftInvaderInstance && !DimensionalRiftManager.getInstance().getRoom(riftType, riftRoom).checkIfInZone(me.getX(), me.getY(), me.getZ()))
  197. return false;
  198. }
  199. }
  200.  
  201. // Check if the target is a L2Summon
  202. if (target instanceof L2Summon)
  203. {
  204. L2PcInstance owner = ((L2Summon)target).getOwner();
  205. if (owner != null)
  206. {
  207. if (me.getFactionId() != null)
  208. {
  209. // Check if player is an ally
  210. if (me.getFactionId().equals("varka_silenos_clan") && owner.isAlliedWithVarka())
  211. return false;
  212.  
  213. if (me.getFactionId().equals("ketra_orc_clan") && owner.isAlliedWithKetra())
  214. return false;
  215. }
  216. }
  217. }
  218.  
  219. // Check if the actor is a L2GuardInstance
  220. if (_actor instanceof L2GuardInstance)
  221. {
  222. // Check if the L2PcInstance target has karma (=PK)
  223. if (target instanceof L2PcInstance && ((L2PcInstance) target).getKarma() > 0)
  224. return GeoData.getInstance().canSeeTarget(me, target); // Los Check
  225.  
  226.  
  227. // Check if the L2MonsterInstance target is aggressive
  228. if (target instanceof L2MonsterInstance)
  229. return (((L2MonsterInstance) target).isAggressive() && GeoData.getInstance().canSeeTarget(me, target));
  230.  
  231. return false;
  232. }
  233. // Check if the actor is a L2FriendlyMobInstance
  234. else if (_actor instanceof L2FriendlyMobInstance)
  235. {
  236. // Check if the target isn't another L2NpcInstance
  237. if (target instanceof L2NpcInstance) return false;
  238.  
  239. // Check if the L2PcInstance target has karma (=PK)
  240. if (target instanceof L2PcInstance && ((L2PcInstance) target).getKarma() > 0)
  241. return GeoData.getInstance().canSeeTarget(me, target); // Los Check
  242.  
  243. return false;
  244. }
  245. // The actor is a L2MonsterInstance
  246. else
  247. {
  248. // Check if the target isn't another L2NpcInstance
  249. if (target instanceof L2NpcInstance) return false;
  250.  
  251. // depending on config, do not allow mobs to attack _new_ players in peacezones,
  252. // unless they are already following those players from outside the peacezone.
  253. if (!Config.ALT_MOB_AGGRO_IN_PEACEZONE && target.isInsideZone(L2Character.ZONE_PEACE))
  254. return false;
  255.  
  256. if (Config.CHAMPION_ENABLE && me.isChampion() && Config.CHAMPION_PASSIVE)
  257. return false;
  258.  
  259. // Check if the actor is Aggressive
  260. return (me.isAggressive() && GeoData.getInstance().canSeeTarget(me, target));
  261. }
  262. }
  263.  
  264. public void startAITask()
  265. {
  266. // If not idle - create an AI task (schedule onEvtThink repeatedly)
  267. if (aiTask == null)
  268. aiTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(this, 1000, 1000);
  269. }
  270.  
  271. public void stopAITask()
  272. {
  273. if (aiTask != null)
  274. {
  275. aiTask.cancel(false);
  276. aiTask = null;
  277. }
  278. }
  279.  
  280. protected void onEvtDead()
  281. {
  282. stopAITask();
  283. super.onEvtDead();
  284. }
  285.  
  286. /**
  287. * Set the Intention of this L2CharacterAI and create an AI Task executed every 1s (call onEvtThink method) for this L2Attackable.<BR><BR>
  288. *
  289. * <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>
  290. *
  291. * @param intention The new Intention to set to the AI
  292. * @param arg0 The first parameter of the Intention
  293. * @param arg1 The second parameter of the Intention
  294. *
  295. */
  296. synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
  297. {
  298. if (intention == AI_INTENTION_IDLE || intention == AI_INTENTION_ACTIVE)
  299. {
  300. // Check if actor is not dead
  301. if (!_actor.isAlikeDead())
  302. {
  303. L2Attackable npc = (L2Attackable) _actor;
  304.  
  305. // If its _knownPlayer isn't empty set the Intention to AI_INTENTION_ACTIVE
  306. if (npc.getKnownList().getKnownPlayers().size() > 0)
  307. intention = AI_INTENTION_ACTIVE;
  308. else
  309. {
  310. if (npc.getSpawn() != null)
  311. {
  312. if (!npc.isInsideRadius(npc.getSpawn().getLocx(), npc.getSpawn().getLocy(), npc.getSpawn().getLocz(), Config.MAX_DRIFT_RANGE + Config.MAX_DRIFT_RANGE, true, false))
  313. intention = AI_INTENTION_ACTIVE;
  314. }
  315. }
  316. }
  317.  
  318. if (intention == AI_INTENTION_IDLE)
  319. {
  320. // Set the Intention of this L2AttackableAI to AI_INTENTION_IDLE
  321. super.changeIntention(AI_INTENTION_IDLE, null, null);
  322.  
  323. // Stop AI task and detach AI from NPC
  324. if (aiTask != null)
  325. {
  326. aiTask.cancel(true);
  327. aiTask = null;
  328. }
  329.  
  330. // Cancel the AI
  331. _accessor.detachAI();
  332.  
  333. return;
  334. }
  335. }
  336.  
  337. // Set the Intention of this L2AttackableAI to intention
  338. super.changeIntention(intention, arg0, arg1);
  339.  
  340. // If not idle - create an AI task (schedule onEvtThink repeatedly)
  341. startAITask();
  342. }
  343.  
  344. /**
  345. * Manage the Attack Intention : Stop current Attack (if necessary), Calculate attack timeout, Start a new Attack and Launch Think Event.<BR><BR>
  346. *
  347. * @param target The L2Character to attack
  348. *
  349. */
  350. protected void onIntentionAttack(L2Character target)
  351. {
  352. // Calculate the attack timeout
  353. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  354.  
  355. // self and buffs
  356. if (_selfAnalysis.lastBuffTick+100 < GameTimeController.getGameTicks())
  357. {
  358. for (L2Skill sk : _selfAnalysis.buffSkills)
  359. {
  360. if (_actor.getFirstEffect(sk.getId()) == null)
  361. {
  362. if (_actor.getCurrentMp() < sk.getMpConsume())
  363. continue;
  364.  
  365. if (_actor.isSkillDisabled(sk.getId()))
  366. continue;
  367.  
  368. // no clan buffs here?
  369. if (sk.getTargetType() == L2Skill.SkillTargetType.TARGET_CLAN)
  370. continue;
  371.  
  372. L2Object OldTarget = _actor.getTarget();
  373. _actor.setTarget(_actor);
  374. clientStopMoving(null);
  375. _accessor.doCast(sk);
  376.  
  377. // forcing long reuse delay so if cast get interrupted or there would be several buffs, doesn't cast again
  378. _selfAnalysis.lastBuffTick = GameTimeController.getGameTicks();
  379. _actor.setTarget(OldTarget);
  380. }
  381. }
  382. }
  383.  
  384. // Manage the Attack Intention : Stop current Attack (if necessary), Start a new Attack and Launch Think Event
  385. super.onIntentionAttack(target);
  386. }
  387.  
  388. /**
  389. * Manage AI standard thinks of a L2Attackable (called by onEvtThink).<BR><BR>
  390. *
  391. * <B><U> Actions</U> :</B><BR><BR>
  392. * <li>Update every 1s the _globalAggro counter to come close to 0</li>
  393. * <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>
  394. * <li>If the actor is a L2GuardInstance that can't attack, order to it to return to its home location</li>
  395. * <li>If the actor is a L2MonsterInstance that can't attack, order to it to random walk (1/100)</li><BR><BR>
  396. *
  397. */
  398. private void thinkActive()
  399. {
  400. L2Attackable npc = (L2Attackable) _actor;
  401.  
  402. // Update every 1s the _globalAggro counter to come close to 0
  403. if (_globalAggro != 0)
  404. {
  405. if (_globalAggro < 0) _globalAggro++;
  406. else _globalAggro--;
  407. }
  408.  
  409. // Add all autoAttackable L2Character in L2Attackable Aggro Range to its _aggroList with 0 damage and 1 hate
  410. // A L2Attackable isn't aggressive during 10s after its spawn because _globalAggro is set to -10
  411. if (_globalAggro >= 0)
  412. {
  413. // Get all visible objects inside its Aggro Range
  414. //L2Object[] objects = L2World.getInstance().getVisibleObjects(_actor, ((L2NpcInstance)_actor).getAggroRange());
  415.  
  416. // Go through visible objects
  417. for (L2Object obj : npc.getKnownList().getKnownObjects().values())
  418. {
  419. if (!(obj instanceof L2Character)) continue;
  420.  
  421. L2Character target = (L2Character) obj;
  422.  
  423. /*
  424. * Check to see if this is a festival mob spawn.
  425. * If it is, then check to see if the aggro trigger
  426. * is a festival participant...if so, move to attack it.
  427. */
  428. if ((_actor instanceof L2FestivalMonsterInstance) && obj instanceof L2PcInstance)
  429. {
  430. L2PcInstance targetPlayer = (L2PcInstance) obj;
  431.  
  432. if (!(targetPlayer.isFestivalParticipant())) continue;
  433. }
  434.  
  435. // For each L2Character check if the target is autoattackable
  436. if (autoAttackCondition(target)) // check aggression
  437. {
  438. // Get the hate level of the L2Attackable against this L2Character target contained in _aggroList
  439. int hating = npc.getHating(target);
  440.  
  441. // Add the attacker to the L2Attackable _aggroList with 0 damage and 1 hate
  442. if (hating == 0)
  443. npc.addDamageHate(target, 0, 1);
  444. }
  445. }
  446.  
  447. // Choose a target from its aggroList
  448. L2Character hated;
  449. if (_actor.isConfused())
  450. hated = getAttackTarget(); // effect handles selection
  451. else
  452. hated = npc.getMostHated();
  453.  
  454. // Order to the L2Attackable to attack the target
  455. if (hated != null && !npc.isCoreAIDisabled())
  456. {
  457. // Get the hate level of the L2Attackable against this L2Character target contained in _aggroList
  458. int aggro = npc.getHating(hated);
  459.  
  460. if (aggro + _globalAggro > 0)
  461. {
  462. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  463. if (!_actor.isRunning()) _actor.setRunning();
  464.  
  465. // Set the AI Intention to AI_INTENTION_ATTACK
  466. setIntention(CtrlIntention.AI_INTENTION_ATTACK, hated);
  467. }
  468.  
  469. return;
  470. }
  471. }
  472.  
  473. // Chance to forget attackers after some time
  474. if ((npc.getCurrentHp() == npc.getStat().getMaxHp()) && !npc.getAttackByList().isEmpty())
  475. {
  476. npc.clearAggroList();
  477. npc.getAttackByList().clear();
  478. }
  479.  
  480. // Check if the actor is a L2GuardInstance
  481. if (_actor instanceof L2GuardInstance)
  482. {
  483. // Order to the L2GuardInstance to return to its home location because there's no target to attack
  484. ((L2GuardInstance) _actor).returnHome();
  485. }
  486.  
  487. // If this is a festival monster, then it remains in the same location.
  488. if (_actor instanceof L2FestivalMonsterInstance)
  489.  
  490. return;
  491.  
  492. // Minions following leader
  493. if (_actor instanceof L2MinionInstance && ((L2MinionInstance)_actor).getLeader() != null)
  494. {
  495. int offset;
  496.  
  497. if (_actor.isRaid()) offset = 500; // for Raids - need correction
  498. else offset = 200; // for normal minions - need correction :)
  499.  
  500. if(((L2MinionInstance)_actor).getLeader().isRunning()) _actor.setRunning();
  501. else _actor.setWalking();
  502.  
  503. if (_actor.getPlanDistanceSq(((L2MinionInstance)_actor).getLeader()) > offset*offset)
  504. {
  505. int x1, y1, z1;
  506. x1 = ((L2MinionInstance)_actor).getLeader().getX() + Rnd.nextInt((offset - 30) * 2) - (offset - 30);
  507. y1 = ((L2MinionInstance)_actor).getLeader().getY() + Rnd.nextInt((offset - 30) * 2) - (offset - 30);
  508. z1 = ((L2MinionInstance)_actor).getLeader().getZ();
  509. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  510. moveTo(x1, y1, z1);
  511. return;
  512. }
  513. else if (Rnd.nextInt(RANDOM_WALK_RATE) == 0)
  514. {
  515. // self and clan buffs
  516. for (L2Skill sk : _selfAnalysis.buffSkills)
  517. {
  518. if (_actor.getFirstEffect(sk.getId()) == null)
  519. {
  520. // if clan buffs, don't buff every time
  521. if (sk.getTargetType() != L2Skill.SkillTargetType.TARGET_SELF && Rnd.nextInt(2) != 0)
  522. continue;
  523. if (_actor.getCurrentMp() < sk.getMpConsume())
  524. continue;
  525. if (_actor.isSkillDisabled(sk.getId()))
  526. continue;
  527. L2Object OldTarget = _actor.getTarget();
  528. _actor.setTarget(_actor);
  529. clientStopMoving(null);
  530. _accessor.doCast(sk);
  531. _actor.setTarget(OldTarget);
  532. return;
  533. }
  534. }
  535. }
  536. }
  537.  
  538. // Order to the L2MonsterInstance to random walk (1/100)
  539. else if (npc.getSpawn() != null && Rnd.nextInt(RANDOM_WALK_RATE) == 0
  540. && !(_actor instanceof L2RaidBossInstance
  541. || _actor instanceof L2MinionInstance
  542. || _actor instanceof L2GrandBossInstance
  543. || _actor instanceof L2ChestInstance
  544. || _actor instanceof L2GuardInstance
  545. || _actor instanceof L2SepulcherMonsterInstance
  546. || npc.isQuestMonster()))
  547. {
  548. int x1, y1, z1;
  549.  
  550. int range = Config.MAX_DRIFT_RANGE;
  551.  
  552. // self and clan buffs
  553. for (L2Skill sk : _selfAnalysis.buffSkills)
  554. {
  555. if (_actor.getFirstEffect(sk.getId()) == null)
  556. {
  557. if (_actor.getCurrentMp() < sk.getMpConsume())
  558. continue;
  559.  
  560. if (_actor.isSkillDisabled(sk.getId()))
  561. continue;
  562.  
  563. L2Object OldTarget = _actor.getTarget();
  564. _actor.setTarget(_actor);
  565. clientStopMoving(null);
  566. _accessor.doCast(sk);
  567. _actor.setTarget(OldTarget);
  568. return;
  569. }
  570. }
  571.  
  572. // If NPC with random coord in territory
  573. if (npc.getSpawn().getLocx() == 0 && npc.getSpawn().getLocy() == 0)
  574. {
  575. // Calculate a destination point in the spawn area
  576. int p[] = Territory.getInstance().getRandomPoint(npc.getSpawn().getLocation());
  577. x1 = p[0];
  578. y1 = p[1];
  579. z1 = p[2];
  580.  
  581. // Calculate the distance between the current position of the L2Character and the target (x,y)
  582. double distance2 = _actor.getPlanDistanceSq(x1, y1);
  583.  
  584. if (distance2 > range * range)
  585. {
  586. npc.setIsReturningToSpawnPoint(true);
  587. float delay = (float) Math.sqrt(distance2) / range;
  588. x1 = _actor.getX() + (int) ((x1 - _actor.getX()) / delay);
  589. y1 = _actor.getY() + (int) ((y1 - _actor.getY()) / delay);
  590. }
  591.  
  592. // If NPC with random fixed coord, don't move (unless needs to return to spawnpoint)
  593. if (Territory.getInstance().getProcMax(npc.getSpawn().getLocation()) > 0 && !npc.isReturningToSpawnPoint())
  594. return;
  595. }
  596. else
  597. {
  598. // If NPC with fixed coord
  599. x1 = npc.getSpawn().getLocx();
  600. y1 = npc.getSpawn().getLocy();
  601. z1 = npc.getSpawn().getLocz();
  602.  
  603. if (!_actor.isInsideRadius(x1, y1, z1, Config.MAX_DRIFT_RANGE + Config.MAX_DRIFT_RANGE, true, false))
  604. npc.setIsReturningToSpawnPoint(true);
  605. else
  606. {
  607. // If NPC with fixed coord
  608. x1 += Rnd.nextInt(range * 2) - range;
  609. y1 += Rnd.nextInt(range * 2) - range;
  610. z1 = npc.getZ();
  611. }
  612. }
  613.  
  614. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  615. moveTo(x1, y1, z1);
  616. }
  617. }
  618.  
  619. /**
  620. * Manage AI attack thinks of a L2Attackable (called by onEvtThink).<BR><BR>
  621. *
  622. * <B><U> Actions</U> :</B><BR><BR>
  623. * <li>Update the attack timeout if actor is running</li>
  624. * <li>If target is dead or timeout is expired, stop this attack and set the Intention to AI_INTENTION_ACTIVE</li>
  625. * <li>Call all L2Object of its Faction inside the Faction Range</li>
  626. * <li>Chose a target and order to attack it with magic skill or physical attack</li><BR><BR>
  627. *
  628. * TODO: Manage casting rules to healer mobs (like Ant Nurses)
  629. *
  630. */
  631. private void thinkAttack()
  632. {
  633. L2Character originalAttackTarget = getAttackTarget();
  634. // Check if target is dead or if timeout is expired to stop this attack
  635. if (originalAttackTarget == null || originalAttackTarget.isAlikeDead()
  636. || _attackTimeout < GameTimeController.getGameTicks())
  637. {
  638. // Stop hating this target after the attack timeout or if target is dead
  639. if (originalAttackTarget != null)
  640. {
  641. L2Attackable npc = (L2Attackable) _actor;
  642. npc.stopHating(originalAttackTarget);
  643. }
  644.  
  645. // Cancel target and timeout
  646. _attackTimeout = Integer.MAX_VALUE;
  647.  
  648. // Set the AI Intention to AI_INTENTION_ACTIVE
  649. setIntention(AI_INTENTION_ACTIVE);
  650.  
  651. _actor.setWalking();
  652. return;
  653. }
  654.  
  655. // Handle all L2Object of its Faction inside the Faction Range
  656. if (((L2NpcInstance)_actor).getFactionId() != null && !(originalAttackTarget instanceof L2Attackable))
  657. {
  658. String faction_id = ((L2NpcInstance) _actor).getFactionId();
  659.  
  660. // Go through all L2Object that belong to its faction
  661. for (L2Object obj : _actor.getKnownList().getKnownObjects().values())
  662. {
  663. if (obj instanceof L2NpcInstance)
  664. {
  665. L2NpcInstance npc = (L2NpcInstance) obj;
  666. if (npc == null)
  667. continue;
  668.  
  669. if (!faction_id.equals(npc.getFactionId()))
  670. continue;
  671.  
  672. // Check if the L2Object is inside the Faction Range of the actor
  673. if (_actor.isInsideRadius(npc, (npc.getFactionRange() + npc.getAggroRange()), false, true) && npc.getAI() != null)
  674. {
  675. if (Math.abs(originalAttackTarget.getZ() - npc.getZ()) < 600
  676. && _actor.getAttackByList().contains(originalAttackTarget)
  677. && (npc.getAI()._intention == CtrlIntention.AI_INTENTION_IDLE
  678. || npc.getAI()._intention == CtrlIntention.AI_INTENTION_ACTIVE)
  679. && GeoData.getInstance().canSeeTarget(_actor, npc))
  680. {
  681. L2PcInstance player = originalAttackTarget.getActingPlayer();
  682. if (player != null)
  683. {
  684. if (npc.getTemplate().getEventQuests(Quest.QuestEventType.ON_FACTION_CALL) != null)
  685. {
  686. for (Quest quest : npc.getTemplate().getEventQuests(Quest.QuestEventType.ON_FACTION_CALL))
  687. quest.notifyFactionCall(npc, (L2NpcInstance) _actor, player, (originalAttackTarget instanceof L2Summon));
  688. }
  689. }
  690.  
  691. if (originalAttackTarget instanceof L2PcInstance && originalAttackTarget.isInParty()
  692. && originalAttackTarget.getParty().isInDimensionalRift())
  693. {
  694. byte riftType = originalAttackTarget.getParty().getDimensionalRift().getType();
  695. byte riftRoom = originalAttackTarget.getParty().getDimensionalRift().getCurrentRoom();
  696. if (_actor instanceof L2RiftInvaderInstance && !DimensionalRiftManager.getInstance().getRoom(riftType, riftRoom).checkIfInZone(npc.getX(), npc.getY(), npc.getZ()))
  697. continue;
  698. }
  699.  
  700. // Notify the L2Object AI with EVT_AGGRESSION
  701. npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, originalAttackTarget, 1);
  702. }
  703.  
  704. // heal or resurrect friends
  705. if (_selfAnalysis.hasHealOrResurrect && !_actor.isAttackingDisabled()
  706. && npc.getCurrentHp() < npc.getMaxHp()*0.6
  707. && _actor.getCurrentHp() > _actor.getMaxHp()/2
  708. && _actor.getCurrentMp() > _actor.getMaxMp()/2)
  709. {
  710. if (npc.isDead() && _actor instanceof L2MinionInstance)
  711. {
  712. if (((L2MinionInstance)_actor).getLeader() == npc)
  713. {
  714. for (L2Skill sk : _selfAnalysis.resurrectSkills)
  715. {
  716. if (_actor.getCurrentMp() < sk.getMpConsume())
  717. continue;
  718.  
  719. if (_actor.isSkillDisabled(sk.getId()))
  720. continue;
  721.  
  722. if (!Util.checkIfInRange(sk.getCastRange(), _actor, npc, true))
  723. continue;
  724.  
  725. if (10 >= Rnd.get(100)) // chance
  726. continue;
  727.  
  728. if (!GeoData.getInstance().canSeeTarget(_actor, npc))
  729. break;
  730.  
  731. L2Object OldTarget = _actor.getTarget();
  732. _actor.setTarget(npc);
  733. // would this ever be fast enough for the decay not to run?
  734. // giving some extra seconds
  735. DecayTaskManager.getInstance().cancelDecayTask(npc);
  736. DecayTaskManager.getInstance().addDecayTask(npc);
  737. clientStopMoving(null);
  738. _accessor.doCast(sk);
  739. _actor.setTarget(OldTarget);
  740. return;
  741. }
  742. }
  743. }
  744. else if (npc.isInCombat())
  745. {
  746. for (L2Skill sk : _selfAnalysis.healSkills)
  747. {
  748. if (_actor.getCurrentMp() < sk.getMpConsume())
  749. continue;
  750.  
  751. if (_actor.isSkillDisabled(sk.getId()))
  752. continue;
  753.  
  754. if (!Util.checkIfInRange(sk.getCastRange(), _actor, npc, true))
  755. continue;
  756.  
  757. int chance = 4;
  758. if (_actor instanceof L2MinionInstance)
  759. {
  760. // minions support boss
  761. if (((L2MinionInstance)_actor).getLeader() == npc)
  762. chance = 6;
  763. else
  764. chance = 3;
  765. }
  766.  
  767. if (npc instanceof L2GrandBossInstance)
  768. chance = 6;
  769.  
  770. if (chance >= Rnd.get(100)) // chance
  771. continue;
  772.  
  773. if (!GeoData.getInstance().canSeeTarget(_actor, npc))
  774. break;
  775.  
  776. L2Object OldTarget = _actor.getTarget();
  777. _actor.setTarget(npc);
  778. clientStopMoving(null);
  779. _accessor.doCast(sk);
  780. _actor.setTarget(OldTarget);
  781. return;
  782. }
  783. }
  784. }
  785. }
  786. }
  787. }
  788. }
  789.  
  790. if (_actor.isAttackingDisabled())
  791. return;
  792.  
  793. // Get 2 most hated chars
  794. List<L2Character> hated = ((L2Attackable) _actor).get2MostHated();
  795. if (_actor.isConfused())
  796. {
  797. if (hated != null)
  798. hated.set(0, originalAttackTarget); // effect handles selection
  799. else
  800. {
  801. hated = new FastList<L2Character>();
  802. hated.add(originalAttackTarget);
  803. hated.add(null);
  804. }
  805. }
  806.  
  807. if (hated == null || hated.get(0) == null)
  808. {
  809. setIntention(AI_INTENTION_ACTIVE);
  810. return;
  811. }
  812.  
  813. if (hated.get(0) != originalAttackTarget)
  814. setAttackTarget(hated.get(0));
  815.  
  816. _mostHatedAnalysis.Update(hated.get(0));
  817. _secondMostHatedAnalysis.Update(hated.get(1));
  818.  
  819. // Get all information needed to choose between physical or magical attack
  820. _actor.setTarget(_mostHatedAnalysis.character);
  821. double dist2 = _actor.getPlanDistanceSq(_mostHatedAnalysis.character.getX(), _mostHatedAnalysis.character.getY());
  822. int combinedCollision = (int)(_actor.getTemplate().collisionRadius + _mostHatedAnalysis.character.getTemplate().collisionRadius);
  823. int range = _actor.getPhysicalAttackRange() + combinedCollision;
  824.  
  825. // Reconsider target if _actor hasn't got hits in for last 14 sec
  826. if (!_actor.isMuted() && _attackTimeout - 160 < GameTimeController.getGameTicks() && _secondMostHatedAnalysis.character != null)
  827. {
  828. if (Util.checkIfInRange(900, _actor, hated.get(1), true))
  829. {
  830. // take off 2* the amount the aggro is larger than second most
  831. int aggro = 2 * (((L2Attackable) _actor).getHating(hated.get(0)) - ((L2Attackable) _actor).getHating(hated.get(1)));
  832. onEvtAggression(hated.get(0), -aggro);
  833. // Calculate a new attack timeout
  834. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  835. }
  836. }
  837.  
  838. // Reconsider target during next round if actor is rooted and cannot reach mostHated but can
  839. // reach secondMostHated
  840. if (_actor.isRooted() && _secondMostHatedAnalysis.character != null)
  841. {
  842. if (_selfAnalysis.isMage && dist2 > _selfAnalysis.maxCastRange * _selfAnalysis.maxCastRange
  843. && _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY()) < _selfAnalysis.maxCastRange * _selfAnalysis.maxCastRange)
  844. {
  845. int aggro = 1 + (((L2Attackable) _actor).getHating(hated.get(0)) - ((L2Attackable) _actor).getHating(hated.get(1)));
  846. onEvtAggression(hated.get(0), -aggro);
  847. }
  848. else if (dist2 > range * range && _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY()) < range * range)
  849. {
  850. int aggro = 1 + (((L2Attackable) _actor).getHating(hated.get(0)) - ((L2Attackable) _actor).getHating(hated.get(1)));
  851. onEvtAggression(hated.get(0), -aggro);
  852. }
  853. }
  854.  
  855. // Considering, if bigger range will be attempted
  856. if ((dist2 < 10000 + combinedCollision*combinedCollision)
  857. && !_selfAnalysis.isFighter && !_selfAnalysis.isBalanced
  858. && (_selfAnalysis.hasLongRangeSkills || _selfAnalysis.isArcher)
  859. && (_mostHatedAnalysis.isBalanced || _mostHatedAnalysis.isFighter)
  860. && (_mostHatedAnalysis.character.isRooted() || _mostHatedAnalysis.isSlower)
  861. && (Config.GEODATA == 2 ? 20 : 12) >= Rnd.get(100)) // chance
  862. {
  863. int posX = _actor.getX();
  864. int posY = _actor.getY();
  865. int posZ = _actor.getZ();
  866. double distance = Math.sqrt(dist2); // This way, we only do the sqrt if we need it
  867.  
  868. int signx=-1;
  869. int signy=-1;
  870. if (_actor.getX() > _mostHatedAnalysis.character.getX())
  871. signx=1;
  872. if (_actor.getY()>_mostHatedAnalysis.character.getY())
  873. signy=1;
  874. posX += Math.round((float)((signx * ((range / 2) + (Rnd.get(range)))) - distance));
  875. posY += Math.round((float)((signy * ((range / 2) + (Rnd.get(range)))) - distance));
  876. setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, new L2CharPosition(posX, posY, posZ, 0));
  877. return;
  878. }
  879.  
  880. // Cannot see target, needs to go closer, currently just goes to range 300 if mage
  881. if ((dist2 > 310*310 + combinedCollision*combinedCollision) && this._selfAnalysis.hasLongRangeSkills
  882. && !GeoData.getInstance().canSeeTarget(_actor, _mostHatedAnalysis.character))
  883. {
  884. if (!(_selfAnalysis.isMage && _actor.isMuted()))
  885. {
  886. moveToPawn(_mostHatedAnalysis.character, 300);
  887. return;
  888. }
  889. }
  890.  
  891. if (_mostHatedAnalysis.character.isMoving())
  892. range += 50;
  893.  
  894. // Check if the actor is far from target
  895. if (dist2 > range*range)
  896. {
  897. if (!_actor.isMuted() && (_selfAnalysis.hasLongRangeSkills || !_selfAnalysis.healSkills.isEmpty()))
  898. {
  899. // check for long ranged skills and heal/buff skills
  900. if (!_mostHatedAnalysis.isCanceled)
  901. {
  902. for (L2Skill sk : _selfAnalysis.cancelSkills)
  903. {
  904. int castRange = sk.getCastRange() + combinedCollision;
  905. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  906. continue;
  907.  
  908. if (Rnd.nextInt(100) <= 8)
  909. {
  910. clientStopMoving(null);
  911. _accessor.doCast(sk);
  912. _mostHatedAnalysis.isCanceled = true;
  913. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  914. return;
  915. }
  916. }
  917. }
  918.  
  919. if (_selfAnalysis.lastDebuffTick+60 < GameTimeController.getGameTicks())
  920. {
  921. for (L2Skill sk : _selfAnalysis.debuffSkills)
  922. {
  923. int castRange = sk.getCastRange() + combinedCollision;
  924. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  925. continue;
  926.  
  927. int chance = 8;
  928. if (_selfAnalysis.isFighter && _mostHatedAnalysis.isMage)
  929. chance = 3;
  930. if (_selfAnalysis.isFighter && _mostHatedAnalysis.isArcher)
  931. chance = 12;
  932. if (_selfAnalysis.isMage && !_mostHatedAnalysis.isMage)
  933. chance = 10;
  934. if (_mostHatedAnalysis.isMagicResistant)
  935. chance /= 2;
  936.  
  937. if (Rnd.nextInt(100) <= chance)
  938. {
  939. clientStopMoving(null);
  940. _accessor.doCast(sk);
  941. _selfAnalysis.lastDebuffTick = GameTimeController.getGameTicks();
  942. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  943. return;
  944. }
  945. }
  946. }
  947.  
  948. if (!_mostHatedAnalysis.character.isMuted())
  949. {
  950. int chance = 8;
  951. if (!(_mostHatedAnalysis.isMage || _mostHatedAnalysis.isBalanced))
  952. chance = 3;
  953.  
  954. for (L2Skill sk : _selfAnalysis.muteSkills)
  955. {
  956. int castRange = sk.getCastRange() + combinedCollision;
  957. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  958. continue;
  959.  
  960. if (Rnd.nextInt(100) <= chance)
  961. {
  962. clientStopMoving(null);
  963. _accessor.doCast(sk);
  964. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  965. return;
  966. }
  967. }
  968. }
  969.  
  970. if (_secondMostHatedAnalysis.character != null && !_secondMostHatedAnalysis.character.isMuted() && (_secondMostHatedAnalysis.isMage || _secondMostHatedAnalysis.isBalanced))
  971. {
  972. double secondHatedDist2 = _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY());
  973. for (L2Skill sk : _selfAnalysis.muteSkills)
  974. {
  975. int castRange = sk.getCastRange() + combinedCollision;
  976. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (secondHatedDist2 > castRange * castRange))
  977. continue;
  978.  
  979. if (Rnd.nextInt(100) <= 2)
  980. {
  981. _actor.setTarget(_secondMostHatedAnalysis.character);
  982. clientStopMoving(null);
  983. _accessor.doCast(sk);
  984. _actor.setTarget(_mostHatedAnalysis.character);
  985. return;
  986. }
  987. }
  988. }
  989.  
  990. if (!_mostHatedAnalysis.character.isSleeping())
  991. {
  992. for (L2Skill sk : _selfAnalysis.sleepSkills)
  993. {
  994. int castRange = sk.getCastRange() + combinedCollision;
  995. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  996. continue;
  997.  
  998. if (Rnd.nextInt(100) <= 1)
  999. {
  1000. clientStopMoving(null);
  1001. _accessor.doCast(sk);
  1002. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  1003. return;
  1004. }
  1005. }
  1006. }
  1007.  
  1008. if (_secondMostHatedAnalysis.character != null && !_secondMostHatedAnalysis.character.isSleeping())
  1009. {
  1010. double secondHatedDist2 = _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY());
  1011. for (L2Skill sk : _selfAnalysis.sleepSkills)
  1012. {
  1013. int castRange = sk.getCastRange() + combinedCollision;
  1014. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (secondHatedDist2 > castRange * castRange))
  1015. continue;
  1016.  
  1017. if (Rnd.nextInt(100) <= 3)
  1018. {
  1019. _actor.setTarget(_secondMostHatedAnalysis.character);
  1020. clientStopMoving(null);
  1021. _accessor.doCast(sk);
  1022. _actor.setTarget(_mostHatedAnalysis.character);
  1023. return;
  1024. }
  1025. }
  1026. }
  1027.  
  1028. if (!_mostHatedAnalysis.character.isRooted())
  1029. {
  1030. for (L2Skill sk : _selfAnalysis.rootSkills)
  1031. {
  1032. int castRange = sk.getCastRange() + combinedCollision;
  1033. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1034. continue;
  1035.  
  1036. if (Rnd.nextInt(100) <= (_mostHatedAnalysis.isSlower ? 3 : 8));
  1037. {
  1038. clientStopMoving(null);
  1039. _accessor.doCast(sk);
  1040. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  1041. return;
  1042. }
  1043. }
  1044. }
  1045.  
  1046. if (!_mostHatedAnalysis.character.isAttackingDisabled())
  1047. {
  1048. for (L2Skill sk : _selfAnalysis.generalDisablers)
  1049. {
  1050. int castRange = sk.getCastRange() + combinedCollision;
  1051. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1052. continue;
  1053.  
  1054. if (Rnd.nextInt(100) <= ((_selfAnalysis.isFighter && _actor.isRooted()) ? 15 : 7))
  1055. {
  1056. clientStopMoving(null);
  1057. _accessor.doCast(sk);
  1058. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  1059. return;
  1060. }
  1061. }
  1062. }
  1063.  
  1064. if (_actor.getCurrentHp() < _actor.getMaxHp()*0.4)
  1065. {
  1066. for (L2Skill sk : _selfAnalysis.healSkills)
  1067. {
  1068. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk))
  1069. continue;
  1070.  
  1071. int chance = 7;
  1072. if (_mostHatedAnalysis.character.isAttackingDisabled())
  1073. chance += 10;
  1074.  
  1075. if (_secondMostHatedAnalysis.character == null || _secondMostHatedAnalysis.character.isAttackingDisabled())
  1076. chance += 10;
  1077.  
  1078. if (Rnd.nextInt(100) <= chance)
  1079. {
  1080. _actor.setTarget(_actor);
  1081. clientStopMoving(null);
  1082. _accessor.doCast(sk);
  1083. _actor.setTarget(_mostHatedAnalysis.character);
  1084. return;
  1085. }
  1086. }
  1087. }
  1088.  
  1089. // chance decision for launching long range skills
  1090. int castingChance = 5;
  1091. if (_selfAnalysis.isMage)
  1092. castingChance = 50; // mages
  1093.  
  1094. if (_selfAnalysis.isBalanced)
  1095. {
  1096. if (!_mostHatedAnalysis.isFighter) // advance to mages
  1097. castingChance = 15;
  1098. else
  1099. castingChance = 25; // stay away from fighters
  1100. }
  1101.  
  1102. if (_selfAnalysis.isFighter)
  1103. {
  1104. if (_mostHatedAnalysis.isMage)
  1105. castingChance = 3;
  1106. else
  1107. castingChance = 7;
  1108.  
  1109. if (_actor.isRooted())
  1110. castingChance = 20; // doesn't matter if no success first round
  1111. }
  1112.  
  1113. for (L2Skill sk : _selfAnalysis.generalSkills)
  1114. {
  1115. int castRange = sk.getCastRange() + combinedCollision;
  1116. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1117. continue;
  1118.  
  1119. if (Rnd.nextInt(100) <= castingChance)
  1120. {
  1121. clientStopMoving(null);
  1122. _accessor.doCast(sk);
  1123. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  1124. return;
  1125. }
  1126. }
  1127. }
  1128.  
  1129. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  1130. if (_selfAnalysis.isMage)
  1131. {
  1132. if (_actor.isMuted())
  1133. return;
  1134. range = _selfAnalysis.maxCastRange;
  1135. }
  1136.  
  1137. if (_mostHatedAnalysis.character.isMoving())
  1138. range -= 100;
  1139. if (range < 5)
  1140. range = 5;
  1141. moveToPawn(_mostHatedAnalysis.character, range);
  1142. return;
  1143. }
  1144. // **************************************************
  1145. // Else, if this is close enough for physical attacks
  1146. else
  1147. {
  1148. // In case many mobs are trying to hit from same place, move a bit,
  1149. // circling around the target
  1150. if (Rnd.nextInt(100) <= 33) // check it once per 3 seconds
  1151. {
  1152. for (L2Object nearby : _actor.getKnownList().getKnownCharactersInRadius(10))
  1153. {
  1154. if (nearby instanceof L2Attackable && nearby != _mostHatedAnalysis.character)
  1155. {
  1156. int diffx = Rnd.get(combinedCollision, combinedCollision+40);
  1157. if (Rnd.get(10) < 5)
  1158. diffx = -diffx;
  1159.  
  1160. int diffy = Rnd.get(combinedCollision, combinedCollision+40);
  1161. if (Rnd.get(10) < 5)
  1162. diffy = -diffy;
  1163.  
  1164. moveTo(_mostHatedAnalysis.character.getX() + diffx, _mostHatedAnalysis.character.getY() + diffy, _mostHatedAnalysis.character.getZ());
  1165. return;
  1166. }
  1167. }
  1168. }
  1169.  
  1170. // Calculate a new attack timeout.
  1171. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  1172.  
  1173. // check for close combat skills && heal/buff skills
  1174. if (!_mostHatedAnalysis.isCanceled)
  1175. {
  1176. for (L2Skill sk : _selfAnalysis.cancelSkills)
  1177. {
  1178. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1179. continue;
  1180.  
  1181. int castRange = sk.getCastRange() + combinedCollision;
  1182. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1183. continue;
  1184.  
  1185. if (Rnd.nextInt(100) <= 8)
  1186. {
  1187. clientStopMoving(null);
  1188. _accessor.doCast(sk);
  1189. _mostHatedAnalysis.isCanceled = true;
  1190. return;
  1191. }
  1192. }
  1193. }
  1194.  
  1195. if (_selfAnalysis.lastDebuffTick+60 < GameTimeController.getGameTicks())
  1196. {
  1197. for (L2Skill sk : _selfAnalysis.debuffSkills)
  1198. {
  1199. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1200. continue;
  1201.  
  1202. int castRange = sk.getCastRange() + combinedCollision;
  1203. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1204. continue;
  1205.  
  1206. int chance = 5;
  1207. if (_selfAnalysis.isFighter && _mostHatedAnalysis.isMage)
  1208. chance = 3;
  1209. if (_selfAnalysis.isFighter && _mostHatedAnalysis.isArcher)
  1210. chance = 3;
  1211. if (_selfAnalysis.isMage && !_mostHatedAnalysis.isMage)
  1212. chance = 4;
  1213. if (_mostHatedAnalysis.isMagicResistant)
  1214. chance /= 2;
  1215. if (sk.getCastRange() < 200)
  1216. chance += 3;
  1217.  
  1218. if (Rnd.nextInt(100) <= chance)
  1219. {
  1220. clientStopMoving(null);
  1221. _accessor.doCast(sk);
  1222. _selfAnalysis.lastDebuffTick = GameTimeController.getGameTicks();
  1223. return;
  1224. }
  1225. }
  1226. }
  1227.  
  1228. if (!_mostHatedAnalysis.character.isMuted() && (_mostHatedAnalysis.isMage || _mostHatedAnalysis.isBalanced))
  1229. {
  1230. for (L2Skill sk : _selfAnalysis.muteSkills)
  1231. {
  1232. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1233. continue;
  1234.  
  1235. int castRange = sk.getCastRange() + combinedCollision;
  1236. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1237. continue;
  1238.  
  1239. if (Rnd.nextInt(100) <= 7)
  1240. {
  1241. clientStopMoving(null);
  1242. _accessor.doCast(sk);
  1243. return;
  1244. }
  1245. }
  1246. }
  1247.  
  1248. if (_secondMostHatedAnalysis.character != null && !_secondMostHatedAnalysis.character.isMuted() && (_secondMostHatedAnalysis.isMage || _secondMostHatedAnalysis.isBalanced))
  1249. {
  1250. double secondHatedDist2 = _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY());
  1251. for (L2Skill sk : _selfAnalysis.muteSkills)
  1252. {
  1253. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1254. continue;
  1255.  
  1256. int castRange = sk.getCastRange() + combinedCollision;
  1257. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (secondHatedDist2 > castRange * castRange))
  1258. continue;
  1259.  
  1260. if (Rnd.nextInt(100) <= 3)
  1261. {
  1262. _actor.setTarget(_secondMostHatedAnalysis.character);
  1263. clientStopMoving(null);
  1264. _accessor.doCast(sk);
  1265. _actor.setTarget(_mostHatedAnalysis.character);
  1266. return;
  1267. }
  1268. }
  1269. }
  1270.  
  1271. if (_secondMostHatedAnalysis.character != null && !_secondMostHatedAnalysis.character.isSleeping())
  1272. {
  1273. double secondHatedDist2 = _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY());
  1274. for (L2Skill sk : _selfAnalysis.sleepSkills)
  1275. {
  1276. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1277. continue;
  1278.  
  1279. int castRange = sk.getCastRange() + combinedCollision;
  1280. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (secondHatedDist2 > castRange * castRange))
  1281. continue;
  1282.  
  1283. if (Rnd.nextInt(100) <= 4)
  1284. {
  1285. _actor.setTarget(_secondMostHatedAnalysis.character);
  1286. clientStopMoving(null);
  1287. _accessor.doCast(sk);
  1288. _actor.setTarget(_mostHatedAnalysis.character);
  1289. return;
  1290. }
  1291. }
  1292. }
  1293.  
  1294. if (!_mostHatedAnalysis.character.isRooted() && _mostHatedAnalysis.isFighter && !_selfAnalysis.isFighter)
  1295. {
  1296. for (L2Skill sk : _selfAnalysis.rootSkills)
  1297. {
  1298. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1299. continue;
  1300.  
  1301. int castRange = sk.getCastRange() + combinedCollision;
  1302. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1303. continue;
  1304.  
  1305. if (Rnd.nextInt(100) <= 4)
  1306. {
  1307. clientStopMoving(null);
  1308. _accessor.doCast(sk);
  1309. return;
  1310. }
  1311. }
  1312. }
  1313.  
  1314. if (!_mostHatedAnalysis.character.isAttackingDisabled())
  1315. {
  1316. for (L2Skill sk : _selfAnalysis.generalDisablers)
  1317. {
  1318. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1319. continue;
  1320.  
  1321. int castRange = sk.getCastRange() + combinedCollision;
  1322. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1323. continue;
  1324.  
  1325. if (Rnd.nextInt(100) <= ((sk.getCastRange() < 200) ? 10 : 7))
  1326. {
  1327. clientStopMoving(null);
  1328. _accessor.doCast(sk);
  1329. return;
  1330. }
  1331. }
  1332. }
  1333.  
  1334. if (_actor.getCurrentHp() < _actor.getMaxHp()*0.4)
  1335. {
  1336. for (L2Skill sk : _selfAnalysis.healSkills)
  1337. {
  1338. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1339. continue;
  1340.  
  1341. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk))
  1342. continue;
  1343.  
  1344. int chance = 7;
  1345. if (_mostHatedAnalysis.character.isAttackingDisabled())
  1346. chance += 10;
  1347. if (_secondMostHatedAnalysis.character == null || _secondMostHatedAnalysis.character.isAttackingDisabled())
  1348. chance += 10;
  1349.  
  1350. if (Rnd.nextInt(100) <= chance)
  1351. {
  1352. _actor.setTarget(_actor);
  1353. clientStopMoving(null);
  1354. _accessor.doCast(sk);
  1355. _actor.setTarget(_mostHatedAnalysis.character);
  1356. return;
  1357. }
  1358. }
  1359. }
  1360.  
  1361. for (L2Skill sk : _selfAnalysis.generalSkills)
  1362. {
  1363. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1364. continue;
  1365.  
  1366. int castRange = sk.getCastRange() + combinedCollision;
  1367. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1368. continue;
  1369.  
  1370. // chance decision for launching general skills in melee fight
  1371. // close range skills should be higher, long range lower
  1372. int castingChance = 5;
  1373. if (_selfAnalysis.isMage)
  1374. {
  1375. if (sk.getCastRange() < 200)
  1376. castingChance = 35;
  1377. else
  1378. castingChance = 25; // mages
  1379. }
  1380.  
  1381. if (_selfAnalysis.isBalanced)
  1382. {
  1383. if (sk.getCastRange() < 200)
  1384. castingChance = 12;
  1385. else
  1386. {
  1387. if (_mostHatedAnalysis.isMage) // hit mages
  1388. castingChance = 2;
  1389. else
  1390. castingChance = 5;
  1391. }
  1392. }
  1393.  
  1394. if (_selfAnalysis.isFighter)
  1395. {
  1396. if (sk.getCastRange() < 200)
  1397. castingChance = 12;
  1398. else
  1399. {
  1400. if (_mostHatedAnalysis.isMage)
  1401. castingChance = 1;
  1402. else
  1403. castingChance = 3;
  1404. }
  1405. }
  1406.  
  1407. if (Rnd.nextInt(100) <= castingChance)
  1408. {
  1409. clientStopMoving(null);
  1410. _accessor.doCast(sk);
  1411. return;
  1412. }
  1413. }
  1414.  
  1415. // Finally, physical attacks
  1416. clientStopMoving(null);
  1417. _accessor.doAttack(getAttackTarget());
  1418. }
  1419. }
  1420.  
  1421. /**
  1422. * Manage AI thinking actions of a L2Attackable.<BR><BR>
  1423. */
  1424. protected void onEvtThink()
  1425. {
  1426. // Check if the actor can't use skills and if a thinking action isn't already in progress
  1427. if (thinking || _actor.isAllSkillsDisabled()) return;
  1428.  
  1429. // Start thinking action
  1430. thinking = true;
  1431.  
  1432. try
  1433. {
  1434. // Manage AI thinks of a L2Attackable
  1435. if (getIntention() == AI_INTENTION_ACTIVE) thinkActive();
  1436. else if (getIntention() == AI_INTENTION_ATTACK) thinkAttack();
  1437. }
  1438. finally
  1439. {
  1440. // Stop thinking action
  1441. thinking = false;
  1442. }
  1443. }
  1444.  
  1445. /**
  1446. * Launch actions corresponding to the Event Attacked.<BR><BR>
  1447. *
  1448. * <B><U> Actions</U> :</B><BR><BR>
  1449. * <li>Init the attack : Calculate the attack timeout, Set the _globalAggro to 0, Add the attacker to the actor _aggroList</li>
  1450. * <li>Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance</li>
  1451. * <li>Set the Intention to AI_INTENTION_ATTACK</li><BR><BR>
  1452. *
  1453. * @param attacker The L2Character that attacks the actor
  1454. *
  1455. */
  1456. protected void onEvtAttacked(L2Character attacker)
  1457. {
  1458. L2Attackable me = (L2Attackable) _actor;
  1459.  
  1460. // Calculate the attack timeout
  1461. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  1462.  
  1463. // Set the _globalAggro to 0 to permit attack even just after spawn
  1464. if (_globalAggro < 0) _globalAggro = 0;
  1465.  
  1466. // Add the attacker to the _aggroList of the actor
  1467. me.addDamageHate(attacker, 0, 1);
  1468.  
  1469. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  1470. if (!_actor.isRunning()) _actor.setRunning();
  1471.  
  1472. // Set the Intention to AI_INTENTION_ATTACK
  1473. if (getIntention() != AI_INTENTION_ATTACK)
  1474. {
  1475. setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker);
  1476. }
  1477. else if (me.getMostHated() != getAttackTarget())
  1478. {
  1479. setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker);
  1480. }
  1481.  
  1482. // If this attackable is a L2MonsterInstance and it has spawned minions, call its minions to battle
  1483. if (me instanceof L2MonsterInstance)
  1484. {
  1485. L2MonsterInstance master = (L2MonsterInstance) me;
  1486. if (me instanceof L2MinionInstance)
  1487. {
  1488. master = ((L2MinionInstance)me).getLeader();
  1489. if (master != null && !master.isInCombat() && !master.isDead())
  1490. {
  1491. master.addDamageHate(attacker, 0, 1);
  1492. master.callMinionsToAssist(attacker);
  1493. }
  1494. }
  1495. else if (master.hasMinions())
  1496. master.callMinionsToAssist(attacker);
  1497. }
  1498.  
  1499. super.onEvtAttacked(attacker);
  1500. }
  1501.  
  1502. /**
  1503. * Launch actions corresponding to the Event Aggression.<BR><BR>
  1504. *
  1505. * <B><U> Actions</U> :</B><BR><BR>
  1506. * <li>Add the target to the actor _aggroList or update hate if already present </li>
  1507. * <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>
  1508. *
  1509. * @param attacker The L2Character that attacks
  1510. * @param aggro The value of hate to add to the actor against the target
  1511. *
  1512. */
  1513. protected void onEvtAggression(L2Character target, int aggro)
  1514. {
  1515. L2Attackable me = (L2Attackable) _actor;
  1516.  
  1517. if (target != null)
  1518. {
  1519. // Add the target to the actor _aggroList or update hate if already present
  1520. me.addDamageHate(target, 0, aggro);
  1521.  
  1522. // Get the hate of the actor against the target
  1523. // only if hate is definitely reduced
  1524. if (aggro < 0)
  1525. {
  1526. if (me.getHating(target) <= 0)
  1527. {
  1528. if (me.getMostHated() == null)
  1529. {
  1530. _globalAggro = -25;
  1531. me.clearAggroList();
  1532. setIntention(AI_INTENTION_ACTIVE);
  1533. _actor.setWalking();
  1534. }
  1535. }
  1536. return;
  1537. }
  1538.  
  1539. // Set the actor AI Intention to AI_INTENTION_ATTACK
  1540. if (getIntention() != CtrlIntention.AI_INTENTION_ATTACK)
  1541. {
  1542. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  1543. if (!_actor.isRunning()) _actor.setRunning();
  1544.  
  1545. setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
  1546. }
  1547. }
  1548. else
  1549. {
  1550. // currently only for setting lower general aggro
  1551. if (aggro >= 0)
  1552. return;
  1553.  
  1554. L2Character mostHated = me.getMostHated();
  1555. if (mostHated == null)
  1556. {
  1557. _globalAggro = -25;
  1558. return;
  1559. }
  1560. else
  1561. for (L2Character aggroed : me.getAggroList().keySet())
  1562. me.addDamageHate(aggroed, 0, aggro);
  1563.  
  1564. aggro = me.getHating(mostHated);
  1565. if (aggro <= 0)
  1566. {
  1567. _globalAggro = -25;
  1568. me.clearAggroList();
  1569. setIntention(AI_INTENTION_ACTIVE);
  1570. _actor.setWalking();
  1571. }
  1572. }
  1573. }
  1574. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement