L2AttackableAI.java 80 KB

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