L2AttackableAI.java 80 KB

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