L2CharacterAI.java 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  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.instancemanager.WalkingManager;
  37. import com.l2jserver.gameserver.model.L2Object;
  38. import com.l2jserver.gameserver.model.Location;
  39. import com.l2jserver.gameserver.model.actor.L2Attackable;
  40. import com.l2jserver.gameserver.model.actor.L2Character;
  41. import com.l2jserver.gameserver.model.actor.L2Npc;
  42. import com.l2jserver.gameserver.model.actor.L2Playable;
  43. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  44. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  45. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  46. import com.l2jserver.gameserver.model.effects.L2EffectType;
  47. import com.l2jserver.gameserver.model.events.EventDispatcher;
  48. import com.l2jserver.gameserver.model.events.impl.character.npc.OnNpcMoveFinished;
  49. import com.l2jserver.gameserver.model.interfaces.ILocational;
  50. import com.l2jserver.gameserver.model.items.L2Weapon;
  51. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  52. import com.l2jserver.gameserver.model.items.type.WeaponType;
  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 to scripts
  657. EventDispatcher.getInstance().notifyEventAsync(new OnNpcMoveFinished(npc), npc);
  658. }
  659. // If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE
  660. if (getIntention() == AI_INTENTION_MOVE_TO)
  661. {
  662. setIntention(AI_INTENTION_ACTIVE);
  663. }
  664. // Launch actions corresponding to the Event Think
  665. onEvtThink();
  666. }
  667. /**
  668. * Launch actions corresponding to the Event ArrivedRevalidate.<br>
  669. * <B><U> Actions</U> :</B>
  670. * <ul>
  671. * <li>Launch actions corresponding to the Event Think</li>
  672. * </ul>
  673. */
  674. @Override
  675. protected void onEvtArrivedRevalidate()
  676. {
  677. // Launch actions corresponding to the Event Think
  678. onEvtThink();
  679. }
  680. /**
  681. * Launch actions corresponding to the Event ArrivedBlocked.<br>
  682. * <B><U> Actions</U> :</B>
  683. * <ul>
  684. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  685. * <li>If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE</li>
  686. * <li>Launch actions corresponding to the Event Think</li>
  687. * </ul>
  688. */
  689. @Override
  690. protected void onEvtArrivedBlocked(Location blocked_at_loc)
  691. {
  692. // If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE
  693. if ((getIntention() == AI_INTENTION_MOVE_TO) || (getIntention() == AI_INTENTION_CAST))
  694. {
  695. setIntention(AI_INTENTION_ACTIVE);
  696. }
  697. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  698. clientStopMoving(blocked_at_loc);
  699. // Launch actions corresponding to the Event Think
  700. onEvtThink();
  701. }
  702. /**
  703. * Launch actions corresponding to the Event ForgetObject.<br>
  704. * <B><U> Actions</U> :</B>
  705. * <ul>
  706. * <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>
  707. * <li>If the object was targeted to attack, stop the auto-attack, cancel target and set the Intention to AI_INTENTION_ACTIVE</li>
  708. * <li>If the object was targeted to cast, cancel target and set the Intention to AI_INTENTION_ACTIVE</li>
  709. * <li>If the object was targeted to follow, stop the movement, cancel AI Follow Task and set the Intention to AI_INTENTION_ACTIVE</li>
  710. * <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>
  711. * </ul>
  712. */
  713. @Override
  714. protected void onEvtForgetObject(L2Object object)
  715. {
  716. // If the object was targeted and the Intention was AI_INTENTION_INTERACT or AI_INTENTION_PICK_UP, set the Intention to AI_INTENTION_ACTIVE
  717. if (getTarget() == object)
  718. {
  719. setTarget(null);
  720. if ((getIntention() == AI_INTENTION_INTERACT) || (getIntention() == AI_INTENTION_PICK_UP))
  721. {
  722. setIntention(AI_INTENTION_ACTIVE);
  723. }
  724. }
  725. // Check if the object was targeted to attack
  726. if (getAttackTarget() == object)
  727. {
  728. // Cancel attack target
  729. setAttackTarget(null);
  730. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  731. setIntention(AI_INTENTION_ACTIVE);
  732. }
  733. // Check if the object was targeted to cast
  734. if (getCastTarget() == object)
  735. {
  736. // Cancel cast target
  737. setCastTarget(null);
  738. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  739. setIntention(AI_INTENTION_ACTIVE);
  740. }
  741. // Check if the object was targeted to follow
  742. if (getFollowTarget() == object)
  743. {
  744. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  745. clientStopMoving(null);
  746. // Stop an AI Follow Task
  747. stopFollow();
  748. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  749. setIntention(AI_INTENTION_ACTIVE);
  750. }
  751. // Check if the targeted object was the actor
  752. if (_actor == object)
  753. {
  754. // Cancel AI target
  755. setTarget(null);
  756. setAttackTarget(null);
  757. setCastTarget(null);
  758. // Stop an AI Follow Task
  759. stopFollow();
  760. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  761. clientStopMoving(null);
  762. // Set the Intention of this AbstractAI to AI_INTENTION_IDLE
  763. changeIntention(AI_INTENTION_IDLE, null, null);
  764. }
  765. }
  766. /**
  767. * Launch actions corresponding to the Event Cancel.<br>
  768. * <B><U> Actions</U> :</B>
  769. * <ul>
  770. * <li>Stop an AI Follow Task</li>
  771. * <li>Launch actions corresponding to the Event Think</li>
  772. * </ul>
  773. */
  774. @Override
  775. protected void onEvtCancel()
  776. {
  777. _actor.abortCast();
  778. // Stop an AI Follow Task
  779. stopFollow();
  780. if (!AttackStanceTaskManager.getInstance().hasAttackStanceTask(_actor))
  781. {
  782. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  783. }
  784. // Launch actions corresponding to the Event Think
  785. onEvtThink();
  786. }
  787. /**
  788. * Launch actions corresponding to the Event Dead.<br>
  789. * <B><U> Actions</U> :</B>
  790. * <ul>
  791. * <li>Stop an AI Follow Task</li>
  792. * <li>Kill the actor client side by sending Server->Client packet AutoAttackStop, StopMove/StopRotation, Die (broadcast)</li>
  793. * </ul>
  794. */
  795. @Override
  796. protected void onEvtDead()
  797. {
  798. // Stop an AI Tasks
  799. stopAITask();
  800. // Kill the actor client side by sending Server->Client packet AutoAttackStop, StopMove/StopRotation, Die (broadcast)
  801. clientNotifyDead();
  802. if (!(_actor instanceof L2Playable))
  803. {
  804. _actor.setWalking();
  805. }
  806. }
  807. /**
  808. * Launch actions corresponding to the Event Fake Death.<br>
  809. * <B><U> Actions</U> :</B>
  810. * <ul>
  811. * <li>Stop an AI Follow Task</li>
  812. * </ul>
  813. */
  814. @Override
  815. protected void onEvtFakeDeath()
  816. {
  817. // Stop an AI Follow Task
  818. stopFollow();
  819. // Stop the actor movement and send Server->Client packet StopMove/StopRotation (broadcast)
  820. clientStopMoving(null);
  821. // Init AI
  822. _intention = AI_INTENTION_IDLE;
  823. setTarget(null);
  824. setCastTarget(null);
  825. setAttackTarget(null);
  826. }
  827. /**
  828. * Do nothing.
  829. */
  830. @Override
  831. protected void onEvtFinishCasting()
  832. {
  833. // do nothing
  834. }
  835. protected boolean maybeMoveToPosition(ILocational worldPosition, int offset)
  836. {
  837. if (worldPosition == null)
  838. {
  839. _log.warning("maybeMoveToPosition: worldPosition == NULL!");
  840. return false;
  841. }
  842. if (offset < 0)
  843. {
  844. return false; // skill radius -1
  845. }
  846. if (!_actor.isInsideRadius(worldPosition, offset + _actor.getTemplate().getCollisionRadius(), false, false))
  847. {
  848. if (_actor.isMovementDisabled())
  849. {
  850. return true;
  851. }
  852. if (!_actor.isRunning() && !(this instanceof L2PlayerAI) && !(this instanceof L2SummonAI))
  853. {
  854. _actor.setRunning();
  855. }
  856. stopFollow();
  857. int x = _actor.getX();
  858. int y = _actor.getY();
  859. double dx = worldPosition.getX() - x;
  860. double dy = worldPosition.getY() - y;
  861. double dist = Math.sqrt((dx * dx) + (dy * dy));
  862. double sin = dy / dist;
  863. double cos = dx / dist;
  864. dist -= offset - 5;
  865. x += (int) (dist * cos);
  866. y += (int) (dist * sin);
  867. moveTo(x, y, worldPosition.getZ());
  868. return true;
  869. }
  870. if (getFollowTarget() != null)
  871. {
  872. stopFollow();
  873. }
  874. return false;
  875. }
  876. /**
  877. * Manage the Move to Pawn action in function of the distance and of the Interact area.<br>
  878. * <B><U> Actions</U> :</B>
  879. * <ul>
  880. * <li>Get the distance between the current position of the L2Character and the target (x,y)</li>
  881. * <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>
  882. * <li>If the distance <= offset+20, Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  883. * </ul>
  884. * <B><U> Example of use </U> :</B>
  885. * <ul>
  886. * <li>L2PLayerAI, L2SummonAI</li>
  887. * </ul>
  888. * @param target The targeted L2Object
  889. * @param offset The Interact area radius
  890. * @return True if a movement must be done
  891. */
  892. protected boolean maybeMoveToPawn(L2Object target, int offset)
  893. {
  894. // Get the distance between the current position of the L2Character and the target (x,y)
  895. if (target == null)
  896. {
  897. _log.warning("maybeMoveToPawn: target == NULL!");
  898. return false;
  899. }
  900. if (offset < 0)
  901. {
  902. return false; // skill radius -1
  903. }
  904. offset += _actor.getTemplate().getCollisionRadius();
  905. if (target instanceof L2Character)
  906. {
  907. offset += ((L2Character) target).getTemplate().getCollisionRadius();
  908. }
  909. if (!_actor.isInsideRadius(target, offset, false, false))
  910. {
  911. // Caller should be L2Playable and thinkAttack/thinkCast/thinkInteract/thinkPickUp
  912. if (getFollowTarget() != null)
  913. {
  914. // allow larger hit range when the target is moving (check is run only once per second)
  915. if (!_actor.isInsideRadius(target, offset + 100, false, false))
  916. {
  917. return true;
  918. }
  919. stopFollow();
  920. return false;
  921. }
  922. if (_actor.isMovementDisabled())
  923. {
  924. // If player is trying attack target but he cannot move to attack target
  925. // change his intention to idle
  926. if (_actor.getAI().getIntention() == CtrlIntention.AI_INTENTION_ATTACK)
  927. {
  928. _actor.getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  929. }
  930. return true;
  931. }
  932. // while flying there is no move to cast
  933. if ((_actor.getAI().getIntention() == CtrlIntention.AI_INTENTION_CAST) && (_actor instanceof L2PcInstance) && ((L2PcInstance) _actor).isTransformed())
  934. {
  935. if (!((L2PcInstance) _actor).getTransformation().isCombat())
  936. {
  937. _actor.sendPacket(SystemMessageId.DIST_TOO_FAR_CASTING_STOPPED);
  938. _actor.sendPacket(ActionFailed.STATIC_PACKET);
  939. return true;
  940. }
  941. }
  942. // If not running, set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  943. if (!_actor.isRunning() && !(this instanceof L2PlayerAI) && !(this instanceof L2SummonAI))
  944. {
  945. _actor.setRunning();
  946. }
  947. stopFollow();
  948. if ((target instanceof L2Character) && !(target instanceof L2DoorInstance))
  949. {
  950. if (((L2Character) target).isMoving())
  951. {
  952. offset -= 100;
  953. }
  954. if (offset < 5)
  955. {
  956. offset = 5;
  957. }
  958. startFollow((L2Character) target, offset);
  959. }
  960. else
  961. {
  962. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  963. moveToPawn(target, offset);
  964. }
  965. return true;
  966. }
  967. if (getFollowTarget() != null)
  968. {
  969. stopFollow();
  970. }
  971. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  972. // clientStopMoving(null);
  973. return false;
  974. }
  975. /**
  976. * Modify current Intention and actions if the target is lost or dead.<br>
  977. * <B><U> Actions</U> : <I>If the target is lost or dead</I></B>
  978. * <ul>
  979. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  980. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  981. * <li>Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE</li>
  982. * </ul>
  983. * <B><U> Example of use </U> :</B>
  984. * <ul>
  985. * <li>L2PLayerAI, L2SummonAI</li>
  986. * </ul>
  987. * @param target The targeted L2Object
  988. * @return True if the target is lost or dead (false if fakedeath)
  989. */
  990. protected boolean checkTargetLostOrDead(L2Character target)
  991. {
  992. if ((target == null) || target.isAlikeDead())
  993. {
  994. // check if player is fakedeath
  995. if ((target instanceof L2PcInstance) && ((L2PcInstance) target).isFakeDeath())
  996. {
  997. target.stopFakeDeath(true);
  998. return false;
  999. }
  1000. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  1001. setIntention(AI_INTENTION_ACTIVE);
  1002. return true;
  1003. }
  1004. return false;
  1005. }
  1006. /**
  1007. * Modify current Intention and actions if the target is lost.<br>
  1008. * <B><U> Actions</U> : <I>If the target is lost</I></B>
  1009. * <ul>
  1010. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  1011. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  1012. * <li>Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE</li>
  1013. * </ul>
  1014. * <B><U> Example of use </U> :</B>
  1015. * <ul>
  1016. * <li>L2PLayerAI, L2SummonAI</li>
  1017. * </ul>
  1018. * @param target The targeted L2Object
  1019. * @return True if the target is lost
  1020. */
  1021. protected boolean checkTargetLost(L2Object target)
  1022. {
  1023. // check if player is fakedeath
  1024. if (target instanceof L2PcInstance)
  1025. {
  1026. L2PcInstance target2 = (L2PcInstance) target; // convert object to chara
  1027. if (target2.isFakeDeath())
  1028. {
  1029. target2.stopFakeDeath(true);
  1030. return false;
  1031. }
  1032. }
  1033. if (target == null)
  1034. {
  1035. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  1036. setIntention(AI_INTENTION_ACTIVE);
  1037. return true;
  1038. }
  1039. if ((_actor != null) && (_skill != null) && _skill.isBad() && (_skill.getAffectRange() > 0) && (Config.GEODATA > 0) && !GeoData.getInstance().canSeeTarget(_actor, target))
  1040. {
  1041. setIntention(AI_INTENTION_ACTIVE);
  1042. return true;
  1043. }
  1044. return false;
  1045. }
  1046. protected class SelfAnalysis
  1047. {
  1048. public boolean isMage = false;
  1049. public boolean isBalanced;
  1050. public boolean isArcher = false;
  1051. public boolean isHealer = false;
  1052. public boolean isFighter = false;
  1053. public boolean cannotMoveOnLand = false;
  1054. public List<Skill> generalSkills = new FastList<>();
  1055. public List<Skill> buffSkills = new FastList<>();
  1056. public int lastBuffTick = 0;
  1057. public List<Skill> debuffSkills = new FastList<>();
  1058. public int lastDebuffTick = 0;
  1059. public List<Skill> cancelSkills = new FastList<>();
  1060. public List<Skill> healSkills = new FastList<>();
  1061. // public List<L2Skill> trickSkills = new FastList<>();
  1062. public List<Skill> generalDisablers = new FastList<>();
  1063. public List<Skill> sleepSkills = new FastList<>();
  1064. public List<Skill> rootSkills = new FastList<>();
  1065. public List<Skill> muteSkills = new FastList<>();
  1066. public List<Skill> resurrectSkills = new FastList<>();
  1067. public boolean hasHealOrResurrect = false;
  1068. public boolean hasLongRangeSkills = false;
  1069. public boolean hasLongRangeDamageSkills = false;
  1070. public int maxCastRange = 0;
  1071. public SelfAnalysis()
  1072. {
  1073. }
  1074. public void init()
  1075. {
  1076. switch (((L2NpcTemplate) _actor.getTemplate()).getAIType())
  1077. {
  1078. case FIGHTER:
  1079. isFighter = true;
  1080. break;
  1081. case MAGE:
  1082. isMage = true;
  1083. break;
  1084. case CORPSE:
  1085. case BALANCED:
  1086. isBalanced = true;
  1087. break;
  1088. case ARCHER:
  1089. isArcher = true;
  1090. break;
  1091. case HEALER:
  1092. isHealer = true;
  1093. break;
  1094. default:
  1095. isFighter = true;
  1096. break;
  1097. }
  1098. // water movement analysis
  1099. if (_actor.isNpc())
  1100. {
  1101. switch (_actor.getId())
  1102. {
  1103. case 20314: // great white shark
  1104. case 20849: // Light Worm
  1105. cannotMoveOnLand = true;
  1106. break;
  1107. default:
  1108. cannotMoveOnLand = false;
  1109. break;
  1110. }
  1111. }
  1112. // skill analysis
  1113. for (Skill sk : _actor.getAllSkills())
  1114. {
  1115. if (sk.isPassive())
  1116. {
  1117. continue;
  1118. }
  1119. int castRange = sk.getCastRange();
  1120. boolean hasLongRangeDamageSkill = false;
  1121. if (sk.isContinuous())
  1122. {
  1123. if (!sk.isDebuff())
  1124. {
  1125. buffSkills.add(sk);
  1126. }
  1127. else
  1128. {
  1129. debuffSkills.add(sk);
  1130. }
  1131. continue;
  1132. }
  1133. if (sk.hasEffectType(L2EffectType.DISPEL, L2EffectType.DISPEL_BY_SLOT))
  1134. {
  1135. cancelSkills.add(sk);
  1136. }
  1137. else if (sk.hasEffectType(L2EffectType.HEAL))
  1138. {
  1139. healSkills.add(sk);
  1140. hasHealOrResurrect = true;
  1141. }
  1142. else if (sk.hasEffectType(L2EffectType.SLEEP))
  1143. {
  1144. sleepSkills.add(sk);
  1145. }
  1146. else if (sk.hasEffectType(L2EffectType.STUN, L2EffectType.PARALYZE))
  1147. {
  1148. // hardcoding petrification until improvements are made to
  1149. // EffectTemplate... petrification is totally different for
  1150. // AI than paralyze
  1151. switch (sk.getId())
  1152. {
  1153. case 367:
  1154. case 4111:
  1155. case 4383:
  1156. case 4616:
  1157. case 4578:
  1158. sleepSkills.add(sk);
  1159. break;
  1160. default:
  1161. generalDisablers.add(sk);
  1162. break;
  1163. }
  1164. }
  1165. else if (sk.hasEffectType(L2EffectType.ROOT))
  1166. {
  1167. rootSkills.add(sk);
  1168. }
  1169. else if (sk.hasEffectType(L2EffectType.FEAR))
  1170. {
  1171. debuffSkills.add(sk);
  1172. }
  1173. else if (sk.hasEffectType(L2EffectType.MUTE))
  1174. {
  1175. muteSkills.add(sk);
  1176. }
  1177. else if (sk.hasEffectType(L2EffectType.RESURRECTION))
  1178. {
  1179. resurrectSkills.add(sk);
  1180. hasHealOrResurrect = true;
  1181. }
  1182. else
  1183. {
  1184. generalSkills.add(sk);
  1185. hasLongRangeDamageSkill = true;
  1186. }
  1187. if (castRange > 70)
  1188. {
  1189. hasLongRangeSkills = true;
  1190. if (hasLongRangeDamageSkill)
  1191. {
  1192. hasLongRangeDamageSkills = true;
  1193. }
  1194. }
  1195. if (castRange > maxCastRange)
  1196. {
  1197. maxCastRange = castRange;
  1198. }
  1199. }
  1200. // Because of missing skills, some mages/balanced cannot play like mages
  1201. if (!hasLongRangeDamageSkills && isMage)
  1202. {
  1203. isBalanced = true;
  1204. isMage = false;
  1205. isFighter = false;
  1206. }
  1207. if (!hasLongRangeSkills && (isMage || isBalanced))
  1208. {
  1209. isBalanced = false;
  1210. isMage = false;
  1211. isFighter = true;
  1212. }
  1213. if (generalSkills.isEmpty() && isMage)
  1214. {
  1215. isBalanced = true;
  1216. isMage = false;
  1217. }
  1218. }
  1219. }
  1220. protected class TargetAnalysis
  1221. {
  1222. public L2Character character;
  1223. public boolean isMage;
  1224. public boolean isBalanced;
  1225. public boolean isArcher;
  1226. public boolean isFighter;
  1227. public boolean isCanceled;
  1228. public boolean isSlower;
  1229. public boolean isMagicResistant;
  1230. public TargetAnalysis()
  1231. {
  1232. }
  1233. public void update(L2Character target)
  1234. {
  1235. // update status once in 4 seconds
  1236. if ((target == character) && (Rnd.nextInt(100) > 25))
  1237. {
  1238. return;
  1239. }
  1240. character = target;
  1241. if (target == null)
  1242. {
  1243. return;
  1244. }
  1245. isMage = false;
  1246. isBalanced = false;
  1247. isArcher = false;
  1248. isFighter = false;
  1249. isCanceled = false;
  1250. if (target.getMAtk(null, null) > (1.5 * target.getPAtk(null)))
  1251. {
  1252. isMage = true;
  1253. }
  1254. else if (((target.getPAtk(null) * 0.8) < target.getMAtk(null, null)) || ((target.getMAtk(null, null) * 0.8) > target.getPAtk(null)))
  1255. {
  1256. isBalanced = true;
  1257. }
  1258. else
  1259. {
  1260. L2Weapon weapon = target.getActiveWeaponItem();
  1261. if ((weapon != null) && ((weapon.getItemType() == WeaponType.BOW) || (weapon.getItemType() == WeaponType.CROSSBOW)))
  1262. {
  1263. isArcher = true;
  1264. }
  1265. else
  1266. {
  1267. isFighter = true;
  1268. }
  1269. }
  1270. if (target.getRunSpeed() < (_actor.getRunSpeed() - 3))
  1271. {
  1272. isSlower = true;
  1273. }
  1274. else
  1275. {
  1276. isSlower = false;
  1277. }
  1278. if ((target.getMDef(null, null) * 1.2) > _actor.getMAtk(null, null))
  1279. {
  1280. isMagicResistant = true;
  1281. }
  1282. else
  1283. {
  1284. isMagicResistant = false;
  1285. }
  1286. if (target.getBuffCount() < 4)
  1287. {
  1288. isCanceled = true;
  1289. }
  1290. }
  1291. }
  1292. public boolean canAura(Skill sk)
  1293. {
  1294. if ((sk.getTargetType() == L2TargetType.AURA) || (sk.getTargetType() == L2TargetType.BEHIND_AURA) || (sk.getTargetType() == L2TargetType.FRONT_AURA) || (sk.getTargetType() == L2TargetType.AURA_CORPSE_MOB))
  1295. {
  1296. for (L2Object target : _actor.getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1297. {
  1298. if (target == getAttackTarget())
  1299. {
  1300. return true;
  1301. }
  1302. }
  1303. }
  1304. return false;
  1305. }
  1306. public boolean canAOE(Skill sk)
  1307. {
  1308. if (sk.hasEffectType(L2EffectType.DISPEL, L2EffectType.DISPEL_BY_SLOT))
  1309. {
  1310. if ((sk.getTargetType() == L2TargetType.AURA) || (sk.getTargetType() == L2TargetType.BEHIND_AURA) || (sk.getTargetType() == L2TargetType.FRONT_AURA) || (sk.getTargetType() == L2TargetType.AURA_CORPSE_MOB))
  1311. {
  1312. boolean cancast = true;
  1313. for (L2Character target : _actor.getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1314. {
  1315. if (!GeoData.getInstance().canSeeTarget(_actor, target))
  1316. {
  1317. continue;
  1318. }
  1319. if (target instanceof L2Attackable)
  1320. {
  1321. L2Npc targets = ((L2Npc) target);
  1322. L2Npc actors = ((L2Npc) _actor);
  1323. if (!targets.isInEnemyClan(actors) && !actors.isChaos())
  1324. {
  1325. continue;
  1326. }
  1327. }
  1328. if (target.isAffectedBySkill(sk.getId()))
  1329. {
  1330. cancast = false;
  1331. }
  1332. }
  1333. if (cancast)
  1334. {
  1335. return true;
  1336. }
  1337. }
  1338. else if ((sk.getTargetType() == L2TargetType.AREA) || (sk.getTargetType() == L2TargetType.BEHIND_AREA) || (sk.getTargetType() == L2TargetType.FRONT_AREA))
  1339. {
  1340. boolean cancast = true;
  1341. for (L2Character target : getAttackTarget().getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1342. {
  1343. if (!GeoData.getInstance().canSeeTarget(_actor, target) || (target == null))
  1344. {
  1345. continue;
  1346. }
  1347. if (target instanceof L2Attackable)
  1348. {
  1349. L2Npc targets = ((L2Npc) target);
  1350. L2Npc actors = ((L2Npc) _actor);
  1351. if (!targets.isInEnemyClan(actors) && !actors.isChaos())
  1352. {
  1353. continue;
  1354. }
  1355. }
  1356. if (!target.getEffectList().isEmpty())
  1357. {
  1358. cancast = true;
  1359. }
  1360. }
  1361. if (cancast)
  1362. {
  1363. return true;
  1364. }
  1365. }
  1366. }
  1367. else
  1368. {
  1369. if ((sk.getTargetType() == L2TargetType.AURA) || (sk.getTargetType() == L2TargetType.BEHIND_AURA) || (sk.getTargetType() == L2TargetType.FRONT_AURA) || (sk.getTargetType() == L2TargetType.AURA_CORPSE_MOB))
  1370. {
  1371. boolean cancast = false;
  1372. for (L2Character target : _actor.getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1373. {
  1374. if (!GeoData.getInstance().canSeeTarget(_actor, target))
  1375. {
  1376. continue;
  1377. }
  1378. if (target instanceof L2Attackable)
  1379. {
  1380. L2Npc targets = ((L2Npc) target);
  1381. L2Npc actors = ((L2Npc) _actor);
  1382. if (!targets.isInEnemyClan(actors) && !actors.isChaos())
  1383. {
  1384. continue;
  1385. }
  1386. }
  1387. if (!target.getEffectList().isEmpty())
  1388. {
  1389. cancast = true;
  1390. }
  1391. }
  1392. if (cancast)
  1393. {
  1394. return true;
  1395. }
  1396. }
  1397. else if ((sk.getTargetType() == L2TargetType.AREA) || (sk.getTargetType() == L2TargetType.BEHIND_AREA) || (sk.getTargetType() == L2TargetType.FRONT_AREA))
  1398. {
  1399. boolean cancast = true;
  1400. for (L2Character target : getAttackTarget().getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1401. {
  1402. if (!GeoData.getInstance().canSeeTarget(_actor, target))
  1403. {
  1404. continue;
  1405. }
  1406. if (target instanceof L2Attackable)
  1407. {
  1408. L2Npc targets = ((L2Npc) target);
  1409. L2Npc actors = ((L2Npc) _actor);
  1410. if (!targets.isInEnemyClan(actors) && !actors.isChaos())
  1411. {
  1412. continue;
  1413. }
  1414. }
  1415. if (target.isAffectedBySkill(sk.getId()))
  1416. {
  1417. cancast = false;
  1418. }
  1419. }
  1420. if (cancast)
  1421. {
  1422. return true;
  1423. }
  1424. }
  1425. }
  1426. return false;
  1427. }
  1428. public boolean canParty(Skill sk)
  1429. {
  1430. if (sk.getTargetType() == L2TargetType.PARTY)
  1431. {
  1432. int count = 0;
  1433. int ccount = 0;
  1434. for (L2Character target : _actor.getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1435. {
  1436. if (!(target instanceof L2Attackable) || !GeoData.getInstance().canSeeTarget(_actor, target))
  1437. {
  1438. continue;
  1439. }
  1440. L2Npc targets = ((L2Npc) target);
  1441. L2Npc actors = ((L2Npc) _actor);
  1442. if (targets.isInMyClan(actors))
  1443. {
  1444. count++;
  1445. if (target.isAffectedBySkill(sk.getId()))
  1446. {
  1447. ccount++;
  1448. }
  1449. }
  1450. }
  1451. if (ccount < count)
  1452. {
  1453. return true;
  1454. }
  1455. }
  1456. return false;
  1457. }
  1458. public boolean isParty(Skill sk)
  1459. {
  1460. return (sk.getTargetType() == L2TargetType.PARTY);
  1461. }
  1462. }