L2ControllableMobAI.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 com.l2jserver.gameserver.model.L2Object;
  22. import com.l2jserver.gameserver.model.L2Skill;
  23. import com.l2jserver.gameserver.model.MobGroup;
  24. import com.l2jserver.gameserver.model.MobGroupTable;
  25. import com.l2jserver.gameserver.model.actor.L2Attackable;
  26. import com.l2jserver.gameserver.model.actor.L2Character;
  27. import com.l2jserver.gameserver.model.actor.L2Npc;
  28. import com.l2jserver.gameserver.model.actor.L2Playable;
  29. import com.l2jserver.gameserver.model.actor.L2Character.AIAccessor;
  30. import com.l2jserver.gameserver.model.actor.instance.L2ControllableMobInstance;
  31. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  32. import com.l2jserver.gameserver.model.actor.instance.L2NpcInstance;
  33. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  34. import com.l2jserver.gameserver.util.Util;
  35. import com.l2jserver.util.Rnd;
  36. import javolution.util.FastList;
  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().collisionRadius + target.getTemplate().collisionRadius;
  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().collisionRadius + getForcedTarget().getTemplate().collisionRadius;
  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. //synchronized (_actor.getKnownList().getKnownObjects())
  259. {
  260. for (L2Object obj : objs)
  261. {
  262. if (!(obj instanceof L2Npc))
  263. continue;
  264. L2Npc npc = (L2Npc) obj;
  265. if (!faction_id.equals(npc.getFactionId()))
  266. continue;
  267. if (_actor.isInsideRadius(npc, npc.getFactionRange(), false, true) && Math.abs(getAttackTarget().getZ() - npc.getZ()) < 200)
  268. {
  269. npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, getAttackTarget(), 1);
  270. }
  271. }
  272. }
  273. }
  274. L2Skill[] skills = null;
  275. double dist2 = 0;
  276. int range = 0;
  277. int max_range = 0;
  278. try
  279. {
  280. _actor.setTarget(getAttackTarget());
  281. skills = _actor.getAllSkills();
  282. dist2 = _actor.getPlanDistanceSq(getAttackTarget().getX(), getAttackTarget().getY());
  283. range = _actor.getPhysicalAttackRange() + _actor.getTemplate().collisionRadius + getAttackTarget().getTemplate().collisionRadius;
  284. max_range = range;
  285. }
  286. catch (NullPointerException e)
  287. {
  288. _log.log(Level.WARNING, "Encountered Null Value: " + e.getMessage(), e);
  289. }
  290. if (!_actor.isMuted() && dist2 > (range + 20) * (range + 20))
  291. {
  292. // check distant skills
  293. for (L2Skill sk : skills)
  294. {
  295. int castRange = sk.getCastRange();
  296. if (castRange * castRange >= dist2 && !_actor.isSkillDisabled(sk) && _actor.getCurrentMp() > _actor.getStat().getMpConsume(sk))
  297. {
  298. _accessor.doCast(sk);
  299. return;
  300. }
  301. max_range = Math.max(max_range, castRange);
  302. }
  303. moveToPawn(getAttackTarget(), range);
  304. return;
  305. }
  306. // Force mobs to attack anybody if confused.
  307. L2Character hated;
  308. if (_actor.isConfused())
  309. hated = findNextRndTarget();
  310. else
  311. hated = getAttackTarget();
  312. if (hated == null)
  313. {
  314. setIntention(AI_INTENTION_ACTIVE);
  315. return;
  316. }
  317. if (hated != getAttackTarget())
  318. setAttackTarget(hated);
  319. if (!_actor.isMuted() && skills.length > 0 && Rnd.nextInt(5) == 3)
  320. {
  321. for (L2Skill sk : skills)
  322. {
  323. int castRange = sk.getCastRange();
  324. if (castRange * castRange >= dist2 && !_actor.isSkillDisabled(sk) && _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk))
  325. {
  326. _accessor.doCast(sk);
  327. return;
  328. }
  329. }
  330. }
  331. _accessor.doAttack(getAttackTarget());
  332. }
  333. }
  334. private void thinkActive()
  335. {
  336. setAttackTarget(findNextRndTarget());
  337. L2Character hated;
  338. if (_actor.isConfused())
  339. hated = findNextRndTarget();
  340. else
  341. hated = getAttackTarget();
  342. if (hated != null)
  343. {
  344. _actor.setRunning();
  345. setIntention(CtrlIntention.AI_INTENTION_ATTACK, hated);
  346. }
  347. }
  348. private boolean autoAttackCondition(L2Character target)
  349. {
  350. if (target == null || !(_actor instanceof L2Attackable))
  351. return false;
  352. L2Attackable me = (L2Attackable) _actor;
  353. if (target instanceof L2NpcInstance || target instanceof L2DoorInstance)
  354. return false;
  355. if (target.isAlikeDead() || !me.isInsideRadius(target, me.getAggroRange(), false, false) || Math.abs(_actor.getZ() - target.getZ()) > 100)
  356. return false;
  357. // Check if the target isn't invulnerable
  358. if (target.isInvul())
  359. return false;
  360. // Spawn protection (only against mobs)
  361. if(target instanceof L2PcInstance && ((L2PcInstance)target).isSpawnProtected())
  362. return false;
  363. // Check if the target is a L2PlayableInstance
  364. if (target instanceof L2Playable)
  365. {
  366. // Check if the target isn't in silent move mode
  367. if (((L2Playable) target).isSilentMoving())
  368. return false;
  369. }
  370. if (target instanceof L2Npc)
  371. return false;
  372. return me.isAggressive();
  373. }
  374. private L2Character findNextRndTarget()
  375. {
  376. int aggroRange = ((L2Attackable) _actor).getAggroRange();
  377. L2Attackable npc = (L2Attackable) _actor;
  378. int npcX, npcY, targetX, targetY;
  379. double dy, dx;
  380. double dblAggroRange = aggroRange * aggroRange;
  381. List<L2Character> potentialTarget = new FastList<L2Character>();
  382. Collection<L2Object> objs = npc.getKnownList().getKnownObjects().values();
  383. //synchronized (npc.getKnownList().getKnownObjects())
  384. {
  385. for (L2Object obj : objs)
  386. {
  387. if (!(obj instanceof L2Character))
  388. continue;
  389. npcX = npc.getX();
  390. npcY = npc.getY();
  391. targetX = obj.getX();
  392. targetY = obj.getY();
  393. dx = npcX - targetX;
  394. dy = npcY - targetY;
  395. if (dx * dx + dy * dy > dblAggroRange)
  396. continue;
  397. L2Character target = (L2Character) obj;
  398. if (autoAttackCondition(target)) // check aggression
  399. potentialTarget.add(target);
  400. }
  401. }
  402. if (potentialTarget.isEmpty()) // nothing to do
  403. return null;
  404. // we choose a random target
  405. int choice = Rnd.nextInt(potentialTarget.size());
  406. L2Character target = potentialTarget.get(choice);
  407. return target;
  408. }
  409. private L2ControllableMobInstance findNextGroupTarget()
  410. {
  411. return getGroupTarget().getRandomMob();
  412. }
  413. public L2ControllableMobAI(AIAccessor accessor)
  414. {
  415. super(accessor);
  416. setAlternateAI(AI_IDLE);
  417. }
  418. public int getAlternateAI()
  419. {
  420. return _alternateAI;
  421. }
  422. public void setAlternateAI(int _alternateai)
  423. {
  424. _alternateAI = _alternateai;
  425. }
  426. public void forceAttack(L2Character target)
  427. {
  428. setAlternateAI(AI_FORCEATTACK);
  429. setForcedTarget(target);
  430. }
  431. public void forceAttackGroup(MobGroup group)
  432. {
  433. setForcedTarget(null);
  434. setGroupTarget(group);
  435. setAlternateAI(AI_ATTACK_GROUP);
  436. }
  437. public void stop()
  438. {
  439. setAlternateAI(AI_IDLE);
  440. clientStopMoving(null);
  441. }
  442. public void move(int x, int y, int z)
  443. {
  444. moveTo(x, y, z);
  445. }
  446. public void follow(L2Character target)
  447. {
  448. setAlternateAI(AI_FOLLOW);
  449. setForcedTarget(target);
  450. }
  451. public boolean isThinking()
  452. {
  453. return _isThinking;
  454. }
  455. public boolean isNotMoving()
  456. {
  457. return _isNotMoving;
  458. }
  459. public void setNotMoving(boolean isNotMoving)
  460. {
  461. _isNotMoving = isNotMoving;
  462. }
  463. public void setThinking(boolean isThinking)
  464. {
  465. _isThinking = isThinking;
  466. }
  467. private L2Character getForcedTarget()
  468. {
  469. return _forcedTarget;
  470. }
  471. private MobGroup getGroupTarget()
  472. {
  473. return _targetGroup;
  474. }
  475. private void setForcedTarget(L2Character forcedTarget)
  476. {
  477. _forcedTarget = forcedTarget;
  478. }
  479. private void setGroupTarget(MobGroup targetGroup)
  480. {
  481. _targetGroup = targetGroup;
  482. }
  483. }