AbstractAI.java 27 KB

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