L2SummonAI.java 7.3 KB

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