AbstractNpcAI.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.npc;
  16. import java.util.logging.Logger;
  17. import com.l2jserver.gameserver.ai.CtrlIntention;
  18. import com.l2jserver.gameserver.model.actor.L2Attackable;
  19. import com.l2jserver.gameserver.model.actor.L2Character;
  20. import com.l2jserver.gameserver.model.actor.L2Npc;
  21. import com.l2jserver.gameserver.model.actor.L2Playable;
  22. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  23. import com.l2jserver.gameserver.model.holders.ItemHolder;
  24. import com.l2jserver.gameserver.network.NpcStringId;
  25. import com.l2jserver.gameserver.network.serverpackets.NpcSay;
  26. import com.l2jserver.gameserver.network.serverpackets.SocialAction;
  27. import com.l2jserver.gameserver.scripting.scriptengine.impl.L2Script;
  28. import com.l2jserver.gameserver.util.Broadcast;
  29. /**
  30. * Abstract NPC AI class for datapack based AIs.
  31. * @author UnAfraid, Zoey76
  32. */
  33. public abstract class AbstractNpcAI extends L2Script
  34. {
  35. public Logger _log = Logger.getLogger(getClass().getSimpleName());
  36. /**
  37. * Simple on first talk event handler.
  38. */
  39. @Override
  40. public String onFirstTalk(L2Npc npc, L2PcInstance player)
  41. {
  42. return npc.getNpcId() + ".html";
  43. }
  44. public AbstractNpcAI(String name, String descr)
  45. {
  46. super(-1, name, descr);
  47. }
  48. /**
  49. * Registers the fallowing 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. for (int id : mobs)
  64. {
  65. addEventId(id, QuestEventType.ON_ATTACK);
  66. addEventId(id, QuestEventType.ON_KILL);
  67. addEventId(id, QuestEventType.ON_SPAWN);
  68. addEventId(id, QuestEventType.ON_SPELL_FINISHED);
  69. addEventId(id, QuestEventType.ON_SKILL_SEE);
  70. addEventId(id, QuestEventType.ON_FACTION_CALL);
  71. addEventId(id, QuestEventType.ON_AGGRO_RANGE_ENTER);
  72. }
  73. }
  74. /**
  75. * This is used to register all monsters contained in mobs for a particular script
  76. * event types defined in types.
  77. * @param mobs
  78. * @param types
  79. */
  80. public void registerMobs(int[] mobs, QuestEventType... types)
  81. {
  82. for (int id : mobs)
  83. {
  84. for (QuestEventType type : types)
  85. {
  86. addEventId(id, type);
  87. }
  88. }
  89. }
  90. public void registerMobs(Iterable<Integer> mobs, QuestEventType... types)
  91. {
  92. for (int id : mobs)
  93. {
  94. for (QuestEventType type : types)
  95. {
  96. addEventId(id, type);
  97. }
  98. }
  99. }
  100. /**
  101. * Broadcasts NpcSay packet to all known players with custom string.
  102. * @param npc
  103. * @param type
  104. * @param text
  105. */
  106. protected void broadcastNpcSay(L2Npc npc, int type, String text)
  107. {
  108. Broadcast.toKnownPlayers(npc, new NpcSay(npc.getObjectId(), type, npc.getTemplate().getIdTemplate(), text));
  109. }
  110. /**
  111. * Broadcasts NpcSay packet to all known players with npc string id.
  112. * @param npc
  113. * @param type
  114. * @param stringId
  115. */
  116. protected void broadcastNpcSay(L2Npc npc, int type, NpcStringId stringId)
  117. {
  118. Broadcast.toKnownPlayers(npc, new NpcSay(npc.getObjectId(), type, npc.getTemplate().getIdTemplate(), stringId));
  119. }
  120. /**
  121. * Broadcasts NpcSay packet to all known players with custom string in specific radius.
  122. * @param npc
  123. * @param type
  124. * @param text
  125. * @param radius
  126. */
  127. protected void broadcastNpcSay(L2Npc npc, int type, String text, int radius)
  128. {
  129. Broadcast.toKnownPlayersInRadius(npc, new NpcSay(npc.getObjectId(), type, npc.getTemplate().getIdTemplate(), text), radius);
  130. }
  131. /**
  132. * Broadcasts NpcSay packet to all known players with npc string id in specific radius.
  133. * @param npc
  134. * @param type
  135. * @param stringId
  136. * @param radius
  137. */
  138. protected void broadcastNpcSay(L2Npc npc, int type, NpcStringId stringId, int radius)
  139. {
  140. Broadcast.toKnownPlayersInRadius(npc, new NpcSay(npc.getObjectId(), type, npc.getTemplate().getIdTemplate(), stringId), radius);
  141. }
  142. /**
  143. * Broadcasts SocialAction packet to self and known players.
  144. * @param character
  145. * @param actionId
  146. */
  147. protected void broadcastSocialAction(L2Character character, int actionId)
  148. {
  149. Broadcast.toSelfAndKnownPlayers(character, new SocialAction(character.getObjectId(), actionId));
  150. }
  151. /**
  152. * Broadcasts SocialAction packet to self and known players in specific radius.
  153. * @param character
  154. * @param actionId
  155. * @param radius
  156. */
  157. protected void broadcastSocialAction(L2Character character, int actionId, int radius)
  158. {
  159. Broadcast.toSelfAndKnownPlayersInRadius(character, new SocialAction(character.getObjectId(), actionId), radius);
  160. }
  161. /**
  162. * Give item/reward to the player
  163. * @param player
  164. * @param holder
  165. */
  166. protected void giveItems(L2PcInstance player, ItemHolder holder)
  167. {
  168. super.giveItems(player, holder.getId(), holder.getCount());
  169. }
  170. /**
  171. * Monster is running and attacking the playable.
  172. * @param npc
  173. * @param playable
  174. */
  175. protected void attackPlayer(L2Attackable npc, L2Playable playable)
  176. {
  177. npc.setIsRunning(true);
  178. npc.addDamageHate(playable, 0, 999);
  179. npc.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, playable);
  180. }
  181. }