L2PlayerAI.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.ai;
  20. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  21. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_CAST;
  22. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  23. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_INTERACT;
  24. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_MOVE_TO;
  25. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_PICK_UP;
  26. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_REST;
  27. import com.l2jserver.gameserver.model.L2Object;
  28. import com.l2jserver.gameserver.model.Location;
  29. import com.l2jserver.gameserver.model.actor.L2Character;
  30. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  31. import com.l2jserver.gameserver.model.actor.instance.L2StaticObjectInstance;
  32. import com.l2jserver.gameserver.model.skills.Skill;
  33. import com.l2jserver.gameserver.model.skills.targets.L2TargetType;
  34. public class L2PlayerAI extends L2PlayableAI
  35. {
  36. private boolean _thinking; // to prevent recursive thinking
  37. private IntentionCommand _nextIntention = null;
  38. public L2PlayerAI(L2PcInstance creature)
  39. {
  40. super(creature);
  41. }
  42. void saveNextIntention(CtrlIntention intention, Object arg0, Object arg1)
  43. {
  44. _nextIntention = new IntentionCommand(intention, arg0, arg1);
  45. }
  46. @Override
  47. public IntentionCommand getNextIntention()
  48. {
  49. return _nextIntention;
  50. }
  51. /**
  52. * Saves the current Intention for this L2PlayerAI if necessary and calls changeIntention in AbstractAI.
  53. * @param intention The new Intention to set to the AI
  54. * @param arg0 The first parameter of the Intention
  55. * @param arg1 The second parameter of the Intention
  56. */
  57. @Override
  58. protected synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
  59. {
  60. // Forget next if it's not cast or it's cast and skill is toggle.
  61. if ((intention != AI_INTENTION_CAST) || ((arg0 != null) && !((Skill) arg0).isToggle()))
  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>
  79. * <B><U> Actions</U> :</B>
  80. * <ul>
  81. * <li>Launch actions corresponding to the Event Think</li>
  82. * </ul>
  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>
  97. * <B><U> Actions</U> :</B>
  98. * <ul>
  99. * <li>Stop an AI Follow Task</li>
  100. * <li>Launch actions corresponding to the Event Think</li>
  101. * </ul>
  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>
  111. * <B>What it does:</B><br>
  112. * 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.
  113. */
  114. @Override
  115. protected void onEvtFinishCasting()
  116. {
  117. if (getIntention() == AI_INTENTION_CAST)
  118. {
  119. // run interrupted or next intention
  120. IntentionCommand nextIntention = _nextIntention;
  121. if (nextIntention != null)
  122. {
  123. if (nextIntention._crtlIntention != AI_INTENTION_CAST) // previous state shouldn't be casting
  124. {
  125. setIntention(nextIntention._crtlIntention, nextIntention._arg0, nextIntention._arg1);
  126. }
  127. else
  128. {
  129. setIntention(AI_INTENTION_IDLE);
  130. }
  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>
  160. * <B><U> Actions</U> : </B>
  161. * <ul>
  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>
  165. * </ul>
  166. */
  167. @Override
  168. protected void onIntentionMoveTo(Location loc)
  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, loc, null);
  180. return;
  181. }
  182. // Set the Intention of this AbstractAI to AI_INTENTION_MOVE_TO
  183. changeIntention(AI_INTENTION_MOVE_TO, loc, 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(loc.getX(), loc.getY(), loc.getZ());
  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. {
  203. return;
  204. }
  205. if (checkTargetLostOrDead(target))
  206. {
  207. // Notify the target
  208. setAttackTarget(null);
  209. return;
  210. }
  211. if (maybeMoveToPawn(target, _actor.getPhysicalAttackRange()))
  212. {
  213. return;
  214. }
  215. _actor.doAttack(target);
  216. }
  217. private void thinkCast()
  218. {
  219. L2Character target = getCastTarget();
  220. if ((_skill.getTargetType() == L2TargetType.GROUND) && (_actor instanceof L2PcInstance))
  221. {
  222. if (maybeMoveToPosition(((L2PcInstance) _actor).getCurrentSkillWorldPosition(), _actor.getMagicalAttackRange(_skill)))
  223. {
  224. _actor.setIsCastingNow(false);
  225. return;
  226. }
  227. }
  228. else
  229. {
  230. if (checkTargetLost(target))
  231. {
  232. if (_skill.isBad() && (getAttackTarget() != null))
  233. {
  234. // Notify the target
  235. setCastTarget(null);
  236. }
  237. _actor.setIsCastingNow(false);
  238. return;
  239. }
  240. if ((target != null) && maybeMoveToPawn(target, _actor.getMagicalAttackRange(_skill)))
  241. {
  242. _actor.setIsCastingNow(false);
  243. return;
  244. }
  245. }
  246. if ((_skill.getHitTime() > 50) && !_skill.isSimultaneousCast())
  247. {
  248. clientStopMoving(null);
  249. }
  250. _actor.doCast(_skill);
  251. }
  252. private void thinkPickUp()
  253. {
  254. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  255. {
  256. return;
  257. }
  258. L2Object target = getTarget();
  259. if (checkTargetLost(target))
  260. {
  261. return;
  262. }
  263. if (maybeMoveToPawn(target, 36))
  264. {
  265. return;
  266. }
  267. setIntention(AI_INTENTION_IDLE);
  268. _actor.getActingPlayer().doPickupItem(target);
  269. }
  270. private void thinkInteract()
  271. {
  272. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  273. {
  274. return;
  275. }
  276. L2Object target = getTarget();
  277. if (checkTargetLost(target))
  278. {
  279. return;
  280. }
  281. if (maybeMoveToPawn(target, 36))
  282. {
  283. return;
  284. }
  285. if (!(target instanceof L2StaticObjectInstance))
  286. {
  287. _actor.getActingPlayer().doInteract((L2Character) target);
  288. }
  289. setIntention(AI_INTENTION_IDLE);
  290. }
  291. @Override
  292. protected void onEvtThink()
  293. {
  294. if (_thinking && (getIntention() != AI_INTENTION_CAST))
  295. {
  296. return;
  297. }
  298. _thinking = true;
  299. try
  300. {
  301. if (getIntention() == AI_INTENTION_ATTACK)
  302. {
  303. thinkAttack();
  304. }
  305. else if (getIntention() == AI_INTENTION_CAST)
  306. {
  307. thinkCast();
  308. }
  309. else if (getIntention() == AI_INTENTION_PICK_UP)
  310. {
  311. thinkPickUp();
  312. }
  313. else if (getIntention() == AI_INTENTION_INTERACT)
  314. {
  315. thinkInteract();
  316. }
  317. }
  318. finally
  319. {
  320. _thinking = false;
  321. }
  322. }
  323. }