AbstractAI.java 27 KB

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