AbstractAI.java 26 KB

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