L2ControllableMobAI.java 14 KB

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