L2AttackableAI.java 63 KB

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