L2CharacterAI.java 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  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_ACTIVE;
  17. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  18. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_CAST;
  19. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_FOLLOW;
  20. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  21. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_INTERACT;
  22. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_MOVE_TO;
  23. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_PICK_UP;
  24. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_REST;
  25. import java.util.List;
  26. import javolution.util.FastList;
  27. import net.sf.l2j.Config;
  28. import net.sf.l2j.gameserver.Universe;
  29. import net.sf.l2j.gameserver.model.L2CharPosition;
  30. import net.sf.l2j.gameserver.model.L2ItemInstance;
  31. import net.sf.l2j.gameserver.model.L2Object;
  32. import net.sf.l2j.gameserver.model.L2Skill;
  33. import net.sf.l2j.gameserver.model.L2ItemInstance.ItemLocation;
  34. import net.sf.l2j.gameserver.model.actor.L2Attackable;
  35. import net.sf.l2j.gameserver.model.actor.L2Character;
  36. import net.sf.l2j.gameserver.model.actor.L2Npc;
  37. import net.sf.l2j.gameserver.model.actor.instance.L2AirShipInstance;
  38. import net.sf.l2j.gameserver.model.actor.instance.L2BoatInstance;
  39. import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
  40. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  41. import net.sf.l2j.gameserver.network.SystemMessageId;
  42. import net.sf.l2j.gameserver.network.serverpackets.ActionFailed;
  43. import net.sf.l2j.gameserver.network.serverpackets.AutoAttackStop;
  44. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  45. import net.sf.l2j.gameserver.taskmanager.AttackStanceTaskManager;
  46. import net.sf.l2j.gameserver.templates.chars.L2NpcTemplate;
  47. import net.sf.l2j.gameserver.templates.item.L2Weapon;
  48. import net.sf.l2j.gameserver.templates.item.L2WeaponType;
  49. import net.sf.l2j.util.Point3D;
  50. import net.sf.l2j.util.Rnd;
  51. /**
  52. * This class manages AI of L2Character.<BR><BR>
  53. *
  54. * L2CharacterAI :<BR><BR>
  55. * <li>L2AttackableAI</li>
  56. * <li>L2DoorAI</li>
  57. * <li>L2PlayerAI</li>
  58. * <li>L2SummonAI</li><BR><BR>
  59. *
  60. */
  61. public class L2CharacterAI extends AbstractAI
  62. {
  63. public class IntentionCommand
  64. {
  65. protected final CtrlIntention _crtlIntention;
  66. protected final Object _arg0, _arg1;
  67. protected IntentionCommand(CtrlIntention pIntention, Object pArg0, Object pArg1)
  68. {
  69. _crtlIntention = pIntention;
  70. _arg0 = pArg0;
  71. _arg1 = pArg1;
  72. }
  73. public CtrlIntention getCtrlIntention()
  74. {
  75. return _crtlIntention;
  76. }
  77. }
  78. /**
  79. * Constructor of L2CharacterAI.<BR><BR>
  80. *
  81. * @param accessor The AI accessor of the L2Character
  82. *
  83. */
  84. public L2CharacterAI(L2Character.AIAccessor accessor)
  85. {
  86. super(accessor);
  87. }
  88. public IntentionCommand getNextIntention()
  89. {
  90. return null;
  91. }
  92. @Override
  93. protected void onEvtAttacked(L2Character attacker)
  94. {
  95. if (attacker instanceof L2Attackable && !((L2Attackable) attacker).isCoreAIDisabled())
  96. clientStartAutoAttack();
  97. }
  98. /**
  99. * Manage the Idle Intention : Stop Attack, Movement and Stand Up the actor.<BR><BR>
  100. *
  101. * <B><U> Actions</U> :</B><BR><BR>
  102. * <li>Set the AI Intention to AI_INTENTION_IDLE </li>
  103. * <li>Init cast and attack target </li>
  104. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast) </li>
  105. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast) </li>
  106. * <li>Stand up the actor server side AND client side by sending Server->Client packet ChangeWaitType (broadcast) </li><BR><BR>
  107. *
  108. */
  109. @Override
  110. protected void onIntentionIdle()
  111. {
  112. // Set the AI Intention to AI_INTENTION_IDLE
  113. changeIntention(AI_INTENTION_IDLE, null, null);
  114. // Init cast and attack target
  115. setCastTarget(null);
  116. setAttackTarget(null);
  117. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  118. clientStopMoving(null);
  119. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  120. clientStopAutoAttack();
  121. }
  122. /**
  123. * Manage the Active Intention : Stop Attack, Movement and Launch Think Event.<BR><BR>
  124. *
  125. * <B><U> Actions</U> : <I>if the Intention is not already Active</I></B><BR><BR>
  126. * <li>Set the AI Intention to AI_INTENTION_ACTIVE </li>
  127. * <li>Init cast and attack target </li>
  128. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast) </li>
  129. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast) </li>
  130. * <li>Launch the Think Event </li><BR><BR>
  131. *
  132. */
  133. @Override
  134. protected void onIntentionActive()
  135. {
  136. // Check if the Intention is not already Active
  137. if (getIntention() != AI_INTENTION_ACTIVE)
  138. {
  139. // Set the AI Intention to AI_INTENTION_ACTIVE
  140. changeIntention(AI_INTENTION_ACTIVE, null, null);
  141. // Init cast and attack target
  142. setCastTarget(null);
  143. setAttackTarget(null);
  144. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  145. clientStopMoving(null);
  146. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  147. clientStopAutoAttack();
  148. // Also enable random animations for this L2Character if allowed
  149. // This is only for mobs - town npcs are handled in their constructor
  150. if (_actor instanceof L2Attackable)
  151. ((L2Npc) _actor).startRandomAnimationTimer();
  152. // Launch the Think Event
  153. onEvtThink();
  154. }
  155. }
  156. /**
  157. * Manage the Rest Intention.<BR><BR>
  158. *
  159. * <B><U> Actions</U> : </B><BR><BR>
  160. * <li>Set the AI Intention to AI_INTENTION_IDLE </li><BR><BR>
  161. *
  162. */
  163. @Override
  164. protected void onIntentionRest()
  165. {
  166. // Set the AI Intention to AI_INTENTION_IDLE
  167. setIntention(AI_INTENTION_IDLE);
  168. }
  169. /**
  170. * Manage the Attack Intention : Stop current Attack (if necessary), Start a new Attack and Launch Think Event.<BR><BR>
  171. *
  172. * <B><U> Actions</U> : </B><BR><BR>
  173. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast) </li>
  174. * <li>Set the Intention of this AI to AI_INTENTION_ATTACK </li>
  175. * <li>Set or change the AI attack target </li>
  176. * <li>Start the actor Auto Attack client side by sending Server->Client packet AutoAttackStart (broadcast) </li>
  177. * <li>Launch the Think Event </li><BR><BR>
  178. *
  179. *
  180. * <B><U> Overridden in</U> :</B><BR><BR>
  181. * <li>L2AttackableAI : Calculate attack timeout</li><BR><BR>
  182. *
  183. */
  184. @Override
  185. protected void onIntentionAttack(L2Character target)
  186. {
  187. // stop invul effect if exist
  188. if (_actor.getInvulEffect() != null)
  189. _actor.getInvulEffect().exit();
  190. if (target == null)
  191. {
  192. clientActionFailed();
  193. return;
  194. }
  195. if (getIntention() == AI_INTENTION_REST)
  196. {
  197. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  198. clientActionFailed();
  199. return;
  200. }
  201. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow() || _actor.isAfraid())
  202. {
  203. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  204. clientActionFailed();
  205. return;
  206. }
  207. // Check if the Intention is already AI_INTENTION_ATTACK
  208. if (getIntention() == AI_INTENTION_ATTACK)
  209. {
  210. // Check if the AI already targets the L2Character
  211. if (getAttackTarget() != target)
  212. {
  213. // Set the AI attack target (change target)
  214. setAttackTarget(target);
  215. stopFollow();
  216. // Launch the Think Event
  217. notifyEvent(CtrlEvent.EVT_THINK, null);
  218. }
  219. else
  220. clientActionFailed(); // else client freezes until cancel target
  221. }
  222. else
  223. {
  224. // Set the Intention of this AbstractAI to AI_INTENTION_ATTACK
  225. changeIntention(AI_INTENTION_ATTACK, target, null);
  226. // Set the AI attack target
  227. setAttackTarget(target);
  228. stopFollow();
  229. // Launch the Think Event
  230. notifyEvent(CtrlEvent.EVT_THINK, null);
  231. }
  232. }
  233. /**
  234. * Manage the Cast Intention : Stop current Attack, Init the AI in order to cast and Launch Think Event.<BR><BR>
  235. *
  236. * <B><U> Actions</U> : </B><BR><BR>
  237. * <li>Set the AI cast target </li>
  238. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast) </li>
  239. * <li>Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor </li>
  240. * <li>Set the AI skill used by INTENTION_CAST </li>
  241. * <li>Set the Intention of this AI to AI_INTENTION_CAST </li>
  242. * <li>Launch the Think Event </li><BR><BR>
  243. *
  244. */
  245. @Override
  246. protected void onIntentionCast(L2Skill skill, L2Object target)
  247. {
  248. // stop invul effect if exist
  249. if (_actor.getInvulEffect() != null)
  250. _actor.getInvulEffect().exit();
  251. if (getIntention() == AI_INTENTION_REST && skill.isMagic())
  252. {
  253. clientActionFailed();
  254. _actor.setIsCastingNow(false);
  255. return;
  256. }
  257. // Set the AI cast target
  258. setCastTarget((L2Character) target);
  259. // Stop actions client-side to cast the skill
  260. if (skill.getHitTime() > 50)
  261. {
  262. // Abort the attack of the L2Character and send Server->Client ActionFailed packet
  263. _actor.abortAttack();
  264. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  265. // no need for second ActionFailed packet, abortAttack() already sent it
  266. //clientActionFailed();
  267. }
  268. // Set the AI skill used by INTENTION_CAST
  269. _skill = skill;
  270. // Change the Intention of this AbstractAI to AI_INTENTION_CAST
  271. changeIntention(AI_INTENTION_CAST, skill, target);
  272. // Launch the Think Event
  273. notifyEvent(CtrlEvent.EVT_THINK, null);
  274. }
  275. /**
  276. * Manage the Move To Intention : Stop current Attack and Launch a Move to Location Task.<BR><BR>
  277. *
  278. * <B><U> Actions</U> : </B><BR><BR>
  279. * <li>Stop the actor auto-attack server side AND client side by sending Server->Client packet AutoAttackStop (broadcast) </li>
  280. * <li>Set the Intention of this AI to AI_INTENTION_MOVE_TO </li>
  281. * <li>Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast) </li><BR><BR>
  282. *
  283. */
  284. @Override
  285. protected void onIntentionMoveTo(L2CharPosition pos)
  286. {
  287. if (getIntention() == AI_INTENTION_REST)
  288. {
  289. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  290. clientActionFailed();
  291. return;
  292. }
  293. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  294. {
  295. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  296. clientActionFailed();
  297. return;
  298. }
  299. // Set the Intention of this AbstractAI to AI_INTENTION_MOVE_TO
  300. changeIntention(AI_INTENTION_MOVE_TO, pos, null);
  301. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  302. clientStopAutoAttack();
  303. // Abort the attack of the L2Character and send Server->Client ActionFailed packet
  304. _actor.abortAttack();
  305. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  306. moveTo(pos.x, pos.y, pos.z);
  307. }
  308. /* (non-Javadoc)
  309. * @see net.sf.l2j.gameserver.ai.AbstractAI#onIntentionMoveToInABoat(net.sf.l2j.gameserver.model.L2CharPosition, net.sf.l2j.gameserver.model.L2CharPosition)
  310. */
  311. @Override
  312. protected void onIntentionMoveToInABoat(L2CharPosition destination, L2CharPosition origin)
  313. {
  314. if (getIntention() == AI_INTENTION_REST)
  315. {
  316. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  317. clientActionFailed();
  318. return;
  319. }
  320. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  321. {
  322. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  323. clientActionFailed();
  324. return;
  325. }
  326. // Set the Intention of this AbstractAI to AI_INTENTION_MOVE_TO
  327. //
  328. //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);
  329. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  330. clientStopAutoAttack();
  331. // Abort the attack of the L2Character and send Server->Client ActionFailed packet
  332. _actor.abortAttack();
  333. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  334. moveToInABoat(destination, origin);
  335. }
  336. /* (non-Javadoc)
  337. * @see net.sf.l2j.gameserver.ai.AbstractAI#onIntentionMoveToInAirShip(net.sf.l2j.gameserver.model.L2CharPosition, net.sf.l2j.gameserver.model.L2CharPosition)
  338. */
  339. @Override
  340. protected void onIntentionMoveToInAirShip(L2CharPosition destination, L2CharPosition origin)
  341. {
  342. if (getIntention() == AI_INTENTION_REST)
  343. {
  344. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  345. clientActionFailed();
  346. return;
  347. }
  348. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  349. {
  350. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  351. clientActionFailed();
  352. return;
  353. }
  354. // Set the Intention of this AbstractAI to AI_INTENTION_MOVE_TO
  355. //
  356. //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);
  357. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  358. clientStopAutoAttack();
  359. // Abort the attack of the L2Character and send Server->Client ActionFailed packet
  360. _actor.abortAttack();
  361. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  362. moveToInAirShip(destination, origin);
  363. }
  364. /**
  365. * Manage the Follow Intention : Stop current Attack and Launch a Follow Task.<BR><BR>
  366. *
  367. * <B><U> Actions</U> : </B><BR><BR>
  368. * <li>Stop the actor auto-attack server side AND client side by sending Server->Client packet AutoAttackStop (broadcast) </li>
  369. * <li>Set the Intention of this AI to AI_INTENTION_FOLLOW </li>
  370. * <li>Create and Launch an AI Follow Task to execute every 1s </li><BR><BR>
  371. *
  372. */
  373. @Override
  374. protected void onIntentionFollow(L2Character target)
  375. {
  376. if (getIntention() == AI_INTENTION_REST)
  377. {
  378. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  379. clientActionFailed();
  380. return;
  381. }
  382. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  383. {
  384. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  385. clientActionFailed();
  386. return;
  387. }
  388. if (_actor.isImmobilized() || _actor.isRooted())
  389. {
  390. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  391. clientActionFailed();
  392. return;
  393. }
  394. // Dead actors can`t follow
  395. if (_actor.isDead())
  396. {
  397. clientActionFailed();
  398. return;
  399. }
  400. // do not follow yourself
  401. if (_actor == target)
  402. {
  403. clientActionFailed();
  404. return;
  405. }
  406. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  407. clientStopAutoAttack();
  408. // Set the Intention of this AbstractAI to AI_INTENTION_FOLLOW
  409. changeIntention(AI_INTENTION_FOLLOW, target, null);
  410. // Create and Launch an AI Follow Task to execute every 1s
  411. startFollow(target);
  412. }
  413. /**
  414. * Manage the PickUp Intention : Set the pick up target and Launch a Move To Pawn Task (offset=20).<BR><BR>
  415. *
  416. * <B><U> Actions</U> : </B><BR><BR>
  417. * <li>Set the AI pick up target </li>
  418. * <li>Set the Intention of this AI to AI_INTENTION_PICK_UP </li>
  419. * <li>Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast) </li><BR><BR>
  420. *
  421. */
  422. @Override
  423. protected void onIntentionPickUp(L2Object object)
  424. {
  425. if (getIntention() == AI_INTENTION_REST)
  426. {
  427. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  428. clientActionFailed();
  429. return;
  430. }
  431. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  432. {
  433. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  434. clientActionFailed();
  435. return;
  436. }
  437. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  438. clientStopAutoAttack();
  439. if (object instanceof L2ItemInstance && ((L2ItemInstance)object).getLocation() != ItemLocation.VOID)
  440. return;
  441. // Set the Intention of this AbstractAI to AI_INTENTION_PICK_UP
  442. changeIntention(AI_INTENTION_PICK_UP, object, null);
  443. // Set the AI pick up target
  444. setTarget(object);
  445. if (object.getX() == 0 && object.getY() == 0) // TODO: Find the drop&spawn bug
  446. {
  447. _log.warning("Object in coords 0,0 - using a temporary fix");
  448. object.setXYZ(getActor().getX(), getActor().getY(), getActor().getZ() + 5);
  449. }
  450. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  451. moveToPawn(object, 20);
  452. }
  453. /**
  454. * Manage the Interact Intention : Set the interact target and Launch a Move To Pawn Task (offset=60).<BR><BR>
  455. *
  456. * <B><U> Actions</U> : </B><BR><BR>
  457. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast) </li>
  458. * <li>Set the AI interact target </li>
  459. * <li>Set the Intention of this AI to AI_INTENTION_INTERACT </li>
  460. * <li>Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast) </li><BR><BR>
  461. *
  462. */
  463. @Override
  464. protected void onIntentionInteract(L2Object object)
  465. {
  466. if (getIntention() == AI_INTENTION_REST)
  467. {
  468. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  469. clientActionFailed();
  470. return;
  471. }
  472. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  473. {
  474. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  475. clientActionFailed();
  476. return;
  477. }
  478. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  479. clientStopAutoAttack();
  480. if (getIntention() != AI_INTENTION_INTERACT)
  481. {
  482. // Set the Intention of this AbstractAI to AI_INTENTION_INTERACT
  483. changeIntention(AI_INTENTION_INTERACT, object, null);
  484. // Set the AI interact target
  485. setTarget(object);
  486. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  487. moveToPawn(object, 60);
  488. }
  489. }
  490. /**
  491. * Do nothing.<BR><BR>
  492. */
  493. @Override
  494. protected void onEvtThink()
  495. {
  496. // do nothing
  497. }
  498. /**
  499. * Do nothing.<BR><BR>
  500. */
  501. @Override
  502. protected void onEvtAggression(L2Character target, int aggro)
  503. {
  504. // do nothing
  505. }
  506. /**
  507. * Launch actions corresponding to the Event Stunned then onAttacked Event.<BR><BR>
  508. *
  509. * <B><U> Actions</U> :</B><BR><BR>
  510. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  511. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  512. * <li>Break an attack and send Server->Client ActionFailed packet and a System Message to the L2Character </li>
  513. * <li>Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character </li>
  514. * <li>Launch actions corresponding to the Event onAttacked (only for L2AttackableAI after the stunning periode) </li><BR><BR>
  515. *
  516. */
  517. @Override
  518. protected void onEvtStunned(L2Character attacker)
  519. {
  520. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  521. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  522. if (AttackStanceTaskManager.getInstance().getAttackStanceTask(_actor))
  523. AttackStanceTaskManager.getInstance().removeAttackStanceTask(_actor);
  524. // Stop Server AutoAttack also
  525. setAutoAttacking(false);
  526. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  527. clientStopMoving(null);
  528. // Launch actions corresponding to the Event onAttacked (only for L2AttackableAI after the stunning periode)
  529. onEvtAttacked(attacker);
  530. }
  531. @Override
  532. protected void onEvtParalyzed(L2Character attacker)
  533. {
  534. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  535. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  536. if (AttackStanceTaskManager.getInstance().getAttackStanceTask(_actor))
  537. AttackStanceTaskManager.getInstance().removeAttackStanceTask(_actor);
  538. // Stop Server AutoAttack also
  539. setAutoAttacking(false);
  540. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  541. clientStopMoving(null);
  542. // Launch actions corresponding to the Event onAttacked (only for L2AttackableAI after the stunning periode)
  543. onEvtAttacked(attacker);
  544. }
  545. /**
  546. * Launch actions corresponding to the Event Sleeping.<BR><BR>
  547. *
  548. * <B><U> Actions</U> :</B><BR><BR>
  549. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  550. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  551. * <li>Break an attack and send Server->Client ActionFailed packet and a System Message to the L2Character </li>
  552. * <li>Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character </li><BR><BR>
  553. *
  554. */
  555. @Override
  556. protected void onEvtSleeping(L2Character attacker)
  557. {
  558. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  559. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  560. if (AttackStanceTaskManager.getInstance().getAttackStanceTask(_actor))
  561. AttackStanceTaskManager.getInstance().removeAttackStanceTask(_actor);
  562. // stop Server AutoAttack also
  563. setAutoAttacking(false);
  564. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  565. clientStopMoving(null);
  566. }
  567. /**
  568. * Launch actions corresponding to the Event Rooted.<BR><BR>
  569. *
  570. * <B><U> Actions</U> :</B><BR><BR>
  571. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  572. * <li>Launch actions corresponding to the Event onAttacked</li><BR><BR>
  573. *
  574. */
  575. @Override
  576. protected void onEvtRooted(L2Character attacker)
  577. {
  578. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  579. //_actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  580. //if (AttackStanceTaskManager.getInstance().getAttackStanceTask(_actor))
  581. // AttackStanceTaskManager.getInstance().removeAttackStanceTask(_actor);
  582. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  583. clientStopMoving(null);
  584. // Launch actions corresponding to the Event onAttacked
  585. onEvtAttacked(attacker);
  586. }
  587. /**
  588. * Launch actions corresponding to the Event Confused.<BR><BR>
  589. *
  590. * <B><U> Actions</U> :</B><BR><BR>
  591. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  592. * <li>Launch actions corresponding to the Event onAttacked</li><BR><BR>
  593. *
  594. */
  595. @Override
  596. protected void onEvtConfused(L2Character attacker)
  597. {
  598. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  599. clientStopMoving(null);
  600. // Launch actions corresponding to the Event onAttacked
  601. onEvtAttacked(attacker);
  602. }
  603. /**
  604. * Launch actions corresponding to the Event Muted.<BR><BR>
  605. *
  606. * <B><U> Actions</U> :</B><BR><BR>
  607. * <li>Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character </li><BR><BR>
  608. *
  609. */
  610. @Override
  611. protected void onEvtMuted(L2Character attacker)
  612. {
  613. // Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character
  614. onEvtAttacked(attacker);
  615. }
  616. /**
  617. * Launch actions corresponding to the Event ReadyToAct.<BR><BR>
  618. *
  619. * <B><U> Actions</U> :</B><BR><BR>
  620. * <li>Launch actions corresponding to the Event Think</li><BR><BR>
  621. *
  622. */
  623. @Override
  624. protected void onEvtReadyToAct()
  625. {
  626. // Launch actions corresponding to the Event Think
  627. onEvtThink();
  628. }
  629. /**
  630. * Do nothing.<BR><BR>
  631. */
  632. @Override
  633. protected void onEvtUserCmd(Object arg0, Object arg1)
  634. {
  635. // do nothing
  636. }
  637. /**
  638. * Launch actions corresponding to the Event Arrived.<BR><BR>
  639. *
  640. * <B><U> Actions</U> :</B><BR><BR>
  641. * <li>If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE</li>
  642. * <li>Launch actions corresponding to the Event Think</li><BR><BR>
  643. *
  644. */
  645. @Override
  646. protected void onEvtArrived()
  647. {
  648. // Launch an explore task if necessary
  649. if (Config.ACTIVATE_POSITION_RECORDER && _accessor.getActor() instanceof L2PcInstance)
  650. {
  651. ((L2PcInstance) _accessor.getActor()).explore();
  652. }
  653. _accessor.getActor().revalidateZone(true);
  654. if (_accessor.getActor().moveToNextRoutePoint())
  655. return;
  656. if (_accessor.getActor() instanceof L2Attackable)
  657. {
  658. ((L2Attackable) _accessor.getActor()).setisReturningToSpawnPoint(false);
  659. }
  660. clientStoppedMoving();
  661. // If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE
  662. if (getIntention() == AI_INTENTION_MOVE_TO)
  663. setIntention(AI_INTENTION_ACTIVE);
  664. // Launch actions corresponding to the Event Think
  665. onEvtThink();
  666. if (_actor instanceof L2BoatInstance)
  667. {
  668. ((L2BoatInstance) _actor).evtArrived();
  669. }
  670. else if (_actor instanceof L2AirShipInstance)
  671. {
  672. ((L2AirShipInstance) _actor).evtArrived();
  673. }
  674. }
  675. /**
  676. * Launch actions corresponding to the Event ArrivedRevalidate.<BR><BR>
  677. *
  678. * <B><U> Actions</U> :</B><BR><BR>
  679. * <li>Launch actions corresponding to the Event Think</li><BR><BR>
  680. *
  681. */
  682. @Override
  683. protected void onEvtArrivedRevalidate()
  684. {
  685. // Launch actions corresponding to the Event Think
  686. onEvtThink();
  687. }
  688. /**
  689. * Launch actions corresponding to the Event ArrivedBlocked.<BR><BR>
  690. *
  691. * <B><U> Actions</U> :</B><BR><BR>
  692. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  693. * <li>If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE</li>
  694. * <li>Launch actions corresponding to the Event Think</li><BR><BR>
  695. *
  696. */
  697. @Override
  698. protected void onEvtArrivedBlocked(L2CharPosition blocked_at_pos)
  699. {
  700. // If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE
  701. if (getIntention() == AI_INTENTION_MOVE_TO || getIntention() == AI_INTENTION_CAST)
  702. setIntention(AI_INTENTION_ACTIVE);
  703. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  704. clientStopMoving(blocked_at_pos);
  705. if (Config.ACTIVATE_POSITION_RECORDER && Universe.getInstance().shouldLog(_accessor.getActor().getObjectId()))
  706. {
  707. if (!_accessor.getActor().isFlying())
  708. Universe.getInstance().registerObstacle(blocked_at_pos.x, blocked_at_pos.y, blocked_at_pos.z);
  709. if (_accessor.getActor() instanceof L2PcInstance)
  710. ((L2PcInstance) _accessor.getActor()).explore();
  711. }
  712. // Launch actions corresponding to the Event Think
  713. onEvtThink();
  714. }
  715. /**
  716. * Launch actions corresponding to the Event ForgetObject.<BR><BR>
  717. *
  718. * <B><U> Actions</U> :</B><BR><BR>
  719. * <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>
  720. * <li>If the object was targeted to attack, stop the auto-attack, cancel target and set the Intention to AI_INTENTION_ACTIVE</li>
  721. * <li>If the object was targeted to cast, cancel target and set the Intention to AI_INTENTION_ACTIVE</li>
  722. * <li>If the object was targeted to follow, stop the movement, cancel AI Follow Task and set the Intention to AI_INTENTION_ACTIVE</li>
  723. * <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>
  724. *
  725. */
  726. @Override
  727. protected void onEvtForgetObject(L2Object object)
  728. {
  729. // If the object was targeted and the Intention was AI_INTENTION_INTERACT or AI_INTENTION_PICK_UP, set the Intention to AI_INTENTION_ACTIVE
  730. if (getTarget() == object)
  731. {
  732. setTarget(null);
  733. if (getIntention() == AI_INTENTION_INTERACT)
  734. setIntention(AI_INTENTION_ACTIVE);
  735. else if (getIntention() == AI_INTENTION_PICK_UP)
  736. setIntention(AI_INTENTION_ACTIVE);
  737. }
  738. // Check if the object was targeted to attack
  739. if (getAttackTarget() == object)
  740. {
  741. // Cancel attack target
  742. setAttackTarget(null);
  743. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  744. setIntention(AI_INTENTION_ACTIVE);
  745. }
  746. // Check if the object was targeted to cast
  747. if (getCastTarget() == object)
  748. {
  749. // Cancel cast target
  750. setCastTarget(null);
  751. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  752. setIntention(AI_INTENTION_ACTIVE);
  753. }
  754. // Check if the object was targeted to follow
  755. if (getFollowTarget() == object)
  756. {
  757. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  758. clientStopMoving(null);
  759. // Stop an AI Follow Task
  760. stopFollow();
  761. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  762. setIntention(AI_INTENTION_ACTIVE);
  763. }
  764. // Check if the targeted object was the actor
  765. if (_actor == object)
  766. {
  767. // Cancel AI target
  768. setTarget(null);
  769. setAttackTarget(null);
  770. setCastTarget(null);
  771. // Stop an AI Follow Task
  772. stopFollow();
  773. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  774. clientStopMoving(null);
  775. // Set the Intention of this AbstractAI to AI_INTENTION_IDLE
  776. changeIntention(AI_INTENTION_IDLE, null, null);
  777. }
  778. }
  779. /**
  780. * Launch actions corresponding to the Event Cancel.<BR><BR>
  781. *
  782. * <B><U> Actions</U> :</B><BR><BR>
  783. * <li>Stop an AI Follow Task</li>
  784. * <li>Launch actions corresponding to the Event Think</li><BR><BR>
  785. *
  786. */
  787. @Override
  788. protected void onEvtCancel()
  789. {
  790. _actor.abortCast();
  791. // Stop an AI Follow Task
  792. stopFollow();
  793. if (!AttackStanceTaskManager.getInstance().getAttackStanceTask(_actor))
  794. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  795. // Launch actions corresponding to the Event Think
  796. onEvtThink();
  797. }
  798. /**
  799. * Launch actions corresponding to the Event Dead.<BR><BR>
  800. *
  801. * <B><U> Actions</U> :</B><BR><BR>
  802. * <li>Stop an AI Follow Task</li>
  803. * <li>Kill the actor client side by sending Server->Client packet AutoAttackStop, StopMove/StopRotation, Die (broadcast)</li><BR><BR>
  804. *
  805. */
  806. @Override
  807. protected void onEvtDead()
  808. {
  809. // Stop an AI Follow Task
  810. stopFollow();
  811. // Kill the actor client side by sending Server->Client packet AutoAttackStop, StopMove/StopRotation, Die (broadcast)
  812. clientNotifyDead();
  813. if (!(_actor instanceof L2PcInstance))
  814. _actor.setWalking();
  815. }
  816. /**
  817. * Launch actions corresponding to the Event Fake Death.<BR><BR>
  818. *
  819. * <B><U> Actions</U> :</B><BR><BR>
  820. * <li>Stop an AI Follow Task</li>
  821. *
  822. */
  823. @Override
  824. protected void onEvtFakeDeath()
  825. {
  826. // Stop an AI Follow Task
  827. stopFollow();
  828. // Stop the actor movement and send Server->Client packet StopMove/StopRotation (broadcast)
  829. clientStopMoving(null);
  830. // Init AI
  831. _intention = AI_INTENTION_IDLE;
  832. setTarget(null);
  833. setCastTarget(null);
  834. setAttackTarget(null);
  835. }
  836. /**
  837. * Do nothing.<BR><BR>
  838. */
  839. @Override
  840. protected void onEvtFinishCasting()
  841. {
  842. // do nothing
  843. }
  844. protected boolean maybeMoveToPosition(Point3D worldPosition, int offset)
  845. {
  846. if (worldPosition == null)
  847. {
  848. _log.warning("maybeMoveToPosition: worldPosition == NULL!");
  849. return false;
  850. }
  851. if (offset < 0)
  852. return false; // skill radius -1
  853. if (!_actor.isInsideRadius(worldPosition.getX(), worldPosition.getY(), offset + _actor.getTemplate().collisionRadius, false))
  854. {
  855. if (_actor.isMovementDisabled())
  856. return true;
  857. if (!_actor.isRunning() && !(this instanceof L2PlayerAI) && !(this instanceof L2SummonAI))
  858. _actor.setRunning();
  859. stopFollow();
  860. int x = _actor.getX();
  861. int y = _actor.getY();
  862. double dx = worldPosition.getX() - x;
  863. double dy = worldPosition.getY() - y;
  864. double dist = Math.sqrt(dx * dx + dy * dy);
  865. double sin = dy / dist;
  866. double cos = dx / dist;
  867. dist -= offset - 5;
  868. x += (int) (dist * cos);
  869. y += (int) (dist * sin);
  870. moveTo(x, y, worldPosition.getZ());
  871. return true;
  872. }
  873. if (getFollowTarget() != null)
  874. stopFollow();
  875. return false;
  876. }
  877. /**
  878. * Manage the Move to Pawn action in function of the distance and of the Interact area.<BR><BR>
  879. *
  880. * <B><U> Actions</U> :</B><BR><BR>
  881. * <li>Get the distance between the current position of the L2Character and the target (x,y)</li>
  882. * <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>
  883. * <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>
  884. *
  885. * <B><U> Example of use </U> :</B><BR><BR>
  886. * <li> L2PLayerAI, L2SummonAI</li><BR><BR>
  887. *
  888. * @param target The targeted L2Object
  889. * @param offset The Interact area radius
  890. *
  891. * @return True if a movement must be done
  892. *
  893. */
  894. protected boolean maybeMoveToPawn(L2Object target, int offset)
  895. {
  896. // Get the distance between the current position of the L2Character and the target (x,y)
  897. if (target == null)
  898. {
  899. _log.warning("maybeMoveToPawn: target == NULL!");
  900. return false;
  901. }
  902. if (offset < 0)
  903. return false; // skill radius -1
  904. offset += _actor.getTemplate().collisionRadius;
  905. if (target instanceof L2Character)
  906. offset += ((L2Character) target).getTemplate().collisionRadius;
  907. if (!_actor.isInsideRadius(target, offset, false, false))
  908. {
  909. // Caller should be L2Playable and thinkAttack/thinkCast/thinkInteract/thinkPickUp
  910. if (getFollowTarget() != null)
  911. {
  912. // allow larger hit range when the target is moving (check is run only once per second)
  913. if (!_actor.isInsideRadius(target, offset + 100, false, false))
  914. return true;
  915. stopFollow();
  916. return false;
  917. }
  918. if (_actor.isMovementDisabled())
  919. return true;
  920. // while flying there is no move to cast
  921. if (_actor.getAI().getIntention() == CtrlIntention.AI_INTENTION_CAST &&
  922. _actor instanceof L2PcInstance && ((L2PcInstance)_actor).isTransformed())
  923. {
  924. if (!((L2PcInstance)_actor).getTransformation().canStartFollowToCast())
  925. {
  926. ((L2PcInstance)_actor).sendPacket(new SystemMessage(SystemMessageId.DIST_TOO_FAR_CASTING_STOPPED));
  927. ((L2PcInstance)_actor).sendPacket(ActionFailed.STATIC_PACKET);
  928. return true;
  929. }
  930. }
  931. // If not running, set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  932. if (!_actor.isRunning() && !(this instanceof L2PlayerAI) && !(this instanceof L2SummonAI))
  933. _actor.setRunning();
  934. stopFollow();
  935. if ((target instanceof L2Character) && !(target instanceof L2DoorInstance))
  936. {
  937. if (((L2Character) target).isMoving())
  938. offset -= 100;
  939. if (offset < 5)
  940. offset = 5;
  941. startFollow((L2Character) target, offset);
  942. }
  943. else
  944. {
  945. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  946. moveToPawn(target, offset);
  947. }
  948. return true;
  949. }
  950. if (getFollowTarget() != null)
  951. stopFollow();
  952. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  953. // clientStopMoving(null);
  954. return false;
  955. }
  956. /**
  957. * Modify current Intention and actions if the target is lost or dead.<BR><BR>
  958. *
  959. * <B><U> Actions</U> : <I>If the target is lost or dead</I></B><BR><BR>
  960. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  961. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  962. * <li>Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE</li><BR><BR>
  963. *
  964. * <B><U> Example of use </U> :</B><BR><BR>
  965. * <li> L2PLayerAI, L2SummonAI</li><BR><BR>
  966. *
  967. * @param target The targeted L2Object
  968. *
  969. * @return True if the target is lost or dead (false if fakedeath)
  970. *
  971. */
  972. protected boolean checkTargetLostOrDead(L2Character target)
  973. {
  974. if (target == null || target.isAlikeDead())
  975. {
  976. //check if player is fakedeath
  977. if (target instanceof L2PcInstance && ((L2PcInstance)target).isFakeDeath())
  978. {
  979. target.stopFakeDeath(null);
  980. return false;
  981. }
  982. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  983. setIntention(AI_INTENTION_ACTIVE);
  984. return true;
  985. }
  986. return false;
  987. }
  988. /**
  989. * Modify current Intention and actions if the target is lost.<BR><BR>
  990. *
  991. * <B><U> Actions</U> : <I>If the target is lost</I></B><BR><BR>
  992. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  993. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  994. * <li>Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE</li><BR><BR>
  995. *
  996. * <B><U> Example of use </U> :</B><BR><BR>
  997. * <li> L2PLayerAI, L2SummonAI</li><BR><BR>
  998. *
  999. * @param target The targeted L2Object
  1000. *
  1001. * @return True if the target is lost
  1002. *
  1003. */
  1004. protected boolean checkTargetLost(L2Object target)
  1005. {
  1006. // check if player is fakedeath
  1007. if (target instanceof L2PcInstance)
  1008. {
  1009. L2PcInstance target2 = (L2PcInstance) target; //convert object to chara
  1010. if (target2.isFakeDeath())
  1011. {
  1012. target2.stopFakeDeath(null);
  1013. return false;
  1014. }
  1015. }
  1016. if (target == null)
  1017. {
  1018. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  1019. setIntention(AI_INTENTION_ACTIVE);
  1020. return true;
  1021. }
  1022. return false;
  1023. }
  1024. protected class SelfAnalysis
  1025. {
  1026. public boolean isMage = false;
  1027. public boolean isBalanced;
  1028. public boolean isArcher = false;
  1029. public boolean isHealer = false;
  1030. public boolean isFighter = false;
  1031. public boolean cannotMoveOnLand = false;
  1032. public List<L2Skill> generalSkills = new FastList<L2Skill>();
  1033. public List<L2Skill> buffSkills = new FastList<L2Skill>();
  1034. public int lastBuffTick = 0;
  1035. public List<L2Skill> debuffSkills = new FastList<L2Skill>();
  1036. public int lastDebuffTick = 0;
  1037. public List<L2Skill> cancelSkills = new FastList<L2Skill>();
  1038. public List<L2Skill> healSkills = new FastList<L2Skill>();
  1039. //public List<L2Skill> trickSkills = new FastList<L2Skill>();
  1040. public List<L2Skill> generalDisablers = new FastList<L2Skill>();
  1041. public List<L2Skill> sleepSkills = new FastList<L2Skill>();
  1042. public List<L2Skill> rootSkills = new FastList<L2Skill>();
  1043. public List<L2Skill> muteSkills = new FastList<L2Skill>();
  1044. public List<L2Skill> resurrectSkills = new FastList<L2Skill>();
  1045. public boolean hasHealOrResurrect = false;
  1046. public boolean hasLongRangeSkills = false;
  1047. public boolean hasLongRangeDamageSkills = false;
  1048. public int maxCastRange = 0;
  1049. public SelfAnalysis()
  1050. {
  1051. }
  1052. public void init()
  1053. {
  1054. switch (((L2NpcTemplate) _actor.getTemplate()).AI)
  1055. {
  1056. case FIGHTER:
  1057. isFighter = true;
  1058. break;
  1059. case MAGE:
  1060. isMage = true;
  1061. break;
  1062. case CORPSE:
  1063. case BALANCED:
  1064. isBalanced = true;
  1065. break;
  1066. case ARCHER:
  1067. isArcher = true;
  1068. break;
  1069. case HEALER:
  1070. isHealer = true;
  1071. break;
  1072. default:
  1073. isFighter = true;
  1074. break;
  1075. }
  1076. // water movement analysis
  1077. if (_actor instanceof L2Npc)
  1078. {
  1079. int npcId = ((L2Npc) _actor).getNpcId();
  1080. switch (npcId)
  1081. {
  1082. case 20314: // great white shark
  1083. case 20849: // Light Worm
  1084. cannotMoveOnLand = true;
  1085. break;
  1086. default:
  1087. cannotMoveOnLand = false;
  1088. break;
  1089. }
  1090. }
  1091. // skill analysis
  1092. for (L2Skill sk : _actor.getAllSkills())
  1093. {
  1094. if (sk.isPassive())
  1095. continue;
  1096. int castRange = sk.getCastRange();
  1097. boolean hasLongRangeDamageSkill = false;
  1098. switch (sk.getSkillType())
  1099. {
  1100. case HEAL:
  1101. case HEAL_PERCENT:
  1102. case HEAL_STATIC:
  1103. case BALANCE_LIFE:
  1104. case HOT:
  1105. healSkills.add(sk);
  1106. hasHealOrResurrect = true;
  1107. continue; // won't be considered something for fighting
  1108. case BUFF:
  1109. buffSkills.add(sk);
  1110. continue; // won't be considered something for fighting
  1111. case PARALYZE:
  1112. case STUN:
  1113. // hardcoding petrification until improvements are made to
  1114. // EffectTemplate... petrification is totally different for
  1115. // AI than paralyze
  1116. switch (sk.getId())
  1117. {
  1118. case 367:
  1119. case 4111:
  1120. case 4383:
  1121. case 4616:
  1122. case 4578:
  1123. sleepSkills.add(sk);
  1124. break;
  1125. default:
  1126. generalDisablers.add(sk);
  1127. break;
  1128. }
  1129. break;
  1130. case MUTE:
  1131. muteSkills.add(sk);
  1132. break;
  1133. case SLEEP:
  1134. sleepSkills.add(sk);
  1135. break;
  1136. case ROOT:
  1137. rootSkills.add(sk);
  1138. break;
  1139. case FEAR: // could be used as an alternative for healing?
  1140. case CONFUSION:
  1141. // trickSkills.add(sk);
  1142. case DEBUFF:
  1143. debuffSkills.add(sk);
  1144. break;
  1145. case CANCEL:
  1146. case MAGE_BANE:
  1147. case WARRIOR_BANE:
  1148. case NEGATE:
  1149. cancelSkills.add(sk);
  1150. break;
  1151. case RESURRECT:
  1152. resurrectSkills.add(sk);
  1153. hasHealOrResurrect = true;
  1154. break;
  1155. case NOTDONE:
  1156. case COREDONE:
  1157. continue; // won't be considered something for fighting
  1158. default:
  1159. generalSkills.add(sk);
  1160. hasLongRangeDamageSkill = true;
  1161. break;
  1162. }
  1163. if (castRange > 70)
  1164. {
  1165. hasLongRangeSkills = true;
  1166. if (hasLongRangeDamageSkill)
  1167. hasLongRangeDamageSkills = true;
  1168. }
  1169. if (castRange > maxCastRange)
  1170. maxCastRange = castRange;
  1171. }
  1172. // Because of missing skills, some mages/balanced cannot play like mages
  1173. if (!hasLongRangeDamageSkills && isMage)
  1174. {
  1175. isBalanced = true;
  1176. isMage = false;
  1177. isFighter = false;
  1178. }
  1179. if (!hasLongRangeSkills && (isMage || isBalanced))
  1180. {
  1181. isBalanced = false;
  1182. isMage = false;
  1183. isFighter = true;
  1184. }
  1185. if (generalSkills.isEmpty() && isMage)
  1186. {
  1187. isBalanced = true;
  1188. isMage = false;
  1189. }
  1190. }
  1191. }
  1192. protected class TargetAnalysis
  1193. {
  1194. public L2Character character;
  1195. public boolean isMage;
  1196. public boolean isBalanced;
  1197. public boolean isArcher;
  1198. public boolean isFighter;
  1199. public boolean isCanceled;
  1200. public boolean isSlower;
  1201. public boolean isMagicResistant;
  1202. public TargetAnalysis()
  1203. {
  1204. }
  1205. public void update(L2Character target)
  1206. {
  1207. // update status once in 4 seconds
  1208. if (target == character && Rnd.nextInt(100) > 25)
  1209. return;
  1210. character = target;
  1211. if (target == null)
  1212. return;
  1213. isMage = false;
  1214. isBalanced = false;
  1215. isArcher = false;
  1216. isFighter = false;
  1217. isCanceled = false;
  1218. if (target.getMAtk(null, null) > 1.5 * target.getPAtk(null))
  1219. isMage = true;
  1220. else if (target.getPAtk(null) * 0.8 < target.getMAtk(null, null) || target.getMAtk(null, null) * 0.8 > target.getPAtk(null))
  1221. {
  1222. isBalanced = true;
  1223. }
  1224. else
  1225. {
  1226. L2Weapon weapon = target.getActiveWeaponItem();
  1227. if (weapon != null && (weapon.getItemType() == L2WeaponType.BOW || weapon.getItemType() == L2WeaponType.CROSSBOW))
  1228. isArcher = true;
  1229. else
  1230. isFighter = true;
  1231. }
  1232. if (target.getRunSpeed() < _actor.getRunSpeed() - 3)
  1233. isSlower = true;
  1234. else
  1235. isSlower = false;
  1236. if (target.getMDef(null, null) * 1.2 > _actor.getMAtk(null, null))
  1237. isMagicResistant = true;
  1238. else
  1239. isMagicResistant = false;
  1240. if (target.getBuffCount() < 4)
  1241. isCanceled = true;
  1242. }
  1243. }
  1244. }