L2AttackableAI.java 54 KB

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