L2AttackableAI.java 79 KB

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