L2AttackableAI.java 79 KB

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