L2CharacterAI.java 50 KB

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