L2SiegeGuardAI.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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 static net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  23. import java.util.concurrent.Future;
  24. import net.sf.l2j.Config;
  25. import net.sf.l2j.gameserver.GameTimeController;
  26. import net.sf.l2j.gameserver.GeoData;
  27. import net.sf.l2j.gameserver.ThreadPoolManager;
  28. import net.sf.l2j.gameserver.model.L2Attackable;
  29. import net.sf.l2j.gameserver.model.L2Character;
  30. import net.sf.l2j.gameserver.model.L2Effect;
  31. import net.sf.l2j.gameserver.model.L2Object;
  32. import net.sf.l2j.gameserver.model.L2Skill;
  33. import net.sf.l2j.gameserver.model.L2Summon;
  34. import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
  35. import net.sf.l2j.gameserver.model.actor.instance.L2FolkInstance;
  36. import net.sf.l2j.gameserver.model.actor.instance.L2MonsterInstance;
  37. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  38. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  39. import net.sf.l2j.gameserver.model.actor.instance.L2SiegeGuardInstance;
  40. import net.sf.l2j.util.Rnd;
  41. /**
  42. * This class manages AI of L2Attackable.<BR><BR>
  43. *
  44. */
  45. public class L2SiegeGuardAI extends L2CharacterAI implements Runnable
  46. {
  47. //protected static final Logger _log = Logger.getLogger(L2SiegeGuardAI.class.getName());
  48. private static final int MAX_ATTACK_TIMEOUT = 300; // int ticks, i.e. 30 seconds
  49. /** The L2Attackable AI task executed every 1s (call onEvtThink method)*/
  50. private Future<?> _aiTask;
  51. /** The delay after wich the attacked is stopped */
  52. private int _attackTimeout;
  53. /** The L2Attackable aggro counter */
  54. private int _globalAggro;
  55. /** The flag used to indicate that a thinking action is in progress */
  56. private boolean _thinking; // to prevent recursive thinking
  57. private int _attackRange;
  58. /**
  59. * Constructor of L2AttackableAI.<BR><BR>
  60. *
  61. * @param accessor The AI accessor of the L2Character
  62. *
  63. */
  64. public L2SiegeGuardAI(L2Character.AIAccessor accessor)
  65. {
  66. super(accessor);
  67. _attackTimeout = Integer.MAX_VALUE;
  68. _globalAggro = -10; // 10 seconds timeout of ATTACK after respawn
  69. _attackRange = ((L2Attackable) _actor).getPhysicalAttackRange();
  70. }
  71. public void run()
  72. {
  73. // Launch actions corresponding to the Event Think
  74. onEvtThink();
  75. }
  76. /**
  77. * Return True if the target is autoattackable (depends on the actor type).<BR><BR>
  78. *
  79. * <B><U> Actor is a L2GuardInstance</U> :</B><BR><BR>
  80. * <li>The target isn't a Folk or a Door</li>
  81. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  82. * <li>The target is in the actor Aggro range and is at the same height</li>
  83. * <li>The L2PcInstance target has karma (=PK)</li>
  84. * <li>The L2MonsterInstance target is aggressive</li><BR><BR>
  85. *
  86. * <B><U> Actor is a L2SiegeGuardInstance</U> :</B><BR><BR>
  87. * <li>The target isn't a Folk or a Door</li>
  88. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  89. * <li>The target is in the actor Aggro range and is at the same height</li>
  90. * <li>A siege is in progress</li>
  91. * <li>The L2PcInstance target isn't a Defender</li><BR><BR>
  92. *
  93. * <B><U> Actor is a L2FriendlyMobInstance</U> :</B><BR><BR>
  94. * <li>The target isn't a Folk, a Door or another L2NpcInstance</li>
  95. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  96. * <li>The target is in the actor Aggro range and is at the same height</li>
  97. * <li>The L2PcInstance target has karma (=PK)</li><BR><BR>
  98. *
  99. * <B><U> Actor is a L2MonsterInstance</U> :</B><BR><BR>
  100. * <li>The target isn't a Folk, a Door or another L2NpcInstance</li>
  101. * <li>The target isn't dead, isn't invulnerable, isn't in silent moving mode AND too far (>100)</li>
  102. * <li>The target is in the actor Aggro range and is at the same height</li>
  103. * <li>The actor is Aggressive</li><BR><BR>
  104. *
  105. * @param target The targeted L2Object
  106. *
  107. */
  108. private boolean autoAttackCondition(L2Character target)
  109. {
  110. // Check if the target isn't another guard, folk or a door
  111. if (target == null ||
  112. target instanceof L2SiegeGuardInstance ||
  113. target instanceof L2FolkInstance ||
  114. target instanceof L2DoorInstance ||
  115. target.isAlikeDead() ||
  116. target.isInvul()) return false;
  117. // Get the owner if the target is a summon
  118. if (target instanceof L2Summon)
  119. {
  120. L2PcInstance owner = ((L2Summon)target).getOwner();
  121. if (_actor.isInsideRadius(owner, 1000, true, false))
  122. target = owner;
  123. }
  124. // Check if the target is a L2PcInstance
  125. if (target instanceof L2PcInstance)
  126. {
  127. // Check if the target isn't in silent move mode AND too far (>100)
  128. if (((L2PcInstance) target).isSilentMoving()
  129. && !_actor.isInsideRadius(target, 250, false, false)) return false;
  130. }
  131. // Los Check Here
  132. return (_actor.isAutoAttackable(target) && GeoData.getInstance().canSeeTarget(_actor, target));
  133. }
  134. /**
  135. * Set the Intention of this L2CharacterAI and create an AI Task executed every 1s (call onEvtThink method) for this L2Attackable.<BR><BR>
  136. *
  137. * <FONT COLOR=#FF0000><B> <U>Caution</U> : If actor _knowPlayer isn't EMPTY, AI_INTENTION_IDLE will be change in AI_INTENTION_ACTIVE</B></FONT><BR><BR>
  138. *
  139. * @param intention The new Intention to set to the AI
  140. * @param arg0 The first parameter of the Intention
  141. * @param arg1 The second parameter of the Intention
  142. *
  143. */
  144. @Override
  145. synchronized void changeIntention(CtrlIntention intention, Object arg0, Object arg1)
  146. {
  147. if (Config.DEBUG)
  148. _log.info("L2SiegeAI.changeIntention(" + intention + ", " + arg0 + ", " + arg1 + ")");
  149. ((L2Attackable)_actor).setisReturningToSpawnPoint(false);
  150. if (intention == AI_INTENTION_IDLE /*|| intention == AI_INTENTION_ACTIVE*/) // active becomes idle if only a summon is present
  151. {
  152. // Check if actor is not dead
  153. if (!_actor.isAlikeDead())
  154. {
  155. L2Attackable npc = (L2Attackable) _actor;
  156. // If its _knownPlayer isn't empty set the Intention to AI_INTENTION_ACTIVE
  157. if (npc.getKnownList().getKnownPlayers().size() > 0) intention = AI_INTENTION_ACTIVE;
  158. else intention = AI_INTENTION_IDLE;
  159. }
  160. if (intention == AI_INTENTION_IDLE)
  161. {
  162. // Set the Intention of this L2AttackableAI to AI_INTENTION_IDLE
  163. super.changeIntention(AI_INTENTION_IDLE, null, null);
  164. // Stop AI task and detach AI from NPC
  165. if (_aiTask != null)
  166. {
  167. _aiTask.cancel(true);
  168. _aiTask = null;
  169. }
  170. // Cancel the AI
  171. _accessor.detachAI();
  172. return;
  173. }
  174. }
  175. // Set the Intention of this L2AttackableAI to intention
  176. super.changeIntention(intention, arg0, arg1);
  177. // If not idle - create an AI task (schedule onEvtThink repeatedly)
  178. if (_aiTask == null)
  179. {
  180. _aiTask = ThreadPoolManager.getInstance().scheduleAiAtFixedRate(this, 1000, 1000);
  181. }
  182. }
  183. /**
  184. * Manage the Attack Intention : Stop current Attack (if necessary), Calculate attack timeout, Start a new Attack and Launch Think Event.<BR><BR>
  185. *
  186. * @param target The L2Character to attack
  187. *
  188. */
  189. @Override
  190. protected void onIntentionAttack(L2Character target)
  191. {
  192. // Calculate the attack timeout
  193. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  194. // Manage the Attack Intention : Stop current Attack (if necessary), Start a new Attack and Launch Think Event
  195. //if (_actor.getTarget() != null)
  196. super.onIntentionAttack(target);
  197. }
  198. /**
  199. * Manage AI standard thinks of a L2Attackable (called by onEvtThink).<BR><BR>
  200. *
  201. * <B><U> Actions</U> :</B><BR><BR>
  202. * <li>Update every 1s the _globalAggro counter to come close to 0</li>
  203. * <li>If the actor is Aggressive and can attack, add all autoAttackable L2Character in its Aggro Range to its _aggroList, chose a target and order to attack it</li>
  204. * <li>If the actor can't attack, order to it to return to its home location</li>
  205. *
  206. */
  207. private void thinkActive()
  208. {
  209. L2Attackable npc = (L2Attackable) _actor;
  210. // Update every 1s the _globalAggro counter to come close to 0
  211. if (_globalAggro != 0)
  212. {
  213. if (_globalAggro < 0) _globalAggro++;
  214. else _globalAggro--;
  215. }
  216. // Add all autoAttackable L2Character in L2Attackable Aggro Range to its _aggroList with 0 damage and 1 hate
  217. // A L2Attackable isn't aggressive during 10s after its spawn because _globalAggro is set to -10
  218. if (_globalAggro >= 0)
  219. {
  220. for (L2Character target : npc.getKnownList().getKnownCharactersInRadius(_attackRange))
  221. {
  222. if (target == null) continue;
  223. if (autoAttackCondition(target)) // check aggression
  224. {
  225. // Get the hate level of the L2Attackable against this L2Character target contained in _aggroList
  226. int hating = npc.getHating(target);
  227. // Add the attacker to the L2Attackable _aggroList with 0 damage and 1 hate
  228. if (hating == 0) npc.addDamageHate(target, 0, 1);
  229. }
  230. }
  231. // Chose a target from its aggroList
  232. L2Character hated;
  233. if (_actor.isConfused()) hated = _attackTarget; // Force mobs to attak anybody if confused
  234. else hated = npc.getMostHated();
  235. // Order to the L2Attackable to attack the target
  236. if (hated != null)
  237. {
  238. // Get the hate level of the L2Attackable against this L2Character target contained in _aggroList
  239. int aggro = npc.getHating(hated);
  240. if (aggro + _globalAggro > 0)
  241. {
  242. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  243. if (!_actor.isRunning()) _actor.setRunning();
  244. // Set the AI Intention to AI_INTENTION_ATTACK
  245. setIntention(CtrlIntention.AI_INTENTION_ATTACK, hated, null);
  246. }
  247. return;
  248. }
  249. }
  250. // Order to the L2SiegeGuardInstance to return to its home location because there's no target to attack
  251. ((L2SiegeGuardInstance) _actor).returnHome();
  252. return;
  253. }
  254. private void attackPrepare()
  255. {
  256. // Get all information needed to chose between physical or magical attack
  257. L2Skill[] skills = null;
  258. double dist_2 = 0;
  259. int range = 0;
  260. L2SiegeGuardInstance sGuard = (L2SiegeGuardInstance) _actor;
  261. try
  262. {
  263. _actor.setTarget(_attackTarget);
  264. skills = _actor.getAllSkills();
  265. dist_2 = _actor.getPlanDistanceSq(_attackTarget.getX(), _attackTarget.getY());
  266. range = _actor.getPhysicalAttackRange() + _actor.getTemplate().collisionRadius + _attackTarget.getTemplate().collisionRadius;
  267. }
  268. catch (NullPointerException e)
  269. {
  270. //_log.warning("AttackableAI: Attack target is NULL.");
  271. _actor.setTarget(null);
  272. setIntention(AI_INTENTION_IDLE, null, null);
  273. return;
  274. }
  275. // never attack defenders
  276. if(_attackTarget instanceof L2PcInstance && sGuard.getCastle().getSiege().checkIsDefender(((L2PcInstance)_attackTarget).getClan()))
  277. {
  278. // Cancel the target
  279. sGuard.stopHating(_attackTarget);
  280. _actor.setTarget(null);
  281. setIntention(AI_INTENTION_IDLE, null, null);
  282. return;
  283. }
  284. if (!GeoData.getInstance().canSeeTarget(_actor, _attackTarget))
  285. {
  286. // Siege guards differ from normal mobs currently:
  287. // If target cannot seen, don't attack any more
  288. sGuard.stopHating(_attackTarget);
  289. _actor.setTarget(null);
  290. setIntention(AI_INTENTION_IDLE, null, null);
  291. return;
  292. }
  293. // Check if the actor isn't muted and if it is far from target
  294. if (!_actor.isMuted() && dist_2 > (range + 20) * (range + 20))
  295. {
  296. // check for long ranged skills and heal/buff skills
  297. if (!Config.ALT_GAME_MOB_ATTACK_AI
  298. || (_actor instanceof L2MonsterInstance && Rnd.nextInt(100) <= 5))
  299. for (L2Skill sk : skills)
  300. {
  301. int castRange = sk.getCastRange();
  302. if (((sk.getSkillType() == L2Skill.SkillType.BUFF || sk.getSkillType() == L2Skill.SkillType.HEAL) || (dist_2 >= castRange * castRange / 9)
  303. && (dist_2 <= castRange * castRange) && (castRange > 70))
  304. && !_actor.isSkillDisabled(sk.getId())
  305. && _actor.getCurrentMp() >= _actor.getStat().getMpConsume(sk) && !sk.isPassive())
  306. {
  307. L2Object OldTarget = _actor.getTarget();
  308. if (sk.getSkillType() == L2Skill.SkillType.BUFF
  309. || sk.getSkillType() == L2Skill.SkillType.HEAL)
  310. {
  311. boolean useSkillSelf = true;
  312. if (sk.getSkillType() == L2Skill.SkillType.HEAL
  313. && _actor.getCurrentHp() > (int) (_actor.getMaxHp() / 1.5))
  314. {
  315. useSkillSelf = false;
  316. break;
  317. }
  318. if (sk.getSkillType() == L2Skill.SkillType.BUFF)
  319. {
  320. L2Effect[] effects = _actor.getAllEffects();
  321. for (int i = 0; effects != null && i < effects.length; i++)
  322. {
  323. L2Effect effect = effects[i];
  324. if (effect.getSkill() == sk)
  325. {
  326. useSkillSelf = false;
  327. break;
  328. }
  329. }
  330. }
  331. if (useSkillSelf) _actor.setTarget(_actor);
  332. }
  333. clientStopMoving(null);
  334. _accessor.doCast(sk);
  335. _actor.setTarget(OldTarget);
  336. return;
  337. }
  338. }
  339. // Check if the L2SiegeGuardInstance is attacking, knows the target and can't run
  340. if (!(_actor.isAttackingNow()) && (_actor.getRunSpeed() == 0)
  341. && (_actor.getKnownList().knowsObject(_attackTarget)))
  342. {
  343. // Cancel the target
  344. _actor.getKnownList().removeKnownObject(_attackTarget);
  345. _actor.setTarget(null);
  346. setIntention(AI_INTENTION_IDLE, null, null);
  347. }
  348. else
  349. {
  350. double dx = _actor.getX() - _attackTarget.getX();
  351. double dy = _actor.getY() - _attackTarget.getY();
  352. double dz = _actor.getZ() - _attackTarget.getZ();
  353. double homeX = _attackTarget.getX() - sGuard.getHomeX();
  354. double homeY = _attackTarget.getY() - sGuard.getHomeY();
  355. // Check if the L2SiegeGuardInstance isn't too far from it's home location
  356. if ((dx * dx + dy * dy > 10000) && (homeX * homeX + homeY * homeY > 3240000) // 1800 * 1800
  357. && (_actor.getKnownList().knowsObject(_attackTarget)))
  358. {
  359. // Cancel the target
  360. _actor.getKnownList().removeKnownObject(_attackTarget);
  361. _actor.setTarget(null);
  362. setIntention(AI_INTENTION_IDLE, null, null);
  363. }
  364. else // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  365. {
  366. // Temporary hack for preventing guards jumping off towers,
  367. // before replacing this with effective geodata checks and AI modification
  368. if(dz*dz < 170*170) // normally 130 if guard z coordinates correct
  369. moveToPawn(_attackTarget, range);
  370. }
  371. }
  372. return;
  373. }
  374. // Else, if the actor is muted and far from target, just "move to pawn"
  375. else if (_actor.isMuted() && dist_2 > (range + 20) * (range + 20))
  376. {
  377. // Temporary hack for preventing guards jumping off towers,
  378. // before replacing this with effective geodata checks and AI modification
  379. double dz = _actor.getZ() - _attackTarget.getZ();
  380. if(dz*dz < 170*170) // normally 130 if guard z coordinates correct
  381. moveToPawn(_attackTarget, range);
  382. return;
  383. }
  384. // Else, if this is close enough to attack
  385. else if (dist_2 <= (range + 20) * (range + 20))
  386. {
  387. // Force mobs to attak anybody if confused
  388. L2Character hated = null;
  389. if (_actor.isConfused()) hated = _attackTarget;
  390. else hated = ((L2Attackable) _actor).getMostHated();
  391. if (hated == null)
  392. {
  393. setIntention(AI_INTENTION_ACTIVE, null, null);
  394. return;
  395. }
  396. if (hated != _attackTarget) _attackTarget = hated;
  397. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  398. // check for close combat skills && heal/buff skills
  399. if (!_actor.isMuted() && Rnd.nextInt(100) <= 5)
  400. {
  401. for (L2Skill sk : skills)
  402. {
  403. int castRange = sk.getCastRange();
  404. if (castRange * castRange >= dist_2 && castRange <= 70 && !sk.isPassive()
  405. && _actor.getCurrentMp() >= _actor.getStat().getMpConsume(sk)
  406. && !_actor.isSkillDisabled(sk.getId()))
  407. {
  408. L2Object OldTarget = _actor.getTarget();
  409. if (sk.getSkillType() == L2Skill.SkillType.BUFF
  410. || sk.getSkillType() == L2Skill.SkillType.HEAL)
  411. {
  412. boolean useSkillSelf = true;
  413. if (sk.getSkillType() == L2Skill.SkillType.HEAL
  414. && _actor.getCurrentHp() > (int) (_actor.getMaxHp() / 1.5))
  415. {
  416. useSkillSelf = false;
  417. break;
  418. }
  419. if (sk.getSkillType() == L2Skill.SkillType.BUFF)
  420. {
  421. L2Effect[] effects = _actor.getAllEffects();
  422. for (int i = 0; effects != null && i < effects.length; i++)
  423. {
  424. L2Effect effect = effects[i];
  425. if (effect.getSkill() == sk)
  426. {
  427. useSkillSelf = false;
  428. break;
  429. }
  430. }
  431. }
  432. if (useSkillSelf) _actor.setTarget(_actor);
  433. }
  434. clientStopMoving(null);
  435. _accessor.doCast(sk);
  436. _actor.setTarget(OldTarget);
  437. return;
  438. }
  439. }
  440. }
  441. // Finally, do the physical attack itself
  442. _accessor.doAttack(_attackTarget);
  443. }
  444. }
  445. /**
  446. * Manage AI attack thinks of a L2Attackable (called by onEvtThink).<BR><BR>
  447. *
  448. * <B><U> Actions</U> :</B><BR><BR>
  449. * <li>Update the attack timeout if actor is running</li>
  450. * <li>If target is dead or timeout is expired, stop this attack and set the Intention to AI_INTENTION_ACTIVE</li>
  451. * <li>Call all L2Object of its Faction inside the Faction Range</li>
  452. * <li>Chose a target and order to attack it with magic skill or physical attack</li><BR><BR>
  453. *
  454. * TODO: Manage casting rules to healer mobs (like Ant Nurses)
  455. *
  456. */
  457. private void thinkAttack()
  458. {
  459. if (Config.DEBUG)
  460. _log.info("L2SiegeGuardAI.thinkAttack(); timeout="
  461. + (_attackTimeout - GameTimeController.getGameTicks()));
  462. if (_attackTimeout < GameTimeController.getGameTicks())
  463. {
  464. // Check if the actor is running
  465. if (_actor.isRunning())
  466. {
  467. // Set the actor movement type to walk and send Server->Client packet ChangeMoveType to all others L2PcInstance
  468. _actor.setWalking();
  469. // Calculate a new attack timeout
  470. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  471. }
  472. }
  473. // Check if target is dead or if timeout is expired to stop this attack
  474. if (_attackTarget == null || _attackTarget.isAlikeDead()
  475. || _attackTimeout < GameTimeController.getGameTicks())
  476. {
  477. // Stop hating this target after the attack timeout or if target is dead
  478. if (_attackTarget != null)
  479. {
  480. L2Attackable npc = (L2Attackable) _actor;
  481. npc.stopHating(_attackTarget);
  482. }
  483. // Cancel target and timeout
  484. _attackTimeout = Integer.MAX_VALUE;
  485. _attackTarget = null;
  486. // Set the AI Intention to AI_INTENTION_ACTIVE
  487. setIntention(AI_INTENTION_ACTIVE, null, null);
  488. _actor.setWalking();
  489. return;
  490. }
  491. attackPrepare();
  492. factionNotify();
  493. }
  494. private final void factionNotify()
  495. {
  496. // Call all L2Object of its Faction inside the Faction Range
  497. if (((L2NpcInstance) _actor).getFactionId() == null || _attackTarget == null || _actor == null)
  498. return;
  499. if (_attackTarget.isInvul()) return;
  500. String faction_id = ((L2NpcInstance) _actor).getFactionId();
  501. // Go through all L2Object that belong to its faction
  502. for (L2Character cha : _actor.getKnownList().getKnownCharactersInRadius(1000))
  503. {
  504. if (cha == null) continue;
  505. if (!(cha instanceof L2NpcInstance)) continue;
  506. L2NpcInstance npc = (L2NpcInstance) cha;
  507. if (faction_id != npc.getFactionId()) continue;
  508. // Check if the L2Object is inside the Faction Range of the actor
  509. if ((npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE || npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE)
  510. && _actor.isInsideRadius(npc, npc.getFactionRange(), false, true)
  511. && npc.getTarget() == null
  512. && _attackTarget.isInsideRadius(npc, npc.getFactionRange(), false, true)
  513. )
  514. {
  515. if (Config.GEODATA > 0)
  516. {
  517. if (GeoData.getInstance().canSeeTarget(npc, _attackTarget))
  518. // Notify the L2Object AI with EVT_AGGRESSION
  519. npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, _attackTarget, 1);
  520. }
  521. else
  522. {
  523. if (Math.abs(_attackTarget.getZ() - npc.getZ()) < 600)
  524. // Notify the L2Object AI with EVT_AGGRESSION
  525. npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, _attackTarget, 1);
  526. }
  527. }
  528. }
  529. }
  530. /**
  531. * Manage AI thinking actions of a L2Attackable.<BR><BR>
  532. */
  533. @Override
  534. protected void onEvtThink()
  535. {
  536. // if(getIntention() != AI_INTENTION_IDLE && (!_actor.isVisible() || !_actor.hasAI() || !_actor.isKnownPlayers()))
  537. // setIntention(AI_INTENTION_IDLE);
  538. // Check if the actor can't use skills and if a thinking action isn't already in progress
  539. if (_thinking || _actor.isAllSkillsDisabled()) return;
  540. // Start thinking action
  541. _thinking = true;
  542. try
  543. {
  544. // Manage AI thinks of a L2Attackable
  545. if (getIntention() == AI_INTENTION_ACTIVE) thinkActive();
  546. else if (getIntention() == AI_INTENTION_ATTACK) thinkAttack();
  547. }
  548. finally
  549. {
  550. // Stop thinking action
  551. _thinking = false;
  552. }
  553. }
  554. /**
  555. * Launch actions corresponding to the Event Attacked.<BR><BR>
  556. *
  557. * <B><U> Actions</U> :</B><BR><BR>
  558. * <li>Init the attack : Calculate the attack timeout, Set the _globalAggro to 0, Add the attacker to the actor _aggroList</li>
  559. * <li>Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance</li>
  560. * <li>Set the Intention to AI_INTENTION_ATTACK</li><BR><BR>
  561. *
  562. * @param attacker The L2Character that attacks the actor
  563. *
  564. */
  565. @Override
  566. protected void onEvtAttacked(L2Character attacker)
  567. {
  568. // Calculate the attack timeout
  569. _attackTimeout = MAX_ATTACK_TIMEOUT + GameTimeController.getGameTicks();
  570. // Set the _globalAggro to 0 to permit attack even just after spawn
  571. if (_globalAggro < 0) _globalAggro = 0;
  572. // Add the attacker to the _aggroList of the actor
  573. ((L2Attackable) _actor).addDamageHate(attacker, 0, 1);
  574. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  575. if (!_actor.isRunning()) _actor.setRunning();
  576. // Set the Intention to AI_INTENTION_ATTACK
  577. if (getIntention() != AI_INTENTION_ATTACK)
  578. {
  579. setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker, null);
  580. }
  581. super.onEvtAttacked(attacker);
  582. }
  583. /**
  584. * Launch actions corresponding to the Event Aggression.<BR><BR>
  585. *
  586. * <B><U> Actions</U> :</B><BR><BR>
  587. * <li>Add the target to the actor _aggroList or update hate if already present </li>
  588. * <li>Set the actor Intention to AI_INTENTION_ATTACK (if actor is L2GuardInstance check if it isn't too far from its home location)</li><BR><BR>
  589. *
  590. * @param attacker The L2Character that attacks
  591. * @param aggro The value of hate to add to the actor against the target
  592. *
  593. */
  594. @Override
  595. protected void onEvtAggression(L2Character target, int aggro)
  596. {
  597. if (_actor == null) return;
  598. L2Attackable me = (L2Attackable) _actor;
  599. if (target != null)
  600. {
  601. // Add the target to the actor _aggroList or update hate if already present
  602. me.addDamageHate(target, 0, aggro);
  603. // Get the hate of the actor against the target
  604. aggro = me.getHating(target);
  605. if (aggro <= 0)
  606. {
  607. if (me.getMostHated() == null)
  608. {
  609. _globalAggro = -25;
  610. me.clearAggroList();
  611. setIntention(AI_INTENTION_IDLE, null, null);
  612. }
  613. return;
  614. }
  615. // Set the actor AI Intention to AI_INTENTION_ATTACK
  616. if (getIntention() != CtrlIntention.AI_INTENTION_ATTACK)
  617. {
  618. // Set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  619. if (!_actor.isRunning()) _actor.setRunning();
  620. L2SiegeGuardInstance sGuard = (L2SiegeGuardInstance) _actor;
  621. double homeX = target.getX() - sGuard.getHomeX();
  622. double homeY = target.getY() - sGuard.getHomeY();
  623. // Check if the L2SiegeGuardInstance is not too far from its home location
  624. if (homeX * homeX + homeY * homeY < 3240000) // 1800 * 1800
  625. setIntention(CtrlIntention.AI_INTENTION_ATTACK, target, null);
  626. }
  627. }
  628. else
  629. {
  630. // currently only for setting lower general aggro
  631. if(aggro >= 0) return;
  632. L2Character mostHated = me.getMostHated();
  633. if (mostHated == null)
  634. {
  635. _globalAggro = -25;
  636. return;
  637. }
  638. else
  639. for(L2Character aggroed : me.getAggroListRP().keySet())
  640. me.addDamageHate(aggroed, 0, aggro);
  641. aggro = me.getHating(mostHated);
  642. if (aggro <= 0)
  643. {
  644. _globalAggro = -25;
  645. me.clearAggroList();
  646. setIntention(AI_INTENTION_IDLE, null, null);
  647. }
  648. }
  649. }
  650. @Override
  651. protected void onEvtDead()
  652. {
  653. stopAITask();
  654. super.onEvtDead();
  655. }
  656. public void stopAITask()
  657. {
  658. if (_aiTask != null)
  659. {
  660. _aiTask.cancel(false);
  661. _aiTask = null;
  662. }
  663. _accessor.detachAI();
  664. }
  665. }