L2AttackableAI.java 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
  21. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  22. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  23. import java.util.concurrent.Future;
  24. import net.sf.l2j.Config;
  25. import net.sf.l2j.gameserver.GameTimeController;
  26. import net.sf.l2j.gameserver.GeoData;
  27. import net.sf.l2j.gameserver.Territory;
  28. import net.sf.l2j.gameserver.ThreadPoolManager;
  29. import net.sf.l2j.gameserver.instancemanager.DimensionalRiftManager;
  30. import net.sf.l2j.gameserver.model.L2Attackable;
  31. import net.sf.l2j.gameserver.model.L2CharPosition;
  32. import net.sf.l2j.gameserver.model.L2Character;
  33. import net.sf.l2j.gameserver.model.L2Effect;
  34. import net.sf.l2j.gameserver.model.L2Object;
  35. import net.sf.l2j.gameserver.model.L2Skill;
  36. import net.sf.l2j.gameserver.model.L2Summon;
  37. import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
  38. import net.sf.l2j.gameserver.model.actor.instance.L2FestivalMonsterInstance;
  39. import net.sf.l2j.gameserver.model.actor.instance.L2FolkInstance;
  40. import net.sf.l2j.gameserver.model.actor.instance.L2FriendlyMobInstance;
  41. import net.sf.l2j.gameserver.model.actor.instance.L2GuardInstance;
  42. import net.sf.l2j.gameserver.model.actor.instance.L2MinionInstance;
  43. import net.sf.l2j.gameserver.model.actor.instance.L2MonsterInstance;
  44. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  45. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  46. import net.sf.l2j.gameserver.model.actor.instance.L2PenaltyMonsterInstance;
  47. import net.sf.l2j.gameserver.model.actor.instance.L2RaidBossInstance;
  48. import net.sf.l2j.gameserver.model.actor.instance.L2RiftInvaderInstance;
  49. import net.sf.l2j.gameserver.templates.L2Weapon;
  50. import net.sf.l2j.gameserver.templates.L2WeaponType;
  51. import net.sf.l2j.util.Rnd;
  52. /**
  53. * This class manages AI of L2Attackable.<BR><BR>
  54. *
  55. */
  56. public class L2AttackableAI extends L2CharacterAI implements Runnable
  57. {
  58. //protected static final Logger _log = Logger.getLogger(L2AttackableAI.class.getName());
  59. private static final int RANDOM_WALK_RATE = 30; // confirmed
  60. // private static final int MAX_DRIFT_RANGE = 300;
  61. private static final int MAX_ATTACK_TIMEOUT = 300; // int ticks, i.e. 30 seconds
  62. /** The L2Attackable AI task executed every 1s (call onEvtThink method)*/
  63. private Future<?> _aiTask;
  64. /** The delay after wich the attacked is stopped */
  65. private int _attackTimeout;
  66. /** The L2Attackable aggro counter */
  67. private int _globalAggro;
  68. /** The flag used to indicate that a thinking action is in progress */
  69. private boolean _thinking; // to prevent recursive thinking
  70. /**
  71. * Constructor of L2AttackableAI.<BR><BR>
  72. *
  73. * @param accessor The AI accessor of the L2Character
  74. *
  75. */
  76. public L2AttackableAI(L2Character.AIAccessor accessor)
  77. {
  78. super(accessor);
  79. _attackTimeout = Integer.MAX_VALUE;
  80. _globalAggro = -10; // 10 seconds timeout of ATTACK after respawn
  81. }
  82. public void run()
  83. {
  84. // Launch actions corresponding to the Event Think
  85. onEvtThink();
  86. }
  87. /**
  88. * Return True if the target is autoattackable (depends on the actor type).<BR><BR>
  89. *
  90. * <B><U> Actor is a L2GuardInstance</U> :</B><BR><BR>
  91. * <li>The target isn't a Folk or a Door</li>
  92. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  93. * <li>The target is in the actor Aggro range and is at the same height</li>
  94. * <li>The L2PcInstance target has karma (=PK)</li>
  95. * <li>The L2MonsterInstance target is aggressive</li><BR><BR>
  96. *
  97. * <B><U> Actor is a L2SiegeGuardInstance</U> :</B><BR><BR>
  98. * <li>The target isn't a Folk or a Door</li>
  99. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  100. * <li>The target is in the actor Aggro range and is at the same height</li>
  101. * <li>A siege is in progress</li>
  102. * <li>The L2PcInstance target isn't a Defender</li><BR><BR>
  103. *
  104. * <B><U> Actor is a L2FriendlyMobInstance</U> :</B><BR><BR>
  105. * <li>The target isn't a Folk, a Door or another L2NpcInstance</li>
  106. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  107. * <li>The target is in the actor Aggro range and is at the same height</li>
  108. * <li>The L2PcInstance target has karma (=PK)</li><BR><BR>
  109. *
  110. * <B><U> Actor is a L2MonsterInstance</U> :</B><BR><BR>
  111. * <li>The target isn't a Folk, a Door or another L2NpcInstance</li>
  112. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  113. * <li>The target is in the actor Aggro range and is at the same height</li>
  114. * <li>The actor is Aggressive</li><BR><BR>
  115. *
  116. * @param target The targeted L2Object
  117. *
  118. */
  119. private boolean autoAttackCondition(L2Character target)
  120. {
  121. if (target == null || !(_actor instanceof L2Attackable)) return false;
  122. L2Attackable me = (L2Attackable) _actor;
  123. // Check if the target isn't invulnerable
  124. if (target.isInvul())
  125. {
  126. // However EffectInvincible requires to check GMs specially
  127. if (target instanceof L2PcInstance && ((L2PcInstance)target).isGM())
  128. return false;
  129. if (target instanceof L2Summon && ((L2Summon)target).getOwner().isGM())
  130. return false;
  131. }
  132. // Check if the target isn't a Folk or a Door
  133. if (target instanceof L2FolkInstance || target instanceof L2DoorInstance) return false;
  134. // Check if the target isn't dead, is in the Aggro range and is at the same height
  135. if (target.isAlikeDead()
  136. || !me.isInsideRadius(target, me.getAggroRange(), false, false)
  137. || Math.abs(_actor.getZ() - target.getZ()) > 300) return false;
  138. // Check if the target is a L2PcInstance
  139. if (target instanceof L2PcInstance)
  140. {
  141. // Don't take the aggro if the GM has the access level below or equal to GM_DONT_TAKE_AGGRO
  142. if (((L2PcInstance)target).isGM() && ((L2PcInstance)target).getAccessLevel() <= Config.GM_DONT_TAKE_AGGRO)
  143. return false;
  144. // Check if the AI isn't a Raid Boss and the target isn't in silent move mode
  145. if (!(me instanceof L2RaidBossInstance) && ((L2PcInstance)target).isSilentMoving())
  146. return false;
  147. // Check if player is an ally //TODO! [Nemesiss] it should be rather boolean or smth like that
  148. // Comparing String isnt good idea!
  149. if (me.getFactionId() == "varka" && ((L2PcInstance)target).isAlliedWithVarka())
  150. return false;
  151. if (me.getFactionId() == "ketra" && ((L2PcInstance)target).isAlliedWithKetra())
  152. return false;
  153. // check if the target is within the grace period for JUST getting up from fake death
  154. if (((L2PcInstance)target).isRecentFakeDeath())
  155. return false;
  156. if (target.isInParty() && target.getParty().isInDimensionalRift())
  157. {
  158. byte riftType = target.getParty().getDimensionalRift().getType();
  159. byte riftRoom = target.getParty().getDimensionalRift().getCurrentRoom();
  160. if (me instanceof L2RiftInvaderInstance
  161. && !DimensionalRiftManager.getInstance().getRoom(riftType, riftRoom).checkIfInZone(me.getX(), me.getY(), me.getZ()))
  162. return false;
  163. }
  164. }
  165. // Check if the actor is a L2GuardInstance
  166. if (_actor instanceof L2GuardInstance)
  167. {
  168. // Check if the L2PcInstance target has karma (=PK)
  169. if (target instanceof L2PcInstance && ((L2PcInstance) target).getKarma() > 0)
  170. // Los Check
  171. return GeoData.getInstance().canSeeTarget(me, target);
  172. //if (target instanceof L2Summon)
  173. // return ((L2Summon)target).getKarma() > 0;
  174. // Check if the L2MonsterInstance target is aggressive
  175. if (target instanceof L2MonsterInstance)
  176. return (((L2MonsterInstance) target).isAggressive() && GeoData.getInstance().canSeeTarget(me, target));
  177. return false;
  178. }
  179. else if (_actor instanceof L2FriendlyMobInstance)
  180. { // the actor is a L2FriendlyMobInstance
  181. // Check if the target isn't another L2NpcInstance
  182. if (target instanceof L2NpcInstance) return false;
  183. // Check if the L2PcInstance target has karma (=PK)
  184. if (target instanceof L2PcInstance && ((L2PcInstance) target).getKarma() > 0)
  185. // Los Check
  186. return GeoData.getInstance().canSeeTarget(me, target);
  187. else
  188. return false;
  189. }
  190. else
  191. { //The actor is a L2MonsterInstance
  192. // Check if the target isn't another L2NpcInstance
  193. if (target instanceof L2NpcInstance) return false;
  194. // depending on config, do not allow mobs to attack _new_ players in peacezones,
  195. // unless they are already following those players from outside the peacezone.
  196. if (!Config.ALT_MOB_AGRO_IN_PEACEZONE && target.isInsideZone(L2Character.ZONE_PEACE))
  197. return false;
  198. // Check if the actor is Aggressive
  199. return (me.isAggressive() && GeoData.getInstance().canSeeTarget(me, target));
  200. }
  201. }
  202. public void startAITask()
  203. {
  204. // If not idle - create an AI task (schedule onEvtThink repeatedly)
  205. if (_aiTask == null)
  206. {
  207. _aiTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(this, 1000, 1000);
  208. }
  209. }
  210. public void stopAITask()
  211. {
  212. if (_aiTask != null)
  213. {
  214. _aiTask.cancel(false);
  215. _aiTask = null;
  216. }
  217. }
  218. @Override
  219. protected void onEvtDead()
  220. {
  221. stopAITask();
  222. super.onEvtDead();
  223. }
  224. /**
  225. * Set the Intention of this L2CharacterAI and create an AI Task executed every 1s (call onEvtThink method) for this L2Attackable.<BR><BR>
  226. *
  227. * <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>
  228. *
  229. * @param intention The new Intention to set to the AI
  230. * @param arg0 The first parameter of the Intention
  231. * @param arg1 The second parameter of the Intention
  232. *
  233. */
  234. @Override
  235. synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
  236. {
  237. if (intention == AI_INTENTION_IDLE || intention == AI_INTENTION_ACTIVE)
  238. {
  239. // Check if actor is not dead
  240. if (!_actor.isAlikeDead())
  241. {
  242. L2Attackable npc = (L2Attackable) _actor;
  243. // If its _knownPlayer isn't empty set the Intention to AI_INTENTION_ACTIVE
  244. if (npc.getKnownList().getKnownPlayers().size() > 0) intention = AI_INTENTION_ACTIVE;
  245. }
  246. if (intention == AI_INTENTION_IDLE)
  247. {
  248. // Set the Intention of this L2AttackableAI to AI_INTENTION_IDLE
  249. super.changeIntention(AI_INTENTION_IDLE, null, null);
  250. // Stop AI task and detach AI from NPC
  251. if (_aiTask != null)
  252. {
  253. _aiTask.cancel(true);
  254. _aiTask = null;
  255. }
  256. // Cancel the AI
  257. _accessor.detachAI();
  258. return;
  259. }
  260. }
  261. // Set the Intention of this L2AttackableAI to intention
  262. super.changeIntention(intention, arg0, arg1);
  263. // If not idle - create an AI task (schedule onEvtThink repeatedly)
  264. startAITask();
  265. }
  266. /**
  267. * Manage the Attack Intention : Stop current Attack (if necessary), Calculate attack timeout, Start a new Attack and Launch Think Event.<BR><BR>
  268. *
  269. * @param target The L2Character to attack
  270. *
  271. */
  272. @Override
  273. protected void onIntentionAttack(L2Character target)
  274. {
  275. // Calculate the attack timeout
  276. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  277. // Manage the Attack Intention : Stop current Attack (if necessary), Start a new Attack and Launch Think Event
  278. super.onIntentionAttack(target);
  279. }
  280. /**
  281. * Manage AI standard thinks of a L2Attackable (called by onEvtThink).<BR><BR>
  282. *
  283. * <B><U> Actions</U> :</B><BR><BR>
  284. * <li>Update every 1s the _globalAggro counter to come close to 0</li>
  285. * <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>
  286. * <li>If the actor is a L2GuardInstance that can't attack, order to it to return to its home location</li>
  287. * <li>If the actor is a L2MonsterInstance that can't attack, order to it to random walk (1/100)</li><BR><BR>
  288. *
  289. */
  290. private void thinkActive()
  291. {
  292. L2Attackable npc = (L2Attackable) _actor;
  293. // Update every 1s the _globalAggro counter to come close to 0
  294. if (_globalAggro != 0)
  295. {
  296. if (_globalAggro < 0) _globalAggro++;
  297. else _globalAggro--;
  298. }
  299. // Add all autoAttackable L2Character in L2Attackable Aggro Range to its _aggroList with 0 damage and 1 hate
  300. // A L2Attackable isn't aggressive during 10s after its spawn because _globalAggro is set to -10
  301. if (_globalAggro >= 0)
  302. {
  303. // Get all visible objects inside its Aggro Range
  304. //L2Object[] objects = L2World.getInstance().getVisibleObjects(_actor, ((L2NpcInstance)_actor).getAggroRange());
  305. // Go through visible objects
  306. for (L2Object obj : npc.getKnownList().getKnownObjects().values())
  307. {
  308. if (obj == null || !(obj instanceof L2Character)) continue;
  309. L2Character target = (L2Character) obj;
  310. /*
  311. * Check to see if this is a festival mob spawn.
  312. * If it is, then check to see if the aggro trigger
  313. * is a festival participant...if so, move to attack it.
  314. */
  315. if ((_actor instanceof L2FestivalMonsterInstance) && obj instanceof L2PcInstance)
  316. {
  317. L2PcInstance targetPlayer = (L2PcInstance) obj;
  318. if (!(targetPlayer.isFestivalParticipant())) continue;
  319. }
  320. // For each L2Character check if the target is autoattackable
  321. if (autoAttackCondition(target)) // check aggression
  322. {
  323. // Get the hate level of the L2Attackable against this L2Character target contained in _aggroList
  324. int hating = npc.getHating(target);
  325. // Add the attacker to the L2Attackable _aggroList with 0 damage and 1 hate
  326. if (hating == 0) npc.addDamageHate(target, 0, 1);
  327. }
  328. }
  329. // Chose a target from its aggroList
  330. L2Character hated;
  331. if (_actor.isConfused()) hated = getAttackTarget(); // Force mobs to attak anybody if confused
  332. else hated = npc.getMostHated();
  333. // Order to the L2Attackable to attack the target
  334. if (hated != null)
  335. {
  336. // Get the hate level of the L2Attackable against this L2Character target contained in _aggroList
  337. int aggro = npc.getHating(hated);
  338. if (aggro + _globalAggro > 0)
  339. {
  340. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  341. if (!_actor.isRunning()) _actor.setRunning();
  342. // Set the AI Intention to AI_INTENTION_ATTACK
  343. setIntention(CtrlIntention.AI_INTENTION_ATTACK, hated);
  344. }
  345. return;
  346. }
  347. }
  348. // Check if the actor is a L2GuardInstance
  349. if (_actor instanceof L2GuardInstance)
  350. {
  351. // Order to the L2GuardInstance to return to its home location because there's no target to attack
  352. ((L2GuardInstance) _actor).returnHome();
  353. }
  354. // If this is a festival monster, then it remains in the same location.
  355. if (_actor instanceof L2FestivalMonsterInstance) return;
  356. // Minions following leader
  357. if (_actor instanceof L2MinionInstance && ((L2MinionInstance)_actor).getLeader() != null)
  358. {
  359. int offset;
  360. if (_actor.isRaid()) offset = 500; // for Raids - need correction
  361. else offset = 200; // for normal minions - need correction :)
  362. if(((L2MinionInstance)_actor).getLeader().isRunning()) _actor.setRunning();
  363. else _actor.setWalking();
  364. if (_actor.getPlanDistanceSq(((L2MinionInstance)_actor).getLeader()) > offset*offset)
  365. {
  366. int x1, y1, z1;
  367. x1 = ((L2MinionInstance)_actor).getLeader().getX() + Rnd.nextInt( (offset - 30) * 2 ) - ( offset - 30 );
  368. y1 = ((L2MinionInstance)_actor).getLeader().getY() + Rnd.nextInt( (offset - 30) * 2 ) - ( offset - 30 );
  369. z1 = ((L2MinionInstance)_actor).getLeader().getZ();
  370. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  371. moveTo(x1, y1, z1);
  372. return;
  373. }
  374. }
  375. // Order to the L2MonsterInstance to random walk (1/100)
  376. else if (npc.getSpawn() != null && Rnd.nextInt(RANDOM_WALK_RATE) == 0)
  377. {
  378. int x1, y1, z1;
  379. // If NPC with random coord in territory
  380. if (npc.getSpawn().getLocx() == 0 && npc.getSpawn().getLocy() == 0)
  381. {
  382. // If NPC with random fixed coord, don't move
  383. if (Territory.getInstance().getProcMax(npc.getSpawn().getLocation()) > 0) return;
  384. // Calculate a destination point in the spawn area
  385. int p[] = Territory.getInstance().getRandomPoint(npc.getSpawn().getLocation());
  386. x1 = p[0];
  387. y1 = p[1];
  388. z1 = p[2];
  389. // Calculate the distance between the current position of the L2Character and the target (x,y)
  390. double distance2 = _actor.getPlanDistanceSq(x1, y1);
  391. if (distance2 > Config.MAX_DRIFT_RANGE * Config.MAX_DRIFT_RANGE)
  392. {
  393. npc.setisReturningToSpawnPoint(true);
  394. float delay = (float) Math.sqrt(distance2) / Config.MAX_DRIFT_RANGE;
  395. x1 = _actor.getX() + (int) ((x1 - _actor.getX()) / delay);
  396. y1 = _actor.getY() + (int) ((y1 - _actor.getY()) / delay);
  397. }
  398. else
  399. npc.setisReturningToSpawnPoint(false);
  400. }
  401. else
  402. {
  403. // If NPC with fixed coord
  404. x1 = npc.getSpawn().getLocx() + Rnd.nextInt(Config.MAX_DRIFT_RANGE * 2)
  405. - Config.MAX_DRIFT_RANGE;
  406. y1 = npc.getSpawn().getLocy() + Rnd.nextInt(Config.MAX_DRIFT_RANGE * 2)
  407. - Config.MAX_DRIFT_RANGE;
  408. z1 = npc.getZ();
  409. }
  410. //_log.config("Curent pos ("+getX()+", "+getY()+"), moving to ("+x1+", "+y1+").");
  411. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  412. moveTo(x1, y1, z1);
  413. }
  414. return;
  415. }
  416. /**
  417. * Manage AI attack thinks of a L2Attackable (called by onEvtThink).<BR><BR>
  418. *
  419. * <B><U> Actions</U> :</B><BR><BR>
  420. * <li>Update the attack timeout if actor is running</li>
  421. * <li>If target is dead or timeout is expired, stop this attack and set the Intention to AI_INTENTION_ACTIVE</li>
  422. * <li>Call all L2Object of its Faction inside the Faction Range</li>
  423. * <li>Chose a target and order to attack it with magic skill or physical attack</li><BR><BR>
  424. *
  425. * TODO: Manage casting rules to healer mobs (like Ant Nurses)
  426. *
  427. */
  428. private void thinkAttack()
  429. {
  430. if (_attackTimeout < GameTimeController.getGameTicks())
  431. {
  432. // Check if the actor is running
  433. if (_actor.isRunning())
  434. {
  435. // Set the actor movement type to walk and send Server->Client packet ChangeMoveType to all others L2PcInstance
  436. _actor.setWalking();
  437. // Calculate a new attack timeout
  438. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  439. }
  440. }
  441. // Check if target is dead or if timeout is expired to stop this attack
  442. if (getAttackTarget() == null || getAttackTarget().isAlikeDead()
  443. || _attackTimeout < GameTimeController.getGameTicks())
  444. {
  445. // Stop hating this target after the attack timeout or if target is dead
  446. if (getAttackTarget() != null)
  447. {
  448. L2Attackable npc = (L2Attackable) _actor;
  449. npc.stopHating(getAttackTarget());
  450. }
  451. // Set the AI Intention to AI_INTENTION_ACTIVE
  452. setIntention(AI_INTENTION_ACTIVE);
  453. _actor.setWalking();
  454. }
  455. else
  456. {
  457. // Call all L2Object of its Faction inside the Faction Range
  458. if (((L2NpcInstance) _actor).getFactionId() != null)
  459. {
  460. String faction_id = ((L2NpcInstance) _actor).getFactionId();
  461. // Go through all L2Object that belong to its faction
  462. for (L2Object obj : _actor.getKnownList().getKnownObjects().values())
  463. {
  464. if (obj instanceof L2NpcInstance)
  465. {
  466. L2NpcInstance npc = (L2NpcInstance) obj;
  467. if (npc == null || getAttackTarget() == null || faction_id != npc.getFactionId())
  468. continue;
  469. // Check if the L2Object is inside the Faction Range of the actor
  470. if (_actor.isInsideRadius(npc, npc.getFactionRange(), true, false)
  471. && GeoData.getInstance().canSeeTarget(_actor, npc)
  472. && Math.abs(getAttackTarget().getZ() - npc.getZ()) < 600
  473. && npc.getAI() != null
  474. && _actor.getAttackByList().contains(getAttackTarget())
  475. && (npc.getAI()._intention == CtrlIntention.AI_INTENTION_IDLE
  476. || npc.getAI()._intention == CtrlIntention.AI_INTENTION_ACTIVE))
  477. {
  478. if (getAttackTarget() instanceof L2PcInstance
  479. && getAttackTarget().isInParty()
  480. && getAttackTarget().getParty().isInDimensionalRift())
  481. {
  482. byte riftType = getAttackTarget().getParty().getDimensionalRift().getType();
  483. byte riftRoom = getAttackTarget().getParty().getDimensionalRift().getCurrentRoom();
  484. if (_actor instanceof L2RiftInvaderInstance
  485. && !DimensionalRiftManager.getInstance().getRoom(riftType, riftRoom).checkIfInZone(npc.getX(), npc.getY(), npc.getZ()))
  486. continue;
  487. }
  488. // Notify the L2Object AI with EVT_AGGRESSION
  489. npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, getAttackTarget(), 1);
  490. }
  491. }
  492. }
  493. }
  494. if(_actor.isAttackingDisabled()) return;
  495. // Get all information needed to chose between physical or magical attack
  496. L2Skill[] skills = null;
  497. double dist2 = 0;
  498. int range = 0;
  499. try
  500. {
  501. _actor.setTarget(getAttackTarget());
  502. skills = _actor.getAllSkills();
  503. dist2 = _actor.getPlanDistanceSq(getAttackTarget().getX(), getAttackTarget().getY());
  504. range = _actor.getPhysicalAttackRange() + _actor.getTemplate().collisionRadius + getAttackTarget().getTemplate().collisionRadius;
  505. }
  506. catch (NullPointerException e)
  507. {
  508. //_log.warning("AttackableAI: Attack target is NULL.");
  509. setIntention(AI_INTENTION_ACTIVE);
  510. return;
  511. }
  512. L2Weapon weapon = _actor.getActiveWeaponItem();
  513. if (weapon != null && weapon.getItemType() == L2WeaponType.BOW)
  514. {
  515. // Micht: kepping this one otherwise we should do 2 sqrt
  516. double distance2 = _actor.getPlanDistanceSq(getAttackTarget().getX(), getAttackTarget().getY());
  517. if (distance2 <= 10000)
  518. {
  519. int chance = 5;
  520. if (chance >= Rnd.get(100))
  521. {
  522. int posX = _actor.getX();
  523. int posY = _actor.getY();
  524. int posZ = _actor.getZ();
  525. double distance = Math.sqrt(distance2); // This way, we only do the sqrt if we need it
  526. int signx=-1;
  527. int signy=-1;
  528. if (_actor.getX()>getAttackTarget().getX())
  529. signx=1;
  530. if (_actor.getY()>getAttackTarget().getY())
  531. signy=1;
  532. posX += Math.round((float)((signx * ((range / 2) + (Rnd.get(range)))) - distance));
  533. posY += Math.round((float)((signy * ((range / 2) + (Rnd.get(range)))) - distance));
  534. setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, new L2CharPosition(posX, posY, posZ, 0));
  535. return;
  536. }
  537. }
  538. }
  539. // Force mobs to attack anybody if confused
  540. L2Character hated;
  541. if (_actor.isConfused()) hated = getAttackTarget();
  542. else hated = ((L2Attackable) _actor).getMostHated();
  543. if (hated == null)
  544. {
  545. setIntention(AI_INTENTION_ACTIVE);
  546. return;
  547. }
  548. if (hated != getAttackTarget())
  549. {
  550. setAttackTarget(hated);
  551. }
  552. // We should calculate new distance cuz mob can have changed the target
  553. dist2 = _actor.getPlanDistanceSq(hated.getX(), hated.getY());
  554. if (hated.isMoving()) range += 50;
  555. // Check if the actor isn't far from target
  556. if (dist2 > range*range)
  557. {
  558. // check for long ranged skills and heal/buff skills
  559. if (!_actor.isMuted() &&
  560. (!Config.ALT_GAME_MOB_ATTACK_AI || (_actor instanceof L2MonsterInstance && Rnd.nextInt(100) <= 5))
  561. )
  562. for (L2Skill sk : skills)
  563. {
  564. int castRange = sk.getCastRange();
  565. if (((sk.getSkillType() == L2Skill.SkillType.BUFF || sk.getSkillType() == L2Skill.SkillType.HEAL) || (dist2 >= castRange * castRange / 9.0)
  566. && (dist2 <= castRange * castRange) && (castRange > 70))
  567. && !_actor.isSkillDisabled(sk.getId())
  568. && _actor.getCurrentMp() >= _actor.getStat().getMpConsume(sk)
  569. && !sk.isPassive()
  570. && Rnd.nextInt(100) <= 5)
  571. {
  572. L2Object OldTarget = _actor.getTarget();
  573. if (sk.getSkillType() == L2Skill.SkillType.BUFF
  574. || sk.getSkillType() == L2Skill.SkillType.HEAL)
  575. {
  576. boolean useSkillSelf = true;
  577. if (sk.getSkillType() == L2Skill.SkillType.HEAL
  578. && _actor.getCurrentHp() > (int) (_actor.getMaxHp() / 1.5))
  579. {
  580. useSkillSelf = false;
  581. break;
  582. }
  583. if (sk.getSkillType() == L2Skill.SkillType.BUFF)
  584. {
  585. L2Effect[] effects = _actor.getAllEffects();
  586. for (int i = 0; effects != null && i < effects.length; i++)
  587. {
  588. L2Effect effect = effects[i];
  589. if (effect.getSkill() == sk)
  590. {
  591. useSkillSelf = false;
  592. break;
  593. }
  594. }
  595. }
  596. if (useSkillSelf) _actor.setTarget(_actor);
  597. }
  598. clientStopMoving(null);
  599. _accessor.doCast(sk);
  600. _actor.setTarget(OldTarget);
  601. return;
  602. }
  603. }
  604. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  605. if (hated.isMoving()) range -= 100; if (range < 5) range = 5;
  606. moveToPawn(getAttackTarget(), range);
  607. return;
  608. }
  609. // Else, if this is close enough to attack
  610. else
  611. {
  612. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  613. // check for close combat skills && heal/buff skills
  614. if (!_actor.isMuted() /*&& _rnd.nextInt(100) <= 5*/)
  615. {
  616. boolean useSkillSelf = true;;
  617. for (L2Skill sk : skills)
  618. {
  619. if (/*sk.getCastRange() >= dist && sk.getCastRange() <= 70 && */!sk.isPassive()
  620. && _actor.getCurrentMp() >= _actor.getStat().getMpConsume(sk)
  621. && !_actor.isSkillDisabled(sk.getId()) && (Rnd.nextInt(100) <= 8
  622. || (_actor instanceof L2PenaltyMonsterInstance && Rnd.nextInt(100) <= 20)))
  623. {
  624. L2Object OldTarget = _actor.getTarget();
  625. if (sk.getSkillType() == L2Skill.SkillType.BUFF
  626. || sk.getSkillType() == L2Skill.SkillType.HEAL)
  627. {
  628. useSkillSelf = true;
  629. if (sk.getSkillType() == L2Skill.SkillType.HEAL
  630. && _actor.getCurrentHp() > (int) (_actor.getMaxHp() / 1.5))
  631. {
  632. useSkillSelf = false;
  633. break;
  634. }
  635. if (sk.getSkillType() == L2Skill.SkillType.BUFF)
  636. {
  637. L2Effect[] effects = _actor.getAllEffects();
  638. for (int i = 0; effects != null && i < effects.length; i++)
  639. {
  640. L2Effect effect = effects[i];
  641. if (effect.getSkill() == sk)
  642. {
  643. useSkillSelf = false;
  644. break;
  645. }
  646. }
  647. }
  648. if (useSkillSelf) _actor.setTarget(_actor);
  649. }
  650. // GeoData Los Check here
  651. if (!useSkillSelf && !GeoData.getInstance().canSeeTarget(_actor, _actor.getTarget()))
  652. return;
  653. clientStopMoving(null);
  654. _accessor.doCast(sk);
  655. _actor.setTarget(OldTarget);
  656. return;
  657. }
  658. }
  659. }
  660. // Finally, physical attacks
  661. clientStopMoving(null);
  662. _accessor.doAttack(hated);
  663. }
  664. }
  665. }
  666. /**
  667. * Manage AI thinking actions of a L2Attackable.<BR><BR>
  668. */
  669. @Override
  670. protected void onEvtThink()
  671. {
  672. // Check if the actor can't use skills and if a thinking action isn't already in progress
  673. if (_thinking || _actor.isAllSkillsDisabled()) return;
  674. // Start thinking action
  675. _thinking = true;
  676. try
  677. {
  678. // Manage AI thinks of a L2Attackable
  679. if (getIntention() == AI_INTENTION_ACTIVE) thinkActive();
  680. else if (getIntention() == AI_INTENTION_ATTACK) thinkAttack();
  681. }
  682. finally
  683. {
  684. // Stop thinking action
  685. _thinking = false;
  686. }
  687. }
  688. /**
  689. * Launch actions corresponding to the Event Attacked.<BR><BR>
  690. *
  691. * <B><U> Actions</U> :</B><BR><BR>
  692. * <li>Init the attack : Calculate the attack timeout, Set the _globalAggro to 0, Add the attacker to the actor _aggroList</li>
  693. * <li>Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance</li>
  694. * <li>Set the Intention to AI_INTENTION_ATTACK</li><BR><BR>
  695. *
  696. * @param attacker The L2Character that attacks the actor
  697. *
  698. */
  699. @Override
  700. protected void onEvtAttacked(L2Character attacker)
  701. {
  702. //if (_actor instanceof L2ChestInstance && !((L2ChestInstance)_actor).isInteracted())
  703. //{
  704. //((L2ChestInstance)_actor).deleteMe();
  705. //((L2ChestInstance)_actor).getSpawn().startRespawn();
  706. //return;
  707. //}
  708. // Calculate the attack timeout
  709. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  710. // Set the _globalAggro to 0 to permit attack even just after spawn
  711. if (_globalAggro < 0) _globalAggro = 0;
  712. // Add the attacker to the _aggroList of the actor
  713. ((L2Attackable) _actor).addDamageHate(attacker, 0, 1);
  714. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  715. if (!_actor.isRunning()) _actor.setRunning();
  716. // Set the Intention to AI_INTENTION_ATTACK
  717. if (getIntention() != AI_INTENTION_ATTACK)
  718. {
  719. setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker);
  720. }
  721. else if (((L2Attackable) _actor).getMostHated() != getAttackTarget())
  722. {
  723. setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker);
  724. }
  725. super.onEvtAttacked(attacker);
  726. }
  727. /**
  728. * Launch actions corresponding to the Event Aggression.<BR><BR>
  729. *
  730. * <B><U> Actions</U> :</B><BR><BR>
  731. * <li>Add the target to the actor _aggroList or update hate if already present </li>
  732. * <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>
  733. *
  734. * @param attacker The L2Character that attacks
  735. * @param aggro The value of hate to add to the actor against the target
  736. *
  737. */
  738. @Override
  739. protected void onEvtAggression(L2Character target, int aggro)
  740. {
  741. L2Attackable me = (L2Attackable) _actor;
  742. if (target != null)
  743. {
  744. // Add the target to the actor _aggroList or update hate if already present
  745. me.addDamageHate(target, 0, aggro);
  746. // Set the actor AI Intention to AI_INTENTION_ATTACK
  747. if (getIntention() != CtrlIntention.AI_INTENTION_ATTACK)
  748. {
  749. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  750. if (!_actor.isRunning()) _actor.setRunning();
  751. setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
  752. }
  753. }
  754. }
  755. @Override
  756. protected void onIntentionActive()
  757. {
  758. // Cancel attack timeout
  759. _attackTimeout = Integer.MAX_VALUE;
  760. super.onIntentionActive();
  761. }
  762. public void setGlobalAggro(int value)
  763. {
  764. _globalAggro = value;
  765. }
  766. }