L2AttackableAI.java 77 KB

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