L2PlayerAI.java 10 KB

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