L2PlayerAI.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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.gameserver.model.L2CharPosition;
  24. import com.l2jserver.gameserver.model.L2Object;
  25. import com.l2jserver.gameserver.model.actor.L2Character;
  26. import com.l2jserver.gameserver.model.actor.L2Character.AIAccessor;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.actor.instance.L2StaticObjectInstance;
  29. import com.l2jserver.gameserver.model.skills.L2Skill;
  30. import com.l2jserver.gameserver.model.skills.targets.L2TargetType;
  31. public class L2PlayerAI extends L2PlayableAI
  32. {
  33. private boolean _thinking; // to prevent recursive thinking
  34. IntentionCommand _nextIntention = null;
  35. public L2PlayerAI(AIAccessor accessor)
  36. {
  37. super(accessor);
  38. }
  39. void saveNextIntention(CtrlIntention intention, Object arg0, Object arg1)
  40. {
  41. _nextIntention = new IntentionCommand(intention, arg0, arg1);
  42. }
  43. @Override
  44. public IntentionCommand getNextIntention()
  45. {
  46. return _nextIntention;
  47. }
  48. /**
  49. * Saves the current Intention for this L2PlayerAI if necessary and calls changeIntention in AbstractAI.<BR><BR>
  50. *
  51. * @param intention The new Intention to set to the AI
  52. * @param arg0 The first parameter of the Intention
  53. * @param arg1 The second parameter of the Intention
  54. *
  55. */
  56. @Override
  57. protected synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
  58. {
  59. // do nothing unless CAST intention
  60. // however, forget interrupted actions when starting to use an offensive skill
  61. if (intention != AI_INTENTION_CAST || (arg0 != null && ((L2Skill) arg0).isOffensive()))
  62. {
  63. _nextIntention = null;
  64. super.changeIntention(intention, arg0, arg1);
  65. return;
  66. }
  67. // do nothing if next intention is same as current one.
  68. if (intention == _intention && arg0 == _intentionArg0 && arg1 == _intentionArg1)
  69. {
  70. super.changeIntention(intention, arg0, arg1);
  71. return;
  72. }
  73. // save current intention so it can be used after cast
  74. saveNextIntention(_intention, _intentionArg0, _intentionArg1);
  75. super.changeIntention(intention, arg0, arg1);
  76. }
  77. /**
  78. * Launch actions corresponding to the Event ReadyToAct.<BR><BR>
  79. *
  80. * <B><U> Actions</U> :</B><BR><BR>
  81. * <li>Launch actions corresponding to the Event Think</li><BR><BR>
  82. *
  83. */
  84. @Override
  85. protected void onEvtReadyToAct()
  86. {
  87. // Launch actions corresponding to the Event Think
  88. if (_nextIntention != null)
  89. {
  90. setIntention(_nextIntention._crtlIntention, _nextIntention._arg0, _nextIntention._arg1);
  91. _nextIntention = null;
  92. }
  93. super.onEvtReadyToAct();
  94. }
  95. /**
  96. * Launch actions corresponding to the Event Cancel.<BR><BR>
  97. *
  98. * <B><U> Actions</U> :</B><BR><BR>
  99. * <li>Stop an AI Follow Task</li>
  100. * <li>Launch actions corresponding to the Event Think</li><BR><BR>
  101. *
  102. */
  103. @Override
  104. protected void onEvtCancel()
  105. {
  106. _nextIntention = null;
  107. super.onEvtCancel();
  108. }
  109. /**
  110. * Finalize the casting of a skill. This method overrides L2CharacterAI method.<BR><BR>
  111. *
  112. * <B>What it does:</B>
  113. * Check if actual intention is set to CAST and, if so, retrieves latest intention
  114. * before the actual CAST and set it as the current intention for the player
  115. */
  116. @Override
  117. protected void onEvtFinishCasting()
  118. {
  119. if (getIntention() == AI_INTENTION_CAST)
  120. {
  121. // run interrupted or next intention
  122. IntentionCommand nextIntention = _nextIntention;
  123. if (nextIntention != null)
  124. {
  125. if (nextIntention._crtlIntention != AI_INTENTION_CAST) // previous state shouldn't be casting
  126. {
  127. setIntention(nextIntention._crtlIntention, nextIntention._arg0, nextIntention._arg1);
  128. }
  129. else
  130. setIntention(AI_INTENTION_IDLE);
  131. }
  132. else
  133. {
  134. // set intention to idle if skill doesn't change intention.
  135. setIntention(AI_INTENTION_IDLE);
  136. }
  137. }
  138. }
  139. @Override
  140. protected void onIntentionRest()
  141. {
  142. if (getIntention() != AI_INTENTION_REST)
  143. {
  144. changeIntention(AI_INTENTION_REST, null, null);
  145. setTarget(null);
  146. if (getAttackTarget() != null)
  147. {
  148. setAttackTarget(null);
  149. }
  150. clientStopMoving(null);
  151. }
  152. }
  153. @Override
  154. protected void onIntentionActive()
  155. {
  156. setIntention(AI_INTENTION_IDLE);
  157. }
  158. /**
  159. * Manage the Move To Intention : Stop current Attack and Launch a Move to Location Task.<BR><BR>
  160. *
  161. * <B><U> Actions</U> : </B><BR><BR>
  162. * <li>Stop the actor auto-attack server side AND client side by sending Server->Client packet AutoAttackStop (broadcast) </li>
  163. * <li>Set the Intention of this AI to AI_INTENTION_MOVE_TO </li>
  164. * <li>Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast) </li><BR><BR>
  165. *
  166. */
  167. @Override
  168. protected void onIntentionMoveTo(L2CharPosition pos)
  169. {
  170. if (getIntention() == AI_INTENTION_REST)
  171. {
  172. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  173. clientActionFailed();
  174. return;
  175. }
  176. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow() || _actor.isAttackingNow())
  177. {
  178. clientActionFailed();
  179. saveNextIntention(AI_INTENTION_MOVE_TO, pos, null);
  180. return;
  181. }
  182. // Set the Intention of this AbstractAI to AI_INTENTION_MOVE_TO
  183. changeIntention(AI_INTENTION_MOVE_TO, pos, null);
  184. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  185. clientStopAutoAttack();
  186. // Abort the attack of the L2Character and send Server->Client ActionFailed packet
  187. _actor.abortAttack();
  188. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  189. moveTo(pos.x, pos.y, pos.z);
  190. }
  191. @Override
  192. protected void clientNotifyDead()
  193. {
  194. _clientMovingToPawnOffset = 0;
  195. _clientMoving = false;
  196. super.clientNotifyDead();
  197. }
  198. private void thinkAttack()
  199. {
  200. L2Character target = getAttackTarget();
  201. if (target == null)
  202. return;
  203. if (checkTargetLostOrDead(target))
  204. {
  205. // Notify the target
  206. setAttackTarget(null);
  207. return;
  208. }
  209. if (maybeMoveToPawn(target, _actor.getPhysicalAttackRange()))
  210. return;
  211. _accessor.doAttack(target);
  212. }
  213. private void thinkCast()
  214. {
  215. L2Character target = getCastTarget();
  216. if (_skill.getTargetType() == L2TargetType.TARGET_GROUND && _actor instanceof L2PcInstance)
  217. {
  218. if (maybeMoveToPosition(((L2PcInstance) _actor).getCurrentSkillWorldPosition(), _actor.getMagicalAttackRange(_skill)))
  219. {
  220. _actor.setIsCastingNow(false);
  221. return;
  222. }
  223. }
  224. else
  225. {
  226. if (checkTargetLost(target))
  227. {
  228. if (_skill.isOffensive() && getAttackTarget() != null)
  229. {
  230. //Notify the target
  231. setCastTarget(null);
  232. }
  233. _actor.setIsCastingNow(false);
  234. return;
  235. }
  236. if (target != null && maybeMoveToPawn(target, _actor.getMagicalAttackRange(_skill)))
  237. {
  238. _actor.setIsCastingNow(false);
  239. return;
  240. }
  241. }
  242. if (_skill.getHitTime() > 50 && !_skill.isSimultaneousCast())
  243. clientStopMoving(null);
  244. L2Object oldTarget = _actor.getTarget();
  245. if (oldTarget != null && target != null && oldTarget != target)
  246. {
  247. // Replace the current target by the cast target
  248. _actor.setTarget(getCastTarget());
  249. // Launch the Cast of the skill
  250. _accessor.doCast(_skill);
  251. // Restore the initial target
  252. _actor.setTarget(oldTarget);
  253. }
  254. else
  255. _accessor.doCast(_skill);
  256. }
  257. private void thinkPickUp()
  258. {
  259. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  260. return;
  261. L2Object target = getTarget();
  262. if (checkTargetLost(target))
  263. return;
  264. if (maybeMoveToPawn(target, 36))
  265. return;
  266. setIntention(AI_INTENTION_IDLE);
  267. ((L2PcInstance.AIAccessor) _accessor).doPickupItem(target);
  268. }
  269. private void thinkInteract()
  270. {
  271. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  272. return;
  273. L2Object target = getTarget();
  274. if (checkTargetLost(target))
  275. return;
  276. if (maybeMoveToPawn(target, 36))
  277. return;
  278. if (!(target instanceof L2StaticObjectInstance))
  279. ((L2PcInstance.AIAccessor) _accessor).doInteract((L2Character) target);
  280. setIntention(AI_INTENTION_IDLE);
  281. }
  282. @Override
  283. protected void onEvtThink()
  284. {
  285. if (_thinking && getIntention() != AI_INTENTION_CAST) // casting must always continue
  286. return;
  287. _thinking = true;
  288. try
  289. {
  290. if (getIntention() == AI_INTENTION_ATTACK)
  291. thinkAttack();
  292. else if (getIntention() == AI_INTENTION_CAST)
  293. thinkCast();
  294. else if (getIntention() == AI_INTENTION_PICK_UP)
  295. thinkPickUp();
  296. else if (getIntention() == AI_INTENTION_INTERACT)
  297. thinkInteract();
  298. }
  299. finally
  300. {
  301. _thinking = false;
  302. }
  303. }
  304. }