L2AttackableAI.java 80 KB

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