L2SummonAI.java 6.8 KB

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