L2ControllableMobAI.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2, or (at your option)
  5. * any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. * 02111-1307, USA.
  16. *
  17. * http://www.gnu.org/copyleft/gpl.html
  18. */
  19. package net.sf.l2j.gameserver.ai;
  20. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
  21. import static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  22. import java.util.List;
  23. import javolution.util.FastList;
  24. import net.sf.l2j.gameserver.model.L2Attackable;
  25. import net.sf.l2j.gameserver.model.L2Character;
  26. import net.sf.l2j.gameserver.model.L2Object;
  27. import net.sf.l2j.gameserver.model.L2Skill;
  28. import net.sf.l2j.gameserver.model.MobGroup;
  29. import net.sf.l2j.gameserver.model.MobGroupTable;
  30. import net.sf.l2j.gameserver.model.L2Character.AIAccessor;
  31. import net.sf.l2j.gameserver.model.actor.instance.L2ControllableMobInstance;
  32. import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
  33. import net.sf.l2j.gameserver.model.actor.instance.L2FolkInstance;
  34. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  35. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  36. import net.sf.l2j.gameserver.util.Util;
  37. import net.sf.l2j.util.Rnd;
  38. /**
  39. * @author littlecrow
  40. * AI for controllable mobs
  41. *
  42. */
  43. public class L2ControllableMobAI extends L2AttackableAI
  44. {
  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() || _actor.isAllSkillsDisabled())
  72. return;
  73. setThinking(true);
  74. try {
  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. setThinking(false);
  103. }
  104. }
  105. protected void thinkCast()
  106. {
  107. L2Attackable npc = (L2Attackable)_actor;
  108. if (getAttackTarget() == null || getAttackTarget().isAlikeDead())
  109. {
  110. setAttackTarget(findNextRndTarget());
  111. clientStopMoving(null);
  112. }
  113. if (getAttackTarget() == null)
  114. return;
  115. npc.setTarget(getAttackTarget());
  116. L2Skill[] skills = null;
  117. //double dist2 = 0;
  118. try {
  119. skills = _actor.getAllSkills();
  120. // dist2 = _actor.getPlanDistanceSq(getAttackTarget().getX(), getAttackTarget().getY());
  121. }
  122. catch (NullPointerException e) {
  123. _log.warning("Encountered Null Value.");
  124. e.printStackTrace();
  125. }
  126. if (!_actor.isMuted())
  127. {
  128. int max_range = 0;
  129. // check distant skills
  130. for (L2Skill sk : skills)
  131. {
  132. if (Util.checkIfInRange(sk.getCastRange(), _actor, getAttackTarget(), true)
  133. && !_actor.isSkillDisabled(sk.getId())
  134. && _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. 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. _log.warning("Encountered Null Value.");
  174. e.printStackTrace();
  175. }
  176. if (!_actor.isMuted() && dist2 > (range + 20) * (range + 20))
  177. {
  178. // check distant skills
  179. for (L2Skill sk : skills)
  180. {
  181. int castRange = sk.getCastRange();
  182. if (castRange * castRange >= dist2
  183. && !_actor.isSkillDisabled(sk.getId())
  184. && _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. _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. _log.warning("Encountered Null Value.");
  218. e.printStackTrace();
  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
  227. && !_actor.isSkillDisabled(sk.getId())
  228. && _actor.getCurrentMp() > _actor.getStat().getMpConsume(sk))
  229. {
  230. _accessor.doCast(sk);
  231. return;
  232. }
  233. max_range = Math.max(max_range, castRange);
  234. }
  235. if (!isNotMoving())
  236. moveToPawn(getForcedTarget(), _actor.getPhysicalAttackRange()/*range*/);
  237. return;
  238. }
  239. _accessor.doAttack(getForcedTarget());
  240. }
  241. protected void thinkAttack()
  242. {
  243. if (getAttackTarget() == null || getAttackTarget().isAlikeDead())
  244. {
  245. if (getAttackTarget() != null)
  246. {
  247. // stop hating
  248. L2Attackable npc = (L2Attackable) _actor;
  249. npc.stopHating(getAttackTarget());
  250. }
  251. setIntention(AI_INTENTION_ACTIVE);
  252. }
  253. else
  254. {
  255. // notify aggression
  256. if (((L2NpcInstance) _actor).getFactionId() != null)
  257. {
  258. String faction_id = ((L2NpcInstance) _actor).getFactionId();
  259. for (L2Object obj : _actor.getKnownList().getKnownObjects().values())
  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() - npc.getZ()) < 200)
  268. {
  269. npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, getAttackTarget(), 1);
  270. }
  271. }
  272. }
  273. L2Skill[] skills = null;
  274. double dist2 = 0;
  275. int range = 0;
  276. int max_range = 0;
  277. try {
  278. _actor.setTarget(getAttackTarget());
  279. skills = _actor.getAllSkills();
  280. dist2 = _actor.getPlanDistanceSq(getAttackTarget().getX(), getAttackTarget().getY());
  281. range = _actor.getPhysicalAttackRange() + _actor.getTemplate().collisionRadius + getAttackTarget().getTemplate().collisionRadius;
  282. max_range = range;
  283. }
  284. catch (NullPointerException e) {
  285. _log.warning("Encountered Null Value.");
  286. e.printStackTrace();
  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
  295. && !_actor.isSkillDisabled(sk.getId())
  296. && _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
  325. && !_actor.isSkillDisabled(sk.getId())
  326. && _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)) return false;
  354. L2Attackable me = (L2Attackable)_actor;
  355. if (target instanceof L2FolkInstance
  356. || target instanceof L2DoorInstance)
  357. return false;
  358. if (target.isAlikeDead()
  359. || !me.isInsideRadius(target, me.getAggroRange(), false, false)
  360. || Math.abs(_actor.getZ() - target.getZ()) > 100)
  361. return false;
  362. // Check if the target isn't invulnerable
  363. if (target.isInvul())
  364. return false;
  365. // Check if the target is a L2PcInstance
  366. if (target instanceof L2PcInstance)
  367. {
  368. // Check if the target isn't in silent move mode
  369. if (((L2PcInstance)target).isSilentMoving())
  370. return false;
  371. }
  372. if (target instanceof L2NpcInstance)
  373. return false;
  374. return me.isAggressive();
  375. }
  376. private L2Character findNextRndTarget()
  377. {
  378. int aggroRange = ((L2Attackable)_actor).getAggroRange();
  379. L2Attackable npc = (L2Attackable)_actor;
  380. int npcX, npcY, targetX, targetY;
  381. double dy, dx;
  382. double dblAggroRange = aggroRange*aggroRange;
  383. List<L2Character> potentialTarget = new FastList<L2Character>();
  384. for (L2Object obj : npc.getKnownList().getKnownObjects().values())
  385. {
  386. if (!(obj instanceof L2Character))
  387. continue;
  388. npcX = npc.getX();
  389. npcY = npc.getY();
  390. targetX = obj.getX();
  391. targetY = obj.getY();
  392. dx = npcX - targetX;
  393. dy = npcY - targetY;
  394. if (dx*dx + dy*dy > dblAggroRange)
  395. continue;
  396. L2Character target = (L2Character) obj;
  397. if (autoAttackCondition(target)) // check aggression
  398. potentialTarget.add(target);
  399. }
  400. if (potentialTarget.size() == 0) // nothing to do
  401. return null;
  402. // we choose a random target
  403. int choice = Rnd.nextInt(potentialTarget.size());
  404. L2Character target = potentialTarget.get(choice);
  405. return target;
  406. }
  407. private L2ControllableMobInstance findNextGroupTarget()
  408. {
  409. return getGroupTarget().getRandomMob();
  410. }
  411. public L2ControllableMobAI(AIAccessor accessor)
  412. {
  413. super(accessor);
  414. setAlternateAI(AI_IDLE);
  415. }
  416. public int getAlternateAI()
  417. {
  418. return _alternateAI;
  419. }
  420. public void setAlternateAI(int _alternateai)
  421. {
  422. _alternateAI = _alternateai;
  423. }
  424. public void forceAttack(L2Character target)
  425. {
  426. setAlternateAI(AI_FORCEATTACK);
  427. setForcedTarget(target);
  428. }
  429. public void forceAttackGroup(MobGroup group)
  430. {
  431. setForcedTarget(null);
  432. setGroupTarget(group);
  433. setAlternateAI(AI_ATTACK_GROUP);
  434. }
  435. public void stop()
  436. {
  437. setAlternateAI(AI_IDLE);
  438. clientStopMoving(null);
  439. }
  440. public void move(int x, int y, int z)
  441. {
  442. moveTo(x, y, z);
  443. }
  444. public void follow(L2Character target)
  445. {
  446. setAlternateAI(AI_FOLLOW);
  447. setForcedTarget(target);
  448. }
  449. public boolean isThinking()
  450. {
  451. return _isThinking;
  452. }
  453. public boolean isNotMoving()
  454. {
  455. return _isNotMoving;
  456. }
  457. public void setNotMoving(boolean isNotMoving)
  458. {
  459. _isNotMoving = isNotMoving;
  460. }
  461. public void setThinking(boolean isThinking)
  462. {
  463. _isThinking = isThinking;
  464. }
  465. private L2Character getForcedTarget()
  466. {
  467. return _forcedTarget;
  468. }
  469. private MobGroup getGroupTarget()
  470. {
  471. return _targetGroup;
  472. }
  473. private void setForcedTarget(L2Character forcedTarget)
  474. {
  475. _forcedTarget = forcedTarget;
  476. }
  477. private void setGroupTarget(MobGroup targetGroup)
  478. {
  479. _targetGroup = targetGroup;
  480. }
  481. }