AbstractNpcAI.java 5.2 KB

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