L2AttackableAI.java 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  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 net.sf.l2j.gameserver.ai;
  16. import static net.sf.l2j.gameserver.ai.CtrlIntention.*;
  17. import java.util.Collection;
  18. import java.util.List;
  19. import java.util.concurrent.Future;
  20. import javolution.util.FastList;
  21. import net.sf.l2j.Config;
  22. import net.sf.l2j.gameserver.GameTimeController;
  23. import net.sf.l2j.gameserver.GeoData;
  24. import net.sf.l2j.gameserver.Territory;
  25. import net.sf.l2j.gameserver.ThreadPoolManager;
  26. import net.sf.l2j.gameserver.instancemanager.DimensionalRiftManager;
  27. import net.sf.l2j.gameserver.model.L2Attackable;
  28. import net.sf.l2j.gameserver.model.L2CharPosition;
  29. import net.sf.l2j.gameserver.model.L2Character;
  30. import net.sf.l2j.gameserver.model.L2Object;
  31. import net.sf.l2j.gameserver.model.L2Skill;
  32. import net.sf.l2j.gameserver.model.L2Summon;
  33. import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
  34. import net.sf.l2j.gameserver.model.actor.instance.L2FestivalMonsterInstance;
  35. import net.sf.l2j.gameserver.model.actor.instance.L2FolkInstance;
  36. import net.sf.l2j.gameserver.model.actor.instance.L2FriendlyMobInstance;
  37. import net.sf.l2j.gameserver.model.actor.instance.L2GrandBossInstance;
  38. import net.sf.l2j.gameserver.model.actor.instance.L2GuardInstance;
  39. import net.sf.l2j.gameserver.model.actor.instance.L2MinionInstance;
  40. import net.sf.l2j.gameserver.model.actor.instance.L2MonsterInstance;
  41. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  42. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  43. import net.sf.l2j.gameserver.model.actor.instance.L2PlayableInstance;
  44. import net.sf.l2j.gameserver.model.actor.instance.L2RiftInvaderInstance;
  45. import net.sf.l2j.gameserver.model.quest.Quest;
  46. import net.sf.l2j.gameserver.taskmanager.DecayTaskManager;
  47. import net.sf.l2j.gameserver.util.Util;
  48. import net.sf.l2j.util.Rnd;
  49. /**
  50. * This class manages AI of L2Attackable.<BR><BR>
  51. *
  52. */
  53. public class L2AttackableAI extends L2CharacterAI implements Runnable
  54. {
  55. //protected static final Logger _log = Logger.getLogger(L2AttackableAI.class.getName());
  56. private static final int RANDOM_WALK_RATE = 30; // confirmed
  57. // private static final int MAX_DRIFT_RANGE = 300;
  58. private static final int MAX_ATTACK_TIMEOUT = 300; // int ticks, i.e. 30 seconds
  59. /** The L2Attackable AI task executed every 1s (call onEvtThink method)*/
  60. private Future<?> _aiTask;
  61. /** The delay after which the attacked is stopped */
  62. private int _attackTimeout;
  63. /** The L2Attackable aggro counter */
  64. private int _globalAggro;
  65. /** The flag used to indicate that a thinking action is in progress */
  66. private boolean _thinking; // to prevent recursive thinking
  67. /** For attack AI, analysis of mob and its targets */
  68. private SelfAnalysis _selfAnalysis = new SelfAnalysis();
  69. private TargetAnalysis _mostHatedAnalysis = new TargetAnalysis();
  70. private TargetAnalysis _secondMostHatedAnalysis = new TargetAnalysis();
  71. /**
  72. * Constructor of L2AttackableAI.<BR><BR>
  73. *
  74. * @param accessor The AI accessor of the L2Character
  75. *
  76. */
  77. public L2AttackableAI(L2Character.AIAccessor accessor)
  78. {
  79. super(accessor);
  80. _selfAnalysis.init();
  81. _attackTimeout = Integer.MAX_VALUE;
  82. _globalAggro = -10; // 10 seconds timeout of ATTACK after respawn
  83. }
  84. public void run()
  85. {
  86. // Launch actions corresponding to the Event Think
  87. onEvtThink();
  88. }
  89. /**
  90. * Return True if the target is autoattackable (depends on the actor type).<BR><BR>
  91. *
  92. * <B><U> Actor is a L2GuardInstance</U> :</B><BR><BR>
  93. * <li>The target isn't a Folk or a Door</li>
  94. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  95. * <li>The target is in the actor Aggro range and is at the same height</li>
  96. * <li>The L2PcInstance target has karma (=PK)</li>
  97. * <li>The L2MonsterInstance target is aggressive</li><BR><BR>
  98. *
  99. * <B><U> Actor is a L2SiegeGuardInstance</U> :</B><BR><BR>
  100. * <li>The target isn't a Folk or a Door</li>
  101. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  102. * <li>The target is in the actor Aggro range and is at the same height</li>
  103. * <li>A siege is in progress</li>
  104. * <li>The L2PcInstance target isn't a Defender</li><BR><BR>
  105. *
  106. * <B><U> Actor is a L2FriendlyMobInstance</U> :</B><BR><BR>
  107. * <li>The target isn't a Folk, a Door or another L2NpcInstance</li>
  108. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  109. * <li>The target is in the actor Aggro range and is at the same height</li>
  110. * <li>The L2PcInstance target has karma (=PK)</li><BR><BR>
  111. *
  112. * <B><U> Actor is a L2MonsterInstance</U> :</B><BR><BR>
  113. * <li>The target isn't a Folk, a Door or another L2NpcInstance</li>
  114. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  115. * <li>The target is in the actor Aggro range and is at the same height</li>
  116. * <li>The actor is Aggressive</li><BR><BR>
  117. *
  118. * @param target The targeted L2Object
  119. *
  120. */
  121. private boolean autoAttackCondition(L2Character target)
  122. {
  123. if (target == null || !(_actor instanceof L2Attackable))
  124. return false;
  125. L2Attackable me = (L2Attackable) _actor;
  126. // Check if the target isn't invulnerable
  127. if (target.isInvul())
  128. {
  129. // However EffectInvincible requires to check GMs specially
  130. if (target instanceof L2PcInstance && ((L2PcInstance) target).isGM())
  131. return false;
  132. if (target instanceof L2Summon && ((L2Summon) target).getOwner().isGM())
  133. return false;
  134. }
  135. // Check if the target isn't a Folk or a Door
  136. if (target instanceof L2FolkInstance || target instanceof L2DoorInstance)
  137. return false;
  138. // Check if the target isn't dead, is in the Aggro range and is at the same height
  139. if (target.isAlikeDead() || !me.isInsideRadius(target, me.getAggroRange(), false, false) || Math.abs(_actor.getZ() - target.getZ()) > 300)
  140. return false;
  141. if (_selfAnalysis.cannotMoveOnLand && !target.isInsideZone(L2Character.ZONE_WATER))
  142. return false;
  143. // Check if the target is a L2PlayableInstance
  144. if (target instanceof L2PlayableInstance)
  145. {
  146. // Check if the AI isn't a Raid Boss and the target isn't in silent move mode
  147. if (!(me.isRaid()) && ((L2PlayableInstance) target).isSilentMoving())
  148. return false;
  149. }
  150. // Check if the target is a L2PcInstance
  151. if (target instanceof L2PcInstance)
  152. {
  153. // Don't take the aggro if the GM has the access level below or equal to GM_DONT_TAKE_AGGRO
  154. if (((L2PcInstance) target).isGM() && !((L2PcInstance) target).getAccessLevel().canTakeAggro())
  155. return false;
  156. // TODO: Ideally, autoattack condition should be called from the AI script. In that case,
  157. // it should only implement the basic behaviors while the script will add more specific
  158. // behaviors (like varka/ketra alliance, etc). Once implemented, remove specialized stuff
  159. // from this location. (Fulminus)
  160. // Check if player is an ally (comparing mem addr)
  161. if (me.getFactionId() == "varka" && ((L2PcInstance) target).isAlliedWithVarka())
  162. return false;
  163. if (me.getFactionId() == "ketra" && ((L2PcInstance) target).isAlliedWithKetra())
  164. return false;
  165. // check if the target is within the grace period for JUST getting up from fake death
  166. if (((L2PcInstance) target).isRecentFakeDeath())
  167. return false;
  168. if (target.isInParty() && target.getParty().isInDimensionalRift())
  169. {
  170. byte riftType = target.getParty().getDimensionalRift().getType();
  171. byte riftRoom = target.getParty().getDimensionalRift().getCurrentRoom();
  172. if (me instanceof L2RiftInvaderInstance && !DimensionalRiftManager.getInstance().getRoom(riftType, riftRoom).checkIfInZone(me.getX(), me.getY(), me.getZ()))
  173. return false;
  174. }
  175. }
  176. // Check if the target is a L2Summon
  177. if (target instanceof L2Summon)
  178. {
  179. L2PcInstance owner = ((L2Summon) target).getOwner();
  180. if (owner != null)
  181. {
  182. // Don't take the aggro if the GM has the access level below or equal to GM_DONT_TAKE_AGGRO
  183. if (owner.isGM() && (owner.isInvul() || !owner.getAccessLevel().canTakeAggro()))
  184. return false;
  185. // Check if player is an ally (comparing mem addr)
  186. if (me.getFactionId() == "varka" && owner.isAlliedWithVarka())
  187. return false;
  188. if (me.getFactionId() == "ketra" && owner.isAlliedWithKetra())
  189. return false;
  190. }
  191. }
  192. // Check if the actor is a L2GuardInstance
  193. if (_actor instanceof L2GuardInstance)
  194. {
  195. // Check if the L2PcInstance target has karma (=PK)
  196. if (target instanceof L2PcInstance && ((L2PcInstance) target).getKarma() > 0)
  197. // Los Check
  198. return GeoData.getInstance().canSeeTarget(me, target);
  199. //if (target instanceof L2Summon)
  200. // return ((L2Summon)target).getKarma() > 0;
  201. // Check if the L2MonsterInstance target is aggressive
  202. if (target instanceof L2MonsterInstance)
  203. return (((L2MonsterInstance) target).isAggressive() && GeoData.getInstance().canSeeTarget(me, target));
  204. return false;
  205. }
  206. else if (_actor instanceof L2FriendlyMobInstance)
  207. { // the actor is a L2FriendlyMobInstance
  208. // Check if the target isn't another L2NpcInstance
  209. if (target instanceof L2NpcInstance)
  210. return false;
  211. // Check if the L2PcInstance target has karma (=PK)
  212. if (target instanceof L2PcInstance && ((L2PcInstance) target).getKarma() > 0)
  213. // Los Check
  214. return GeoData.getInstance().canSeeTarget(me, target);
  215. else
  216. return false;
  217. }
  218. else
  219. { //The actor is a L2MonsterInstance
  220. // Check if the target isn't another L2NpcInstance
  221. if (target instanceof L2NpcInstance)
  222. return false;
  223. // depending on config, do not allow mobs to attack _new_ players in peacezones,
  224. // unless they are already following those players from outside the peacezone.
  225. if (!Config.ALT_MOB_AGRO_IN_PEACEZONE && target.isInsideZone(L2Character.ZONE_PEACE))
  226. return false;
  227. if (me.isChampion() && Config.L2JMOD_CHAMPION_PASSIVE)
  228. return false;
  229. // Check if the actor is Aggressive
  230. return (me.isAggressive() && GeoData.getInstance().canSeeTarget(me, target));
  231. }
  232. }
  233. public void startAITask()
  234. {
  235. // If not idle - create an AI task (schedule onEvtThink repeatedly)
  236. if (_aiTask == null)
  237. {
  238. _aiTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(this, 1000, 1000);
  239. }
  240. }
  241. public void stopAITask()
  242. {
  243. if (_aiTask != null)
  244. {
  245. _aiTask.cancel(false);
  246. _aiTask = null;
  247. }
  248. }
  249. @Override
  250. protected void onEvtDead()
  251. {
  252. stopAITask();
  253. super.onEvtDead();
  254. }
  255. /**
  256. * Set the Intention of this L2CharacterAI and create an AI Task executed every 1s (call onEvtThink method) for this L2Attackable.<BR><BR>
  257. *
  258. * <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>
  259. *
  260. * @param intention The new Intention to set to the AI
  261. * @param arg0 The first parameter of the Intention
  262. * @param arg1 The second parameter of the Intention
  263. *
  264. */
  265. @Override
  266. synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
  267. {
  268. if (intention == AI_INTENTION_IDLE || intention == AI_INTENTION_ACTIVE)
  269. {
  270. // Check if actor is not dead
  271. if (!_actor.isAlikeDead())
  272. {
  273. L2Attackable npc = (L2Attackable) _actor;
  274. // If its _knownPlayer isn't empty set the Intention to AI_INTENTION_ACTIVE
  275. if (npc.getKnownList().getKnownPlayers().size() > 0)
  276. intention = AI_INTENTION_ACTIVE;
  277. }
  278. if (intention == AI_INTENTION_IDLE)
  279. {
  280. // Set the Intention of this L2AttackableAI to AI_INTENTION_IDLE
  281. super.changeIntention(AI_INTENTION_IDLE, null, null);
  282. // Stop AI task and detach AI from NPC
  283. if (_aiTask != null)
  284. {
  285. _aiTask.cancel(true);
  286. _aiTask = null;
  287. }
  288. // Cancel the AI
  289. _accessor.detachAI();
  290. return;
  291. }
  292. }
  293. // Set the Intention of this L2AttackableAI to intention
  294. super.changeIntention(intention, arg0, arg1);
  295. // If not idle - create an AI task (schedule onEvtThink repeatedly)
  296. startAITask();
  297. }
  298. /**
  299. * Manage the Attack Intention : Stop current Attack (if necessary), Calculate attack timeout, Start a new Attack and Launch Think Event.<BR><BR>
  300. *
  301. * @param target The L2Character to attack
  302. *
  303. */
  304. @Override
  305. protected void onIntentionAttack(L2Character target)
  306. {
  307. // Calculate the attack timeout
  308. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  309. // self and buffs
  310. if (_selfAnalysis.lastBuffTick + 100 < GameTimeController.getGameTicks())
  311. {
  312. for (L2Skill sk : _selfAnalysis.buffSkills)
  313. {
  314. if (_actor.getFirstEffect(sk.getId()) == null)
  315. {
  316. if (_actor.getCurrentMp() < sk.getMpConsume())
  317. continue;
  318. if (_actor.isSkillDisabled(sk.getId()))
  319. continue;
  320. // no clan buffs here?
  321. if (sk.getTargetType() == L2Skill.SkillTargetType.TARGET_CLAN)
  322. continue;
  323. L2Object OldTarget = _actor.getTarget();
  324. _actor.setTarget(_actor);
  325. clientStopMoving(null);
  326. _accessor.doCast(sk);
  327. // forcing long reuse delay so if cast get interrupted or there would be several buffs, doesn't cast again
  328. _selfAnalysis.lastBuffTick = GameTimeController.getGameTicks();
  329. _actor.setTarget(OldTarget);
  330. }
  331. }
  332. }
  333. // Manage the Attack Intention : Stop current Attack (if necessary), Start a new Attack and Launch Think Event
  334. super.onIntentionAttack(target);
  335. }
  336. /**
  337. * Manage AI standard thinks of a L2Attackable (called by onEvtThink).<BR><BR>
  338. *
  339. * <B><U> Actions</U> :</B><BR><BR>
  340. * <li>Update every 1s the _globalAggro counter to come close to 0</li>
  341. * <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>
  342. * <li>If the actor is a L2GuardInstance that can't attack, order to it to return to its home location</li>
  343. * <li>If the actor is a L2MonsterInstance that can't attack, order to it to random walk (1/100)</li><BR><BR>
  344. *
  345. */
  346. private void thinkActive()
  347. {
  348. L2Attackable npc = (L2Attackable) _actor;
  349. // Update every 1s the _globalAggro counter to come close to 0
  350. if (_globalAggro != 0)
  351. {
  352. if (_globalAggro < 0)
  353. _globalAggro++;
  354. else
  355. _globalAggro--;
  356. }
  357. // Add all autoAttackable L2Character in L2Attackable Aggro Range to its _aggroList with 0 damage and 1 hate
  358. // A L2Attackable isn't aggressive during 10s after its spawn because _globalAggro is set to -10
  359. if (_globalAggro >= 0)
  360. {
  361. // Get all visible objects inside its Aggro Range
  362. //L2Object[] objects = L2World.getInstance().getVisibleObjects(_actor, ((L2NpcInstance)_actor).getAggroRange());
  363. // Go through visible objects
  364. Collection<L2Object> objs = npc.getKnownList().getKnownObjects().values();
  365. //synchronized (npc.getKnownList().getKnownObjects())
  366. {
  367. for (L2Object obj : objs)
  368. {
  369. if (!(obj instanceof L2Character))
  370. continue;
  371. L2Character target = (L2Character) obj;
  372. /*
  373. * Check to see if this is a festival mob spawn.
  374. * If it is, then check to see if the aggro trigger
  375. * is a festival participant...if so, move to attack it.
  376. */
  377. if ((_actor instanceof L2FestivalMonsterInstance) && obj instanceof L2PcInstance)
  378. {
  379. L2PcInstance targetPlayer = (L2PcInstance) obj;
  380. if (!(targetPlayer.isFestivalParticipant()))
  381. continue;
  382. }
  383. /*
  384. * Temporarily adding this commented code as a concept to be used eventually.
  385. * However, the way it is written below will NOT work correctly. The NPC
  386. * should only notify Aggro Range Enter when someone enters the range from outside.
  387. * Instead, the below code will keep notifying even while someone remains within
  388. * the range. Perhaps we need a short knownlist of range = aggroRange for just
  389. * people who are actively within the npc's aggro range?...(Fulminus)
  390. // notify AI that a playable instance came within aggro range
  391. if ((obj instanceof L2PcInstance) || (obj instanceof L2Summon))
  392. {
  393. if ( !((L2Character)obj).isAlikeDead()
  394. && !npc.isInsideRadius(obj, npc.getAggroRange(), true, false) )
  395. {
  396. L2PcInstance targetPlayer = (obj instanceof L2PcInstance)? (L2PcInstance) obj: ((L2Summon) obj).getOwner();
  397. if (npc.getTemplate().getEventQuests(Quest.QuestEventType.ON_AGGRO_RANGE_ENTER) !=null)
  398. for (Quest quest: npc.getTemplate().getEventQuests(Quest.QuestEventType.ON_AGGRO_RANGE_ENTER))
  399. quest.notifyAggroRangeEnter(npc, targetPlayer, (obj instanceof L2Summon));
  400. }
  401. }
  402. */
  403. // TODO: The AI Script ought to handle aggro behaviors in onSee. Once implemented, aggro behaviors ought
  404. // to be removed from here. (Fulminus)
  405. // For each L2Character check if the target is autoattackable
  406. if (autoAttackCondition(target)) // check aggression
  407. {
  408. // Get the hate level of the L2Attackable against this L2Character target contained in _aggroList
  409. int hating = npc.getHating(target);
  410. // Add the attacker to the L2Attackable _aggroList with 0 damage and 1 hate
  411. if (hating == 0)
  412. npc.addDamageHate(target, 0, 1);
  413. }
  414. }
  415. }
  416. // Chose a target from its aggroList
  417. L2Character hated;
  418. if (_actor.isConfused())
  419. hated = getAttackTarget(); // effect handles selection
  420. else
  421. hated = npc.getMostHated();
  422. // Order to the L2Attackable to attack the target
  423. if (hated != null)
  424. {
  425. // Get the hate level of the L2Attackable against this L2Character target contained in _aggroList
  426. int aggro = npc.getHating(hated);
  427. if (aggro + _globalAggro > 0)
  428. {
  429. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  430. if (!_actor.isRunning())
  431. _actor.setRunning();
  432. // Set the AI Intention to AI_INTENTION_ATTACK
  433. setIntention(CtrlIntention.AI_INTENTION_ATTACK, hated);
  434. }
  435. return;
  436. }
  437. }
  438. // Check if the actor is a L2GuardInstance
  439. if (_actor instanceof L2GuardInstance)
  440. {
  441. // Order to the L2GuardInstance to return to its home location because there's no target to attack
  442. ((L2GuardInstance) _actor).returnHome();
  443. }
  444. // If this is a festival monster, then it remains in the same location.
  445. if (_actor instanceof L2FestivalMonsterInstance)
  446. return;
  447. // Check if the mob should not return to spawn point
  448. if (!npc.canReturnToSpawnPoint())
  449. return;
  450. // Minions following leader
  451. if (_actor instanceof L2MinionInstance && ((L2MinionInstance) _actor).getLeader() != null)
  452. {
  453. int offset;
  454. if (_actor.isRaidMinion())
  455. offset = 500; // for Raids - need correction
  456. else
  457. offset = 200; // for normal minions - need correction :)
  458. if (((L2MinionInstance) _actor).getLeader().isRunning())
  459. _actor.setRunning();
  460. else
  461. _actor.setWalking();
  462. if (_actor.getPlanDistanceSq(((L2MinionInstance) _actor).getLeader()) > offset * offset)
  463. {
  464. int x1, y1, z1;
  465. x1 = ((L2MinionInstance) _actor).getLeader().getX() + Rnd.nextInt((offset - 30) * 2) - (offset - 30);
  466. y1 = ((L2MinionInstance) _actor).getLeader().getY() + Rnd.nextInt((offset - 30) * 2) - (offset - 30);
  467. z1 = ((L2MinionInstance) _actor).getLeader().getZ();
  468. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  469. moveTo(x1, y1, z1);
  470. return;
  471. }
  472. else if (Rnd.nextInt(RANDOM_WALK_RATE) == 0)
  473. {
  474. // self and clan buffs
  475. for (L2Skill sk : _selfAnalysis.buffSkills)
  476. {
  477. if (_actor.getFirstEffect(sk.getId()) == null)
  478. {
  479. // if clan buffs, don't buff every time
  480. if (sk.getTargetType() != L2Skill.SkillTargetType.TARGET_SELF && Rnd.nextInt(2) != 0)
  481. continue;
  482. if (_actor.getCurrentMp() < sk.getMpConsume())
  483. continue;
  484. if (_actor.isSkillDisabled(sk.getId()))
  485. continue;
  486. L2Object OldTarget = _actor.getTarget();
  487. _actor.setTarget(_actor);
  488. clientStopMoving(null);
  489. _accessor.doCast(sk);
  490. _actor.setTarget(OldTarget);
  491. return;
  492. }
  493. }
  494. }
  495. }
  496. // Order to the L2MonsterInstance to random walk (1/100)
  497. else if (npc.getSpawn() != null && Rnd.nextInt(RANDOM_WALK_RATE) == 0 && !(_actor.isRaid() || _actor instanceof L2MinionInstance))
  498. {
  499. int x1, y1, z1;
  500. int range = Config.MAX_DRIFT_RANGE;
  501. // self and clan buffs
  502. for (L2Skill sk : _selfAnalysis.buffSkills)
  503. {
  504. if (_actor.getFirstEffect(sk.getId()) == null)
  505. {
  506. // if clan buffs, don't buff every time
  507. if (sk.getTargetType() != L2Skill.SkillTargetType.TARGET_SELF && Rnd.nextInt(2) != 0)
  508. continue;
  509. if (_actor.getCurrentMp() < sk.getMpConsume())
  510. continue;
  511. if (_actor.isSkillDisabled(sk.getId()))
  512. continue;
  513. L2Object OldTarget = _actor.getTarget();
  514. _actor.setTarget(_actor);
  515. clientStopMoving(null);
  516. _accessor.doCast(sk);
  517. _actor.setTarget(OldTarget);
  518. return;
  519. }
  520. }
  521. // If NPC with random coord in territory
  522. if (npc.getSpawn().getLocx() == 0 && npc.getSpawn().getLocy() == 0)
  523. {
  524. // Calculate a destination point in the spawn area
  525. int p[] = Territory.getInstance().getRandomPoint(npc.getSpawn().getLocation());
  526. x1 = p[0];
  527. y1 = p[1];
  528. z1 = p[2];
  529. // Calculate the distance between the current position of the L2Character and the target (x,y)
  530. double distance2 = _actor.getPlanDistanceSq(x1, y1);
  531. if (distance2 > range * range)
  532. {
  533. npc.setisReturningToSpawnPoint(true);
  534. float delay = (float) Math.sqrt(distance2) / range;
  535. x1 = _actor.getX() + (int) ((x1 - _actor.getX()) / delay);
  536. y1 = _actor.getY() + (int) ((y1 - _actor.getY()) / delay);
  537. }
  538. // If NPC with random fixed coord, don't move (unless needs to return to spawnpoint)
  539. if (Territory.getInstance().getProcMax(npc.getSpawn().getLocation()) > 0 && !npc.isReturningToSpawnPoint())
  540. return;
  541. }
  542. else
  543. {
  544. // If NPC with fixed coord
  545. x1 = npc.getSpawn().getLocx() + Rnd.nextInt(range * 2) - range;
  546. y1 = npc.getSpawn().getLocy() + Rnd.nextInt(range * 2) - range;
  547. z1 = npc.getZ();
  548. if (_actor.getPlanDistanceSq(x1, y1) > range * range)
  549. {
  550. npc.setisReturningToSpawnPoint(true);
  551. }
  552. }
  553. //_log.config("Curent pos ("+getX()+", "+getY()+"), moving to ("+x1+", "+y1+").");
  554. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  555. moveTo(x1, y1, z1);
  556. }
  557. }
  558. /**
  559. * Manage AI attack thinks of a L2Attackable (called by onEvtThink).<BR><BR>
  560. *
  561. * <B><U> Actions</U> :</B><BR><BR>
  562. * <li>Update the attack timeout if actor is running</li>
  563. * <li>If target is dead or timeout is expired, stop this attack and set the Intention to AI_INTENTION_ACTIVE</li>
  564. * <li>Call all L2Object of its Faction inside the Faction Range</li>
  565. * <li>Chose a target and order to attack it with magic skill or physical attack</li><BR><BR>
  566. *
  567. * TODO: Manage casting rules to healer mobs (like Ant Nurses)
  568. *
  569. */
  570. private void thinkAttack()
  571. {
  572. if (_attackTimeout < GameTimeController.getGameTicks())
  573. {
  574. // Check if the actor is running
  575. if (_actor.isRunning())
  576. {
  577. // Set the actor movement type to walk and send Server->Client packet ChangeMoveType to all others L2PcInstance
  578. _actor.setWalking();
  579. // Calculate a new attack timeout
  580. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  581. }
  582. }
  583. L2Character originalAttackTarget = getAttackTarget();
  584. // Check if target is dead or if timeout is expired to stop this attack
  585. if (originalAttackTarget == null || originalAttackTarget.isAlikeDead() || _attackTimeout < GameTimeController.getGameTicks())
  586. {
  587. // Stop hating this target after the attack timeout or if target is dead
  588. if (originalAttackTarget != null)
  589. ((L2Attackable) _actor).stopHating(originalAttackTarget);
  590. // Set the AI Intention to AI_INTENTION_ACTIVE
  591. setIntention(AI_INTENTION_ACTIVE);
  592. _actor.setWalking();
  593. return;
  594. }
  595. // Handle all L2Object of its Faction inside the Faction Range
  596. if (((L2NpcInstance) _actor).getFactionId() != null)
  597. {
  598. String faction_id = ((L2NpcInstance) _actor).getFactionId();
  599. // Go through all L2Object that belong to its faction
  600. Collection<L2Object> objs = _actor.getKnownList().getKnownObjects().values();
  601. //synchronized (_actor.getKnownList().getKnownObjects())
  602. {
  603. for (L2Object obj : objs)
  604. {
  605. if (obj instanceof L2NpcInstance)
  606. {
  607. L2NpcInstance npc = (L2NpcInstance) obj;
  608. //Handle SevenSigns mob Factions
  609. String npcfaction = npc.getFactionId();
  610. boolean sevenSignFaction = false;
  611. // TODO: Unhardcode this by AI scripts (DrHouse)
  612. //Catacomb mobs should assist lilim and nephilim other than dungeon
  613. if ( (faction_id == "c_dungeon_clan") &&
  614. ((npcfaction == "c_dungeon_lilim") || npcfaction == "c_dungeon_nephi"))
  615. sevenSignFaction = true;
  616. //Lilim mobs should assist other Lilim and catacomb mobs
  617. else if ( (faction_id == "c_dungeon_lilim") &&
  618. (npcfaction == "c_dungeon_clan"))
  619. sevenSignFaction = true;
  620. //Nephilim mobs should assist other Nephilim and catacomb mobs
  621. else if ( (faction_id == "c_dungeon_nephi") &&
  622. (npcfaction == "c_dungeon_clan"))
  623. sevenSignFaction = true;
  624. if (faction_id != npc.getFactionId() && !sevenSignFaction)
  625. continue;
  626. // Check if the L2Object is inside the Faction Range of
  627. // the actor
  628. if (_actor.isInsideRadius(npc, npc.getFactionRange() + npc.getTemplate().collisionRadius, true, false) && npc.getAI() != null)
  629. {
  630. if (Math.abs(originalAttackTarget.getZ() - npc.getZ()) < 600 && _actor.getAttackByList().contains(originalAttackTarget)
  631. && (npc.getAI()._intention == CtrlIntention.AI_INTENTION_IDLE || npc.getAI()._intention == CtrlIntention.AI_INTENTION_ACTIVE) && GeoData.getInstance().canSeeTarget(_actor, npc))
  632. {
  633. if ((originalAttackTarget instanceof L2PcInstance) || (originalAttackTarget instanceof L2Summon))
  634. {
  635. if (npc.getTemplate().getEventQuests(Quest.QuestEventType.ON_FACTION_CALL) != null)
  636. {
  637. L2PcInstance player = (originalAttackTarget instanceof L2PcInstance) ? (L2PcInstance) originalAttackTarget : ((L2Summon) originalAttackTarget).getOwner();
  638. for (Quest quest : npc.getTemplate().getEventQuests(Quest.QuestEventType.ON_FACTION_CALL))
  639. quest.notifyFactionCall(npc, (L2NpcInstance) _actor, player, (originalAttackTarget instanceof L2Summon));
  640. }
  641. }
  642. }
  643. // heal or resurrect friends
  644. if (_selfAnalysis.hasHealOrResurrect && !_actor.isAttackingDisabled() && npc.getCurrentHp() < npc.getMaxHp() * 0.6 && _actor.getCurrentHp() > _actor.getMaxHp() / 2
  645. && _actor.getCurrentMp() > _actor.getMaxMp() / 2
  646. )
  647. {
  648. if (npc.isDead() && _actor instanceof L2MinionInstance)
  649. {
  650. if (((L2MinionInstance) _actor).getLeader() == npc)
  651. {
  652. for (L2Skill sk : _selfAnalysis.resurrectSkills)
  653. {
  654. if (_actor.getCurrentMp() < sk.getMpConsume())
  655. continue;
  656. if (_actor.isSkillDisabled(sk.getId()))
  657. continue;
  658. if (!Util.checkIfInRange(sk.getCastRange(), _actor, npc, true))
  659. continue;
  660. if (10 >= Rnd.get(100)) // chance
  661. continue;
  662. if (!GeoData.getInstance().canSeeTarget(_actor, npc))
  663. break;
  664. L2Object OldTarget = _actor.getTarget();
  665. _actor.setTarget(npc);
  666. // would this ever be fast enough
  667. // for the decay not to run?
  668. // giving some extra seconds
  669. DecayTaskManager.getInstance().cancelDecayTask(npc);
  670. DecayTaskManager.getInstance().addDecayTask(npc);
  671. clientStopMoving(null);
  672. _accessor.doCast(sk);
  673. _actor.setTarget(OldTarget);
  674. return;
  675. }
  676. }
  677. }
  678. else if (npc.isInCombat())
  679. {
  680. for (L2Skill sk : _selfAnalysis.healSkills)
  681. {
  682. if (_actor.getCurrentMp() < sk.getMpConsume())
  683. continue;
  684. if (_actor.isSkillDisabled(sk.getId()))
  685. continue;
  686. if (!Util.checkIfInRange(sk.getCastRange(), _actor, npc, true))
  687. continue;
  688. int chance = 4;
  689. if (_actor instanceof L2MinionInstance)
  690. {
  691. // minions support boss
  692. if (((L2MinionInstance) _actor).getLeader() == npc)
  693. chance = 6;
  694. else
  695. chance = 3;
  696. }
  697. if (npc instanceof L2GrandBossInstance)
  698. chance = 6;
  699. if (chance >= Rnd.get(100)) // chance
  700. continue;
  701. if (!GeoData.getInstance().canSeeTarget(_actor, npc))
  702. break;
  703. L2Object OldTarget = _actor.getTarget();
  704. _actor.setTarget(npc);
  705. clientStopMoving(null);
  706. _accessor.doCast(sk);
  707. _actor.setTarget(OldTarget);
  708. return;
  709. }
  710. }
  711. }
  712. }
  713. }
  714. }
  715. }
  716. }
  717. if (_actor.isAttackingDisabled())
  718. return;
  719. // Get 2 most hated chars
  720. List<L2Character> hated = ((L2Attackable) _actor).get2MostHated();
  721. if (_actor.isConfused())
  722. {
  723. if (hated != null)
  724. hated.set(0, originalAttackTarget); // effect handles selection
  725. else
  726. {
  727. hated = new FastList<L2Character>();
  728. hated.add(originalAttackTarget);
  729. hated.add(null);
  730. }
  731. }
  732. if (hated == null || hated.get(0) == null)
  733. {
  734. setIntention(AI_INTENTION_ACTIVE);
  735. return;
  736. }
  737. if (hated.get(0) != originalAttackTarget)
  738. {
  739. setAttackTarget(hated.get(0));
  740. }
  741. _mostHatedAnalysis.update(hated.get(0));
  742. _secondMostHatedAnalysis.update(hated.get(1));
  743. // Get all information needed to choose between physical or magical attack
  744. _actor.setTarget(_mostHatedAnalysis.character);
  745. double dist2 = _actor.getPlanDistanceSq(_mostHatedAnalysis.character.getX(), _mostHatedAnalysis.character.getY());
  746. int combinedCollision = _actor.getTemplate().collisionRadius + _mostHatedAnalysis.character.getTemplate().collisionRadius;
  747. int range = _actor.getPhysicalAttackRange() + combinedCollision;
  748. // Reconsider target next round if _actor hasn't got hits in for last 14 seconds
  749. if (!_actor.isMuted() && _attackTimeout - 160 < GameTimeController.getGameTicks() && _secondMostHatedAnalysis.character != null)
  750. {
  751. if (Util.checkIfInRange(900, _actor, hated.get(1), true))
  752. {
  753. // take off 2* the amount the aggro is larger than second most
  754. ((L2Attackable) _actor).reduceHate(hated.get(0), 2 * (((L2Attackable) _actor).getHating(hated.get(0)) - ((L2Attackable) _actor).getHating(hated.get(1))));
  755. // Calculate a new attack timeout
  756. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  757. }
  758. }
  759. // Reconsider target during next round if actor is rooted and cannot reach mostHated but can
  760. // reach secondMostHated
  761. if (_actor.isRooted() && _secondMostHatedAnalysis.character != null)
  762. {
  763. if (_selfAnalysis.isMage && dist2 > _selfAnalysis.maxCastRange * _selfAnalysis.maxCastRange
  764. && _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY()) < _selfAnalysis.maxCastRange * _selfAnalysis.maxCastRange)
  765. {
  766. ((L2Attackable) _actor).reduceHate(hated.get(0), 1 + (((L2Attackable) _actor).getHating(hated.get(0)) - ((L2Attackable) _actor).getHating(hated.get(1))));
  767. }
  768. else if (dist2 > range * range && _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY()) < range * range)
  769. {
  770. ((L2Attackable) _actor).reduceHate(hated.get(0), 1 + (((L2Attackable) _actor).getHating(hated.get(0)) - ((L2Attackable) _actor).getHating(hated.get(1))));
  771. }
  772. }
  773. // Considering, if bigger range will be attempted
  774. if ((dist2 < 10000 + combinedCollision * combinedCollision)
  775. && !_selfAnalysis.isFighter && !_selfAnalysis.isBalanced
  776. && (_selfAnalysis.hasLongRangeSkills || _selfAnalysis.isArcher || _selfAnalysis.isHealer)
  777. && (_mostHatedAnalysis.isBalanced || _mostHatedAnalysis.isFighter)
  778. && (_mostHatedAnalysis.character.isRooted() || _mostHatedAnalysis.isSlower)
  779. && (Config.GEODATA == 2 ? 20 : 12) >= Rnd.get(100) // chance
  780. )
  781. {
  782. int posX = _actor.getX();
  783. int posY = _actor.getY();
  784. int posZ = _actor.getZ();
  785. double distance = Math.sqrt(dist2); // This way, we only do the sqrt if we need it
  786. int signx = -1;
  787. int signy = -1;
  788. if (_actor.getX() > _mostHatedAnalysis.character.getX())
  789. signx = 1;
  790. if (_actor.getY() > _mostHatedAnalysis.character.getY())
  791. signy = 1;
  792. posX += Math.round((float) ((signx * ((range / 2) + (Rnd.get(range)))) - distance));
  793. posY += Math.round((float) ((signy * ((range / 2) + (Rnd.get(range)))) - distance));
  794. setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, new L2CharPosition(posX, posY, posZ, 0));
  795. return;
  796. }
  797. // Cannot see target, needs to go closer, currently just goes to range 300 if mage
  798. if ((dist2 > 310 * 310 + combinedCollision * combinedCollision)
  799. && this._selfAnalysis.hasLongRangeSkills
  800. && !GeoData.getInstance().canSeeTarget(_actor, _mostHatedAnalysis.character))
  801. {
  802. if (!(_selfAnalysis.isMage && _actor.isMuted()))
  803. {
  804. moveToPawn(_mostHatedAnalysis.character, 300);
  805. return;
  806. }
  807. }
  808. if (_mostHatedAnalysis.character.isMoving())
  809. range += 50;
  810. // Check if the actor is far from target
  811. if (dist2 > range * range)
  812. {
  813. if (!_actor.isMuted() && (_selfAnalysis.hasLongRangeSkills || !_selfAnalysis.healSkills.isEmpty()))
  814. {
  815. // check for long ranged skills and heal/buff skills
  816. if (!_mostHatedAnalysis.isCanceled)
  817. {
  818. for (L2Skill sk : _selfAnalysis.cancelSkills)
  819. {
  820. int castRange = sk.getCastRange() + combinedCollision;
  821. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  822. continue;
  823. if (Rnd.nextInt(100) <= 8)
  824. {
  825. clientStopMoving(null);
  826. _accessor.doCast(sk);
  827. _mostHatedAnalysis.isCanceled = true;
  828. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  829. return;
  830. }
  831. }
  832. }
  833. if (this._selfAnalysis.lastDebuffTick + 60 < GameTimeController.getGameTicks())
  834. {
  835. for (L2Skill sk : _selfAnalysis.debuffSkills)
  836. {
  837. int castRange = sk.getCastRange() + combinedCollision;
  838. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  839. continue;
  840. int chance = 8;
  841. if (_selfAnalysis.isFighter && _mostHatedAnalysis.isMage)
  842. chance = 3;
  843. if (_selfAnalysis.isFighter && _mostHatedAnalysis.isArcher)
  844. chance = 12;
  845. if (_selfAnalysis.isMage && !_mostHatedAnalysis.isMage)
  846. chance = 10;
  847. if (_selfAnalysis.isHealer)
  848. chance = 12;
  849. if (_mostHatedAnalysis.isMagicResistant)
  850. chance /= 2;
  851. if (Rnd.nextInt(100) <= chance)
  852. {
  853. clientStopMoving(null);
  854. _accessor.doCast(sk);
  855. _selfAnalysis.lastDebuffTick = GameTimeController.getGameTicks();
  856. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  857. return;
  858. }
  859. }
  860. }
  861. if (!_mostHatedAnalysis.character.isMuted())
  862. {
  863. int chance = 8;
  864. if (!(_mostHatedAnalysis.isMage || _mostHatedAnalysis.isBalanced))
  865. chance = 3;
  866. for (L2Skill sk : _selfAnalysis.muteSkills)
  867. {
  868. int castRange = sk.getCastRange() + combinedCollision;
  869. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  870. continue;
  871. if (Rnd.nextInt(100) <= chance)
  872. {
  873. clientStopMoving(null);
  874. _accessor.doCast(sk);
  875. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  876. return;
  877. }
  878. }
  879. }
  880. if (_secondMostHatedAnalysis.character != null && !_secondMostHatedAnalysis.character.isMuted() && (_secondMostHatedAnalysis.isMage || _secondMostHatedAnalysis.isBalanced))
  881. {
  882. double secondHatedDist2 = _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY());
  883. for (L2Skill sk : _selfAnalysis.muteSkills)
  884. {
  885. int castRange = sk.getCastRange() + combinedCollision;
  886. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (secondHatedDist2 > castRange * castRange))
  887. continue;
  888. if (Rnd.nextInt(100) <= 2)
  889. {
  890. _actor.setTarget(_secondMostHatedAnalysis.character);
  891. clientStopMoving(null);
  892. _accessor.doCast(sk);
  893. _actor.setTarget(_mostHatedAnalysis.character);
  894. return;
  895. }
  896. }
  897. }
  898. if (!_mostHatedAnalysis.character.isSleeping())
  899. {
  900. for (L2Skill sk : _selfAnalysis.sleepSkills)
  901. {
  902. int castRange = sk.getCastRange() + combinedCollision;
  903. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  904. continue;
  905. if (Rnd.nextInt(100) <= (_selfAnalysis.isHealer ? 10 : 1))
  906. {
  907. clientStopMoving(null);
  908. _accessor.doCast(sk);
  909. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  910. return;
  911. }
  912. }
  913. }
  914. if (_secondMostHatedAnalysis.character != null && !_secondMostHatedAnalysis.character.isSleeping())
  915. {
  916. double secondHatedDist2 = _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY());
  917. for (L2Skill sk : _selfAnalysis.sleepSkills)
  918. {
  919. int castRange = sk.getCastRange() + combinedCollision;
  920. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (secondHatedDist2 > castRange * castRange))
  921. continue;
  922. if (Rnd.nextInt(100) <= (_selfAnalysis.isHealer ? 10 : 3))
  923. {
  924. _actor.setTarget(_secondMostHatedAnalysis.character);
  925. clientStopMoving(null);
  926. _accessor.doCast(sk);
  927. _actor.setTarget(_mostHatedAnalysis.character);
  928. return;
  929. }
  930. }
  931. }
  932. if (!_mostHatedAnalysis.character.isRooted())
  933. {
  934. for (L2Skill sk : _selfAnalysis.rootSkills)
  935. {
  936. int castRange = sk.getCastRange() + combinedCollision;
  937. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  938. continue;
  939. if (Rnd.nextInt(100) <= (_mostHatedAnalysis.isSlower ? 3 : 8))
  940. {
  941. clientStopMoving(null);
  942. _accessor.doCast(sk);
  943. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  944. return;
  945. }
  946. }
  947. }
  948. if (!_mostHatedAnalysis.character.isAttackingDisabled())
  949. {
  950. for (L2Skill sk : _selfAnalysis.generalDisablers)
  951. {
  952. int castRange = sk.getCastRange() + combinedCollision;
  953. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  954. continue;
  955. if (Rnd.nextInt(100) <= ((_selfAnalysis.isFighter && _actor.isRooted()) ? 15 : 7))
  956. {
  957. clientStopMoving(null);
  958. _accessor.doCast(sk);
  959. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  960. return;
  961. }
  962. }
  963. }
  964. if (_actor.getCurrentHp() < _actor.getMaxHp() * 0.4)
  965. {
  966. for (L2Skill sk : _selfAnalysis.healSkills)
  967. {
  968. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk))
  969. continue;
  970. int chance = 7;
  971. if (_mostHatedAnalysis.character.isAttackingDisabled())
  972. chance += 10;
  973. if (_secondMostHatedAnalysis.character == null || _secondMostHatedAnalysis.character.isAttackingDisabled())
  974. chance += 10;
  975. if (Rnd.nextInt(100) <= chance)
  976. {
  977. _actor.setTarget(_actor);
  978. clientStopMoving(null);
  979. _accessor.doCast(sk);
  980. _actor.setTarget(_mostHatedAnalysis.character);
  981. return;
  982. }
  983. }
  984. }
  985. // chance decision for launching long range skills
  986. int castingChance = 5;
  987. if (_selfAnalysis.isMage || _selfAnalysis.isHealer)
  988. castingChance = 50; // mages
  989. if (_selfAnalysis.isBalanced)
  990. {
  991. if (!_mostHatedAnalysis.isFighter) // advance to mages
  992. castingChance = 15;
  993. else
  994. castingChance = 25; // stay away from fighters
  995. }
  996. if (_selfAnalysis.isFighter)
  997. {
  998. if (_mostHatedAnalysis.isMage)
  999. castingChance = 3;
  1000. else
  1001. castingChance = 7;
  1002. if (_actor.isRooted())
  1003. castingChance = 20; // doesn't matter if no success first round
  1004. }
  1005. for (L2Skill sk : _selfAnalysis.generalSkills)
  1006. {
  1007. int castRange = sk.getCastRange() + combinedCollision;
  1008. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1009. continue;
  1010. if (Rnd.nextInt(100) <= castingChance)
  1011. {
  1012. clientStopMoving(null);
  1013. _accessor.doCast(sk);
  1014. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  1015. return;
  1016. }
  1017. }
  1018. }
  1019. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  1020. if (_selfAnalysis.isMage && !_actor.isMuted())
  1021. {
  1022. // mages stay a bit further away if not muted or low mana
  1023. if ((_actor.getMaxMp() / 3) < _actor.getCurrentMp())
  1024. {
  1025. range = _selfAnalysis.maxCastRange;
  1026. if (dist2 < range * range) // don't move backwards here
  1027. return;
  1028. }
  1029. }
  1030. // healers do not even follow
  1031. if (_selfAnalysis.isHealer)
  1032. return;
  1033. if (_mostHatedAnalysis.character.isMoving())
  1034. range -= 100;
  1035. if (range < 5)
  1036. range = 5;
  1037. moveToPawn(_mostHatedAnalysis.character, range);
  1038. return;
  1039. }
  1040. // **************************************************
  1041. // Else, if this is close enough for physical attacks
  1042. else
  1043. {
  1044. // In case many mobs are trying to hit from same place, move a bit,
  1045. // circling around the target
  1046. if (Rnd.nextInt(100) <= 33) // check it once per 3 seconds
  1047. {
  1048. for (L2Object nearby : _actor.getKnownList().getKnownCharactersInRadius(10))
  1049. {
  1050. if (nearby instanceof L2Attackable && nearby != _mostHatedAnalysis.character)
  1051. {
  1052. int diffx = Rnd.get(combinedCollision, combinedCollision + 40);
  1053. if (Rnd.get(10) < 5)
  1054. diffx = -diffx;
  1055. int diffy = Rnd.get(combinedCollision, combinedCollision + 40);
  1056. if (Rnd.get(10) < 5)
  1057. diffy = -diffy;
  1058. moveTo(_mostHatedAnalysis.character.getX() + diffx, _mostHatedAnalysis.character.getY() + diffy, _mostHatedAnalysis.character.getZ());
  1059. return;
  1060. }
  1061. }
  1062. }
  1063. // Calculate a new attack timeout.
  1064. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  1065. // check for close combat skills && heal/buff skills
  1066. if (!_mostHatedAnalysis.isCanceled)
  1067. {
  1068. for (L2Skill sk : _selfAnalysis.cancelSkills)
  1069. {
  1070. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1071. continue;
  1072. int castRange = sk.getCastRange() + combinedCollision;
  1073. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1074. continue;
  1075. if (Rnd.nextInt(100) <= 8)
  1076. {
  1077. clientStopMoving(null);
  1078. _accessor.doCast(sk);
  1079. _mostHatedAnalysis.isCanceled = true;
  1080. return;
  1081. }
  1082. }
  1083. }
  1084. if (this._selfAnalysis.lastDebuffTick + 60 < GameTimeController.getGameTicks())
  1085. {
  1086. for (L2Skill sk : _selfAnalysis.debuffSkills)
  1087. {
  1088. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1089. continue;
  1090. int castRange = sk.getCastRange() + combinedCollision;
  1091. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1092. continue;
  1093. int chance = 5;
  1094. if (_selfAnalysis.isFighter && _mostHatedAnalysis.isMage)
  1095. chance = 3;
  1096. if (_selfAnalysis.isFighter && _mostHatedAnalysis.isArcher)
  1097. chance = 3;
  1098. if (_selfAnalysis.isMage && !_mostHatedAnalysis.isMage)
  1099. chance = 4;
  1100. if (_selfAnalysis.isHealer)
  1101. chance = 12;
  1102. if (_mostHatedAnalysis.isMagicResistant)
  1103. chance /= 2;
  1104. if (sk.getCastRange() < 200)
  1105. chance += 3;
  1106. if (Rnd.nextInt(100) <= chance)
  1107. {
  1108. clientStopMoving(null);
  1109. _accessor.doCast(sk);
  1110. _selfAnalysis.lastDebuffTick = GameTimeController.getGameTicks();
  1111. return;
  1112. }
  1113. }
  1114. }
  1115. if (!_mostHatedAnalysis.character.isMuted() && (_mostHatedAnalysis.isMage || _mostHatedAnalysis.isBalanced))
  1116. {
  1117. for (L2Skill sk : _selfAnalysis.muteSkills)
  1118. {
  1119. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1120. continue;
  1121. int castRange = sk.getCastRange() + combinedCollision;
  1122. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1123. continue;
  1124. if (Rnd.nextInt(100) <= 7)
  1125. {
  1126. clientStopMoving(null);
  1127. _accessor.doCast(sk);
  1128. return;
  1129. }
  1130. }
  1131. }
  1132. if (_secondMostHatedAnalysis.character != null && !_secondMostHatedAnalysis.character.isMuted() && (_secondMostHatedAnalysis.isMage || _secondMostHatedAnalysis.isBalanced))
  1133. {
  1134. double secondHatedDist2 = _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY());
  1135. for (L2Skill sk : _selfAnalysis.muteSkills)
  1136. {
  1137. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1138. continue;
  1139. int castRange = sk.getCastRange() + combinedCollision;
  1140. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (secondHatedDist2 > castRange * castRange))
  1141. continue;
  1142. if (Rnd.nextInt(100) <= 3)
  1143. {
  1144. _actor.setTarget(_secondMostHatedAnalysis.character);
  1145. clientStopMoving(null);
  1146. _accessor.doCast(sk);
  1147. _actor.setTarget(_mostHatedAnalysis.character);
  1148. return;
  1149. }
  1150. }
  1151. }
  1152. if (!_mostHatedAnalysis.character.isSleeping() && _selfAnalysis.isHealer)
  1153. {
  1154. for (L2Skill sk : _selfAnalysis.sleepSkills)
  1155. {
  1156. int castRange = sk.getCastRange() + combinedCollision;
  1157. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1158. continue;
  1159. if (Rnd.nextInt(100) <= 10)
  1160. {
  1161. clientStopMoving(null);
  1162. _accessor.doCast(sk);
  1163. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  1164. return;
  1165. }
  1166. }
  1167. }
  1168. if (_secondMostHatedAnalysis.character != null && !_secondMostHatedAnalysis.character.isSleeping())
  1169. {
  1170. double secondHatedDist2 = _actor.getPlanDistanceSq(_secondMostHatedAnalysis.character.getX(), _secondMostHatedAnalysis.character.getY());
  1171. for (L2Skill sk : _selfAnalysis.sleepSkills)
  1172. {
  1173. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1174. continue;
  1175. int castRange = sk.getCastRange() + combinedCollision;
  1176. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (secondHatedDist2 > castRange * castRange))
  1177. continue;
  1178. if (Rnd.nextInt(100) <= (_selfAnalysis.isHealer ? 10 : 4))
  1179. {
  1180. _actor.setTarget(_secondMostHatedAnalysis.character);
  1181. clientStopMoving(null);
  1182. _accessor.doCast(sk);
  1183. _actor.setTarget(_mostHatedAnalysis.character);
  1184. return;
  1185. }
  1186. }
  1187. }
  1188. if (!_mostHatedAnalysis.character.isRooted() && _mostHatedAnalysis.isFighter && !_selfAnalysis.isFighter)
  1189. {
  1190. for (L2Skill sk : _selfAnalysis.rootSkills)
  1191. {
  1192. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1193. continue;
  1194. int castRange = sk.getCastRange() + combinedCollision;
  1195. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1196. continue;
  1197. if (Rnd.nextInt(100) <= (_selfAnalysis.isHealer ? 10 : 4))
  1198. {
  1199. clientStopMoving(null);
  1200. _accessor.doCast(sk);
  1201. return;
  1202. }
  1203. }
  1204. }
  1205. if (!_mostHatedAnalysis.character.isAttackingDisabled())
  1206. {
  1207. for (L2Skill sk : _selfAnalysis.generalDisablers)
  1208. {
  1209. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1210. continue;
  1211. int castRange = sk.getCastRange() + combinedCollision;
  1212. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1213. continue;
  1214. if (Rnd.nextInt(100) <= ((sk.getCastRange() < 200) ? 10 : 7))
  1215. {
  1216. clientStopMoving(null);
  1217. _accessor.doCast(sk);
  1218. return;
  1219. }
  1220. }
  1221. }
  1222. if (_actor.getCurrentHp() < _actor.getMaxHp() * (_selfAnalysis.isHealer ? 0.7 : 0.4))
  1223. {
  1224. for (L2Skill sk : _selfAnalysis.healSkills)
  1225. {
  1226. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1227. continue;
  1228. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk))
  1229. continue;
  1230. int chance = (_selfAnalysis.isHealer ? 15 : 7);
  1231. if (_mostHatedAnalysis.character.isAttackingDisabled())
  1232. chance += 10;
  1233. if (_secondMostHatedAnalysis.character == null || _secondMostHatedAnalysis.character.isAttackingDisabled())
  1234. chance += 10;
  1235. if (Rnd.nextInt(100) <= chance)
  1236. {
  1237. _actor.setTarget(_actor);
  1238. clientStopMoving(null);
  1239. _accessor.doCast(sk);
  1240. _actor.setTarget(_mostHatedAnalysis.character);
  1241. return;
  1242. }
  1243. }
  1244. }
  1245. for (L2Skill sk : _selfAnalysis.generalSkills)
  1246. {
  1247. if ((_actor.isMuted() && sk.isMagic()) || (_actor.isPhysicalMuted() && !sk.isMagic()))
  1248. continue;
  1249. int castRange = sk.getCastRange() + combinedCollision;
  1250. if (_actor.isSkillDisabled(sk.getId()) || _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk) || (dist2 > castRange * castRange))
  1251. continue;
  1252. // chance decision for launching general skills in melee fight
  1253. // close range skills should be higher, long range lower
  1254. int castingChance = 5;
  1255. if (_selfAnalysis.isMage || _selfAnalysis.isHealer)
  1256. {
  1257. if (sk.getCastRange() < 200)
  1258. castingChance = 35;
  1259. else
  1260. castingChance = 25; // mages
  1261. }
  1262. if (_selfAnalysis.isBalanced)
  1263. {
  1264. if (sk.getCastRange() < 200)
  1265. castingChance = 12;
  1266. else
  1267. {
  1268. if (_mostHatedAnalysis.isMage) // hit mages
  1269. castingChance = 2;
  1270. else
  1271. castingChance = 5;
  1272. }
  1273. }
  1274. if (_selfAnalysis.isFighter)
  1275. {
  1276. if (sk.getCastRange() < 200)
  1277. castingChance = 12;
  1278. else
  1279. {
  1280. if (_mostHatedAnalysis.isMage)
  1281. castingChance = 1;
  1282. else
  1283. castingChance = 3;
  1284. }
  1285. }
  1286. if (Rnd.nextInt(100) <= castingChance)
  1287. {
  1288. clientStopMoving(null);
  1289. _accessor.doCast(sk);
  1290. return;
  1291. }
  1292. }
  1293. // Finally, physical attacks
  1294. if (!_selfAnalysis.isHealer)
  1295. {
  1296. clientStopMoving(null);
  1297. _accessor.doAttack(_mostHatedAnalysis.character);
  1298. }
  1299. }
  1300. }
  1301. /**
  1302. * Manage AI thinking actions of a L2Attackable.<BR><BR>
  1303. */
  1304. @Override
  1305. protected void onEvtThink()
  1306. {
  1307. // Check if the thinking action is already in progress
  1308. if (_thinking || _actor.isCastingNow() || _actor.isAllSkillsDisabled())
  1309. return;
  1310. // Start thinking action
  1311. _thinking = true;
  1312. try
  1313. {
  1314. // Manage AI thinks of a L2Attackable
  1315. if (getIntention() == AI_INTENTION_ACTIVE)
  1316. thinkActive();
  1317. else if (getIntention() == AI_INTENTION_ATTACK)
  1318. thinkAttack();
  1319. }
  1320. finally
  1321. {
  1322. // Stop thinking action
  1323. _thinking = false;
  1324. }
  1325. }
  1326. /**
  1327. * Launch actions corresponding to the Event Attacked.<BR><BR>
  1328. *
  1329. * <B><U> Actions</U> :</B><BR><BR>
  1330. * <li>Init the attack : Calculate the attack timeout, Set the _globalAggro to 0, Add the attacker to the actor _aggroList</li>
  1331. * <li>Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance</li>
  1332. * <li>Set the Intention to AI_INTENTION_ATTACK</li><BR><BR>
  1333. *
  1334. * @param attacker The L2Character that attacks the actor
  1335. *
  1336. */
  1337. @Override
  1338. protected void onEvtAttacked(L2Character attacker)
  1339. {
  1340. //if (_actor instanceof L2ChestInstance && !((L2ChestInstance)_actor).isInteracted())
  1341. //{
  1342. //((L2ChestInstance)_actor).deleteMe();
  1343. //((L2ChestInstance)_actor).getSpawn().startRespawn();
  1344. //return;
  1345. //}
  1346. // Calculate the attack timeout
  1347. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  1348. // Set the _globalAggro to 0 to permit attack even just after spawn
  1349. if (_globalAggro < 0)
  1350. _globalAggro = 0;
  1351. // Add the attacker to the _aggroList of the actor
  1352. if (!((L2Attackable) _actor).isCoreAIDisabled())
  1353. ((L2Attackable) _actor).addDamageHate(attacker, 0, 1);
  1354. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  1355. if (!_actor.isRunning())
  1356. _actor.setRunning();
  1357. // Set the Intention to AI_INTENTION_ATTACK
  1358. if (getIntention() != AI_INTENTION_ATTACK && !((L2Attackable) _actor).isCoreAIDisabled())
  1359. {
  1360. setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker);
  1361. }
  1362. else if (((L2Attackable) _actor).getMostHated() != getAttackTarget() && !((L2Attackable) _actor).isCoreAIDisabled())
  1363. {
  1364. setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker);
  1365. }
  1366. else if (getIntention() != AI_INTENTION_INTERACT && ((L2Attackable) _actor).isCoreAIDisabled())
  1367. setIntention(CtrlIntention.AI_INTENTION_INTERACT, attacker);
  1368. super.onEvtAttacked(attacker);
  1369. }
  1370. /**
  1371. * Launch actions corresponding to the Event Aggression.<BR><BR>
  1372. *
  1373. * <B><U> Actions</U> :</B><BR><BR>
  1374. * <li>Add the target to the actor _aggroList or update hate if already present </li>
  1375. * <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>
  1376. *
  1377. * @param attacker The L2Character that attacks
  1378. * @param aggro The value of hate to add to the actor against the target
  1379. *
  1380. */
  1381. @Override
  1382. protected void onEvtAggression(L2Character target, int aggro)
  1383. {
  1384. L2Attackable me = (L2Attackable) _actor;
  1385. if (target != null)
  1386. {
  1387. // Add the target to the actor _aggroList or update hate if already present
  1388. me.addDamageHate(target, 0, aggro);
  1389. // Set the actor AI Intention to AI_INTENTION_ATTACK
  1390. if (getIntention() != CtrlIntention.AI_INTENTION_ATTACK)
  1391. {
  1392. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  1393. if (!_actor.isRunning())
  1394. _actor.setRunning();
  1395. setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
  1396. }
  1397. }
  1398. }
  1399. @Override
  1400. protected void onIntentionActive()
  1401. {
  1402. // Cancel attack timeout
  1403. _attackTimeout = Integer.MAX_VALUE;
  1404. super.onIntentionActive();
  1405. }
  1406. public void setGlobalAggro(int value)
  1407. {
  1408. _globalAggro = value;
  1409. }
  1410. }