SummonNpc.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C) 2004-2013 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 handlers.effecthandlers;
  20. import com.l2jserver.gameserver.datatables.NpcTable;
  21. import com.l2jserver.gameserver.idfactory.IdFactory;
  22. import com.l2jserver.gameserver.model.L2Spawn;
  23. import com.l2jserver.gameserver.model.actor.L2Npc;
  24. import com.l2jserver.gameserver.model.actor.instance.L2DecoyInstance;
  25. import com.l2jserver.gameserver.model.actor.instance.L2EffectPointInstance;
  26. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  27. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  28. import com.l2jserver.gameserver.model.effects.EffectTemplate;
  29. import com.l2jserver.gameserver.model.effects.L2Effect;
  30. import com.l2jserver.gameserver.model.effects.L2EffectType;
  31. import com.l2jserver.gameserver.model.skills.targets.L2TargetType;
  32. import com.l2jserver.gameserver.model.stats.Env;
  33. import com.l2jserver.gameserver.util.Point3D;
  34. import com.l2jserver.util.Rnd;
  35. /**
  36. * Summon Npc effect implementation.
  37. * @author Zoey76
  38. */
  39. public class SummonNpc extends L2Effect
  40. {
  41. private final int _despawnDelay;
  42. private final int _npcId;
  43. private final int _npcCount;
  44. private final boolean _randomOffset;
  45. private final boolean _isSummonSpawn;
  46. public SummonNpc(Env env, EffectTemplate template)
  47. {
  48. super(env, template);
  49. _despawnDelay = template.getParameters().getInt("despawnDelay", 20000);
  50. _npcId = template.getParameters().getInt("npcId", 0);
  51. _npcCount = template.getParameters().getInt("npcCount", 1);
  52. _randomOffset = template.getParameters().getBoolean("randomOffset", false);
  53. _isSummonSpawn = template.getParameters().getBoolean("isSummonSpawn", false);
  54. }
  55. @Override
  56. public L2EffectType getEffectType()
  57. {
  58. return L2EffectType.NONE;
  59. }
  60. @Override
  61. public boolean isInstant()
  62. {
  63. return true;
  64. }
  65. @Override
  66. public boolean onStart()
  67. {
  68. if ((getEffected() == null) || !getEffected().isPlayer() || getEffected().isAlikeDead() || getEffected().getActingPlayer().inObserverMode())
  69. {
  70. return false;
  71. }
  72. if ((_npcId <= 0) || (_npcCount <= 0))
  73. {
  74. _log.warning(SummonNpc.class.getSimpleName() + ": Invalid NPC Id or count skill Id: " + getSkill().getId());
  75. return false;
  76. }
  77. final L2PcInstance player = getEffected().getActingPlayer();
  78. if (player.isMounted())
  79. {
  80. return false;
  81. }
  82. final L2NpcTemplate npcTemplate = NpcTable.getInstance().getTemplate(_npcId);
  83. if (npcTemplate == null)
  84. {
  85. _log.warning(SummonNpc.class.getSimpleName() + ": Spawn of the nonexisting NPC Id: " + _npcId + ", skill Id:" + getSkill().getId());
  86. return false;
  87. }
  88. switch (npcTemplate.getType())
  89. {
  90. case "L2Decoy":
  91. {
  92. final L2DecoyInstance decoy = new L2DecoyInstance(IdFactory.getInstance().getNextId(), npcTemplate, player, _despawnDelay);
  93. decoy.setCurrentHp(decoy.getMaxHp());
  94. decoy.setCurrentMp(decoy.getMaxMp());
  95. decoy.setHeading(player.getHeading());
  96. decoy.setInstanceId(player.getInstanceId());
  97. decoy.setSummoner(player);
  98. decoy.spawnMe(player.getX(), player.getY(), player.getZ());
  99. player.setDecoy(decoy);
  100. break;
  101. }
  102. case "L2EffectPoint": // TODO: Implement proper signet skills.
  103. {
  104. final L2EffectPointInstance effectPoint = new L2EffectPointInstance(IdFactory.getInstance().getNextId(), npcTemplate, player);
  105. effectPoint.setCurrentHp(effectPoint.getMaxHp());
  106. effectPoint.setCurrentMp(effectPoint.getMaxMp());
  107. int x = player.getX();
  108. int y = player.getY();
  109. int z = player.getZ();
  110. if (getSkill().getTargetType() == L2TargetType.GROUND)
  111. {
  112. final Point3D wordPosition = player.getActingPlayer().getCurrentSkillWorldPosition();
  113. if (wordPosition != null)
  114. {
  115. x = wordPosition.getX();
  116. y = wordPosition.getY();
  117. z = wordPosition.getZ();
  118. }
  119. }
  120. effectPoint.setIsInvul(true);
  121. effectPoint.setSummoner(player);
  122. effectPoint.spawnMe(x, y, z);
  123. if (_despawnDelay > 0)
  124. {
  125. effectPoint.scheduleDespawn(_despawnDelay);
  126. }
  127. break;
  128. }
  129. default:
  130. {
  131. L2Spawn spawn;
  132. try
  133. {
  134. spawn = new L2Spawn(npcTemplate);
  135. }
  136. catch (Exception e)
  137. {
  138. _log.warning(SummonNpc.class.getSimpleName() + ": " + e.getMessage());
  139. return false;
  140. }
  141. int x = player.getX();
  142. int y = player.getY();
  143. if (_randomOffset)
  144. {
  145. x += (Rnd.nextBoolean() ? Rnd.get(20, 50) : Rnd.get(-50, -20));
  146. y += (Rnd.nextBoolean() ? Rnd.get(20, 50) : Rnd.get(-50, -20));
  147. }
  148. spawn.setX(x);
  149. spawn.setY(y);
  150. spawn.setZ(player.getZ());
  151. spawn.setHeading(player.getHeading());
  152. spawn.stopRespawn();
  153. final L2Npc npc = spawn.doSpawn(_isSummonSpawn);
  154. npc.setSummoner(player);
  155. npc.setName(npcTemplate.getName());
  156. npc.setTitle(npcTemplate.getName());
  157. if (_despawnDelay > 0)
  158. {
  159. npc.scheduleDespawn(_despawnDelay);
  160. }
  161. npc.setIsRunning(false); // TODO: Fix broadcast info.
  162. }
  163. }
  164. return true;
  165. }
  166. }