2
0

L2AttackableAI.java 81 KB

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