L2AttackableAI.java 81 KB

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