L2AttackableAI.java 81 KB

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