L2AttackableAI.java 80 KB

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