L2PlayerAI.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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.L2Character;
  26. import net.sf.l2j.gameserver.model.L2Object;
  27. import net.sf.l2j.gameserver.model.L2Skill;
  28. import net.sf.l2j.gameserver.model.L2Character.AIAccessor;
  29. import net.sf.l2j.gameserver.model.L2Skill.SkillTargetType;
  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. if (_nextIntention != null)
  128. {
  129. if (_nextIntention._crtlIntention != AI_INTENTION_CAST) // previous state shouldn't be casting
  130. {
  131. setIntention(_nextIntention._crtlIntention, _nextIntention._arg0, _nextIntention._arg1);
  132. }
  133. else
  134. setIntention(AI_INTENTION_IDLE);
  135. }
  136. else
  137. {
  138. /*
  139. if (Config.DEBUG)
  140. _log.warning("L2PlayerAI: no previous intention set... Setting it to IDLE");
  141. */
  142. // set intention to idle if skill doesn't change intention.
  143. setIntention(AI_INTENTION_IDLE);
  144. }
  145. }
  146. }
  147. @Override
  148. protected void onIntentionRest()
  149. {
  150. if (getIntention() != AI_INTENTION_REST)
  151. {
  152. changeIntention(AI_INTENTION_REST, null, null);
  153. setTarget(null);
  154. if (getAttackTarget() != null)
  155. {
  156. setAttackTarget(null);
  157. }
  158. clientStopMoving(null);
  159. }
  160. }
  161. @Override
  162. protected void onIntentionActive()
  163. {
  164. setIntention(AI_INTENTION_IDLE);
  165. }
  166. /**
  167. * Manage the Move To Intention : Stop current Attack and Launch a Move to Location Task.<BR><BR>
  168. *
  169. * <B><U> Actions</U> : </B><BR><BR>
  170. * <li>Stop the actor auto-attack server side AND client side by sending Server->Client packet AutoAttackStop (broadcast) </li>
  171. * <li>Set the Intention of this AI to AI_INTENTION_MOVE_TO </li>
  172. * <li>Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast) </li><BR><BR>
  173. *
  174. */
  175. @Override
  176. protected void onIntentionMoveTo(L2CharPosition pos)
  177. {
  178. if (getIntention() == AI_INTENTION_REST)
  179. {
  180. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  181. clientActionFailed();
  182. return;
  183. }
  184. if (_actor.isAllSkillsDisabled() || _actor.isAttackingNow())
  185. {
  186. clientActionFailed();
  187. saveNextIntention(AI_INTENTION_MOVE_TO, pos, null);
  188. return;
  189. }
  190. // Set the Intention of this AbstractAI to AI_INTENTION_MOVE_TO
  191. changeIntention(AI_INTENTION_MOVE_TO, pos, null);
  192. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  193. clientStopAutoAttack();
  194. // Abort the attack of the L2Character and send Server->Client ActionFailed packet
  195. _actor.abortAttack();
  196. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  197. moveTo(pos.x, pos.y, pos.z);
  198. }
  199. @Override
  200. protected void clientNotifyDead()
  201. {
  202. _clientMovingToPawnOffset = 0;
  203. _clientMoving = false;
  204. super.clientNotifyDead();
  205. }
  206. private void thinkAttack()
  207. {
  208. L2Character target = getAttackTarget();
  209. if (target == null)
  210. return;
  211. if (checkTargetLostOrDead(target))
  212. {
  213. if (target != null)
  214. {
  215. // Notify the target
  216. setAttackTarget(null);
  217. }
  218. return;
  219. }
  220. if (maybeMoveToPawn(target, _actor.getPhysicalAttackRange()))
  221. return;
  222. _accessor.doAttack(target);
  223. return;
  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. return;
  234. }
  235. else
  236. {
  237. if (checkTargetLost(target))
  238. {
  239. if (_skill.isOffensive() && getAttackTarget() != null)
  240. {
  241. //Notify the target
  242. setCastTarget(null);
  243. }
  244. return;
  245. }
  246. if (target != null && maybeMoveToPawn(target, _actor.getMagicalAttackRange(_skill)))
  247. return;
  248. }
  249. if (_skill.getHitTime() > 50)
  250. clientStopMoving(null);
  251. L2Object oldTarget = _actor.getTarget();
  252. if (oldTarget != null && target != null && oldTarget != target)
  253. {
  254. // Replace the current target by the cast target
  255. _actor.setTarget(getCastTarget());
  256. // Launch the Cast of the skill
  257. _accessor.doCast(_skill);
  258. // Restore the initial target
  259. _actor.setTarget(oldTarget);
  260. }
  261. else
  262. _accessor.doCast(_skill);
  263. return;
  264. }
  265. private void thinkPickUp()
  266. {
  267. if (_actor.isAllSkillsDisabled())
  268. return;
  269. L2Object target = getTarget();
  270. if (checkTargetLost(target))
  271. return;
  272. if (maybeMoveToPawn(target, 36))
  273. return;
  274. setIntention(AI_INTENTION_IDLE);
  275. ((L2PcInstance.AIAccessor) _accessor).doPickupItem(target);
  276. return;
  277. }
  278. private void thinkInteract()
  279. {
  280. if (_actor.isAllSkillsDisabled())
  281. return;
  282. L2Object target = getTarget();
  283. if (checkTargetLost(target))
  284. return;
  285. if (maybeMoveToPawn(target, 36))
  286. return;
  287. if (!(target instanceof L2StaticObjectInstance))
  288. ((L2PcInstance.AIAccessor) _accessor).doInteract((L2Character) target);
  289. setIntention(AI_INTENTION_IDLE);
  290. return;
  291. }
  292. @Override
  293. protected void onEvtThink()
  294. {
  295. if (_thinking || _actor.isAllSkillsDisabled())
  296. return;
  297. /*
  298. if (Config.DEBUG)
  299. _log.warning("L2PlayerAI: onEvtThink -> Check intention");
  300. */
  301. _thinking = true;
  302. try
  303. {
  304. if (getIntention() == AI_INTENTION_ATTACK)
  305. thinkAttack();
  306. else if (getIntention() == AI_INTENTION_CAST)
  307. thinkCast();
  308. else if (getIntention() == AI_INTENTION_PICK_UP)
  309. thinkPickUp();
  310. else if (getIntention() == AI_INTENTION_INTERACT)
  311. thinkInteract();
  312. }
  313. finally
  314. {
  315. _thinking = false;
  316. }
  317. }
  318. }