SummonNpc.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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().getInteger("despawnDelay", 20000);
  50. _npcId = template.getParameters().getInteger("npcId", 0);
  51. _npcCount = template.getParameters().getInteger("npcCount", 1);
  52. _randomOffset = template.getParameters().getBool("randomOffset", false);
  53. _isSummonSpawn = template.getParameters().getBool("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.hasSummon() || 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.spawnMe(player.getX(), player.getY(), player.getZ());
  98. player.setDecoy(decoy);
  99. break;
  100. }
  101. case "L2EffectPoint": // TODO: Implement proper signet skills.
  102. {
  103. final L2EffectPointInstance effectPoint = new L2EffectPointInstance(IdFactory.getInstance().getNextId(), npcTemplate, player);
  104. effectPoint.setCurrentHp(effectPoint.getMaxHp());
  105. effectPoint.setCurrentMp(effectPoint.getMaxMp());
  106. int x = player.getX();
  107. int y = player.getY();
  108. int z = player.getZ();
  109. if (getSkill().getTargetType() == L2TargetType.GROUND)
  110. {
  111. final Point3D wordPosition = player.getActingPlayer().getCurrentSkillWorldPosition();
  112. if (wordPosition != null)
  113. {
  114. x = wordPosition.getX();
  115. y = wordPosition.getY();
  116. z = wordPosition.getZ();
  117. }
  118. }
  119. getSkill().getEffects(player, effectPoint);
  120. effectPoint.setIsInvul(true);
  121. effectPoint.spawnMe(x, y, z);
  122. break;
  123. }
  124. default:
  125. {
  126. L2Spawn spawn;
  127. try
  128. {
  129. spawn = new L2Spawn(npcTemplate);
  130. }
  131. catch (Exception e)
  132. {
  133. _log.warning(SummonNpc.class.getSimpleName() + ": " + e.getMessage());
  134. return false;
  135. }
  136. int x = player.getX();
  137. int y = player.getY();
  138. if (_randomOffset)
  139. {
  140. x += (Rnd.nextBoolean() ? Rnd.get(20, 50) : Rnd.get(-50, -20));
  141. y += (Rnd.nextBoolean() ? Rnd.get(20, 50) : Rnd.get(-50, -20));
  142. }
  143. spawn.setX(x);
  144. spawn.setY(y);
  145. spawn.setZ(player.getZ());
  146. spawn.setHeading(player.getHeading());
  147. spawn.stopRespawn();
  148. final L2Npc npc = spawn.doSpawn(_isSummonSpawn);
  149. npc.setName(npcTemplate.getName());
  150. npc.setTitle(npcTemplate.getName());
  151. npc.setSummoner(player);
  152. if (_despawnDelay > 0)
  153. {
  154. npc.scheduleDespawn(_despawnDelay);
  155. }
  156. npc.setIsRunning(false); // TODO: Fix broadcast info.
  157. }
  158. }
  159. return true;
  160. }
  161. }