L2AttackableAI.java 80 KB

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