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