L2CharacterAI.java 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.ai;
  20. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
  21. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  22. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_CAST;
  23. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_FOLLOW;
  24. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  25. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_INTERACT;
  26. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_MOVE_TO;
  27. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_PICK_UP;
  28. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_REST;
  29. import java.util.List;
  30. import javolution.util.FastList;
  31. import com.l2jserver.Config;
  32. import com.l2jserver.gameserver.GameTimeController;
  33. import com.l2jserver.gameserver.GeoData;
  34. import com.l2jserver.gameserver.ThreadPoolManager;
  35. import com.l2jserver.gameserver.enums.ItemLocation;
  36. import com.l2jserver.gameserver.enums.QuestEventType;
  37. import com.l2jserver.gameserver.instancemanager.WalkingManager;
  38. import com.l2jserver.gameserver.model.L2Object;
  39. import com.l2jserver.gameserver.model.Location;
  40. import com.l2jserver.gameserver.model.actor.L2Attackable;
  41. import com.l2jserver.gameserver.model.actor.L2Character;
  42. import com.l2jserver.gameserver.model.actor.L2Npc;
  43. import com.l2jserver.gameserver.model.actor.L2Playable;
  44. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  45. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  46. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  47. import com.l2jserver.gameserver.model.effects.L2EffectType;
  48. import com.l2jserver.gameserver.model.interfaces.ILocational;
  49. import com.l2jserver.gameserver.model.items.L2Weapon;
  50. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  51. import com.l2jserver.gameserver.model.items.type.WeaponType;
  52. import com.l2jserver.gameserver.model.quest.Quest;
  53. import com.l2jserver.gameserver.model.skills.Skill;
  54. import com.l2jserver.gameserver.model.skills.targets.L2TargetType;
  55. import com.l2jserver.gameserver.network.SystemMessageId;
  56. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  57. import com.l2jserver.gameserver.network.serverpackets.AutoAttackStop;
  58. import com.l2jserver.gameserver.taskmanager.AttackStanceTaskManager;
  59. import com.l2jserver.util.Rnd;
  60. /**
  61. * This class manages AI of L2Character.<br>
  62. * L2CharacterAI :
  63. * <ul>
  64. * <li>L2AttackableAI</li>
  65. * <li>L2DoorAI</li>
  66. * <li>L2PlayerAI</li>
  67. * <li>L2SummonAI</li>
  68. * </ul>
  69. */
  70. public class L2CharacterAI extends AbstractAI
  71. {
  72. public static class IntentionCommand
  73. {
  74. protected final CtrlIntention _crtlIntention;
  75. protected final Object _arg0, _arg1;
  76. protected IntentionCommand(CtrlIntention pIntention, Object pArg0, Object pArg1)
  77. {
  78. _crtlIntention = pIntention;
  79. _arg0 = pArg0;
  80. _arg1 = pArg1;
  81. }
  82. public CtrlIntention getCtrlIntention()
  83. {
  84. return _crtlIntention;
  85. }
  86. }
  87. /**
  88. * Cast Task
  89. * @author Zoey76
  90. */
  91. public static class CastTask implements Runnable
  92. {
  93. private final L2Character _activeChar;
  94. private final L2Object _target;
  95. private final Skill _skill;
  96. public CastTask(L2Character actor, Skill skill, L2Object target)
  97. {
  98. _activeChar = actor;
  99. _target = target;
  100. _skill = skill;
  101. }
  102. @Override
  103. public void run()
  104. {
  105. if (_activeChar.isAttackingNow())
  106. {
  107. _activeChar.abortAttack();
  108. }
  109. _activeChar.getAI().changeIntentionToCast(_skill, _target);
  110. }
  111. }
  112. /**
  113. * Constructor of L2CharacterAI.
  114. * @param accessor The AI accessor of the L2Character
  115. */
  116. public L2CharacterAI(L2Character.AIAccessor accessor)
  117. {
  118. super(accessor);
  119. }
  120. public IntentionCommand getNextIntention()
  121. {
  122. return null;
  123. }
  124. @Override
  125. protected void onEvtAttacked(L2Character attacker)
  126. {
  127. if ((attacker instanceof L2Attackable) && !((L2Attackable) attacker).isCoreAIDisabled())
  128. {
  129. clientStartAutoAttack();
  130. }
  131. }
  132. /**
  133. * Manage the Idle Intention : Stop Attack, Movement and Stand Up the actor.<br>
  134. * <B><U> Actions</U> :</B>
  135. * <ul>
  136. * <li>Set the AI Intention to AI_INTENTION_IDLE</li>
  137. * <li>Init cast and attack target</li>
  138. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  139. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  140. * <li>Stand up the actor server side AND client side by sending Server->Client packet ChangeWaitType (broadcast)</li>
  141. * </ul>
  142. */
  143. @Override
  144. protected void onIntentionIdle()
  145. {
  146. // Set the AI Intention to AI_INTENTION_IDLE
  147. changeIntention(AI_INTENTION_IDLE, null, null);
  148. // Init cast and attack target
  149. setCastTarget(null);
  150. setAttackTarget(null);
  151. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  152. clientStopMoving(null);
  153. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  154. clientStopAutoAttack();
  155. }
  156. /**
  157. * Manage the Active Intention : Stop Attack, Movement and Launch Think Event.<br>
  158. * <B><U> Actions</U> : <I>if the Intention is not already Active</I></B>
  159. * <ul>
  160. * <li>Set the AI Intention to AI_INTENTION_ACTIVE</li>
  161. * <li>Init cast and attack target</li>
  162. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  163. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  164. * <li>Launch the Think Event</li>
  165. * </ul>
  166. */
  167. @Override
  168. protected void onIntentionActive()
  169. {
  170. // Check if the Intention is not already Active
  171. if (getIntention() != AI_INTENTION_ACTIVE)
  172. {
  173. // Set the AI Intention to AI_INTENTION_ACTIVE
  174. changeIntention(AI_INTENTION_ACTIVE, null, null);
  175. // Init cast and attack target
  176. setCastTarget(null);
  177. setAttackTarget(null);
  178. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  179. clientStopMoving(null);
  180. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  181. clientStopAutoAttack();
  182. // Also enable random animations for this L2Character if allowed
  183. // This is only for mobs - town npcs are handled in their constructor
  184. if (_actor instanceof L2Attackable)
  185. {
  186. ((L2Npc) _actor).startRandomAnimationTimer();
  187. }
  188. // Launch the Think Event
  189. onEvtThink();
  190. }
  191. }
  192. /**
  193. * Manage the Rest Intention.<br>
  194. * <B><U> Actions</U> : </B>
  195. * <ul>
  196. * <li>Set the AI Intention to AI_INTENTION_IDLE</li>
  197. * </ul>
  198. */
  199. @Override
  200. protected void onIntentionRest()
  201. {
  202. // Set the AI Intention to AI_INTENTION_IDLE
  203. setIntention(AI_INTENTION_IDLE);
  204. }
  205. /**
  206. * Manage the Attack Intention : Stop current Attack (if necessary), Start a new Attack and Launch Think Event.<br>
  207. * <B><U> Actions</U> : </B>
  208. * <ul>
  209. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  210. * <li>Set the Intention of this AI to AI_INTENTION_ATTACK</li>
  211. * <li>Set or change the AI attack target</li>
  212. * <li>Start the actor Auto Attack client side by sending Server->Client packet AutoAttackStart (broadcast)</li>
  213. * <li>Launch the Think Event</li>
  214. * </ul>
  215. * <B><U> Overridden in</U> :</B>
  216. * <ul>
  217. * <li>L2AttackableAI : Calculate attack timeout</li>
  218. * </ul>
  219. */
  220. @Override
  221. protected void onIntentionAttack(L2Character target)
  222. {
  223. if (target == null)
  224. {
  225. clientActionFailed();
  226. return;
  227. }
  228. if (getIntention() == AI_INTENTION_REST)
  229. {
  230. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  231. clientActionFailed();
  232. return;
  233. }
  234. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow() || _actor.isAfraid())
  235. {
  236. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  237. clientActionFailed();
  238. return;
  239. }
  240. // Check if the Intention is already AI_INTENTION_ATTACK
  241. if (getIntention() == AI_INTENTION_ATTACK)
  242. {
  243. // Check if the AI already targets the L2Character
  244. if (getAttackTarget() != target)
  245. {
  246. // Set the AI attack target (change target)
  247. setAttackTarget(target);
  248. stopFollow();
  249. // Launch the Think Event
  250. notifyEvent(CtrlEvent.EVT_THINK, null);
  251. }
  252. else
  253. {
  254. clientActionFailed(); // else client freezes until cancel target
  255. }
  256. }
  257. else
  258. {
  259. // Set the Intention of this AbstractAI to AI_INTENTION_ATTACK
  260. changeIntention(AI_INTENTION_ATTACK, target, null);
  261. // Set the AI attack target
  262. setAttackTarget(target);
  263. stopFollow();
  264. // Launch the Think Event
  265. notifyEvent(CtrlEvent.EVT_THINK, null);
  266. }
  267. }
  268. /**
  269. * Manage the Cast Intention : Stop current Attack, Init the AI in order to cast and Launch Think Event.<br>
  270. * <B><U> Actions</U> : </B>
  271. * <ul>
  272. * <li>Set the AI cast target</li>
  273. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  274. * <li>Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor</li>
  275. * <li>Set the AI skill used by INTENTION_CAST</li>
  276. * <li>Set the Intention of this AI to AI_INTENTION_CAST</li>
  277. * <li>Launch the Think Event</li>
  278. * </ul>
  279. */
  280. @Override
  281. protected void onIntentionCast(Skill skill, L2Object target)
  282. {
  283. if ((getIntention() == AI_INTENTION_REST) && skill.isMagic())
  284. {
  285. clientActionFailed();
  286. _actor.setIsCastingNow(false);
  287. return;
  288. }
  289. if (_actor.getBowAttackEndTime() > GameTimeController.getInstance().getGameTicks())
  290. {
  291. ThreadPoolManager.getInstance().scheduleGeneral(new CastTask(_actor, skill, target), (_actor.getBowAttackEndTime() - GameTimeController.getInstance().getGameTicks()) * GameTimeController.MILLIS_IN_TICK);
  292. }
  293. else
  294. {
  295. changeIntentionToCast(skill, target);
  296. }
  297. }
  298. protected void changeIntentionToCast(Skill skill, L2Object target)
  299. {
  300. // Set the AI cast target
  301. setCastTarget((L2Character) target);
  302. // Set the AI skill used by INTENTION_CAST
  303. _skill = skill;
  304. // Change the Intention of this AbstractAI to AI_INTENTION_CAST
  305. changeIntention(AI_INTENTION_CAST, skill, target);
  306. // Launch the Think Event
  307. notifyEvent(CtrlEvent.EVT_THINK, null);
  308. }
  309. /**
  310. * Manage the Move To Intention : Stop current Attack and Launch a Move to Location Task.<br>
  311. * <B><U> Actions</U> : </B>
  312. * <ul>
  313. * <li>Stop the actor auto-attack server side AND client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  314. * <li>Set the Intention of this AI to AI_INTENTION_MOVE_TO</li>
  315. * <li>Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)</li>
  316. * </ul>
  317. */
  318. @Override
  319. protected void onIntentionMoveTo(Location loc)
  320. {
  321. if (getIntention() == AI_INTENTION_REST)
  322. {
  323. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  324. clientActionFailed();
  325. return;
  326. }
  327. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  328. {
  329. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  330. clientActionFailed();
  331. return;
  332. }
  333. // Set the Intention of this AbstractAI to AI_INTENTION_MOVE_TO
  334. changeIntention(AI_INTENTION_MOVE_TO, loc, null);
  335. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  336. clientStopAutoAttack();
  337. // Abort the attack of the L2Character and send Server->Client ActionFailed packet
  338. _actor.abortAttack();
  339. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  340. moveTo(loc.getX(), loc.getY(), loc.getZ());
  341. }
  342. /**
  343. * Manage the Follow Intention : Stop current Attack and Launch a Follow Task.<br>
  344. * <B><U> Actions</U> : </B>
  345. * <ul>
  346. * <li>Stop the actor auto-attack server side AND client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  347. * <li>Set the Intention of this AI to AI_INTENTION_FOLLOW</li>
  348. * <li>Create and Launch an AI Follow Task to execute every 1s</li>
  349. * </ul>
  350. */
  351. @Override
  352. protected void onIntentionFollow(L2Character target)
  353. {
  354. if (getIntention() == AI_INTENTION_REST)
  355. {
  356. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  357. clientActionFailed();
  358. return;
  359. }
  360. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  361. {
  362. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  363. clientActionFailed();
  364. return;
  365. }
  366. if (_actor.isMovementDisabled())
  367. {
  368. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  369. clientActionFailed();
  370. return;
  371. }
  372. // Dead actors can`t follow
  373. if (_actor.isDead())
  374. {
  375. clientActionFailed();
  376. return;
  377. }
  378. // do not follow yourself
  379. if (_actor == target)
  380. {
  381. clientActionFailed();
  382. return;
  383. }
  384. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  385. clientStopAutoAttack();
  386. // Set the Intention of this AbstractAI to AI_INTENTION_FOLLOW
  387. changeIntention(AI_INTENTION_FOLLOW, target, null);
  388. // Create and Launch an AI Follow Task to execute every 1s
  389. startFollow(target);
  390. }
  391. /**
  392. * Manage the PickUp Intention : Set the pick up target and Launch a Move To Pawn Task (offset=20).<br>
  393. * <B><U> Actions</U> : </B>
  394. * <ul>
  395. * <li>Set the AI pick up target</li>
  396. * <li>Set the Intention of this AI to AI_INTENTION_PICK_UP</li>
  397. * <li>Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)</li>
  398. * </ul>
  399. */
  400. @Override
  401. protected void onIntentionPickUp(L2Object object)
  402. {
  403. if (getIntention() == AI_INTENTION_REST)
  404. {
  405. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  406. clientActionFailed();
  407. return;
  408. }
  409. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  410. {
  411. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  412. clientActionFailed();
  413. return;
  414. }
  415. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  416. clientStopAutoAttack();
  417. if ((object instanceof L2ItemInstance) && (((L2ItemInstance) object).getItemLocation() != ItemLocation.VOID))
  418. {
  419. return;
  420. }
  421. // Set the Intention of this AbstractAI to AI_INTENTION_PICK_UP
  422. changeIntention(AI_INTENTION_PICK_UP, object, null);
  423. // Set the AI pick up target
  424. setTarget(object);
  425. if ((object.getX() == 0) && (object.getY() == 0)) // TODO: Find the drop&spawn bug
  426. {
  427. _log.warning("Object in coords 0,0 - using a temporary fix");
  428. object.setXYZ(getActor().getX(), getActor().getY(), getActor().getZ() + 5);
  429. }
  430. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  431. moveToPawn(object, 20);
  432. }
  433. /**
  434. * Manage the Interact Intention : Set the interact target and Launch a Move To Pawn Task (offset=60).<br>
  435. * <B><U> Actions</U> : </B>
  436. * <ul>
  437. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  438. * <li>Set the AI interact target</li>
  439. * <li>Set the Intention of this AI to AI_INTENTION_INTERACT</li>
  440. * <li>Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)</li>
  441. * </ul>
  442. */
  443. @Override
  444. protected void onIntentionInteract(L2Object object)
  445. {
  446. if (getIntention() == AI_INTENTION_REST)
  447. {
  448. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  449. clientActionFailed();
  450. return;
  451. }
  452. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  453. {
  454. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  455. clientActionFailed();
  456. return;
  457. }
  458. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  459. clientStopAutoAttack();
  460. if (getIntention() != AI_INTENTION_INTERACT)
  461. {
  462. // Set the Intention of this AbstractAI to AI_INTENTION_INTERACT
  463. changeIntention(AI_INTENTION_INTERACT, object, null);
  464. // Set the AI interact target
  465. setTarget(object);
  466. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  467. moveToPawn(object, 60);
  468. }
  469. }
  470. /**
  471. * Do nothing.
  472. */
  473. @Override
  474. protected void onEvtThink()
  475. {
  476. // do nothing
  477. }
  478. /**
  479. * Do nothing.
  480. */
  481. @Override
  482. protected void onEvtAggression(L2Character target, int aggro)
  483. {
  484. // do nothing
  485. }
  486. /**
  487. * Launch actions corresponding to the Event Stunned then onAttacked Event.<br>
  488. * <B><U> Actions</U> :</B>
  489. * <ul>
  490. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  491. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  492. * <li>Break an attack and send Server->Client ActionFailed packet and a System Message to the L2Character</li>
  493. * <li>Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character</li>
  494. * <li>Launch actions corresponding to the Event onAttacked (only for L2AttackableAI after the stunning periode)</li>
  495. * </ul>
  496. */
  497. @Override
  498. protected void onEvtStunned(L2Character attacker)
  499. {
  500. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  501. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  502. if (AttackStanceTaskManager.getInstance().hasAttackStanceTask(_actor))
  503. {
  504. AttackStanceTaskManager.getInstance().removeAttackStanceTask(_actor);
  505. }
  506. // Stop Server AutoAttack also
  507. setAutoAttacking(false);
  508. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  509. clientStopMoving(null);
  510. // Launch actions corresponding to the Event onAttacked (only for L2AttackableAI after the stunning periode)
  511. onEvtAttacked(attacker);
  512. }
  513. @Override
  514. protected void onEvtParalyzed(L2Character attacker)
  515. {
  516. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  517. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  518. if (AttackStanceTaskManager.getInstance().hasAttackStanceTask(_actor))
  519. {
  520. AttackStanceTaskManager.getInstance().removeAttackStanceTask(_actor);
  521. }
  522. // Stop Server AutoAttack also
  523. setAutoAttacking(false);
  524. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  525. clientStopMoving(null);
  526. // Launch actions corresponding to the Event onAttacked (only for L2AttackableAI after the stunning periode)
  527. onEvtAttacked(attacker);
  528. }
  529. /**
  530. * Launch actions corresponding to the Event Sleeping.<br>
  531. * <B><U> Actions</U> :</B>
  532. * <ul>
  533. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  534. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  535. * <li>Break an attack and send Server->Client ActionFailed packet and a System Message to the L2Character</li>
  536. * <li>Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character</li>
  537. * </ul>
  538. */
  539. @Override
  540. protected void onEvtSleeping(L2Character attacker)
  541. {
  542. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  543. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  544. if (AttackStanceTaskManager.getInstance().hasAttackStanceTask(_actor))
  545. {
  546. AttackStanceTaskManager.getInstance().removeAttackStanceTask(_actor);
  547. }
  548. // stop Server AutoAttack also
  549. setAutoAttacking(false);
  550. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  551. clientStopMoving(null);
  552. }
  553. /**
  554. * Launch actions corresponding to the Event Rooted.<br>
  555. * <B><U> Actions</U> :</B>
  556. * <ul>
  557. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  558. * <li>Launch actions corresponding to the Event onAttacked</li>
  559. * </ul>
  560. */
  561. @Override
  562. protected void onEvtRooted(L2Character attacker)
  563. {
  564. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  565. // _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  566. // if (AttackStanceTaskManager.getInstance().hasAttackStanceTask(_actor))
  567. // AttackStanceTaskManager.getInstance().removeAttackStanceTask(_actor);
  568. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  569. clientStopMoving(null);
  570. // Launch actions corresponding to the Event onAttacked
  571. onEvtAttacked(attacker);
  572. }
  573. /**
  574. * Launch actions corresponding to the Event Confused.<br>
  575. * <B><U> Actions</U> :</B>
  576. * <ul>
  577. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  578. * <li>Launch actions corresponding to the Event onAttacked</li>
  579. * </ul>
  580. */
  581. @Override
  582. protected void onEvtConfused(L2Character attacker)
  583. {
  584. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  585. clientStopMoving(null);
  586. // Launch actions corresponding to the Event onAttacked
  587. onEvtAttacked(attacker);
  588. }
  589. /**
  590. * Launch actions corresponding to the Event Muted.<br>
  591. * <B><U> Actions</U> :</B>
  592. * <ul>
  593. * <li>Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character</li>
  594. * </ul>
  595. */
  596. @Override
  597. protected void onEvtMuted(L2Character attacker)
  598. {
  599. // Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character
  600. onEvtAttacked(attacker);
  601. }
  602. /**
  603. * Do nothing.
  604. */
  605. @Override
  606. protected void onEvtEvaded(L2Character attacker)
  607. {
  608. // do nothing
  609. }
  610. /**
  611. * Launch actions corresponding to the Event ReadyToAct.<br>
  612. * <B><U> Actions</U> :</B>
  613. * <ul>
  614. * <li>Launch actions corresponding to the Event Think</li>
  615. * </ul>
  616. */
  617. @Override
  618. protected void onEvtReadyToAct()
  619. {
  620. // Launch actions corresponding to the Event Think
  621. onEvtThink();
  622. }
  623. /**
  624. * Do nothing.
  625. */
  626. @Override
  627. protected void onEvtUserCmd(Object arg0, Object arg1)
  628. {
  629. // do nothing
  630. }
  631. /**
  632. * Launch actions corresponding to the Event Arrived.<br>
  633. * <B><U> Actions</U> :</B>
  634. * <ul>
  635. * <li>If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE</li>
  636. * <li>Launch actions corresponding to the Event Think</li>
  637. * </ul>
  638. */
  639. @Override
  640. protected void onEvtArrived()
  641. {
  642. _accessor.getActor().revalidateZone(true);
  643. if (_accessor.getActor().moveToNextRoutePoint())
  644. {
  645. return;
  646. }
  647. if (_accessor.getActor() instanceof L2Attackable)
  648. {
  649. ((L2Attackable) _accessor.getActor()).setisReturningToSpawnPoint(false);
  650. }
  651. clientStoppedMoving();
  652. if (_actor instanceof L2Npc)
  653. {
  654. L2Npc npc = (L2Npc) _actor;
  655. WalkingManager.getInstance().onArrived(npc); // Walking Manager support
  656. // Notify quest
  657. if (npc.getTemplate().getEventQuests(QuestEventType.ON_MOVE_FINISHED) != null)
  658. {
  659. for (Quest quest : npc.getTemplate().getEventQuests(QuestEventType.ON_MOVE_FINISHED))
  660. {
  661. quest.notifyMoveFinished(npc);
  662. }
  663. }
  664. }
  665. // If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE
  666. if (getIntention() == AI_INTENTION_MOVE_TO)
  667. {
  668. setIntention(AI_INTENTION_ACTIVE);
  669. }
  670. // Launch actions corresponding to the Event Think
  671. onEvtThink();
  672. }
  673. /**
  674. * Launch actions corresponding to the Event ArrivedRevalidate.<br>
  675. * <B><U> Actions</U> :</B>
  676. * <ul>
  677. * <li>Launch actions corresponding to the Event Think</li>
  678. * </ul>
  679. */
  680. @Override
  681. protected void onEvtArrivedRevalidate()
  682. {
  683. // Launch actions corresponding to the Event Think
  684. onEvtThink();
  685. }
  686. /**
  687. * Launch actions corresponding to the Event ArrivedBlocked.<br>
  688. * <B><U> Actions</U> :</B>
  689. * <ul>
  690. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  691. * <li>If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE</li>
  692. * <li>Launch actions corresponding to the Event Think</li>
  693. * </ul>
  694. */
  695. @Override
  696. protected void onEvtArrivedBlocked(Location blocked_at_loc)
  697. {
  698. // If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE
  699. if ((getIntention() == AI_INTENTION_MOVE_TO) || (getIntention() == AI_INTENTION_CAST))
  700. {
  701. setIntention(AI_INTENTION_ACTIVE);
  702. }
  703. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  704. clientStopMoving(blocked_at_loc);
  705. // Launch actions corresponding to the Event Think
  706. onEvtThink();
  707. }
  708. /**
  709. * Launch actions corresponding to the Event ForgetObject.<br>
  710. * <B><U> Actions</U> :</B>
  711. * <ul>
  712. * <li>If the object was targeted and the Intention was AI_INTENTION_INTERACT or AI_INTENTION_PICK_UP, set the Intention to AI_INTENTION_ACTIVE</li>
  713. * <li>If the object was targeted to attack, stop the auto-attack, cancel target and set the Intention to AI_INTENTION_ACTIVE</li>
  714. * <li>If the object was targeted to cast, cancel target and set the Intention to AI_INTENTION_ACTIVE</li>
  715. * <li>If the object was targeted to follow, stop the movement, cancel AI Follow Task and set the Intention to AI_INTENTION_ACTIVE</li>
  716. * <li>If the targeted object was the actor , cancel AI target, stop AI Follow Task, stop the movement and set the Intention to AI_INTENTION_IDLE</li>
  717. * </ul>
  718. */
  719. @Override
  720. protected void onEvtForgetObject(L2Object object)
  721. {
  722. // If the object was targeted and the Intention was AI_INTENTION_INTERACT or AI_INTENTION_PICK_UP, set the Intention to AI_INTENTION_ACTIVE
  723. if (getTarget() == object)
  724. {
  725. setTarget(null);
  726. if ((getIntention() == AI_INTENTION_INTERACT) || (getIntention() == AI_INTENTION_PICK_UP))
  727. {
  728. setIntention(AI_INTENTION_ACTIVE);
  729. }
  730. }
  731. // Check if the object was targeted to attack
  732. if (getAttackTarget() == object)
  733. {
  734. // Cancel attack target
  735. setAttackTarget(null);
  736. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  737. setIntention(AI_INTENTION_ACTIVE);
  738. }
  739. // Check if the object was targeted to cast
  740. if (getCastTarget() == object)
  741. {
  742. // Cancel cast target
  743. setCastTarget(null);
  744. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  745. setIntention(AI_INTENTION_ACTIVE);
  746. }
  747. // Check if the object was targeted to follow
  748. if (getFollowTarget() == object)
  749. {
  750. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  751. clientStopMoving(null);
  752. // Stop an AI Follow Task
  753. stopFollow();
  754. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  755. setIntention(AI_INTENTION_ACTIVE);
  756. }
  757. // Check if the targeted object was the actor
  758. if (_actor == object)
  759. {
  760. // Cancel AI target
  761. setTarget(null);
  762. setAttackTarget(null);
  763. setCastTarget(null);
  764. // Stop an AI Follow Task
  765. stopFollow();
  766. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  767. clientStopMoving(null);
  768. // Set the Intention of this AbstractAI to AI_INTENTION_IDLE
  769. changeIntention(AI_INTENTION_IDLE, null, null);
  770. }
  771. }
  772. /**
  773. * Launch actions corresponding to the Event Cancel.<br>
  774. * <B><U> Actions</U> :</B>
  775. * <ul>
  776. * <li>Stop an AI Follow Task</li>
  777. * <li>Launch actions corresponding to the Event Think</li>
  778. * </ul>
  779. */
  780. @Override
  781. protected void onEvtCancel()
  782. {
  783. _actor.abortCast();
  784. // Stop an AI Follow Task
  785. stopFollow();
  786. if (!AttackStanceTaskManager.getInstance().hasAttackStanceTask(_actor))
  787. {
  788. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  789. }
  790. // Launch actions corresponding to the Event Think
  791. onEvtThink();
  792. }
  793. /**
  794. * Launch actions corresponding to the Event Dead.<br>
  795. * <B><U> Actions</U> :</B>
  796. * <ul>
  797. * <li>Stop an AI Follow Task</li>
  798. * <li>Kill the actor client side by sending Server->Client packet AutoAttackStop, StopMove/StopRotation, Die (broadcast)</li>
  799. * </ul>
  800. */
  801. @Override
  802. protected void onEvtDead()
  803. {
  804. // Stop an AI Tasks
  805. stopAITask();
  806. // Kill the actor client side by sending Server->Client packet AutoAttackStop, StopMove/StopRotation, Die (broadcast)
  807. clientNotifyDead();
  808. if (!(_actor instanceof L2Playable))
  809. {
  810. _actor.setWalking();
  811. }
  812. }
  813. /**
  814. * Launch actions corresponding to the Event Fake Death.<br>
  815. * <B><U> Actions</U> :</B>
  816. * <ul>
  817. * <li>Stop an AI Follow Task</li>
  818. * </ul>
  819. */
  820. @Override
  821. protected void onEvtFakeDeath()
  822. {
  823. // Stop an AI Follow Task
  824. stopFollow();
  825. // Stop the actor movement and send Server->Client packet StopMove/StopRotation (broadcast)
  826. clientStopMoving(null);
  827. // Init AI
  828. _intention = AI_INTENTION_IDLE;
  829. setTarget(null);
  830. setCastTarget(null);
  831. setAttackTarget(null);
  832. }
  833. /**
  834. * Do nothing.
  835. */
  836. @Override
  837. protected void onEvtFinishCasting()
  838. {
  839. // do nothing
  840. }
  841. protected boolean maybeMoveToPosition(ILocational worldPosition, int offset)
  842. {
  843. if (worldPosition == null)
  844. {
  845. _log.warning("maybeMoveToPosition: worldPosition == NULL!");
  846. return false;
  847. }
  848. if (offset < 0)
  849. {
  850. return false; // skill radius -1
  851. }
  852. if (!_actor.isInsideRadius(worldPosition, offset + _actor.getTemplate().getCollisionRadius(), false, false))
  853. {
  854. if (_actor.isMovementDisabled())
  855. {
  856. return true;
  857. }
  858. if (!_actor.isRunning() && !(this instanceof L2PlayerAI) && !(this instanceof L2SummonAI))
  859. {
  860. _actor.setRunning();
  861. }
  862. stopFollow();
  863. int x = _actor.getX();
  864. int y = _actor.getY();
  865. double dx = worldPosition.getX() - x;
  866. double dy = worldPosition.getY() - y;
  867. double dist = Math.sqrt((dx * dx) + (dy * dy));
  868. double sin = dy / dist;
  869. double cos = dx / dist;
  870. dist -= offset - 5;
  871. x += (int) (dist * cos);
  872. y += (int) (dist * sin);
  873. moveTo(x, y, worldPosition.getZ());
  874. return true;
  875. }
  876. if (getFollowTarget() != null)
  877. {
  878. stopFollow();
  879. }
  880. return false;
  881. }
  882. /**
  883. * Manage the Move to Pawn action in function of the distance and of the Interact area.<br>
  884. * <B><U> Actions</U> :</B>
  885. * <ul>
  886. * <li>Get the distance between the current position of the L2Character and the target (x,y)</li>
  887. * <li>If the distance > offset+20, move the actor (by running) to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)</li>
  888. * <li>If the distance <= offset+20, Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  889. * </ul>
  890. * <B><U> Example of use </U> :</B>
  891. * <ul>
  892. * <li>L2PLayerAI, L2SummonAI</li>
  893. * </ul>
  894. * @param target The targeted L2Object
  895. * @param offset The Interact area radius
  896. * @return True if a movement must be done
  897. */
  898. protected boolean maybeMoveToPawn(L2Object target, int offset)
  899. {
  900. // Get the distance between the current position of the L2Character and the target (x,y)
  901. if (target == null)
  902. {
  903. _log.warning("maybeMoveToPawn: target == NULL!");
  904. return false;
  905. }
  906. if (offset < 0)
  907. {
  908. return false; // skill radius -1
  909. }
  910. offset += _actor.getTemplate().getCollisionRadius();
  911. if (target instanceof L2Character)
  912. {
  913. offset += ((L2Character) target).getTemplate().getCollisionRadius();
  914. }
  915. if (!_actor.isInsideRadius(target, offset, false, false))
  916. {
  917. // Caller should be L2Playable and thinkAttack/thinkCast/thinkInteract/thinkPickUp
  918. if (getFollowTarget() != null)
  919. {
  920. // allow larger hit range when the target is moving (check is run only once per second)
  921. if (!_actor.isInsideRadius(target, offset + 100, false, false))
  922. {
  923. return true;
  924. }
  925. stopFollow();
  926. return false;
  927. }
  928. if (_actor.isMovementDisabled())
  929. {
  930. // If player is trying attack target but he cannot move to attack target
  931. // change his intention to idle
  932. if (_actor.getAI().getIntention() == CtrlIntention.AI_INTENTION_ATTACK)
  933. {
  934. _actor.getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  935. }
  936. return true;
  937. }
  938. // while flying there is no move to cast
  939. if ((_actor.getAI().getIntention() == CtrlIntention.AI_INTENTION_CAST) && (_actor instanceof L2PcInstance) && ((L2PcInstance) _actor).isTransformed())
  940. {
  941. if (!((L2PcInstance) _actor).getTransformation().isCombat())
  942. {
  943. _actor.sendPacket(SystemMessageId.DIST_TOO_FAR_CASTING_STOPPED);
  944. _actor.sendPacket(ActionFailed.STATIC_PACKET);
  945. return true;
  946. }
  947. }
  948. // If not running, set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  949. if (!_actor.isRunning() && !(this instanceof L2PlayerAI) && !(this instanceof L2SummonAI))
  950. {
  951. _actor.setRunning();
  952. }
  953. stopFollow();
  954. if ((target instanceof L2Character) && !(target instanceof L2DoorInstance))
  955. {
  956. if (((L2Character) target).isMoving())
  957. {
  958. offset -= 100;
  959. }
  960. if (offset < 5)
  961. {
  962. offset = 5;
  963. }
  964. startFollow((L2Character) target, offset);
  965. }
  966. else
  967. {
  968. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  969. moveToPawn(target, offset);
  970. }
  971. return true;
  972. }
  973. if (getFollowTarget() != null)
  974. {
  975. stopFollow();
  976. }
  977. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  978. // clientStopMoving(null);
  979. return false;
  980. }
  981. /**
  982. * Modify current Intention and actions if the target is lost or dead.<br>
  983. * <B><U> Actions</U> : <I>If the target is lost or dead</I></B>
  984. * <ul>
  985. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  986. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  987. * <li>Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE</li>
  988. * </ul>
  989. * <B><U> Example of use </U> :</B>
  990. * <ul>
  991. * <li>L2PLayerAI, L2SummonAI</li>
  992. * </ul>
  993. * @param target The targeted L2Object
  994. * @return True if the target is lost or dead (false if fakedeath)
  995. */
  996. protected boolean checkTargetLostOrDead(L2Character target)
  997. {
  998. if ((target == null) || target.isAlikeDead())
  999. {
  1000. // check if player is fakedeath
  1001. if ((target instanceof L2PcInstance) && ((L2PcInstance) target).isFakeDeath())
  1002. {
  1003. target.stopFakeDeath(true);
  1004. return false;
  1005. }
  1006. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  1007. setIntention(AI_INTENTION_ACTIVE);
  1008. return true;
  1009. }
  1010. return false;
  1011. }
  1012. /**
  1013. * Modify current Intention and actions if the target is lost.<br>
  1014. * <B><U> Actions</U> : <I>If the target is lost</I></B>
  1015. * <ul>
  1016. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  1017. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  1018. * <li>Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE</li>
  1019. * </ul>
  1020. * <B><U> Example of use </U> :</B>
  1021. * <ul>
  1022. * <li>L2PLayerAI, L2SummonAI</li>
  1023. * </ul>
  1024. * @param target The targeted L2Object
  1025. * @return True if the target is lost
  1026. */
  1027. protected boolean checkTargetLost(L2Object target)
  1028. {
  1029. // check if player is fakedeath
  1030. if (target instanceof L2PcInstance)
  1031. {
  1032. L2PcInstance target2 = (L2PcInstance) target; // convert object to chara
  1033. if (target2.isFakeDeath())
  1034. {
  1035. target2.stopFakeDeath(true);
  1036. return false;
  1037. }
  1038. }
  1039. if (target == null)
  1040. {
  1041. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  1042. setIntention(AI_INTENTION_ACTIVE);
  1043. return true;
  1044. }
  1045. if ((_actor != null) && (_skill != null) && _skill.isBad() && (_skill.getAffectRange() > 0) && (Config.GEODATA > 0) && !GeoData.getInstance().canSeeTarget(_actor, target))
  1046. {
  1047. setIntention(AI_INTENTION_ACTIVE);
  1048. return true;
  1049. }
  1050. return false;
  1051. }
  1052. protected class SelfAnalysis
  1053. {
  1054. public boolean isMage = false;
  1055. public boolean isBalanced;
  1056. public boolean isArcher = false;
  1057. public boolean isHealer = false;
  1058. public boolean isFighter = false;
  1059. public boolean cannotMoveOnLand = false;
  1060. public List<Skill> generalSkills = new FastList<>();
  1061. public List<Skill> buffSkills = new FastList<>();
  1062. public int lastBuffTick = 0;
  1063. public List<Skill> debuffSkills = new FastList<>();
  1064. public int lastDebuffTick = 0;
  1065. public List<Skill> cancelSkills = new FastList<>();
  1066. public List<Skill> healSkills = new FastList<>();
  1067. // public List<L2Skill> trickSkills = new FastList<>();
  1068. public List<Skill> generalDisablers = new FastList<>();
  1069. public List<Skill> sleepSkills = new FastList<>();
  1070. public List<Skill> rootSkills = new FastList<>();
  1071. public List<Skill> muteSkills = new FastList<>();
  1072. public List<Skill> resurrectSkills = new FastList<>();
  1073. public boolean hasHealOrResurrect = false;
  1074. public boolean hasLongRangeSkills = false;
  1075. public boolean hasLongRangeDamageSkills = false;
  1076. public int maxCastRange = 0;
  1077. public SelfAnalysis()
  1078. {
  1079. }
  1080. public void init()
  1081. {
  1082. switch (((L2NpcTemplate) _actor.getTemplate()).getAIType())
  1083. {
  1084. case FIGHTER:
  1085. isFighter = true;
  1086. break;
  1087. case MAGE:
  1088. isMage = true;
  1089. break;
  1090. case CORPSE:
  1091. case BALANCED:
  1092. isBalanced = true;
  1093. break;
  1094. case ARCHER:
  1095. isArcher = true;
  1096. break;
  1097. case HEALER:
  1098. isHealer = true;
  1099. break;
  1100. default:
  1101. isFighter = true;
  1102. break;
  1103. }
  1104. // water movement analysis
  1105. if (_actor.isNpc())
  1106. {
  1107. switch (_actor.getId())
  1108. {
  1109. case 20314: // great white shark
  1110. case 20849: // Light Worm
  1111. cannotMoveOnLand = true;
  1112. break;
  1113. default:
  1114. cannotMoveOnLand = false;
  1115. break;
  1116. }
  1117. }
  1118. // skill analysis
  1119. for (Skill sk : _actor.getAllSkills())
  1120. {
  1121. if (sk.isPassive())
  1122. {
  1123. continue;
  1124. }
  1125. int castRange = sk.getCastRange();
  1126. boolean hasLongRangeDamageSkill = false;
  1127. if (sk.isContinuous())
  1128. {
  1129. if (!sk.isDebuff())
  1130. {
  1131. buffSkills.add(sk);
  1132. }
  1133. else
  1134. {
  1135. debuffSkills.add(sk);
  1136. }
  1137. continue;
  1138. }
  1139. if (sk.hasEffectType(L2EffectType.DISPEL, L2EffectType.DISPEL_BY_SLOT))
  1140. {
  1141. cancelSkills.add(sk);
  1142. }
  1143. else if (sk.hasEffectType(L2EffectType.HEAL, L2EffectType.HEAL_PERCENT))
  1144. {
  1145. healSkills.add(sk);
  1146. hasHealOrResurrect = true;
  1147. }
  1148. else if (sk.hasEffectType(L2EffectType.SLEEP))
  1149. {
  1150. sleepSkills.add(sk);
  1151. }
  1152. else if (sk.hasEffectType(L2EffectType.STUN, L2EffectType.PARALYZE))
  1153. {
  1154. // hardcoding petrification until improvements are made to
  1155. // EffectTemplate... petrification is totally different for
  1156. // AI than paralyze
  1157. switch (sk.getId())
  1158. {
  1159. case 367:
  1160. case 4111:
  1161. case 4383:
  1162. case 4616:
  1163. case 4578:
  1164. sleepSkills.add(sk);
  1165. break;
  1166. default:
  1167. generalDisablers.add(sk);
  1168. break;
  1169. }
  1170. }
  1171. else if (sk.hasEffectType(L2EffectType.ROOT))
  1172. {
  1173. rootSkills.add(sk);
  1174. }
  1175. else if (sk.hasEffectType(L2EffectType.FEAR))
  1176. {
  1177. debuffSkills.add(sk);
  1178. }
  1179. else if (sk.hasEffectType(L2EffectType.MUTE))
  1180. {
  1181. muteSkills.add(sk);
  1182. }
  1183. else if (sk.hasEffectType(L2EffectType.RESURRECTION))
  1184. {
  1185. resurrectSkills.add(sk);
  1186. hasHealOrResurrect = true;
  1187. }
  1188. else
  1189. {
  1190. generalSkills.add(sk);
  1191. hasLongRangeDamageSkill = true;
  1192. }
  1193. if (castRange > 70)
  1194. {
  1195. hasLongRangeSkills = true;
  1196. if (hasLongRangeDamageSkill)
  1197. {
  1198. hasLongRangeDamageSkills = true;
  1199. }
  1200. }
  1201. if (castRange > maxCastRange)
  1202. {
  1203. maxCastRange = castRange;
  1204. }
  1205. }
  1206. // Because of missing skills, some mages/balanced cannot play like mages
  1207. if (!hasLongRangeDamageSkills && isMage)
  1208. {
  1209. isBalanced = true;
  1210. isMage = false;
  1211. isFighter = false;
  1212. }
  1213. if (!hasLongRangeSkills && (isMage || isBalanced))
  1214. {
  1215. isBalanced = false;
  1216. isMage = false;
  1217. isFighter = true;
  1218. }
  1219. if (generalSkills.isEmpty() && isMage)
  1220. {
  1221. isBalanced = true;
  1222. isMage = false;
  1223. }
  1224. }
  1225. }
  1226. protected class TargetAnalysis
  1227. {
  1228. public L2Character character;
  1229. public boolean isMage;
  1230. public boolean isBalanced;
  1231. public boolean isArcher;
  1232. public boolean isFighter;
  1233. public boolean isCanceled;
  1234. public boolean isSlower;
  1235. public boolean isMagicResistant;
  1236. public TargetAnalysis()
  1237. {
  1238. }
  1239. public void update(L2Character target)
  1240. {
  1241. // update status once in 4 seconds
  1242. if ((target == character) && (Rnd.nextInt(100) > 25))
  1243. {
  1244. return;
  1245. }
  1246. character = target;
  1247. if (target == null)
  1248. {
  1249. return;
  1250. }
  1251. isMage = false;
  1252. isBalanced = false;
  1253. isArcher = false;
  1254. isFighter = false;
  1255. isCanceled = false;
  1256. if (target.getMAtk(null, null) > (1.5 * target.getPAtk(null)))
  1257. {
  1258. isMage = true;
  1259. }
  1260. else if (((target.getPAtk(null) * 0.8) < target.getMAtk(null, null)) || ((target.getMAtk(null, null) * 0.8) > target.getPAtk(null)))
  1261. {
  1262. isBalanced = true;
  1263. }
  1264. else
  1265. {
  1266. L2Weapon weapon = target.getActiveWeaponItem();
  1267. if ((weapon != null) && ((weapon.getItemType() == WeaponType.BOW) || (weapon.getItemType() == WeaponType.CROSSBOW)))
  1268. {
  1269. isArcher = true;
  1270. }
  1271. else
  1272. {
  1273. isFighter = true;
  1274. }
  1275. }
  1276. if (target.getRunSpeed() < (_actor.getRunSpeed() - 3))
  1277. {
  1278. isSlower = true;
  1279. }
  1280. else
  1281. {
  1282. isSlower = false;
  1283. }
  1284. if ((target.getMDef(null, null) * 1.2) > _actor.getMAtk(null, null))
  1285. {
  1286. isMagicResistant = true;
  1287. }
  1288. else
  1289. {
  1290. isMagicResistant = false;
  1291. }
  1292. if (target.getBuffCount() < 4)
  1293. {
  1294. isCanceled = true;
  1295. }
  1296. }
  1297. }
  1298. public boolean canAura(Skill sk)
  1299. {
  1300. if ((sk.getTargetType() == L2TargetType.AURA) || (sk.getTargetType() == L2TargetType.BEHIND_AURA) || (sk.getTargetType() == L2TargetType.FRONT_AURA) || (sk.getTargetType() == L2TargetType.AURA_CORPSE_MOB))
  1301. {
  1302. for (L2Object target : _actor.getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1303. {
  1304. if (target == getAttackTarget())
  1305. {
  1306. return true;
  1307. }
  1308. }
  1309. }
  1310. return false;
  1311. }
  1312. public boolean canAOE(Skill sk)
  1313. {
  1314. if (sk.hasEffectType(L2EffectType.DISPEL, L2EffectType.DISPEL_BY_SLOT))
  1315. {
  1316. if ((sk.getTargetType() == L2TargetType.AURA) || (sk.getTargetType() == L2TargetType.BEHIND_AURA) || (sk.getTargetType() == L2TargetType.FRONT_AURA) || (sk.getTargetType() == L2TargetType.AURA_CORPSE_MOB))
  1317. {
  1318. boolean cancast = true;
  1319. for (L2Character target : _actor.getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1320. {
  1321. if (!GeoData.getInstance().canSeeTarget(_actor, target))
  1322. {
  1323. continue;
  1324. }
  1325. if (target instanceof L2Attackable)
  1326. {
  1327. L2Npc targets = ((L2Npc) target);
  1328. L2Npc actors = ((L2Npc) _actor);
  1329. if (!targets.isInEnemyClan(actors) && !actors.isChaos())
  1330. {
  1331. continue;
  1332. }
  1333. }
  1334. if (target.isAffectedBySkill(sk.getId()))
  1335. {
  1336. cancast = false;
  1337. }
  1338. }
  1339. if (cancast)
  1340. {
  1341. return true;
  1342. }
  1343. }
  1344. else if ((sk.getTargetType() == L2TargetType.AREA) || (sk.getTargetType() == L2TargetType.BEHIND_AREA) || (sk.getTargetType() == L2TargetType.FRONT_AREA))
  1345. {
  1346. boolean cancast = true;
  1347. for (L2Character target : getAttackTarget().getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1348. {
  1349. if (!GeoData.getInstance().canSeeTarget(_actor, target) || (target == null))
  1350. {
  1351. continue;
  1352. }
  1353. if (target instanceof L2Attackable)
  1354. {
  1355. L2Npc targets = ((L2Npc) target);
  1356. L2Npc actors = ((L2Npc) _actor);
  1357. if (!targets.isInEnemyClan(actors) && !actors.isChaos())
  1358. {
  1359. continue;
  1360. }
  1361. }
  1362. if (!target.getEffectList().isEmpty())
  1363. {
  1364. cancast = true;
  1365. }
  1366. }
  1367. if (cancast)
  1368. {
  1369. return true;
  1370. }
  1371. }
  1372. }
  1373. else
  1374. {
  1375. if ((sk.getTargetType() == L2TargetType.AURA) || (sk.getTargetType() == L2TargetType.BEHIND_AURA) || (sk.getTargetType() == L2TargetType.FRONT_AURA) || (sk.getTargetType() == L2TargetType.AURA_CORPSE_MOB))
  1376. {
  1377. boolean cancast = false;
  1378. for (L2Character target : _actor.getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1379. {
  1380. if (!GeoData.getInstance().canSeeTarget(_actor, target))
  1381. {
  1382. continue;
  1383. }
  1384. if (target instanceof L2Attackable)
  1385. {
  1386. L2Npc targets = ((L2Npc) target);
  1387. L2Npc actors = ((L2Npc) _actor);
  1388. if (!targets.isInEnemyClan(actors) && !actors.isChaos())
  1389. {
  1390. continue;
  1391. }
  1392. }
  1393. if (!target.getEffectList().isEmpty())
  1394. {
  1395. cancast = true;
  1396. }
  1397. }
  1398. if (cancast)
  1399. {
  1400. return true;
  1401. }
  1402. }
  1403. else if ((sk.getTargetType() == L2TargetType.AREA) || (sk.getTargetType() == L2TargetType.BEHIND_AREA) || (sk.getTargetType() == L2TargetType.FRONT_AREA))
  1404. {
  1405. boolean cancast = true;
  1406. for (L2Character target : getAttackTarget().getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1407. {
  1408. if (!GeoData.getInstance().canSeeTarget(_actor, target))
  1409. {
  1410. continue;
  1411. }
  1412. if (target instanceof L2Attackable)
  1413. {
  1414. L2Npc targets = ((L2Npc) target);
  1415. L2Npc actors = ((L2Npc) _actor);
  1416. if (!targets.isInEnemyClan(actors) && !actors.isChaos())
  1417. {
  1418. continue;
  1419. }
  1420. }
  1421. if (target.isAffectedBySkill(sk.getId()))
  1422. {
  1423. cancast = false;
  1424. }
  1425. }
  1426. if (cancast)
  1427. {
  1428. return true;
  1429. }
  1430. }
  1431. }
  1432. return false;
  1433. }
  1434. public boolean canParty(Skill sk)
  1435. {
  1436. if (sk.getTargetType() == L2TargetType.PARTY)
  1437. {
  1438. int count = 0;
  1439. int ccount = 0;
  1440. for (L2Character target : _actor.getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1441. {
  1442. if (!(target instanceof L2Attackable) || !GeoData.getInstance().canSeeTarget(_actor, target))
  1443. {
  1444. continue;
  1445. }
  1446. L2Npc targets = ((L2Npc) target);
  1447. L2Npc actors = ((L2Npc) _actor);
  1448. if (targets.isInMyClan(actors))
  1449. {
  1450. count++;
  1451. if (target.isAffectedBySkill(sk.getId()))
  1452. {
  1453. ccount++;
  1454. }
  1455. }
  1456. }
  1457. if (ccount < count)
  1458. {
  1459. return true;
  1460. }
  1461. }
  1462. return false;
  1463. }
  1464. public boolean isParty(Skill sk)
  1465. {
  1466. return (sk.getTargetType() == L2TargetType.PARTY);
  1467. }
  1468. }