AbstractNpcAI.java 5.9 KB

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