L2AttackableAI.java 80 KB

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