L2ControllableMobAI.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 net.sf.l2j.gameserver.ai;
  16. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
  17. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  18. import java.util.Collection;
  19. import java.util.List;
  20. import javolution.util.FastList;
  21. import net.sf.l2j.gameserver.model.L2Attackable;
  22. import net.sf.l2j.gameserver.model.L2Character;
  23. import net.sf.l2j.gameserver.model.L2Object;
  24. import net.sf.l2j.gameserver.model.L2Skill;
  25. import net.sf.l2j.gameserver.model.MobGroup;
  26. import net.sf.l2j.gameserver.model.MobGroupTable;
  27. import net.sf.l2j.gameserver.model.L2Character.AIAccessor;
  28. import net.sf.l2j.gameserver.model.actor.instance.L2ControllableMobInstance;
  29. import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
  30. import net.sf.l2j.gameserver.model.actor.instance.L2FolkInstance;
  31. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  32. import net.sf.l2j.gameserver.model.actor.instance.L2PlayableInstance;
  33. import net.sf.l2j.gameserver.util.Util;
  34. import net.sf.l2j.util.Rnd;
  35. /**
  36. * @author littlecrow
  37. * AI for controllable mobs
  38. *
  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. return;
  70. setThinking(true);
  71. try
  72. {
  73. switch (getAlternateAI())
  74. {
  75. case AI_IDLE:
  76. if (getIntention() != CtrlIntention.AI_INTENTION_ACTIVE)
  77. setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  78. break;
  79. case AI_FOLLOW:
  80. thinkFollow();
  81. break;
  82. case AI_CAST:
  83. thinkCast();
  84. break;
  85. case AI_FORCEATTACK:
  86. thinkForceAttack();
  87. break;
  88. case AI_ATTACK_GROUP:
  89. thinkAttackGroup();
  90. break;
  91. default:
  92. if (getIntention() == AI_INTENTION_ACTIVE)
  93. thinkActive();
  94. else if (getIntention() == AI_INTENTION_ATTACK)
  95. thinkAttack();
  96. break;
  97. }
  98. }
  99. finally
  100. {
  101. setThinking(false);
  102. }
  103. }
  104. protected void thinkCast()
  105. {
  106. L2Attackable npc = (L2Attackable) _actor;
  107. if (getAttackTarget() == null || getAttackTarget().isAlikeDead())
  108. {
  109. setAttackTarget(findNextRndTarget());
  110. clientStopMoving(null);
  111. }
  112. if (getAttackTarget() == null)
  113. return;
  114. npc.setTarget(getAttackTarget());
  115. L2Skill[] skills = null;
  116. //double dist2 = 0;
  117. try
  118. {
  119. skills = _actor.getAllSkills();
  120. // dist2 = _actor.getPlanDistanceSq(getAttackTarget().getX(), getAttackTarget().getY());
  121. }
  122. catch (NullPointerException e)
  123. {
  124. _log.warning("Encountered Null Value.");
  125. e.printStackTrace();
  126. }
  127. if (!_actor.isMuted())
  128. {
  129. int max_range = 0;
  130. // check distant skills
  131. for (L2Skill sk : skills)
  132. {
  133. if (Util.checkIfInRange(sk.getCastRange(), _actor, getAttackTarget(), true) && !_actor.isSkillDisabled(sk.getId()) && _actor.getCurrentMp() > _actor.getStat().getMpConsume(sk))
  134. {
  135. _accessor.doCast(sk);
  136. return;
  137. }
  138. max_range = Math.max(max_range, sk.getCastRange());
  139. }
  140. if (!isNotMoving())
  141. moveToPawn(getAttackTarget(), max_range);
  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. return;
  156. L2Skill[] skills = null;
  157. double dist2 = 0;
  158. int range = 0;
  159. int max_range = 0;
  160. _actor.setTarget(target);
  161. // as a response, we put the target in a forcedattack mode
  162. L2ControllableMobInstance theTarget = (L2ControllableMobInstance) target;
  163. L2ControllableMobAI ctrlAi = (L2ControllableMobAI) theTarget.getAI();
  164. ctrlAi.forceAttack(_actor);
  165. try
  166. {
  167. skills = _actor.getAllSkills();
  168. dist2 = _actor.getPlanDistanceSq(target.getX(), target.getY());
  169. range = _actor.getPhysicalAttackRange() + _actor.getTemplate().collisionRadius + target.getTemplate().collisionRadius;
  170. max_range = range;
  171. }
  172. catch (NullPointerException e)
  173. {
  174. _log.warning("Encountered Null Value.");
  175. e.printStackTrace();
  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.getId()) && _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.warning("Encountered Null Value.");
  219. e.printStackTrace();
  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.getId()) && _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 (((L2NpcInstance) _actor).getFactionId() != null)
  256. {
  257. String faction_id = ((L2NpcInstance) _actor).getFactionId();
  258. Collection<L2Object> objs = _actor.getKnownList().getKnownObjects().values();
  259. //synchronized (_actor.getKnownList().getKnownObjects())
  260. {
  261. for (L2Object obj : objs)
  262. {
  263. if (!(obj instanceof L2NpcInstance))
  264. continue;
  265. L2NpcInstance npc = (L2NpcInstance) obj;
  266. if (faction_id != npc.getFactionId())
  267. continue;
  268. if (_actor.isInsideRadius(npc, npc.getFactionRange(), false, true) && Math.abs(getAttackTarget().getZ() - npc.getZ()) < 200)
  269. {
  270. npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, getAttackTarget(), 1);
  271. }
  272. }
  273. }
  274. }
  275. L2Skill[] skills = null;
  276. double dist2 = 0;
  277. int range = 0;
  278. int max_range = 0;
  279. try
  280. {
  281. _actor.setTarget(getAttackTarget());
  282. skills = _actor.getAllSkills();
  283. dist2 = _actor.getPlanDistanceSq(getAttackTarget().getX(), getAttackTarget().getY());
  284. range = _actor.getPhysicalAttackRange() + _actor.getTemplate().collisionRadius + getAttackTarget().getTemplate().collisionRadius;
  285. max_range = range;
  286. }
  287. catch (NullPointerException e)
  288. {
  289. _log.warning("Encountered Null Value.");
  290. e.printStackTrace();
  291. }
  292. if (!_actor.isMuted() && dist2 > (range + 20) * (range + 20))
  293. {
  294. // check distant skills
  295. for (L2Skill sk : skills)
  296. {
  297. int castRange = sk.getCastRange();
  298. if (castRange * castRange >= dist2 && !_actor.isSkillDisabled(sk.getId()) && _actor.getCurrentMp() > _actor.getStat().getMpConsume(sk))
  299. {
  300. _accessor.doCast(sk);
  301. return;
  302. }
  303. max_range = Math.max(max_range, castRange);
  304. }
  305. moveToPawn(getAttackTarget(), range);
  306. return;
  307. }
  308. // Force mobs to attack anybody if confused.
  309. L2Character hated;
  310. if (_actor.isConfused())
  311. hated = findNextRndTarget();
  312. else
  313. hated = getAttackTarget();
  314. if (hated == null)
  315. {
  316. setIntention(AI_INTENTION_ACTIVE);
  317. return;
  318. }
  319. if (hated != getAttackTarget())
  320. setAttackTarget(hated);
  321. if (!_actor.isMuted() && skills.length > 0 && Rnd.nextInt(5) == 3)
  322. {
  323. for (L2Skill sk : skills)
  324. {
  325. int castRange = sk.getCastRange();
  326. if (castRange * castRange >= dist2 && !_actor.isSkillDisabled(sk.getId()) && _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk))
  327. {
  328. _accessor.doCast(sk);
  329. return;
  330. }
  331. }
  332. }
  333. _accessor.doAttack(getAttackTarget());
  334. }
  335. }
  336. private void thinkActive()
  337. {
  338. setAttackTarget(findNextRndTarget());
  339. L2Character hated;
  340. if (_actor.isConfused())
  341. hated = findNextRndTarget();
  342. else
  343. hated = getAttackTarget();
  344. if (hated != null)
  345. {
  346. _actor.setRunning();
  347. setIntention(CtrlIntention.AI_INTENTION_ATTACK, hated);
  348. }
  349. return;
  350. }
  351. private boolean autoAttackCondition(L2Character target)
  352. {
  353. if (target == null || !(_actor instanceof L2Attackable))
  354. return false;
  355. L2Attackable me = (L2Attackable) _actor;
  356. if (target instanceof L2FolkInstance || target instanceof L2DoorInstance)
  357. return false;
  358. if (target.isAlikeDead() || !me.isInsideRadius(target, me.getAggroRange(), false, false) || Math.abs(_actor.getZ() - target.getZ()) > 100)
  359. return false;
  360. // Check if the target isn't invulnerable
  361. if (target.isInvul())
  362. return false;
  363. // Check if the target is a L2PlayableInstance
  364. if (target instanceof L2PlayableInstance)
  365. {
  366. // Check if the target isn't in silent move mode
  367. if (((L2PlayableInstance) target).isSilentMoving())
  368. return false;
  369. }
  370. if (target instanceof L2NpcInstance)
  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.size() == 0) // 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. }