L2AttackableAI.java 82 KB

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