L2CharacterAI.java 49 KB

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