AbstractAI.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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. */
  294. @Override
  295. public final void notifyEvent(CtrlEvent evt, Object... args)
  296. {
  297. if ((!_actor.isVisible() && !_actor.isTeleporting()) || !_actor.hasAI())
  298. {
  299. return;
  300. }
  301. switch (evt)
  302. {
  303. case EVT_THINK:
  304. onEvtThink();
  305. break;
  306. case EVT_ATTACKED:
  307. onEvtAttacked((L2Character) args[0]);
  308. break;
  309. case EVT_AGGRESSION:
  310. onEvtAggression((L2Character) args[0], ((Number) args[1]).intValue());
  311. break;
  312. case EVT_STUNNED:
  313. onEvtStunned((L2Character) args[0]);
  314. break;
  315. case EVT_PARALYZED:
  316. onEvtParalyzed((L2Character) args[0]);
  317. break;
  318. case EVT_SLEEPING:
  319. onEvtSleeping((L2Character) args[0]);
  320. break;
  321. case EVT_ROOTED:
  322. onEvtRooted((L2Character) args[0]);
  323. break;
  324. case EVT_CONFUSED:
  325. onEvtConfused((L2Character) args[0]);
  326. break;
  327. case EVT_MUTED:
  328. onEvtMuted((L2Character) args[0]);
  329. break;
  330. case EVT_EVADED:
  331. onEvtEvaded((L2Character) args[0]);
  332. break;
  333. case EVT_READY_TO_ACT:
  334. if (!_actor.isCastingNow() && !_actor.isCastingSimultaneouslyNow())
  335. {
  336. onEvtReadyToAct();
  337. }
  338. break;
  339. case EVT_USER_CMD:
  340. onEvtUserCmd(args[0], args[1]);
  341. break;
  342. case EVT_ARRIVED:
  343. // happens e.g. from stopmove but we don't process it if we're casting
  344. if (!_actor.isCastingNow() && !_actor.isCastingSimultaneouslyNow())
  345. {
  346. onEvtArrived();
  347. }
  348. break;
  349. case EVT_ARRIVED_REVALIDATE:
  350. // this is disregarded if the char is not moving any more
  351. if (_actor.isMoving())
  352. {
  353. onEvtArrivedRevalidate();
  354. }
  355. break;
  356. case EVT_ARRIVED_BLOCKED:
  357. onEvtArrivedBlocked((Location) args[0]);
  358. break;
  359. case EVT_FORGET_OBJECT:
  360. onEvtForgetObject((L2Object) args[0]);
  361. break;
  362. case EVT_CANCEL:
  363. onEvtCancel();
  364. break;
  365. case EVT_DEAD:
  366. onEvtDead();
  367. break;
  368. case EVT_FAKE_DEATH:
  369. onEvtFakeDeath();
  370. break;
  371. case EVT_FINISH_CASTING:
  372. onEvtFinishCasting();
  373. break;
  374. case EVT_AFRAID:
  375. {
  376. onEvtAfraid((L2Character) args[0], (Boolean) args[1]);
  377. break;
  378. }
  379. }
  380. // Do next action.
  381. if ((_nextAction != null) && _nextAction.getEvents().contains(evt))
  382. {
  383. _nextAction.doAction();
  384. }
  385. }
  386. protected abstract void onIntentionIdle();
  387. protected abstract void onIntentionActive();
  388. protected abstract void onIntentionRest();
  389. protected abstract void onIntentionAttack(L2Character target);
  390. protected abstract void onIntentionCast(Skill skill, L2Object target);
  391. protected abstract void onIntentionMoveTo(Location destination);
  392. protected abstract void onIntentionFollow(L2Character target);
  393. protected abstract void onIntentionPickUp(L2Object item);
  394. protected abstract void onIntentionInteract(L2Object object);
  395. protected abstract void onEvtThink();
  396. protected abstract void onEvtAttacked(L2Character attacker);
  397. protected abstract void onEvtAggression(L2Character target, int aggro);
  398. protected abstract void onEvtStunned(L2Character attacker);
  399. protected abstract void onEvtParalyzed(L2Character attacker);
  400. protected abstract void onEvtSleeping(L2Character attacker);
  401. protected abstract void onEvtRooted(L2Character attacker);
  402. protected abstract void onEvtConfused(L2Character attacker);
  403. protected abstract void onEvtMuted(L2Character attacker);
  404. protected abstract void onEvtEvaded(L2Character attacker);
  405. protected abstract void onEvtReadyToAct();
  406. protected abstract void onEvtUserCmd(Object arg0, Object arg1);
  407. protected abstract void onEvtArrived();
  408. protected abstract void onEvtArrivedRevalidate();
  409. protected abstract void onEvtArrivedBlocked(Location blocked_at_pos);
  410. protected abstract void onEvtForgetObject(L2Object object);
  411. protected abstract void onEvtCancel();
  412. protected abstract void onEvtDead();
  413. protected abstract void onEvtFakeDeath();
  414. protected abstract void onEvtFinishCasting();
  415. protected abstract void onEvtAfraid(L2Character effector, boolean start);
  416. /**
  417. * 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>
  418. */
  419. protected void clientActionFailed()
  420. {
  421. if (_actor instanceof L2PcInstance)
  422. {
  423. _actor.sendPacket(ActionFailed.STATIC_PACKET);
  424. }
  425. }
  426. /**
  427. * Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn <I>(broadcast)</I>.<br>
  428. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  429. * @param pawn
  430. * @param offset
  431. */
  432. protected void moveToPawn(L2Object pawn, int offset)
  433. {
  434. // Check if actor can move
  435. if (!_actor.isMovementDisabled())
  436. {
  437. if (offset < 10)
  438. {
  439. offset = 10;
  440. }
  441. // prevent possible extra calls to this function (there is none?),
  442. // also don't send movetopawn packets too often
  443. boolean sendPacket = true;
  444. if (_clientMoving && (_target == pawn))
  445. {
  446. if (_clientMovingToPawnOffset == offset)
  447. {
  448. if (GameTimeController.getInstance().getGameTicks() < _moveToPawnTimeout)
  449. {
  450. return;
  451. }
  452. sendPacket = false;
  453. }
  454. else if (_actor.isOnGeodataPath())
  455. {
  456. // minimum time to calculate new route is 2 seconds
  457. if (GameTimeController.getInstance().getGameTicks() < (_moveToPawnTimeout + 10))
  458. {
  459. return;
  460. }
  461. }
  462. }
  463. // Set AI movement data
  464. _clientMoving = true;
  465. _clientMovingToPawnOffset = offset;
  466. _target = pawn;
  467. _moveToPawnTimeout = GameTimeController.getInstance().getGameTicks();
  468. _moveToPawnTimeout += 1000 / GameTimeController.MILLIS_IN_TICK;
  469. if (pawn == null)
  470. {
  471. return;
  472. }
  473. // Calculate movement data for a move to location action and add the actor to movingObjects of GameTimeController
  474. _actor.moveToLocation(pawn.getX(), pawn.getY(), pawn.getZ(), offset);
  475. if (!_actor.isMoving())
  476. {
  477. clientActionFailed();
  478. return;
  479. }
  480. // Send a Server->Client packet MoveToPawn/CharMoveToLocation to the actor and all L2PcInstance in its _knownPlayers
  481. if (pawn instanceof L2Character)
  482. {
  483. if (_actor.isOnGeodataPath())
  484. {
  485. _actor.broadcastPacket(new MoveToLocation(_actor));
  486. _clientMovingToPawnOffset = 0;
  487. }
  488. else if (sendPacket)
  489. {
  490. _actor.broadcastPacket(new MoveToPawn(_actor, (L2Character) pawn, offset));
  491. }
  492. }
  493. else
  494. {
  495. _actor.broadcastPacket(new MoveToLocation(_actor));
  496. }
  497. }
  498. else
  499. {
  500. clientActionFailed();
  501. }
  502. }
  503. /**
  504. * Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation <I>(broadcast)</I>.<br>
  505. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  506. * @param x
  507. * @param y
  508. * @param z
  509. */
  510. protected void moveTo(int x, int y, int z)
  511. {
  512. // Chek if actor can move
  513. if (!_actor.isMovementDisabled())
  514. {
  515. // Set AI movement data
  516. _clientMoving = true;
  517. _clientMovingToPawnOffset = 0;
  518. // Calculate movement data for a move to location action and add the actor to movingObjects of GameTimeController
  519. _actor.moveToLocation(x, y, z, 0);
  520. // Send a Server->Client packet CharMoveToLocation to the actor and all L2PcInstance in its _knownPlayers
  521. _actor.broadcastPacket(new MoveToLocation(_actor));
  522. }
  523. else
  524. {
  525. clientActionFailed();
  526. }
  527. }
  528. /**
  529. * Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation <I>(broadcast)</I>.<br>
  530. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  531. * @param loc
  532. */
  533. protected void clientStopMoving(Location loc)
  534. {
  535. // Stop movement of the L2Character
  536. if (_actor.isMoving())
  537. {
  538. _actor.stopMove(loc);
  539. }
  540. _clientMovingToPawnOffset = 0;
  541. if (_clientMoving || (loc != null))
  542. {
  543. _clientMoving = false;
  544. // Send a Server->Client packet StopMove to the actor and all L2PcInstance in its _knownPlayers
  545. _actor.broadcastPacket(new StopMove(_actor));
  546. if (loc != null)
  547. {
  548. // Send a Server->Client packet StopRotation to the actor and all L2PcInstance in its _knownPlayers
  549. _actor.broadcastPacket(new StopRotation(_actor.getObjectId(), loc.getHeading(), 0));
  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. _actor.broadcastPacket(new StopMove(_actor));
  562. }
  563. _clientMoving = false;
  564. }
  565. public boolean isAutoAttacking()
  566. {
  567. return _clientAutoAttacking;
  568. }
  569. public void setAutoAttacking(boolean isAutoAttacking)
  570. {
  571. if (_actor instanceof L2Summon)
  572. {
  573. L2Summon summon = (L2Summon) _actor;
  574. if (summon.getOwner() != null)
  575. {
  576. summon.getOwner().getAI().setAutoAttacking(isAutoAttacking);
  577. }
  578. return;
  579. }
  580. _clientAutoAttacking = isAutoAttacking;
  581. }
  582. /**
  583. * Start the actor Auto Attack client side by sending Server->Client packet AutoAttackStart <I>(broadcast)</I>.<br>
  584. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  585. */
  586. public void clientStartAutoAttack()
  587. {
  588. if (_actor instanceof L2Summon)
  589. {
  590. L2Summon summon = (L2Summon) _actor;
  591. if (summon.getOwner() != null)
  592. {
  593. summon.getOwner().getAI().clientStartAutoAttack();
  594. }
  595. return;
  596. }
  597. if (!isAutoAttacking())
  598. {
  599. if (_actor.isPlayer() && _actor.hasSummon())
  600. {
  601. _actor.getSummon().broadcastPacket(new AutoAttackStart(_actor.getSummon().getObjectId()));
  602. }
  603. // Send a Server->Client packet AutoAttackStart to the actor and all L2PcInstance in its _knownPlayers
  604. _actor.broadcastPacket(new AutoAttackStart(_actor.getObjectId()));
  605. setAutoAttacking(true);
  606. }
  607. AttackStanceTaskManager.getInstance().addAttackStanceTask(_actor);
  608. }
  609. /**
  610. * Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop <I>(broadcast)</I>.<br>
  611. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  612. */
  613. public void clientStopAutoAttack()
  614. {
  615. if (_actor instanceof L2Summon)
  616. {
  617. L2Summon summon = (L2Summon) _actor;
  618. if (summon.getOwner() != null)
  619. {
  620. summon.getOwner().getAI().clientStopAutoAttack();
  621. }
  622. return;
  623. }
  624. if (_actor instanceof L2PcInstance)
  625. {
  626. if (!AttackStanceTaskManager.getInstance().hasAttackStanceTask(_actor) && isAutoAttacking())
  627. {
  628. AttackStanceTaskManager.getInstance().addAttackStanceTask(_actor);
  629. }
  630. }
  631. else if (isAutoAttacking())
  632. {
  633. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  634. setAutoAttacking(false);
  635. }
  636. }
  637. /**
  638. * Kill the actor client side by sending Server->Client packet AutoAttackStop, StopMove/StopRotation, Die <I>(broadcast)</I>.<br>
  639. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  640. */
  641. protected void clientNotifyDead()
  642. {
  643. // Send a Server->Client packet Die to the actor and all L2PcInstance in its _knownPlayers
  644. Die msg = new Die(_actor);
  645. _actor.broadcastPacket(msg);
  646. // Init AI
  647. _intention = AI_INTENTION_IDLE;
  648. _target = null;
  649. _castTarget = null;
  650. _attackTarget = null;
  651. // Cancel the follow task if necessary
  652. stopFollow();
  653. }
  654. /**
  655. * Update the state of this actor client side by sending Server->Client packet MoveToPawn/CharMoveToLocation and AutoAttackStart to the L2PcInstance player.<br>
  656. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT>
  657. * @param player The L2PcIstance to notify with state of this L2Character
  658. */
  659. public void describeStateToPlayer(L2PcInstance player)
  660. {
  661. if (getActor().isVisibleFor(player))
  662. {
  663. if (_clientMoving)
  664. {
  665. if ((_clientMovingToPawnOffset != 0) && (_followTarget != null))
  666. {
  667. // Send a Server->Client packet MoveToPawn to the actor and all L2PcInstance in its _knownPlayers
  668. player.sendPacket(new MoveToPawn(_actor, _followTarget, _clientMovingToPawnOffset));
  669. }
  670. else
  671. {
  672. // Send a Server->Client packet CharMoveToLocation to the actor and all L2PcInstance in its _knownPlayers
  673. player.sendPacket(new MoveToLocation(_actor));
  674. }
  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. }