AbstractAI.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 net.sf.l2j.gameserver.ai;
  16. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  17. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_FOLLOW;
  18. import static net.sf.l2j.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 net.sf.l2j.gameserver.GameTimeController;
  23. import net.sf.l2j.gameserver.ThreadPoolManager;
  24. import net.sf.l2j.gameserver.model.L2CharPosition;
  25. import net.sf.l2j.gameserver.model.L2Character;
  26. import net.sf.l2j.gameserver.model.L2Object;
  27. import net.sf.l2j.gameserver.model.L2Skill;
  28. import net.sf.l2j.gameserver.model.L2Summon;
  29. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  30. import net.sf.l2j.gameserver.network.serverpackets.ActionFailed;
  31. import net.sf.l2j.gameserver.network.serverpackets.AutoAttackStart;
  32. import net.sf.l2j.gameserver.network.serverpackets.AutoAttackStop;
  33. import net.sf.l2j.gameserver.network.serverpackets.Die;
  34. import net.sf.l2j.gameserver.network.serverpackets.MoveToLocation;
  35. import net.sf.l2j.gameserver.network.serverpackets.MoveToLocationInVehicle;
  36. import net.sf.l2j.gameserver.network.serverpackets.MoveToPawn;
  37. import net.sf.l2j.gameserver.network.serverpackets.StopMove;
  38. import net.sf.l2j.gameserver.network.serverpackets.StopRotation;
  39. import net.sf.l2j.gameserver.taskmanager.AttackStanceTaskManager;
  40. /**
  41. * Mother class of all objects AI in the world.<BR><BR>
  42. *
  43. * AbastractAI :<BR><BR>
  44. * <li>L2CharacterAI</li><BR><BR>
  45. *
  46. */
  47. abstract class AbstractAI implements Ctrl
  48. {
  49. protected static final Logger _log = Logger.getLogger(AbstractAI.class.getName());
  50. class FollowTask implements Runnable
  51. {
  52. protected int _range = 60;
  53. public FollowTask()
  54. {
  55. }
  56. public FollowTask(int range)
  57. {
  58. _range = range;
  59. }
  60. public void run()
  61. {
  62. try
  63. {
  64. if (_followTask == null)
  65. return;
  66. L2Character followTarget = _followTarget; // copy to prevent NPE
  67. if (followTarget == null)
  68. {
  69. if (_actor instanceof L2Summon)
  70. ((L2Summon) _actor).setFollowStatus(false);
  71. setIntention(AI_INTENTION_IDLE);
  72. return;
  73. }
  74. if (!_actor.isInsideRadius(followTarget, _range, true, false))
  75. {
  76. if (!_actor.isInsideRadius(followTarget, 3000, true, false))
  77. {
  78. // if the target is too far (maybe also teleported)
  79. if (_actor instanceof L2Summon)
  80. ((L2Summon) _actor).setFollowStatus(false);
  81. setIntention(AI_INTENTION_IDLE);
  82. return;
  83. }
  84. moveToPawn(followTarget, _range);
  85. }
  86. }
  87. catch (Exception e)
  88. {
  89. _log.log(Level.WARNING, "", e);
  90. }
  91. }
  92. }
  93. /** The character that this AI manages */
  94. protected final L2Character _actor;
  95. /** An accessor for private methods of the actor */
  96. protected final L2Character.AIAccessor _accessor;
  97. /** Current long-term intention */
  98. protected CtrlIntention _intention = AI_INTENTION_IDLE;
  99. /** Current long-term intention parameter */
  100. protected Object _intentionArg0 = null;
  101. /** Current long-term intention parameter */
  102. protected Object _intentionArg1 = null;
  103. /** Flags about client's state, in order to know which messages to send */
  104. protected boolean _clientMoving;
  105. /** Flags about client's state, in order to know which messages to send */
  106. protected boolean _clientAutoAttacking;
  107. /** Flags about client's state, in order to know which messages to send */
  108. protected int _clientMovingToPawnOffset;
  109. /** Different targets this AI maintains */
  110. private L2Object _target;
  111. private L2Character _castTarget;
  112. protected L2Character _attackTarget;
  113. protected L2Character _followTarget;
  114. /** The skill we are currently casting by INTENTION_CAST */
  115. L2Skill _skill;
  116. /** Different internal state flags */
  117. private int _moveToPawnTimeout;
  118. protected Future<?> _followTask = null;
  119. private static final int FOLLOW_INTERVAL = 1000;
  120. private static final int ATTACK_FOLLOW_INTERVAL = 500;
  121. /**
  122. * Constructor of AbstractAI.<BR><BR>
  123. *
  124. * @param accessor The AI accessor of the L2Character
  125. *
  126. */
  127. protected AbstractAI(L2Character.AIAccessor accessor)
  128. {
  129. _accessor = accessor;
  130. // Get the L2Character managed by this Accessor AI
  131. _actor = accessor.getActor();
  132. }
  133. /**
  134. * Return the L2Character managed by this Accessor AI.<BR><BR>
  135. */
  136. public L2Character getActor()
  137. {
  138. return _actor;
  139. }
  140. /**
  141. * Return the current Intention.<BR><BR>
  142. */
  143. public CtrlIntention getIntention()
  144. {
  145. return _intention;
  146. }
  147. protected synchronized void setCastTarget(L2Character target)
  148. {
  149. _castTarget = target;
  150. }
  151. /**
  152. * Return the current cast target.<BR><BR>
  153. */
  154. public L2Character getCastTarget()
  155. {
  156. return _castTarget;
  157. }
  158. protected synchronized void setAttackTarget(L2Character target)
  159. {
  160. _attackTarget = target;
  161. }
  162. /**
  163. * Return current attack target.<BR><BR>
  164. */
  165. public L2Character getAttackTarget()
  166. {
  167. return _attackTarget;
  168. }
  169. /**
  170. * Set the Intention of this AbstractAI.<BR><BR>
  171. *
  172. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method is USED by AI classes</B></FONT><BR><BR>
  173. *
  174. * <B><U> Overridden in </U> : </B><BR>
  175. * <B>L2AttackableAI</B> : Create an AI Task executed every 1s (if necessary)<BR>
  176. * <B>L2PlayerAI</B> : Stores the current AI intention parameters to later restore it if necessary<BR><BR>
  177. *
  178. * @param intention The new Intention to set to the AI
  179. * @param arg0 The first parameter of the Intention
  180. * @param arg1 The second parameter of the Intention
  181. *
  182. */
  183. synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
  184. {
  185. /*
  186. if (Config.DEBUG)
  187. _log.warning("AbstractAI: changeIntention -> " + intention + " " + arg0 + " " + arg1);
  188. */
  189. _intention = intention;
  190. _intentionArg0 = arg0;
  191. _intentionArg1 = arg1;
  192. }
  193. /**
  194. * Launch the L2CharacterAI onIntention method corresponding to the new Intention.<BR><BR>
  195. *
  196. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Stop the FOLLOW mode if necessary</B></FONT><BR><BR>
  197. *
  198. * @param intention The new Intention to set to the AI
  199. *
  200. */
  201. public final void setIntention(CtrlIntention intention)
  202. {
  203. setIntention(intention, null, null);
  204. }
  205. /**
  206. * Launch the L2CharacterAI onIntention method corresponding to the new Intention.<BR><BR>
  207. *
  208. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Stop the FOLLOW mode if necessary</B></FONT><BR><BR>
  209. *
  210. * @param intention The new Intention to set to the AI
  211. * @param arg0 The first parameter of the Intention (optional target)
  212. *
  213. */
  214. public final void setIntention(CtrlIntention intention, Object arg0)
  215. {
  216. setIntention(intention, arg0, null);
  217. }
  218. /**
  219. * Launch the L2CharacterAI onIntention method corresponding to the new Intention.<BR><BR>
  220. *
  221. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Stop the FOLLOW mode if necessary</B></FONT><BR><BR>
  222. *
  223. * @param intention The new Intention to set to the AI
  224. * @param arg0 The first parameter of the Intention (optional target)
  225. * @param arg1 The second parameter of the Intention (optional target)
  226. *
  227. */
  228. /*
  229. public final void informAIIntention(CtrlIntention intent, Object arg0) {
  230. ThreadPoolManager.getInstance().executeAi(new InformAIMsg(this, intent, arg0));
  231. }
  232. public final void informAIIntention(CtrlIntention intent) {
  233. ThreadPoolManager.getInstance().executeAi(new InformAIMsg(this, intent, null));
  234. }
  235. public class InformAIMsg implements Runnable {
  236. private AbstractAI _ai;
  237. private CtrlIntention _intent;
  238. private Object _arg0;
  239. public InformAIMsg(AbstractAI ai, CtrlIntention intention, Object arg0) {
  240. _ai=ai;
  241. _intent = intention;
  242. _arg0 = arg0;
  243. }
  244. public final void run() {
  245. _ai.setIntention(_intent, _arg0, null);
  246. }
  247. }
  248. */
  249. public final void setIntention(CtrlIntention intention, Object arg0, Object arg1)
  250. {
  251. /*
  252. if (Config.DEBUG)
  253. _log.warning("AbstractAI: setIntention -> " + intention + " " + arg0 + " " + arg1);
  254. */
  255. // Stop the follow mode if necessary
  256. if (intention != AI_INTENTION_FOLLOW && intention != AI_INTENTION_ATTACK)
  257. stopFollow();
  258. // Launch the onIntention method of the L2CharacterAI corresponding to the new Intention
  259. switch (intention)
  260. {
  261. case AI_INTENTION_IDLE:
  262. onIntentionIdle();
  263. break;
  264. case AI_INTENTION_ACTIVE:
  265. onIntentionActive();
  266. break;
  267. case AI_INTENTION_REST:
  268. onIntentionRest();
  269. break;
  270. case AI_INTENTION_ATTACK:
  271. onIntentionAttack((L2Character) arg0);
  272. break;
  273. case AI_INTENTION_CAST:
  274. onIntentionCast((L2Skill) arg0, (L2Object) arg1);
  275. break;
  276. case AI_INTENTION_MOVE_TO:
  277. onIntentionMoveTo((L2CharPosition) arg0);
  278. break;
  279. case AI_INTENTION_MOVE_TO_IN_A_BOAT:
  280. onIntentionMoveToInABoat((L2CharPosition) arg0, (L2CharPosition) arg1);
  281. break;
  282. case AI_INTENTION_FOLLOW:
  283. onIntentionFollow((L2Character) arg0);
  284. break;
  285. case AI_INTENTION_PICK_UP:
  286. onIntentionPickUp((L2Object) arg0);
  287. break;
  288. case AI_INTENTION_INTERACT:
  289. onIntentionInteract((L2Object) arg0);
  290. break;
  291. }
  292. }
  293. /**
  294. * Launch the L2CharacterAI onEvt method corresponding to the Event.<BR><BR>
  295. *
  296. * <FONT COLOR=#FF0000><B> <U>Caution</U> : The current general intention won't be change
  297. * (ex : If the character attack and is stunned, he will attack again after the stunned period)</B></FONT><BR><BR>
  298. *
  299. * @param evt The event whose the AI must be notified
  300. *
  301. */
  302. public final void notifyEvent(CtrlEvent evt)
  303. {
  304. notifyEvent(evt, null, null);
  305. }
  306. /**
  307. * Launch the L2CharacterAI onEvt method corresponding to the Event.<BR><BR>
  308. *
  309. * <FONT COLOR=#FF0000><B> <U>Caution</U> : The current general intention won't be change
  310. * (ex : If the character attack and is stunned, he will attack again after the stunned period)</B></FONT><BR><BR>
  311. *
  312. * @param evt The event whose the AI must be notified
  313. * @param arg0 The first parameter of the Event (optional target)
  314. *
  315. */
  316. public final void notifyEvent(CtrlEvent evt, Object arg0)
  317. {
  318. notifyEvent(evt, arg0, null);
  319. }
  320. /*
  321. public final void informAIEvent(CtrlEvent evt) {
  322. ThreadPoolManager.getInstance().executeAi(new InformAIEvent(this, evt, null, null));
  323. }
  324. public final void informAIEvent(CtrlEvent evt, Object arg0) {
  325. ThreadPoolManager.getInstance().executeAi(new InformAIEvent(this, evt, arg0, null));
  326. }
  327. public final void informAIEvent(CtrlEvent evt, Object arg0, Object arg1) {
  328. ThreadPoolManager.getInstance().executeAi(new InformAIEvent(this, evt, arg0, arg1));
  329. }
  330. public class InformAIEvent implements Runnable {
  331. private AbstractAI _ai;
  332. private CtrlEvent _evt;
  333. private Object _arg0, _arg1;
  334. public InformAIEvent(AbstractAI ai, CtrlEvent evt, Object arg0, Object arg1) {
  335. _ai=ai;
  336. _evt = evt;
  337. _arg0 = arg0;
  338. _arg1 = arg1;
  339. }
  340. public final void run() {
  341. _ai.notifyEvent(_evt, _arg0, _arg1);
  342. }
  343. }
  344. */
  345. /**
  346. * Launch the L2CharacterAI onEvt method corresponding to the Event.<BR><BR>
  347. *
  348. * <FONT COLOR=#FF0000><B> <U>Caution</U> : The current general intention won't be change
  349. * (ex : If the character attack and is stunned, he will attack again after the stunned period)</B></FONT><BR><BR>
  350. *
  351. * @param evt The event whose the AI must be notified
  352. * @param arg0 The first parameter of the Event (optional target)
  353. * @param arg1 The second parameter of the Event (optional target)
  354. *
  355. */
  356. public final void notifyEvent(CtrlEvent evt, Object arg0, Object arg1)
  357. {
  358. if (!_actor.isVisible() || !_actor.hasAI())
  359. return;
  360. /*
  361. if (Config.DEBUG)
  362. _log.warning("AbstractAI: notifyEvent -> " + evt + " " + arg0 + " " + arg1);
  363. */
  364. switch (evt)
  365. {
  366. case EVT_THINK:
  367. onEvtThink();
  368. break;
  369. case EVT_ATTACKED:
  370. onEvtAttacked((L2Character) arg0);
  371. break;
  372. case EVT_AGGRESSION:
  373. onEvtAggression((L2Character) arg0, ((Number) arg1).intValue());
  374. break;
  375. case EVT_STUNNED:
  376. onEvtStunned((L2Character) arg0);
  377. break;
  378. case EVT_PARALYZED:
  379. onEvtParalyzed((L2Character) arg0);
  380. break;
  381. case EVT_SLEEPING:
  382. onEvtSleeping((L2Character) arg0);
  383. break;
  384. case EVT_ROOTED:
  385. onEvtRooted((L2Character) arg0);
  386. break;
  387. case EVT_CONFUSED:
  388. onEvtConfused((L2Character) arg0);
  389. break;
  390. case EVT_MUTED:
  391. onEvtMuted((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 onIntentionMoveToInABoat(L2CharPosition destination, L2CharPosition origin);
  437. protected abstract void onIntentionFollow(L2Character target);
  438. protected abstract void onIntentionPickUp(L2Object item);
  439. protected abstract void onIntentionInteract(L2Object object);
  440. protected abstract void onEvtThink();
  441. protected abstract void onEvtAttacked(L2Character attacker);
  442. protected abstract void onEvtAggression(L2Character target, int aggro);
  443. protected abstract void onEvtStunned(L2Character attacker);
  444. protected abstract void onEvtParalyzed(L2Character attacker);
  445. protected abstract void onEvtSleeping(L2Character attacker);
  446. protected abstract void onEvtRooted(L2Character attacker);
  447. protected abstract void onEvtConfused(L2Character attacker);
  448. protected abstract void onEvtMuted(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. protected void moveToInABoat(L2CharPosition destination, L2CharPosition origin)
  561. {
  562. // Check if actor can move
  563. if (!_actor.isMovementDisabled())
  564. {
  565. /* // Set AI movement data
  566. _client_moving = true;
  567. _client_moving_to_pawn_offset = 0;
  568. // Calculate movement data for a move to location action and add the actor to movingObjects of GameTimeController
  569. _accessor.moveTo(((L2PcInstance)_actor).getBoat().getX() - destination.x,((L2PcInstance)_actor).getBoat().getY()- destination.y,((L2PcInstance)_actor).getBoat().getZ() - destination.z);
  570. */
  571. // Send a Server->Client packet CharMoveToLocation to the actor and all L2PcInstance in its _knownPlayers
  572. //CharMoveToLocation msg = new CharMoveToLocation(_actor);
  573. if (((L2PcInstance) _actor).getBoat() != null)
  574. {
  575. MoveToLocationInVehicle msg = new MoveToLocationInVehicle(_actor, destination, origin);
  576. _actor.broadcastPacket(msg);
  577. }
  578. }
  579. else
  580. {
  581. _actor.sendPacket(ActionFailed.STATIC_PACKET);
  582. }
  583. }
  584. /**
  585. * Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation <I>(broadcast)</I>.<BR><BR>
  586. *
  587. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT><BR><BR>
  588. *
  589. */
  590. protected void clientStopMoving(L2CharPosition pos)
  591. {
  592. /*
  593. if (Config.DEBUG)
  594. _log.warning("clientStopMoving();");
  595. */
  596. // Stop movement of the L2Character
  597. if (_actor.isMoving())
  598. _accessor.stopMove(pos);
  599. _clientMovingToPawnOffset = 0;
  600. if (_clientMoving || pos != null)
  601. {
  602. _clientMoving = false;
  603. // Send a Server->Client packet StopMove to the actor and all L2PcInstance in its _knownPlayers
  604. StopMove msg = new StopMove(_actor);
  605. _actor.broadcastPacket(msg);
  606. if (pos != null)
  607. {
  608. // Send a Server->Client packet StopRotation to the actor and all L2PcInstance in its _knownPlayers
  609. StopRotation sr = new StopRotation(_actor, pos.heading);
  610. _actor.sendPacket(sr);
  611. _actor.broadcastPacket(sr);
  612. }
  613. }
  614. }
  615. // Client has already arrived to target, no need to force StopMove packet
  616. protected void clientStoppedMoving()
  617. {
  618. if (_clientMovingToPawnOffset > 0) // movetoPawn needs to be stopped
  619. {
  620. _clientMovingToPawnOffset = 0;
  621. StopMove msg = new StopMove(_actor);
  622. _actor.broadcastPacket(msg);
  623. }
  624. _clientMoving = false;
  625. }
  626. public boolean isAutoAttacking()
  627. {
  628. return _clientAutoAttacking;
  629. }
  630. public void setAutoAttacking(boolean isAutoAttacking)
  631. {
  632. _clientAutoAttacking = isAutoAttacking;
  633. }
  634. /**
  635. * Start the actor Auto Attack client side by sending Server->Client packet AutoAttackStart <I>(broadcast)</I>.<BR><BR>
  636. *
  637. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT><BR><BR>
  638. *
  639. */
  640. public void clientStartAutoAttack()
  641. {
  642. if (!isAutoAttacking())
  643. {
  644. // Send a Server->Client packet AutoAttackStart to the actor and all L2PcInstance in its _knownPlayers
  645. _actor.broadcastPacket(new AutoAttackStart(_actor.getObjectId()));
  646. setAutoAttacking(true);
  647. }
  648. AttackStanceTaskManager.getInstance().addAttackStanceTask(_actor);
  649. }
  650. /**
  651. * Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop <I>(broadcast)</I>.<BR><BR>
  652. *
  653. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT><BR><BR>
  654. *
  655. */
  656. public void clientStopAutoAttack()
  657. {
  658. if (_actor instanceof L2PcInstance)
  659. {
  660. if (!AttackStanceTaskManager.getInstance().getAttackStanceTask(_actor) && isAutoAttacking())
  661. AttackStanceTaskManager.getInstance().addAttackStanceTask(_actor);
  662. }
  663. else if (isAutoAttacking())
  664. {
  665. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  666. }
  667. setAutoAttacking(false);
  668. }
  669. /**
  670. * Kill the actor client side by sending Server->Client packet AutoAttackStop, StopMove/StopRotation, Die <I>(broadcast)</I>.<BR><BR>
  671. *
  672. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT><BR><BR>
  673. *
  674. */
  675. protected void clientNotifyDead()
  676. {
  677. // Send a Server->Client packet Die to the actor and all L2PcInstance in its _knownPlayers
  678. Die msg = new Die(_actor);
  679. _actor.broadcastPacket(msg);
  680. // Init AI
  681. _intention = AI_INTENTION_IDLE;
  682. _target = null;
  683. _castTarget = null;
  684. _attackTarget = null;
  685. // Cancel the follow task if necessary
  686. stopFollow();
  687. }
  688. /**
  689. * Update the state of this actor client side by sending Server->Client packet MoveToPawn/CharMoveToLocation and AutoAttackStart to the L2PcInstance player.<BR><BR>
  690. *
  691. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Low level function, used by AI subclasses</B></FONT><BR><BR>
  692. *
  693. * @param player The L2PcIstance to notify with state of this L2Character
  694. *
  695. */
  696. public void describeStateToPlayer(L2PcInstance player)
  697. {
  698. if (_clientMoving)
  699. {
  700. if (_clientMovingToPawnOffset != 0 && _followTarget != null)
  701. {
  702. // Send a Server->Client packet MoveToPawn to the actor and all L2PcInstance in its _knownPlayers
  703. MoveToPawn msg = new MoveToPawn(_actor, _followTarget, _clientMovingToPawnOffset);
  704. player.sendPacket(msg);
  705. }
  706. else
  707. {
  708. // Send a Server->Client packet CharMoveToLocation to the actor and all L2PcInstance in its _knownPlayers
  709. MoveToLocation msg = new MoveToLocation(_actor);
  710. player.sendPacket(msg);
  711. }
  712. }
  713. }
  714. /**
  715. * Create and Launch an AI Follow Task to execute every 1s.<BR><BR>
  716. *
  717. * @param target The L2Character to follow
  718. *
  719. */
  720. public synchronized void startFollow(L2Character target)
  721. {
  722. if (_followTask != null)
  723. {
  724. _followTask.cancel(false);
  725. _followTask = null;
  726. }
  727. // Create and Launch an AI Follow Task to execute every 1s
  728. _followTarget = target;
  729. _followTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new FollowTask(), 5, FOLLOW_INTERVAL);
  730. }
  731. /**
  732. * Create and Launch an AI Follow Task to execute every 0.5s, following at specified range.<BR><BR>
  733. *
  734. * @param target The L2Character to follow
  735. *
  736. */
  737. public synchronized void startFollow(L2Character target, int range)
  738. {
  739. if (_followTask != null)
  740. {
  741. _followTask.cancel(false);
  742. _followTask = null;
  743. }
  744. _followTarget = target;
  745. _followTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new FollowTask(range), 5, ATTACK_FOLLOW_INTERVAL);
  746. }
  747. /**
  748. * Stop an AI Follow Task.<BR><BR>
  749. */
  750. public synchronized void stopFollow()
  751. {
  752. if (_followTask != null)
  753. {
  754. // Stop the Follow Task
  755. _followTask.cancel(false);
  756. _followTask = null;
  757. }
  758. _followTarget = null;
  759. }
  760. protected L2Character getFollowTarget()
  761. {
  762. return _followTarget;
  763. }
  764. protected L2Object getTarget()
  765. {
  766. return _target;
  767. }
  768. protected synchronized void setTarget(L2Object target)
  769. {
  770. _target = target;
  771. }
  772. }