SkillChannelizer.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (C) 2004-2013 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.Iterator;
  22. import java.util.List;
  23. import java.util.concurrent.ScheduledFuture;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import com.l2jserver.gameserver.GeoData;
  27. import com.l2jserver.gameserver.ThreadPoolManager;
  28. import com.l2jserver.gameserver.datatables.SkillTable;
  29. import com.l2jserver.gameserver.enums.ShotType;
  30. import com.l2jserver.gameserver.model.Location;
  31. import com.l2jserver.gameserver.model.actor.L2Character;
  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 L2Character _channelized;
  44. private L2Skill _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 L2Character getChannelized()
  55. {
  56. return _channelized;
  57. }
  58. public boolean hasChannelized()
  59. {
  60. return _channelized != null;
  61. }
  62. public void startChanneling(L2Skill 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. _channelized.getSkillChannelized().removeChannelizer(_skill.getChannelingSkillId(), getChannelizer());
  89. _channelized = null;
  90. }
  91. // unset skill.
  92. _skill = null;
  93. }
  94. public L2Skill getSkill()
  95. {
  96. return _skill;
  97. }
  98. public boolean isChanneling()
  99. {
  100. return _task != null;
  101. }
  102. @Override
  103. public void run()
  104. {
  105. if (!isChanneling())
  106. {
  107. return;
  108. }
  109. if (_skill.getMpPerChanneling() > 0)
  110. {
  111. // Validate mana per tick.
  112. if (_channelizer.getCurrentMp() < _skill.getMpPerChanneling())
  113. {
  114. if (_channelizer.isPlayer())
  115. {
  116. _channelizer.sendPacket(SystemMessageId.SKILL_REMOVED_DUE_LACK_MP);
  117. }
  118. _channelizer.abortCast();
  119. return;
  120. }
  121. // Reduce mana per tick
  122. _channelizer.reduceCurrentMp(_skill.getMpPerChanneling());
  123. }
  124. // Apply channeling skills on the targets.
  125. if (_skill.getChannelingSkillId() > 0)
  126. {
  127. final L2Skill baseSkill = SkillTable.getInstance().getInfo(_skill.getChannelingSkillId(), 1);
  128. if (baseSkill == null)
  129. {
  130. _log.log(Level.WARNING, getClass().getSimpleName() + ": skill " + _skill + " couldn't find effect id skill: " + _skill.getChannelingSkillId() + " !");
  131. _channelizer.abortCast();
  132. return;
  133. }
  134. if (_channelized == null)
  135. {
  136. final List<L2Character> targets = getTargetList();
  137. if (targets.isEmpty())
  138. {
  139. _log.log(Level.WARNING, getClass().getSimpleName() + ": skill " + _skill + " couldn't find proper target!");
  140. _channelizer.abortCast();
  141. return;
  142. }
  143. _channelized = targets.get(0);
  144. _channelized.getSkillChannelized().addChannelizer(_skill.getChannelingSkillId(), getChannelizer());
  145. }
  146. if (!Util.checkIfInRange(_skill.getEffectRange(), _channelizer, _channelized, true))
  147. {
  148. _channelizer.abortCast();
  149. _channelizer.sendPacket(SystemMessageId.CANT_SEE_TARGET);
  150. }
  151. else if (!GeoData.getInstance().canSeeTarget(_channelizer, _channelized))
  152. {
  153. _channelizer.abortCast();
  154. _channelizer.sendPacket(SystemMessageId.CANT_SEE_TARGET);
  155. }
  156. else
  157. {
  158. final int maxSkillLevel = SkillTable.getInstance().getMaxLevel(_skill.getChannelingSkillId());
  159. final int skillLevel = Math.min(_channelized.getSkillChannelized().getChannerlizersSize(_skill.getChannelingSkillId()), maxSkillLevel);
  160. final BuffInfo info = _channelized.getEffectList().getBuffInfoBySkillId(_skill.getChannelingSkillId());
  161. if ((info == null) || (info.getSkill().getLevel() < skillLevel))
  162. {
  163. final L2Skill skill = SkillTable.getInstance().getInfo(_skill.getChannelingSkillId(), skillLevel);
  164. skill.applyEffects(getChannelizer(), _channelized);
  165. }
  166. _channelizer.broadcastPacket(new MagicSkillLaunched(_channelizer, _skill.getId(), _skill.getLevel(), _channelized));
  167. }
  168. }
  169. else
  170. {
  171. final List<L2Character> targets = getTargetList();
  172. final Iterator<L2Character> it = targets.iterator();
  173. while (it.hasNext())
  174. {
  175. final L2Character target = it.next();
  176. if (!GeoData.getInstance().canSeeTarget(_channelizer, target))
  177. {
  178. it.remove();
  179. continue;
  180. }
  181. if (_channelizer.isPlayable() && target.isPlayable() && _skill.isBad())
  182. {
  183. // Validate pvp conditions.
  184. if (_channelizer.isPlayable() && _channelizer.getActingPlayer().canAttackCharacter(target))
  185. {
  186. // Apply channeling skill effects on the target.
  187. _skill.applyEffects(_channelizer, target);
  188. // Update the pvp flag of the caster.
  189. _channelizer.getActingPlayer().updatePvPStatus(target);
  190. }
  191. else
  192. {
  193. it.remove();
  194. }
  195. }
  196. else
  197. {
  198. // Apply channeling skill effects on the target.
  199. _skill.applyEffects(_channelizer, target);
  200. }
  201. }
  202. // Broadcast MagicSkillLaunched on every cast.
  203. _channelizer.broadcastPacket(new MagicSkillLaunched(_channelizer, _skill.getId(), _skill.getLevel(), targets.toArray(new L2Character[0])));
  204. // Reduce shots.
  205. if (_skill.useSpiritShot())
  206. {
  207. _channelizer.setChargedShot(_channelizer.isChargedShot(ShotType.BLESSED_SPIRITSHOTS) ? ShotType.BLESSED_SPIRITSHOTS : ShotType.SPIRITSHOTS, false);
  208. }
  209. else
  210. {
  211. _channelizer.setChargedShot(ShotType.SOULSHOTS, false);
  212. }
  213. // Shots are re-charged every cast.
  214. _channelizer.rechargeShots(_skill.useSoulShot(), _skill.useSpiritShot());
  215. }
  216. }
  217. public List<L2Character> getTargetList()
  218. {
  219. // Get possible targets
  220. final List<L2Character> targets = new ArrayList<>();
  221. switch (_skill.getTargetType())
  222. {
  223. case GROUND:
  224. {
  225. int x = _channelizer.getX();
  226. int y = _channelizer.getY();
  227. int z = _channelizer.getZ();
  228. if (_channelizer.isPlayer())
  229. {
  230. final Location wordPosition = _channelizer.getActingPlayer().getCurrentSkillWorldPosition();
  231. if (wordPosition != null)
  232. {
  233. x = wordPosition.getX();
  234. y = wordPosition.getY();
  235. z = wordPosition.getZ();
  236. }
  237. }
  238. for (L2Character cha : _channelizer.getKnownList().getKnownCharacters())
  239. {
  240. // Null target or caster himself is not valid target.
  241. if ((cha == null) || (cha == _channelizer))
  242. {
  243. continue;
  244. }
  245. // Target is too far.
  246. if (cha.calculateDistance(x, y, z, true, false) > _skill.getAffectRange())
  247. {
  248. continue;
  249. }
  250. // Only attackable creatures can be attacked.
  251. if (cha.isL2Attackable() || cha.isPlayable())
  252. {
  253. if (cha.isAlikeDead())
  254. {
  255. continue;
  256. }
  257. // Valid target, registering it.
  258. targets.add(cha);
  259. }
  260. }
  261. break;
  262. }
  263. default:
  264. {
  265. // Null target, not L2Character or caster himself is not valid target.
  266. if ((_channelizer.getTarget() != null) && _channelizer.getTarget().isCharacter() && (_channelizer.getTarget() != _channelizer))
  267. {
  268. targets.add((L2Character) _channelizer.getTarget());
  269. }
  270. break;
  271. }
  272. }
  273. return targets;
  274. }
  275. }