L2PlayerAI.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.ai;
  16. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  17. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_CAST;
  18. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  19. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_INTERACT;
  20. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_MOVE_TO;
  21. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_PICK_UP;
  22. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_REST;
  23. import net.sf.l2j.Config;
  24. import net.sf.l2j.gameserver.model.L2CharPosition;
  25. import net.sf.l2j.gameserver.model.L2Object;
  26. import net.sf.l2j.gameserver.model.L2Skill;
  27. import net.sf.l2j.gameserver.model.L2Skill.SkillTargetType;
  28. import net.sf.l2j.gameserver.model.actor.L2Character;
  29. import net.sf.l2j.gameserver.model.actor.L2Character.AIAccessor;
  30. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  31. import net.sf.l2j.gameserver.model.actor.instance.L2StaticObjectInstance;
  32. public class L2PlayerAI extends L2CharacterAI
  33. {
  34. private boolean _thinking; // to prevent recursive thinking
  35. IntentionCommand _nextIntention = null;
  36. public L2PlayerAI(AIAccessor accessor)
  37. {
  38. super(accessor);
  39. }
  40. void saveNextIntention(CtrlIntention intention, Object arg0, Object arg1)
  41. {
  42. _nextIntention = new IntentionCommand(intention, arg0, arg1);
  43. }
  44. @Override
  45. public IntentionCommand getNextIntention()
  46. {
  47. return _nextIntention;
  48. }
  49. /**
  50. * Saves the current Intention for this L2PlayerAI if necessary and calls changeIntention in AbstractAI.<BR><BR>
  51. *
  52. * @param intention The new Intention to set to the AI
  53. * @param arg0 The first parameter of the Intention
  54. * @param arg1 The second parameter of the Intention
  55. *
  56. */
  57. @Override
  58. synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
  59. {
  60. /*
  61. if (Config.DEBUG)
  62. _log.warning("L2PlayerAI: changeIntention -> " + intention + " " + arg0 + " " + arg1);
  63. */
  64. // do nothing unless CAST intention
  65. // however, forget interrupted actions when starting to use an offensive skill
  66. if (intention != AI_INTENTION_CAST || (arg0 != null && ((L2Skill) arg0).isOffensive()))
  67. {
  68. _nextIntention = null;
  69. super.changeIntention(intention, arg0, arg1);
  70. return;
  71. }
  72. // do nothing if next intention is same as current one.
  73. if (intention == _intention && arg0 == _intentionArg0 && arg1 == _intentionArg1)
  74. {
  75. super.changeIntention(intention, arg0, arg1);
  76. return;
  77. }
  78. // save current intention so it can be used after cast
  79. saveNextIntention(_intention, _intentionArg0, _intentionArg1);
  80. super.changeIntention(intention, arg0, arg1);
  81. }
  82. /**
  83. * Launch actions corresponding to the Event ReadyToAct.<BR><BR>
  84. *
  85. * <B><U> Actions</U> :</B><BR><BR>
  86. * <li>Launch actions corresponding to the Event Think</li><BR><BR>
  87. *
  88. */
  89. @Override
  90. protected void onEvtReadyToAct()
  91. {
  92. // Launch actions corresponding to the Event Think
  93. if (_nextIntention != null)
  94. {
  95. setIntention(_nextIntention._crtlIntention, _nextIntention._arg0, _nextIntention._arg1);
  96. _nextIntention = null;
  97. }
  98. super.onEvtReadyToAct();
  99. }
  100. /**
  101. * Launch actions corresponding to the Event Cancel.<BR><BR>
  102. *
  103. * <B><U> Actions</U> :</B><BR><BR>
  104. * <li>Stop an AI Follow Task</li>
  105. * <li>Launch actions corresponding to the Event Think</li><BR><BR>
  106. *
  107. */
  108. @Override
  109. protected void onEvtCancel()
  110. {
  111. _nextIntention = null;
  112. super.onEvtCancel();
  113. }
  114. /**
  115. * Finalize the casting of a skill. This method overrides L2CharacterAI method.<BR><BR>
  116. *
  117. * <B>What it does:</B>
  118. * Check if actual intention is set to CAST and, if so, retrieves latest intention
  119. * before the actual CAST and set it as the current intention for the player
  120. */
  121. @Override
  122. protected void onEvtFinishCasting()
  123. {
  124. if (getIntention() == AI_INTENTION_CAST)
  125. {
  126. // run interrupted or next intention
  127. IntentionCommand nextIntention = _nextIntention;
  128. if (nextIntention != null)
  129. {
  130. if (nextIntention._crtlIntention != AI_INTENTION_CAST) // previous state shouldn't be casting
  131. {
  132. setIntention(nextIntention._crtlIntention, nextIntention._arg0, nextIntention._arg1);
  133. }
  134. else
  135. setIntention(AI_INTENTION_IDLE);
  136. }
  137. else
  138. {
  139. /*
  140. if (Config.DEBUG)
  141. _log.warning("L2PlayerAI: no previous intention set... Setting it to IDLE");
  142. */
  143. // set intention to idle if skill doesn't change intention.
  144. setIntention(AI_INTENTION_IDLE);
  145. }
  146. }
  147. }
  148. @Override
  149. protected void onIntentionRest()
  150. {
  151. if (getIntention() != AI_INTENTION_REST)
  152. {
  153. changeIntention(AI_INTENTION_REST, null, null);
  154. setTarget(null);
  155. if (getAttackTarget() != null)
  156. {
  157. setAttackTarget(null);
  158. }
  159. clientStopMoving(null);
  160. }
  161. }
  162. @Override
  163. protected void onIntentionActive()
  164. {
  165. setIntention(AI_INTENTION_IDLE);
  166. }
  167. /**
  168. * Manage the Move To Intention : Stop current Attack and Launch a Move to Location Task.<BR><BR>
  169. *
  170. * <B><U> Actions</U> : </B><BR><BR>
  171. * <li>Stop the actor auto-attack server side AND client side by sending Server->Client packet AutoAttackStop (broadcast) </li>
  172. * <li>Set the Intention of this AI to AI_INTENTION_MOVE_TO </li>
  173. * <li>Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast) </li><BR><BR>
  174. *
  175. */
  176. @Override
  177. protected void onIntentionMoveTo(L2CharPosition pos)
  178. {
  179. if (getIntention() == AI_INTENTION_REST)
  180. {
  181. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  182. clientActionFailed();
  183. return;
  184. }
  185. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow() || _actor.isAttackingNow())
  186. {
  187. clientActionFailed();
  188. saveNextIntention(AI_INTENTION_MOVE_TO, pos, null);
  189. return;
  190. }
  191. // Set the Intention of this AbstractAI to AI_INTENTION_MOVE_TO
  192. changeIntention(AI_INTENTION_MOVE_TO, pos, null);
  193. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  194. clientStopAutoAttack();
  195. // Abort the attack of the L2Character and send Server->Client ActionFailed packet
  196. _actor.abortAttack();
  197. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  198. moveTo(pos.x, pos.y, pos.z);
  199. }
  200. @Override
  201. protected void clientNotifyDead()
  202. {
  203. _clientMovingToPawnOffset = 0;
  204. _clientMoving = false;
  205. super.clientNotifyDead();
  206. }
  207. private void thinkAttack()
  208. {
  209. L2Character target = getAttackTarget();
  210. if (target == null)
  211. return;
  212. if (checkTargetLostOrDead(target))
  213. {
  214. if (target != null)
  215. {
  216. // Notify the target
  217. setAttackTarget(null);
  218. }
  219. return;
  220. }
  221. if (maybeMoveToPawn(target, _actor.getPhysicalAttackRange()))
  222. return;
  223. // stop invul effect if exist
  224. if (_actor.getInvulEffect() != null)
  225. _actor.getInvulEffect().exit();
  226. _accessor.doAttack(target);
  227. }
  228. private void thinkCast()
  229. {
  230. L2Character target = getCastTarget();
  231. if (Config.DEBUG)
  232. _log.warning("L2PlayerAI: thinkCast -> Start");
  233. if (_skill.getTargetType() == SkillTargetType.TARGET_GROUND && _actor instanceof L2PcInstance)
  234. {
  235. if (maybeMoveToPosition(((L2PcInstance) _actor).getCurrentSkillWorldPosition(), _actor.getMagicalAttackRange(_skill)))
  236. {
  237. _actor.setIsCastingNow(false);
  238. return;
  239. }
  240. }
  241. else
  242. {
  243. if (checkTargetLost(target))
  244. {
  245. if (_skill.isOffensive() && getAttackTarget() != null)
  246. {
  247. //Notify the target
  248. setCastTarget(null);
  249. }
  250. _actor.setIsCastingNow(false);
  251. return;
  252. }
  253. if (target != null && maybeMoveToPawn(target, _actor.getMagicalAttackRange(_skill)))
  254. {
  255. _actor.setIsCastingNow(false);
  256. return;
  257. }
  258. }
  259. if (_skill.getHitTime() > 50)
  260. clientStopMoving(null);
  261. // stop invul effect if exist
  262. if (_actor.getInvulEffect() != null)
  263. _actor.getInvulEffect().exit();
  264. L2Object oldTarget = _actor.getTarget();
  265. if (oldTarget != null && target != null && oldTarget != target)
  266. {
  267. // Replace the current target by the cast target
  268. _actor.setTarget(getCastTarget());
  269. // Launch the Cast of the skill
  270. _accessor.doCast(_skill);
  271. // Restore the initial target
  272. _actor.setTarget(oldTarget);
  273. }
  274. else
  275. _accessor.doCast(_skill);
  276. }
  277. private void thinkPickUp()
  278. {
  279. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  280. return;
  281. L2Object target = getTarget();
  282. if (checkTargetLost(target))
  283. return;
  284. if (maybeMoveToPawn(target, 36))
  285. return;
  286. setIntention(AI_INTENTION_IDLE);
  287. ((L2PcInstance.AIAccessor) _accessor).doPickupItem(target);
  288. }
  289. private void thinkInteract()
  290. {
  291. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  292. return;
  293. L2Object target = getTarget();
  294. if (checkTargetLost(target))
  295. return;
  296. if (maybeMoveToPawn(target, 36))
  297. return;
  298. if (!(target instanceof L2StaticObjectInstance))
  299. ((L2PcInstance.AIAccessor) _accessor).doInteract((L2Character) target);
  300. setIntention(AI_INTENTION_IDLE);
  301. }
  302. @Override
  303. protected void onEvtThink()
  304. {
  305. if (_thinking && getIntention() != AI_INTENTION_CAST) // casting must always continue
  306. return;
  307. /*
  308. if (Config.DEBUG)
  309. _log.warning("L2PlayerAI: onEvtThink -> Check intention");
  310. */
  311. _thinking = true;
  312. try
  313. {
  314. if (getIntention() == AI_INTENTION_ATTACK)
  315. thinkAttack();
  316. else if (getIntention() == AI_INTENTION_CAST)
  317. thinkCast();
  318. else if (getIntention() == AI_INTENTION_PICK_UP)
  319. thinkPickUp();
  320. else if (getIntention() == AI_INTENTION_INTERACT)
  321. thinkInteract();
  322. }
  323. finally
  324. {
  325. _thinking = false;
  326. }
  327. }
  328. }