L2ControllableMobAI.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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() || _actor.isAllSkillsDisabled())
  69. return;
  70. setThinking(true);
  71. try {
  72. switch (getAlternateAI())
  73. {
  74. case AI_IDLE:
  75. if (getIntention() != CtrlIntention.AI_INTENTION_ACTIVE)
  76. setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  77. break;
  78. case AI_FOLLOW:
  79. thinkFollow();
  80. break;
  81. case AI_CAST:
  82. thinkCast();
  83. break;
  84. case AI_FORCEATTACK:
  85. thinkForceAttack();
  86. break;
  87. case AI_ATTACK_GROUP:
  88. thinkAttackGroup();
  89. break;
  90. default:
  91. if (getIntention() == AI_INTENTION_ACTIVE)
  92. thinkActive();
  93. else if (getIntention() == AI_INTENTION_ATTACK)
  94. thinkAttack();
  95. break;
  96. }
  97. }
  98. finally {
  99. setThinking(false);
  100. }
  101. }
  102. protected void thinkCast()
  103. {
  104. L2Attackable npc = (L2Attackable)_actor;
  105. if (getAttackTarget() == null || getAttackTarget().isAlikeDead())
  106. {
  107. setAttackTarget(findNextRndTarget());
  108. clientStopMoving(null);
  109. }
  110. if (getAttackTarget() == null)
  111. return;
  112. npc.setTarget(getAttackTarget());
  113. L2Skill[] skills = null;
  114. //double dist2 = 0;
  115. try {
  116. skills = _actor.getAllSkills();
  117. // dist2 = _actor.getPlanDistanceSq(getAttackTarget().getX(), getAttackTarget().getY());
  118. }
  119. catch (NullPointerException e) {
  120. _log.warning("Encountered Null Value.");
  121. e.printStackTrace();
  122. }
  123. if (!_actor.isMuted())
  124. {
  125. int max_range = 0;
  126. // check distant skills
  127. for (L2Skill sk : skills)
  128. {
  129. if (Util.checkIfInRange(sk.getCastRange(), _actor, getAttackTarget(), true)
  130. && !_actor.isSkillDisabled(sk.getId())
  131. && _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. moveToPawn(getAttackTarget(), max_range);
  140. return;
  141. }
  142. }
  143. protected void thinkAttackGroup()
  144. {
  145. L2Character target = getForcedTarget();
  146. if (target == null || target.isAlikeDead())
  147. {
  148. // try to get next group target
  149. setForcedTarget(findNextGroupTarget());
  150. clientStopMoving(null);
  151. }
  152. if (target == null)
  153. return;
  154. L2Skill[] skills = null;
  155. double dist2 = 0;
  156. int range = 0;
  157. int max_range = 0;
  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. try {
  164. skills = _actor.getAllSkills();
  165. dist2 = _actor.getPlanDistanceSq(target.getX(), target.getY());
  166. range = _actor.getPhysicalAttackRange() + _actor.getTemplate().collisionRadius + target.getTemplate().collisionRadius;
  167. max_range = range;
  168. }
  169. catch (NullPointerException e) {
  170. _log.warning("Encountered Null Value.");
  171. e.printStackTrace();
  172. }
  173. if (!_actor.isMuted() && dist2 > (range + 20) * (range + 20))
  174. {
  175. // check distant skills
  176. for (L2Skill sk : skills)
  177. {
  178. int castRange = sk.getCastRange();
  179. if (castRange * castRange >= dist2
  180. && !_actor.isSkillDisabled(sk.getId())
  181. && _actor.getCurrentMp() > _actor.getStat().getMpConsume(sk))
  182. {
  183. _accessor.doCast(sk);
  184. return;
  185. }
  186. max_range = Math.max(max_range, castRange);
  187. }
  188. if (!isNotMoving())
  189. moveToPawn(target, range);
  190. return;
  191. }
  192. _accessor.doAttack(target);
  193. }
  194. protected void thinkForceAttack()
  195. {
  196. if (getForcedTarget() == null || getForcedTarget().isAlikeDead())
  197. {
  198. clientStopMoving(null);
  199. setIntention(AI_INTENTION_ACTIVE);
  200. setAlternateAI(AI_IDLE);
  201. }
  202. L2Skill[] skills = null;
  203. double dist2 = 0;
  204. int range = 0;
  205. int max_range = 0;
  206. try {
  207. _actor.setTarget(getForcedTarget());
  208. skills = _actor.getAllSkills();
  209. dist2 = _actor.getPlanDistanceSq(getForcedTarget().getX(), getForcedTarget().getY());
  210. range = _actor.getPhysicalAttackRange() + _actor.getTemplate().collisionRadius + getForcedTarget().getTemplate().collisionRadius;
  211. max_range = range;
  212. }
  213. catch (NullPointerException e) {
  214. _log.warning("Encountered Null Value.");
  215. e.printStackTrace();
  216. }
  217. if (!_actor.isMuted() && dist2 > (range + 20) * (range + 20))
  218. {
  219. // check distant skills
  220. for (L2Skill sk : skills)
  221. {
  222. int castRange = sk.getCastRange();
  223. if (castRange * castRange >= dist2
  224. && !_actor.isSkillDisabled(sk.getId())
  225. && _actor.getCurrentMp() > _actor.getStat().getMpConsume(sk))
  226. {
  227. _accessor.doCast(sk);
  228. return;
  229. }
  230. max_range = Math.max(max_range, castRange);
  231. }
  232. if (!isNotMoving())
  233. moveToPawn(getForcedTarget(), _actor.getPhysicalAttackRange()/*range*/);
  234. return;
  235. }
  236. _accessor.doAttack(getForcedTarget());
  237. }
  238. protected void thinkAttack()
  239. {
  240. if (getAttackTarget() == null || getAttackTarget().isAlikeDead())
  241. {
  242. if (getAttackTarget() != null)
  243. {
  244. // stop hating
  245. L2Attackable npc = (L2Attackable) _actor;
  246. npc.stopHating(getAttackTarget());
  247. }
  248. setIntention(AI_INTENTION_ACTIVE);
  249. }
  250. else
  251. {
  252. // notify aggression
  253. if (((L2NpcInstance) _actor).getFactionId() != null)
  254. {
  255. String faction_id = ((L2NpcInstance) _actor).getFactionId();
  256. Collection<L2Object> objs = _actor.getKnownList().getKnownObjects().values();
  257. //synchronized (_actor.getKnownList().getKnownObjects())
  258. {
  259. for (L2Object obj : objs)
  260. {
  261. if (!(obj instanceof L2NpcInstance))
  262. continue;
  263. L2NpcInstance npc = (L2NpcInstance) obj;
  264. if (faction_id != npc.getFactionId())
  265. continue;
  266. if (_actor.isInsideRadius(npc, npc.getFactionRange(), false, true)
  267. && Math.abs(getAttackTarget().getZ()
  268. - 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. _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. _log.warning("Encountered Null Value.");
  288. e.printStackTrace();
  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
  297. && !_actor.isSkillDisabled(sk.getId())
  298. && _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
  327. && !_actor.isSkillDisabled(sk.getId())
  328. && _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk))
  329. {
  330. _accessor.doCast(sk);
  331. return;
  332. }
  333. }
  334. }
  335. _accessor.doAttack(getAttackTarget());
  336. }
  337. }
  338. private void thinkActive()
  339. {
  340. setAttackTarget(findNextRndTarget());
  341. L2Character hated;
  342. if (_actor.isConfused())
  343. hated = findNextRndTarget();
  344. else
  345. hated = getAttackTarget();
  346. if (hated != null)
  347. {
  348. _actor.setRunning();
  349. setIntention(CtrlIntention.AI_INTENTION_ATTACK, hated);
  350. }
  351. return;
  352. }
  353. private boolean autoAttackCondition(L2Character target)
  354. {
  355. if (target == null || !(_actor instanceof L2Attackable)) return false;
  356. L2Attackable me = (L2Attackable)_actor;
  357. if (target instanceof L2FolkInstance
  358. || target instanceof L2DoorInstance)
  359. return false;
  360. if (target.isAlikeDead()
  361. || !me.isInsideRadius(target, me.getAggroRange(), false, false)
  362. || Math.abs(_actor.getZ() - target.getZ()) > 100)
  363. return false;
  364. // Check if the target isn't invulnerable
  365. if (target.isInvul())
  366. return false;
  367. // Check if the target is a L2PlayableInstance
  368. if (target instanceof L2PlayableInstance)
  369. {
  370. // Check if the target isn't in silent move mode
  371. if (((L2PlayableInstance)target).isSilentMoving())
  372. return false;
  373. }
  374. if (target instanceof L2NpcInstance)
  375. return false;
  376. return me.isAggressive();
  377. }
  378. private L2Character findNextRndTarget()
  379. {
  380. int aggroRange = ((L2Attackable)_actor).getAggroRange();
  381. L2Attackable npc = (L2Attackable)_actor;
  382. int npcX, npcY, targetX, targetY;
  383. double dy, dx;
  384. double dblAggroRange = aggroRange*aggroRange;
  385. List<L2Character> potentialTarget = new FastList<L2Character>();
  386. Collection<L2Object> objs = npc.getKnownList().getKnownObjects().values();
  387. //synchronized (npc.getKnownList().getKnownObjects())
  388. {
  389. for (L2Object obj : objs)
  390. {
  391. if (!(obj instanceof L2Character))
  392. continue;
  393. npcX = npc.getX();
  394. npcY = npc.getY();
  395. targetX = obj.getX();
  396. targetY = obj.getY();
  397. dx = npcX - targetX;
  398. dy = npcY - targetY;
  399. if (dx * dx + dy * dy > dblAggroRange)
  400. continue;
  401. L2Character target = (L2Character) obj;
  402. if (autoAttackCondition(target)) // check aggression
  403. potentialTarget.add(target);
  404. }
  405. }
  406. if (potentialTarget.size() == 0) // nothing to do
  407. return null;
  408. // we choose a random target
  409. int choice = Rnd.nextInt(potentialTarget.size());
  410. L2Character target = potentialTarget.get(choice);
  411. return target;
  412. }
  413. private L2ControllableMobInstance findNextGroupTarget()
  414. {
  415. return getGroupTarget().getRandomMob();
  416. }
  417. public L2ControllableMobAI(AIAccessor accessor)
  418. {
  419. super(accessor);
  420. setAlternateAI(AI_IDLE);
  421. }
  422. public int getAlternateAI()
  423. {
  424. return _alternateAI;
  425. }
  426. public void setAlternateAI(int _alternateai)
  427. {
  428. _alternateAI = _alternateai;
  429. }
  430. public void forceAttack(L2Character target)
  431. {
  432. setAlternateAI(AI_FORCEATTACK);
  433. setForcedTarget(target);
  434. }
  435. public void forceAttackGroup(MobGroup group)
  436. {
  437. setForcedTarget(null);
  438. setGroupTarget(group);
  439. setAlternateAI(AI_ATTACK_GROUP);
  440. }
  441. public void stop()
  442. {
  443. setAlternateAI(AI_IDLE);
  444. clientStopMoving(null);
  445. }
  446. public void move(int x, int y, int z)
  447. {
  448. moveTo(x, y, z);
  449. }
  450. public void follow(L2Character target)
  451. {
  452. setAlternateAI(AI_FOLLOW);
  453. setForcedTarget(target);
  454. }
  455. public boolean isThinking()
  456. {
  457. return _isThinking;
  458. }
  459. public boolean isNotMoving()
  460. {
  461. return _isNotMoving;
  462. }
  463. public void setNotMoving(boolean isNotMoving)
  464. {
  465. _isNotMoving = isNotMoving;
  466. }
  467. public void setThinking(boolean isThinking)
  468. {
  469. _isThinking = isThinking;
  470. }
  471. private L2Character getForcedTarget()
  472. {
  473. return _forcedTarget;
  474. }
  475. private MobGroup getGroupTarget()
  476. {
  477. return _targetGroup;
  478. }
  479. private void setForcedTarget(L2Character forcedTarget)
  480. {
  481. _forcedTarget = forcedTarget;
  482. }
  483. private void setGroupTarget(MobGroup targetGroup)
  484. {
  485. _targetGroup = targetGroup;
  486. }
  487. }