L2AttackableAI.java 78 KB

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