L2SummonAI.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_FOLLOW;
  18. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  19. import net.sf.l2j.gameserver.model.L2Summon;
  20. import net.sf.l2j.gameserver.model.L2Character.AIAccessor;
  21. public class L2SummonAI extends L2CharacterAI
  22. {
  23. private boolean _thinking; // to prevent recursive thinking
  24. private boolean _startFollow = ((L2Summon) _actor).getFollowStatus();
  25. public L2SummonAI(AIAccessor accessor)
  26. {
  27. super(accessor);
  28. }
  29. @Override
  30. protected void onIntentionIdle()
  31. {
  32. stopFollow();
  33. _startFollow = false;
  34. onIntentionActive();
  35. }
  36. @Override
  37. protected void onIntentionActive()
  38. {
  39. L2Summon summon = (L2Summon) _actor;
  40. if (_startFollow)
  41. setIntention(AI_INTENTION_FOLLOW, summon.getOwner());
  42. else
  43. super.onIntentionActive();
  44. }
  45. private void thinkAttack()
  46. {
  47. if (checkTargetLostOrDead(getAttackTarget()))
  48. {
  49. setAttackTarget(null);
  50. return;
  51. }
  52. if (maybeMoveToPawn(getAttackTarget(), _actor.getPhysicalAttackRange()))
  53. return;
  54. clientStopMoving(null);
  55. _accessor.doAttack(getAttackTarget());
  56. return;
  57. }
  58. private void thinkCast()
  59. {
  60. L2Summon summon = (L2Summon) _actor;
  61. if (checkTargetLost(getCastTarget()))
  62. {
  63. setCastTarget(null);
  64. return;
  65. }
  66. boolean val = _startFollow;
  67. if (maybeMoveToPawn(getCastTarget(), _actor.getMagicalAttackRange(_skill)))
  68. return;
  69. clientStopMoving(null);
  70. summon.setFollowStatus(false);
  71. setIntention(AI_INTENTION_IDLE);
  72. _startFollow = val;
  73. _accessor.doCast(_skill);
  74. return;
  75. }
  76. private void thinkPickUp()
  77. {
  78. if (_actor.isAllSkillsDisabled())
  79. return;
  80. if (checkTargetLost(getTarget()))
  81. return;
  82. if (maybeMoveToPawn(getTarget(), 36))
  83. return;
  84. setIntention(AI_INTENTION_IDLE);
  85. ((L2Summon.AIAccessor) _accessor).doPickupItem(getTarget());
  86. return;
  87. }
  88. private void thinkInteract()
  89. {
  90. if (_actor.isAllSkillsDisabled())
  91. return;
  92. if (checkTargetLost(getTarget()))
  93. return;
  94. if (maybeMoveToPawn(getTarget(), 36))
  95. return;
  96. setIntention(AI_INTENTION_IDLE);
  97. return;
  98. }
  99. @Override
  100. protected void onEvtThink()
  101. {
  102. if (_thinking || _actor.isAllSkillsDisabled())
  103. return;
  104. _thinking = true;
  105. try
  106. {
  107. switch (getIntention())
  108. {
  109. case AI_INTENTION_ATTACK:
  110. thinkAttack();
  111. break;
  112. case AI_INTENTION_CAST:
  113. thinkCast();
  114. break;
  115. case AI_INTENTION_PICK_UP:
  116. thinkPickUp();
  117. break;
  118. case AI_INTENTION_INTERACT:
  119. thinkInteract();
  120. break;
  121. }
  122. }
  123. finally
  124. {
  125. _thinking = false;
  126. }
  127. }
  128. @Override
  129. protected void onEvtFinishCasting()
  130. {
  131. if (_actor.getAI().getIntention() != AI_INTENTION_ATTACK)
  132. ((L2Summon) _actor).setFollowStatus(_startFollow);
  133. }
  134. public void notifyFollowStatusChange()
  135. {
  136. _startFollow = !_startFollow;
  137. switch (getIntention())
  138. {
  139. case AI_INTENTION_ACTIVE:
  140. case AI_INTENTION_FOLLOW:
  141. case AI_INTENTION_IDLE:
  142. ((L2Summon) _actor).setFollowStatus(_startFollow);
  143. }
  144. }
  145. public void setStartFollowController(boolean val)
  146. {
  147. _startFollow = val;
  148. }
  149. }