AbstractNpcAI.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. @Override
  45. public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
  46. {
  47. return null;
  48. }
  49. public AbstractNpcAI(String name, String descr)
  50. {
  51. super(-1, name, descr);
  52. }
  53. /**
  54. * Registers the fallowing events to the current script:<br>
  55. * <ul>
  56. * <li>ON_ATTACK</li>
  57. * <li>ON_KILL</li>
  58. * <li>ON_SPAWN</li>
  59. * <li>ON_SPELL_FINISHED</li>
  60. * <li>ON_SKILL_SEE</li>
  61. * <li>ON_FACTION_CALL</li>
  62. * <li>ON_AGGR_RANGE_ENTER</li>
  63. * </ul>
  64. * @param mobs
  65. */
  66. public void registerMobs(int... mobs)
  67. {
  68. for (int id : mobs)
  69. {
  70. addEventId(id, QuestEventType.ON_ATTACK);
  71. addEventId(id, QuestEventType.ON_KILL);
  72. addEventId(id, QuestEventType.ON_SPAWN);
  73. addEventId(id, QuestEventType.ON_SPELL_FINISHED);
  74. addEventId(id, QuestEventType.ON_SKILL_SEE);
  75. addEventId(id, QuestEventType.ON_FACTION_CALL);
  76. addEventId(id, QuestEventType.ON_AGGRO_RANGE_ENTER);
  77. }
  78. }
  79. /**
  80. * This is used to register all monsters contained in mobs for a particular script
  81. * event types defined in types.
  82. * @param mobs
  83. * @param types
  84. */
  85. public void registerMobs(int[] mobs, QuestEventType... types)
  86. {
  87. for (int id : mobs)
  88. {
  89. for (QuestEventType type : types)
  90. {
  91. addEventId(id, type);
  92. }
  93. }
  94. }
  95. public void registerMobs(Iterable<Integer> mobs, QuestEventType... types)
  96. {
  97. for (int id : mobs)
  98. {
  99. for (QuestEventType type : types)
  100. {
  101. addEventId(id, type);
  102. }
  103. }
  104. }
  105. /**
  106. * Broadcasts NpcSay packet to all known players with custom string.
  107. * @param npc
  108. * @param type
  109. * @param text
  110. */
  111. protected void broadcastNpcSay(L2Npc npc, int type, String text)
  112. {
  113. Broadcast.toKnownPlayers(npc, new NpcSay(npc.getObjectId(), type, npc.getTemplate().getIdTemplate(), text));
  114. }
  115. /**
  116. * Broadcasts NpcSay packet to all known players with npc string id.
  117. * @param npc
  118. * @param type
  119. * @param stringId
  120. */
  121. protected void broadcastNpcSay(L2Npc npc, int type, NpcStringId stringId)
  122. {
  123. Broadcast.toKnownPlayers(npc, new NpcSay(npc.getObjectId(), type, npc.getTemplate().getIdTemplate(), stringId));
  124. }
  125. /**
  126. * Broadcasts NpcSay packet to all known players with custom string in specific radius.
  127. * @param npc
  128. * @param type
  129. * @param text
  130. * @param radius
  131. */
  132. protected void broadcastNpcSay(L2Npc npc, int type, String text, int radius)
  133. {
  134. Broadcast.toKnownPlayersInRadius(npc, new NpcSay(npc.getObjectId(), type, npc.getTemplate().getIdTemplate(), text), radius);
  135. }
  136. /**
  137. * Broadcasts NpcSay packet to all known players with npc string id in specific radius.
  138. * @param npc
  139. * @param type
  140. * @param stringId
  141. * @param radius
  142. */
  143. protected void broadcastNpcSay(L2Npc npc, int type, NpcStringId stringId, int radius)
  144. {
  145. Broadcast.toKnownPlayersInRadius(npc, new NpcSay(npc.getObjectId(), type, npc.getTemplate().getIdTemplate(), stringId), radius);
  146. }
  147. /**
  148. * Broadcasts SocialAction packet to self and known players.
  149. * @param character
  150. * @param actionId
  151. */
  152. protected void broadcastSocialAction(L2Character character, int actionId)
  153. {
  154. Broadcast.toSelfAndKnownPlayers(character, new SocialAction(character.getObjectId(), actionId));
  155. }
  156. /**
  157. * Broadcasts SocialAction packet to self and known players in specific radius.
  158. * @param character
  159. * @param actionId
  160. * @param radius
  161. */
  162. protected void broadcastSocialAction(L2Character character, int actionId, int radius)
  163. {
  164. Broadcast.toSelfAndKnownPlayersInRadius(character, new SocialAction(character.getObjectId(), actionId), radius);
  165. }
  166. /**
  167. * Give item/reward to the player
  168. * @param player
  169. * @param holder
  170. */
  171. protected void giveItems(L2PcInstance player, ItemHolder holder)
  172. {
  173. super.giveItems(player, holder.getId(), holder.getCount());
  174. }
  175. /**
  176. * Monster is running and attacking the playable.
  177. * @param npc
  178. * @param playable
  179. */
  180. protected void attackPlayer(L2Attackable npc, L2Playable playable)
  181. {
  182. npc.setIsRunning(true);
  183. npc.addDamageHate(playable, 0, 999);
  184. npc.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, playable);
  185. }
  186. }