L2AttackableAI.java 82 KB

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