L2AttackableAI.java 78 KB

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