AbstractNpcAI.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.holders.SkillHolder;
  28. import com.l2jserver.gameserver.model.quest.Quest;
  29. import com.l2jserver.gameserver.network.NpcStringId;
  30. import com.l2jserver.gameserver.network.serverpackets.NpcSay;
  31. import com.l2jserver.gameserver.network.serverpackets.SocialAction;
  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 Quest
  38. {
  39. public final 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. * Broadcasts NpcSay packet to all known players with custom string.
  77. * @param npc
  78. * @param type
  79. * @param text
  80. */
  81. protected void broadcastNpcSay(L2Npc npc, int type, String text)
  82. {
  83. Broadcast.toKnownPlayers(npc, new NpcSay(npc.getObjectId(), type, npc.getTemplate().getDisplayId(), text));
  84. }
  85. /**
  86. * Broadcasts NpcSay packet to all known players with npc string id.
  87. * @param npc
  88. * @param type
  89. * @param stringId
  90. */
  91. protected void broadcastNpcSay(L2Npc npc, int type, NpcStringId stringId)
  92. {
  93. Broadcast.toKnownPlayers(npc, new NpcSay(npc.getObjectId(), type, npc.getTemplate().getDisplayId(), stringId));
  94. }
  95. /**
  96. * Broadcasts NpcSay packet to all known players with npc string id.
  97. * @param npc
  98. * @param type
  99. * @param stringId
  100. * @param parameters
  101. */
  102. protected void broadcastNpcSay(L2Npc npc, int type, NpcStringId stringId, String... parameters)
  103. {
  104. final NpcSay say = new NpcSay(npc.getObjectId(), type, npc.getTemplate().getDisplayId(), stringId);
  105. if (parameters != null)
  106. {
  107. for (String parameter : parameters)
  108. {
  109. say.addStringParameter(parameter);
  110. }
  111. }
  112. Broadcast.toKnownPlayers(npc, say);
  113. }
  114. /**
  115. * Broadcasts NpcSay packet to all known players with custom string in specific radius.
  116. * @param npc
  117. * @param type
  118. * @param text
  119. * @param radius
  120. */
  121. protected void broadcastNpcSay(L2Npc npc, int type, String text, int radius)
  122. {
  123. Broadcast.toKnownPlayersInRadius(npc, new NpcSay(npc.getObjectId(), type, npc.getTemplate().getDisplayId(), text), radius);
  124. }
  125. /**
  126. * Broadcasts NpcSay packet to all known players with npc string id in specific radius.
  127. * @param npc
  128. * @param type
  129. * @param stringId
  130. * @param radius
  131. */
  132. protected void broadcastNpcSay(L2Npc npc, int type, NpcStringId stringId, int radius)
  133. {
  134. Broadcast.toKnownPlayersInRadius(npc, new NpcSay(npc.getObjectId(), type, npc.getTemplate().getDisplayId(), stringId), radius);
  135. }
  136. /**
  137. * Broadcasts SocialAction packet to self and known players.
  138. * @param character
  139. * @param actionId
  140. */
  141. protected void broadcastSocialAction(L2Character character, int actionId)
  142. {
  143. Broadcast.toSelfAndKnownPlayers(character, new SocialAction(character.getObjectId(), actionId));
  144. }
  145. /**
  146. * Broadcasts SocialAction packet to self and known players in specific radius.
  147. * @param character
  148. * @param actionId
  149. * @param radius
  150. */
  151. protected void broadcastSocialAction(L2Character character, int actionId, int radius)
  152. {
  153. Broadcast.toSelfAndKnownPlayersInRadius(character, new SocialAction(character.getObjectId(), actionId), radius);
  154. }
  155. /**
  156. * Monster is running and attacking the playable.
  157. * @param npc
  158. * @param playable
  159. */
  160. protected void attackPlayer(L2Attackable npc, L2Playable playable)
  161. {
  162. attackPlayer(npc, playable, 999);
  163. }
  164. /**
  165. * Monster is running and attacking the target.
  166. * @param npc the NPC that performs the attack
  167. * @param target the target of the attack
  168. * @param desire the desire to perform the attack
  169. */
  170. protected void attackPlayer(L2Npc npc, L2Playable target, int desire)
  171. {
  172. if (npc instanceof L2Attackable)
  173. {
  174. ((L2Attackable) npc).addDamageHate(target, 0, desire);
  175. }
  176. npc.setIsRunning(true);
  177. npc.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
  178. }
  179. /**
  180. * Monster cast an skill to the character.
  181. * @param npc the NPC whom cast the skill
  182. * @param target the skill target
  183. * @param skill the skill to cast
  184. * @param desire the desire to cast the skill
  185. */
  186. protected void castSkill(L2Npc npc, L2Character target, SkillHolder skill, int desire)
  187. {
  188. if (npc instanceof L2Attackable)
  189. {
  190. ((L2Attackable) npc).addDamageHate(target, 0, desire);
  191. }
  192. npc.setTarget(target);
  193. npc.getAI().setIntention(CtrlIntention.AI_INTENTION_CAST, skill.getSkill(), target);
  194. }
  195. }