L2AttackableAI.java 80 KB

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