L2AttackableAI.java 80 KB

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