L2ControllableMobAI.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. if (!_actor.isMuted())
  126. {
  127. int max_range = 0;
  128. // check distant skills
  129. for (L2Skill sk : _actor.getAllSkills())
  130. {
  131. if (Util.checkIfInRange(sk.getCastRange(), _actor, getAttackTarget(), true) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() > _actor.getStat().getMpConsume(sk)))
  132. {
  133. _accessor.doCast(sk);
  134. return;
  135. }
  136. max_range = Math.max(max_range, sk.getCastRange());
  137. }
  138. if (!isNotMoving())
  139. {
  140. moveToPawn(getAttackTarget(), max_range);
  141. }
  142. return;
  143. }
  144. }
  145. protected void thinkAttackGroup()
  146. {
  147. L2Character target = getForcedTarget();
  148. if ((target == null) || target.isAlikeDead())
  149. {
  150. // try to get next group target
  151. setForcedTarget(findNextGroupTarget());
  152. clientStopMoving(null);
  153. }
  154. if (target == null)
  155. {
  156. return;
  157. }
  158. _actor.setTarget(target);
  159. // as a response, we put the target in a forcedattack mode
  160. L2ControllableMobInstance theTarget = (L2ControllableMobInstance) target;
  161. L2ControllableMobAI ctrlAi = (L2ControllableMobAI) theTarget.getAI();
  162. ctrlAi.forceAttack(_actor);
  163. double dist2 = _actor.getPlanDistanceSq(target.getX(), target.getY());
  164. int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + target.getTemplate().getCollisionRadius();
  165. int max_range = range;
  166. if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20))))
  167. {
  168. // check distant skills
  169. for (L2Skill sk : _actor.getAllSkills())
  170. {
  171. int castRange = sk.getCastRange();
  172. if (((castRange * castRange) >= dist2) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() > _actor.getStat().getMpConsume(sk)))
  173. {
  174. _accessor.doCast(sk);
  175. return;
  176. }
  177. max_range = Math.max(max_range, castRange);
  178. }
  179. if (!isNotMoving())
  180. {
  181. moveToPawn(target, range);
  182. }
  183. return;
  184. }
  185. _accessor.doAttack(target);
  186. }
  187. protected void thinkForceAttack()
  188. {
  189. if ((getForcedTarget() == null) || getForcedTarget().isAlikeDead())
  190. {
  191. clientStopMoving(null);
  192. setIntention(AI_INTENTION_ACTIVE);
  193. setAlternateAI(AI_IDLE);
  194. }
  195. _actor.setTarget(getForcedTarget());
  196. double dist2 = _actor.getPlanDistanceSq(getForcedTarget().getX(), getForcedTarget().getY());
  197. int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + getForcedTarget().getTemplate().getCollisionRadius();
  198. int max_range = range;
  199. if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20))))
  200. {
  201. // check distant skills
  202. for (L2Skill sk : _actor.getAllSkills())
  203. {
  204. int castRange = sk.getCastRange();
  205. if (((castRange * castRange) >= dist2) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() > _actor.getStat().getMpConsume(sk)))
  206. {
  207. _accessor.doCast(sk);
  208. return;
  209. }
  210. max_range = Math.max(max_range, castRange);
  211. }
  212. if (!isNotMoving())
  213. {
  214. moveToPawn(getForcedTarget(), _actor.getPhysicalAttackRange()/* range */);
  215. }
  216. return;
  217. }
  218. _accessor.doAttack(getForcedTarget());
  219. }
  220. protected void thinkAttack()
  221. {
  222. if ((getAttackTarget() == null) || getAttackTarget().isAlikeDead())
  223. {
  224. if (getAttackTarget() != null)
  225. {
  226. // stop hating
  227. L2Attackable npc = (L2Attackable) _actor;
  228. npc.stopHating(getAttackTarget());
  229. }
  230. setIntention(AI_INTENTION_ACTIVE);
  231. }
  232. else
  233. {
  234. // notify aggression
  235. if (((L2Npc) _actor).getFactionId() != null)
  236. {
  237. String faction_id = ((L2Npc) _actor).getFactionId();
  238. Collection<L2Object> objs = _actor.getKnownList().getKnownObjects().values();
  239. for (L2Object obj : objs)
  240. {
  241. if (!(obj instanceof L2Npc))
  242. {
  243. continue;
  244. }
  245. L2Npc npc = (L2Npc) obj;
  246. if (!faction_id.equals(npc.getFactionId()))
  247. {
  248. continue;
  249. }
  250. if (_actor.isInsideRadius(npc, npc.getFactionRange(), false, true) && (Math.abs(getAttackTarget().getZ() - npc.getZ()) < 200))
  251. {
  252. npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, getAttackTarget(), 1);
  253. }
  254. }
  255. }
  256. _actor.setTarget(getAttackTarget());
  257. double dist2 = _actor.getPlanDistanceSq(getAttackTarget().getX(), getAttackTarget().getY());
  258. int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + getAttackTarget().getTemplate().getCollisionRadius();
  259. int max_range = range;
  260. if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20))))
  261. {
  262. // check distant skills
  263. for (L2Skill sk : _actor.getAllSkills())
  264. {
  265. int castRange = sk.getCastRange();
  266. if (((castRange * castRange) >= dist2) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() > _actor.getStat().getMpConsume(sk)))
  267. {
  268. _accessor.doCast(sk);
  269. return;
  270. }
  271. max_range = Math.max(max_range, castRange);
  272. }
  273. moveToPawn(getAttackTarget(), range);
  274. return;
  275. }
  276. // Force mobs to attack anybody if confused.
  277. L2Character hated;
  278. if (_actor.isConfused())
  279. {
  280. hated = findNextRndTarget();
  281. }
  282. else
  283. {
  284. hated = getAttackTarget();
  285. }
  286. if (hated == null)
  287. {
  288. setIntention(AI_INTENTION_ACTIVE);
  289. return;
  290. }
  291. if (hated != getAttackTarget())
  292. {
  293. setAttackTarget(hated);
  294. }
  295. if (!_actor.isMuted() && (Rnd.nextInt(5) == 3))
  296. {
  297. for (L2Skill sk : _actor.getAllSkills())
  298. {
  299. int castRange = sk.getCastRange();
  300. if (((castRange * castRange) >= dist2) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() < _actor.getStat().getMpConsume(sk)))
  301. {
  302. _accessor.doCast(sk);
  303. return;
  304. }
  305. }
  306. }
  307. _accessor.doAttack(getAttackTarget());
  308. }
  309. }
  310. private void thinkActive()
  311. {
  312. setAttackTarget(findNextRndTarget());
  313. L2Character hated;
  314. if (_actor.isConfused())
  315. {
  316. hated = findNextRndTarget();
  317. }
  318. else
  319. {
  320. hated = getAttackTarget();
  321. }
  322. if (hated != null)
  323. {
  324. _actor.setRunning();
  325. setIntention(CtrlIntention.AI_INTENTION_ATTACK, hated);
  326. }
  327. }
  328. private boolean autoAttackCondition(L2Character target)
  329. {
  330. if ((target == null) || !(_actor instanceof L2Attackable))
  331. {
  332. return false;
  333. }
  334. L2Attackable me = (L2Attackable) _actor;
  335. if ((target instanceof L2NpcInstance) || (target instanceof L2DoorInstance))
  336. {
  337. return false;
  338. }
  339. if (target.isAlikeDead() || !me.isInsideRadius(target, me.getAggroRange(), false, false) || (Math.abs(_actor.getZ() - target.getZ()) > 100))
  340. {
  341. return false;
  342. }
  343. // Check if the target isn't invulnerable
  344. if (target.isInvul())
  345. {
  346. return false;
  347. }
  348. // Spawn protection (only against mobs)
  349. if ((target instanceof L2PcInstance) && ((L2PcInstance) target).isSpawnProtected())
  350. {
  351. return false;
  352. }
  353. // Check if the target is a L2Playable
  354. if (target.isPlayable())
  355. {
  356. // Check if the target isn't in silent move mode
  357. if (((L2Playable) target).isSilentMoving())
  358. {
  359. return false;
  360. }
  361. }
  362. if (target instanceof L2Npc)
  363. {
  364. return false;
  365. }
  366. return me.isAggressive();
  367. }
  368. private L2Character findNextRndTarget()
  369. {
  370. int aggroRange = ((L2Attackable) _actor).getAggroRange();
  371. L2Attackable npc = (L2Attackable) _actor;
  372. int npcX, npcY, targetX, targetY;
  373. double dy, dx;
  374. double dblAggroRange = aggroRange * aggroRange;
  375. List<L2Character> potentialTarget = new FastList<>();
  376. Collection<L2Object> objs = npc.getKnownList().getKnownObjects().values();
  377. for (L2Object obj : objs)
  378. {
  379. if (!(obj instanceof L2Character))
  380. {
  381. continue;
  382. }
  383. npcX = npc.getX();
  384. npcY = npc.getY();
  385. targetX = obj.getX();
  386. targetY = obj.getY();
  387. dx = npcX - targetX;
  388. dy = npcY - targetY;
  389. if (((dx * dx) + (dy * dy)) > dblAggroRange)
  390. {
  391. continue;
  392. }
  393. L2Character target = (L2Character) obj;
  394. if (autoAttackCondition(target))
  395. {
  396. potentialTarget.add(target);
  397. }
  398. }
  399. if (potentialTarget.isEmpty())
  400. {
  401. return null;
  402. }
  403. // we choose a random target
  404. int choice = Rnd.nextInt(potentialTarget.size());
  405. L2Character target = potentialTarget.get(choice);
  406. return target;
  407. }
  408. private L2ControllableMobInstance findNextGroupTarget()
  409. {
  410. return getGroupTarget().getRandomMob();
  411. }
  412. public L2ControllableMobAI(AIAccessor accessor)
  413. {
  414. super(accessor);
  415. setAlternateAI(AI_IDLE);
  416. }
  417. public int getAlternateAI()
  418. {
  419. return _alternateAI;
  420. }
  421. public void setAlternateAI(int _alternateai)
  422. {
  423. _alternateAI = _alternateai;
  424. }
  425. public void forceAttack(L2Character target)
  426. {
  427. setAlternateAI(AI_FORCEATTACK);
  428. setForcedTarget(target);
  429. }
  430. public void forceAttackGroup(MobGroup group)
  431. {
  432. setForcedTarget(null);
  433. setGroupTarget(group);
  434. setAlternateAI(AI_ATTACK_GROUP);
  435. }
  436. public void stop()
  437. {
  438. setAlternateAI(AI_IDLE);
  439. clientStopMoving(null);
  440. }
  441. public void move(int x, int y, int z)
  442. {
  443. moveTo(x, y, z);
  444. }
  445. public void follow(L2Character target)
  446. {
  447. setAlternateAI(AI_FOLLOW);
  448. setForcedTarget(target);
  449. }
  450. public boolean isThinking()
  451. {
  452. return _isThinking;
  453. }
  454. public boolean isNotMoving()
  455. {
  456. return _isNotMoving;
  457. }
  458. public void setNotMoving(boolean isNotMoving)
  459. {
  460. _isNotMoving = isNotMoving;
  461. }
  462. public void setThinking(boolean isThinking)
  463. {
  464. _isThinking = isThinking;
  465. }
  466. private L2Character getForcedTarget()
  467. {
  468. return _forcedTarget;
  469. }
  470. private MobGroup getGroupTarget()
  471. {
  472. return _targetGroup;
  473. }
  474. private void setForcedTarget(L2Character forcedTarget)
  475. {
  476. _forcedTarget = forcedTarget;
  477. }
  478. private void setGroupTarget(MobGroup targetGroup)
  479. {
  480. _targetGroup = targetGroup;
  481. }
  482. }