L2AttackableAI.java 80 KB

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