L2ControllableMobAI.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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.util.Util;
  34. import com.l2jserver.util.Rnd;
  35. import javolution.util.FastList;
  36. /**
  37. * @author littlecrow
  38. * AI for controllable mobs
  39. *
  40. */
  41. public class L2ControllableMobAI extends L2AttackableAI
  42. {
  43. public static final int AI_IDLE = 1;
  44. public static final int AI_NORMAL = 2;
  45. public static final int AI_FORCEATTACK = 3;
  46. public static final int AI_FOLLOW = 4;
  47. public static final int AI_CAST = 5;
  48. public static final int AI_ATTACK_GROUP = 6;
  49. private int _alternateAI;
  50. private boolean _isThinking; // to prevent thinking recursively
  51. private boolean _isNotMoving;
  52. private L2Character _forcedTarget;
  53. private MobGroup _targetGroup;
  54. protected void thinkFollow()
  55. {
  56. L2Attackable me = (L2Attackable) _actor;
  57. if (!Util.checkIfInRange(MobGroupTable.FOLLOW_RANGE, me, getForcedTarget(), true))
  58. {
  59. int signX = (Rnd.nextInt(2) == 0) ? -1 : 1;
  60. int signY = (Rnd.nextInt(2) == 0) ? -1 : 1;
  61. int randX = Rnd.nextInt(MobGroupTable.FOLLOW_RANGE);
  62. int randY = Rnd.nextInt(MobGroupTable.FOLLOW_RANGE);
  63. moveTo(getForcedTarget().getX() + signX * randX, getForcedTarget().getY() + signY * randY, getForcedTarget().getZ());
  64. }
  65. }
  66. @Override
  67. protected void onEvtThink()
  68. {
  69. if (isThinking())
  70. return;
  71. setThinking(true);
  72. try
  73. {
  74. switch (getAlternateAI())
  75. {
  76. case AI_IDLE:
  77. if (getIntention() != CtrlIntention.AI_INTENTION_ACTIVE)
  78. setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  79. break;
  80. case AI_FOLLOW:
  81. thinkFollow();
  82. break;
  83. case AI_CAST:
  84. thinkCast();
  85. break;
  86. case AI_FORCEATTACK:
  87. thinkForceAttack();
  88. break;
  89. case AI_ATTACK_GROUP:
  90. thinkAttackGroup();
  91. break;
  92. default:
  93. if (getIntention() == AI_INTENTION_ACTIVE)
  94. thinkActive();
  95. else if (getIntention() == AI_INTENTION_ATTACK)
  96. thinkAttack();
  97. break;
  98. }
  99. }
  100. finally
  101. {
  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. {
  120. skills = _actor.getAllSkills();
  121. // dist2 = _actor.getPlanDistanceSq(getAttackTarget().getX(), getAttackTarget().getY());
  122. }
  123. catch (NullPointerException e)
  124. {
  125. _log.log(Level.WARNING, "Encountered Null Value: " + e.getMessage(), e);
  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.log(Level.WARNING, "Encountered Null Value: " + e.getMessage(), e);
  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 && !_actor.isSkillDisabled(sk) && _actor.getCurrentMp() > _actor.getStat().getMpConsume(sk))
  183. {
  184. _accessor.doCast(sk);
  185. return;
  186. }
  187. max_range = Math.max(max_range, castRange);
  188. }
  189. if (!isNotMoving())
  190. moveToPawn(target, range);
  191. return;
  192. }
  193. _accessor.doAttack(target);
  194. }
  195. protected void thinkForceAttack()
  196. {
  197. if (getForcedTarget() == null || getForcedTarget().isAlikeDead())
  198. {
  199. clientStopMoving(null);
  200. setIntention(AI_INTENTION_ACTIVE);
  201. setAlternateAI(AI_IDLE);
  202. }
  203. L2Skill[] skills = null;
  204. double dist2 = 0;
  205. int range = 0;
  206. int max_range = 0;
  207. try
  208. {
  209. _actor.setTarget(getForcedTarget());
  210. skills = _actor.getAllSkills();
  211. dist2 = _actor.getPlanDistanceSq(getForcedTarget().getX(), getForcedTarget().getY());
  212. range = _actor.getPhysicalAttackRange() + _actor.getTemplate().collisionRadius + getForcedTarget().getTemplate().collisionRadius;
  213. max_range = range;
  214. }
  215. catch (NullPointerException e)
  216. {
  217. _log.log(Level.WARNING, "Encountered Null Value: " + e.getMessage(), e);
  218. }
  219. if (!_actor.isMuted() && dist2 > (range + 20) * (range + 20))
  220. {
  221. // check distant skills
  222. for (L2Skill sk : skills)
  223. {
  224. int castRange = sk.getCastRange();
  225. if (castRange * castRange >= dist2 && !_actor.isSkillDisabled(sk) && _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 (((L2Npc) _actor).getFactionId() != null)
  254. {
  255. String faction_id = ((L2Npc) _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 L2Npc))
  262. continue;
  263. L2Npc npc = (L2Npc) obj;
  264. if (!faction_id.equals(npc.getFactionId()))
  265. continue;
  266. if (_actor.isInsideRadius(npc, npc.getFactionRange(), false, true) && Math.abs(getAttackTarget().getZ() - npc.getZ()) < 200)
  267. {
  268. npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, getAttackTarget(), 1);
  269. }
  270. }
  271. }
  272. }
  273. L2Skill[] skills = null;
  274. double dist2 = 0;
  275. int range = 0;
  276. int max_range = 0;
  277. try
  278. {
  279. _actor.setTarget(getAttackTarget());
  280. skills = _actor.getAllSkills();
  281. dist2 = _actor.getPlanDistanceSq(getAttackTarget().getX(), getAttackTarget().getY());
  282. range = _actor.getPhysicalAttackRange() + _actor.getTemplate().collisionRadius + getAttackTarget().getTemplate().collisionRadius;
  283. max_range = range;
  284. }
  285. catch (NullPointerException e)
  286. {
  287. _log.log(Level.WARNING, "Encountered Null Value: " + e.getMessage(), e);
  288. }
  289. if (!_actor.isMuted() && dist2 > (range + 20) * (range + 20))
  290. {
  291. // check distant skills
  292. for (L2Skill sk : skills)
  293. {
  294. int castRange = sk.getCastRange();
  295. if (castRange * castRange >= dist2 && !_actor.isSkillDisabled(sk) && _actor.getCurrentMp() > _actor.getStat().getMpConsume(sk))
  296. {
  297. _accessor.doCast(sk);
  298. return;
  299. }
  300. max_range = Math.max(max_range, castRange);
  301. }
  302. moveToPawn(getAttackTarget(), range);
  303. return;
  304. }
  305. // Force mobs to attack anybody if confused.
  306. L2Character hated;
  307. if (_actor.isConfused())
  308. hated = findNextRndTarget();
  309. else
  310. hated = getAttackTarget();
  311. if (hated == null)
  312. {
  313. setIntention(AI_INTENTION_ACTIVE);
  314. return;
  315. }
  316. if (hated != getAttackTarget())
  317. setAttackTarget(hated);
  318. if (!_actor.isMuted() && skills.length > 0 && Rnd.nextInt(5) == 3)
  319. {
  320. for (L2Skill sk : skills)
  321. {
  322. int castRange = sk.getCastRange();
  323. if (castRange * castRange >= dist2 && !_actor.isSkillDisabled(sk) && _actor.getCurrentMp() < _actor.getStat().getMpConsume(sk))
  324. {
  325. _accessor.doCast(sk);
  326. return;
  327. }
  328. }
  329. }
  330. _accessor.doAttack(getAttackTarget());
  331. }
  332. }
  333. private void thinkActive()
  334. {
  335. setAttackTarget(findNextRndTarget());
  336. L2Character hated;
  337. if (_actor.isConfused())
  338. hated = findNextRndTarget();
  339. else
  340. hated = getAttackTarget();
  341. if (hated != null)
  342. {
  343. _actor.setRunning();
  344. setIntention(CtrlIntention.AI_INTENTION_ATTACK, hated);
  345. }
  346. }
  347. private boolean autoAttackCondition(L2Character target)
  348. {
  349. if (target == null || !(_actor instanceof L2Attackable))
  350. return false;
  351. L2Attackable me = (L2Attackable) _actor;
  352. if (target instanceof L2NpcInstance || target instanceof L2DoorInstance)
  353. return false;
  354. if (target.isAlikeDead() || !me.isInsideRadius(target, me.getAggroRange(), false, false) || Math.abs(_actor.getZ() - target.getZ()) > 100)
  355. return false;
  356. // Check if the target isn't invulnerable
  357. if (target.isInvul())
  358. return false;
  359. // Check if the target is a L2PlayableInstance
  360. if (target instanceof L2Playable)
  361. {
  362. // Check if the target isn't in silent move mode
  363. if (((L2Playable) target).isSilentMoving())
  364. return false;
  365. }
  366. if (target instanceof L2Npc)
  367. return false;
  368. return me.isAggressive();
  369. }
  370. private L2Character findNextRndTarget()
  371. {
  372. int aggroRange = ((L2Attackable) _actor).getAggroRange();
  373. L2Attackable npc = (L2Attackable) _actor;
  374. int npcX, npcY, targetX, targetY;
  375. double dy, dx;
  376. double dblAggroRange = aggroRange * aggroRange;
  377. List<L2Character> potentialTarget = new FastList<L2Character>();
  378. Collection<L2Object> objs = npc.getKnownList().getKnownObjects().values();
  379. //synchronized (npc.getKnownList().getKnownObjects())
  380. {
  381. for (L2Object obj : objs)
  382. {
  383. if (!(obj instanceof L2Character))
  384. continue;
  385. npcX = npc.getX();
  386. npcY = npc.getY();
  387. targetX = obj.getX();
  388. targetY = obj.getY();
  389. dx = npcX - targetX;
  390. dy = npcY - targetY;
  391. if (dx * dx + dy * dy > dblAggroRange)
  392. continue;
  393. L2Character target = (L2Character) obj;
  394. if (autoAttackCondition(target)) // check aggression
  395. potentialTarget.add(target);
  396. }
  397. }
  398. if (potentialTarget.isEmpty()) // nothing to do
  399. return null;
  400. // we choose a random target
  401. int choice = Rnd.nextInt(potentialTarget.size());
  402. L2Character target = potentialTarget.get(choice);
  403. return target;
  404. }
  405. private L2ControllableMobInstance findNextGroupTarget()
  406. {
  407. return getGroupTarget().getRandomMob();
  408. }
  409. public L2ControllableMobAI(AIAccessor accessor)
  410. {
  411. super(accessor);
  412. setAlternateAI(AI_IDLE);
  413. }
  414. public int getAlternateAI()
  415. {
  416. return _alternateAI;
  417. }
  418. public void setAlternateAI(int _alternateai)
  419. {
  420. _alternateAI = _alternateai;
  421. }
  422. public void forceAttack(L2Character target)
  423. {
  424. setAlternateAI(AI_FORCEATTACK);
  425. setForcedTarget(target);
  426. }
  427. public void forceAttackGroup(MobGroup group)
  428. {
  429. setForcedTarget(null);
  430. setGroupTarget(group);
  431. setAlternateAI(AI_ATTACK_GROUP);
  432. }
  433. public void stop()
  434. {
  435. setAlternateAI(AI_IDLE);
  436. clientStopMoving(null);
  437. }
  438. public void move(int x, int y, int z)
  439. {
  440. moveTo(x, y, z);
  441. }
  442. public void follow(L2Character target)
  443. {
  444. setAlternateAI(AI_FOLLOW);
  445. setForcedTarget(target);
  446. }
  447. public boolean isThinking()
  448. {
  449. return _isThinking;
  450. }
  451. public boolean isNotMoving()
  452. {
  453. return _isNotMoving;
  454. }
  455. public void setNotMoving(boolean isNotMoving)
  456. {
  457. _isNotMoving = isNotMoving;
  458. }
  459. public void setThinking(boolean isThinking)
  460. {
  461. _isThinking = isThinking;
  462. }
  463. private L2Character getForcedTarget()
  464. {
  465. return _forcedTarget;
  466. }
  467. private MobGroup getGroupTarget()
  468. {
  469. return _targetGroup;
  470. }
  471. private void setForcedTarget(L2Character forcedTarget)
  472. {
  473. _forcedTarget = forcedTarget;
  474. }
  475. private void setGroupTarget(MobGroup targetGroup)
  476. {
  477. _targetGroup = targetGroup;
  478. }
  479. }