L2AttackableAI.java 79 KB

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