AbstractAI.java 27 KB

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