L2AttackableAI.java 57 KB

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