L2AttackableAIScript.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 ai.group_template;
  16. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  17. import com.l2jserver.gameserver.ai.CtrlEvent;
  18. import com.l2jserver.gameserver.ai.CtrlIntention;
  19. import com.l2jserver.gameserver.datatables.NpcTable;
  20. import com.l2jserver.gameserver.instancemanager.DimensionalRiftManager;
  21. import com.l2jserver.gameserver.model.L2Object;
  22. import com.l2jserver.gameserver.model.L2Skill;
  23. import com.l2jserver.gameserver.model.actor.L2Attackable;
  24. import com.l2jserver.gameserver.model.actor.L2Character;
  25. import com.l2jserver.gameserver.model.actor.L2Npc;
  26. import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.actor.instance.L2RiftInvaderInstance;
  29. import com.l2jserver.gameserver.model.quest.Quest;
  30. import com.l2jserver.gameserver.model.quest.jython.QuestJython;
  31. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  32. import com.l2jserver.gameserver.util.Util;
  33. /**
  34. *
  35. * Overarching Superclass for all mob AI
  36. * @author Fulminus
  37. *
  38. */
  39. public class L2AttackableAIScript extends QuestJython
  40. {
  41. /**
  42. * This is used to register all monsters contained in mobs for a particular script<BR>
  43. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method register ID for all QuestEventTypes<BR>
  44. * Do not use for group_template AIs</B></FONT><BR>
  45. * @param mobs
  46. * @see #registerMobs(int[], QuestEventType...)
  47. */
  48. public void registerMobs(int[] mobs)
  49. {
  50. for (int id : mobs)
  51. {
  52. addEventId(id, QuestEventType.ON_ATTACK);
  53. addEventId(id, QuestEventType.ON_KILL);
  54. addEventId(id, QuestEventType.ON_SPAWN);
  55. addEventId(id, QuestEventType.ON_SPELL_FINISHED);
  56. addEventId(id, QuestEventType.ON_SKILL_SEE);
  57. addEventId(id, QuestEventType.ON_FACTION_CALL);
  58. addEventId(id, QuestEventType.ON_AGGRO_RANGE_ENTER);
  59. }
  60. }
  61. /**
  62. * This is used to register all monsters contained in mobs for a particular script
  63. * event types defined in types.
  64. * @param mobs
  65. * @param types
  66. */
  67. public void registerMobs(int[] mobs, QuestEventType... types)
  68. {
  69. for (int id : mobs)
  70. {
  71. for (QuestEventType type : types)
  72. {
  73. addEventId(id, type);
  74. }
  75. }
  76. }
  77. public L2AttackableAIScript (int questId, String name, String descr)
  78. {
  79. super(questId, name, descr);
  80. }
  81. @Override
  82. public String onAdvEvent (String event, L2Npc npc, L2PcInstance player)
  83. {
  84. return null;
  85. }
  86. @Override
  87. public String onSpellFinished(L2Npc npc, L2PcInstance player, L2Skill skill)
  88. {
  89. return null;
  90. }
  91. @Override
  92. public String onSkillSee (L2Npc npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isPet)
  93. {
  94. if (caster == null)
  95. {
  96. return null;
  97. }
  98. if (!(npc instanceof L2Attackable))
  99. {
  100. return null;
  101. }
  102. L2Attackable attackable = (L2Attackable)npc;
  103. int skillAggroPoints = skill.getAggroPoints();
  104. if (caster.getPet() != null)
  105. {
  106. if (targets.length == 1 && Util.contains(targets, caster.getPet()))
  107. skillAggroPoints = 0;
  108. }
  109. if (skillAggroPoints > 0)
  110. {
  111. if ( attackable.hasAI() && (attackable.getAI().getIntention() == AI_INTENTION_ATTACK))
  112. {
  113. L2Object npcTarget = attackable.getTarget();
  114. for (L2Object skillTarget : targets)
  115. {
  116. if (npcTarget == skillTarget || npc == skillTarget)
  117. {
  118. L2Character originalCaster = isPet? caster.getPet(): caster;
  119. attackable.addDamageHate(originalCaster, 0, (skillAggroPoints*150)/(attackable.getLevel()+7));
  120. }
  121. }
  122. }
  123. }
  124. return null;
  125. }
  126. @Override
  127. public String onFactionCall (L2Npc npc, L2Npc caller, L2PcInstance attacker, boolean isPet)
  128. {
  129. if(attacker == null)
  130. return null;
  131. L2Character originalAttackTarget = (isPet? attacker.getPet(): attacker);
  132. if ( attacker.isInParty()
  133. && attacker.getParty().isInDimensionalRift())
  134. {
  135. byte riftType = attacker.getParty().getDimensionalRift().getType();
  136. byte riftRoom = attacker.getParty().getDimensionalRift().getCurrentRoom();
  137. if (caller instanceof L2RiftInvaderInstance
  138. && !DimensionalRiftManager.getInstance().getRoom(riftType, riftRoom).checkIfInZone(npc.getX(), npc.getY(), npc.getZ()))
  139. {
  140. return null;
  141. }
  142. }
  143. // By default, when a faction member calls for help, attack the caller's attacker.
  144. // Notify the AI with EVT_AGGRESSION
  145. npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, originalAttackTarget, 1);
  146. return null;
  147. }
  148. @Override
  149. public String onAggroRangeEnter (L2Npc npc, L2PcInstance player, boolean isPet)
  150. {
  151. if (player == null)
  152. return null;
  153. L2Character target = isPet ? player.getPet() : player;
  154. ((L2Attackable) npc).addDamageHate(target, 0, 1);
  155. // Set the intention to the L2Attackable to AI_INTENTION_ACTIVE
  156. if (npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE)
  157. npc.getAI().setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  158. return null;
  159. }
  160. @Override
  161. public String onSpawn (L2Npc npc)
  162. {
  163. return null;
  164. }
  165. @Override
  166. public String onAttack (L2Npc npc, L2PcInstance attacker, int damage, boolean isPet)
  167. {
  168. if ((attacker != null) && (npc instanceof L2Attackable))
  169. {
  170. L2Attackable attackable = (L2Attackable)npc;
  171. L2Character originalAttacker = isPet? attacker.getPet(): attacker;
  172. attackable.getAI().notifyEvent(CtrlEvent.EVT_ATTACKED, originalAttacker);
  173. attackable.addDamageHate(originalAttacker, damage, (damage*100)/(attackable.getLevel()+7));
  174. }
  175. return null;
  176. }
  177. @Override
  178. public String onKill (L2Npc npc, L2PcInstance killer, boolean isPet)
  179. {
  180. if (npc instanceof L2MonsterInstance)
  181. {
  182. final L2MonsterInstance mob = (L2MonsterInstance)npc;
  183. if (mob.getLeader() != null)
  184. mob.getLeader().getMinionList().onMinionDie(mob, -1);
  185. if (mob.hasMinions())
  186. mob.getMinionList().onMasterDie(false);
  187. }
  188. return null;
  189. }
  190. public static void main(String[] args)
  191. {
  192. L2AttackableAIScript ai = new L2AttackableAIScript(-1,"L2AttackableAIScript","L2AttackableAIScript");
  193. // register all mobs here...
  194. for (int level =1; level<100; level++)
  195. {
  196. L2NpcTemplate[] templates = NpcTable.getInstance().getAllOfLevel(level);
  197. if ((templates != null) && (templates.length > 0))
  198. {
  199. for (L2NpcTemplate t: templates)
  200. {
  201. try
  202. {
  203. if ( L2Attackable.class.isAssignableFrom(Class.forName("com.l2jserver.gameserver.model.actor.instance."+t.type+"Instance")))
  204. {
  205. ai.addEventId(t.npcId, Quest.QuestEventType.ON_ATTACK);
  206. ai.addEventId(t.npcId, Quest.QuestEventType.ON_KILL);
  207. ai.addEventId(t.npcId, Quest.QuestEventType.ON_SPAWN);
  208. ai.addEventId(t.npcId, Quest.QuestEventType.ON_SKILL_SEE);
  209. ai.addEventId(t.npcId, Quest.QuestEventType.ON_FACTION_CALL);
  210. ai.addEventId(t.npcId, Quest.QuestEventType.ON_AGGRO_RANGE_ENTER);
  211. }
  212. }
  213. catch(ClassNotFoundException ex)
  214. {
  215. _log.info("Class not found "+t.type+"Instance");
  216. }
  217. }
  218. }
  219. }
  220. }
  221. }