L2PlayerAI.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.ai;
  16. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  17. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_CAST;
  18. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  19. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_INTERACT;
  20. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_MOVE_TO;
  21. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_PICK_UP;
  22. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_REST;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.gameserver.model.L2CharPosition;
  25. import com.l2jserver.gameserver.model.L2Object;
  26. import com.l2jserver.gameserver.model.L2Skill;
  27. import com.l2jserver.gameserver.model.L2Skill.SkillTargetType;
  28. import com.l2jserver.gameserver.model.actor.L2Character;
  29. import com.l2jserver.gameserver.model.actor.L2Character.AIAccessor;
  30. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  31. import com.l2jserver.gameserver.model.actor.instance.L2StaticObjectInstance;
  32. public class L2PlayerAI extends L2PlayableAI
  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. _accessor.doAttack(target);
  224. }
  225. private void thinkCast()
  226. {
  227. L2Character target = getCastTarget();
  228. if (Config.DEBUG)
  229. _log.warning("L2PlayerAI: thinkCast -> Start");
  230. if (_skill.getTargetType() == SkillTargetType.TARGET_GROUND && _actor instanceof L2PcInstance)
  231. {
  232. if (maybeMoveToPosition(((L2PcInstance) _actor).getCurrentSkillWorldPosition(), _actor.getMagicalAttackRange(_skill)))
  233. {
  234. _actor.setIsCastingNow(false);
  235. return;
  236. }
  237. }
  238. else
  239. {
  240. if (checkTargetLost(target))
  241. {
  242. if (_skill.isOffensive() && getAttackTarget() != null)
  243. {
  244. //Notify the target
  245. setCastTarget(null);
  246. }
  247. _actor.setIsCastingNow(false);
  248. return;
  249. }
  250. if (target != null && maybeMoveToPawn(target, _actor.getMagicalAttackRange(_skill)))
  251. {
  252. _actor.setIsCastingNow(false);
  253. return;
  254. }
  255. }
  256. if (_skill.getHitTime() > 50)
  257. clientStopMoving(null);
  258. L2Object oldTarget = _actor.getTarget();
  259. if (oldTarget != null && target != null && oldTarget != target)
  260. {
  261. // Replace the current target by the cast target
  262. _actor.setTarget(getCastTarget());
  263. // Launch the Cast of the skill
  264. _accessor.doCast(_skill);
  265. // Restore the initial target
  266. _actor.setTarget(oldTarget);
  267. }
  268. else
  269. _accessor.doCast(_skill);
  270. }
  271. private void thinkPickUp()
  272. {
  273. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  274. return;
  275. L2Object target = getTarget();
  276. if (checkTargetLost(target))
  277. return;
  278. if (maybeMoveToPawn(target, 36))
  279. return;
  280. setIntention(AI_INTENTION_IDLE);
  281. ((L2PcInstance.AIAccessor) _accessor).doPickupItem(target);
  282. }
  283. private void thinkInteract()
  284. {
  285. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  286. return;
  287. L2Object target = getTarget();
  288. if (checkTargetLost(target))
  289. return;
  290. if (maybeMoveToPawn(target, 36))
  291. return;
  292. if (!(target instanceof L2StaticObjectInstance))
  293. ((L2PcInstance.AIAccessor) _accessor).doInteract((L2Character) target);
  294. setIntention(AI_INTENTION_IDLE);
  295. }
  296. @Override
  297. protected void onEvtThink()
  298. {
  299. if (_thinking && getIntention() != AI_INTENTION_CAST) // casting must always continue
  300. return;
  301. /*
  302. if (Config.DEBUG)
  303. _log.warning("L2PlayerAI: onEvtThink -> Check intention");
  304. */
  305. _thinking = true;
  306. try
  307. {
  308. if (getIntention() == AI_INTENTION_ATTACK)
  309. thinkAttack();
  310. else if (getIntention() == AI_INTENTION_CAST)
  311. thinkCast();
  312. else if (getIntention() == AI_INTENTION_PICK_UP)
  313. thinkPickUp();
  314. else if (getIntention() == AI_INTENTION_INTERACT)
  315. thinkInteract();
  316. }
  317. finally
  318. {
  319. _thinking = false;
  320. }
  321. }
  322. }