L2SummonAI.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.ai;
  20. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  21. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_FOLLOW;
  22. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  23. import java.util.concurrent.Future;
  24. import com.l2jserver.Config;
  25. import com.l2jserver.gameserver.GeoData;
  26. import com.l2jserver.gameserver.ThreadPoolManager;
  27. import com.l2jserver.gameserver.model.L2Object;
  28. import com.l2jserver.gameserver.model.actor.L2Character;
  29. import com.l2jserver.gameserver.model.actor.L2Character.AIAccessor;
  30. import com.l2jserver.gameserver.model.actor.L2Summon;
  31. import com.l2jserver.gameserver.model.skills.L2Skill;
  32. import com.l2jserver.util.Rnd;
  33. public class L2SummonAI extends L2PlayableAI implements Runnable
  34. {
  35. private static final int AVOID_RADIUS = 70;
  36. private volatile boolean _thinking; // to prevent recursive thinking
  37. private volatile boolean _startFollow = ((L2Summon) _actor).getFollowStatus();
  38. private L2Character _lastAttack = null;
  39. private volatile boolean _startAvoid = false;
  40. private Future<?> _avoidTask = null;
  41. public L2SummonAI(AIAccessor accessor)
  42. {
  43. super(accessor);
  44. }
  45. @Override
  46. protected void onIntentionIdle()
  47. {
  48. stopFollow();
  49. _startFollow = false;
  50. onIntentionActive();
  51. }
  52. @Override
  53. protected void onIntentionActive()
  54. {
  55. L2Summon summon = (L2Summon) _actor;
  56. if (_startFollow)
  57. {
  58. setIntention(AI_INTENTION_FOLLOW, summon.getOwner());
  59. }
  60. else
  61. {
  62. super.onIntentionActive();
  63. }
  64. }
  65. @Override
  66. synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
  67. {
  68. switch (intention)
  69. {
  70. case AI_INTENTION_ACTIVE:
  71. case AI_INTENTION_FOLLOW:
  72. startAvoidTask();
  73. break;
  74. default:
  75. stopAvoidTask();
  76. }
  77. super.changeIntention(intention, arg0, arg1);
  78. }
  79. private void thinkAttack()
  80. {
  81. if (checkTargetLostOrDead(getAttackTarget()))
  82. {
  83. setAttackTarget(null);
  84. return;
  85. }
  86. if (maybeMoveToPawn(getAttackTarget(), _actor.getPhysicalAttackRange()))
  87. {
  88. return;
  89. }
  90. clientStopMoving(null);
  91. _accessor.doAttack(getAttackTarget());
  92. }
  93. private void thinkCast()
  94. {
  95. L2Summon summon = (L2Summon) _actor;
  96. if (checkTargetLost(getCastTarget()))
  97. {
  98. setCastTarget(null);
  99. return;
  100. }
  101. boolean val = _startFollow;
  102. if (maybeMoveToPawn(getCastTarget(), _actor.getMagicalAttackRange(_skill)))
  103. {
  104. return;
  105. }
  106. clientStopMoving(null);
  107. summon.setFollowStatus(false);
  108. setIntention(AI_INTENTION_IDLE);
  109. _startFollow = val;
  110. _accessor.doCast(_skill);
  111. }
  112. private void thinkPickUp()
  113. {
  114. if (checkTargetLost(getTarget()))
  115. {
  116. return;
  117. }
  118. if (maybeMoveToPawn(getTarget(), 36))
  119. {
  120. return;
  121. }
  122. setIntention(AI_INTENTION_IDLE);
  123. ((L2Summon.AIAccessor) _accessor).doPickupItem(getTarget());
  124. }
  125. private void thinkInteract()
  126. {
  127. if (checkTargetLost(getTarget()))
  128. {
  129. return;
  130. }
  131. if (maybeMoveToPawn(getTarget(), 36))
  132. {
  133. return;
  134. }
  135. setIntention(AI_INTENTION_IDLE);
  136. }
  137. @Override
  138. protected void onEvtThink()
  139. {
  140. if (_thinking || _actor.isCastingNow() || _actor.isAllSkillsDisabled())
  141. {
  142. return;
  143. }
  144. _thinking = true;
  145. try
  146. {
  147. switch (getIntention())
  148. {
  149. case AI_INTENTION_ATTACK:
  150. thinkAttack();
  151. break;
  152. case AI_INTENTION_CAST:
  153. thinkCast();
  154. break;
  155. case AI_INTENTION_PICK_UP:
  156. thinkPickUp();
  157. break;
  158. case AI_INTENTION_INTERACT:
  159. thinkInteract();
  160. break;
  161. }
  162. }
  163. finally
  164. {
  165. _thinking = false;
  166. }
  167. }
  168. @Override
  169. protected void onEvtFinishCasting()
  170. {
  171. if (_lastAttack == null)
  172. {
  173. ((L2Summon) _actor).setFollowStatus(_startFollow);
  174. }
  175. else
  176. {
  177. setIntention(CtrlIntention.AI_INTENTION_ATTACK, _lastAttack);
  178. _lastAttack = null;
  179. }
  180. }
  181. @Override
  182. protected void onEvtAttacked(L2Character attacker)
  183. {
  184. super.onEvtAttacked(attacker);
  185. avoidAttack(attacker);
  186. }
  187. @Override
  188. protected void onEvtEvaded(L2Character attacker)
  189. {
  190. super.onEvtEvaded(attacker);
  191. avoidAttack(attacker);
  192. }
  193. private void avoidAttack(L2Character attacker)
  194. {
  195. // trying to avoid if summon near owner
  196. if ((((L2Summon) _actor).getOwner() != null) && (((L2Summon) _actor).getOwner() != attacker) && ((L2Summon) _actor).getOwner().isInsideRadius(_actor, 2 * AVOID_RADIUS, true, false))
  197. {
  198. _startAvoid = true;
  199. }
  200. }
  201. @Override
  202. public void run()
  203. {
  204. if (_startAvoid)
  205. {
  206. _startAvoid = false;
  207. if (!_clientMoving && !_actor.isDead() && !_actor.isMovementDisabled())
  208. {
  209. final int ownerX = ((L2Summon) _actor).getOwner().getX();
  210. final int ownerY = ((L2Summon) _actor).getOwner().getY();
  211. final double angle = Math.toRadians(Rnd.get(-90, 90)) + Math.atan2(ownerY - _actor.getY(), ownerX - _actor.getX());
  212. final int targetX = ownerX + (int) (AVOID_RADIUS * Math.cos(angle));
  213. final int targetY = ownerY + (int) (AVOID_RADIUS * Math.sin(angle));
  214. if ((Config.GEODATA == 0) || GeoData.getInstance().canMoveFromToTarget(_actor.getX(), _actor.getY(), _actor.getZ(), targetX, targetY, _actor.getZ(), _actor.getInstanceId()))
  215. {
  216. moveTo(targetX, targetY, _actor.getZ());
  217. }
  218. }
  219. }
  220. }
  221. public void notifyFollowStatusChange()
  222. {
  223. _startFollow = !_startFollow;
  224. switch (getIntention())
  225. {
  226. case AI_INTENTION_ACTIVE:
  227. case AI_INTENTION_FOLLOW:
  228. case AI_INTENTION_IDLE:
  229. case AI_INTENTION_MOVE_TO:
  230. case AI_INTENTION_PICK_UP:
  231. ((L2Summon) _actor).setFollowStatus(_startFollow);
  232. }
  233. }
  234. public void setStartFollowController(boolean val)
  235. {
  236. _startFollow = val;
  237. }
  238. @Override
  239. protected void onIntentionCast(L2Skill skill, L2Object target)
  240. {
  241. if (getIntention() == AI_INTENTION_ATTACK)
  242. {
  243. _lastAttack = getAttackTarget();
  244. }
  245. else
  246. {
  247. _lastAttack = null;
  248. }
  249. super.onIntentionCast(skill, target);
  250. }
  251. private void startAvoidTask()
  252. {
  253. if (_avoidTask == null)
  254. {
  255. _avoidTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(this, 100, 100);
  256. }
  257. }
  258. private void stopAvoidTask()
  259. {
  260. if (_avoidTask != null)
  261. {
  262. _avoidTask.cancel(false);
  263. _avoidTask = null;
  264. }
  265. }
  266. @Override
  267. public void stopAITask()
  268. {
  269. stopAvoidTask();
  270. super.stopAITask();
  271. }
  272. }