AbstractNpcAI.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (C) 2004-2015 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack 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 DataPack 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 ai.npc;
  20. import com.l2jserver.gameserver.model.actor.L2Character;
  21. import com.l2jserver.gameserver.model.actor.L2Npc;
  22. import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
  23. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  24. import com.l2jserver.gameserver.model.holders.MinionHolder;
  25. import com.l2jserver.gameserver.model.quest.Quest;
  26. import com.l2jserver.gameserver.network.NpcStringId;
  27. import com.l2jserver.gameserver.network.serverpackets.NpcSay;
  28. import com.l2jserver.gameserver.network.serverpackets.SocialAction;
  29. import com.l2jserver.gameserver.util.Broadcast;
  30. /**
  31. * Abstract NPC AI class for datapack based AIs.
  32. * @author UnAfraid, Zoey76
  33. */
  34. public abstract class AbstractNpcAI extends Quest
  35. {
  36. public AbstractNpcAI(String name, String descr)
  37. {
  38. super(-1, name, descr);
  39. }
  40. /**
  41. * Simple on first talk event handler.
  42. */
  43. @Override
  44. public String onFirstTalk(L2Npc npc, L2PcInstance player)
  45. {
  46. return npc.getId() + ".html";
  47. }
  48. /**
  49. * Registers the following events to the current script:<br>
  50. * <ul>
  51. * <li>ON_ATTACK</li>
  52. * <li>ON_KILL</li>
  53. * <li>ON_SPAWN</li>
  54. * <li>ON_SPELL_FINISHED</li>
  55. * <li>ON_SKILL_SEE</li>
  56. * <li>ON_FACTION_CALL</li>
  57. * <li>ON_AGGR_RANGE_ENTER</li>
  58. * </ul>
  59. * @param mobs
  60. */
  61. public void registerMobs(int... mobs)
  62. {
  63. addAttackId(mobs);
  64. addKillId(mobs);
  65. addSpawnId(mobs);
  66. addSpellFinishedId(mobs);
  67. addSkillSeeId(mobs);
  68. addAggroRangeEnterId(mobs);
  69. addFactionCallId(mobs);
  70. }
  71. /**
  72. * Broadcasts NpcSay packet to all known players with custom string.
  73. * @param npc
  74. * @param type
  75. * @param text
  76. */
  77. protected void broadcastNpcSay(L2Npc npc, int type, String text)
  78. {
  79. Broadcast.toKnownPlayers(npc, new NpcSay(npc.getObjectId(), type, npc.getTemplate().getDisplayId(), text));
  80. }
  81. /**
  82. * Broadcasts NpcSay packet to all known players with npc string id.
  83. * @param npc
  84. * @param type
  85. * @param stringId
  86. */
  87. protected void broadcastNpcSay(L2Npc npc, int type, NpcStringId stringId)
  88. {
  89. Broadcast.toKnownPlayers(npc, new NpcSay(npc.getObjectId(), type, npc.getTemplate().getDisplayId(), stringId));
  90. }
  91. /**
  92. * Broadcasts NpcSay packet to all known players with npc string id.
  93. * @param npc
  94. * @param type
  95. * @param stringId
  96. * @param parameters
  97. */
  98. protected void broadcastNpcSay(L2Npc npc, int type, NpcStringId stringId, String... parameters)
  99. {
  100. final NpcSay say = new NpcSay(npc.getObjectId(), type, npc.getTemplate().getDisplayId(), stringId);
  101. if (parameters != null)
  102. {
  103. for (String parameter : parameters)
  104. {
  105. say.addStringParameter(parameter);
  106. }
  107. }
  108. Broadcast.toKnownPlayers(npc, say);
  109. }
  110. /**
  111. * Broadcasts NpcSay packet to all known players with custom string in specific radius.
  112. * @param npc
  113. * @param type
  114. * @param text
  115. * @param radius
  116. */
  117. protected void broadcastNpcSay(L2Npc npc, int type, String text, int radius)
  118. {
  119. Broadcast.toKnownPlayersInRadius(npc, new NpcSay(npc.getObjectId(), type, npc.getTemplate().getDisplayId(), text), radius);
  120. }
  121. /**
  122. * Broadcasts NpcSay packet to all known players with npc string id in specific radius.
  123. * @param npc
  124. * @param type
  125. * @param stringId
  126. * @param radius
  127. */
  128. protected void broadcastNpcSay(L2Npc npc, int type, NpcStringId stringId, int radius)
  129. {
  130. Broadcast.toKnownPlayersInRadius(npc, new NpcSay(npc.getObjectId(), type, npc.getTemplate().getDisplayId(), stringId), radius);
  131. }
  132. /**
  133. * Broadcasts SocialAction packet to self and known players.
  134. * @param character
  135. * @param actionId
  136. */
  137. protected void broadcastSocialAction(L2Character character, int actionId)
  138. {
  139. Broadcast.toSelfAndKnownPlayers(character, new SocialAction(character.getObjectId(), actionId));
  140. }
  141. /**
  142. * Broadcasts SocialAction packet to self and known players in specific radius.
  143. * @param character
  144. * @param actionId
  145. * @param radius
  146. */
  147. protected void broadcastSocialAction(L2Character character, int actionId, int radius)
  148. {
  149. Broadcast.toSelfAndKnownPlayersInRadius(character, new SocialAction(character.getObjectId(), actionId), radius);
  150. }
  151. public void spawnMinions(final L2Npc npc, final String spawnName)
  152. {
  153. for (MinionHolder is : npc.getTemplate().getParameters().getMinionList(spawnName))
  154. {
  155. addMinion((L2MonsterInstance) npc, is.getId());
  156. }
  157. }
  158. }