L2AttackableAI.java 82 KB

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