L2ControllableMobAI.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * Copyright (C) 2004-2014 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_ACTIVE;
  21. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import javolution.util.FastList;
  25. import com.l2jserver.gameserver.model.L2Object;
  26. import com.l2jserver.gameserver.model.MobGroup;
  27. import com.l2jserver.gameserver.model.MobGroupTable;
  28. import com.l2jserver.gameserver.model.actor.L2Attackable;
  29. import com.l2jserver.gameserver.model.actor.L2Character;
  30. import com.l2jserver.gameserver.model.actor.L2Character.AIAccessor;
  31. import com.l2jserver.gameserver.model.actor.L2Npc;
  32. import com.l2jserver.gameserver.model.actor.L2Playable;
  33. import com.l2jserver.gameserver.model.actor.instance.L2ControllableMobInstance;
  34. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  35. import com.l2jserver.gameserver.model.actor.instance.L2NpcInstance;
  36. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  37. import com.l2jserver.gameserver.model.skills.Skill;
  38. import com.l2jserver.gameserver.util.Util;
  39. import com.l2jserver.util.Rnd;
  40. /**
  41. * AI for controllable mobs
  42. * @author littlecrow
  43. */
  44. public class L2ControllableMobAI extends L2AttackableAI
  45. {
  46. public static final int AI_IDLE = 1;
  47. public static final int AI_NORMAL = 2;
  48. public static final int AI_FORCEATTACK = 3;
  49. public static final int AI_FOLLOW = 4;
  50. public static final int AI_CAST = 5;
  51. public static final int AI_ATTACK_GROUP = 6;
  52. private int _alternateAI;
  53. private boolean _isThinking; // to prevent thinking recursively
  54. private boolean _isNotMoving;
  55. private L2Character _forcedTarget;
  56. private MobGroup _targetGroup;
  57. protected void thinkFollow()
  58. {
  59. L2Attackable me = (L2Attackable) _actor;
  60. if (!Util.checkIfInRange(MobGroupTable.FOLLOW_RANGE, me, getForcedTarget(), true))
  61. {
  62. int signX = (Rnd.nextInt(2) == 0) ? -1 : 1;
  63. int signY = (Rnd.nextInt(2) == 0) ? -1 : 1;
  64. int randX = Rnd.nextInt(MobGroupTable.FOLLOW_RANGE);
  65. int randY = Rnd.nextInt(MobGroupTable.FOLLOW_RANGE);
  66. moveTo(getForcedTarget().getX() + (signX * randX), getForcedTarget().getY() + (signY * randY), getForcedTarget().getZ());
  67. }
  68. }
  69. @Override
  70. protected void onEvtThink()
  71. {
  72. if (isThinking())
  73. {
  74. return;
  75. }
  76. setThinking(true);
  77. try
  78. {
  79. switch (getAlternateAI())
  80. {
  81. case AI_IDLE:
  82. if (getIntention() != CtrlIntention.AI_INTENTION_ACTIVE)
  83. {
  84. setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  85. }
  86. break;
  87. case AI_FOLLOW:
  88. thinkFollow();
  89. break;
  90. case AI_CAST:
  91. thinkCast();
  92. break;
  93. case AI_FORCEATTACK:
  94. thinkForceAttack();
  95. break;
  96. case AI_ATTACK_GROUP:
  97. thinkAttackGroup();
  98. break;
  99. default:
  100. if (getIntention() == AI_INTENTION_ACTIVE)
  101. {
  102. thinkActive();
  103. }
  104. else if (getIntention() == AI_INTENTION_ATTACK)
  105. {
  106. thinkAttack();
  107. }
  108. break;
  109. }
  110. }
  111. finally
  112. {
  113. setThinking(false);
  114. }
  115. }
  116. protected void thinkCast()
  117. {
  118. L2Attackable npc = (L2Attackable) _actor;
  119. if ((getAttackTarget() == null) || getAttackTarget().isAlikeDead())
  120. {
  121. setAttackTarget(findNextRndTarget());
  122. clientStopMoving(null);
  123. }
  124. if (getAttackTarget() == null)
  125. {
  126. return;
  127. }
  128. npc.setTarget(getAttackTarget());
  129. if (!_actor.isMuted())
  130. {
  131. int max_range = 0;
  132. // check distant skills
  133. for (Skill sk : _actor.getAllSkills())
  134. {
  135. if (Util.checkIfInRange(sk.getCastRange(), _actor, getAttackTarget(), true) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() > _actor.getStat().getMpConsume(sk)))
  136. {
  137. _accessor.doCast(sk);
  138. return;
  139. }
  140. max_range = Math.max(max_range, sk.getCastRange());
  141. }
  142. if (!isNotMoving())
  143. {
  144. moveToPawn(getAttackTarget(), max_range);
  145. }
  146. return;
  147. }
  148. }
  149. protected void thinkAttackGroup()
  150. {
  151. L2Character target = getForcedTarget();
  152. if ((target == null) || target.isAlikeDead())
  153. {
  154. // try to get next group target
  155. setForcedTarget(findNextGroupTarget());
  156. clientStopMoving(null);
  157. }
  158. if (target == null)
  159. {
  160. return;
  161. }
  162. _actor.setTarget(target);
  163. // as a response, we put the target in a forcedattack mode
  164. L2ControllableMobInstance theTarget = (L2ControllableMobInstance) target;
  165. L2ControllableMobAI ctrlAi = (L2ControllableMobAI) theTarget.getAI();
  166. ctrlAi.forceAttack(_actor);
  167. double dist2 = _actor.calculateDistance(target, false, true);
  168. int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + target.getTemplate().getCollisionRadius();
  169. int max_range = range;
  170. if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20))))
  171. {
  172. // check distant skills
  173. for (Skill sk : _actor.getAllSkills())
  174. {
  175. int castRange = sk.getCastRange();
  176. if (((castRange * castRange) >= dist2) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() > _actor.getStat().getMpConsume(sk)))
  177. {
  178. _accessor.doCast(sk);
  179. return;
  180. }
  181. max_range = Math.max(max_range, castRange);
  182. }
  183. if (!isNotMoving())
  184. {
  185. moveToPawn(target, range);
  186. }
  187. return;
  188. }
  189. _accessor.doAttack(target);
  190. }
  191. protected void thinkForceAttack()
  192. {
  193. if ((getForcedTarget() == null) || getForcedTarget().isAlikeDead())
  194. {
  195. clientStopMoving(null);
  196. setIntention(AI_INTENTION_ACTIVE);
  197. setAlternateAI(AI_IDLE);
  198. }
  199. _actor.setTarget(getForcedTarget());
  200. double dist2 = _actor.calculateDistance(getForcedTarget(), false, true);
  201. int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + getForcedTarget().getTemplate().getCollisionRadius();
  202. int max_range = range;
  203. if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20))))
  204. {
  205. // check distant skills
  206. for (Skill sk : _actor.getAllSkills())
  207. {
  208. int castRange = sk.getCastRange();
  209. if (((castRange * castRange) >= dist2) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() > _actor.getStat().getMpConsume(sk)))
  210. {
  211. _accessor.doCast(sk);
  212. return;
  213. }
  214. max_range = Math.max(max_range, castRange);
  215. }
  216. if (!isNotMoving())
  217. {
  218. moveToPawn(getForcedTarget(), _actor.getPhysicalAttackRange()/* range */);
  219. }
  220. return;
  221. }
  222. _accessor.doAttack(getForcedTarget());
  223. }
  224. protected void thinkAttack()
  225. {
  226. if ((getAttackTarget() == null) || getAttackTarget().isAlikeDead())
  227. {
  228. if (getAttackTarget() != null)
  229. {
  230. // stop hating
  231. L2Attackable npc = (L2Attackable) _actor;
  232. npc.stopHating(getAttackTarget());
  233. }
  234. setIntention(AI_INTENTION_ACTIVE);
  235. }
  236. else
  237. {
  238. // notify aggression
  239. if (((L2Npc) _actor).getTemplate().getClans() != null)
  240. {
  241. Collection<L2Object> objs = _actor.getKnownList().getKnownObjects().values();
  242. for (L2Object obj : objs)
  243. {
  244. if (!(obj instanceof L2Npc))
  245. {
  246. continue;
  247. }
  248. L2Npc npc = (L2Npc) obj;
  249. if (!npc.isInMyClan((L2Npc) _actor))
  250. {
  251. continue;
  252. }
  253. if (_actor.isInsideRadius(npc, npc.getTemplate().getClanHelpRange(), false, true) && (Math.abs(getAttackTarget().getZ() - npc.getZ()) < 200))
  254. {
  255. npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, getAttackTarget(), 1);
  256. }
  257. }
  258. }
  259. _actor.setTarget(getAttackTarget());
  260. double dist2 = _actor.calculateDistance(getAttackTarget(), false, true);
  261. int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + getAttackTarget().getTemplate().getCollisionRadius();
  262. int max_range = range;
  263. if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20))))
  264. {
  265. // check distant skills
  266. for (Skill sk : _actor.getAllSkills())
  267. {
  268. int castRange = sk.getCastRange();
  269. if (((castRange * castRange) >= dist2) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() > _actor.getStat().getMpConsume(sk)))
  270. {
  271. _accessor.doCast(sk);
  272. return;
  273. }
  274. max_range = Math.max(max_range, castRange);
  275. }
  276. moveToPawn(getAttackTarget(), range);
  277. return;
  278. }
  279. // Force mobs to attack anybody if confused.
  280. L2Character hated;
  281. if (_actor.isConfused())
  282. {
  283. hated = findNextRndTarget();
  284. }
  285. else
  286. {
  287. hated = getAttackTarget();
  288. }
  289. if (hated == null)
  290. {
  291. setIntention(AI_INTENTION_ACTIVE);
  292. return;
  293. }
  294. if (hated != getAttackTarget())
  295. {
  296. setAttackTarget(hated);
  297. }
  298. if (!_actor.isMuted() && (Rnd.nextInt(5) == 3))
  299. {
  300. for (Skill sk : _actor.getAllSkills())
  301. {
  302. int castRange = sk.getCastRange();
  303. if (((castRange * castRange) >= dist2) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() < _actor.getStat().getMpConsume(sk)))
  304. {
  305. _accessor.doCast(sk);
  306. return;
  307. }
  308. }
  309. }
  310. _accessor.doAttack(getAttackTarget());
  311. }
  312. }
  313. private void thinkActive()
  314. {
  315. setAttackTarget(findNextRndTarget());
  316. L2Character hated;
  317. if (_actor.isConfused())
  318. {
  319. hated = findNextRndTarget();
  320. }
  321. else
  322. {
  323. hated = getAttackTarget();
  324. }
  325. if (hated != null)
  326. {
  327. _actor.setRunning();
  328. setIntention(CtrlIntention.AI_INTENTION_ATTACK, hated);
  329. }
  330. }
  331. private boolean autoAttackCondition(L2Character target)
  332. {
  333. if ((target == null) || !(_actor instanceof L2Attackable))
  334. {
  335. return false;
  336. }
  337. L2Attackable me = (L2Attackable) _actor;
  338. if ((target instanceof L2NpcInstance) || (target instanceof L2DoorInstance))
  339. {
  340. return false;
  341. }
  342. if (target.isAlikeDead() || !me.isInsideRadius(target, me.getAggroRange(), false, false) || (Math.abs(_actor.getZ() - target.getZ()) > 100))
  343. {
  344. return false;
  345. }
  346. // Check if the target isn't invulnerable
  347. if (target.isInvul())
  348. {
  349. return false;
  350. }
  351. // Spawn protection (only against mobs)
  352. if ((target instanceof L2PcInstance) && ((L2PcInstance) target).isSpawnProtected())
  353. {
  354. return false;
  355. }
  356. // Check if the target is a L2Playable
  357. if (target.isPlayable())
  358. {
  359. // Check if the target isn't in silent move mode
  360. if (((L2Playable) target).isSilentMoving())
  361. {
  362. return false;
  363. }
  364. }
  365. if (target instanceof L2Npc)
  366. {
  367. return false;
  368. }
  369. return me.isAggressive();
  370. }
  371. private L2Character findNextRndTarget()
  372. {
  373. int aggroRange = ((L2Attackable) _actor).getAggroRange();
  374. L2Attackable npc = (L2Attackable) _actor;
  375. int npcX, npcY, targetX, targetY;
  376. double dy, dx;
  377. double dblAggroRange = aggroRange * aggroRange;
  378. List<L2Character> potentialTarget = new FastList<>();
  379. Collection<L2Object> objs = npc.getKnownList().getKnownObjects().values();
  380. for (L2Object obj : objs)
  381. {
  382. if (!(obj instanceof L2Character))
  383. {
  384. continue;
  385. }
  386. npcX = npc.getX();
  387. npcY = npc.getY();
  388. targetX = obj.getX();
  389. targetY = obj.getY();
  390. dx = npcX - targetX;
  391. dy = npcY - targetY;
  392. if (((dx * dx) + (dy * dy)) > dblAggroRange)
  393. {
  394. continue;
  395. }
  396. L2Character target = (L2Character) obj;
  397. if (autoAttackCondition(target))
  398. {
  399. potentialTarget.add(target);
  400. }
  401. }
  402. if (potentialTarget.isEmpty())
  403. {
  404. return null;
  405. }
  406. // we choose a random target
  407. int choice = Rnd.nextInt(potentialTarget.size());
  408. L2Character target = potentialTarget.get(choice);
  409. return target;
  410. }
  411. private L2ControllableMobInstance findNextGroupTarget()
  412. {
  413. return getGroupTarget().getRandomMob();
  414. }
  415. public L2ControllableMobAI(AIAccessor accessor)
  416. {
  417. super(accessor);
  418. setAlternateAI(AI_IDLE);
  419. }
  420. public int getAlternateAI()
  421. {
  422. return _alternateAI;
  423. }
  424. public void setAlternateAI(int _alternateai)
  425. {
  426. _alternateAI = _alternateai;
  427. }
  428. public void forceAttack(L2Character target)
  429. {
  430. setAlternateAI(AI_FORCEATTACK);
  431. setForcedTarget(target);
  432. }
  433. public void forceAttackGroup(MobGroup group)
  434. {
  435. setForcedTarget(null);
  436. setGroupTarget(group);
  437. setAlternateAI(AI_ATTACK_GROUP);
  438. }
  439. public void stop()
  440. {
  441. setAlternateAI(AI_IDLE);
  442. clientStopMoving(null);
  443. }
  444. public void move(int x, int y, int z)
  445. {
  446. moveTo(x, y, z);
  447. }
  448. public void follow(L2Character target)
  449. {
  450. setAlternateAI(AI_FOLLOW);
  451. setForcedTarget(target);
  452. }
  453. public boolean isThinking()
  454. {
  455. return _isThinking;
  456. }
  457. public boolean isNotMoving()
  458. {
  459. return _isNotMoving;
  460. }
  461. public void setNotMoving(boolean isNotMoving)
  462. {
  463. _isNotMoving = isNotMoving;
  464. }
  465. public void setThinking(boolean isThinking)
  466. {
  467. _isThinking = isThinking;
  468. }
  469. private L2Character getForcedTarget()
  470. {
  471. return _forcedTarget;
  472. }
  473. private MobGroup getGroupTarget()
  474. {
  475. return _targetGroup;
  476. }
  477. private void setForcedTarget(L2Character forcedTarget)
  478. {
  479. _forcedTarget = forcedTarget;
  480. }
  481. private void setGroupTarget(MobGroup targetGroup)
  482. {
  483. _targetGroup = targetGroup;
  484. }
  485. }