SkillChannelizer.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server 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 Server 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 com.l2jserver.gameserver.model.skills;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.concurrent.ScheduledFuture;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import com.l2jserver.gameserver.GeoData;
  26. import com.l2jserver.gameserver.ThreadPoolManager;
  27. import com.l2jserver.gameserver.datatables.SkillData;
  28. import com.l2jserver.gameserver.enums.ShotType;
  29. import com.l2jserver.gameserver.model.L2Object;
  30. import com.l2jserver.gameserver.model.actor.L2Character;
  31. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  32. import com.l2jserver.gameserver.network.SystemMessageId;
  33. import com.l2jserver.gameserver.network.serverpackets.MagicSkillLaunched;
  34. import com.l2jserver.gameserver.util.Util;
  35. /**
  36. * Skill Channelizer implementation.
  37. * @author UnAfraid
  38. */
  39. public class SkillChannelizer implements Runnable
  40. {
  41. private static final Logger _log = Logger.getLogger(SkillChannelizer.class.getName());
  42. private final L2Character _channelizer;
  43. private List<L2Character> _channelized;
  44. private Skill _skill;
  45. private volatile ScheduledFuture<?> _task = null;
  46. public SkillChannelizer(L2Character channelizer)
  47. {
  48. _channelizer = channelizer;
  49. }
  50. public L2Character getChannelizer()
  51. {
  52. return _channelizer;
  53. }
  54. public List<L2Character> getChannelized()
  55. {
  56. return _channelized;
  57. }
  58. public boolean hasChannelized()
  59. {
  60. return _channelized != null;
  61. }
  62. public void startChanneling(Skill skill)
  63. {
  64. // Verify for same status.
  65. if (isChanneling())
  66. {
  67. _log.log(Level.WARNING, "Character: " + toString() + " is attempting to channel skill but he already does!");
  68. return;
  69. }
  70. // Start channeling.
  71. _skill = skill;
  72. _task = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(this, skill.getChannelingTickInitialDelay(), skill.getChannelingTickInterval());
  73. }
  74. public void stopChanneling()
  75. {
  76. // Verify for same status.
  77. if (!isChanneling())
  78. {
  79. _log.log(Level.WARNING, "Character: " + toString() + " is attempting to stop channel skill but he does not!");
  80. return;
  81. }
  82. // Cancel the task and unset it.
  83. _task.cancel(false);
  84. _task = null;
  85. // Cancel target channelization and unset it.
  86. if (_channelized != null)
  87. {
  88. for (L2Character chars : _channelized)
  89. {
  90. chars.getSkillChannelized().removeChannelizer(_skill.getChannelingSkillId(), getChannelizer());
  91. }
  92. _channelized = null;
  93. }
  94. // unset skill.
  95. _skill = null;
  96. }
  97. public Skill getSkill()
  98. {
  99. return _skill;
  100. }
  101. public boolean isChanneling()
  102. {
  103. return _task != null;
  104. }
  105. @Override
  106. public void run()
  107. {
  108. if (!isChanneling())
  109. {
  110. return;
  111. }
  112. try
  113. {
  114. if (_skill.getMpPerChanneling() > 0)
  115. {
  116. // Validate mana per tick.
  117. if (_channelizer.getCurrentMp() < _skill.getMpPerChanneling())
  118. {
  119. if (_channelizer.isPlayer())
  120. {
  121. _channelizer.sendPacket(SystemMessageId.SKILL_REMOVED_DUE_LACK_MP);
  122. }
  123. _channelizer.abortCast();
  124. return;
  125. }
  126. // Reduce mana per tick
  127. _channelizer.reduceCurrentMp(_skill.getMpPerChanneling());
  128. }
  129. // Apply channeling skills on the targets.
  130. if (_skill.getChannelingSkillId() > 0)
  131. {
  132. final Skill baseSkill = SkillData.getInstance().getSkill(_skill.getChannelingSkillId(), 1);
  133. if (baseSkill == null)
  134. {
  135. _log.log(Level.WARNING, getClass().getSimpleName() + ": skill " + _skill + " couldn't find effect id skill: " + _skill.getChannelingSkillId() + " !");
  136. _channelizer.abortCast();
  137. return;
  138. }
  139. final List<L2Character> targetList = new ArrayList<>();
  140. for (L2Object chars : _skill.getTargetList(_channelizer))
  141. {
  142. if (chars.isCharacter())
  143. {
  144. targetList.add((L2Character) chars);
  145. ((L2Character) chars).getSkillChannelized().addChannelizer(_skill.getChannelingSkillId(), getChannelizer());
  146. }
  147. }
  148. if (targetList.isEmpty())
  149. {
  150. return;
  151. }
  152. _channelized = targetList;
  153. for (L2Character character : _channelized)
  154. {
  155. if (!Util.checkIfInRange(_skill.getEffectRange(), _channelizer, character, true))
  156. {
  157. continue;
  158. }
  159. else if (!GeoData.getInstance().canSeeTarget(_channelizer, character))
  160. {
  161. continue;
  162. }
  163. else
  164. {
  165. final int maxSkillLevel = SkillData.getInstance().getMaxLevel(_skill.getChannelingSkillId());
  166. final int skillLevel = Math.min(character.getSkillChannelized().getChannerlizersSize(_skill.getChannelingSkillId()), maxSkillLevel);
  167. final BuffInfo info = character.getEffectList().getBuffInfoBySkillId(_skill.getChannelingSkillId());
  168. if ((info == null) || (info.getSkill().getLevel() < skillLevel))
  169. {
  170. final Skill skill = SkillData.getInstance().getSkill(_skill.getChannelingSkillId(), skillLevel);
  171. if (skill == null)
  172. {
  173. _log.log(Level.WARNING, getClass().getSimpleName() + ": Non existent channeling skill requested: " + _skill);
  174. _channelizer.abortCast();
  175. return;
  176. }
  177. // Update PvP status
  178. if (character.isPlayable() && getChannelizer().isPlayer())
  179. {
  180. ((L2PcInstance) getChannelizer()).updatePvPStatus(character);
  181. }
  182. skill.applyEffects(getChannelizer(), character);
  183. // Reduce shots.
  184. if (_skill.useSpiritShot())
  185. {
  186. _channelizer.setChargedShot(_channelizer.isChargedShot(ShotType.BLESSED_SPIRITSHOTS) ? ShotType.BLESSED_SPIRITSHOTS : ShotType.SPIRITSHOTS, false);
  187. }
  188. else
  189. {
  190. _channelizer.setChargedShot(ShotType.SOULSHOTS, false);
  191. }
  192. // Shots are re-charged every cast.
  193. _channelizer.rechargeShots(_skill.useSoulShot(), _skill.useSpiritShot());
  194. }
  195. _channelizer.broadcastPacket(new MagicSkillLaunched(_channelizer, _skill.getId(), _skill.getLevel(), character));
  196. }
  197. }
  198. }
  199. }
  200. catch (Exception e)
  201. {
  202. _log.log(Level.WARNING, "Error while channelizing skill: " + _skill + " channelizer: " + _channelizer + " channelized: " + _channelized, e);
  203. }
  204. }
  205. }