L2ControllableMobAI.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.ai;
  20. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
  21. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import javolution.util.FastList;
  25. import com.l2jserver.gameserver.model.L2Object;
  26. import com.l2jserver.gameserver.model.MobGroup;
  27. import com.l2jserver.gameserver.model.MobGroupTable;
  28. import com.l2jserver.gameserver.model.actor.L2Attackable;
  29. import com.l2jserver.gameserver.model.actor.L2Character;
  30. import com.l2jserver.gameserver.model.actor.L2Character.AIAccessor;
  31. import com.l2jserver.gameserver.model.actor.L2Npc;
  32. import com.l2jserver.gameserver.model.actor.L2Playable;
  33. import com.l2jserver.gameserver.model.actor.instance.L2ControllableMobInstance;
  34. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  35. import com.l2jserver.gameserver.model.actor.instance.L2NpcInstance;
  36. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  37. import com.l2jserver.gameserver.model.skills.L2Skill;
  38. import com.l2jserver.gameserver.util.Util;
  39. import com.l2jserver.util.Rnd;
  40. /**
  41. * AI for controllable mobs
  42. * @author littlecrow
  43. */
  44. public class L2ControllableMobAI extends L2AttackableAI
  45. {
  46. public static final int AI_IDLE = 1;
  47. public static final int AI_NORMAL = 2;
  48. public static final int AI_FORCEATTACK = 3;
  49. public static final int AI_FOLLOW = 4;
  50. public static final int AI_CAST = 5;
  51. public static final int AI_ATTACK_GROUP = 6;
  52. private int _alternateAI;
  53. private boolean _isThinking; // to prevent thinking recursively
  54. private boolean _isNotMoving;
  55. private L2Character _forcedTarget;
  56. private MobGroup _targetGroup;
  57. protected void thinkFollow()
  58. {
  59. L2Attackable me = (L2Attackable) _actor;
  60. if (!Util.checkIfInRange(MobGroupTable.FOLLOW_RANGE, me, getForcedTarget(), true))
  61. {
  62. int signX = (Rnd.nextInt(2) == 0) ? -1 : 1;
  63. int signY = (Rnd.nextInt(2) == 0) ? -1 : 1;
  64. int randX = Rnd.nextInt(MobGroupTable.FOLLOW_RANGE);
  65. int randY = Rnd.nextInt(MobGroupTable.FOLLOW_RANGE);
  66. moveTo(getForcedTarget().getX() + (signX * randX), getForcedTarget().getY() + (signY * randY), getForcedTarget().getZ());
  67. }
  68. }
  69. @Override
  70. protected void onEvtThink()
  71. {
  72. if (isThinking())
  73. {
  74. return;
  75. }
  76. setThinking(true);
  77. try
  78. {
  79. switch (getAlternateAI())
  80. {
  81. case AI_IDLE:
  82. if (getIntention() != CtrlIntention.AI_INTENTION_ACTIVE)
  83. {
  84. setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  85. }
  86. break;
  87. case AI_FOLLOW:
  88. thinkFollow();
  89. break;
  90. case AI_CAST:
  91. thinkCast();
  92. break;
  93. case AI_FORCEATTACK:
  94. thinkForceAttack();
  95. break;
  96. case AI_ATTACK_GROUP:
  97. thinkAttackGroup();
  98. break;
  99. default:
  100. if (getIntention() == AI_INTENTION_ACTIVE)
  101. {
  102. thinkActive();
  103. }
  104. else if (getIntention() == AI_INTENTION_ATTACK)
  105. {
  106. thinkAttack();
  107. }
  108. break;
  109. }
  110. }
  111. finally
  112. {
  113. setThinking(false);
  114. }
  115. }
  116. protected void thinkCast()
  117. {
  118. L2Attackable npc = (L2Attackable) _actor;
  119. if ((getAttackTarget() == null) || getAttackTarget().isAlikeDead())
  120. {
  121. setAttackTarget(findNextRndTarget());
  122. clientStopMoving(null);
  123. }
  124. if (getAttackTarget() == null)
  125. {
  126. return;
  127. }
  128. npc.setTarget(getAttackTarget());
  129. if (!_actor.isMuted())
  130. {
  131. int max_range = 0;
  132. // check distant skills
  133. for (L2Skill sk : _actor.getAllSkills())
  134. {
  135. if (Util.checkIfInRange(sk.getCastRange(), _actor, getAttackTarget(), true) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() > _actor.getStat().getMpConsume(sk)))
  136. {
  137. _accessor.doCast(sk);
  138. return;
  139. }
  140. max_range = Math.max(max_range, sk.getCastRange());
  141. }
  142. if (!isNotMoving())
  143. {
  144. moveToPawn(getAttackTarget(), max_range);
  145. }
  146. return;
  147. }
  148. }
  149. protected void thinkAttackGroup()
  150. {
  151. L2Character target = getForcedTarget();
  152. if ((target == null) || target.isAlikeDead())
  153. {
  154. // try to get next group target
  155. setForcedTarget(findNextGroupTarget());
  156. clientStopMoving(null);
  157. }
  158. if (target == null)
  159. {
  160. return;
  161. }
  162. _actor.setTarget(target);
  163. // as a response, we put the target in a forcedattack mode
  164. L2ControllableMobInstance theTarget = (L2ControllableMobInstance) target;
  165. L2ControllableMobAI ctrlAi = (L2ControllableMobAI) theTarget.getAI();
  166. ctrlAi.forceAttack(_actor);
  167. double dist2 = _actor.calculateDistance(target, false, true);
  168. int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + target.getTemplate().getCollisionRadius();
  169. int max_range = range;
  170. if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20))))
  171. {
  172. // check distant skills
  173. for (L2Skill sk : _actor.getAllSkills())
  174. {
  175. int castRange = sk.getCastRange();
  176. if (((castRange * castRange) >= dist2) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() > _actor.getStat().getMpConsume(sk)))
  177. {
  178. _accessor.doCast(sk);
  179. return;
  180. }
  181. max_range = Math.max(max_range, castRange);
  182. }
  183. if (!isNotMoving())
  184. {
  185. moveToPawn(target, range);
  186. }
  187. return;
  188. }
  189. _accessor.doAttack(target);
  190. }
  191. protected void thinkForceAttack()
  192. {
  193. if ((getForcedTarget() == null) || getForcedTarget().isAlikeDead())
  194. {
  195. clientStopMoving(null);
  196. setIntention(AI_INTENTION_ACTIVE);
  197. setAlternateAI(AI_IDLE);
  198. }
  199. _actor.setTarget(getForcedTarget());
  200. double dist2 = _actor.calculateDistance(getForcedTarget(), false, true);
  201. int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + getForcedTarget().getTemplate().getCollisionRadius();
  202. int max_range = range;
  203. if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20))))
  204. {
  205. // check distant skills
  206. for (L2Skill sk : _actor.getAllSkills())
  207. {
  208. int castRange = sk.getCastRange();
  209. if (((castRange * castRange) >= dist2) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() > _actor.getStat().getMpConsume(sk)))
  210. {
  211. _accessor.doCast(sk);
  212. return;
  213. }
  214. max_range = Math.max(max_range, castRange);
  215. }
  216. if (!isNotMoving())
  217. {
  218. moveToPawn(getForcedTarget(), _actor.getPhysicalAttackRange()/* range */);
  219. }
  220. return;
  221. }
  222. _accessor.doAttack(getForcedTarget());
  223. }
  224. protected void thinkAttack()
  225. {
  226. if ((getAttackTarget() == null) || getAttackTarget().isAlikeDead())
  227. {
  228. if (getAttackTarget() != null)
  229. {
  230. // stop hating
  231. L2Attackable npc = (L2Attackable) _actor;
  232. npc.stopHating(getAttackTarget());
  233. }
  234. setIntention(AI_INTENTION_ACTIVE);
  235. }
  236. else
  237. {
  238. // notify aggression
  239. if (((L2Npc) _actor).getFactionId() != null)
  240. {
  241. String faction_id = ((L2Npc) _actor).getFactionId();
  242. Collection<L2Object> objs = _actor.getKnownList().getKnownObjects().values();
  243. for (L2Object obj : objs)
  244. {
  245. if (!(obj instanceof L2Npc))
  246. {
  247. continue;
  248. }
  249. L2Npc npc = (L2Npc) obj;
  250. if (!faction_id.equals(npc.getFactionId()))
  251. {
  252. continue;
  253. }
  254. if (_actor.isInsideRadius(npc, npc.getFactionRange(), false, true) && (Math.abs(getAttackTarget().getZ() - npc.getZ()) < 200))
  255. {
  256. npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, getAttackTarget(), 1);
  257. }
  258. }
  259. }
  260. _actor.setTarget(getAttackTarget());
  261. double dist2 = _actor.calculateDistance(getAttackTarget(), false, true);
  262. int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + getAttackTarget().getTemplate().getCollisionRadius();
  263. int max_range = range;
  264. if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20))))
  265. {
  266. // check distant skills
  267. for (L2Skill sk : _actor.getAllSkills())
  268. {
  269. int castRange = sk.getCastRange();
  270. if (((castRange * castRange) >= dist2) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() > _actor.getStat().getMpConsume(sk)))
  271. {
  272. _accessor.doCast(sk);
  273. return;
  274. }
  275. max_range = Math.max(max_range, castRange);
  276. }
  277. moveToPawn(getAttackTarget(), range);
  278. return;
  279. }
  280. // Force mobs to attack anybody if confused.
  281. L2Character hated;
  282. if (_actor.isConfused())
  283. {
  284. hated = findNextRndTarget();
  285. }
  286. else
  287. {
  288. hated = getAttackTarget();
  289. }
  290. if (hated == null)
  291. {
  292. setIntention(AI_INTENTION_ACTIVE);
  293. return;
  294. }
  295. if (hated != getAttackTarget())
  296. {
  297. setAttackTarget(hated);
  298. }
  299. if (!_actor.isMuted() && (Rnd.nextInt(5) == 3))
  300. {
  301. for (L2Skill sk : _actor.getAllSkills())
  302. {
  303. int castRange = sk.getCastRange();
  304. if (((castRange * castRange) >= dist2) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() < _actor.getStat().getMpConsume(sk)))
  305. {
  306. _accessor.doCast(sk);
  307. return;
  308. }
  309. }
  310. }
  311. _accessor.doAttack(getAttackTarget());
  312. }
  313. }
  314. private void thinkActive()
  315. {
  316. setAttackTarget(findNextRndTarget());
  317. L2Character hated;
  318. if (_actor.isConfused())
  319. {
  320. hated = findNextRndTarget();
  321. }
  322. else
  323. {
  324. hated = getAttackTarget();
  325. }
  326. if (hated != null)
  327. {
  328. _actor.setRunning();
  329. setIntention(CtrlIntention.AI_INTENTION_ATTACK, hated);
  330. }
  331. }
  332. private boolean autoAttackCondition(L2Character target)
  333. {
  334. if ((target == null) || !(_actor instanceof L2Attackable))
  335. {
  336. return false;
  337. }
  338. L2Attackable me = (L2Attackable) _actor;
  339. if ((target instanceof L2NpcInstance) || (target instanceof L2DoorInstance))
  340. {
  341. return false;
  342. }
  343. if (target.isAlikeDead() || !me.isInsideRadius(target, me.getAggroRange(), false, false) || (Math.abs(_actor.getZ() - target.getZ()) > 100))
  344. {
  345. return false;
  346. }
  347. // Check if the target isn't invulnerable
  348. if (target.isInvul())
  349. {
  350. return false;
  351. }
  352. // Spawn protection (only against mobs)
  353. if ((target instanceof L2PcInstance) && ((L2PcInstance) target).isSpawnProtected())
  354. {
  355. return false;
  356. }
  357. // Check if the target is a L2Playable
  358. if (target.isPlayable())
  359. {
  360. // Check if the target isn't in silent move mode
  361. if (((L2Playable) target).isSilentMoving())
  362. {
  363. return false;
  364. }
  365. }
  366. if (target instanceof L2Npc)
  367. {
  368. return false;
  369. }
  370. return me.isAggressive();
  371. }
  372. private L2Character findNextRndTarget()
  373. {
  374. int aggroRange = ((L2Attackable) _actor).getAggroRange();
  375. L2Attackable npc = (L2Attackable) _actor;
  376. int npcX, npcY, targetX, targetY;
  377. double dy, dx;
  378. double dblAggroRange = aggroRange * aggroRange;
  379. List<L2Character> potentialTarget = new FastList<>();
  380. Collection<L2Object> objs = npc.getKnownList().getKnownObjects().values();
  381. for (L2Object obj : objs)
  382. {
  383. if (!(obj instanceof L2Character))
  384. {
  385. continue;
  386. }
  387. npcX = npc.getX();
  388. npcY = npc.getY();
  389. targetX = obj.getX();
  390. targetY = obj.getY();
  391. dx = npcX - targetX;
  392. dy = npcY - targetY;
  393. if (((dx * dx) + (dy * dy)) > dblAggroRange)
  394. {
  395. continue;
  396. }
  397. L2Character target = (L2Character) obj;
  398. if (autoAttackCondition(target))
  399. {
  400. potentialTarget.add(target);
  401. }
  402. }
  403. if (potentialTarget.isEmpty())
  404. {
  405. return null;
  406. }
  407. // we choose a random target
  408. int choice = Rnd.nextInt(potentialTarget.size());
  409. L2Character target = potentialTarget.get(choice);
  410. return target;
  411. }
  412. private L2ControllableMobInstance findNextGroupTarget()
  413. {
  414. return getGroupTarget().getRandomMob();
  415. }
  416. public L2ControllableMobAI(AIAccessor accessor)
  417. {
  418. super(accessor);
  419. setAlternateAI(AI_IDLE);
  420. }
  421. public int getAlternateAI()
  422. {
  423. return _alternateAI;
  424. }
  425. public void setAlternateAI(int _alternateai)
  426. {
  427. _alternateAI = _alternateai;
  428. }
  429. public void forceAttack(L2Character target)
  430. {
  431. setAlternateAI(AI_FORCEATTACK);
  432. setForcedTarget(target);
  433. }
  434. public void forceAttackGroup(MobGroup group)
  435. {
  436. setForcedTarget(null);
  437. setGroupTarget(group);
  438. setAlternateAI(AI_ATTACK_GROUP);
  439. }
  440. public void stop()
  441. {
  442. setAlternateAI(AI_IDLE);
  443. clientStopMoving(null);
  444. }
  445. public void move(int x, int y, int z)
  446. {
  447. moveTo(x, y, z);
  448. }
  449. public void follow(L2Character target)
  450. {
  451. setAlternateAI(AI_FOLLOW);
  452. setForcedTarget(target);
  453. }
  454. public boolean isThinking()
  455. {
  456. return _isThinking;
  457. }
  458. public boolean isNotMoving()
  459. {
  460. return _isNotMoving;
  461. }
  462. public void setNotMoving(boolean isNotMoving)
  463. {
  464. _isNotMoving = isNotMoving;
  465. }
  466. public void setThinking(boolean isThinking)
  467. {
  468. _isThinking = isThinking;
  469. }
  470. private L2Character getForcedTarget()
  471. {
  472. return _forcedTarget;
  473. }
  474. private MobGroup getGroupTarget()
  475. {
  476. return _targetGroup;
  477. }
  478. private void setForcedTarget(L2Character forcedTarget)
  479. {
  480. _forcedTarget = forcedTarget;
  481. }
  482. private void setGroupTarget(MobGroup targetGroup)
  483. {
  484. _targetGroup = targetGroup;
  485. }
  486. }