L2AttackableAI.java 79 KB

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