AbstractAI.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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.<BR><BR>
  133. */
  134. public L2Character getActor()
  135. {
  136. return _actor;
  137. }
  138. /**
  139. * Return the current Intention.<BR><BR>
  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.<BR><BR>
  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.<BR><BR>
  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. *
  475. */
  476. protected void moveToPawn(L2Object pawn, int offset)
  477. {
  478. // Check if actor can move
  479. if (!_actor.isMovementDisabled())
  480. {
  481. if (offset < 10)
  482. offset = 10;
  483. // prevent possible extra calls to this function (there is none?),
  484. // also don't send movetopawn packets too often
  485. boolean sendPacket = true;
  486. if (_clientMoving && _target == pawn)
  487. {
  488. if (_clientMovingToPawnOffset == offset)
  489. {
  490. if (GameTimeController.getGameTicks() < _moveToPawnTimeout)
  491. return;
  492. sendPacket = false;
  493. }
  494. else if (_actor.isOnGeodataPath())
  495. {
  496. // minimum time to calculate new route is 2 seconds
  497. if (GameTimeController.getGameTicks() < (_moveToPawnTimeout + 10))
  498. return;
  499. }
  500. }
  501. // Set AI movement data
  502. _clientMoving = true;
  503. _clientMovingToPawnOffset = offset;
  504. _target = pawn;
  505. _moveToPawnTimeout = GameTimeController.getGameTicks();
  506. _moveToPawnTimeout += 1000 / GameTimeController.MILLIS_IN_TICK;
  507. if (pawn == null || _accessor == null)
  508. return;
  509. // Calculate movement data for a move to location action and add the actor to movingObjects of GameTimeController
  510. _accessor.moveTo(pawn.getX(), pawn.getY(), pawn.getZ(), offset);
  511. if (!_actor.isMoving())
  512. {
  513. _actor.sendPacket(ActionFailed.STATIC_PACKET);
  514. return;
  515. }
  516. // Send a Server->Client packet MoveToPawn/CharMoveToLocation to the actor and all L2PcInstance in its _knownPlayers
  517. if (pawn instanceof L2Character)
  518. {
  519. if (_actor.isOnGeodataPath())
  520. {
  521. _actor.broadcastPacket(new MoveToLocation(_actor));
  522. _clientMovingToPawnOffset = 0;
  523. }
  524. else if (sendPacket) // don't repeat unnecessarily
  525. _actor.broadcastPacket(new MoveToPawn(_actor, (L2Character) pawn, offset));
  526. }
  527. else
  528. _actor.broadcastPacket(new MoveToLocation(_actor));
  529. }
  530. else
  531. {
  532. _actor.sendPacket(ActionFailed.STATIC_PACKET);
  533. }
  534. }
  535. /**
  536. * Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation <I>(broadcast)</I>.<BR><BR>
  537. *
  538. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT><BR><BR>
  539. *
  540. */
  541. protected void moveTo(int x, int y, int z)
  542. {
  543. // Chek if actor can move
  544. if (!_actor.isMovementDisabled())
  545. {
  546. // Set AI movement data
  547. _clientMoving = true;
  548. _clientMovingToPawnOffset = 0;
  549. // Calculate movement data for a move to location action and add the actor to movingObjects of GameTimeController
  550. _accessor.moveTo(x, y, z);
  551. // Send a Server->Client packet CharMoveToLocation to the actor and all L2PcInstance in its _knownPlayers
  552. MoveToLocation msg = new MoveToLocation(_actor);
  553. _actor.broadcastPacket(msg);
  554. }
  555. else
  556. {
  557. _actor.sendPacket(ActionFailed.STATIC_PACKET);
  558. }
  559. }
  560. /**
  561. * Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation <I>(broadcast)</I>.<BR><BR>
  562. *
  563. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT><BR><BR>
  564. *
  565. */
  566. protected void clientStopMoving(L2CharPosition pos)
  567. {
  568. /*
  569. if (Config.DEBUG)
  570. _log.warning("clientStopMoving();");
  571. */
  572. // Stop movement of the L2Character
  573. if (_actor.isMoving())
  574. _accessor.stopMove(pos);
  575. _clientMovingToPawnOffset = 0;
  576. if (_clientMoving || pos != null)
  577. {
  578. _clientMoving = false;
  579. // Send a Server->Client packet StopMove to the actor and all L2PcInstance in its _knownPlayers
  580. StopMove msg = new StopMove(_actor);
  581. _actor.broadcastPacket(msg);
  582. if (pos != null)
  583. {
  584. // Send a Server->Client packet StopRotation to the actor and all L2PcInstance in its _knownPlayers
  585. StopRotation sr = new StopRotation(_actor.getObjectId(), pos.heading, 0);
  586. _actor.sendPacket(sr);
  587. _actor.broadcastPacket(sr);
  588. }
  589. }
  590. }
  591. // Client has already arrived to target, no need to force StopMove packet
  592. protected void clientStoppedMoving()
  593. {
  594. if (_clientMovingToPawnOffset > 0) // movetoPawn needs to be stopped
  595. {
  596. _clientMovingToPawnOffset = 0;
  597. StopMove msg = new StopMove(_actor);
  598. _actor.broadcastPacket(msg);
  599. }
  600. _clientMoving = false;
  601. }
  602. public boolean isAutoAttacking()
  603. {
  604. return _clientAutoAttacking;
  605. }
  606. public void setAutoAttacking(boolean isAutoAttacking)
  607. {
  608. if (_actor instanceof L2Summon)
  609. {
  610. L2Summon summon = (L2Summon) _actor;
  611. if (summon.getOwner() != null)
  612. summon.getOwner().getAI().setAutoAttacking(isAutoAttacking);
  613. return;
  614. }
  615. _clientAutoAttacking = isAutoAttacking;
  616. }
  617. /**
  618. * Start the actor Auto Attack client side by sending Server->Client packet AutoAttackStart <I>(broadcast)</I>.<BR><BR>
  619. *
  620. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT><BR><BR>
  621. *
  622. */
  623. public void clientStartAutoAttack()
  624. {
  625. if (_actor instanceof L2Summon)
  626. {
  627. L2Summon summon = (L2Summon) _actor;
  628. if (summon.getOwner() != null)
  629. summon.getOwner().getAI().clientStartAutoAttack();
  630. return;
  631. }
  632. if (!isAutoAttacking())
  633. {
  634. if (_actor instanceof L2PcInstance && ((L2PcInstance)_actor).getPet() != null)
  635. ((L2PcInstance)_actor).getPet().broadcastPacket(new AutoAttackStart(((L2PcInstance)_actor).getPet().getObjectId()));
  636. // Send a Server->Client packet AutoAttackStart to the actor and all L2PcInstance in its _knownPlayers
  637. _actor.broadcastPacket(new AutoAttackStart(_actor.getObjectId()));
  638. setAutoAttacking(true);
  639. }
  640. AttackStanceTaskManager.getInstance().addAttackStanceTask(_actor);
  641. }
  642. /**
  643. * Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop <I>(broadcast)</I>.<BR><BR>
  644. *
  645. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT><BR><BR>
  646. *
  647. */
  648. public void clientStopAutoAttack()
  649. {
  650. if (_actor instanceof L2Summon)
  651. {
  652. L2Summon summon = (L2Summon) _actor;
  653. if (summon.getOwner() != null)
  654. summon.getOwner().getAI().clientStopAutoAttack();
  655. return;
  656. }
  657. if (_actor instanceof L2PcInstance)
  658. {
  659. if (!AttackStanceTaskManager.getInstance().getAttackStanceTask(_actor) && isAutoAttacking())
  660. AttackStanceTaskManager.getInstance().addAttackStanceTask(_actor);
  661. }
  662. else if (isAutoAttacking())
  663. {
  664. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  665. setAutoAttacking(false);
  666. }
  667. }
  668. /**
  669. * Kill the actor client side by sending Server->Client packet AutoAttackStop, StopMove/StopRotation, Die <I>(broadcast)</I>.<BR><BR>
  670. *
  671. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT><BR><BR>
  672. *
  673. */
  674. protected void clientNotifyDead()
  675. {
  676. // Send a Server->Client packet Die to the actor and all L2PcInstance in its _knownPlayers
  677. Die msg = new Die(_actor);
  678. _actor.broadcastPacket(msg);
  679. // Init AI
  680. _intention = AI_INTENTION_IDLE;
  681. _target = null;
  682. _castTarget = null;
  683. _attackTarget = null;
  684. // Cancel the follow task if necessary
  685. stopFollow();
  686. }
  687. /**
  688. * Update the state of this actor client side by sending Server->Client packet MoveToPawn/CharMoveToLocation and AutoAttackStart to the L2PcInstance player.<BR><BR>
  689. *
  690. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT><BR><BR>
  691. *
  692. * @param player The L2PcIstance to notify with state of this L2Character
  693. *
  694. */
  695. public void describeStateToPlayer(L2PcInstance player)
  696. {
  697. if (_clientMoving)
  698. {
  699. if (_clientMovingToPawnOffset != 0 && _followTarget != null)
  700. {
  701. // Send a Server->Client packet MoveToPawn to the actor and all L2PcInstance in its _knownPlayers
  702. MoveToPawn msg = new MoveToPawn(_actor, _followTarget, _clientMovingToPawnOffset);
  703. player.sendPacket(msg);
  704. }
  705. else
  706. {
  707. // Send a Server->Client packet CharMoveToLocation to the actor and all L2PcInstance in its _knownPlayers
  708. MoveToLocation msg = new MoveToLocation(_actor);
  709. player.sendPacket(msg);
  710. }
  711. }
  712. }
  713. /**
  714. * Create and Launch an AI Follow Task to execute every 1s.<BR><BR>
  715. *
  716. * @param target The L2Character to follow
  717. *
  718. */
  719. public synchronized void startFollow(L2Character target)
  720. {
  721. if (_followTask != null)
  722. {
  723. _followTask.cancel(false);
  724. _followTask = null;
  725. }
  726. // Create and Launch an AI Follow Task to execute every 1s
  727. _followTarget = target;
  728. _followTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new FollowTask(), 5, FOLLOW_INTERVAL);
  729. }
  730. /**
  731. * Create and Launch an AI Follow Task to execute every 0.5s, following at specified range.<BR><BR>
  732. *
  733. * @param target The L2Character to follow
  734. *
  735. */
  736. public synchronized void startFollow(L2Character target, int range)
  737. {
  738. if (_followTask != null)
  739. {
  740. _followTask.cancel(false);
  741. _followTask = null;
  742. }
  743. _followTarget = target;
  744. _followTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new FollowTask(range), 5, ATTACK_FOLLOW_INTERVAL);
  745. }
  746. /**
  747. * Stop an AI Follow Task.<BR><BR>
  748. */
  749. public synchronized void stopFollow()
  750. {
  751. if (_followTask != null)
  752. {
  753. // Stop the Follow Task
  754. _followTask.cancel(false);
  755. _followTask = null;
  756. }
  757. _followTarget = null;
  758. }
  759. protected L2Character getFollowTarget()
  760. {
  761. return _followTarget;
  762. }
  763. protected L2Object getTarget()
  764. {
  765. return _target;
  766. }
  767. protected void setTarget(L2Object target)
  768. {
  769. _target = target;
  770. }
  771. /**
  772. * Stop all Ai tasks and futures.
  773. */
  774. public void stopAITask()
  775. {
  776. stopFollow();
  777. }
  778. @Override
  779. public String toString()
  780. {
  781. if (_actor == null)
  782. return "Actor: null";
  783. return "Actor: " + _actor;
  784. }
  785. }