L2CharacterAI.java 48 KB

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