L2CharacterAI.java 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2, or (at your option)
  5. * any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. * 02111-1307, USA.
  16. *
  17. * http://www.gnu.org/copyleft/gpl.html
  18. */
  19. package net.sf.l2j.gameserver.ai;
  20. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
  21. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  22. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_CAST;
  23. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_FOLLOW;
  24. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  25. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_INTERACT;
  26. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_MOVE_TO;
  27. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_PICK_UP;
  28. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_REST;
  29. import net.sf.l2j.Config;
  30. import net.sf.l2j.gameserver.Universe;
  31. import net.sf.l2j.gameserver.model.L2Attackable;
  32. import net.sf.l2j.gameserver.model.L2CharPosition;
  33. import net.sf.l2j.gameserver.model.L2Character;
  34. import net.sf.l2j.gameserver.model.L2Object;
  35. import net.sf.l2j.gameserver.model.L2Skill;
  36. import net.sf.l2j.gameserver.model.actor.instance.L2BoatInstance;
  37. import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
  38. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  39. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  40. import net.sf.l2j.gameserver.model.actor.instance.L2PlayableInstance;
  41. import net.sf.l2j.gameserver.serverpackets.AutoAttackStop;
  42. import net.sf.l2j.gameserver.taskmanager.AttackStanceTaskManager;
  43. /**
  44. * This class manages AI of L2Character.<BR><BR>
  45. *
  46. * L2CharacterAI :<BR><BR>
  47. * <li>L2AttackableAI</li>
  48. * <li>L2DoorAI</li>
  49. * <li>L2PlayerAI</li>
  50. * <li>L2SummonAI</li><BR><BR>
  51. *
  52. */
  53. public class L2CharacterAI extends AbstractAI
  54. {
  55. @Override
  56. protected void onEvtAttacked(L2Character attacker)
  57. {
  58. clientStartAutoAttack();
  59. }
  60. /**
  61. * Constructor of L2CharacterAI.<BR><BR>
  62. *
  63. * @param accessor The AI accessor of the L2Character
  64. *
  65. */
  66. public L2CharacterAI(L2Character.AIAccessor accessor)
  67. {
  68. super(accessor);
  69. }
  70. /**
  71. * Manage the Idle Intention : Stop Attack, Movement and Stand Up the actor.<BR><BR>
  72. *
  73. * <B><U> Actions</U> :</B><BR><BR>
  74. * <li>Set the AI Intention to AI_INTENTION_IDLE </li>
  75. * <li>Init cast and attack target </li>
  76. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast) </li>
  77. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast) </li>
  78. * <li>Stand up the actor server side AND client side by sending Server->Client packet ChangeWaitType (broadcast) </li><BR><BR>
  79. *
  80. */
  81. @Override
  82. protected void onIntentionIdle()
  83. {
  84. // Set the AI Intention to AI_INTENTION_IDLE
  85. changeIntention(AI_INTENTION_IDLE, null, null);
  86. // Init cast and attack target
  87. setCastTarget(null);
  88. setAttackTarget(null);
  89. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  90. clientStopMoving(null);
  91. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  92. clientStopAutoAttack();
  93. }
  94. /**
  95. * Manage the Active Intention : Stop Attack, Movement and Launch Think Event.<BR><BR>
  96. *
  97. * <B><U> Actions</U> : <I>if the Intention is not already Active</I></B><BR><BR>
  98. * <li>Set the AI Intention to AI_INTENTION_ACTIVE </li>
  99. * <li>Init cast and attack target </li>
  100. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast) </li>
  101. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast) </li>
  102. * <li>Launch the Think Event </li><BR><BR>
  103. *
  104. */
  105. @Override
  106. protected void onIntentionActive()
  107. {
  108. // Check if the Intention is not already Active
  109. if (getIntention() != AI_INTENTION_ACTIVE)
  110. {
  111. // Set the AI Intention to AI_INTENTION_ACTIVE
  112. changeIntention(AI_INTENTION_ACTIVE, null, null);
  113. // Init cast and attack target
  114. setCastTarget(null);
  115. setAttackTarget(null);
  116. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  117. clientStopMoving(null);
  118. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  119. clientStopAutoAttack();
  120. // Also enable random animations for this L2Character if allowed
  121. // This is only for mobs - town npcs are handled in their constructor
  122. if (_actor instanceof L2Attackable)
  123. ((L2NpcInstance)_actor).startRandomAnimationTimer();
  124. // Launch the Think Event
  125. onEvtThink();
  126. }
  127. }
  128. /**
  129. * Manage the Rest Intention.<BR><BR>
  130. *
  131. * <B><U> Actions</U> : </B><BR><BR>
  132. * <li>Set the AI Intention to AI_INTENTION_IDLE </li><BR><BR>
  133. *
  134. */
  135. @Override
  136. protected void onIntentionRest()
  137. {
  138. // Set the AI Intention to AI_INTENTION_IDLE
  139. setIntention(AI_INTENTION_IDLE);
  140. }
  141. /**
  142. * Manage the Attack Intention : Stop current Attack (if necessary), Start a new Attack and Launch Think Event.<BR><BR>
  143. *
  144. * <B><U> Actions</U> : </B><BR><BR>
  145. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast) </li>
  146. * <li>Set the Intention of this AI to AI_INTENTION_ATTACK </li>
  147. * <li>Set or change the AI attack target </li>
  148. * <li>Start the actor Auto Attack client side by sending Server->Client packet AutoAttackStart (broadcast) </li>
  149. * <li>Launch the Think Event </li><BR><BR>
  150. *
  151. *
  152. * <B><U> Overridden in</U> :</B><BR><BR>
  153. * <li>L2AttackableAI : Calculate attack timeout</li><BR><BR>
  154. *
  155. */
  156. @Override
  157. protected void onIntentionAttack(L2Character target)
  158. {
  159. if (target == null)
  160. {
  161. clientActionFailed();
  162. return;
  163. }
  164. if (getIntention() == AI_INTENTION_REST)
  165. {
  166. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  167. clientActionFailed();
  168. return;
  169. }
  170. if (_actor.isAllSkillsDisabled() || _actor.isAfraid())
  171. {
  172. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  173. clientActionFailed();
  174. return;
  175. }
  176. // Check if the Intention is already AI_INTENTION_ATTACK
  177. if (getIntention() == AI_INTENTION_ATTACK)
  178. {
  179. // Check if the AI already targets the L2Character
  180. if (getAttackTarget() != target)
  181. {
  182. // Set the AI attack target (change target)
  183. setAttackTarget(target);
  184. stopFollow();
  185. // Launch the Think Event
  186. notifyEvent(CtrlEvent.EVT_THINK, null);
  187. }
  188. else
  189. clientActionFailed(); // else client freezes until cancel target
  190. }
  191. else
  192. {
  193. // Set the Intention of this AbstractAI to AI_INTENTION_ATTACK
  194. changeIntention(AI_INTENTION_ATTACK, target, null);
  195. // Set the AI attack target
  196. setAttackTarget(target);
  197. stopFollow();
  198. // Launch the Think Event
  199. notifyEvent(CtrlEvent.EVT_THINK, null);
  200. }
  201. }
  202. /**
  203. * Manage the Cast Intention : Stop current Attack, Init the AI in order to cast and Launch Think Event.<BR><BR>
  204. *
  205. * <B><U> Actions</U> : </B><BR><BR>
  206. * <li>Set the AI cast target </li>
  207. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast) </li>
  208. * <li>Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor </li>
  209. * <li>Set the AI skill used by INTENTION_CAST </li>
  210. * <li>Set the Intention of this AI to AI_INTENTION_CAST </li>
  211. * <li>Launch the Think Event </li><BR><BR>
  212. *
  213. */
  214. @Override
  215. protected void onIntentionCast(L2Skill skill, L2Object target)
  216. {
  217. if (getIntention() == AI_INTENTION_REST && skill.isMagic())
  218. {
  219. clientActionFailed();
  220. return;
  221. }
  222. if (_actor.isAllSkillsDisabled())
  223. {
  224. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  225. clientActionFailed();
  226. return;
  227. }
  228. // can't cast if muted
  229. if (_actor.isMuted() && skill.isMagic())
  230. {
  231. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  232. clientActionFailed();
  233. return;
  234. }
  235. // Set the AI cast target
  236. setCastTarget((L2Character) target);
  237. // Stop actions client-side to cast the skill
  238. if (skill.getHitTime() > 50)
  239. {
  240. // Abort the attack of the L2Character and send Server->Client ActionFailed packet
  241. _actor.abortAttack();
  242. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  243. // no need for second ActionFailed packet, abortAttack() already sent it
  244. //clientActionFailed();
  245. }
  246. // Set the AI skill used by INTENTION_CAST
  247. _skill = skill;
  248. // Change the Intention of this AbstractAI to AI_INTENTION_CAST
  249. changeIntention(AI_INTENTION_CAST, skill, target);
  250. // Launch the Think Event
  251. notifyEvent(CtrlEvent.EVT_THINK, null);
  252. }
  253. /**
  254. * Manage the Move To Intention : Stop current Attack and Launch a Move to Location Task.<BR><BR>
  255. *
  256. * <B><U> Actions</U> : </B><BR><BR>
  257. * <li>Stop the actor auto-attack server side AND client side by sending Server->Client packet AutoAttackStop (broadcast) </li>
  258. * <li>Set the Intention of this AI to AI_INTENTION_MOVE_TO </li>
  259. * <li>Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast) </li><BR><BR>
  260. *
  261. */
  262. @Override
  263. protected void onIntentionMoveTo(L2CharPosition pos)
  264. {
  265. if (getIntention() == AI_INTENTION_REST)
  266. {
  267. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  268. clientActionFailed();
  269. return;
  270. }
  271. if (_actor.isAllSkillsDisabled())
  272. {
  273. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  274. clientActionFailed();
  275. return;
  276. }
  277. // Set the Intention of this AbstractAI to AI_INTENTION_MOVE_TO
  278. changeIntention(AI_INTENTION_MOVE_TO, pos, null);
  279. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  280. clientStopAutoAttack();
  281. // Abort the attack of the L2Character and send Server->Client ActionFailed packet
  282. _actor.abortAttack();
  283. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  284. moveTo(pos.x, pos.y, pos.z);
  285. }
  286. /* (non-Javadoc)
  287. * @see net.sf.l2j.gameserver.ai.AbstractAI#onIntentionMoveToInABoat(net.sf.l2j.gameserver.model.L2CharPosition, net.sf.l2j.gameserver.model.L2CharPosition)
  288. */
  289. @Override
  290. protected void onIntentionMoveToInABoat(L2CharPosition destination, L2CharPosition origin)
  291. {
  292. if (getIntention() == AI_INTENTION_REST)
  293. {
  294. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  295. clientActionFailed();
  296. return;
  297. }
  298. if (_actor.isAllSkillsDisabled())
  299. {
  300. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  301. clientActionFailed();
  302. return;
  303. }
  304. // Set the Intention of this AbstractAI to AI_INTENTION_MOVE_TO
  305. //
  306. //changeIntention(AI_INTENTION_MOVE_TO, new L2CharPosition(((L2PcInstance)_actor).getBoat().getX() - destination.x, ((L2PcInstance)_actor).getBoat().getY() - destination.y, ((L2PcInstance)_actor).getBoat().getZ() - destination.z, 0) , null);
  307. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  308. clientStopAutoAttack();
  309. // Abort the attack of the L2Character and send Server->Client ActionFailed packet
  310. _actor.abortAttack();
  311. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  312. moveToInABoat(destination, origin);
  313. }
  314. /**
  315. * Manage the Follow Intention : Stop current Attack and Launch a Follow Task.<BR><BR>
  316. *
  317. * <B><U> Actions</U> : </B><BR><BR>
  318. * <li>Stop the actor auto-attack server side AND client side by sending Server->Client packet AutoAttackStop (broadcast) </li>
  319. * <li>Set the Intention of this AI to AI_INTENTION_FOLLOW </li>
  320. * <li>Create and Launch an AI Follow Task to execute every 1s </li><BR><BR>
  321. *
  322. */
  323. @Override
  324. protected void onIntentionFollow(L2Character target)
  325. {
  326. if (getIntention() == AI_INTENTION_REST)
  327. {
  328. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  329. clientActionFailed();
  330. return;
  331. }
  332. if (_actor.isAllSkillsDisabled())
  333. {
  334. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  335. clientActionFailed();
  336. return;
  337. }
  338. if (_actor.isImobilised() || _actor.isRooted())
  339. {
  340. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  341. clientActionFailed();
  342. return;
  343. }
  344. // Dead actors can`t follow
  345. if (_actor.isDead())
  346. {
  347. clientActionFailed();
  348. return;
  349. }
  350. // do not follow yourself
  351. if (_actor == target)
  352. {
  353. clientActionFailed();
  354. return;
  355. }
  356. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  357. clientStopAutoAttack();
  358. // Set the Intention of this AbstractAI to AI_INTENTION_FOLLOW
  359. changeIntention(AI_INTENTION_FOLLOW, target, null);
  360. // Create and Launch an AI Follow Task to execute every 1s
  361. startFollow(target);
  362. }
  363. /**
  364. * Manage the PickUp Intention : Set the pick up target and Launch a Move To Pawn Task (offset=20).<BR><BR>
  365. *
  366. * <B><U> Actions</U> : </B><BR><BR>
  367. * <li>Set the AI pick up target </li>
  368. * <li>Set the Intention of this AI to AI_INTENTION_PICK_UP </li>
  369. * <li>Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast) </li><BR><BR>
  370. *
  371. */
  372. @Override
  373. protected void onIntentionPickUp(L2Object object)
  374. {
  375. if (getIntention() == AI_INTENTION_REST)
  376. {
  377. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  378. clientActionFailed();
  379. return;
  380. }
  381. if (_actor.isAllSkillsDisabled())
  382. {
  383. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  384. clientActionFailed();
  385. return;
  386. }
  387. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  388. clientStopAutoAttack();
  389. // Set the Intention of this AbstractAI to AI_INTENTION_PICK_UP
  390. changeIntention(AI_INTENTION_PICK_UP, object, null);
  391. // Set the AI pick up target
  392. setTarget(object);
  393. if(object.getX() == 0 && object.getY() == 0) // TODO: Find the drop&spawn bug
  394. {
  395. _log.warning("Object in coords 0,0 - using a temporary fix");
  396. object.setXYZ(getActor().getX(), getActor().getY(), getActor().getZ()+5);
  397. }
  398. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  399. moveToPawn(object, 20);
  400. }
  401. /**
  402. * Manage the Interact Intention : Set the interact target and Launch a Move To Pawn Task (offset=60).<BR><BR>
  403. *
  404. * <B><U> Actions</U> : </B><BR><BR>
  405. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast) </li>
  406. * <li>Set the AI interact target </li>
  407. * <li>Set the Intention of this AI to AI_INTENTION_INTERACT </li>
  408. * <li>Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast) </li><BR><BR>
  409. *
  410. */
  411. @Override
  412. protected void onIntentionInteract(L2Object object)
  413. {
  414. if (getIntention() == AI_INTENTION_REST)
  415. {
  416. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  417. clientActionFailed();
  418. return;
  419. }
  420. if (_actor.isAllSkillsDisabled())
  421. {
  422. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  423. clientActionFailed();
  424. return;
  425. }
  426. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  427. clientStopAutoAttack();
  428. if (getIntention() != AI_INTENTION_INTERACT)
  429. {
  430. // Set the Intention of this AbstractAI to AI_INTENTION_INTERACT
  431. changeIntention(AI_INTENTION_INTERACT, object, null);
  432. // Set the AI interact target
  433. setTarget(object);
  434. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  435. moveToPawn(object, 60);
  436. }
  437. }
  438. /**
  439. * Do nothing.<BR><BR>
  440. */
  441. @Override
  442. protected void onEvtThink()
  443. {
  444. // do nothing
  445. }
  446. /**
  447. * Do nothing.<BR><BR>
  448. */
  449. @Override
  450. protected void onEvtAggression(@SuppressWarnings("unused")
  451. L2Character target, @SuppressWarnings("unused")
  452. int aggro)
  453. {
  454. // do nothing
  455. }
  456. /**
  457. * Launch actions corresponding to the Event Stunned then onAttacked Event.<BR><BR>
  458. *
  459. * <B><U> Actions</U> :</B><BR><BR>
  460. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  461. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  462. * <li>Break an attack and send Server->Client ActionFailed packet and a System Message to the L2Character </li>
  463. * <li>Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character </li>
  464. * <li>Launch actions corresponding to the Event onAttacked (only for L2AttackableAI after the stunning periode) </li><BR><BR>
  465. *
  466. */
  467. @Override
  468. protected void onEvtStunned(L2Character attacker)
  469. {
  470. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  471. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  472. if (AttackStanceTaskManager.getInstance().getAttackStanceTask(_actor))
  473. AttackStanceTaskManager.getInstance().removeAttackStanceTask(_actor);
  474. // Stop Server AutoAttack also
  475. setAutoAttacking(false);
  476. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  477. clientStopMoving(null);
  478. // Launch actions corresponding to the Event onAttacked (only for L2AttackableAI after the stunning periode)
  479. onEvtAttacked(attacker);
  480. }
  481. /**
  482. * Launch actions corresponding to the Event Sleeping.<BR><BR>
  483. *
  484. * <B><U> Actions</U> :</B><BR><BR>
  485. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  486. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  487. * <li>Break an attack and send Server->Client ActionFailed packet and a System Message to the L2Character </li>
  488. * <li>Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character </li><BR><BR>
  489. *
  490. */
  491. @Override
  492. protected void onEvtSleeping(@SuppressWarnings("unused")
  493. L2Character attacker)
  494. {
  495. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  496. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  497. if (AttackStanceTaskManager.getInstance().getAttackStanceTask(_actor))
  498. AttackStanceTaskManager.getInstance().removeAttackStanceTask(_actor);
  499. // stop Server AutoAttack also
  500. setAutoAttacking(false);
  501. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  502. clientStopMoving(null);
  503. }
  504. /**
  505. * Launch actions corresponding to the Event Rooted.<BR><BR>
  506. *
  507. * <B><U> Actions</U> :</B><BR><BR>
  508. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  509. * <li>Launch actions corresponding to the Event onAttacked</li><BR><BR>
  510. *
  511. */
  512. @Override
  513. protected void onEvtRooted(L2Character attacker)
  514. {
  515. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  516. //_actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  517. //if (AttackStanceTaskManager.getInstance().getAttackStanceTask(_actor))
  518. // AttackStanceTaskManager.getInstance().removeAttackStanceTask(_actor);
  519. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  520. clientStopMoving(null);
  521. // Launch actions corresponding to the Event onAttacked
  522. onEvtAttacked(attacker);
  523. }
  524. /**
  525. * Launch actions corresponding to the Event Confused.<BR><BR>
  526. *
  527. * <B><U> Actions</U> :</B><BR><BR>
  528. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  529. * <li>Launch actions corresponding to the Event onAttacked</li><BR><BR>
  530. *
  531. */
  532. @Override
  533. protected void onEvtConfused(L2Character attacker)
  534. {
  535. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  536. clientStopMoving(null);
  537. // Launch actions corresponding to the Event onAttacked
  538. onEvtAttacked(attacker);
  539. }
  540. /**
  541. * Launch actions corresponding to the Event Muted.<BR><BR>
  542. *
  543. * <B><U> Actions</U> :</B><BR><BR>
  544. * <li>Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character </li><BR><BR>
  545. *
  546. */
  547. @Override
  548. protected void onEvtMuted(L2Character attacker)
  549. {
  550. // Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character
  551. onEvtAttacked(attacker);
  552. }
  553. /**
  554. * Launch actions corresponding to the Event ReadyToAct.<BR><BR>
  555. *
  556. * <B><U> Actions</U> :</B><BR><BR>
  557. * <li>Launch actions corresponding to the Event Think</li><BR><BR>
  558. *
  559. */
  560. @Override
  561. protected void onEvtReadyToAct()
  562. {
  563. // Launch actions corresponding to the Event Think
  564. onEvtThink();
  565. }
  566. /**
  567. * Do nothing.<BR><BR>
  568. */
  569. @Override
  570. protected void onEvtUserCmd(@SuppressWarnings("unused")
  571. Object arg0, @SuppressWarnings("unused")
  572. Object arg1)
  573. {
  574. // do nothing
  575. }
  576. /**
  577. * Launch actions corresponding to the Event Arrived.<BR><BR>
  578. *
  579. * <B><U> Actions</U> :</B><BR><BR>
  580. * <li>If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE</li>
  581. * <li>Launch actions corresponding to the Event Think</li><BR><BR>
  582. *
  583. */
  584. @Override
  585. protected void onEvtArrived()
  586. {
  587. // Launch an explore task if necessary
  588. if (_accessor.getActor() instanceof L2PcInstance)
  589. {
  590. if (Config.ACTIVATE_POSITION_RECORDER)
  591. ((L2PcInstance) _accessor.getActor()).explore();
  592. ((L2PcInstance) _accessor.getActor()).revalidateZone(true);
  593. }
  594. else _accessor.getActor().revalidateZone();
  595. if (_accessor.getActor().moveToNextRoutePoint())
  596. return;
  597. clientStoppedMoving();
  598. // If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE
  599. if (getIntention() == AI_INTENTION_MOVE_TO) setIntention(AI_INTENTION_ACTIVE);
  600. // Launch actions corresponding to the Event Think
  601. onEvtThink();
  602. if (_actor instanceof L2BoatInstance)
  603. {
  604. ((L2BoatInstance) _actor).evtArrived();
  605. }
  606. }
  607. /**
  608. * Launch actions corresponding to the Event ArrivedRevalidate.<BR><BR>
  609. *
  610. * <B><U> Actions</U> :</B><BR><BR>
  611. * <li>Launch actions corresponding to the Event Think</li><BR><BR>
  612. *
  613. */
  614. @Override
  615. protected void onEvtArrivedRevalidate()
  616. {
  617. // Launch actions corresponding to the Event Think
  618. onEvtThink();
  619. }
  620. /**
  621. * Launch actions corresponding to the Event ArrivedBlocked.<BR><BR>
  622. *
  623. * <B><U> Actions</U> :</B><BR><BR>
  624. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  625. * <li>If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE</li>
  626. * <li>Launch actions corresponding to the Event Think</li><BR><BR>
  627. *
  628. */
  629. @Override
  630. protected void onEvtArrivedBlocked(L2CharPosition blocked_at_pos)
  631. {
  632. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  633. clientStopMoving(blocked_at_pos);
  634. if (Config.ACTIVATE_POSITION_RECORDER
  635. && Universe.getInstance().shouldLog(_accessor.getActor().getObjectId()))
  636. {
  637. if (!_accessor.getActor().isFlying())
  638. Universe.getInstance().registerObstacle(blocked_at_pos.x, blocked_at_pos.y,
  639. blocked_at_pos.z);
  640. if (_accessor.getActor() instanceof L2PcInstance)
  641. ((L2PcInstance) _accessor.getActor()).explore();
  642. }
  643. // If the Intention was AI_INTENTION_MOVE_TO, tet the Intention to AI_INTENTION_ACTIVE
  644. if (getIntention() == AI_INTENTION_MOVE_TO) setIntention(AI_INTENTION_ACTIVE);
  645. // Launch actions corresponding to the Event Think
  646. onEvtThink();
  647. }
  648. /**
  649. * Launch actions corresponding to the Event ForgetObject.<BR><BR>
  650. *
  651. * <B><U> Actions</U> :</B><BR><BR>
  652. * <li>If the object was targeted and the Intention was AI_INTENTION_INTERACT or AI_INTENTION_PICK_UP, set the Intention to AI_INTENTION_ACTIVE</li>
  653. * <li>If the object was targeted to attack, stop the auto-attack, cancel target and set the Intention to AI_INTENTION_ACTIVE</li>
  654. * <li>If the object was targeted to cast, cancel target and set the Intention to AI_INTENTION_ACTIVE</li>
  655. * <li>If the object was targeted to follow, stop the movement, cancel AI Follow Task and set the Intention to AI_INTENTION_ACTIVE</li>
  656. * <li>If the targeted object was the actor , cancel AI target, stop AI Follow Task, stop the movement and set the Intention to AI_INTENTION_IDLE </li><BR><BR>
  657. *
  658. */
  659. @Override
  660. protected void onEvtForgetObject(L2Object object)
  661. {
  662. // If the object was targeted and the Intention was AI_INTENTION_INTERACT or AI_INTENTION_PICK_UP, set the Intention to AI_INTENTION_ACTIVE
  663. if (getTarget() == object)
  664. {
  665. setTarget(null);
  666. if (getIntention() == AI_INTENTION_INTERACT) setIntention(AI_INTENTION_ACTIVE);
  667. else if (getIntention() == AI_INTENTION_PICK_UP) setIntention(AI_INTENTION_ACTIVE);
  668. }
  669. // Check if the object was targeted to attack
  670. if (getAttackTarget() == object)
  671. {
  672. // Cancel attack target
  673. setAttackTarget(null);
  674. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  675. setIntention(AI_INTENTION_ACTIVE);
  676. }
  677. // Check if the object was targeted to cast
  678. if (getCastTarget() == object)
  679. {
  680. // Cancel cast target
  681. setCastTarget(null);
  682. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  683. setIntention(AI_INTENTION_ACTIVE);
  684. }
  685. // Check if the object was targeted to follow
  686. if (getFollowTarget() == object)
  687. {
  688. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  689. clientStopMoving(null);
  690. // Stop an AI Follow Task
  691. stopFollow();
  692. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  693. setIntention(AI_INTENTION_ACTIVE);
  694. }
  695. // Check if the targeted object was the actor
  696. if (_actor == object)
  697. {
  698. // Cancel AI target
  699. setTarget(null);
  700. setAttackTarget(null);
  701. setCastTarget(null);
  702. // Stop an AI Follow Task
  703. stopFollow();
  704. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  705. clientStopMoving(null);
  706. // Set the Intention of this AbstractAI to AI_INTENTION_IDLE
  707. changeIntention(AI_INTENTION_IDLE, null, null);
  708. }
  709. }
  710. /**
  711. * Launch actions corresponding to the Event Cancel.<BR><BR>
  712. *
  713. * <B><U> Actions</U> :</B><BR><BR>
  714. * <li>Stop an AI Follow Task</li>
  715. * <li>Launch actions corresponding to the Event Think</li><BR><BR>
  716. *
  717. */
  718. @Override
  719. protected void onEvtCancel()
  720. {
  721. // Stop an AI Follow Task
  722. stopFollow();
  723. if (!AttackStanceTaskManager.getInstance().getAttackStanceTask(_actor))
  724. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  725. // Launch actions corresponding to the Event Think
  726. onEvtThink();
  727. }
  728. /**
  729. * Launch actions corresponding to the Event Dead.<BR><BR>
  730. *
  731. * <B><U> Actions</U> :</B><BR><BR>
  732. * <li>Stop an AI Follow Task</li>
  733. * <li>Kill the actor client side by sending Server->Client packet AutoAttackStop, StopMove/StopRotation, Die (broadcast)</li><BR><BR>
  734. *
  735. */
  736. @Override
  737. protected void onEvtDead()
  738. {
  739. // Stop an AI Follow Task
  740. stopFollow();
  741. // Kill the actor client side by sending Server->Client packet AutoAttackStop, StopMove/StopRotation, Die (broadcast)
  742. clientNotifyDead();
  743. if (!(_actor instanceof L2PcInstance))
  744. _actor.setWalking();
  745. }
  746. /**
  747. * Launch actions corresponding to the Event Fake Death.<BR><BR>
  748. *
  749. * <B><U> Actions</U> :</B><BR><BR>
  750. * <li>Stop an AI Follow Task</li>
  751. *
  752. */
  753. @Override
  754. protected void onEvtFakeDeath()
  755. {
  756. // Stop an AI Follow Task
  757. stopFollow();
  758. // Stop the actor movement and send Server->Client packet StopMove/StopRotation (broadcast)
  759. clientStopMoving(null);
  760. // Init AI
  761. _intention = AI_INTENTION_IDLE;
  762. setTarget(null);
  763. setCastTarget(null);
  764. setAttackTarget(null);
  765. }
  766. /**
  767. * Do nothing.<BR><BR>
  768. */
  769. @Override
  770. protected void onEvtFinishCasting()
  771. {
  772. // do nothing
  773. }
  774. /**
  775. * Manage the Move to Pawn action in function of the distance and of the Interact area.<BR><BR>
  776. *
  777. * <B><U> Actions</U> :</B><BR><BR>
  778. * <li>Get the distance between the current position of the L2Character and the target (x,y)</li>
  779. * <li>If the distance > offset+20, move the actor (by running) to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)</li>
  780. * <li>If the distance <= offset+20, Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li><BR><BR>
  781. *
  782. * <B><U> Example of use </U> :</B><BR><BR>
  783. * <li> L2PLayerAI, L2SummonAI</li><BR><BR>
  784. *
  785. * @param target The targeted L2Object
  786. * @param offset The Interact area radius
  787. *
  788. * @return True if a movement must be done
  789. *
  790. */
  791. protected boolean maybeMoveToPawn(L2Object target, int offset)
  792. {
  793. // Get the distance between the current position of the L2Character and the target (x,y)
  794. if (target == null)
  795. {
  796. _log.warning("maybeMoveToPawn: target == NULL!");
  797. return false;
  798. }
  799. if(offset < 0) return false; // skill radius -1
  800. offset += _actor.getTemplate().collisionRadius;
  801. if (target instanceof L2Character)
  802. offset += ((L2Character)target).getTemplate().collisionRadius;
  803. if (!_actor.isInsideRadius(target, offset, false, false))
  804. {
  805. // Caller should be L2Playable and thinkAttack/thinkCast/thinkInteract/thinkPickUp
  806. if (getFollowTarget() != null) {
  807. // prevent attack-follow into peace zones
  808. if(getAttackTarget() != null && _actor instanceof L2PlayableInstance && target instanceof L2PlayableInstance)
  809. {
  810. if(getAttackTarget() == getFollowTarget())
  811. {
  812. // allow GMs to keep following
  813. boolean isGM = (_actor instanceof L2PcInstance ? ((L2PcInstance)_actor).isGM() : false);
  814. if (((L2PlayableInstance)_actor).isInsidePeaceZone(_actor, target) && !isGM)
  815. {
  816. stopFollow();
  817. setIntention(AI_INTENTION_IDLE);
  818. return true;
  819. }
  820. }
  821. }
  822. // if the target is too far (maybe also teleported)
  823. if (!_actor.isInsideRadius(target, 2000, false, false))
  824. {
  825. stopFollow();
  826. setIntention(AI_INTENTION_IDLE);
  827. return true;
  828. }
  829. // allow larger hit range when the target is moving (check is run only once per second)
  830. if (!_actor.isInsideRadius(target, offset + 100, false, false)) return true;
  831. stopFollow();
  832. return false;
  833. }
  834. if (_actor.isMovementDisabled()) return true;
  835. // If not running, set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  836. if (!_actor.isRunning() && !(this instanceof L2PlayerAI)) _actor.setRunning();
  837. stopFollow();
  838. if ((target instanceof L2Character) && !(target instanceof L2DoorInstance))
  839. {
  840. if (((L2Character)target).isMoving()) offset -= 100;
  841. if (offset < 5) offset = 5;
  842. startFollow((L2Character) target, offset);
  843. }
  844. else
  845. {
  846. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  847. moveToPawn(target, offset);
  848. }
  849. return true;
  850. }
  851. if (getFollowTarget() != null) stopFollow();
  852. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  853. // clientStopMoving(null);
  854. return false;
  855. }
  856. /**
  857. * Modify current Intention and actions if the target is lost or dead.<BR><BR>
  858. *
  859. * <B><U> Actions</U> : <I>If the target is lost or dead</I></B><BR><BR>
  860. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  861. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  862. * <li>Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE</li><BR><BR>
  863. *
  864. * <B><U> Example of use </U> :</B><BR><BR>
  865. * <li> L2PLayerAI, L2SummonAI</li><BR><BR>
  866. *
  867. * @param target The targeted L2Object
  868. *
  869. * @return True if the target is lost or dead (false if fakedeath)
  870. *
  871. */
  872. protected boolean checkTargetLostOrDead(L2Character target)
  873. {
  874. if (target == null || target.isAlikeDead())
  875. {
  876. //check if player is fakedeath
  877. if (target != null && target.isFakeDeath())
  878. {
  879. target.stopFakeDeath(null);
  880. return false;
  881. }
  882. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  883. setIntention(AI_INTENTION_ACTIVE);
  884. return true;
  885. }
  886. return false;
  887. }
  888. /**
  889. * Modify current Intention and actions if the target is lost.<BR><BR>
  890. *
  891. * <B><U> Actions</U> : <I>If the target is lost</I></B><BR><BR>
  892. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  893. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  894. * <li>Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE</li><BR><BR>
  895. *
  896. * <B><U> Example of use </U> :</B><BR><BR>
  897. * <li> L2PLayerAI, L2SummonAI</li><BR><BR>
  898. *
  899. * @param target The targeted L2Object
  900. *
  901. * @return True if the target is lost
  902. *
  903. */
  904. protected boolean checkTargetLost(L2Object target)
  905. {
  906. // check if player is fakedeath
  907. if (target instanceof L2PcInstance)
  908. {
  909. L2PcInstance target2 = (L2PcInstance) target; //convert object to chara
  910. if (target2.isFakeDeath())
  911. {
  912. target2.stopFakeDeath(null);
  913. return false;
  914. }
  915. }
  916. if (target == null)
  917. {
  918. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  919. setIntention(AI_INTENTION_ACTIVE);
  920. return true;
  921. }
  922. return false;
  923. }
  924. }