L2SiegeGuardAI.java 30 KB

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