L2CharacterAI.java 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  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.L2EffectType;
  48. import com.l2jserver.gameserver.model.interfaces.ILocational;
  49. import com.l2jserver.gameserver.model.items.L2Weapon;
  50. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  51. import com.l2jserver.gameserver.model.items.type.L2WeaponType;
  52. import com.l2jserver.gameserver.model.quest.Quest;
  53. import com.l2jserver.gameserver.model.skills.L2Skill;
  54. import com.l2jserver.gameserver.model.skills.L2SkillType;
  55. import com.l2jserver.gameserver.model.skills.targets.L2TargetType;
  56. import com.l2jserver.gameserver.network.SystemMessageId;
  57. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  58. import com.l2jserver.gameserver.network.serverpackets.AutoAttackStop;
  59. import com.l2jserver.gameserver.taskmanager.AttackStanceTaskManager;
  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(ILocational 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, offset + _actor.getTemplate().getCollisionRadius(), false, 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. if ((sk.getSkillType() == L2SkillType.NOTDONE) || (sk.getSkillType() == L2SkillType.COREDONE))
  1129. {
  1130. continue;
  1131. }
  1132. else if (sk.isContinuous())
  1133. {
  1134. if (!sk.isDebuff())
  1135. {
  1136. buffSkills.add(sk);
  1137. }
  1138. else
  1139. {
  1140. debuffSkills.add(sk);
  1141. }
  1142. continue;
  1143. }
  1144. else
  1145. {
  1146. if (sk.hasEffectType(L2EffectType.DISPEL))
  1147. {
  1148. cancelSkills.add(sk);
  1149. }
  1150. else if (sk.hasEffectType(L2EffectType.HEAL, L2EffectType.HEAL_PERCENT))
  1151. {
  1152. healSkills.add(sk);
  1153. hasHealOrResurrect = true;
  1154. }
  1155. else if (sk.hasEffectType(L2EffectType.SLEEP))
  1156. {
  1157. sleepSkills.add(sk);
  1158. }
  1159. else if (sk.hasEffectType(L2EffectType.STUN, L2EffectType.PARALYZE))
  1160. {
  1161. // hardcoding petrification until improvements are made to
  1162. // EffectTemplate... petrification is totally different for
  1163. // AI than paralyze
  1164. switch (sk.getId())
  1165. {
  1166. case 367:
  1167. case 4111:
  1168. case 4383:
  1169. case 4616:
  1170. case 4578:
  1171. sleepSkills.add(sk);
  1172. break;
  1173. default:
  1174. generalDisablers.add(sk);
  1175. break;
  1176. }
  1177. }
  1178. else if (sk.hasEffectType(L2EffectType.ROOT))
  1179. {
  1180. rootSkills.add(sk);
  1181. }
  1182. else if (sk.hasEffectType(L2EffectType.FEAR))
  1183. {
  1184. debuffSkills.add(sk);
  1185. }
  1186. else if (sk.hasEffectType(L2EffectType.MUTE))
  1187. {
  1188. muteSkills.add(sk);
  1189. }
  1190. else if (sk.hasEffectType(L2EffectType.RESURRECTION))
  1191. {
  1192. resurrectSkills.add(sk);
  1193. hasHealOrResurrect = true;
  1194. }
  1195. else
  1196. {
  1197. generalSkills.add(sk);
  1198. hasLongRangeDamageSkill = true;
  1199. }
  1200. }
  1201. if (castRange > 70)
  1202. {
  1203. hasLongRangeSkills = true;
  1204. if (hasLongRangeDamageSkill)
  1205. {
  1206. hasLongRangeDamageSkills = true;
  1207. }
  1208. }
  1209. if (castRange > maxCastRange)
  1210. {
  1211. maxCastRange = castRange;
  1212. }
  1213. }
  1214. // Because of missing skills, some mages/balanced cannot play like mages
  1215. if (!hasLongRangeDamageSkills && isMage)
  1216. {
  1217. isBalanced = true;
  1218. isMage = false;
  1219. isFighter = false;
  1220. }
  1221. if (!hasLongRangeSkills && (isMage || isBalanced))
  1222. {
  1223. isBalanced = false;
  1224. isMage = false;
  1225. isFighter = true;
  1226. }
  1227. if (generalSkills.isEmpty() && isMage)
  1228. {
  1229. isBalanced = true;
  1230. isMage = false;
  1231. }
  1232. }
  1233. }
  1234. protected class TargetAnalysis
  1235. {
  1236. public L2Character character;
  1237. public boolean isMage;
  1238. public boolean isBalanced;
  1239. public boolean isArcher;
  1240. public boolean isFighter;
  1241. public boolean isCanceled;
  1242. public boolean isSlower;
  1243. public boolean isMagicResistant;
  1244. public TargetAnalysis()
  1245. {
  1246. }
  1247. public void update(L2Character target)
  1248. {
  1249. // update status once in 4 seconds
  1250. if ((target == character) && (Rnd.nextInt(100) > 25))
  1251. {
  1252. return;
  1253. }
  1254. character = target;
  1255. if (target == null)
  1256. {
  1257. return;
  1258. }
  1259. isMage = false;
  1260. isBalanced = false;
  1261. isArcher = false;
  1262. isFighter = false;
  1263. isCanceled = false;
  1264. if (target.getMAtk(null, null) > (1.5 * target.getPAtk(null)))
  1265. {
  1266. isMage = true;
  1267. }
  1268. else if (((target.getPAtk(null) * 0.8) < target.getMAtk(null, null)) || ((target.getMAtk(null, null) * 0.8) > target.getPAtk(null)))
  1269. {
  1270. isBalanced = true;
  1271. }
  1272. else
  1273. {
  1274. L2Weapon weapon = target.getActiveWeaponItem();
  1275. if ((weapon != null) && ((weapon.getItemType() == L2WeaponType.BOW) || (weapon.getItemType() == L2WeaponType.CROSSBOW)))
  1276. {
  1277. isArcher = true;
  1278. }
  1279. else
  1280. {
  1281. isFighter = true;
  1282. }
  1283. }
  1284. if (target.getRunSpeed() < (_actor.getRunSpeed() - 3))
  1285. {
  1286. isSlower = true;
  1287. }
  1288. else
  1289. {
  1290. isSlower = false;
  1291. }
  1292. if ((target.getMDef(null, null) * 1.2) > _actor.getMAtk(null, null))
  1293. {
  1294. isMagicResistant = true;
  1295. }
  1296. else
  1297. {
  1298. isMagicResistant = false;
  1299. }
  1300. if (target.getBuffCount() < 4)
  1301. {
  1302. isCanceled = true;
  1303. }
  1304. }
  1305. }
  1306. public boolean canAura(L2Skill sk)
  1307. {
  1308. if ((sk.getTargetType() == L2TargetType.AURA) || (sk.getTargetType() == L2TargetType.BEHIND_AURA) || (sk.getTargetType() == L2TargetType.FRONT_AURA) || (sk.getTargetType() == L2TargetType.AURA_CORPSE_MOB))
  1309. {
  1310. for (L2Object target : _actor.getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1311. {
  1312. if (target == getAttackTarget())
  1313. {
  1314. return true;
  1315. }
  1316. }
  1317. }
  1318. return false;
  1319. }
  1320. public boolean canAOE(L2Skill sk)
  1321. {
  1322. if (sk.hasEffectType(L2EffectType.DISPEL))
  1323. {
  1324. if ((sk.getTargetType() == L2TargetType.AURA) || (sk.getTargetType() == L2TargetType.BEHIND_AURA) || (sk.getTargetType() == L2TargetType.FRONT_AURA) || (sk.getTargetType() == L2TargetType.AURA_CORPSE_MOB))
  1325. {
  1326. boolean cancast = true;
  1327. for (L2Character target : _actor.getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1328. {
  1329. if (!GeoData.getInstance().canSeeTarget(_actor, target))
  1330. {
  1331. continue;
  1332. }
  1333. if (target instanceof L2Attackable)
  1334. {
  1335. L2Npc targets = ((L2Npc) target);
  1336. L2Npc actors = ((L2Npc) _actor);
  1337. if ((targets.getEnemyClan() == null) || (actors.getClan() == null) || !targets.getEnemyClan().equals(actors.getClan()) || ((actors.getClan() == null) && (actors.getIsChaos() == 0)))
  1338. {
  1339. continue;
  1340. }
  1341. }
  1342. if (target.isAffectedBySkill(sk.getId()))
  1343. {
  1344. cancast = false;
  1345. }
  1346. }
  1347. if (cancast)
  1348. {
  1349. return true;
  1350. }
  1351. }
  1352. else if ((sk.getTargetType() == L2TargetType.AREA) || (sk.getTargetType() == L2TargetType.BEHIND_AREA) || (sk.getTargetType() == L2TargetType.FRONT_AREA))
  1353. {
  1354. boolean cancast = true;
  1355. for (L2Character target : getAttackTarget().getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1356. {
  1357. if (!GeoData.getInstance().canSeeTarget(_actor, target) || (target == null))
  1358. {
  1359. continue;
  1360. }
  1361. if (target instanceof L2Attackable)
  1362. {
  1363. L2Npc targets = ((L2Npc) target);
  1364. L2Npc actors = ((L2Npc) _actor);
  1365. if ((targets.getEnemyClan() == null) || (actors.getClan() == null) || !targets.getEnemyClan().equals(actors.getClan()) || ((actors.getClan() == null) && (actors.getIsChaos() == 0)))
  1366. {
  1367. continue;
  1368. }
  1369. }
  1370. if (!target.getEffectList().isEmpty())
  1371. {
  1372. cancast = true;
  1373. }
  1374. }
  1375. if (cancast)
  1376. {
  1377. return true;
  1378. }
  1379. }
  1380. }
  1381. else
  1382. {
  1383. if ((sk.getTargetType() == L2TargetType.AURA) || (sk.getTargetType() == L2TargetType.BEHIND_AURA) || (sk.getTargetType() == L2TargetType.FRONT_AURA) || (sk.getTargetType() == L2TargetType.AURA_CORPSE_MOB))
  1384. {
  1385. boolean cancast = false;
  1386. for (L2Character target : _actor.getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1387. {
  1388. if (!GeoData.getInstance().canSeeTarget(_actor, target))
  1389. {
  1390. continue;
  1391. }
  1392. if (target instanceof L2Attackable)
  1393. {
  1394. L2Npc targets = ((L2Npc) target);
  1395. L2Npc actors = ((L2Npc) _actor);
  1396. if ((targets.getEnemyClan() == null) || (actors.getClan() == null) || !targets.getEnemyClan().equals(actors.getClan()) || ((actors.getClan() == null) && (actors.getIsChaos() == 0)))
  1397. {
  1398. continue;
  1399. }
  1400. }
  1401. if (!target.getEffectList().isEmpty())
  1402. {
  1403. cancast = true;
  1404. }
  1405. }
  1406. if (cancast)
  1407. {
  1408. return true;
  1409. }
  1410. }
  1411. else if ((sk.getTargetType() == L2TargetType.AREA) || (sk.getTargetType() == L2TargetType.BEHIND_AREA) || (sk.getTargetType() == L2TargetType.FRONT_AREA))
  1412. {
  1413. boolean cancast = true;
  1414. for (L2Character target : getAttackTarget().getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1415. {
  1416. if (!GeoData.getInstance().canSeeTarget(_actor, target))
  1417. {
  1418. continue;
  1419. }
  1420. if (target instanceof L2Attackable)
  1421. {
  1422. L2Npc targets = ((L2Npc) target);
  1423. L2Npc actors = ((L2Npc) _actor);
  1424. if ((targets.getEnemyClan() == null) || (actors.getClan() == null) || !targets.getEnemyClan().equals(actors.getClan()) || ((actors.getClan() == null) && (actors.getIsChaos() == 0)))
  1425. {
  1426. continue;
  1427. }
  1428. }
  1429. if (target.isAffectedBySkill(sk.getId()))
  1430. {
  1431. cancast = false;
  1432. }
  1433. }
  1434. if (cancast)
  1435. {
  1436. return true;
  1437. }
  1438. }
  1439. }
  1440. return false;
  1441. }
  1442. public boolean canParty(L2Skill sk)
  1443. {
  1444. if (sk.getTargetType() == L2TargetType.PARTY)
  1445. {
  1446. int count = 0;
  1447. int ccount = 0;
  1448. for (L2Character target : _actor.getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1449. {
  1450. if (!(target instanceof L2Attackable) || !GeoData.getInstance().canSeeTarget(_actor, target))
  1451. {
  1452. continue;
  1453. }
  1454. L2Npc targets = ((L2Npc) target);
  1455. L2Npc actors = ((L2Npc) _actor);
  1456. if ((actors.getFactionId() != null) && targets.getFactionId().equals(actors.getFactionId()))
  1457. {
  1458. count++;
  1459. if (target.isAffectedBySkill(sk.getId()))
  1460. {
  1461. ccount++;
  1462. }
  1463. }
  1464. }
  1465. if (ccount < count)
  1466. {
  1467. return true;
  1468. }
  1469. }
  1470. return false;
  1471. }
  1472. public boolean isParty(L2Skill sk)
  1473. {
  1474. return (sk.getTargetType() == L2TargetType.PARTY);
  1475. }
  1476. }