L2SummonAI.java 6.9 KB

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