AbstractAI.java 28 KB

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