L2AttackableAI.java 38 KB

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