L2SummonAI.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 (checkTargetLost(getTarget()))
  79. return;
  80. if (maybeMoveToPawn(getTarget(), 36))
  81. return;
  82. setIntention(AI_INTENTION_IDLE);
  83. ((L2Summon.AIAccessor) _accessor).doPickupItem(getTarget());
  84. return;
  85. }
  86. private void thinkInteract()
  87. {
  88. if (checkTargetLost(getTarget()))
  89. return;
  90. if (maybeMoveToPawn(getTarget(), 36))
  91. return;
  92. setIntention(AI_INTENTION_IDLE);
  93. return;
  94. }
  95. @Override
  96. protected void onEvtThink()
  97. {
  98. if (_thinking || _actor.isCastingNow() || _actor.isAllSkillsDisabled())
  99. return;
  100. _thinking = true;
  101. try
  102. {
  103. switch (getIntention())
  104. {
  105. case AI_INTENTION_ATTACK:
  106. thinkAttack();
  107. break;
  108. case AI_INTENTION_CAST:
  109. thinkCast();
  110. break;
  111. case AI_INTENTION_PICK_UP:
  112. thinkPickUp();
  113. break;
  114. case AI_INTENTION_INTERACT:
  115. thinkInteract();
  116. break;
  117. }
  118. }
  119. finally
  120. {
  121. _thinking = false;
  122. }
  123. }
  124. @Override
  125. protected void onEvtFinishCasting()
  126. {
  127. if (_actor.getAI().getIntention() != AI_INTENTION_ATTACK)
  128. ((L2Summon) _actor).setFollowStatus(_startFollow);
  129. }
  130. public void notifyFollowStatusChange()
  131. {
  132. _startFollow = !_startFollow;
  133. switch (getIntention())
  134. {
  135. case AI_INTENTION_ACTIVE:
  136. case AI_INTENTION_FOLLOW:
  137. case AI_INTENTION_IDLE:
  138. ((L2Summon) _actor).setFollowStatus(_startFollow);
  139. }
  140. }
  141. public void setStartFollowController(boolean val)
  142. {
  143. _startFollow = val;
  144. }
  145. }