AbstractAI.java 26 KB

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