L2AttackableAI.java 55 KB

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