SkillChannelizer.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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.SkillData;
  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 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 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. _channelized.getSkillChannelized().removeChannelizer(_skill.getChannelingSkillId(), getChannelizer());
  89. _channelized = null;
  90. }
  91. // unset skill.
  92. _skill = null;
  93. }
  94. public Skill 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 Skill baseSkill = SkillData.getInstance().getSkill(_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 = SkillData.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 Skill skill = SkillData.getInstance().getSkill(_skill.getChannelingSkillId(), skillLevel);
  166. if (skill == null)
  167. {
  168. _log.log(Level.WARNING, getClass().getSimpleName() + ": Non existent channeling skill requested: " + _skill);
  169. _channelizer.abortCast();
  170. return;
  171. }
  172. skill.applyEffects(getChannelizer(), _channelized);
  173. }
  174. _channelizer.broadcastPacket(new MagicSkillLaunched(_channelizer, _skill.getId(), _skill.getLevel(), _channelized));
  175. }
  176. }
  177. else
  178. {
  179. final List<L2Character> targets = getTargetList();
  180. final Iterator<L2Character> it = targets.iterator();
  181. while (it.hasNext())
  182. {
  183. final L2Character target = it.next();
  184. if (!GeoData.getInstance().canSeeTarget(_channelizer, target))
  185. {
  186. it.remove();
  187. continue;
  188. }
  189. if (_channelizer.isPlayable() && target.isPlayable() && _skill.isBad())
  190. {
  191. // Validate pvp conditions.
  192. if (_channelizer.isPlayable() && _channelizer.getActingPlayer().canAttackCharacter(target))
  193. {
  194. // Apply channeling skill effects on the target.
  195. _skill.applyEffects(_channelizer, target);
  196. // Update the pvp flag of the caster.
  197. _channelizer.getActingPlayer().updatePvPStatus(target);
  198. }
  199. else
  200. {
  201. it.remove();
  202. }
  203. }
  204. else
  205. {
  206. // Apply channeling skill effects on the target.
  207. _skill.applyEffects(_channelizer, target);
  208. }
  209. }
  210. // Broadcast MagicSkillLaunched on every cast.
  211. _channelizer.broadcastPacket(new MagicSkillLaunched(_channelizer, _skill.getId(), _skill.getLevel(), targets.toArray(new L2Character[0])));
  212. // Reduce shots.
  213. if (_skill.useSpiritShot())
  214. {
  215. _channelizer.setChargedShot(_channelizer.isChargedShot(ShotType.BLESSED_SPIRITSHOTS) ? ShotType.BLESSED_SPIRITSHOTS : ShotType.SPIRITSHOTS, false);
  216. }
  217. else
  218. {
  219. _channelizer.setChargedShot(ShotType.SOULSHOTS, false);
  220. }
  221. // Shots are re-charged every cast.
  222. _channelizer.rechargeShots(_skill.useSoulShot(), _skill.useSpiritShot());
  223. }
  224. }
  225. catch (Exception e)
  226. {
  227. _log.log(Level.WARNING, "Error while channelizing skill: " + _skill + " channelizer: " + _channelizer + " channelized: " + _channelized, e);
  228. }
  229. }
  230. public List<L2Character> getTargetList()
  231. {
  232. // Get possible targets
  233. final List<L2Character> targets = new ArrayList<>();
  234. switch (_skill.getTargetType())
  235. {
  236. case GROUND:
  237. {
  238. int x = _channelizer.getX();
  239. int y = _channelizer.getY();
  240. int z = _channelizer.getZ();
  241. if (_channelizer.isPlayer())
  242. {
  243. final Location wordPosition = _channelizer.getActingPlayer().getCurrentSkillWorldPosition();
  244. if (wordPosition != null)
  245. {
  246. x = wordPosition.getX();
  247. y = wordPosition.getY();
  248. z = wordPosition.getZ();
  249. }
  250. }
  251. for (L2Character cha : _channelizer.getKnownList().getKnownCharacters())
  252. {
  253. // Null target or caster himself is not valid target.
  254. if ((cha == null) || (cha == _channelizer))
  255. {
  256. continue;
  257. }
  258. // Target is too far.
  259. if (cha.calculateDistance(x, y, z, true, false) > _skill.getAffectRange())
  260. {
  261. continue;
  262. }
  263. // Only attackable creatures can be attacked.
  264. if (cha.isAttackable() || cha.isPlayable())
  265. {
  266. if (cha.isAlikeDead())
  267. {
  268. continue;
  269. }
  270. // Valid target, registering it.
  271. targets.add(cha);
  272. }
  273. }
  274. break;
  275. }
  276. default:
  277. {
  278. // Null target, not L2Character or caster himself is not valid target.
  279. if ((_channelizer.getTarget() != null) && _channelizer.getTarget().isCharacter() && (_channelizer.getTarget() != _channelizer))
  280. {
  281. targets.add((L2Character) _channelizer.getTarget());
  282. }
  283. break;
  284. }
  285. }
  286. return targets;
  287. }
  288. }