L2AttackableAI.java 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.ai;
  20. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
  21. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  22. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.List;
  26. import java.util.Set;
  27. import java.util.concurrent.Future;
  28. import com.l2jserver.Config;
  29. import com.l2jserver.gameserver.GameTimeController;
  30. import com.l2jserver.gameserver.GeoData;
  31. import com.l2jserver.gameserver.ThreadPoolManager;
  32. import com.l2jserver.gameserver.datatables.NpcData;
  33. import com.l2jserver.gameserver.datatables.TerritoryTable;
  34. import com.l2jserver.gameserver.enums.AISkillScope;
  35. import com.l2jserver.gameserver.enums.AIType;
  36. import com.l2jserver.gameserver.instancemanager.DimensionalRiftManager;
  37. import com.l2jserver.gameserver.model.L2Object;
  38. import com.l2jserver.gameserver.model.Location;
  39. import com.l2jserver.gameserver.model.actor.L2Attackable;
  40. import com.l2jserver.gameserver.model.actor.L2Character;
  41. import com.l2jserver.gameserver.model.actor.L2Npc;
  42. import com.l2jserver.gameserver.model.actor.L2Playable;
  43. import com.l2jserver.gameserver.model.actor.L2Summon;
  44. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  45. import com.l2jserver.gameserver.model.actor.instance.L2FestivalMonsterInstance;
  46. import com.l2jserver.gameserver.model.actor.instance.L2FriendlyMobInstance;
  47. import com.l2jserver.gameserver.model.actor.instance.L2GrandBossInstance;
  48. import com.l2jserver.gameserver.model.actor.instance.L2GuardInstance;
  49. import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
  50. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  51. import com.l2jserver.gameserver.model.actor.instance.L2RaidBossInstance;
  52. import com.l2jserver.gameserver.model.actor.instance.L2RiftInvaderInstance;
  53. import com.l2jserver.gameserver.model.actor.instance.L2StaticObjectInstance;
  54. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  55. import com.l2jserver.gameserver.model.effects.L2EffectType;
  56. import com.l2jserver.gameserver.model.events.EventDispatcher;
  57. import com.l2jserver.gameserver.model.events.impl.character.npc.attackable.OnAttackableFactionCall;
  58. import com.l2jserver.gameserver.model.events.impl.character.npc.attackable.OnAttackableHate;
  59. import com.l2jserver.gameserver.model.events.returns.TerminateReturn;
  60. import com.l2jserver.gameserver.model.skills.Skill;
  61. import com.l2jserver.gameserver.model.skills.targets.L2TargetType;
  62. import com.l2jserver.gameserver.model.zone.ZoneId;
  63. import com.l2jserver.gameserver.util.Util;
  64. import com.l2jserver.util.Rnd;
  65. /**
  66. * This class manages AI of L2Attackable.
  67. */
  68. public class L2AttackableAI extends L2CharacterAI implements Runnable
  69. {
  70. private static final int RANDOM_WALK_RATE = 30; // confirmed
  71. // private static final int MAX_DRIFT_RANGE = 300;
  72. private static final int MAX_ATTACK_TIMEOUT = 1200; // int ticks, i.e. 2min
  73. /**
  74. * The L2Attackable AI task executed every 1s (call onEvtThink method).
  75. */
  76. private Future<?> _aiTask;
  77. /**
  78. * The delay after which the attacked is stopped.
  79. */
  80. private int _attackTimeout;
  81. /**
  82. * The L2Attackable aggro counter.
  83. */
  84. private int _globalAggro;
  85. /**
  86. * The flag used to indicate that a thinking action is in progress, to prevent recursive thinking.
  87. */
  88. private boolean _thinking;
  89. private int timepass = 0;
  90. private int chaostime = 0;
  91. private final L2NpcTemplate _skillrender;
  92. private List<Skill> shortRangeSkills = new ArrayList<>();
  93. private List<Skill> longRangeSkills = new ArrayList<>();
  94. int lastBuffTick;
  95. /**
  96. * Constructor of L2AttackableAI.
  97. * @param accessor The AI accessor of the L2Character
  98. */
  99. public L2AttackableAI(L2Character.AIAccessor accessor)
  100. {
  101. super(accessor);
  102. _skillrender = NpcData.getInstance().getTemplate(getActiveChar().getTemplate().getId());
  103. _attackTimeout = Integer.MAX_VALUE;
  104. _globalAggro = -10; // 10 seconds timeout of ATTACK after respawn
  105. }
  106. @Override
  107. public void run()
  108. {
  109. // Launch actions corresponding to the Event Think
  110. onEvtThink();
  111. }
  112. /**
  113. * <B><U> Actor is a L2GuardInstance</U> :</B>
  114. * <ul>
  115. * <li>The target isn't a Folk or a Door</li>
  116. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  117. * <li>The target is in the actor Aggro range and is at the same height</li>
  118. * <li>The L2PcInstance target has karma (=PK)</li>
  119. * <li>The L2MonsterInstance target is aggressive</li>
  120. * </ul>
  121. * <B><U> Actor is a L2SiegeGuardInstance</U> :</B>
  122. * <ul>
  123. * <li>The target isn't a Folk or a Door</li>
  124. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  125. * <li>The target is in the actor Aggro range and is at the same height</li>
  126. * <li>A siege is in progress</li>
  127. * <li>The L2PcInstance target isn't a Defender</li>
  128. * </ul>
  129. * <B><U> Actor is a L2FriendlyMobInstance</U> :</B>
  130. * <ul>
  131. * <li>The target isn't a Folk, a Door or another L2Npc</li>
  132. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  133. * <li>The target is in the actor Aggro range and is at the same height</li>
  134. * <li>The L2PcInstance target has karma (=PK)</li>
  135. * </ul>
  136. * <B><U> Actor is a L2MonsterInstance</U> :</B>
  137. * <ul>
  138. * <li>The target isn't a Folk, a Door or another L2Npc</li>
  139. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  140. * <li>The target is in the actor Aggro range and is at the same height</li>
  141. * <li>The actor is Aggressive</li>
  142. * </ul>
  143. * @param target The targeted L2Object
  144. * @return True if the target is autoattackable (depends on the actor type).
  145. */
  146. private boolean autoAttackCondition(L2Character target)
  147. {
  148. if ((target == null) || (getActiveChar() == null))
  149. {
  150. return false;
  151. }
  152. final L2Attackable me = getActiveChar();
  153. // Check if the target isn't invulnerable
  154. if (target.isInvul())
  155. {
  156. // However EffectInvincible requires to check GMs specially
  157. if ((target instanceof L2PcInstance) && ((L2PcInstance) target).isGM())
  158. {
  159. return false;
  160. }
  161. if ((target instanceof L2Summon) && ((L2Summon) target).getOwner().isGM())
  162. {
  163. return false;
  164. }
  165. }
  166. // Check if the target isn't a Folk or a Door
  167. if (target instanceof L2DoorInstance)
  168. {
  169. return false;
  170. }
  171. // Check if the target isn't dead, is in the Aggro range and is at the same height
  172. if (target.isAlikeDead() || ((target instanceof L2Playable) && !me.isInsideRadius(target, me.getAggroRange(), true, false)))
  173. {
  174. return false;
  175. }
  176. // Check if the target is a L2Playable
  177. if (target.isPlayable())
  178. {
  179. // Check if the AI isn't a Raid Boss, can See Silent Moving players and the target isn't in silent move mode
  180. if (!(me.isRaid()) && !(me.canSeeThroughSilentMove()) && ((L2Playable) target).isSilentMoving())
  181. {
  182. return false;
  183. }
  184. }
  185. // Gets the player if there is any.
  186. final L2PcInstance player = target.getActingPlayer();
  187. if (player != null)
  188. {
  189. // Don't take the aggro if the GM has the access level below or equal to GM_DONT_TAKE_AGGRO
  190. if (player.isGM() && !player.getAccessLevel().canTakeAggro())
  191. {
  192. return false;
  193. }
  194. // check if the target is within the grace period for JUST getting up from fake death
  195. if (player.isRecentFakeDeath())
  196. {
  197. return false;
  198. }
  199. if (player.isInParty() && player.getParty().isInDimensionalRift())
  200. {
  201. byte riftType = player.getParty().getDimensionalRift().getType();
  202. byte riftRoom = player.getParty().getDimensionalRift().getCurrentRoom();
  203. if ((me instanceof L2RiftInvaderInstance) && !DimensionalRiftManager.getInstance().getRoom(riftType, riftRoom).checkIfInZone(me.getX(), me.getY(), me.getZ()))
  204. {
  205. return false;
  206. }
  207. }
  208. }
  209. // Check if the actor is a L2GuardInstance
  210. if (me instanceof L2GuardInstance)
  211. {
  212. // Check if the L2PcInstance target has karma (=PK)
  213. if ((player != null) && (player.getKarma() > 0))
  214. {
  215. return GeoData.getInstance().canSeeTarget(me, player); // Los Check
  216. }
  217. // Check if the L2MonsterInstance target is aggressive
  218. if ((target instanceof L2MonsterInstance) && Config.GUARD_ATTACK_AGGRO_MOB)
  219. {
  220. return (((L2MonsterInstance) target).isAggressive() && GeoData.getInstance().canSeeTarget(me, target));
  221. }
  222. return false;
  223. }
  224. else if (me instanceof L2FriendlyMobInstance)
  225. {
  226. // Check if the target isn't another L2Npc
  227. if (target instanceof L2Npc)
  228. {
  229. return false;
  230. }
  231. // Check if the L2PcInstance target has karma (=PK)
  232. if ((target instanceof L2PcInstance) && (((L2PcInstance) target).getKarma() > 0))
  233. {
  234. return GeoData.getInstance().canSeeTarget(me, target); // Los Check
  235. }
  236. return false;
  237. }
  238. else
  239. {
  240. if (target instanceof L2Attackable)
  241. {
  242. if (!target.isAutoAttackable(me))
  243. {
  244. return false;
  245. }
  246. if (me.isChaos() && me.isInsideRadius(target, me.getAggroRange(), false, false))
  247. {
  248. if (((L2Attackable) target).isInMyClan(me))
  249. {
  250. return false;
  251. }
  252. // Los Check
  253. return GeoData.getInstance().canSeeTarget(me, target);
  254. }
  255. }
  256. if ((target instanceof L2Attackable) || (target instanceof L2Npc))
  257. {
  258. return false;
  259. }
  260. // depending on config, do not allow mobs to attack _new_ players in peacezones,
  261. // unless they are already following those players from outside the peacezone.
  262. if (!Config.ALT_MOB_AGRO_IN_PEACEZONE && target.isInsideZone(ZoneId.PEACE))
  263. {
  264. return false;
  265. }
  266. if (me.isChampion() && Config.L2JMOD_CHAMPION_PASSIVE)
  267. {
  268. return false;
  269. }
  270. // Check if the actor is Aggressive
  271. return (me.isAggressive() && GeoData.getInstance().canSeeTarget(me, target));
  272. }
  273. }
  274. public void startAITask()
  275. {
  276. // If not idle - create an AI task (schedule onEvtThink repeatedly)
  277. if (_aiTask == null)
  278. {
  279. _aiTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(this, 1000, 1000);
  280. }
  281. }
  282. @Override
  283. public void stopAITask()
  284. {
  285. if (_aiTask != null)
  286. {
  287. _aiTask.cancel(false);
  288. _aiTask = null;
  289. }
  290. super.stopAITask();
  291. }
  292. /**
  293. * Set the Intention of this L2CharacterAI and create an AI Task executed every 1s (call onEvtThink method) for this L2Attackable.<br>
  294. * <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>
  295. * @param intention The new Intention to set to the AI
  296. * @param arg0 The first parameter of the Intention
  297. * @param arg1 The second parameter of the Intention
  298. */
  299. @Override
  300. synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
  301. {
  302. if ((intention == AI_INTENTION_IDLE) || (intention == AI_INTENTION_ACTIVE))
  303. {
  304. // Check if actor is not dead
  305. L2Attackable npc = getActiveChar();
  306. if (!npc.isAlikeDead())
  307. {
  308. // If its _knownPlayer isn't empty set the Intention to AI_INTENTION_ACTIVE
  309. if (!npc.getKnownList().getKnownPlayers().isEmpty())
  310. {
  311. intention = AI_INTENTION_ACTIVE;
  312. }
  313. else
  314. {
  315. if (npc.getSpawn() != null)
  316. {
  317. final Location loc = npc.getSpawn().getLocation(npc);
  318. final int range = Config.MAX_DRIFT_RANGE;
  319. if (!npc.isInsideRadius(loc, range + range, true, false))
  320. {
  321. intention = AI_INTENTION_ACTIVE;
  322. }
  323. }
  324. }
  325. }
  326. if (intention == AI_INTENTION_IDLE)
  327. {
  328. // Set the Intention of this L2AttackableAI to AI_INTENTION_IDLE
  329. super.changeIntention(AI_INTENTION_IDLE, null, null);
  330. // Stop AI task and detach AI from NPC
  331. if (_aiTask != null)
  332. {
  333. _aiTask.cancel(true);
  334. _aiTask = null;
  335. }
  336. // Cancel the AI
  337. _accessor.detachAI();
  338. return;
  339. }
  340. }
  341. // Set the Intention of this L2AttackableAI to intention
  342. super.changeIntention(intention, arg0, arg1);
  343. // If not idle - create an AI task (schedule onEvtThink repeatedly)
  344. startAITask();
  345. }
  346. /**
  347. * Manage the Attack Intention : Stop current Attack (if necessary), Calculate attack timeout, Start a new Attack and Launch Think Event.
  348. * @param target The L2Character to attack
  349. */
  350. @Override
  351. protected void onIntentionAttack(L2Character target)
  352. {
  353. // Calculate the attack timeout
  354. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getInstance().getGameTicks();
  355. // self and buffs
  356. if ((lastBuffTick + 30) < GameTimeController.getInstance().getGameTicks())
  357. {
  358. for (Skill sk : _skillrender.getAISkills(AISkillScope.BUFF))
  359. {
  360. if (cast(sk))
  361. {
  362. break;
  363. }
  364. }
  365. lastBuffTick = GameTimeController.getInstance().getGameTicks();
  366. }
  367. // Manage the Attack Intention : Stop current Attack (if necessary), Start a new Attack and Launch Think Event
  368. super.onIntentionAttack(target);
  369. }
  370. private void thinkCast()
  371. {
  372. if (checkTargetLost(getCastTarget()))
  373. {
  374. setCastTarget(null);
  375. return;
  376. }
  377. if (maybeMoveToPawn(getCastTarget(), _actor.getMagicalAttackRange(_skill)))
  378. {
  379. return;
  380. }
  381. clientStopMoving(null);
  382. setIntention(AI_INTENTION_ACTIVE);
  383. _accessor.doCast(_skill);
  384. }
  385. /**
  386. * Manage AI standard thinks of a L2Attackable (called by onEvtThink). <B><U> Actions</U> :</B>
  387. * <ul>
  388. * <li>Update every 1s the _globalAggro counter to come close to 0</li>
  389. * <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>
  390. * <li>If the actor is a L2GuardInstance that can't attack, order to it to return to its home location</li>
  391. * <li>If the actor is a L2MonsterInstance that can't attack, order to it to random walk (1/100)</li>
  392. * </ul>
  393. */
  394. private void thinkActive()
  395. {
  396. L2Attackable npc = getActiveChar();
  397. // Update every 1s the _globalAggro counter to come close to 0
  398. if (_globalAggro != 0)
  399. {
  400. if (_globalAggro < 0)
  401. {
  402. _globalAggro++;
  403. }
  404. else
  405. {
  406. _globalAggro--;
  407. }
  408. }
  409. // Add all autoAttackable L2Character in L2Attackable Aggro Range to its _aggroList with 0 damage and 1 hate
  410. // A L2Attackable isn't aggressive during 10s after its spawn because _globalAggro is set to -10
  411. if (_globalAggro >= 0)
  412. {
  413. // Get all visible objects inside its Aggro Range
  414. Collection<L2Object> objs = npc.getKnownList().getKnownObjects().values();
  415. for (L2Object obj : objs)
  416. {
  417. if (!(obj instanceof L2Character) || (obj instanceof L2StaticObjectInstance))
  418. {
  419. continue;
  420. }
  421. L2Character target = (L2Character) obj;
  422. /*
  423. * Check to see if this is a festival mob spawn. If it is, then check to see if the aggro trigger is a festival participant...if so, move to attack it.
  424. */
  425. if ((npc instanceof L2FestivalMonsterInstance) && (obj instanceof L2PcInstance))
  426. {
  427. L2PcInstance targetPlayer = (L2PcInstance) obj;
  428. if (!(targetPlayer.isFestivalParticipant()))
  429. {
  430. continue;
  431. }
  432. }
  433. // For each L2Character check if the target is autoattackable
  434. if (autoAttackCondition(target)) // check aggression
  435. {
  436. if (target.isPlayable())
  437. {
  438. final TerminateReturn term = EventDispatcher.getInstance().notifyEvent(new OnAttackableHate(getActiveChar(), target.getActingPlayer(), target.isSummon()), getActiveChar(), TerminateReturn.class);
  439. if ((term != null) && term.terminate())
  440. {
  441. continue;
  442. }
  443. }
  444. // Get the hate level of the L2Attackable against this L2Character target contained in _aggroList
  445. int hating = npc.getHating(target);
  446. // Add the attacker to the L2Attackable _aggroList with 0 damage and 1 hate
  447. if (hating == 0)
  448. {
  449. npc.addDamageHate(target, 0, 0);
  450. }
  451. }
  452. }
  453. // Chose a target from its aggroList
  454. L2Character hated;
  455. if (npc.isConfused())
  456. {
  457. hated = getAttackTarget(); // effect handles selection
  458. }
  459. else
  460. {
  461. hated = npc.getMostHated();
  462. }
  463. // Order to the L2Attackable to attack the target
  464. if ((hated != null) && !npc.isCoreAIDisabled())
  465. {
  466. // Get the hate level of the L2Attackable against this L2Character target contained in _aggroList
  467. int aggro = npc.getHating(hated);
  468. if ((aggro + _globalAggro) > 0)
  469. {
  470. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  471. if (!npc.isRunning())
  472. {
  473. npc.setRunning();
  474. }
  475. // Set the AI Intention to AI_INTENTION_ATTACK
  476. setIntention(CtrlIntention.AI_INTENTION_ATTACK, hated);
  477. }
  478. return;
  479. }
  480. }
  481. // Chance to forget attackers after some time
  482. if ((npc.getCurrentHp() == npc.getMaxHp()) && (npc.getCurrentMp() == npc.getMaxMp()) && !npc.getAttackByList().isEmpty() && (Rnd.nextInt(500) == 0))
  483. {
  484. npc.clearAggroList();
  485. npc.getAttackByList().clear();
  486. if (npc instanceof L2MonsterInstance)
  487. {
  488. if (((L2MonsterInstance) npc).hasMinions())
  489. {
  490. ((L2MonsterInstance) npc).getMinionList().deleteReusedMinions();
  491. }
  492. }
  493. }
  494. // Check if the mob should not return to spawn point
  495. if (!npc.canReturnToSpawnPoint())
  496. {
  497. return;
  498. }
  499. // Check if the actor is a L2GuardInstance
  500. if ((npc instanceof L2GuardInstance) && !npc.isWalker())
  501. {
  502. // Order to the L2GuardInstance to return to its home location because there's no target to attack
  503. npc.returnHome();
  504. }
  505. // If this is a festival monster, then it remains in the same location.
  506. if (npc instanceof L2FestivalMonsterInstance)
  507. {
  508. return;
  509. }
  510. // Minions following leader
  511. final L2Character leader = npc.getLeader();
  512. if ((leader != null) && !leader.isAlikeDead())
  513. {
  514. final int offset;
  515. final int minRadius = 30;
  516. if (npc.isRaidMinion())
  517. {
  518. offset = 500; // for Raids - need correction
  519. }
  520. else
  521. {
  522. offset = 200; // for normal minions - need correction :)
  523. }
  524. if (leader.isRunning())
  525. {
  526. npc.setRunning();
  527. }
  528. else
  529. {
  530. npc.setWalking();
  531. }
  532. if (npc.calculateDistance(leader, false, true) > (offset * offset))
  533. {
  534. int x1, y1, z1;
  535. x1 = Rnd.get(minRadius * 2, offset * 2); // x
  536. y1 = Rnd.get(x1, offset * 2); // distance
  537. y1 = (int) Math.sqrt((y1 * y1) - (x1 * x1)); // y
  538. if (x1 > (offset + minRadius))
  539. {
  540. x1 = (leader.getX() + x1) - offset;
  541. }
  542. else
  543. {
  544. x1 = (leader.getX() - x1) + minRadius;
  545. }
  546. if (y1 > (offset + minRadius))
  547. {
  548. y1 = (leader.getY() + y1) - offset;
  549. }
  550. else
  551. {
  552. y1 = (leader.getY() - y1) + minRadius;
  553. }
  554. z1 = leader.getZ();
  555. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  556. moveTo(x1, y1, z1);
  557. return;
  558. }
  559. else if (Rnd.nextInt(RANDOM_WALK_RATE) == 0)
  560. {
  561. for (Skill sk : _skillrender.getAISkills(AISkillScope.BUFF))
  562. {
  563. if (cast(sk))
  564. {
  565. return;
  566. }
  567. }
  568. }
  569. }
  570. // Order to the L2MonsterInstance to random walk (1/100)
  571. else if ((npc.getSpawn() != null) && (Rnd.nextInt(RANDOM_WALK_RATE) == 0) && !npc.isNoRndWalk())
  572. {
  573. int x1, y1, z1;
  574. final int range = Config.MAX_DRIFT_RANGE;
  575. for (Skill sk : _skillrender.getAISkills(AISkillScope.BUFF))
  576. {
  577. if (cast(sk))
  578. {
  579. return;
  580. }
  581. }
  582. // If NPC with random coord in territory - old method (for backward compartibility)
  583. if ((npc.getSpawn().getX() == 0) && (npc.getSpawn().getY() == 0) && (npc.getSpawn().getSpawnTerritory() == null))
  584. {
  585. // Calculate a destination point in the spawn area
  586. int p[] = TerritoryTable.getInstance().getRandomPoint(npc.getSpawn().getLocationId());
  587. x1 = p[0];
  588. y1 = p[1];
  589. z1 = p[2];
  590. // Calculate the distance between the current position of the L2Character and the target (x,y)
  591. double distance2 = npc.calculateDistance(x1, y1, 0, false, true);
  592. if (distance2 > ((range + range) * (range + range)))
  593. {
  594. npc.setisReturningToSpawnPoint(true);
  595. float delay = (float) Math.sqrt(distance2) / range;
  596. x1 = npc.getX() + (int) ((x1 - npc.getX()) / delay);
  597. y1 = npc.getY() + (int) ((y1 - npc.getY()) / delay);
  598. }
  599. // If NPC with random fixed coord, don't move (unless needs to return to spawnpoint)
  600. if ((TerritoryTable.getInstance().getProcMax(npc.getSpawn().getLocationId()) > 0) && !npc.isReturningToSpawnPoint())
  601. {
  602. return;
  603. }
  604. }
  605. else
  606. {
  607. x1 = npc.getSpawn().getX(npc);
  608. y1 = npc.getSpawn().getY(npc);
  609. z1 = npc.getSpawn().getZ(npc);
  610. if (!npc.isInsideRadius(x1, y1, 0, range, false, false))
  611. {
  612. npc.setisReturningToSpawnPoint(true);
  613. }
  614. else
  615. {
  616. int deltaX = Rnd.nextInt(range * 2); // x
  617. int deltaY = Rnd.get(deltaX, range * 2); // distance
  618. deltaY = (int) Math.sqrt((deltaY * deltaY) - (deltaX * deltaX)); // y
  619. x1 = (deltaX + x1) - range;
  620. y1 = (deltaY + y1) - range;
  621. z1 = npc.getZ();
  622. }
  623. }
  624. // _log.debug("Current pos ("+getX()+", "+getY()+"), moving to ("+x1+", "+y1+").");
  625. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  626. moveTo(x1, y1, z1);
  627. }
  628. }
  629. /**
  630. * Manage AI attack thinks of a L2Attackable (called by onEvtThink). <B><U> Actions</U> :</B>
  631. * <ul>
  632. * <li>Update the attack timeout if actor is running</li>
  633. * <li>If target is dead or timeout is expired, stop this attack and set the Intention to AI_INTENTION_ACTIVE</li>
  634. * <li>Call all L2Object of its Faction inside the Faction Range</li>
  635. * <li>Chose a target and order to attack it with magic skill or physical attack</li>
  636. * </ul>
  637. * TODO: Manage casting rules to healer mobs (like Ant Nurses)
  638. */
  639. private void thinkAttack()
  640. {
  641. final L2Attackable npc = getActiveChar();
  642. if (npc.isCastingNow())
  643. {
  644. return;
  645. }
  646. L2Character originalAttackTarget = getAttackTarget();
  647. // Check if target is dead or if timeout is expired to stop this attack
  648. if ((originalAttackTarget == null) || originalAttackTarget.isAlikeDead() || (_attackTimeout < GameTimeController.getInstance().getGameTicks()))
  649. {
  650. // Stop hating this target after the attack timeout or if target is dead
  651. if (originalAttackTarget != null)
  652. {
  653. npc.stopHating(originalAttackTarget);
  654. }
  655. // Set the AI Intention to AI_INTENTION_ACTIVE
  656. setIntention(AI_INTENTION_ACTIVE);
  657. npc.setWalking();
  658. return;
  659. }
  660. final int collision = npc.getTemplate().getCollisionRadius();
  661. // Handle all L2Object of its Faction inside the Faction Range
  662. Set<Integer> clans = getActiveChar().getTemplate().getClans();
  663. if ((clans != null) && !clans.isEmpty())
  664. {
  665. int factionRange = npc.getTemplate().getClanHelpRange() + collision;
  666. // Go through all L2Object that belong to its faction
  667. Collection<L2Object> objs = npc.getKnownList().getKnownObjects().values();
  668. try
  669. {
  670. for (L2Object obj : objs)
  671. {
  672. if (obj instanceof L2Npc)
  673. {
  674. L2Npc called = (L2Npc) obj;
  675. boolean sevenSignFaction = false;
  676. // TODO: Unhardcode this by AI scripts (DrHouse)
  677. // Catacomb mobs should assist lilim and nephilim other than dungeon
  678. if (getActiveChar().getTemplate().isClan("c_dungeon_clan", "c_dungeon_lilim", "c_dungeon_nephi") && called.getTemplate().isClan("c_dungeon_clan", "c_dungeon_lilim", "c_dungeon_nephi"))
  679. {
  680. sevenSignFaction = true;
  681. }
  682. if (!getActiveChar().getTemplate().isClan(called.getTemplate().getClans()) && !sevenSignFaction)
  683. {
  684. continue;
  685. }
  686. // Check if the L2Object is inside the Faction Range of the actor
  687. if (npc.isInsideRadius(called, factionRange, true, false) && called.hasAI())
  688. {
  689. if ((Math.abs(originalAttackTarget.getZ() - called.getZ()) < 600) && npc.getAttackByList().contains(originalAttackTarget) && ((called.getAI()._intention == CtrlIntention.AI_INTENTION_IDLE) || (called.getAI()._intention == CtrlIntention.AI_INTENTION_ACTIVE)) && (called.getInstanceId() == npc.getInstanceId()))
  690. {
  691. if (originalAttackTarget.isPlayable())
  692. {
  693. if (originalAttackTarget.isInParty() && originalAttackTarget.getParty().isInDimensionalRift())
  694. {
  695. byte riftType = originalAttackTarget.getParty().getDimensionalRift().getType();
  696. byte riftRoom = originalAttackTarget.getParty().getDimensionalRift().getCurrentRoom();
  697. if ((npc instanceof L2RiftInvaderInstance) && !DimensionalRiftManager.getInstance().getRoom(riftType, riftRoom).checkIfInZone(npc.getX(), npc.getY(), npc.getZ()))
  698. {
  699. continue;
  700. }
  701. }
  702. // By default, when a faction member calls for help, attack the caller's attacker.
  703. // Notify the AI with EVT_AGGRESSION
  704. npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, originalAttackTarget, 1);
  705. EventDispatcher.getInstance().notifyEventAsync(new OnAttackableFactionCall(called, getActiveChar(), originalAttackTarget.getActingPlayer(), originalAttackTarget.isSummon()), called);
  706. }
  707. else if ((called instanceof L2Attackable) && (getAttackTarget() != null) && (called.getAI()._intention != CtrlIntention.AI_INTENTION_ATTACK))
  708. {
  709. ((L2Attackable) called).addDamageHate(getAttackTarget(), 0, npc.getHating(getAttackTarget()));
  710. called.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, getAttackTarget());
  711. }
  712. }
  713. }
  714. }
  715. }
  716. }
  717. catch (NullPointerException e)
  718. {
  719. _log.warning(getClass().getSimpleName() + ": thinkAttack() faction call failed: " + e.getMessage());
  720. }
  721. }
  722. if (npc.isCoreAIDisabled())
  723. {
  724. return;
  725. }
  726. // Initialize data
  727. L2Character mostHate = npc.getMostHated();
  728. if (mostHate == null)
  729. {
  730. setIntention(AI_INTENTION_ACTIVE);
  731. return;
  732. }
  733. setAttackTarget(mostHate);
  734. npc.setTarget(mostHate);
  735. final int combinedCollision = collision + mostHate.getTemplate().getCollisionRadius();
  736. List<Skill> aiSuicideSkills = _skillrender.getAISkills(AISkillScope.SUICIDE);
  737. if (!aiSuicideSkills.isEmpty() && ((int) ((npc.getCurrentHp() / npc.getMaxHp()) * 100) < 30))
  738. {
  739. final Skill skill = aiSuicideSkills.get(Rnd.nextInt(aiSuicideSkills.size()));
  740. if (Util.checkIfInRange(skill.getAffectRange(), getActiveChar(), mostHate, false) && (Rnd.get(100) < Rnd.get(npc.getMinSkillChance(), npc.getMaxSkillChance())))
  741. {
  742. if (cast(skill))
  743. {
  744. return;
  745. }
  746. for (Skill sk : aiSuicideSkills)
  747. {
  748. if (cast(sk))
  749. {
  750. return;
  751. }
  752. }
  753. }
  754. }
  755. // ------------------------------------------------------
  756. // In case many mobs are trying to hit from same place, move a bit,
  757. // circling around the target
  758. // Note from Gnacik:
  759. // On l2js because of that sometimes mobs don't attack player only running
  760. // around player without any sense, so decrease chance for now
  761. if (!npc.isMovementDisabled() && (Rnd.nextInt(100) <= 3))
  762. {
  763. for (L2Object nearby : npc.getKnownList().getKnownObjects().values())
  764. {
  765. if ((nearby instanceof L2Attackable) && npc.isInsideRadius(nearby, collision, false, false) && (nearby != mostHate))
  766. {
  767. int newX = combinedCollision + Rnd.get(40);
  768. if (Rnd.nextBoolean())
  769. {
  770. newX = mostHate.getX() + newX;
  771. }
  772. else
  773. {
  774. newX = mostHate.getX() - newX;
  775. }
  776. int newY = combinedCollision + Rnd.get(40);
  777. if (Rnd.nextBoolean())
  778. {
  779. newY = mostHate.getY() + newY;
  780. }
  781. else
  782. {
  783. newY = mostHate.getY() - newY;
  784. }
  785. if (!npc.isInsideRadius(newX, newY, 0, collision, false, false))
  786. {
  787. int newZ = npc.getZ() + 30;
  788. if ((Config.GEODATA == 0) || GeoData.getInstance().canMove(npc.getX(), npc.getY(), npc.getZ(), newX, newY, newZ, npc.getInstanceId()))
  789. {
  790. moveTo(newX, newY, newZ);
  791. }
  792. }
  793. return;
  794. }
  795. }
  796. }
  797. // Dodge if its needed
  798. if (!npc.isMovementDisabled() && (npc.getDodge() > 0))
  799. {
  800. if (Rnd.get(100) <= npc.getDodge())
  801. {
  802. // Micht: kepping this one otherwise we should do 2 sqrt
  803. double distance2 = npc.calculateDistance(mostHate, false, true);
  804. if (Math.sqrt(distance2) <= (60 + combinedCollision))
  805. {
  806. int posX = npc.getX();
  807. int posY = npc.getY();
  808. int posZ = npc.getZ() + 30;
  809. if (originalAttackTarget.getX() < posX)
  810. {
  811. posX = posX + 300;
  812. }
  813. else
  814. {
  815. posX = posX - 300;
  816. }
  817. if (originalAttackTarget.getY() < posY)
  818. {
  819. posY = posY + 300;
  820. }
  821. else
  822. {
  823. posY = posY - 300;
  824. }
  825. if ((Config.GEODATA == 0) || GeoData.getInstance().canMove(npc.getX(), npc.getY(), npc.getZ(), posX, posY, posZ, npc.getInstanceId()))
  826. {
  827. setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, new Location(posX, posY, posZ, 0));
  828. }
  829. return;
  830. }
  831. }
  832. }
  833. // ------------------------------------------------------------------------------
  834. // BOSS/Raid Minion Target Reconsider
  835. if (npc.isRaid() || npc.isRaidMinion())
  836. {
  837. chaostime++;
  838. if (npc instanceof L2RaidBossInstance)
  839. {
  840. if (!((L2MonsterInstance) npc).hasMinions())
  841. {
  842. if (chaostime > Config.RAID_CHAOS_TIME)
  843. {
  844. if (Rnd.get(100) <= (100 - ((npc.getCurrentHp() * 100) / npc.getMaxHp())))
  845. {
  846. aggroReconsider();
  847. chaostime = 0;
  848. return;
  849. }
  850. }
  851. }
  852. else
  853. {
  854. if (chaostime > Config.RAID_CHAOS_TIME)
  855. {
  856. if (Rnd.get(100) <= (100 - ((npc.getCurrentHp() * 200) / npc.getMaxHp())))
  857. {
  858. aggroReconsider();
  859. chaostime = 0;
  860. return;
  861. }
  862. }
  863. }
  864. }
  865. else if (npc instanceof L2GrandBossInstance)
  866. {
  867. if (chaostime > Config.GRAND_CHAOS_TIME)
  868. {
  869. double chaosRate = 100 - ((npc.getCurrentHp() * 300) / npc.getMaxHp());
  870. if (((chaosRate <= 10) && (Rnd.get(100) <= 10)) || ((chaosRate > 10) && (Rnd.get(100) <= chaosRate)))
  871. {
  872. aggroReconsider();
  873. chaostime = 0;
  874. return;
  875. }
  876. }
  877. }
  878. else
  879. {
  880. if (chaostime > Config.MINION_CHAOS_TIME)
  881. {
  882. if (Rnd.get(100) <= (100 - ((npc.getCurrentHp() * 200) / npc.getMaxHp())))
  883. {
  884. aggroReconsider();
  885. chaostime = 0;
  886. return;
  887. }
  888. }
  889. }
  890. }
  891. if (!_skillrender.getAISkills(AISkillScope.GENERAL).isEmpty())
  892. {
  893. // -------------------------------------------------------------------------------
  894. // Heal Condition
  895. List<Skill> aiHealSkills = _skillrender.getAISkills(AISkillScope.HEAL);
  896. if (!aiHealSkills.isEmpty())
  897. {
  898. double percentage = (npc.getCurrentHp() / npc.getMaxHp()) * 100;
  899. if (npc.isMinion())
  900. {
  901. L2Character leader = npc.getLeader();
  902. if ((leader != null) && !leader.isDead() && (Rnd.get(100) > ((leader.getCurrentHp() / leader.getMaxHp()) * 100)))
  903. {
  904. for (Skill sk : aiHealSkills)
  905. {
  906. if (sk.getTargetType() == L2TargetType.SELF)
  907. {
  908. continue;
  909. }
  910. if (!checkSkillCastConditions(sk))
  911. {
  912. continue;
  913. }
  914. if (!Util.checkIfInRange((sk.getCastRange() + collision + leader.getTemplate().getCollisionRadius()), npc, leader, false) && !isParty(sk) && !npc.isMovementDisabled())
  915. {
  916. moveToPawn(leader, sk.getCastRange() + collision + leader.getTemplate().getCollisionRadius());
  917. return;
  918. }
  919. if (GeoData.getInstance().canSeeTarget(npc, leader))
  920. {
  921. clientStopMoving(null);
  922. npc.setTarget(leader);
  923. clientStopMoving(null);
  924. npc.doCast(sk);
  925. return;
  926. }
  927. }
  928. }
  929. }
  930. if (Rnd.get(100) < ((100 - percentage) / 3))
  931. {
  932. for (Skill sk : aiHealSkills)
  933. {
  934. if (!checkSkillCastConditions(sk))
  935. {
  936. continue;
  937. }
  938. clientStopMoving(null);
  939. npc.setTarget(npc);
  940. npc.doCast(sk);
  941. return;
  942. }
  943. }
  944. for (Skill sk : aiHealSkills)
  945. {
  946. if (!checkSkillCastConditions(sk))
  947. {
  948. continue;
  949. }
  950. if (sk.getTargetType() == L2TargetType.ONE)
  951. {
  952. for (L2Character obj : npc.getKnownList().getKnownCharactersInRadius(sk.getCastRange() + collision))
  953. {
  954. if (!(obj instanceof L2Attackable) || obj.isDead())
  955. {
  956. continue;
  957. }
  958. L2Attackable targets = ((L2Attackable) obj);
  959. if (!((L2Attackable) obj).isInMyClan(npc))
  960. {
  961. continue;
  962. }
  963. percentage = (targets.getCurrentHp() / targets.getMaxHp()) * 100;
  964. if (Rnd.get(100) < ((100 - percentage) / 10))
  965. {
  966. if (GeoData.getInstance().canSeeTarget(npc, targets))
  967. {
  968. clientStopMoving(null);
  969. npc.setTarget(obj);
  970. npc.doCast(sk);
  971. return;
  972. }
  973. }
  974. }
  975. }
  976. if (isParty(sk))
  977. {
  978. clientStopMoving(null);
  979. npc.doCast(sk);
  980. return;
  981. }
  982. }
  983. }
  984. // -------------------------------------------------------------------------------
  985. // Res Skill Condition
  986. List<Skill> aiResSkills = _skillrender.getAISkills(AISkillScope.RES);
  987. if (!aiResSkills.isEmpty())
  988. {
  989. if (npc.isMinion())
  990. {
  991. L2Character leader = npc.getLeader();
  992. if ((leader != null) && leader.isDead())
  993. {
  994. for (Skill sk : aiResSkills)
  995. {
  996. if (sk.getTargetType() == L2TargetType.SELF)
  997. {
  998. continue;
  999. }
  1000. if (!checkSkillCastConditions(sk))
  1001. {
  1002. continue;
  1003. }
  1004. if (!Util.checkIfInRange((sk.getCastRange() + collision + leader.getTemplate().getCollisionRadius()), npc, leader, false) && !isParty(sk) && !npc.isMovementDisabled())
  1005. {
  1006. moveToPawn(leader, sk.getCastRange() + collision + leader.getTemplate().getCollisionRadius());
  1007. return;
  1008. }
  1009. if (GeoData.getInstance().canSeeTarget(npc, leader))
  1010. {
  1011. clientStopMoving(null);
  1012. npc.setTarget(leader);
  1013. npc.doCast(sk);
  1014. return;
  1015. }
  1016. }
  1017. }
  1018. }
  1019. for (Skill sk : aiResSkills)
  1020. {
  1021. if (!checkSkillCastConditions(sk))
  1022. {
  1023. continue;
  1024. }
  1025. if (sk.getTargetType() == L2TargetType.ONE)
  1026. {
  1027. for (L2Character obj : npc.getKnownList().getKnownCharactersInRadius(sk.getCastRange() + collision))
  1028. {
  1029. if (!(obj instanceof L2Attackable) || !obj.isDead())
  1030. {
  1031. continue;
  1032. }
  1033. L2Attackable targets = ((L2Attackable) obj);
  1034. if (!npc.isInMyClan(targets))
  1035. {
  1036. continue;
  1037. }
  1038. if (Rnd.get(100) < 10)
  1039. {
  1040. if (GeoData.getInstance().canSeeTarget(npc, targets))
  1041. {
  1042. clientStopMoving(null);
  1043. npc.setTarget(obj);
  1044. npc.doCast(sk);
  1045. return;
  1046. }
  1047. }
  1048. }
  1049. }
  1050. if (isParty(sk))
  1051. {
  1052. clientStopMoving(null);
  1053. L2Object target = getAttackTarget();
  1054. npc.setTarget(npc);
  1055. npc.doCast(sk);
  1056. npc.setTarget(target);
  1057. return;
  1058. }
  1059. }
  1060. }
  1061. }
  1062. double dist = npc.calculateDistance(mostHate, false, false);
  1063. int dist2 = (int) dist - collision;
  1064. int range = npc.getPhysicalAttackRange() + combinedCollision;
  1065. if (mostHate.isMoving())
  1066. {
  1067. range = range + 50;
  1068. if (npc.isMoving())
  1069. {
  1070. range = range + 50;
  1071. }
  1072. }
  1073. // -------------------------------------------------------------------------------
  1074. // Immobilize Condition
  1075. if ((npc.isMovementDisabled() && ((dist > range) || mostHate.isMoving())) || ((dist > range) && mostHate.isMoving()))
  1076. {
  1077. movementDisable();
  1078. return;
  1079. }
  1080. setTimepass(0);
  1081. // --------------------------------------------------------------------------------
  1082. // Skill Use
  1083. List<Skill> aiGeneralSkills = _skillrender.getAISkills(AISkillScope.GENERAL);
  1084. if (!aiGeneralSkills.isEmpty())
  1085. {
  1086. if (Rnd.get(100) < Rnd.get(npc.getMinSkillChance(), npc.getMaxSkillChance()))
  1087. {
  1088. Skill skills = aiGeneralSkills.get(Rnd.nextInt(aiGeneralSkills.size()));
  1089. if (cast(skills))
  1090. {
  1091. return;
  1092. }
  1093. for (Skill sk : aiGeneralSkills)
  1094. {
  1095. if (cast(sk))
  1096. {
  1097. return;
  1098. }
  1099. }
  1100. }
  1101. // --------------------------------------------------------------------------------
  1102. // Long/Short Range skill usage.
  1103. if (npc.hasLSkill() || npc.hasSSkill())
  1104. {
  1105. final List<Skill> shortRangeSkills = shortRangeSkillRender();
  1106. if (!shortRangeSkills.isEmpty() && npc.hasSSkill() && (dist2 <= 150) && (Rnd.get(100) <= npc.getSSkillChance()))
  1107. {
  1108. final Skill shortRangeSkill = shortRangeSkills.get(Rnd.get(shortRangeSkills.size()));
  1109. if ((shortRangeSkill != null) && cast(shortRangeSkill))
  1110. {
  1111. return;
  1112. }
  1113. for (Skill sk : shortRangeSkills)
  1114. {
  1115. if ((sk != null) && cast(sk))
  1116. {
  1117. return;
  1118. }
  1119. }
  1120. }
  1121. final List<Skill> longRangeSkills = longRangeSkillRender();
  1122. if (!longRangeSkills.isEmpty() && npc.hasLSkill() && (dist2 > 150) && (Rnd.get(100) <= npc.getLSkillChance()))
  1123. {
  1124. final Skill longRangeSkill = longRangeSkills.get(Rnd.get(longRangeSkills.size()));
  1125. if ((longRangeSkill != null) && cast(longRangeSkill))
  1126. {
  1127. return;
  1128. }
  1129. for (Skill sk : longRangeSkills)
  1130. {
  1131. if ((sk != null) && cast(sk))
  1132. {
  1133. return;
  1134. }
  1135. }
  1136. }
  1137. }
  1138. }
  1139. // --------------------------------------------------------------------------------
  1140. // Starts Melee or Primary Skill
  1141. if ((dist2 > range) || !GeoData.getInstance().canSeeTarget(npc, mostHate))
  1142. {
  1143. if (npc.isMovementDisabled())
  1144. {
  1145. targetReconsider();
  1146. }
  1147. else if (getAttackTarget() != null)
  1148. {
  1149. if (getAttackTarget().isMoving())
  1150. {
  1151. range -= 100;
  1152. }
  1153. if (range < 5)
  1154. {
  1155. range = 5;
  1156. }
  1157. moveToPawn(getAttackTarget(), range);
  1158. }
  1159. return;
  1160. }
  1161. melee(npc.getPrimarySkillId());
  1162. }
  1163. private void melee(int type)
  1164. {
  1165. if (type != 0)
  1166. {
  1167. switch (type)
  1168. {
  1169. case -1:
  1170. {
  1171. for (Skill sk : _skillrender.getAISkills(AISkillScope.GENERAL))
  1172. {
  1173. if (cast(sk))
  1174. {
  1175. return;
  1176. }
  1177. }
  1178. break;
  1179. }
  1180. case 1:
  1181. {
  1182. for (Skill sk : _skillrender.getAISkills(AISkillScope.ATTACK))
  1183. {
  1184. if (cast(sk))
  1185. {
  1186. return;
  1187. }
  1188. }
  1189. break;
  1190. }
  1191. default:
  1192. {
  1193. for (Skill sk : _skillrender.getAISkills(AISkillScope.GENERAL))
  1194. {
  1195. if (sk.getId() == getActiveChar().getPrimarySkillId())
  1196. {
  1197. if (cast(sk))
  1198. {
  1199. return;
  1200. }
  1201. }
  1202. }
  1203. break;
  1204. }
  1205. }
  1206. }
  1207. _accessor.doAttack(getAttackTarget());
  1208. }
  1209. private boolean cast(Skill sk)
  1210. {
  1211. if (sk == null)
  1212. {
  1213. return false;
  1214. }
  1215. final L2Attackable caster = getActiveChar();
  1216. if (caster.isCastingNow() && !sk.isSimultaneousCast())
  1217. {
  1218. return false;
  1219. }
  1220. if (!checkSkillCastConditions(sk))
  1221. {
  1222. return false;
  1223. }
  1224. if (getAttackTarget() == null)
  1225. {
  1226. if (caster.getMostHated() != null)
  1227. {
  1228. setAttackTarget(caster.getMostHated());
  1229. }
  1230. }
  1231. L2Character attackTarget = getAttackTarget();
  1232. if (attackTarget == null)
  1233. {
  1234. return false;
  1235. }
  1236. double dist = caster.calculateDistance(attackTarget, false, false);
  1237. double dist2 = dist - attackTarget.getTemplate().getCollisionRadius();
  1238. double range = caster.getPhysicalAttackRange() + caster.getTemplate().getCollisionRadius() + attackTarget.getTemplate().getCollisionRadius();
  1239. double srange = sk.getCastRange() + caster.getTemplate().getCollisionRadius();
  1240. if (attackTarget.isMoving())
  1241. {
  1242. dist2 = dist2 - 30;
  1243. }
  1244. if (sk.isContinuous())
  1245. {
  1246. if (!sk.isDebuff())
  1247. {
  1248. if (!caster.isAffectedBySkill(sk.getId()))
  1249. {
  1250. clientStopMoving(null);
  1251. // L2Object target = attackTarget;
  1252. caster.setTarget(caster);
  1253. caster.doCast(sk);
  1254. // _actor.setTarget(target);
  1255. return true;
  1256. }
  1257. // ----------------------------------------
  1258. // If actor already have buff, start looking at others same faction mob to cast
  1259. if (sk.getTargetType() == L2TargetType.SELF)
  1260. {
  1261. return false;
  1262. }
  1263. if (sk.getTargetType() == L2TargetType.ONE)
  1264. {
  1265. L2Character target = effectTargetReconsider(sk, true);
  1266. if (target != null)
  1267. {
  1268. clientStopMoving(null);
  1269. L2Object targets = attackTarget;
  1270. caster.setTarget(target);
  1271. caster.doCast(sk);
  1272. caster.setTarget(targets);
  1273. return true;
  1274. }
  1275. }
  1276. if (canParty(sk))
  1277. {
  1278. clientStopMoving(null);
  1279. L2Object targets = attackTarget;
  1280. caster.setTarget(caster);
  1281. caster.doCast(sk);
  1282. caster.setTarget(targets);
  1283. return true;
  1284. }
  1285. }
  1286. else
  1287. {
  1288. if (GeoData.getInstance().canSeeTarget(caster, attackTarget) && !canAOE(sk) && !attackTarget.isDead() && (dist2 <= srange))
  1289. {
  1290. if (!attackTarget.isAffectedBySkill(sk.getId()))
  1291. {
  1292. clientStopMoving(null);
  1293. caster.doCast(sk);
  1294. return true;
  1295. }
  1296. }
  1297. else if (canAOE(sk))
  1298. {
  1299. if ((sk.getTargetType() == L2TargetType.AURA) || (sk.getTargetType() == L2TargetType.BEHIND_AURA) || (sk.getTargetType() == L2TargetType.FRONT_AURA) || (sk.getTargetType() == L2TargetType.AURA_CORPSE_MOB))
  1300. {
  1301. clientStopMoving(null);
  1302. caster.doCast(sk);
  1303. return true;
  1304. }
  1305. if (((sk.getTargetType() == L2TargetType.AREA) || (sk.getTargetType() == L2TargetType.BEHIND_AREA) || (sk.getTargetType() == L2TargetType.FRONT_AREA)) && GeoData.getInstance().canSeeTarget(caster, attackTarget) && !attackTarget.isDead() && (dist2 <= srange))
  1306. {
  1307. clientStopMoving(null);
  1308. caster.doCast(sk);
  1309. return true;
  1310. }
  1311. }
  1312. else if (sk.getTargetType() == L2TargetType.ONE)
  1313. {
  1314. L2Character target = effectTargetReconsider(sk, false);
  1315. if (target != null)
  1316. {
  1317. clientStopMoving(null);
  1318. caster.doCast(sk);
  1319. return true;
  1320. }
  1321. }
  1322. }
  1323. }
  1324. if (sk.hasEffectType(L2EffectType.DISPEL, L2EffectType.DISPEL_BY_SLOT))
  1325. {
  1326. if (sk.getTargetType() == L2TargetType.ONE)
  1327. {
  1328. if ((attackTarget.getEffectList().getFirstEffect(L2EffectType.BUFF) != null) && GeoData.getInstance().canSeeTarget(caster, attackTarget) && !attackTarget.isDead() && (dist2 <= srange))
  1329. {
  1330. clientStopMoving(null);
  1331. caster.doCast(sk);
  1332. return true;
  1333. }
  1334. L2Character target = effectTargetReconsider(sk, false);
  1335. if (target != null)
  1336. {
  1337. clientStopMoving(null);
  1338. L2Object targets = attackTarget;
  1339. caster.setTarget(target);
  1340. caster.doCast(sk);
  1341. caster.setTarget(targets);
  1342. return true;
  1343. }
  1344. }
  1345. else if (canAOE(sk))
  1346. {
  1347. if (((sk.getTargetType() == L2TargetType.AURA) || (sk.getTargetType() == L2TargetType.BEHIND_AURA) || (sk.getTargetType() == L2TargetType.FRONT_AURA)) && GeoData.getInstance().canSeeTarget(caster, attackTarget))
  1348. {
  1349. clientStopMoving(null);
  1350. caster.doCast(sk);
  1351. return true;
  1352. }
  1353. else if (((sk.getTargetType() == L2TargetType.AREA) || (sk.getTargetType() == L2TargetType.BEHIND_AREA) || (sk.getTargetType() == L2TargetType.FRONT_AREA)) && GeoData.getInstance().canSeeTarget(caster, attackTarget) && !attackTarget.isDead() && (dist2 <= srange))
  1354. {
  1355. clientStopMoving(null);
  1356. caster.doCast(sk);
  1357. return true;
  1358. }
  1359. }
  1360. }
  1361. if (sk.hasEffectType(L2EffectType.HEAL))
  1362. {
  1363. double percentage = (caster.getCurrentHp() / caster.getMaxHp()) * 100;
  1364. if (caster.isMinion() && (sk.getTargetType() != L2TargetType.SELF))
  1365. {
  1366. L2Character leader = caster.getLeader();
  1367. if ((leader != null) && !leader.isDead() && (Rnd.get(100) > ((leader.getCurrentHp() / leader.getMaxHp()) * 100)))
  1368. {
  1369. if (!Util.checkIfInRange((sk.getCastRange() + caster.getTemplate().getCollisionRadius() + leader.getTemplate().getCollisionRadius()), caster, leader, false) && !isParty(sk) && !caster.isMovementDisabled())
  1370. {
  1371. moveToPawn(leader, sk.getCastRange() + caster.getTemplate().getCollisionRadius() + leader.getTemplate().getCollisionRadius());
  1372. }
  1373. if (GeoData.getInstance().canSeeTarget(caster, leader))
  1374. {
  1375. clientStopMoving(null);
  1376. caster.setTarget(leader);
  1377. caster.doCast(sk);
  1378. return true;
  1379. }
  1380. }
  1381. }
  1382. if (Rnd.get(100) < ((100 - percentage) / 3))
  1383. {
  1384. clientStopMoving(null);
  1385. caster.setTarget(caster);
  1386. caster.doCast(sk);
  1387. return true;
  1388. }
  1389. if (sk.getTargetType() == L2TargetType.ONE)
  1390. {
  1391. for (L2Character obj : caster.getKnownList().getKnownCharactersInRadius(sk.getCastRange() + caster.getTemplate().getCollisionRadius()))
  1392. {
  1393. if (!(obj instanceof L2Attackable) || obj.isDead())
  1394. {
  1395. continue;
  1396. }
  1397. L2Attackable targets = ((L2Attackable) obj);
  1398. if (!caster.isInMyClan(targets))
  1399. {
  1400. continue;
  1401. }
  1402. percentage = (targets.getCurrentHp() / targets.getMaxHp()) * 100;
  1403. if (Rnd.get(100) < ((100 - percentage) / 10))
  1404. {
  1405. if (GeoData.getInstance().canSeeTarget(caster, targets))
  1406. {
  1407. clientStopMoving(null);
  1408. caster.setTarget(obj);
  1409. caster.doCast(sk);
  1410. return true;
  1411. }
  1412. }
  1413. }
  1414. }
  1415. if (isParty(sk))
  1416. {
  1417. for (L2Character obj : caster.getKnownList().getKnownCharactersInRadius(sk.getAffectRange() + caster.getTemplate().getCollisionRadius()))
  1418. {
  1419. if (!(obj instanceof L2Attackable))
  1420. {
  1421. continue;
  1422. }
  1423. L2Npc targets = ((L2Npc) obj);
  1424. if (targets.isInMyClan(caster))
  1425. {
  1426. if ((obj.getCurrentHp() < obj.getMaxHp()) && (Rnd.get(100) <= 20))
  1427. {
  1428. clientStopMoving(null);
  1429. caster.setTarget(caster);
  1430. caster.doCast(sk);
  1431. return true;
  1432. }
  1433. }
  1434. }
  1435. }
  1436. }
  1437. if (sk.hasEffectType(L2EffectType.PHYSICAL_ATTACK, L2EffectType.PHYSICAL_ATTACK_HP_LINK, L2EffectType.MAGICAL_ATTACK, L2EffectType.DEATH_LINK, L2EffectType.HP_DRAIN))
  1438. {
  1439. if (!canAura(sk))
  1440. {
  1441. if (GeoData.getInstance().canSeeTarget(caster, attackTarget) && !attackTarget.isDead() && (dist2 <= srange))
  1442. {
  1443. clientStopMoving(null);
  1444. caster.doCast(sk);
  1445. return true;
  1446. }
  1447. L2Character target = skillTargetReconsider(sk);
  1448. if (target != null)
  1449. {
  1450. clientStopMoving(null);
  1451. L2Object targets = attackTarget;
  1452. caster.setTarget(target);
  1453. caster.doCast(sk);
  1454. caster.setTarget(targets);
  1455. return true;
  1456. }
  1457. }
  1458. else
  1459. {
  1460. clientStopMoving(null);
  1461. caster.doCast(sk);
  1462. return true;
  1463. }
  1464. }
  1465. if (sk.hasEffectType(L2EffectType.SLEEP))
  1466. {
  1467. if (sk.getTargetType() == L2TargetType.ONE)
  1468. {
  1469. if (!attackTarget.isDead() && (dist2 <= srange))
  1470. {
  1471. if ((dist2 > range) || attackTarget.isMoving())
  1472. {
  1473. if (!attackTarget.isAffectedBySkill(sk.getId()))
  1474. {
  1475. clientStopMoving(null);
  1476. caster.doCast(sk);
  1477. return true;
  1478. }
  1479. }
  1480. }
  1481. L2Character target = effectTargetReconsider(sk, false);
  1482. if (target != null)
  1483. {
  1484. clientStopMoving(null);
  1485. caster.doCast(sk);
  1486. return true;
  1487. }
  1488. }
  1489. else if (canAOE(sk))
  1490. {
  1491. if ((sk.getTargetType() == L2TargetType.AURA) || (sk.getTargetType() == L2TargetType.BEHIND_AURA) || (sk.getTargetType() == L2TargetType.FRONT_AURA))
  1492. {
  1493. clientStopMoving(null);
  1494. caster.doCast(sk);
  1495. return true;
  1496. }
  1497. if (((sk.getTargetType() == L2TargetType.AREA) || (sk.getTargetType() == L2TargetType.BEHIND_AREA) || (sk.getTargetType() == L2TargetType.FRONT_AREA)) && GeoData.getInstance().canSeeTarget(caster, attackTarget) && !attackTarget.isDead() && (dist2 <= srange))
  1498. {
  1499. clientStopMoving(null);
  1500. caster.doCast(sk);
  1501. return true;
  1502. }
  1503. }
  1504. }
  1505. if (sk.hasEffectType(L2EffectType.STUN, L2EffectType.ROOT, L2EffectType.PARALYZE, L2EffectType.MUTE, L2EffectType.FEAR))
  1506. {
  1507. if (GeoData.getInstance().canSeeTarget(caster, attackTarget) && !canAOE(sk) && (dist2 <= srange))
  1508. {
  1509. if (!attackTarget.isAffectedBySkill(sk.getId()))
  1510. {
  1511. clientStopMoving(null);
  1512. caster.doCast(sk);
  1513. return true;
  1514. }
  1515. }
  1516. else if (canAOE(sk))
  1517. {
  1518. if ((sk.getTargetType() == L2TargetType.AURA) || (sk.getTargetType() == L2TargetType.BEHIND_AURA) || (sk.getTargetType() == L2TargetType.FRONT_AURA))
  1519. {
  1520. clientStopMoving(null);
  1521. caster.doCast(sk);
  1522. return true;
  1523. }
  1524. if (((sk.getTargetType() == L2TargetType.AREA) || (sk.getTargetType() == L2TargetType.BEHIND_AREA) || (sk.getTargetType() == L2TargetType.FRONT_AREA)) && GeoData.getInstance().canSeeTarget(caster, attackTarget) && !attackTarget.isDead() && (dist2 <= srange))
  1525. {
  1526. clientStopMoving(null);
  1527. caster.doCast(sk);
  1528. return true;
  1529. }
  1530. }
  1531. else if (sk.getTargetType() == L2TargetType.ONE)
  1532. {
  1533. L2Character target = effectTargetReconsider(sk, false);
  1534. if (target != null)
  1535. {
  1536. clientStopMoving(null);
  1537. caster.doCast(sk);
  1538. return true;
  1539. }
  1540. }
  1541. }
  1542. if (sk.hasEffectType(L2EffectType.DMG_OVER_TIME, L2EffectType.DMG_OVER_TIME_PERCENT))
  1543. {
  1544. if (GeoData.getInstance().canSeeTarget(caster, attackTarget) && !canAOE(sk) && !attackTarget.isDead() && (dist2 <= srange))
  1545. {
  1546. if (!attackTarget.isAffectedBySkill(sk.getId()))
  1547. {
  1548. clientStopMoving(null);
  1549. caster.doCast(sk);
  1550. return true;
  1551. }
  1552. }
  1553. else if (canAOE(sk))
  1554. {
  1555. if ((sk.getTargetType() == L2TargetType.AURA) || (sk.getTargetType() == L2TargetType.BEHIND_AURA) || (sk.getTargetType() == L2TargetType.FRONT_AURA) || (sk.getTargetType() == L2TargetType.AURA_CORPSE_MOB))
  1556. {
  1557. clientStopMoving(null);
  1558. caster.doCast(sk);
  1559. return true;
  1560. }
  1561. if (((sk.getTargetType() == L2TargetType.AREA) || (sk.getTargetType() == L2TargetType.BEHIND_AREA) || (sk.getTargetType() == L2TargetType.FRONT_AREA)) && GeoData.getInstance().canSeeTarget(caster, attackTarget) && !attackTarget.isDead() && (dist2 <= srange))
  1562. {
  1563. clientStopMoving(null);
  1564. caster.doCast(sk);
  1565. return true;
  1566. }
  1567. }
  1568. else if (sk.getTargetType() == L2TargetType.ONE)
  1569. {
  1570. L2Character target = effectTargetReconsider(sk, false);
  1571. if (target != null)
  1572. {
  1573. clientStopMoving(null);
  1574. caster.doCast(sk);
  1575. return true;
  1576. }
  1577. }
  1578. }
  1579. if (sk.hasEffectType(L2EffectType.RESURRECTION))
  1580. {
  1581. if (!isParty(sk))
  1582. {
  1583. if (caster.isMinion() && (sk.getTargetType() != L2TargetType.SELF))
  1584. {
  1585. L2Character leader = caster.getLeader();
  1586. if (leader != null)
  1587. {
  1588. if (leader.isDead())
  1589. {
  1590. if (!Util.checkIfInRange((sk.getCastRange() + caster.getTemplate().getCollisionRadius() + leader.getTemplate().getCollisionRadius()), caster, leader, false) && !isParty(sk) && !caster.isMovementDisabled())
  1591. {
  1592. moveToPawn(leader, sk.getCastRange() + caster.getTemplate().getCollisionRadius() + leader.getTemplate().getCollisionRadius());
  1593. }
  1594. }
  1595. if (GeoData.getInstance().canSeeTarget(caster, leader))
  1596. {
  1597. clientStopMoving(null);
  1598. caster.setTarget(leader);
  1599. caster.doCast(sk);
  1600. return true;
  1601. }
  1602. }
  1603. }
  1604. for (L2Character obj : caster.getKnownList().getKnownCharactersInRadius(sk.getCastRange() + caster.getTemplate().getCollisionRadius()))
  1605. {
  1606. if (!(obj instanceof L2Attackable) || !obj.isDead())
  1607. {
  1608. continue;
  1609. }
  1610. L2Attackable targets = ((L2Attackable) obj);
  1611. if (!caster.isInMyClan(targets))
  1612. {
  1613. continue;
  1614. }
  1615. if (Rnd.get(100) < 10)
  1616. {
  1617. if (GeoData.getInstance().canSeeTarget(caster, targets))
  1618. {
  1619. clientStopMoving(null);
  1620. caster.setTarget(obj);
  1621. caster.doCast(sk);
  1622. return true;
  1623. }
  1624. }
  1625. }
  1626. }
  1627. else if (isParty(sk))
  1628. {
  1629. for (L2Character obj : caster.getKnownList().getKnownCharactersInRadius(sk.getAffectRange() + caster.getTemplate().getCollisionRadius()))
  1630. {
  1631. if (!(obj instanceof L2Attackable))
  1632. {
  1633. continue;
  1634. }
  1635. L2Npc targets = ((L2Npc) obj);
  1636. if (caster.isInMyClan(targets))
  1637. {
  1638. if ((obj.getCurrentHp() < obj.getMaxHp()) && (Rnd.get(100) <= 20))
  1639. {
  1640. clientStopMoving(null);
  1641. caster.setTarget(caster);
  1642. caster.doCast(sk);
  1643. return true;
  1644. }
  1645. }
  1646. }
  1647. }
  1648. }
  1649. if (!canAura(sk))
  1650. {
  1651. if (GeoData.getInstance().canSeeTarget(caster, attackTarget) && !attackTarget.isDead() && (dist2 <= srange))
  1652. {
  1653. clientStopMoving(null);
  1654. caster.doCast(sk);
  1655. return true;
  1656. }
  1657. L2Character target = skillTargetReconsider(sk);
  1658. if (target != null)
  1659. {
  1660. clientStopMoving(null);
  1661. L2Object targets = attackTarget;
  1662. caster.setTarget(target);
  1663. caster.doCast(sk);
  1664. caster.setTarget(targets);
  1665. return true;
  1666. }
  1667. }
  1668. else
  1669. {
  1670. clientStopMoving(null);
  1671. caster.doCast(sk);
  1672. return true;
  1673. }
  1674. return false;
  1675. }
  1676. /**
  1677. * This AI task will start when ACTOR cannot move and attack range larger than distance
  1678. */
  1679. private void movementDisable()
  1680. {
  1681. final L2Attackable npc = getActiveChar();
  1682. double dist = 0;
  1683. double dist2 = 0;
  1684. int range = 0;
  1685. try
  1686. {
  1687. if (npc.getTarget() == null)
  1688. {
  1689. npc.setTarget(getAttackTarget());
  1690. }
  1691. dist = npc.calculateDistance(getAttackTarget(), false, false);
  1692. dist2 = dist - npc.getTemplate().getCollisionRadius();
  1693. range = npc.getPhysicalAttackRange() + npc.getTemplate().getCollisionRadius() + getAttackTarget().getTemplate().getCollisionRadius();
  1694. if (getAttackTarget().isMoving())
  1695. {
  1696. dist = dist - 30;
  1697. if (npc.isMoving())
  1698. {
  1699. dist = dist - 50;
  1700. }
  1701. }
  1702. // Check if activeChar has any skill
  1703. if (!_skillrender.getAISkills(AISkillScope.GENERAL).isEmpty())
  1704. {
  1705. // -------------------------------------------------------------
  1706. // Try to stop the target or disable the target as priority
  1707. int random = Rnd.get(100);
  1708. if (!getAttackTarget().isImmobilized() && (random < 2))
  1709. {
  1710. for (Skill sk : _skillrender.getAISkills(AISkillScope.IMMOBILIZE))
  1711. {
  1712. if (!checkSkillCastConditions(sk) || (((sk.getCastRange() + npc.getTemplate().getCollisionRadius() + getAttackTarget().getTemplate().getCollisionRadius()) <= dist2) && !canAura(sk)))
  1713. {
  1714. continue;
  1715. }
  1716. if (!GeoData.getInstance().canSeeTarget(npc, getAttackTarget()))
  1717. {
  1718. continue;
  1719. }
  1720. if (!getAttackTarget().isAffectedBySkill(sk.getId()))
  1721. {
  1722. clientStopMoving(null);
  1723. // L2Object target = getAttackTarget();
  1724. // _actor.setTarget(_actor);
  1725. npc.doCast(sk);
  1726. // _actor.setTarget(target);
  1727. return;
  1728. }
  1729. }
  1730. }
  1731. // -------------------------------------------------------------
  1732. // Same as Above, but with Mute/FEAR etc....
  1733. if (random < 5)
  1734. {
  1735. for (Skill sk : _skillrender.getAISkills(AISkillScope.COT))
  1736. {
  1737. if (!checkSkillCastConditions(sk) || (((sk.getCastRange() + npc.getTemplate().getCollisionRadius() + getAttackTarget().getTemplate().getCollisionRadius()) <= dist2) && !canAura(sk)))
  1738. {
  1739. continue;
  1740. }
  1741. if (!GeoData.getInstance().canSeeTarget(npc, getAttackTarget()))
  1742. {
  1743. continue;
  1744. }
  1745. if (!getAttackTarget().isAffectedBySkill(sk.getId()))
  1746. {
  1747. clientStopMoving(null);
  1748. // L2Object target = getAttackTarget();
  1749. // _actor.setTarget(_actor);
  1750. npc.doCast(sk);
  1751. // _actor.setTarget(target);
  1752. return;
  1753. }
  1754. }
  1755. }
  1756. // -------------------------------------------------------------
  1757. if (random < 8)
  1758. {
  1759. for (Skill sk : _skillrender.getAISkills(AISkillScope.DEBUFF))
  1760. {
  1761. if (!checkSkillCastConditions(sk) || (((sk.getCastRange() + npc.getTemplate().getCollisionRadius() + getAttackTarget().getTemplate().getCollisionRadius()) <= dist2) && !canAura(sk)))
  1762. {
  1763. continue;
  1764. }
  1765. if (!GeoData.getInstance().canSeeTarget(npc, getAttackTarget()))
  1766. {
  1767. continue;
  1768. }
  1769. if (!getAttackTarget().isAffectedBySkill(sk.getId()))
  1770. {
  1771. clientStopMoving(null);
  1772. // L2Object target = getAttackTarget();
  1773. // _actor.setTarget(_actor);
  1774. npc.doCast(sk);
  1775. // _actor.setTarget(target);
  1776. return;
  1777. }
  1778. }
  1779. }
  1780. // -------------------------------------------------------------
  1781. // Some side effect skill like CANCEL or NEGATE
  1782. if (random < 9)
  1783. {
  1784. for (Skill sk : _skillrender.getAISkills(AISkillScope.NEGATIVE))
  1785. {
  1786. if (!checkSkillCastConditions(sk) || (((sk.getCastRange() + npc.getTemplate().getCollisionRadius() + getAttackTarget().getTemplate().getCollisionRadius()) <= dist2) && !canAura(sk)))
  1787. {
  1788. continue;
  1789. }
  1790. if (!GeoData.getInstance().canSeeTarget(npc, getAttackTarget()))
  1791. {
  1792. continue;
  1793. }
  1794. if (getAttackTarget().getEffectList().getFirstEffect(L2EffectType.BUFF) != null)
  1795. {
  1796. clientStopMoving(null);
  1797. // L2Object target = getAttackTarget();
  1798. // _actor.setTarget(_actor);
  1799. npc.doCast(sk);
  1800. // _actor.setTarget(target);
  1801. return;
  1802. }
  1803. }
  1804. }
  1805. // -------------------------------------------------------------
  1806. // Start ATK SKILL when nothing can be done
  1807. if ((npc.isMovementDisabled() || (npc.getAiType() == AIType.MAGE) || (npc.getAiType() == AIType.HEALER)))
  1808. {
  1809. for (Skill sk : _skillrender.getAISkills(AISkillScope.ATTACK))
  1810. {
  1811. if (!checkSkillCastConditions(sk) || (((sk.getCastRange() + npc.getTemplate().getCollisionRadius() + getAttackTarget().getTemplate().getCollisionRadius()) <= dist2) && !canAura(sk)))
  1812. {
  1813. continue;
  1814. }
  1815. if (!GeoData.getInstance().canSeeTarget(npc, getAttackTarget()))
  1816. {
  1817. continue;
  1818. }
  1819. clientStopMoving(null);
  1820. // L2Object target = getAttackTarget();
  1821. // _actor.setTarget(_actor);
  1822. npc.doCast(sk);
  1823. // _actor.setTarget(target);
  1824. return;
  1825. }
  1826. }
  1827. // -------------------------------------------------------------
  1828. // if there is no ATK skill to use, then try Universal skill
  1829. // @formatter:off
  1830. /*
  1831. for(L2Skill sk:_skillrender.getUniversalSkills())
  1832. {
  1833. if(sk.getMpConsume()>=_actor.getCurrentMp()
  1834. || _actor.isSkillDisabled(sk.getId())
  1835. ||(sk.getCastRange()+ _actor.getTemplate().getCollisionRadius() + getAttackTarget().getTemplate().getCollisionRadius() <= dist2 && !canAura(sk))
  1836. ||(sk.isMagic()&&_actor.isMuted())
  1837. ||(!sk.isMagic()&&_actor.isPhysicalMuted()))
  1838. {
  1839. continue;
  1840. }
  1841. if(!GeoData.getInstance().canSeeTarget(_actor,getAttackTarget()))
  1842. continue;
  1843. clientStopMoving(null);
  1844. L2Object target = getAttackTarget();
  1845. //_actor.setTarget(_actor);
  1846. _actor.doCast(sk);
  1847. //_actor.setTarget(target);
  1848. return;
  1849. }
  1850. */
  1851. // @formatter:on
  1852. }
  1853. // timepass = timepass + 1;
  1854. if (npc.isMovementDisabled())
  1855. {
  1856. // timepass = 0;
  1857. targetReconsider();
  1858. return;
  1859. }
  1860. // else if(timepass>=5)
  1861. // {
  1862. // timepass = 0;
  1863. // AggroReconsider();
  1864. // return;
  1865. // }
  1866. if ((dist > range) || !GeoData.getInstance().canSeeTarget(npc, getAttackTarget()))
  1867. {
  1868. if (getAttackTarget().isMoving())
  1869. {
  1870. range -= 100;
  1871. }
  1872. if (range < 5)
  1873. {
  1874. range = 5;
  1875. }
  1876. moveToPawn(getAttackTarget(), range);
  1877. return;
  1878. }
  1879. melee(npc.getPrimarySkillId());
  1880. }
  1881. catch (NullPointerException e)
  1882. {
  1883. setIntention(AI_INTENTION_ACTIVE);
  1884. _log.warning(getClass().getSimpleName() + ": " + this + " - failed executing movementDisable(): " + e.getMessage());
  1885. return;
  1886. }
  1887. }
  1888. /**
  1889. * @param skill the skill to check.
  1890. * @return {@code true} if the skill is available for casting {@code false} otherwise.
  1891. */
  1892. private boolean checkSkillCastConditions(Skill skill)
  1893. {
  1894. // Not enough MP.
  1895. if (skill.getMpConsume() >= getActiveChar().getCurrentMp())
  1896. {
  1897. return false;
  1898. }
  1899. // Character is in "skill disabled" mode.
  1900. if (getActiveChar().isSkillDisabled(skill))
  1901. {
  1902. return false;
  1903. }
  1904. // If is a static skill and magic skill and character is muted or is a physical skill muted and character is physically muted.
  1905. if (!skill.isStatic() && ((skill.isMagic() && getActiveChar().isMuted()) || getActiveChar().isPhysicalMuted()))
  1906. {
  1907. return false;
  1908. }
  1909. return true;
  1910. }
  1911. private L2Character effectTargetReconsider(Skill sk, boolean positive)
  1912. {
  1913. if (sk == null)
  1914. {
  1915. return null;
  1916. }
  1917. L2Attackable actor = getActiveChar();
  1918. if (!sk.hasEffectType(L2EffectType.DISPEL, L2EffectType.DISPEL_BY_SLOT))
  1919. {
  1920. if (!positive)
  1921. {
  1922. double dist = 0;
  1923. double dist2 = 0;
  1924. int range = 0;
  1925. for (L2Character obj : actor.getAttackByList())
  1926. {
  1927. if ((obj == null) || obj.isDead() || !GeoData.getInstance().canSeeTarget(actor, obj) || (obj == getAttackTarget()))
  1928. {
  1929. continue;
  1930. }
  1931. try
  1932. {
  1933. actor.setTarget(getAttackTarget());
  1934. dist = actor.calculateDistance(obj, false, false);
  1935. dist2 = dist - actor.getTemplate().getCollisionRadius();
  1936. range = sk.getCastRange() + actor.getTemplate().getCollisionRadius() + obj.getTemplate().getCollisionRadius();
  1937. if (obj.isMoving())
  1938. {
  1939. dist2 = dist2 - 70;
  1940. }
  1941. }
  1942. catch (NullPointerException e)
  1943. {
  1944. continue;
  1945. }
  1946. if (dist2 <= range)
  1947. {
  1948. if (!getAttackTarget().isAffectedBySkill(sk.getId()))
  1949. {
  1950. return obj;
  1951. }
  1952. }
  1953. }
  1954. // ----------------------------------------------------------------------
  1955. // If there is nearby Target with aggro, start going on random target that is attackable
  1956. for (L2Character obj : actor.getKnownList().getKnownCharactersInRadius(range))
  1957. {
  1958. if (obj.isDead() || !GeoData.getInstance().canSeeTarget(actor, obj))
  1959. {
  1960. continue;
  1961. }
  1962. try
  1963. {
  1964. actor.setTarget(getAttackTarget());
  1965. dist = actor.calculateDistance(obj, false, false);
  1966. dist2 = dist;
  1967. range = sk.getCastRange() + actor.getTemplate().getCollisionRadius() + obj.getTemplate().getCollisionRadius();
  1968. if (obj.isMoving())
  1969. {
  1970. dist2 = dist2 - 70;
  1971. }
  1972. }
  1973. catch (NullPointerException e)
  1974. {
  1975. continue;
  1976. }
  1977. if ((obj instanceof L2PcInstance) || (obj instanceof L2Summon))
  1978. {
  1979. if (dist2 <= range)
  1980. {
  1981. if (!getAttackTarget().isAffectedBySkill(sk.getId()))
  1982. {
  1983. return obj;
  1984. }
  1985. }
  1986. }
  1987. }
  1988. }
  1989. else if (positive)
  1990. {
  1991. double dist = 0;
  1992. double dist2 = 0;
  1993. int range = 0;
  1994. for (L2Character obj : actor.getKnownList().getKnownCharactersInRadius(range))
  1995. {
  1996. if (!(obj instanceof L2Attackable) || obj.isDead() || !GeoData.getInstance().canSeeTarget(actor, obj))
  1997. {
  1998. continue;
  1999. }
  2000. L2Attackable targets = ((L2Attackable) obj);
  2001. if (targets.isInMyClan(actor))
  2002. {
  2003. continue;
  2004. }
  2005. try
  2006. {
  2007. actor.setTarget(getAttackTarget());
  2008. dist = actor.calculateDistance(obj, false, false);
  2009. dist2 = dist - actor.getTemplate().getCollisionRadius();
  2010. range = sk.getCastRange() + actor.getTemplate().getCollisionRadius() + obj.getTemplate().getCollisionRadius();
  2011. if (obj.isMoving())
  2012. {
  2013. dist2 = dist2 - 70;
  2014. }
  2015. }
  2016. catch (NullPointerException e)
  2017. {
  2018. continue;
  2019. }
  2020. if (dist2 <= range)
  2021. {
  2022. if (!obj.isAffectedBySkill(sk.getId()))
  2023. {
  2024. return obj;
  2025. }
  2026. }
  2027. }
  2028. }
  2029. }
  2030. else
  2031. {
  2032. double dist = 0;
  2033. double dist2 = 0;
  2034. int range = 0;
  2035. range = sk.getCastRange() + actor.getTemplate().getCollisionRadius() + getAttackTarget().getTemplate().getCollisionRadius();
  2036. for (L2Character obj : actor.getKnownList().getKnownCharactersInRadius(range))
  2037. {
  2038. if ((obj == null) || obj.isDead() || !GeoData.getInstance().canSeeTarget(actor, obj))
  2039. {
  2040. continue;
  2041. }
  2042. try
  2043. {
  2044. actor.setTarget(getAttackTarget());
  2045. dist = actor.calculateDistance(obj, false, false);
  2046. dist2 = dist - actor.getTemplate().getCollisionRadius();
  2047. range = sk.getCastRange() + actor.getTemplate().getCollisionRadius() + obj.getTemplate().getCollisionRadius();
  2048. if (obj.isMoving())
  2049. {
  2050. dist2 = dist2 - 70;
  2051. }
  2052. }
  2053. catch (NullPointerException e)
  2054. {
  2055. continue;
  2056. }
  2057. if ((obj instanceof L2PcInstance) || (obj instanceof L2Summon))
  2058. {
  2059. if (dist2 <= range)
  2060. {
  2061. if (getAttackTarget().getEffectList().getFirstEffect(L2EffectType.BUFF) != null)
  2062. {
  2063. return obj;
  2064. }
  2065. }
  2066. }
  2067. }
  2068. }
  2069. return null;
  2070. }
  2071. private L2Character skillTargetReconsider(Skill sk)
  2072. {
  2073. double dist = 0;
  2074. double dist2 = 0;
  2075. int range = 0;
  2076. L2Attackable actor = getActiveChar();
  2077. if (actor.getHateList() != null)
  2078. {
  2079. for (L2Character obj : actor.getHateList())
  2080. {
  2081. if ((obj == null) || !GeoData.getInstance().canSeeTarget(actor, obj) || obj.isDead())
  2082. {
  2083. continue;
  2084. }
  2085. try
  2086. {
  2087. actor.setTarget(getAttackTarget());
  2088. dist = actor.calculateDistance(obj, false, false);
  2089. dist2 = dist - actor.getTemplate().getCollisionRadius();
  2090. range = sk.getCastRange() + actor.getTemplate().getCollisionRadius() + getAttackTarget().getTemplate().getCollisionRadius();
  2091. // if(obj.isMoving())
  2092. // dist2 = dist2 - 40;
  2093. }
  2094. catch (NullPointerException e)
  2095. {
  2096. continue;
  2097. }
  2098. if (dist2 <= range)
  2099. {
  2100. return obj;
  2101. }
  2102. }
  2103. }
  2104. if (!(actor instanceof L2GuardInstance))
  2105. {
  2106. Collection<L2Object> objs = actor.getKnownList().getKnownObjects().values();
  2107. for (L2Object target : objs)
  2108. {
  2109. try
  2110. {
  2111. actor.setTarget(getAttackTarget());
  2112. dist = actor.calculateDistance(target, false, false);
  2113. dist2 = dist;
  2114. range = sk.getCastRange() + actor.getTemplate().getCollisionRadius() + getAttackTarget().getTemplate().getCollisionRadius();
  2115. // if(obj.isMoving())
  2116. // dist2 = dist2 - 40;
  2117. }
  2118. catch (NullPointerException e)
  2119. {
  2120. continue;
  2121. }
  2122. L2Character obj = null;
  2123. if (target instanceof L2Character)
  2124. {
  2125. obj = (L2Character) target;
  2126. }
  2127. if ((obj == null) || !GeoData.getInstance().canSeeTarget(actor, obj) || (dist2 > range))
  2128. {
  2129. continue;
  2130. }
  2131. if (obj instanceof L2PcInstance)
  2132. {
  2133. return obj;
  2134. }
  2135. if (obj instanceof L2Attackable)
  2136. {
  2137. if (actor.isChaos())
  2138. {
  2139. if (((L2Attackable) obj).isInMyClan(actor))
  2140. {
  2141. continue;
  2142. }
  2143. return obj;
  2144. }
  2145. }
  2146. if (obj instanceof L2Summon)
  2147. {
  2148. return obj;
  2149. }
  2150. }
  2151. }
  2152. return null;
  2153. }
  2154. private void targetReconsider()
  2155. {
  2156. double dist = 0;
  2157. double dist2 = 0;
  2158. int range = 0;
  2159. L2Attackable actor = getActiveChar();
  2160. L2Character MostHate = actor.getMostHated();
  2161. if (actor.getHateList() != null)
  2162. {
  2163. for (L2Character obj : actor.getHateList())
  2164. {
  2165. if ((obj == null) || !GeoData.getInstance().canSeeTarget(actor, obj) || obj.isDead() || (obj != MostHate) || (obj == actor))
  2166. {
  2167. continue;
  2168. }
  2169. try
  2170. {
  2171. dist = actor.calculateDistance(obj, false, false);
  2172. dist2 = dist - actor.getTemplate().getCollisionRadius();
  2173. range = actor.getPhysicalAttackRange() + actor.getTemplate().getCollisionRadius() + obj.getTemplate().getCollisionRadius();
  2174. if (obj.isMoving())
  2175. {
  2176. dist2 = dist2 - 70;
  2177. }
  2178. }
  2179. catch (NullPointerException e)
  2180. {
  2181. continue;
  2182. }
  2183. if (dist2 <= range)
  2184. {
  2185. if (MostHate != null)
  2186. {
  2187. actor.addDamageHate(obj, 0, actor.getHating(MostHate));
  2188. }
  2189. else
  2190. {
  2191. actor.addDamageHate(obj, 0, 2000);
  2192. }
  2193. actor.setTarget(obj);
  2194. setAttackTarget(obj);
  2195. return;
  2196. }
  2197. }
  2198. }
  2199. if (!(actor instanceof L2GuardInstance))
  2200. {
  2201. Collection<L2Object> objs = actor.getKnownList().getKnownObjects().values();
  2202. for (L2Object target : objs)
  2203. {
  2204. L2Character obj = null;
  2205. if (target instanceof L2Character)
  2206. {
  2207. obj = (L2Character) target;
  2208. }
  2209. if ((obj == null) || !GeoData.getInstance().canSeeTarget(actor, obj) || obj.isDead() || (obj != MostHate) || (obj == actor) || (obj == getAttackTarget()))
  2210. {
  2211. continue;
  2212. }
  2213. if (obj instanceof L2PcInstance)
  2214. {
  2215. if (MostHate != null)
  2216. {
  2217. actor.addDamageHate(obj, 0, actor.getHating(MostHate));
  2218. }
  2219. else
  2220. {
  2221. actor.addDamageHate(obj, 0, 2000);
  2222. }
  2223. actor.setTarget(obj);
  2224. setAttackTarget(obj);
  2225. }
  2226. else if (obj instanceof L2Attackable)
  2227. {
  2228. if (actor.isChaos())
  2229. {
  2230. if (((L2Attackable) obj).isInMyClan(actor))
  2231. {
  2232. continue;
  2233. }
  2234. if (MostHate != null)
  2235. {
  2236. actor.addDamageHate(obj, 0, actor.getHating(MostHate));
  2237. }
  2238. else
  2239. {
  2240. actor.addDamageHate(obj, 0, 2000);
  2241. }
  2242. actor.setTarget(obj);
  2243. setAttackTarget(obj);
  2244. }
  2245. }
  2246. else if (obj instanceof L2Summon)
  2247. {
  2248. if (MostHate != null)
  2249. {
  2250. actor.addDamageHate(obj, 0, actor.getHating(MostHate));
  2251. }
  2252. else
  2253. {
  2254. actor.addDamageHate(obj, 0, 2000);
  2255. }
  2256. actor.setTarget(obj);
  2257. setAttackTarget(obj);
  2258. }
  2259. }
  2260. }
  2261. }
  2262. private void aggroReconsider()
  2263. {
  2264. L2Attackable actor = getActiveChar();
  2265. L2Character MostHate = actor.getMostHated();
  2266. if (actor.getHateList() != null)
  2267. {
  2268. int rand = Rnd.get(actor.getHateList().size());
  2269. int count = 0;
  2270. for (L2Character obj : actor.getHateList())
  2271. {
  2272. if (count < rand)
  2273. {
  2274. count++;
  2275. continue;
  2276. }
  2277. if ((obj == null) || !GeoData.getInstance().canSeeTarget(actor, obj) || obj.isDead() || (obj == getAttackTarget()) || (obj == actor))
  2278. {
  2279. continue;
  2280. }
  2281. try
  2282. {
  2283. actor.setTarget(getAttackTarget());
  2284. }
  2285. catch (NullPointerException e)
  2286. {
  2287. continue;
  2288. }
  2289. if (MostHate != null)
  2290. {
  2291. actor.addDamageHate(obj, 0, actor.getHating(MostHate));
  2292. }
  2293. else
  2294. {
  2295. actor.addDamageHate(obj, 0, 2000);
  2296. }
  2297. actor.setTarget(obj);
  2298. setAttackTarget(obj);
  2299. return;
  2300. }
  2301. }
  2302. if (!(actor instanceof L2GuardInstance))
  2303. {
  2304. Collection<L2Object> objs = actor.getKnownList().getKnownObjects().values();
  2305. for (L2Object target : objs)
  2306. {
  2307. L2Character obj = null;
  2308. if (target instanceof L2Character)
  2309. {
  2310. obj = (L2Character) target;
  2311. }
  2312. else
  2313. {
  2314. continue;
  2315. }
  2316. if (!GeoData.getInstance().canSeeTarget(actor, obj) || obj.isDead() || (obj != MostHate) || (obj == actor))
  2317. {
  2318. continue;
  2319. }
  2320. if (obj instanceof L2PcInstance)
  2321. {
  2322. if ((MostHate != null) && !MostHate.isDead())
  2323. {
  2324. actor.addDamageHate(obj, 0, actor.getHating(MostHate));
  2325. }
  2326. else
  2327. {
  2328. actor.addDamageHate(obj, 0, 2000);
  2329. }
  2330. actor.setTarget(obj);
  2331. setAttackTarget(obj);
  2332. }
  2333. else if (obj instanceof L2Attackable)
  2334. {
  2335. if (actor.isChaos())
  2336. {
  2337. if (((L2Attackable) obj).isInMyClan(actor))
  2338. {
  2339. continue;
  2340. }
  2341. if (MostHate != null)
  2342. {
  2343. actor.addDamageHate(obj, 0, actor.getHating(MostHate));
  2344. }
  2345. else
  2346. {
  2347. actor.addDamageHate(obj, 0, 2000);
  2348. }
  2349. actor.setTarget(obj);
  2350. setAttackTarget(obj);
  2351. }
  2352. }
  2353. else if (obj instanceof L2Summon)
  2354. {
  2355. if (MostHate != null)
  2356. {
  2357. actor.addDamageHate(obj, 0, actor.getHating(MostHate));
  2358. }
  2359. else
  2360. {
  2361. actor.addDamageHate(obj, 0, 2000);
  2362. }
  2363. actor.setTarget(obj);
  2364. setAttackTarget(obj);
  2365. }
  2366. }
  2367. }
  2368. }
  2369. private List<Skill> longRangeSkillRender()
  2370. {
  2371. longRangeSkills = _skillrender.getAISkills(AISkillScope.LONG_RANGE);
  2372. if (longRangeSkills.isEmpty())
  2373. {
  2374. longRangeSkills = getActiveChar().getLongRangeSkill();
  2375. }
  2376. return longRangeSkills;
  2377. }
  2378. private List<Skill> shortRangeSkillRender()
  2379. {
  2380. shortRangeSkills = _skillrender.getAISkills(AISkillScope.SHORT_RANGE);
  2381. if (shortRangeSkills.isEmpty())
  2382. {
  2383. shortRangeSkills = getActiveChar().getShortRangeSkill();
  2384. }
  2385. return shortRangeSkills;
  2386. }
  2387. /**
  2388. * Manage AI thinking actions of a L2Attackable.
  2389. */
  2390. @Override
  2391. protected void onEvtThink()
  2392. {
  2393. // Check if the actor can't use skills and if a thinking action isn't already in progress
  2394. if (_thinking || getActiveChar().isAllSkillsDisabled())
  2395. {
  2396. return;
  2397. }
  2398. // Start thinking action
  2399. _thinking = true;
  2400. try
  2401. {
  2402. // Manage AI thinks of a L2Attackable
  2403. switch (getIntention())
  2404. {
  2405. case AI_INTENTION_ACTIVE:
  2406. thinkActive();
  2407. break;
  2408. case AI_INTENTION_ATTACK:
  2409. thinkAttack();
  2410. break;
  2411. case AI_INTENTION_CAST:
  2412. thinkCast();
  2413. break;
  2414. }
  2415. }
  2416. catch (Exception e)
  2417. {
  2418. _log.warning(getClass().getSimpleName() + ": " + this + " - onEvtThink() failed: " + e.getMessage());
  2419. }
  2420. finally
  2421. {
  2422. // Stop thinking action
  2423. _thinking = false;
  2424. }
  2425. }
  2426. /**
  2427. * Launch actions corresponding to the Event Attacked.<br>
  2428. * <B><U> Actions</U> :</B>
  2429. * <ul>
  2430. * <li>Init the attack : Calculate the attack timeout, Set the _globalAggro to 0, Add the attacker to the actor _aggroList</li>
  2431. * <li>Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance</li>
  2432. * <li>Set the Intention to AI_INTENTION_ATTACK</li>
  2433. * </ul>
  2434. * @param attacker The L2Character that attacks the actor
  2435. */
  2436. @Override
  2437. protected void onEvtAttacked(L2Character attacker)
  2438. {
  2439. L2Attackable me = getActiveChar();
  2440. // Calculate the attack timeout
  2441. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getInstance().getGameTicks();
  2442. // Set the _globalAggro to 0 to permit attack even just after spawn
  2443. if (_globalAggro < 0)
  2444. {
  2445. _globalAggro = 0;
  2446. }
  2447. // Add the attacker to the _aggroList of the actor
  2448. me.addDamageHate(attacker, 0, 1);
  2449. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  2450. if (!me.isRunning())
  2451. {
  2452. me.setRunning();
  2453. }
  2454. // Set the Intention to AI_INTENTION_ATTACK
  2455. if (getIntention() != AI_INTENTION_ATTACK)
  2456. {
  2457. setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker);
  2458. }
  2459. else if (me.getMostHated() != getAttackTarget())
  2460. {
  2461. setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker);
  2462. }
  2463. if (me instanceof L2MonsterInstance)
  2464. {
  2465. L2MonsterInstance master = (L2MonsterInstance) me;
  2466. if (master.hasMinions())
  2467. {
  2468. master.getMinionList().onAssist(me, attacker);
  2469. }
  2470. master = master.getLeader();
  2471. if ((master != null) && master.hasMinions())
  2472. {
  2473. master.getMinionList().onAssist(me, attacker);
  2474. }
  2475. }
  2476. super.onEvtAttacked(attacker);
  2477. }
  2478. /**
  2479. * Launch actions corresponding to the Event Aggression.<br>
  2480. * <B><U> Actions</U> :</B>
  2481. * <ul>
  2482. * <li>Add the target to the actor _aggroList or update hate if already present</li>
  2483. * <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>
  2484. * </ul>
  2485. * @param aggro The value of hate to add to the actor against the target
  2486. */
  2487. @Override
  2488. protected void onEvtAggression(L2Character target, int aggro)
  2489. {
  2490. L2Attackable me = getActiveChar();
  2491. if (target != null)
  2492. {
  2493. // Add the target to the actor _aggroList or update hate if already present
  2494. me.addDamageHate(target, 0, aggro);
  2495. // Set the actor AI Intention to AI_INTENTION_ATTACK
  2496. if (getIntention() != CtrlIntention.AI_INTENTION_ATTACK)
  2497. {
  2498. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  2499. if (!me.isRunning())
  2500. {
  2501. me.setRunning();
  2502. }
  2503. setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
  2504. }
  2505. if (me instanceof L2MonsterInstance)
  2506. {
  2507. L2MonsterInstance master = (L2MonsterInstance) me;
  2508. if (master.hasMinions())
  2509. {
  2510. master.getMinionList().onAssist(me, target);
  2511. }
  2512. master = master.getLeader();
  2513. if ((master != null) && master.hasMinions())
  2514. {
  2515. master.getMinionList().onAssist(me, target);
  2516. }
  2517. }
  2518. }
  2519. }
  2520. @Override
  2521. protected void onIntentionActive()
  2522. {
  2523. // Cancel attack timeout
  2524. _attackTimeout = Integer.MAX_VALUE;
  2525. super.onIntentionActive();
  2526. }
  2527. public void setGlobalAggro(int value)
  2528. {
  2529. _globalAggro = value;
  2530. }
  2531. /**
  2532. * @param TP The timepass to set.
  2533. */
  2534. public void setTimepass(int TP)
  2535. {
  2536. timepass = TP;
  2537. }
  2538. /**
  2539. * @return Returns the timepass.
  2540. */
  2541. public int getTimepass()
  2542. {
  2543. return timepass;
  2544. }
  2545. public L2Attackable getActiveChar()
  2546. {
  2547. return (L2Attackable) _actor;
  2548. }
  2549. }