L2SummonAI.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_FOLLOW;
  18. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  19. import java.util.concurrent.Future;
  20. import com.l2jserver.Config;
  21. import com.l2jserver.gameserver.GeoData;
  22. import com.l2jserver.gameserver.ThreadPoolManager;
  23. import com.l2jserver.gameserver.model.actor.L2Character;
  24. import com.l2jserver.gameserver.model.actor.L2Summon;
  25. import com.l2jserver.gameserver.model.actor.L2Character.AIAccessor;
  26. import com.l2jserver.util.Rnd;
  27. public class L2SummonAI extends L2PlayableAI implements Runnable
  28. {
  29. private static final int AVOID_RADIUS = 70;
  30. private volatile boolean _thinking; // to prevent recursive thinking
  31. private volatile boolean _startFollow = ((L2Summon) _actor).getFollowStatus();
  32. private volatile boolean _startAvoid = false;
  33. private Future<?> _avoidTask = null;
  34. public L2SummonAI(AIAccessor accessor)
  35. {
  36. super(accessor);
  37. }
  38. @Override
  39. protected void onIntentionIdle()
  40. {
  41. stopFollow();
  42. _startFollow = false;
  43. onIntentionActive();
  44. }
  45. @Override
  46. protected void onIntentionActive()
  47. {
  48. L2Summon summon = (L2Summon) _actor;
  49. if (_startFollow)
  50. setIntention(AI_INTENTION_FOLLOW, summon.getOwner());
  51. else
  52. super.onIntentionActive();
  53. }
  54. @Override
  55. synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
  56. {
  57. switch (intention)
  58. {
  59. case AI_INTENTION_ACTIVE:
  60. case AI_INTENTION_FOLLOW:
  61. startAvoidTask();
  62. break;
  63. default:
  64. stopAvoidTask();
  65. }
  66. super.changeIntention(intention, arg0, arg1);
  67. }
  68. private void thinkAttack()
  69. {
  70. if (checkTargetLostOrDead(getAttackTarget()))
  71. {
  72. setAttackTarget(null);
  73. return;
  74. }
  75. if (maybeMoveToPawn(getAttackTarget(), _actor.getPhysicalAttackRange()))
  76. return;
  77. clientStopMoving(null);
  78. _accessor.doAttack(getAttackTarget());
  79. }
  80. private void thinkCast()
  81. {
  82. L2Summon summon = (L2Summon) _actor;
  83. if (checkTargetLost(getCastTarget()))
  84. {
  85. setCastTarget(null);
  86. return;
  87. }
  88. boolean val = _startFollow;
  89. if (maybeMoveToPawn(getCastTarget(), _actor.getMagicalAttackRange(_skill)))
  90. return;
  91. clientStopMoving(null);
  92. summon.setFollowStatus(false);
  93. setIntention(AI_INTENTION_IDLE);
  94. _startFollow = val;
  95. _accessor.doCast(_skill);
  96. }
  97. private void thinkPickUp()
  98. {
  99. if (checkTargetLost(getTarget()))
  100. return;
  101. if (maybeMoveToPawn(getTarget(), 36))
  102. return;
  103. setIntention(AI_INTENTION_IDLE);
  104. ((L2Summon.AIAccessor) _accessor).doPickupItem(getTarget());
  105. }
  106. private void thinkInteract()
  107. {
  108. if (checkTargetLost(getTarget()))
  109. return;
  110. if (maybeMoveToPawn(getTarget(), 36))
  111. return;
  112. setIntention(AI_INTENTION_IDLE);
  113. }
  114. @Override
  115. protected void onEvtThink()
  116. {
  117. if (_thinking || _actor.isCastingNow() || _actor.isAllSkillsDisabled())
  118. return;
  119. _thinking = true;
  120. try
  121. {
  122. switch (getIntention())
  123. {
  124. case AI_INTENTION_ATTACK:
  125. thinkAttack();
  126. break;
  127. case AI_INTENTION_CAST:
  128. thinkCast();
  129. break;
  130. case AI_INTENTION_PICK_UP:
  131. thinkPickUp();
  132. break;
  133. case AI_INTENTION_INTERACT:
  134. thinkInteract();
  135. break;
  136. }
  137. }
  138. finally
  139. {
  140. _thinking = false;
  141. }
  142. }
  143. @Override
  144. protected void onEvtFinishCasting()
  145. {
  146. if (_actor.getAI().getIntention() != AI_INTENTION_ATTACK)
  147. ((L2Summon) _actor).setFollowStatus(_startFollow);
  148. }
  149. @Override
  150. protected void onEvtAttacked(L2Character attacker)
  151. {
  152. super.onEvtAttacked(attacker);
  153. avoidAttack(attacker);
  154. }
  155. @Override
  156. protected void onEvtEvaded(L2Character attacker)
  157. {
  158. super.onEvtEvaded(attacker);
  159. avoidAttack(attacker);
  160. }
  161. private void avoidAttack(L2Character attacker)
  162. {
  163. // trying to avoid if summon near owner
  164. if (((L2Summon) _actor).getOwner() != null
  165. && ((L2Summon) _actor).getOwner() != attacker
  166. && ((L2Summon) _actor).getOwner().isInsideRadius(_actor, 2 * AVOID_RADIUS, true, false))
  167. _startAvoid = true;
  168. }
  169. public void run()
  170. {
  171. if (_startAvoid)
  172. {
  173. _startAvoid = false;
  174. if (!_clientMoving
  175. && !_actor.isDead()
  176. && !_actor.isMovementDisabled())
  177. {
  178. final int ownerX = ((L2Summon) _actor).getOwner().getX();
  179. final int ownerY = ((L2Summon) _actor).getOwner().getY();
  180. final double angle = Math.toRadians(Rnd.get(-90, 90)) + Math.atan2(ownerY - _actor.getY(), ownerX - _actor.getX());
  181. final int targetX = ownerX + (int)(AVOID_RADIUS * Math.cos(angle));
  182. final int targetY = ownerY + (int)(AVOID_RADIUS * Math.sin(angle));
  183. if (Config.GEODATA == 0
  184. || GeoData.getInstance().canMoveFromToTarget(_actor.getX(), _actor.getY(), _actor.getZ(), targetX, targetY, _actor.getZ(), _actor.getInstanceId()))
  185. moveTo(targetX, targetY, _actor.getZ());
  186. }
  187. }
  188. }
  189. public void notifyFollowStatusChange()
  190. {
  191. _startFollow = !_startFollow;
  192. switch (getIntention())
  193. {
  194. case AI_INTENTION_ACTIVE:
  195. case AI_INTENTION_FOLLOW:
  196. case AI_INTENTION_IDLE:
  197. case AI_INTENTION_MOVE_TO:
  198. case AI_INTENTION_PICK_UP:
  199. ((L2Summon) _actor).setFollowStatus(_startFollow);
  200. }
  201. }
  202. public void setStartFollowController(boolean val)
  203. {
  204. _startFollow = val;
  205. }
  206. private void startAvoidTask()
  207. {
  208. if (_avoidTask == null)
  209. _avoidTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(this, 100, 100);
  210. }
  211. private void stopAvoidTask()
  212. {
  213. if (_avoidTask != null)
  214. {
  215. _avoidTask.cancel(false);
  216. _avoidTask = null;
  217. }
  218. }
  219. @Override
  220. public void stopAITask()
  221. {
  222. stopAvoidTask();
  223. super.stopAITask();
  224. }
  225. }