L2AttackableAI.java 64 KB

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