L2AttackableAI.java 82 KB

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