L2PlayerAI.java 9.9 KB

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