L2AttackableAI.java 80 KB

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