AbstractNpcAI.java 5.3 KB

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