AbstractAI.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.ai;
  16. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  17. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_FOLLOW;
  18. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  19. import java.util.concurrent.Future;
  20. import java.util.logging.Logger;
  21. import com.l2jserver.gameserver.GameTimeController;
  22. import com.l2jserver.gameserver.ThreadPoolManager;
  23. import com.l2jserver.gameserver.model.L2CharPosition;
  24. import com.l2jserver.gameserver.model.L2Object;
  25. import com.l2jserver.gameserver.model.actor.L2Character;
  26. import com.l2jserver.gameserver.model.actor.L2Summon;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.skills.L2Skill;
  29. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  30. import com.l2jserver.gameserver.network.serverpackets.AutoAttackStart;
  31. import com.l2jserver.gameserver.network.serverpackets.AutoAttackStop;
  32. import com.l2jserver.gameserver.network.serverpackets.Die;
  33. import com.l2jserver.gameserver.network.serverpackets.MoveToLocation;
  34. import com.l2jserver.gameserver.network.serverpackets.MoveToPawn;
  35. import com.l2jserver.gameserver.network.serverpackets.StopMove;
  36. import com.l2jserver.gameserver.network.serverpackets.StopRotation;
  37. import com.l2jserver.gameserver.taskmanager.AttackStanceTaskManager;
  38. /**
  39. * Mother class of all objects AI in the world.<br>
  40. * AbastractAI :<br>
  41. * <li>L2CharacterAI</li>
  42. */
  43. public abstract class AbstractAI implements Ctrl
  44. {
  45. protected final Logger _log = Logger.getLogger(getClass().getName());
  46. private NextAction _nextAction;
  47. /**
  48. * @return the _nextAction
  49. */
  50. public NextAction getNextAction()
  51. {
  52. return _nextAction;
  53. }
  54. /**
  55. * @param nextAction the next action to set.
  56. */
  57. public void setNextAction(NextAction nextAction)
  58. {
  59. _nextAction = nextAction;
  60. }
  61. private class FollowTask implements Runnable
  62. {
  63. protected int _range = 70;
  64. public FollowTask()
  65. {
  66. }
  67. public FollowTask(int range)
  68. {
  69. _range = range;
  70. }
  71. @Override
  72. public void run()
  73. {
  74. try
  75. {
  76. if (_followTask == null)
  77. {
  78. return;
  79. }
  80. L2Character followTarget = _followTarget; // copy to prevent NPE
  81. if (followTarget == null)
  82. {
  83. if (_actor instanceof L2Summon)
  84. {
  85. ((L2Summon) _actor).setFollowStatus(false);
  86. }
  87. setIntention(AI_INTENTION_IDLE);
  88. return;
  89. }
  90. if (!_actor.isInsideRadius(followTarget, _range, true, false))
  91. {
  92. if (!_actor.isInsideRadius(followTarget, 3000, true, false))
  93. {
  94. // if the target is too far (maybe also teleported)
  95. if (_actor instanceof L2Summon)
  96. {
  97. ((L2Summon) _actor).setFollowStatus(false);
  98. }
  99. setIntention(AI_INTENTION_IDLE);
  100. return;
  101. }
  102. moveToPawn(followTarget, _range);
  103. }
  104. }
  105. catch (Exception e)
  106. {
  107. _log.warning(getClass().getSimpleName() + ": Error: " + e.getMessage());
  108. }
  109. }
  110. }
  111. /** The character that this AI manages */
  112. protected final L2Character _actor;
  113. /** An accessor for private methods of the actor */
  114. protected final L2Character.AIAccessor _accessor;
  115. /** Current long-term intention */
  116. protected CtrlIntention _intention = AI_INTENTION_IDLE;
  117. /** Current long-term intention parameter */
  118. protected Object _intentionArg0 = null;
  119. /** Current long-term intention parameter */
  120. protected Object _intentionArg1 = null;
  121. /** Flags about client's state, in order to know which messages to send */
  122. protected volatile boolean _clientMoving;
  123. /** Flags about client's state, in order to know which messages to send */
  124. protected volatile boolean _clientAutoAttacking;
  125. /** Flags about client's state, in order to know which messages to send */
  126. protected int _clientMovingToPawnOffset;
  127. /** Different targets this AI maintains */
  128. private L2Object _target;
  129. private L2Character _castTarget;
  130. protected L2Character _attackTarget;
  131. protected L2Character _followTarget;
  132. /** The skill we are currently casting by INTENTION_CAST */
  133. L2Skill _skill;
  134. /** Different internal state flags */
  135. private int _moveToPawnTimeout;
  136. protected Future<?> _followTask = null;
  137. private static final int FOLLOW_INTERVAL = 1000;
  138. private static final int ATTACK_FOLLOW_INTERVAL = 500;
  139. /**
  140. * Constructor of AbstractAI.
  141. * @param accessor The AI accessor of the L2Character
  142. */
  143. protected AbstractAI(L2Character.AIAccessor accessor)
  144. {
  145. _accessor = accessor;
  146. // Get the L2Character managed by this Accessor AI
  147. _actor = accessor.getActor();
  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((L2Skill) arg0, (L2Object) arg1);
  250. break;
  251. case AI_INTENTION_MOVE_TO:
  252. onIntentionMoveTo((L2CharPosition) 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((L2CharPosition) 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(L2Skill skill, L2Object target);
  388. protected abstract void onIntentionMoveTo(L2CharPosition 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(L2CharPosition 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.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.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.getGameTicks();
  464. _moveToPawnTimeout += 1000 / GameTimeController.MILLIS_IN_TICK;
  465. if ((pawn == null) || (_accessor == null))
  466. {
  467. return;
  468. }
  469. // Calculate movement data for a move to location action and add the actor to movingObjects of GameTimeController
  470. _accessor.moveTo(pawn.getX(), pawn.getY(), pawn.getZ(), offset);
  471. if (!_actor.isMoving())
  472. {
  473. _actor.sendPacket(ActionFailed.STATIC_PACKET);
  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. _actor.sendPacket(ActionFailed.STATIC_PACKET);
  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. _accessor.moveTo(x, y, z);
  516. // Send a Server->Client packet CharMoveToLocation to the actor and all L2PcInstance in its _knownPlayers
  517. MoveToLocation msg = new MoveToLocation(_actor);
  518. _actor.broadcastPacket(msg);
  519. }
  520. else
  521. {
  522. _actor.sendPacket(ActionFailed.STATIC_PACKET);
  523. }
  524. }
  525. /**
  526. * Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation <I>(broadcast)</I>.<br>
  527. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  528. * @param pos
  529. */
  530. protected void clientStopMoving(L2CharPosition pos)
  531. {
  532. // Stop movement of the L2Character
  533. if (_actor.isMoving())
  534. {
  535. _accessor.stopMove(pos);
  536. }
  537. _clientMovingToPawnOffset = 0;
  538. if (_clientMoving || (pos != null))
  539. {
  540. _clientMoving = false;
  541. // Send a Server->Client packet StopMove to the actor and all L2PcInstance in its _knownPlayers
  542. StopMove msg = new StopMove(_actor);
  543. _actor.broadcastPacket(msg);
  544. if (pos != null)
  545. {
  546. // Send a Server->Client packet StopRotation to the actor and all L2PcInstance in its _knownPlayers
  547. StopRotation sr = new StopRotation(_actor.getObjectId(), pos.heading, 0);
  548. _actor.sendPacket(sr);
  549. _actor.broadcastPacket(sr);
  550. }
  551. }
  552. }
  553. /**
  554. * Client has already arrived to target, no need to force StopMove packet.
  555. */
  556. protected void clientStoppedMoving()
  557. {
  558. if (_clientMovingToPawnOffset > 0) // movetoPawn needs to be stopped
  559. {
  560. _clientMovingToPawnOffset = 0;
  561. StopMove msg = new StopMove(_actor);
  562. _actor.broadcastPacket(msg);
  563. }
  564. _clientMoving = false;
  565. }
  566. public boolean isAutoAttacking()
  567. {
  568. return _clientAutoAttacking;
  569. }
  570. public void setAutoAttacking(boolean isAutoAttacking)
  571. {
  572. if (_actor instanceof L2Summon)
  573. {
  574. L2Summon summon = (L2Summon) _actor;
  575. if (summon.getOwner() != null)
  576. {
  577. summon.getOwner().getAI().setAutoAttacking(isAutoAttacking);
  578. }
  579. return;
  580. }
  581. _clientAutoAttacking = isAutoAttacking;
  582. }
  583. /**
  584. * Start the actor Auto Attack client side by sending Server->Client packet AutoAttackStart <I>(broadcast)</I>.<br>
  585. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  586. */
  587. public void clientStartAutoAttack()
  588. {
  589. if (_actor instanceof L2Summon)
  590. {
  591. L2Summon summon = (L2Summon) _actor;
  592. if (summon.getOwner() != null)
  593. {
  594. summon.getOwner().getAI().clientStartAutoAttack();
  595. }
  596. return;
  597. }
  598. if (!isAutoAttacking())
  599. {
  600. if ((_actor instanceof L2PcInstance) && ((L2PcInstance) _actor).hasSummon())
  601. {
  602. _actor.getSummon().broadcastPacket(new AutoAttackStart(_actor.getSummon().getObjectId()));
  603. }
  604. // Send a Server->Client packet AutoAttackStart to the actor and all L2PcInstance in its _knownPlayers
  605. _actor.broadcastPacket(new AutoAttackStart(_actor.getObjectId()));
  606. setAutoAttacking(true);
  607. }
  608. AttackStanceTaskManager.getInstance().addAttackStanceTask(_actor);
  609. }
  610. /**
  611. * Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop <I>(broadcast)</I>.<br>
  612. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  613. */
  614. public void clientStopAutoAttack()
  615. {
  616. if (_actor instanceof L2Summon)
  617. {
  618. L2Summon summon = (L2Summon) _actor;
  619. if (summon.getOwner() != null)
  620. {
  621. summon.getOwner().getAI().clientStopAutoAttack();
  622. }
  623. return;
  624. }
  625. if (_actor instanceof L2PcInstance)
  626. {
  627. if (!AttackStanceTaskManager.getInstance().hasAttackStanceTask(_actor) && isAutoAttacking())
  628. {
  629. AttackStanceTaskManager.getInstance().addAttackStanceTask(_actor);
  630. }
  631. }
  632. else if (isAutoAttacking())
  633. {
  634. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  635. setAutoAttacking(false);
  636. }
  637. }
  638. /**
  639. * Kill the actor client side by sending Server->Client packet AutoAttackStop, StopMove/StopRotation, Die <I>(broadcast)</I>.<br>
  640. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  641. */
  642. protected void clientNotifyDead()
  643. {
  644. // Send a Server->Client packet Die to the actor and all L2PcInstance in its _knownPlayers
  645. Die msg = new Die(_actor);
  646. _actor.broadcastPacket(msg);
  647. // Init AI
  648. _intention = AI_INTENTION_IDLE;
  649. _target = null;
  650. _castTarget = null;
  651. _attackTarget = null;
  652. // Cancel the follow task if necessary
  653. stopFollow();
  654. }
  655. /**
  656. * Update the state of this actor client side by sending Server->Client packet MoveToPawn/CharMoveToLocation and AutoAttackStart to the L2PcInstance player.<br>
  657. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  658. * @param player The L2PcIstance to notify with state of this L2Character
  659. */
  660. public void describeStateToPlayer(L2PcInstance player)
  661. {
  662. if (_clientMoving)
  663. {
  664. if ((_clientMovingToPawnOffset != 0) && (_followTarget != null))
  665. {
  666. // Send a Server->Client packet MoveToPawn to the actor and all L2PcInstance in its _knownPlayers
  667. MoveToPawn msg = new MoveToPawn(_actor, _followTarget, _clientMovingToPawnOffset);
  668. player.sendPacket(msg);
  669. }
  670. else
  671. {
  672. // Send a Server->Client packet CharMoveToLocation to the actor and all L2PcInstance in its _knownPlayers
  673. MoveToLocation msg = new MoveToLocation(_actor);
  674. player.sendPacket(msg);
  675. }
  676. }
  677. }
  678. /**
  679. * Create and Launch an AI Follow Task to execute every 1s.
  680. * @param target The L2Character to follow
  681. */
  682. public synchronized void startFollow(L2Character target)
  683. {
  684. if (_followTask != null)
  685. {
  686. _followTask.cancel(false);
  687. _followTask = null;
  688. }
  689. // Create and Launch an AI Follow Task to execute every 1s
  690. _followTarget = target;
  691. _followTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new FollowTask(), 5, FOLLOW_INTERVAL);
  692. }
  693. /**
  694. * Create and Launch an AI Follow Task to execute every 0.5s, following at specified range.
  695. * @param target The L2Character to follow
  696. * @param range
  697. */
  698. public synchronized void startFollow(L2Character target, int range)
  699. {
  700. if (_followTask != null)
  701. {
  702. _followTask.cancel(false);
  703. _followTask = null;
  704. }
  705. _followTarget = target;
  706. _followTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new FollowTask(range), 5, ATTACK_FOLLOW_INTERVAL);
  707. }
  708. /**
  709. * Stop an AI Follow Task.
  710. */
  711. public synchronized void stopFollow()
  712. {
  713. if (_followTask != null)
  714. {
  715. // Stop the Follow Task
  716. _followTask.cancel(false);
  717. _followTask = null;
  718. }
  719. _followTarget = null;
  720. }
  721. protected L2Character getFollowTarget()
  722. {
  723. return _followTarget;
  724. }
  725. protected L2Object getTarget()
  726. {
  727. return _target;
  728. }
  729. protected void setTarget(L2Object target)
  730. {
  731. _target = target;
  732. }
  733. /**
  734. * Stop all Ai tasks and futures.
  735. */
  736. public void stopAITask()
  737. {
  738. stopFollow();
  739. }
  740. @Override
  741. public String toString()
  742. {
  743. return "Actor: " + _actor;
  744. }
  745. }