L2ControllableMobAI.java 15 KB

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