L2CharacterAI.java 48 KB

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