SkillChannelizer.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.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. try
  110. {
  111. if (_skill.getMpPerChanneling() > 0)
  112. {
  113. // Validate mana per tick.
  114. if (_channelizer.getCurrentMp() < _skill.getMpPerChanneling())
  115. {
  116. if (_channelizer.isPlayer())
  117. {
  118. _channelizer.sendPacket(SystemMessageId.SKILL_REMOVED_DUE_LACK_MP);
  119. }
  120. _channelizer.abortCast();
  121. return;
  122. }
  123. // Reduce mana per tick
  124. _channelizer.reduceCurrentMp(_skill.getMpPerChanneling());
  125. }
  126. // Apply channeling skills on the targets.
  127. if (_skill.getChannelingSkillId() > 0)
  128. {
  129. final L2Skill baseSkill = SkillTable.getInstance().getInfo(_skill.getChannelingSkillId(), 1);
  130. if (baseSkill == null)
  131. {
  132. _log.log(Level.WARNING, getClass().getSimpleName() + ": skill " + _skill + " couldn't find effect id skill: " + _skill.getChannelingSkillId() + " !");
  133. _channelizer.abortCast();
  134. return;
  135. }
  136. if (_channelized == null)
  137. {
  138. final List<L2Character> targets = getTargetList();
  139. if (targets.isEmpty())
  140. {
  141. _log.log(Level.WARNING, getClass().getSimpleName() + ": skill " + _skill + " couldn't find proper target!");
  142. _channelizer.abortCast();
  143. return;
  144. }
  145. _channelized = targets.get(0);
  146. _channelized.getSkillChannelized().addChannelizer(_skill.getChannelingSkillId(), getChannelizer());
  147. }
  148. if (!Util.checkIfInRange(_skill.getEffectRange(), _channelizer, _channelized, true))
  149. {
  150. _channelizer.abortCast();
  151. _channelizer.sendPacket(SystemMessageId.CANT_SEE_TARGET);
  152. }
  153. else if (!GeoData.getInstance().canSeeTarget(_channelizer, _channelized))
  154. {
  155. _channelizer.abortCast();
  156. _channelizer.sendPacket(SystemMessageId.CANT_SEE_TARGET);
  157. }
  158. else
  159. {
  160. final int maxSkillLevel = SkillTable.getInstance().getMaxLevel(_skill.getChannelingSkillId());
  161. final int skillLevel = Math.min(_channelized.getSkillChannelized().getChannerlizersSize(_skill.getChannelingSkillId()), maxSkillLevel);
  162. final BuffInfo info = _channelized.getEffectList().getBuffInfoBySkillId(_skill.getChannelingSkillId());
  163. if ((info == null) || (info.getSkill().getLevel() < skillLevel))
  164. {
  165. final L2Skill skill = SkillTable.getInstance().getInfo(_skill.getChannelingSkillId(), skillLevel);
  166. skill.applyEffects(getChannelizer(), _channelized);
  167. }
  168. _channelizer.broadcastPacket(new MagicSkillLaunched(_channelizer, _skill.getId(), _skill.getLevel(), _channelized));
  169. }
  170. }
  171. else
  172. {
  173. final List<L2Character> targets = getTargetList();
  174. final Iterator<L2Character> it = targets.iterator();
  175. while (it.hasNext())
  176. {
  177. final L2Character target = it.next();
  178. if (!GeoData.getInstance().canSeeTarget(_channelizer, target))
  179. {
  180. it.remove();
  181. continue;
  182. }
  183. if (_channelizer.isPlayable() && target.isPlayable() && _skill.isBad())
  184. {
  185. // Validate pvp conditions.
  186. if (_channelizer.isPlayable() && _channelizer.getActingPlayer().canAttackCharacter(target))
  187. {
  188. // Apply channeling skill effects on the target.
  189. _skill.applyEffects(_channelizer, target);
  190. // Update the pvp flag of the caster.
  191. _channelizer.getActingPlayer().updatePvPStatus(target);
  192. }
  193. else
  194. {
  195. it.remove();
  196. }
  197. }
  198. else
  199. {
  200. // Apply channeling skill effects on the target.
  201. _skill.applyEffects(_channelizer, target);
  202. }
  203. }
  204. // Broadcast MagicSkillLaunched on every cast.
  205. _channelizer.broadcastPacket(new MagicSkillLaunched(_channelizer, _skill.getId(), _skill.getLevel(), targets.toArray(new L2Character[0])));
  206. // Reduce shots.
  207. if (_skill.useSpiritShot())
  208. {
  209. _channelizer.setChargedShot(_channelizer.isChargedShot(ShotType.BLESSED_SPIRITSHOTS) ? ShotType.BLESSED_SPIRITSHOTS : ShotType.SPIRITSHOTS, false);
  210. }
  211. else
  212. {
  213. _channelizer.setChargedShot(ShotType.SOULSHOTS, false);
  214. }
  215. // Shots are re-charged every cast.
  216. _channelizer.rechargeShots(_skill.useSoulShot(), _skill.useSpiritShot());
  217. }
  218. }
  219. catch (Exception e)
  220. {
  221. _log.log(Level.WARNING, "Error while channelizing skill: " + _skill + " channelizer: " + _channelizer + " channelized: " + _channelized, e);
  222. }
  223. }
  224. public List<L2Character> getTargetList()
  225. {
  226. // Get possible targets
  227. final List<L2Character> targets = new ArrayList<>();
  228. switch (_skill.getTargetType())
  229. {
  230. case GROUND:
  231. {
  232. int x = _channelizer.getX();
  233. int y = _channelizer.getY();
  234. int z = _channelizer.getZ();
  235. if (_channelizer.isPlayer())
  236. {
  237. final Location wordPosition = _channelizer.getActingPlayer().getCurrentSkillWorldPosition();
  238. if (wordPosition != null)
  239. {
  240. x = wordPosition.getX();
  241. y = wordPosition.getY();
  242. z = wordPosition.getZ();
  243. }
  244. }
  245. for (L2Character cha : _channelizer.getKnownList().getKnownCharacters())
  246. {
  247. // Null target or caster himself is not valid target.
  248. if ((cha == null) || (cha == _channelizer))
  249. {
  250. continue;
  251. }
  252. // Target is too far.
  253. if (cha.calculateDistance(x, y, z, true, false) > _skill.getAffectRange())
  254. {
  255. continue;
  256. }
  257. // Only attackable creatures can be attacked.
  258. if (cha.isAttackable() || cha.isPlayable())
  259. {
  260. if (cha.isAlikeDead())
  261. {
  262. continue;
  263. }
  264. // Valid target, registering it.
  265. targets.add(cha);
  266. }
  267. }
  268. break;
  269. }
  270. default:
  271. {
  272. // Null target, not L2Character or caster himself is not valid target.
  273. if ((_channelizer.getTarget() != null) && _channelizer.getTarget().isCharacter() && (_channelizer.getTarget() != _channelizer))
  274. {
  275. targets.add((L2Character) _channelizer.getTarget());
  276. }
  277. break;
  278. }
  279. }
  280. return targets;
  281. }
  282. }