SkillChannelizer.java 6.8 KB

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