SkillChannelizer.java 8.9 KB

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