L2ControllableMobAI.java 14 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 com.l2jserver.gameserver.model.L2Object;
  21. import com.l2jserver.gameserver.model.L2Skill;
  22. import com.l2jserver.gameserver.model.MobGroup;
  23. import com.l2jserver.gameserver.model.MobGroupTable;
  24. import com.l2jserver.gameserver.model.actor.L2Attackable;
  25. import com.l2jserver.gameserver.model.actor.L2Character;
  26. import com.l2jserver.gameserver.model.actor.L2Npc;
  27. import com.l2jserver.gameserver.model.actor.L2Playable;
  28. import com.l2jserver.gameserver.model.actor.L2Character.AIAccessor;
  29. import com.l2jserver.gameserver.model.actor.instance.L2ControllableMobInstance;
  30. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  31. import com.l2jserver.gameserver.model.actor.instance.L2NpcInstance;
  32. import com.l2jserver.gameserver.util.Util;
  33. import com.l2jserver.util.Rnd;
  34. import javolution.util.FastList;
  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) && _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) && _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) && _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. //synchronized (_actor.getKnownList().getKnownObjects())
  260. {
  261. for (L2Object obj : objs)
  262. {
  263. if (!(obj instanceof L2Npc))
  264. continue;
  265. L2Npc npc = (L2Npc) obj;
  266. if (!faction_id.equals(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) && _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) && _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. }
  350. private boolean autoAttackCondition(L2Character target)
  351. {
  352. if (target == null || !(_actor instanceof L2Attackable))
  353. return false;
  354. L2Attackable me = (L2Attackable) _actor;
  355. if (target instanceof L2NpcInstance || target instanceof L2DoorInstance)
  356. return false;
  357. if (target.isAlikeDead() || !me.isInsideRadius(target, me.getAggroRange(), false, false) || Math.abs(_actor.getZ() - target.getZ()) > 100)
  358. return false;
  359. // Check if the target isn't invulnerable
  360. if (target.isInvul())
  361. return false;
  362. // Check if the target is a L2PlayableInstance
  363. if (target instanceof L2Playable)
  364. {
  365. // Check if the target isn't in silent move mode
  366. if (((L2Playable) target).isSilentMoving())
  367. return false;
  368. }
  369. if (target instanceof L2Npc)
  370. return false;
  371. return me.isAggressive();
  372. }
  373. private L2Character findNextRndTarget()
  374. {
  375. int aggroRange = ((L2Attackable) _actor).getAggroRange();
  376. L2Attackable npc = (L2Attackable) _actor;
  377. int npcX, npcY, targetX, targetY;
  378. double dy, dx;
  379. double dblAggroRange = aggroRange * aggroRange;
  380. List<L2Character> potentialTarget = new FastList<L2Character>();
  381. Collection<L2Object> objs = npc.getKnownList().getKnownObjects().values();
  382. //synchronized (npc.getKnownList().getKnownObjects())
  383. {
  384. for (L2Object obj : objs)
  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. }
  401. if (potentialTarget.isEmpty()) // nothing to do
  402. return null;
  403. // we choose a random target
  404. int choice = Rnd.nextInt(potentialTarget.size());
  405. L2Character target = potentialTarget.get(choice);
  406. return target;
  407. }
  408. private L2ControllableMobInstance findNextGroupTarget()
  409. {
  410. return getGroupTarget().getRandomMob();
  411. }
  412. public L2ControllableMobAI(AIAccessor accessor)
  413. {
  414. super(accessor);
  415. setAlternateAI(AI_IDLE);
  416. }
  417. public int getAlternateAI()
  418. {
  419. return _alternateAI;
  420. }
  421. public void setAlternateAI(int _alternateai)
  422. {
  423. _alternateAI = _alternateai;
  424. }
  425. public void forceAttack(L2Character target)
  426. {
  427. setAlternateAI(AI_FORCEATTACK);
  428. setForcedTarget(target);
  429. }
  430. public void forceAttackGroup(MobGroup group)
  431. {
  432. setForcedTarget(null);
  433. setGroupTarget(group);
  434. setAlternateAI(AI_ATTACK_GROUP);
  435. }
  436. public void stop()
  437. {
  438. setAlternateAI(AI_IDLE);
  439. clientStopMoving(null);
  440. }
  441. public void move(int x, int y, int z)
  442. {
  443. moveTo(x, y, z);
  444. }
  445. public void follow(L2Character target)
  446. {
  447. setAlternateAI(AI_FOLLOW);
  448. setForcedTarget(target);
  449. }
  450. public boolean isThinking()
  451. {
  452. return _isThinking;
  453. }
  454. public boolean isNotMoving()
  455. {
  456. return _isNotMoving;
  457. }
  458. public void setNotMoving(boolean isNotMoving)
  459. {
  460. _isNotMoving = isNotMoving;
  461. }
  462. public void setThinking(boolean isThinking)
  463. {
  464. _isThinking = isThinking;
  465. }
  466. private L2Character getForcedTarget()
  467. {
  468. return _forcedTarget;
  469. }
  470. private MobGroup getGroupTarget()
  471. {
  472. return _targetGroup;
  473. }
  474. private void setForcedTarget(L2Character forcedTarget)
  475. {
  476. _forcedTarget = forcedTarget;
  477. }
  478. private void setGroupTarget(MobGroup targetGroup)
  479. {
  480. _targetGroup = targetGroup;
  481. }
  482. }