L2AttackableAI.java 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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. //Handle SevenSigns mob Factions
  609. String npcfaction = npc.getFactionId();
  610. boolean sevenSignFaction = false;
  611. // TODO: Unhardcode this by AI scripts (DrHouse)
  612. //Catacomb mobs should assist lilim and nephilim other than dungeon
  613. if ( (faction_id == "c_dungeon_clan") &&
  614. ((npcfaction == "c_dungeon_lilim") || npcfaction == "c_dungeon_nephi"))
  615. sevenSignFaction = true;
  616. //Lilim mobs should assist other Lilim and catacomb mobs
  617. else if ( (faction_id == "c_dungeon_lilim") &&
  618. (npcfaction == "c_dungeon_clan"))
  619. sevenSignFaction = true;
  620. //Nephilim mobs should assist other Nephilim and catacomb mobs
  621. else if ( (faction_id == "c_dungeon_nephi") &&
  622. (npcfaction == "c_dungeon_clan"))
  623. sevenSignFaction = true;
  624. if (faction_id != npc.getFactionId() && !sevenSignFaction)
  625. continue;
  626. // Check if the L2Object is inside the Faction Range of
  627. // the actor
  628. if (_actor.isInsideRadius(npc, npc.getFactionRange() + npc.getTemplate().collisionRadius, true, false) && npc.getAI() != null)
  629. {
  630. if (Math.abs(originalAttackTarget.getZ() - npc.getZ()) < 600 && _actor.getAttackByList().contains(originalAttackTarget)
  631. && (npc.getAI()._intention == CtrlIntention.AI_INTENTION_IDLE || npc.getAI()._intention == CtrlIntention.AI_INTENTION_ACTIVE) && GeoData.getInstance().canSeeTarget(_actor, npc))
  632. {
  633. if ((originalAttackTarget instanceof L2PcInstance) || (originalAttackTarget instanceof L2Summon))
  634. {
  635. if (npc.getTemplate().getEventQuests(Quest.QuestEventType.ON_FACTION_CALL) != null)
  636. {
  637. L2PcInstance player = (originalAttackTarget instanceof L2PcInstance) ? (L2PcInstance) originalAttackTarget : ((L2Summon) originalAttackTarget).getOwner();
  638. for (Quest quest : npc.getTemplate().getEventQuests(Quest.QuestEventType.ON_FACTION_CALL))
  639. quest.notifyFactionCall(npc, (L2NpcInstance) _actor, player, (originalAttackTarget instanceof L2Summon));
  640. }
  641. }
  642. }
  643. // heal or resurrect friends
  644. if (_selfAnalysis.hasHealOrResurrect && !_actor.isAttackingDisabled() && npc.getCurrentHp() < npc.getMaxHp() * 0.6 && _actor.getCurrentHp() > _actor.getMaxHp() / 2
  645. && _actor.getCurrentMp() > _actor.getMaxMp() / 2
  646. )
  647. {
  648. if (npc.isDead() && _actor instanceof L2MinionInstance)
  649. {
  650. if (((L2MinionInstance) _actor).getLeader() == npc)
  651. {
  652. for (L2Skill sk : _selfAnalysis.resurrectSkills)
  653. {
  654. if (_actor.getCurrentMp() < sk.getMpConsume())
  655. continue;
  656. if (_actor.isSkillDisabled(sk.getId()))
  657. continue;
  658. if (!Util.checkIfInRange(sk.getCastRange(), _actor, npc, true))
  659. continue;
  660. if (10 >= Rnd.get(100)) // chance
  661. continue;
  662. if (!GeoData.getInstance().canSeeTarget(_actor, npc))
  663. break;
  664. L2Object OldTarget = _actor.getTarget();
  665. _actor.setTarget(npc);
  666. // would this ever be fast enough
  667. // for the decay not to run?
  668. // giving some extra seconds
  669. DecayTaskManager.getInstance().cancelDecayTask(npc);
  670. DecayTaskManager.getInstance().addDecayTask(npc);
  671. clientStopMoving(null);
  672. _accessor.doCast(sk);
  673. _actor.setTarget(OldTarget);
  674. return;
  675. }
  676. }
  677. }
  678. else if (npc.isInCombat())
  679. {
  680. for (L2Skill sk : _selfAnalysis.healSkills)
  681. {
  682. if (_actor.getCurrentMp() < sk.getMpConsume())
  683. continue;
  684. if (_actor.isSkillDisabled(sk.getId()))
  685. continue;
  686. if (!Util.checkIfInRange(sk.getCastRange(), _actor, npc, true))
  687. continue;
  688. int chance = 4;
  689. if (_actor instanceof L2MinionInstance)
  690. {
  691. // minions support boss
  692. if (((L2MinionInstance) _actor).getLeader() == npc)
  693. chance = 6;
  694. else
  695. chance = 3;
  696. }
  697. if (npc instanceof L2GrandBossInstance)
  698. chance = 6;
  699. if (chance >= Rnd.get(100)) // chance
  700. continue;
  701. if (!GeoData.getInstance().canSeeTarget(_actor, npc))
  702. break;
  703. L2Object OldTarget = _actor.getTarget();
  704. _actor.setTarget(npc);
  705. clientStopMoving(null);
  706. _accessor.doCast(sk);
  707. _actor.setTarget(OldTarget);
  708. return;
  709. }
  710. }
  711. }
  712. }
  713. }
  714. }
  715. }
  716. }
  717. if (_actor.isAttackingDisabled())
  718. return;
  719. // Get 2 most hated chars
  720. List<L2Character> hated = ((L2Attackable) _actor).get2MostHated();
  721. if (_actor.isConfused())
  722. {
  723. if (hated != null)
  724. hated.set(0, originalAttackTarget); // effect handles selection
  725. else
  726. {
  727. hated = new FastList<L2Character>();
  728. hated.add(originalAttackTarget);
  729. hated.add(null);
  730. }
  731. }
  732. if (hated == null || hated.get(0) == null)
  733. {
  734. setIntention(AI_INTENTION_ACTIVE);
  735. return;
  736. }
  737. if (hated.get(0) != originalAttackTarget)
  738. {
  739. setAttackTarget(hated.get(0));
  740. }
  741. _mostHatedAnalysis.update(hated.get(0));
  742. _secondMostHatedAnalysis.update(hated.get(1));
  743. // Get all information needed to choose between physical or magical attack
  744. _actor.setTarget(_mostHatedAnalysis.character);
  745. double dist2 = _actor.getPlanDistanceSq(_mostHatedAnalysis.character.getX(), _mostHatedAnalysis.character.getY());
  746. int combinedCollision = _actor.getTemplate().collisionRadius + _mostHatedAnalysis.character.getTemplate().collisionRadius;
  747. int range = _actor.getPhysicalAttackRange() + combinedCollision;
  748. // Reconsider target next round if _actor hasn't got hits in for last 14 seconds
  749. if (!_actor.isMuted() && _attackTimeout - 160 < GameTimeController.getGameTicks() && _secondMostHatedAnalysis.character != null)
  750. {
  751. if (Util.checkIfInRange(900, _actor, hated.get(1), true))
  752. {
  753. // take off 2* the amount the aggro is larger than second most
  754. ((L2Attackable) _actor).reduceHate(hated.get(0), 2 * (((L2Attackable) _actor).getHating(hated.get(0)) - ((L2Attackable) _actor).getHating(hated.get(1))));
  755. // Calculate a new attack timeout
  756. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  757. }
  758. }
  759. // Reconsider target during next round if actor is rooted and cannot reach mostHated but can
  760. // reach secondMostHated
  761. if (_actor.isRooted() && _secondMostHatedAnalysis.character != null)
  762. {
  763. if (_selfAnalysis.isMage && dist2 > _selfAnalysis.maxCastRange * _selfAnalysis.maxCastRange
  764. && _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY()) < _selfAnalysis.maxCastRange * _selfAnalysis.maxCastRange)
  765. {
  766. ((L2Attackable) _actor).reduceHate(hated.get(0), 1 + (((L2Attackable) _actor).getHating(hated.get(0)) - ((L2Attackable) _actor).getHating(hated.get(1))));
  767. }
  768. else if (dist2 > range * range && _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY()) < range * range)
  769. {
  770. ((L2Attackable) _actor).reduceHate(hated.get(0), 1 + (((L2Attackable) _actor).getHating(hated.get(0)) - ((L2Attackable) _actor).getHating(hated.get(1))));
  771. }
  772. }
  773. // Considering, if bigger range will be attempted
  774. if ((dist2 < 10000 + combinedCollision * combinedCollision) && !_selfAnalysis.isFighter && !_selfAnalysis.isBalanced && (_selfAnalysis.hasLongRangeSkills || _selfAnalysis.isArcher)
  775. && (_mostHatedAnalysis.isBalanced || _mostHatedAnalysis.isFighter) && (_mostHatedAnalysis.character.isRooted() || _mostHatedAnalysis.isSlower) && (Config.GEODATA == 2 ? 20 : 12) >= Rnd.get(100) // chance
  776. )
  777. {
  778. int posX = _actor.getX();
  779. int posY = _actor.getY();
  780. int posZ = _actor.getZ();
  781. double distance = Math.sqrt(dist2); // This way, we only do the sqrt if we need it
  782. int signx = -1;
  783. int signy = -1;
  784. if (_actor.getX() > _mostHatedAnalysis.character.getX())
  785. signx = 1;
  786. if (_actor.getY() > _mostHatedAnalysis.character.getY())
  787. signy = 1;
  788. posX += Math.round((float) ((signx * ((range / 2) + (Rnd.get(range)))) - distance));
  789. posY += Math.round((float) ((signy * ((range / 2) + (Rnd.get(range)))) - distance));
  790. setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, new L2CharPosition(posX, posY, posZ, 0));
  791. return;
  792. }
  793. // Cannot see target, needs to go closer, currently just goes to range 300 if mage
  794. if ((dist2 > 310 * 310 + combinedCollision * combinedCollision) && this._selfAnalysis.hasLongRangeSkills && !GeoData.getInstance().canSeeTarget(_actor, _mostHatedAnalysis.character))
  795. {
  796. if (!(_selfAnalysis.isMage && _actor.isMuted()))
  797. {
  798. moveToPawn(_mostHatedAnalysis.character, 300);
  799. return;
  800. }
  801. }
  802. if (_mostHatedAnalysis.character.isMoving())
  803. range += 50;
  804. // Check if the actor is far from target
  805. if (dist2 > range * range)
  806. {
  807. if (!_actor.isMuted() && (_selfAnalysis.hasLongRangeSkills || !_selfAnalysis.healSkills.isEmpty()))
  808. {
  809. // check for long ranged skills and heal/buff skills
  810. if (!_mostHatedAnalysis.isCanceled)
  811. {
  812. for (L2Skill sk : _selfAnalysis.cancelSkills)
  813. {
  814. int castRange = sk.getCastRange() + combinedCollision;
  815. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  816. continue;
  817. if (Rnd.nextInt(100) <= 8)
  818. {
  819. clientStopMoving(null);
  820. _accessor.doCast(sk);
  821. _mostHatedAnalysis.isCanceled = true;
  822. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  823. return;
  824. }
  825. }
  826. }
  827. if (this._selfAnalysis.lastDebuffTick + 60 < GameTimeController.getGameTicks())
  828. {
  829. for (L2Skill sk : _selfAnalysis.debuffSkills)
  830. {
  831. int castRange = sk.getCastRange() + combinedCollision;
  832. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  833. continue;
  834. int chance = 8;
  835. if (_selfAnalysis.isFighter && _mostHatedAnalysis.isMage)
  836. chance = 3;
  837. if (_selfAnalysis.isFighter && _mostHatedAnalysis.isArcher)
  838. chance = 12;
  839. if (_selfAnalysis.isMage && !_mostHatedAnalysis.isMage)
  840. chance = 10;
  841. if (_mostHatedAnalysis.isMagicResistant)
  842. chance /= 2;
  843. if (Rnd.nextInt(100) <= chance)
  844. {
  845. clientStopMoving(null);
  846. _accessor.doCast(sk);
  847. _selfAnalysis.lastDebuffTick = GameTimeController.getGameTicks();
  848. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  849. return;
  850. }
  851. }
  852. }
  853. if (!_mostHatedAnalysis.character.isMuted())
  854. {
  855. int chance = 8;
  856. if (!(_mostHatedAnalysis.isMage || _mostHatedAnalysis.isBalanced))
  857. chance = 3;
  858. for (L2Skill sk : _selfAnalysis.muteSkills)
  859. {
  860. int castRange = sk.getCastRange() + combinedCollision;
  861. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  862. continue;
  863. if (Rnd.nextInt(100) <= chance)
  864. {
  865. clientStopMoving(null);
  866. _accessor.doCast(sk);
  867. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  868. return;
  869. }
  870. }
  871. }
  872. if (_secondMostHatedAnalysis.character != null && !_secondMostHatedAnalysis.character.isMuted() && (_secondMostHatedAnalysis.isMage || _secondMostHatedAnalysis.isBalanced))
  873. {
  874. double secondHatedDist2 = _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY());
  875. for (L2Skill sk : _selfAnalysis.muteSkills)
  876. {
  877. int castRange = sk.getCastRange() + combinedCollision;
  878. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (secondHatedDist2 > castRange * castRange))
  879. continue;
  880. if (Rnd.nextInt(100) <= 2)
  881. {
  882. _actor.setTarget(_secondMostHatedAnalysis.character);
  883. clientStopMoving(null);
  884. _accessor.doCast(sk);
  885. _actor.setTarget(_mostHatedAnalysis.character);
  886. return;
  887. }
  888. }
  889. }
  890. if (!_mostHatedAnalysis.character.isSleeping())
  891. {
  892. for (L2Skill sk : _selfAnalysis.sleepSkills)
  893. {
  894. int castRange = sk.getCastRange() + combinedCollision;
  895. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  896. continue;
  897. if (Rnd.nextInt(100) <= 1)
  898. {
  899. clientStopMoving(null);
  900. _accessor.doCast(sk);
  901. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  902. return;
  903. }
  904. }
  905. }
  906. if (_secondMostHatedAnalysis.character != null && !_secondMostHatedAnalysis.character.isSleeping())
  907. {
  908. double secondHatedDist2 = _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY());
  909. for (L2Skill sk : _selfAnalysis.sleepSkills)
  910. {
  911. int castRange = sk.getCastRange() + combinedCollision;
  912. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (secondHatedDist2 > castRange * castRange))
  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()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  930. continue;
  931. if (Rnd.nextInt(100) <= (_mostHatedAnalysis.isSlower ? 3 : 8))
  932. {
  933. clientStopMoving(null);
  934. _accessor.doCast(sk);
  935. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  936. return;
  937. }
  938. }
  939. }
  940. if (!_mostHatedAnalysis.character.isAttackingDisabled())
  941. {
  942. for (L2Skill sk : _selfAnalysis.generalDisablers)
  943. {
  944. int castRange = sk.getCastRange() + combinedCollision;
  945. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  946. continue;
  947. if (Rnd.nextInt(100) <= ((_selfAnalysis.isFighter && _actor.isRooted()) ? 15 : 7))
  948. {
  949. clientStopMoving(null);
  950. _accessor.doCast(sk);
  951. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  952. return;
  953. }
  954. }
  955. }
  956. if (_actor.getCurrentHp() < _actor.getMaxHp() * 0.4)
  957. {
  958. for (L2Skill sk : _selfAnalysis.healSkills)
  959. {
  960. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk))
  961. continue;
  962. int chance = 7;
  963. if (_mostHatedAnalysis.character.isAttackingDisabled())
  964. chance += 10;
  965. if (_secondMostHatedAnalysis.character == null || _secondMostHatedAnalysis.character.isAttackingDisabled())
  966. chance += 10;
  967. if (Rnd.nextInt(100) <= chance)
  968. {
  969. _actor.setTarget(_actor);
  970. clientStopMoving(null);
  971. _accessor.doCast(sk);
  972. _actor.setTarget(_mostHatedAnalysis.character);
  973. return;
  974. }
  975. }
  976. }
  977. // chance decision for launching long range skills
  978. int castingChance = 5;
  979. if (_selfAnalysis.isMage)
  980. castingChance = 50; // mages
  981. if (_selfAnalysis.isBalanced)
  982. {
  983. if (!_mostHatedAnalysis.isFighter) // advance to mages
  984. castingChance = 15;
  985. else
  986. castingChance = 25; // stay away from fighters
  987. }
  988. if (_selfAnalysis.isFighter)
  989. {
  990. if (_mostHatedAnalysis.isMage)
  991. castingChance = 3;
  992. else
  993. castingChance = 7;
  994. if (_actor.isRooted())
  995. castingChance = 20; // doesn't matter if no success first round
  996. }
  997. for (L2Skill sk : _selfAnalysis.generalSkills)
  998. {
  999. int castRange = sk.getCastRange() + combinedCollision;
  1000. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1001. continue;
  1002. if (Rnd.nextInt(100) <= castingChance)
  1003. {
  1004. clientStopMoving(null);
  1005. _accessor.doCast(sk);
  1006. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  1007. return;
  1008. }
  1009. }
  1010. }
  1011. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  1012. if (_selfAnalysis.isMage)
  1013. {
  1014. if (_actor.isMuted())
  1015. return;
  1016. range = _selfAnalysis.maxCastRange;
  1017. }
  1018. if (_mostHatedAnalysis.character.isMoving())
  1019. range -= 100;
  1020. if (range < 5)
  1021. range = 5;
  1022. moveToPawn(_mostHatedAnalysis.character, range);
  1023. return;
  1024. }
  1025. // **************************************************
  1026. // Else, if this is close enough for physical attacks
  1027. else
  1028. {
  1029. // In case many mobs are trying to hit from same place, move a bit,
  1030. // circling around the target
  1031. if (Rnd.nextInt(100) <= 33) // check it once per 3 seconds
  1032. {
  1033. for (L2Object nearby : _actor.getKnownList().getKnownCharactersInRadius(10))
  1034. {
  1035. if (nearby instanceof L2Attackable && nearby != _mostHatedAnalysis.character)
  1036. {
  1037. int diffx = Rnd.get(combinedCollision, combinedCollision + 40);
  1038. if (Rnd.get(10) < 5)
  1039. diffx = -diffx;
  1040. int diffy = Rnd.get(combinedCollision, combinedCollision + 40);
  1041. if (Rnd.get(10) < 5)
  1042. diffy = -diffy;
  1043. moveTo(_mostHatedAnalysis.character.getX() + diffx, _mostHatedAnalysis.character.getY() + diffy, _mostHatedAnalysis.character.getZ());
  1044. return;
  1045. }
  1046. }
  1047. }
  1048. // Calculate a new attack timeout.
  1049. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  1050. // check for close combat skills && heal/buff skills
  1051. if (!_mostHatedAnalysis.isCanceled)
  1052. {
  1053. for (L2Skill sk : _selfAnalysis.cancelSkills)
  1054. {
  1055. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1056. continue;
  1057. int castRange = sk.getCastRange() + combinedCollision;
  1058. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1059. continue;
  1060. if (Rnd.nextInt(100) <= 8)
  1061. {
  1062. clientStopMoving(null);
  1063. _accessor.doCast(sk);
  1064. _mostHatedAnalysis.isCanceled = true;
  1065. return;
  1066. }
  1067. }
  1068. }
  1069. if (this._selfAnalysis.lastDebuffTick + 60 < GameTimeController.getGameTicks())
  1070. {
  1071. for (L2Skill sk : _selfAnalysis.debuffSkills)
  1072. {
  1073. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1074. continue;
  1075. int castRange = sk.getCastRange() + combinedCollision;
  1076. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1077. continue;
  1078. int chance = 5;
  1079. if (_selfAnalysis.isFighter && _mostHatedAnalysis.isMage)
  1080. chance = 3;
  1081. if (_selfAnalysis.isFighter && _mostHatedAnalysis.isArcher)
  1082. chance = 3;
  1083. if (_selfAnalysis.isMage && !_mostHatedAnalysis.isMage)
  1084. chance = 4;
  1085. if (_mostHatedAnalysis.isMagicResistant)
  1086. chance /= 2;
  1087. if (sk.getCastRange() < 200)
  1088. chance += 3;
  1089. if (Rnd.nextInt(100) <= chance)
  1090. {
  1091. clientStopMoving(null);
  1092. _accessor.doCast(sk);
  1093. _selfAnalysis.lastDebuffTick = GameTimeController.getGameTicks();
  1094. return;
  1095. }
  1096. }
  1097. }
  1098. if (!_mostHatedAnalysis.character.isMuted() && (_mostHatedAnalysis.isMage || _mostHatedAnalysis.isBalanced))
  1099. {
  1100. for (L2Skill sk : _selfAnalysis.muteSkills)
  1101. {
  1102. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1103. continue;
  1104. int castRange = sk.getCastRange() + combinedCollision;
  1105. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1106. continue;
  1107. if (Rnd.nextInt(100) <= 7)
  1108. {
  1109. clientStopMoving(null);
  1110. _accessor.doCast(sk);
  1111. return;
  1112. }
  1113. }
  1114. }
  1115. if (_secondMostHatedAnalysis.character != null && !_secondMostHatedAnalysis.character.isMuted() && (_secondMostHatedAnalysis.isMage || _secondMostHatedAnalysis.isBalanced))
  1116. {
  1117. double secondHatedDist2 = _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY());
  1118. for (L2Skill sk : _selfAnalysis.muteSkills)
  1119. {
  1120. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1121. continue;
  1122. int castRange = sk.getCastRange() + combinedCollision;
  1123. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (secondHatedDist2 > castRange * castRange))
  1124. continue;
  1125. if (Rnd.nextInt(100) <= 3)
  1126. {
  1127. _actor.setTarget(_secondMostHatedAnalysis.character);
  1128. clientStopMoving(null);
  1129. _accessor.doCast(sk);
  1130. _actor.setTarget(_mostHatedAnalysis.character);
  1131. return;
  1132. }
  1133. }
  1134. }
  1135. if (_secondMostHatedAnalysis.character != null && !_secondMostHatedAnalysis.character.isSleeping())
  1136. {
  1137. double secondHatedDist2 = _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY());
  1138. for (L2Skill sk : _selfAnalysis.sleepSkills)
  1139. {
  1140. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1141. continue;
  1142. int castRange = sk.getCastRange() + combinedCollision;
  1143. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (secondHatedDist2 > castRange * castRange))
  1144. continue;
  1145. if (Rnd.nextInt(100) <= 4)
  1146. {
  1147. _actor.setTarget(_secondMostHatedAnalysis.character);
  1148. clientStopMoving(null);
  1149. _accessor.doCast(sk);
  1150. _actor.setTarget(_mostHatedAnalysis.character);
  1151. return;
  1152. }
  1153. }
  1154. }
  1155. if (!_mostHatedAnalysis.character.isRooted() && _mostHatedAnalysis.isFighter && !_selfAnalysis.isFighter)
  1156. {
  1157. for (L2Skill sk : _selfAnalysis.rootSkills)
  1158. {
  1159. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1160. continue;
  1161. int castRange = sk.getCastRange() + combinedCollision;
  1162. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1163. continue;
  1164. if (Rnd.nextInt(100) <= 4)
  1165. {
  1166. clientStopMoving(null);
  1167. _accessor.doCast(sk);
  1168. return;
  1169. }
  1170. }
  1171. }
  1172. if (!_mostHatedAnalysis.character.isAttackingDisabled())
  1173. {
  1174. for (L2Skill sk : _selfAnalysis.generalDisablers)
  1175. {
  1176. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1177. continue;
  1178. int castRange = sk.getCastRange() + combinedCollision;
  1179. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1180. continue;
  1181. if (Rnd.nextInt(100) <= ((sk.getCastRange() < 200) ? 10 : 7))
  1182. {
  1183. clientStopMoving(null);
  1184. _accessor.doCast(sk);
  1185. return;
  1186. }
  1187. }
  1188. }
  1189. if (_actor.getCurrentHp() < _actor.getMaxHp() * 0.4)
  1190. {
  1191. for (L2Skill sk : _selfAnalysis.healSkills)
  1192. {
  1193. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1194. continue;
  1195. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk))
  1196. continue;
  1197. int chance = 7;
  1198. if (_mostHatedAnalysis.character.isAttackingDisabled())
  1199. chance += 10;
  1200. if (_secondMostHatedAnalysis.character == null || _secondMostHatedAnalysis.character.isAttackingDisabled())
  1201. chance += 10;
  1202. if (Rnd.nextInt(100) <= chance)
  1203. {
  1204. _actor.setTarget(_actor);
  1205. clientStopMoving(null);
  1206. _accessor.doCast(sk);
  1207. _actor.setTarget(_mostHatedAnalysis.character);
  1208. return;
  1209. }
  1210. }
  1211. }
  1212. for (L2Skill sk : _selfAnalysis.generalSkills)
  1213. {
  1214. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1215. continue;
  1216. int castRange = sk.getCastRange() + combinedCollision;
  1217. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1218. continue;
  1219. // chance decision for launching general skills in melee fight
  1220. // close range skills should be higher, long range lower
  1221. int castingChance = 5;
  1222. if (_selfAnalysis.isMage)
  1223. {
  1224. if (sk.getCastRange() < 200)
  1225. castingChance = 35;
  1226. else
  1227. castingChance = 25; // mages
  1228. }
  1229. if (_selfAnalysis.isBalanced)
  1230. {
  1231. if (sk.getCastRange() < 200)
  1232. castingChance = 12;
  1233. else
  1234. {
  1235. if (_mostHatedAnalysis.isMage) // hit mages
  1236. castingChance = 2;
  1237. else
  1238. castingChance = 5;
  1239. }
  1240. }
  1241. if (_selfAnalysis.isFighter)
  1242. {
  1243. if (sk.getCastRange() < 200)
  1244. castingChance = 12;
  1245. else
  1246. {
  1247. if (_mostHatedAnalysis.isMage)
  1248. castingChance = 1;
  1249. else
  1250. castingChance = 3;
  1251. }
  1252. }
  1253. if (Rnd.nextInt(100) <= castingChance)
  1254. {
  1255. clientStopMoving(null);
  1256. _accessor.doCast(sk);
  1257. return;
  1258. }
  1259. }
  1260. // Finally, physical attacks
  1261. clientStopMoving(null);
  1262. _accessor.doAttack(getAttackTarget());
  1263. }
  1264. }
  1265. /**
  1266. * Manage AI thinking actions of a L2Attackable.<BR><BR>
  1267. */
  1268. @Override
  1269. protected void onEvtThink()
  1270. {
  1271. // Check if the actor can't use skills and if a thinking action isn't already in progress
  1272. if (_thinking || _actor.isAllSkillsDisabled())
  1273. return;
  1274. // Start thinking action
  1275. _thinking = true;
  1276. try
  1277. {
  1278. // Manage AI thinks of a L2Attackable
  1279. if (getIntention() == AI_INTENTION_ACTIVE)
  1280. thinkActive();
  1281. else if (getIntention() == AI_INTENTION_ATTACK)
  1282. thinkAttack();
  1283. }
  1284. finally
  1285. {
  1286. // Stop thinking action
  1287. _thinking = false;
  1288. }
  1289. }
  1290. /**
  1291. * Launch actions corresponding to the Event Attacked.<BR><BR>
  1292. *
  1293. * <B><U> Actions</U> :</B><BR><BR>
  1294. * <li>Init the attack : Calculate the attack timeout, Set the _globalAggro to 0, Add the attacker to the actor _aggroList</li>
  1295. * <li>Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance</li>
  1296. * <li>Set the Intention to AI_INTENTION_ATTACK</li><BR><BR>
  1297. *
  1298. * @param attacker The L2Character that attacks the actor
  1299. *
  1300. */
  1301. @Override
  1302. protected void onEvtAttacked(L2Character attacker)
  1303. {
  1304. //if (_actor instanceof L2ChestInstance && !((L2ChestInstance)_actor).isInteracted())
  1305. //{
  1306. //((L2ChestInstance)_actor).deleteMe();
  1307. //((L2ChestInstance)_actor).getSpawn().startRespawn();
  1308. //return;
  1309. //}
  1310. // Calculate the attack timeout
  1311. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  1312. // Set the _globalAggro to 0 to permit attack even just after spawn
  1313. if (_globalAggro < 0)
  1314. _globalAggro = 0;
  1315. // Add the attacker to the _aggroList of the actor
  1316. if (!((L2Attackable) _actor).isCoreAIDisabled())
  1317. ((L2Attackable) _actor).addDamageHate(attacker, 0, 1);
  1318. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  1319. if (!_actor.isRunning())
  1320. _actor.setRunning();
  1321. // Set the Intention to AI_INTENTION_ATTACK
  1322. if (getIntention() != AI_INTENTION_ATTACK && !((L2Attackable) _actor).isCoreAIDisabled())
  1323. {
  1324. setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker);
  1325. }
  1326. else if (((L2Attackable) _actor).getMostHated() != getAttackTarget() && !((L2Attackable) _actor).isCoreAIDisabled())
  1327. {
  1328. setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker);
  1329. }
  1330. else if (getIntention() != AI_INTENTION_INTERACT && ((L2Attackable) _actor).isCoreAIDisabled())
  1331. setIntention(CtrlIntention.AI_INTENTION_INTERACT, attacker);
  1332. super.onEvtAttacked(attacker);
  1333. }
  1334. /**
  1335. * Launch actions corresponding to the Event Aggression.<BR><BR>
  1336. *
  1337. * <B><U> Actions</U> :</B><BR><BR>
  1338. * <li>Add the target to the actor _aggroList or update hate if already present </li>
  1339. * <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>
  1340. *
  1341. * @param attacker The L2Character that attacks
  1342. * @param aggro The value of hate to add to the actor against the target
  1343. *
  1344. */
  1345. @Override
  1346. protected void onEvtAggression(L2Character target, int aggro)
  1347. {
  1348. L2Attackable me = (L2Attackable) _actor;
  1349. if (target != null)
  1350. {
  1351. // Add the target to the actor _aggroList or update hate if already present
  1352. me.addDamageHate(target, 0, aggro);
  1353. // Set the actor AI Intention to AI_INTENTION_ATTACK
  1354. if (getIntention() != CtrlIntention.AI_INTENTION_ATTACK)
  1355. {
  1356. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  1357. if (!_actor.isRunning())
  1358. _actor.setRunning();
  1359. setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
  1360. }
  1361. }
  1362. }
  1363. @Override
  1364. protected void onIntentionActive()
  1365. {
  1366. // Cancel attack timeout
  1367. _attackTimeout = Integer.MAX_VALUE;
  1368. super.onIntentionActive();
  1369. }
  1370. public void setGlobalAggro(int value)
  1371. {
  1372. _globalAggro = value;
  1373. }
  1374. }