L2ControllableMobAI.java 15 KB

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