L2PlayerAI.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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_ATTACK;
  21. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_CAST;
  22. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  23. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_INTERACT;
  24. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_PICK_UP;
  25. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_REST;
  26. import java.util.EmptyStackException;
  27. import java.util.Stack;
  28. import net.sf.l2j.gameserver.ThreadPoolManager;
  29. import net.sf.l2j.gameserver.model.L2Character;
  30. import net.sf.l2j.gameserver.model.L2Object;
  31. import net.sf.l2j.gameserver.model.L2Character.AIAccessor;
  32. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  33. import net.sf.l2j.gameserver.model.actor.instance.L2StaticObjectInstance;
  34. import net.sf.l2j.gameserver.model.actor.knownlist.ObjectKnownList.KnownListAsynchronousUpdateTask;
  35. public class L2PlayerAI extends L2CharacterAI
  36. {
  37. private boolean _thinking; // to prevent recursive thinking
  38. class IntentionCommand
  39. {
  40. protected CtrlIntention _crtlIntention;
  41. protected Object _arg0, _arg1;
  42. protected IntentionCommand(CtrlIntention pIntention, Object pArg0, Object pArg1)
  43. {
  44. _crtlIntention = pIntention;
  45. _arg0 = pArg0;
  46. _arg1 = pArg1;
  47. }
  48. }
  49. private Stack<IntentionCommand> _interuptedIntentions = new Stack<IntentionCommand>();
  50. public L2PlayerAI(AIAccessor accessor)
  51. {
  52. super(accessor);
  53. }
  54. /**
  55. * Saves the current Intention for this L2PlayerAI if necessary and calls changeIntention in AbstractAI.<BR><BR>
  56. *
  57. * @param intention The new Intention to set to the AI
  58. * @param arg0 The first parameter of the Intention
  59. * @param arg1 The second parameter of the Intention
  60. *
  61. */
  62. @Override
  63. synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
  64. {
  65. /*
  66. if (Config.DEBUG)
  67. _log.warning("L2PlayerAI: changeIntention -> " + intention + " " + arg0 + " " + arg1);
  68. */
  69. // nothing to do if it does not CAST intention
  70. if (intention != AI_INTENTION_CAST)
  71. {
  72. super.changeIntention(intention, arg0, arg1);
  73. return;
  74. }
  75. // do nothing if next intention is same as current one.
  76. if (intention == _intention && arg0 == _intentionArg0 && arg1 == _intentionArg1)
  77. {
  78. super.changeIntention(intention, arg0, arg1);
  79. return;
  80. }
  81. /*
  82. if (Config.DEBUG)
  83. _log.warning("L2PlayerAI: changeIntention -> Saving current intention: " + _intention + " " + _intention_arg0 + " " + _intention_arg1);
  84. */
  85. // push current intention to stack
  86. _interuptedIntentions.push(new IntentionCommand(_intention, _intentionArg0, _intentionArg1));
  87. super.changeIntention(intention, arg0, arg1);
  88. }
  89. /**
  90. * Finalize the casting of a skill. This method overrides L2CharacterAI method.<BR><BR>
  91. *
  92. * <B>What it does:</B>
  93. * Check if actual intention is set to CAST and, if so, retrieves latest intention
  94. * before the actual CAST and set it as the current intention for the player
  95. */
  96. @Override
  97. protected void onEvtFinishCasting()
  98. {
  99. // forget interupted actions after offensive skill
  100. if (_skill != null && _skill.isOffensive()) _interuptedIntentions.clear();
  101. if (getIntention() == AI_INTENTION_CAST)
  102. {
  103. // run interupted intention if it remain.
  104. if (!_interuptedIntentions.isEmpty())
  105. {
  106. IntentionCommand cmd = null;
  107. try
  108. {
  109. cmd = _interuptedIntentions.pop();
  110. }
  111. catch (EmptyStackException ese)
  112. {
  113. }
  114. /*
  115. if (Config.DEBUG)
  116. _log.warning("L2PlayerAI: onEvtFinishCasting -> " + cmd._intention + " " + cmd._arg0 + " " + cmd._arg1);
  117. */
  118. if (cmd != null && cmd._crtlIntention != AI_INTENTION_CAST) // previous state shouldn't be casting
  119. {
  120. setIntention(cmd._crtlIntention, cmd._arg0, cmd._arg1);
  121. }
  122. else setIntention(AI_INTENTION_IDLE);
  123. }
  124. else
  125. {
  126. /*
  127. if (Config.DEBUG)
  128. _log.warning("L2PlayerAI: no previous intention set... Setting it to IDLE");
  129. */
  130. // set intention to idle if skill doesn't change intention.
  131. setIntention(AI_INTENTION_IDLE);
  132. }
  133. }
  134. }
  135. @Override
  136. protected void onIntentionRest()
  137. {
  138. if (getIntention() != AI_INTENTION_REST)
  139. {
  140. changeIntention(AI_INTENTION_REST, null, null);
  141. setTarget(null);
  142. if (getAttackTarget() != null)
  143. {
  144. setAttackTarget(null);
  145. }
  146. clientStopMoving(null);
  147. }
  148. }
  149. @Override
  150. protected void onIntentionActive()
  151. {
  152. setIntention(AI_INTENTION_IDLE);
  153. }
  154. @Override
  155. protected void clientNotifyDead()
  156. {
  157. _clientMovingToPawnOffset = 0;
  158. _clientMoving = false;
  159. super.clientNotifyDead();
  160. }
  161. private void thinkAttack()
  162. {
  163. L2Character target = getAttackTarget();
  164. if (target == null) return;
  165. if (checkTargetLostOrDead(target))
  166. {
  167. if (target != null)
  168. {
  169. // Notify the target
  170. setAttackTarget(null);
  171. }
  172. return;
  173. }
  174. if (maybeMoveToPawn(target, _actor.getPhysicalAttackRange())) return;
  175. _accessor.doAttack(target);
  176. return;
  177. }
  178. private void thinkCast()
  179. {
  180. L2Character target = getCastTarget();
  181. //if (Config.DEBUG) _log.warning("L2PlayerAI: thinkCast -> Start");
  182. if (checkTargetLost(target))
  183. {
  184. if (_skill.isOffensive() && getAttackTarget() != null)
  185. {
  186. //Notify the target
  187. setCastTarget(null);
  188. }
  189. return;
  190. }
  191. if (target != null)
  192. if (maybeMoveToPawn(target, _actor.getMagicalAttackRange(_skill))) return;
  193. if (_skill.getHitTime() > 50) clientStopMoving(null);
  194. L2Object oldTarget = _actor.getTarget();
  195. if (oldTarget != null)
  196. {
  197. // Replace the current target by the cast target
  198. if (target != null && oldTarget != target) _actor.setTarget(getCastTarget());
  199. // Launch the Cast of the skill
  200. _accessor.doCast(_skill);
  201. // Restore the initial target
  202. if (target != null && oldTarget != target) _actor.setTarget(oldTarget);
  203. }
  204. else _accessor.doCast(_skill);
  205. return;
  206. }
  207. private void thinkPickUp()
  208. {
  209. if (_actor.isAllSkillsDisabled()) return;
  210. L2Object target = getTarget();
  211. if (checkTargetLost(target)) return;
  212. if (maybeMoveToPawn(target, 36)) return;
  213. setIntention(AI_INTENTION_IDLE);
  214. ((L2PcInstance.AIAccessor) _accessor).doPickupItem(target);
  215. return;
  216. }
  217. private void thinkInteract()
  218. {
  219. if (_actor.isAllSkillsDisabled()) return;
  220. L2Object target = getTarget();
  221. if (checkTargetLost(target)) return;
  222. if (maybeMoveToPawn(target, 36)) return;
  223. if (!(target instanceof L2StaticObjectInstance)) ((L2PcInstance.AIAccessor) _accessor).doInteract((L2Character) target);
  224. setIntention(AI_INTENTION_IDLE);
  225. return;
  226. }
  227. @Override
  228. protected void onEvtThink()
  229. {
  230. if (_thinking || _actor.isAllSkillsDisabled()) return;
  231. /*
  232. if (Config.DEBUG)
  233. _log.warning("L2PlayerAI: onEvtThink -> Check intention");
  234. */
  235. _thinking = true;
  236. try
  237. {
  238. if (getIntention() == AI_INTENTION_ATTACK) thinkAttack();
  239. else if (getIntention() == AI_INTENTION_CAST) thinkCast();
  240. else if (getIntention() == AI_INTENTION_PICK_UP) thinkPickUp();
  241. else if (getIntention() == AI_INTENTION_INTERACT) thinkInteract();
  242. }
  243. finally
  244. {
  245. _thinking = false;
  246. }
  247. }
  248. @Override
  249. protected void onEvtArrivedRevalidate()
  250. {
  251. ThreadPoolManager.getInstance().executeTask(new KnownListAsynchronousUpdateTask(_actor));
  252. super.onEvtArrivedRevalidate();
  253. }
  254. }