L2AttackableAI.java 57 KB

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