AbstractAI.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.ai;
  20. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  21. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_FOLLOW;
  22. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  23. import java.util.concurrent.Future;
  24. import java.util.logging.Logger;
  25. import com.l2jserver.gameserver.GameTimeController;
  26. import com.l2jserver.gameserver.ThreadPoolManager;
  27. import com.l2jserver.gameserver.model.L2Object;
  28. import com.l2jserver.gameserver.model.Location;
  29. import com.l2jserver.gameserver.model.actor.L2Character;
  30. import com.l2jserver.gameserver.model.actor.L2Summon;
  31. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  32. import com.l2jserver.gameserver.model.skills.Skill;
  33. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  34. import com.l2jserver.gameserver.network.serverpackets.AutoAttackStart;
  35. import com.l2jserver.gameserver.network.serverpackets.AutoAttackStop;
  36. import com.l2jserver.gameserver.network.serverpackets.Die;
  37. import com.l2jserver.gameserver.network.serverpackets.MoveToLocation;
  38. import com.l2jserver.gameserver.network.serverpackets.MoveToPawn;
  39. import com.l2jserver.gameserver.network.serverpackets.StopMove;
  40. import com.l2jserver.gameserver.network.serverpackets.StopRotation;
  41. import com.l2jserver.gameserver.taskmanager.AttackStanceTaskManager;
  42. /**
  43. * Mother class of all objects AI in the world.<br>
  44. * AbastractAI :<br>
  45. * <li>L2CharacterAI</li>
  46. */
  47. public abstract class AbstractAI implements Ctrl
  48. {
  49. protected final Logger _log = Logger.getLogger(getClass().getName());
  50. private NextAction _nextAction;
  51. /**
  52. * @return the _nextAction
  53. */
  54. public NextAction getNextAction()
  55. {
  56. return _nextAction;
  57. }
  58. /**
  59. * @param nextAction the next action to set.
  60. */
  61. public void setNextAction(NextAction nextAction)
  62. {
  63. _nextAction = nextAction;
  64. }
  65. private class FollowTask implements Runnable
  66. {
  67. protected int _range = 70;
  68. public FollowTask()
  69. {
  70. }
  71. public FollowTask(int range)
  72. {
  73. _range = range;
  74. }
  75. @Override
  76. public void run()
  77. {
  78. try
  79. {
  80. if (_followTask == null)
  81. {
  82. return;
  83. }
  84. L2Character followTarget = _followTarget; // copy to prevent NPE
  85. if (followTarget == null)
  86. {
  87. if (_actor instanceof L2Summon)
  88. {
  89. ((L2Summon) _actor).setFollowStatus(false);
  90. }
  91. setIntention(AI_INTENTION_IDLE);
  92. return;
  93. }
  94. if (!_actor.isInsideRadius(followTarget, _range, true, false))
  95. {
  96. if (!_actor.isInsideRadius(followTarget, 3000, true, false))
  97. {
  98. // if the target is too far (maybe also teleported)
  99. if (_actor instanceof L2Summon)
  100. {
  101. ((L2Summon) _actor).setFollowStatus(false);
  102. }
  103. setIntention(AI_INTENTION_IDLE);
  104. return;
  105. }
  106. moveToPawn(followTarget, _range);
  107. }
  108. }
  109. catch (Exception e)
  110. {
  111. _log.warning(getClass().getSimpleName() + ": Error: " + e.getMessage());
  112. }
  113. }
  114. }
  115. /** The character that this AI manages */
  116. protected final L2Character _actor;
  117. /** Current long-term intention */
  118. protected CtrlIntention _intention = AI_INTENTION_IDLE;
  119. /** Current long-term intention parameter */
  120. protected Object _intentionArg0 = null;
  121. /** Current long-term intention parameter */
  122. protected Object _intentionArg1 = null;
  123. /** Flags about client's state, in order to know which messages to send */
  124. protected volatile boolean _clientMoving;
  125. /** Flags about client's state, in order to know which messages to send */
  126. protected volatile boolean _clientAutoAttacking;
  127. /** Flags about client's state, in order to know which messages to send */
  128. protected int _clientMovingToPawnOffset;
  129. /** Different targets this AI maintains */
  130. private L2Object _target;
  131. private L2Character _castTarget;
  132. protected L2Character _attackTarget;
  133. protected L2Character _followTarget;
  134. /** The skill we are currently casting by INTENTION_CAST */
  135. Skill _skill;
  136. /** Different internal state flags */
  137. private int _moveToPawnTimeout;
  138. protected Future<?> _followTask = null;
  139. private static final int FOLLOW_INTERVAL = 1000;
  140. private static final int ATTACK_FOLLOW_INTERVAL = 500;
  141. /**
  142. * Constructor of AbstractAI.
  143. * @param creature the creature
  144. */
  145. protected AbstractAI(L2Character creature)
  146. {
  147. _actor = creature;
  148. }
  149. /**
  150. * @return the L2Character managed by this Accessor AI.
  151. */
  152. @Override
  153. public L2Character getActor()
  154. {
  155. return _actor;
  156. }
  157. /**
  158. * @return the current Intention.
  159. */
  160. @Override
  161. public CtrlIntention getIntention()
  162. {
  163. return _intention;
  164. }
  165. protected void setCastTarget(L2Character target)
  166. {
  167. _castTarget = target;
  168. }
  169. /**
  170. * @return the current cast target.
  171. */
  172. public L2Character getCastTarget()
  173. {
  174. return _castTarget;
  175. }
  176. protected void setAttackTarget(L2Character target)
  177. {
  178. _attackTarget = target;
  179. }
  180. /**
  181. * @return current attack target.
  182. */
  183. @Override
  184. public L2Character getAttackTarget()
  185. {
  186. return _attackTarget;
  187. }
  188. /**
  189. * Set the Intention of this AbstractAI.<br>
  190. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method is USED by AI classes</B></FONT><B><U><br>
  191. * Overridden in </U> : </B><BR>
  192. * <B>L2AttackableAI</B> : Create an AI Task executed every 1s (if necessary)<BR>
  193. * <B>L2PlayerAI</B> : Stores the current AI intention parameters to later restore it if necessary.
  194. * @param intention The new Intention to set to the AI
  195. * @param arg0 The first parameter of the Intention
  196. * @param arg1 The second parameter of the Intention
  197. */
  198. synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
  199. {
  200. _intention = intention;
  201. _intentionArg0 = arg0;
  202. _intentionArg1 = arg1;
  203. }
  204. /**
  205. * Launch the L2CharacterAI onIntention method corresponding to the new Intention.<br>
  206. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Stop the FOLLOW mode if necessary</B></FONT>
  207. * @param intention The new Intention to set to the AI
  208. */
  209. @Override
  210. public final void setIntention(CtrlIntention intention)
  211. {
  212. setIntention(intention, null, null);
  213. }
  214. /**
  215. * Launch the L2CharacterAI onIntention method corresponding to the new Intention.<br>
  216. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Stop the FOLLOW mode if necessary</B></FONT>
  217. * @param intention The new Intention to set to the AI
  218. * @param arg0 The first parameter of the Intention (optional target)
  219. */
  220. @Override
  221. public final void setIntention(CtrlIntention intention, Object arg0)
  222. {
  223. setIntention(intention, arg0, null);
  224. }
  225. @Override
  226. public final void setIntention(CtrlIntention intention, Object arg0, Object arg1)
  227. {
  228. // Stop the follow mode if necessary
  229. if ((intention != AI_INTENTION_FOLLOW) && (intention != AI_INTENTION_ATTACK))
  230. {
  231. stopFollow();
  232. }
  233. // Launch the onIntention method of the L2CharacterAI corresponding to the new Intention
  234. switch (intention)
  235. {
  236. case AI_INTENTION_IDLE:
  237. onIntentionIdle();
  238. break;
  239. case AI_INTENTION_ACTIVE:
  240. onIntentionActive();
  241. break;
  242. case AI_INTENTION_REST:
  243. onIntentionRest();
  244. break;
  245. case AI_INTENTION_ATTACK:
  246. onIntentionAttack((L2Character) arg0);
  247. break;
  248. case AI_INTENTION_CAST:
  249. onIntentionCast((Skill) arg0, (L2Object) arg1);
  250. break;
  251. case AI_INTENTION_MOVE_TO:
  252. onIntentionMoveTo((Location) arg0);
  253. break;
  254. case AI_INTENTION_FOLLOW:
  255. onIntentionFollow((L2Character) arg0);
  256. break;
  257. case AI_INTENTION_PICK_UP:
  258. onIntentionPickUp((L2Object) arg0);
  259. break;
  260. case AI_INTENTION_INTERACT:
  261. onIntentionInteract((L2Object) arg0);
  262. break;
  263. }
  264. // If do move or follow intention drop next action.
  265. if ((_nextAction != null) && _nextAction.getIntentions().contains(intention))
  266. {
  267. _nextAction = null;
  268. }
  269. }
  270. /**
  271. * Launch the L2CharacterAI onEvt method corresponding to the Event.<br>
  272. * <FONT COLOR=#FF0000><B> <U>Caution</U> : The current general intention won't be change (ex : If the character attack and is stunned, he will attack again after the stunned period)</B></FONT>
  273. * @param evt The event whose the AI must be notified
  274. */
  275. @Override
  276. public final void notifyEvent(CtrlEvent evt)
  277. {
  278. notifyEvent(evt, null, null);
  279. }
  280. /**
  281. * Launch the L2CharacterAI onEvt method corresponding to the Event. <FONT COLOR=#FF0000><B> <U>Caution</U> : The current general intention won't be change (ex : If the character attack and is stunned, he will attack again after the stunned period)</B></FONT>
  282. * @param evt The event whose the AI must be notified
  283. * @param arg0 The first parameter of the Event (optional target)
  284. */
  285. @Override
  286. public final void notifyEvent(CtrlEvent evt, Object arg0)
  287. {
  288. notifyEvent(evt, arg0, null);
  289. }
  290. /**
  291. * Launch the L2CharacterAI onEvt method corresponding to the Event. <FONT COLOR=#FF0000><B> <U>Caution</U> : The current general intention won't be change (ex : If the character attack and is stunned, he will attack again after the stunned period)</B></FONT>
  292. * @param evt The event whose the AI must be notified
  293. * @param arg0 The first parameter of the Event (optional target)
  294. * @param arg1 The second parameter of the Event (optional target)
  295. */
  296. @Override
  297. public final void notifyEvent(CtrlEvent evt, Object arg0, Object arg1)
  298. {
  299. if ((!_actor.isVisible() && !_actor.isTeleporting()) || !_actor.hasAI())
  300. {
  301. return;
  302. }
  303. switch (evt)
  304. {
  305. case EVT_THINK:
  306. onEvtThink();
  307. break;
  308. case EVT_ATTACKED:
  309. onEvtAttacked((L2Character) arg0);
  310. break;
  311. case EVT_AGGRESSION:
  312. onEvtAggression((L2Character) arg0, ((Number) arg1).intValue());
  313. break;
  314. case EVT_STUNNED:
  315. onEvtStunned((L2Character) arg0);
  316. break;
  317. case EVT_PARALYZED:
  318. onEvtParalyzed((L2Character) arg0);
  319. break;
  320. case EVT_SLEEPING:
  321. onEvtSleeping((L2Character) arg0);
  322. break;
  323. case EVT_ROOTED:
  324. onEvtRooted((L2Character) arg0);
  325. break;
  326. case EVT_CONFUSED:
  327. onEvtConfused((L2Character) arg0);
  328. break;
  329. case EVT_MUTED:
  330. onEvtMuted((L2Character) arg0);
  331. break;
  332. case EVT_EVADED:
  333. onEvtEvaded((L2Character) arg0);
  334. break;
  335. case EVT_READY_TO_ACT:
  336. if (!_actor.isCastingNow() && !_actor.isCastingSimultaneouslyNow())
  337. {
  338. onEvtReadyToAct();
  339. }
  340. break;
  341. case EVT_USER_CMD:
  342. onEvtUserCmd(arg0, arg1);
  343. break;
  344. case EVT_ARRIVED:
  345. // happens e.g. from stopmove but we don't process it if we're casting
  346. if (!_actor.isCastingNow() && !_actor.isCastingSimultaneouslyNow())
  347. {
  348. onEvtArrived();
  349. }
  350. break;
  351. case EVT_ARRIVED_REVALIDATE:
  352. // this is disregarded if the char is not moving any more
  353. if (_actor.isMoving())
  354. {
  355. onEvtArrivedRevalidate();
  356. }
  357. break;
  358. case EVT_ARRIVED_BLOCKED:
  359. onEvtArrivedBlocked((Location) arg0);
  360. break;
  361. case EVT_FORGET_OBJECT:
  362. onEvtForgetObject((L2Object) arg0);
  363. break;
  364. case EVT_CANCEL:
  365. onEvtCancel();
  366. break;
  367. case EVT_DEAD:
  368. onEvtDead();
  369. break;
  370. case EVT_FAKE_DEATH:
  371. onEvtFakeDeath();
  372. break;
  373. case EVT_FINISH_CASTING:
  374. onEvtFinishCasting();
  375. break;
  376. }
  377. // Do next action.
  378. if ((_nextAction != null) && _nextAction.getEvents().contains(evt))
  379. {
  380. _nextAction.doAction();
  381. }
  382. }
  383. protected abstract void onIntentionIdle();
  384. protected abstract void onIntentionActive();
  385. protected abstract void onIntentionRest();
  386. protected abstract void onIntentionAttack(L2Character target);
  387. protected abstract void onIntentionCast(Skill skill, L2Object target);
  388. protected abstract void onIntentionMoveTo(Location destination);
  389. protected abstract void onIntentionFollow(L2Character target);
  390. protected abstract void onIntentionPickUp(L2Object item);
  391. protected abstract void onIntentionInteract(L2Object object);
  392. protected abstract void onEvtThink();
  393. protected abstract void onEvtAttacked(L2Character attacker);
  394. protected abstract void onEvtAggression(L2Character target, int aggro);
  395. protected abstract void onEvtStunned(L2Character attacker);
  396. protected abstract void onEvtParalyzed(L2Character attacker);
  397. protected abstract void onEvtSleeping(L2Character attacker);
  398. protected abstract void onEvtRooted(L2Character attacker);
  399. protected abstract void onEvtConfused(L2Character attacker);
  400. protected abstract void onEvtMuted(L2Character attacker);
  401. protected abstract void onEvtEvaded(L2Character attacker);
  402. protected abstract void onEvtReadyToAct();
  403. protected abstract void onEvtUserCmd(Object arg0, Object arg1);
  404. protected abstract void onEvtArrived();
  405. protected abstract void onEvtArrivedRevalidate();
  406. protected abstract void onEvtArrivedBlocked(Location blocked_at_pos);
  407. protected abstract void onEvtForgetObject(L2Object object);
  408. protected abstract void onEvtCancel();
  409. protected abstract void onEvtDead();
  410. protected abstract void onEvtFakeDeath();
  411. protected abstract void onEvtFinishCasting();
  412. /**
  413. * Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor. <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  414. */
  415. protected void clientActionFailed()
  416. {
  417. if (_actor instanceof L2PcInstance)
  418. {
  419. _actor.sendPacket(ActionFailed.STATIC_PACKET);
  420. }
  421. }
  422. /**
  423. * Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn <I>(broadcast)</I>.<br>
  424. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  425. * @param pawn
  426. * @param offset
  427. */
  428. protected void moveToPawn(L2Object pawn, int offset)
  429. {
  430. // Check if actor can move
  431. if (!_actor.isMovementDisabled())
  432. {
  433. if (offset < 10)
  434. {
  435. offset = 10;
  436. }
  437. // prevent possible extra calls to this function (there is none?),
  438. // also don't send movetopawn packets too often
  439. boolean sendPacket = true;
  440. if (_clientMoving && (_target == pawn))
  441. {
  442. if (_clientMovingToPawnOffset == offset)
  443. {
  444. if (GameTimeController.getInstance().getGameTicks() < _moveToPawnTimeout)
  445. {
  446. return;
  447. }
  448. sendPacket = false;
  449. }
  450. else if (_actor.isOnGeodataPath())
  451. {
  452. // minimum time to calculate new route is 2 seconds
  453. if (GameTimeController.getInstance().getGameTicks() < (_moveToPawnTimeout + 10))
  454. {
  455. return;
  456. }
  457. }
  458. }
  459. // Set AI movement data
  460. _clientMoving = true;
  461. _clientMovingToPawnOffset = offset;
  462. _target = pawn;
  463. _moveToPawnTimeout = GameTimeController.getInstance().getGameTicks();
  464. _moveToPawnTimeout += 1000 / GameTimeController.MILLIS_IN_TICK;
  465. if (pawn == null)
  466. {
  467. return;
  468. }
  469. // Calculate movement data for a move to location action and add the actor to movingObjects of GameTimeController
  470. _actor.moveToLocation(pawn.getX(), pawn.getY(), pawn.getZ(), offset);
  471. if (!_actor.isMoving())
  472. {
  473. clientActionFailed();
  474. return;
  475. }
  476. // Send a Server->Client packet MoveToPawn/CharMoveToLocation to the actor and all L2PcInstance in its _knownPlayers
  477. if (pawn instanceof L2Character)
  478. {
  479. if (_actor.isOnGeodataPath())
  480. {
  481. _actor.broadcastPacket(new MoveToLocation(_actor));
  482. _clientMovingToPawnOffset = 0;
  483. }
  484. else if (sendPacket)
  485. {
  486. _actor.broadcastPacket(new MoveToPawn(_actor, (L2Character) pawn, offset));
  487. }
  488. }
  489. else
  490. {
  491. _actor.broadcastPacket(new MoveToLocation(_actor));
  492. }
  493. }
  494. else
  495. {
  496. clientActionFailed();
  497. }
  498. }
  499. /**
  500. * Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation <I>(broadcast)</I>.<br>
  501. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  502. * @param x
  503. * @param y
  504. * @param z
  505. */
  506. protected void moveTo(int x, int y, int z)
  507. {
  508. // Chek if actor can move
  509. if (!_actor.isMovementDisabled())
  510. {
  511. // Set AI movement data
  512. _clientMoving = true;
  513. _clientMovingToPawnOffset = 0;
  514. // Calculate movement data for a move to location action and add the actor to movingObjects of GameTimeController
  515. _actor.moveToLocation(x, y, z, 0);
  516. // Send a Server->Client packet CharMoveToLocation to the actor and all L2PcInstance in its _knownPlayers
  517. _actor.broadcastPacket(new MoveToLocation(_actor));
  518. }
  519. else
  520. {
  521. clientActionFailed();
  522. }
  523. }
  524. /**
  525. * Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation <I>(broadcast)</I>.<br>
  526. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  527. * @param loc
  528. */
  529. protected void clientStopMoving(Location loc)
  530. {
  531. // Stop movement of the L2Character
  532. if (_actor.isMoving())
  533. {
  534. _actor.stopMove(loc);
  535. }
  536. _clientMovingToPawnOffset = 0;
  537. if (_clientMoving || (loc != null))
  538. {
  539. _clientMoving = false;
  540. // Send a Server->Client packet StopMove to the actor and all L2PcInstance in its _knownPlayers
  541. _actor.broadcastPacket(new StopMove(_actor));
  542. if (loc != null)
  543. {
  544. // Send a Server->Client packet StopRotation to the actor and all L2PcInstance in its _knownPlayers
  545. _actor.broadcastPacket(new StopRotation(_actor.getObjectId(), loc.getHeading(), 0));
  546. }
  547. }
  548. }
  549. /**
  550. * Client has already arrived to target, no need to force StopMove packet.
  551. */
  552. protected void clientStoppedMoving()
  553. {
  554. if (_clientMovingToPawnOffset > 0) // movetoPawn needs to be stopped
  555. {
  556. _clientMovingToPawnOffset = 0;
  557. _actor.broadcastPacket(new StopMove(_actor));
  558. }
  559. _clientMoving = false;
  560. }
  561. public boolean isAutoAttacking()
  562. {
  563. return _clientAutoAttacking;
  564. }
  565. public void setAutoAttacking(boolean isAutoAttacking)
  566. {
  567. if (_actor instanceof L2Summon)
  568. {
  569. L2Summon summon = (L2Summon) _actor;
  570. if (summon.getOwner() != null)
  571. {
  572. summon.getOwner().getAI().setAutoAttacking(isAutoAttacking);
  573. }
  574. return;
  575. }
  576. _clientAutoAttacking = isAutoAttacking;
  577. }
  578. /**
  579. * Start the actor Auto Attack client side by sending Server->Client packet AutoAttackStart <I>(broadcast)</I>.<br>
  580. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  581. */
  582. public void clientStartAutoAttack()
  583. {
  584. if (_actor instanceof L2Summon)
  585. {
  586. L2Summon summon = (L2Summon) _actor;
  587. if (summon.getOwner() != null)
  588. {
  589. summon.getOwner().getAI().clientStartAutoAttack();
  590. }
  591. return;
  592. }
  593. if (!isAutoAttacking())
  594. {
  595. if (_actor.isPlayer() && _actor.hasSummon())
  596. {
  597. _actor.getSummon().broadcastPacket(new AutoAttackStart(_actor.getSummon().getObjectId()));
  598. }
  599. // Send a Server->Client packet AutoAttackStart to the actor and all L2PcInstance in its _knownPlayers
  600. _actor.broadcastPacket(new AutoAttackStart(_actor.getObjectId()));
  601. setAutoAttacking(true);
  602. }
  603. AttackStanceTaskManager.getInstance().addAttackStanceTask(_actor);
  604. }
  605. /**
  606. * Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop <I>(broadcast)</I>.<br>
  607. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  608. */
  609. public void clientStopAutoAttack()
  610. {
  611. if (_actor instanceof L2Summon)
  612. {
  613. L2Summon summon = (L2Summon) _actor;
  614. if (summon.getOwner() != null)
  615. {
  616. summon.getOwner().getAI().clientStopAutoAttack();
  617. }
  618. return;
  619. }
  620. if (_actor instanceof L2PcInstance)
  621. {
  622. if (!AttackStanceTaskManager.getInstance().hasAttackStanceTask(_actor) && isAutoAttacking())
  623. {
  624. AttackStanceTaskManager.getInstance().addAttackStanceTask(_actor);
  625. }
  626. }
  627. else if (isAutoAttacking())
  628. {
  629. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  630. setAutoAttacking(false);
  631. }
  632. }
  633. /**
  634. * Kill the actor client side by sending Server->Client packet AutoAttackStop, StopMove/StopRotation, Die <I>(broadcast)</I>.<br>
  635. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  636. */
  637. protected void clientNotifyDead()
  638. {
  639. // Send a Server->Client packet Die to the actor and all L2PcInstance in its _knownPlayers
  640. Die msg = new Die(_actor);
  641. _actor.broadcastPacket(msg);
  642. // Init AI
  643. _intention = AI_INTENTION_IDLE;
  644. _target = null;
  645. _castTarget = null;
  646. _attackTarget = null;
  647. // Cancel the follow task if necessary
  648. stopFollow();
  649. }
  650. /**
  651. * Update the state of this actor client side by sending Server->Client packet MoveToPawn/CharMoveToLocation and AutoAttackStart to the L2PcInstance player.<br>
  652. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  653. * @param player The L2PcIstance to notify with state of this L2Character
  654. */
  655. public void describeStateToPlayer(L2PcInstance player)
  656. {
  657. if (getActor().isVisibleFor(player))
  658. {
  659. if (_clientMoving)
  660. {
  661. if ((_clientMovingToPawnOffset != 0) && (_followTarget != null))
  662. {
  663. // Send a Server->Client packet MoveToPawn to the actor and all L2PcInstance in its _knownPlayers
  664. player.sendPacket(new MoveToPawn(_actor, _followTarget, _clientMovingToPawnOffset));
  665. }
  666. else
  667. {
  668. // Send a Server->Client packet CharMoveToLocation to the actor and all L2PcInstance in its _knownPlayers
  669. player.sendPacket(new MoveToLocation(_actor));
  670. }
  671. }
  672. }
  673. }
  674. /**
  675. * Create and Launch an AI Follow Task to execute every 1s.
  676. * @param target The L2Character to follow
  677. */
  678. public synchronized void startFollow(L2Character target)
  679. {
  680. if (_followTask != null)
  681. {
  682. _followTask.cancel(false);
  683. _followTask = null;
  684. }
  685. // Create and Launch an AI Follow Task to execute every 1s
  686. _followTarget = target;
  687. _followTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new FollowTask(), 5, FOLLOW_INTERVAL);
  688. }
  689. /**
  690. * Create and Launch an AI Follow Task to execute every 0.5s, following at specified range.
  691. * @param target The L2Character to follow
  692. * @param range
  693. */
  694. public synchronized void startFollow(L2Character target, int range)
  695. {
  696. if (_followTask != null)
  697. {
  698. _followTask.cancel(false);
  699. _followTask = null;
  700. }
  701. _followTarget = target;
  702. _followTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new FollowTask(range), 5, ATTACK_FOLLOW_INTERVAL);
  703. }
  704. /**
  705. * Stop an AI Follow Task.
  706. */
  707. public synchronized void stopFollow()
  708. {
  709. if (_followTask != null)
  710. {
  711. // Stop the Follow Task
  712. _followTask.cancel(false);
  713. _followTask = null;
  714. }
  715. _followTarget = null;
  716. }
  717. protected L2Character getFollowTarget()
  718. {
  719. return _followTarget;
  720. }
  721. protected L2Object getTarget()
  722. {
  723. return _target;
  724. }
  725. protected void setTarget(L2Object target)
  726. {
  727. _target = target;
  728. }
  729. /**
  730. * Stop all Ai tasks and futures.
  731. */
  732. public void stopAITask()
  733. {
  734. stopFollow();
  735. }
  736. @Override
  737. public String toString()
  738. {
  739. return "Actor: " + _actor;
  740. }
  741. }