L2SkillDrain.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.skills.l2skills;
  16. import java.util.logging.Level;
  17. import java.util.logging.LogRecord;
  18. import java.util.logging.Logger;
  19. import com.l2jserver.Config;
  20. import com.l2jserver.gameserver.model.L2Effect;
  21. import com.l2jserver.gameserver.model.L2ItemInstance;
  22. import com.l2jserver.gameserver.model.L2Object;
  23. import com.l2jserver.gameserver.model.L2Skill;
  24. import com.l2jserver.gameserver.model.actor.L2Character;
  25. import com.l2jserver.gameserver.model.actor.L2Npc;
  26. import com.l2jserver.gameserver.model.actor.L2Playable;
  27. import com.l2jserver.gameserver.model.actor.L2Summon;
  28. import com.l2jserver.gameserver.model.actor.instance.L2CubicInstance;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.network.SystemMessageId;
  31. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  32. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  33. import com.l2jserver.gameserver.skills.Formulas;
  34. import com.l2jserver.gameserver.templates.StatsSet;
  35. import com.l2jserver.gameserver.templates.skills.L2TargetType;
  36. public class L2SkillDrain extends L2Skill
  37. {
  38. private static final Logger _logDamage = Logger.getLogger("damage");
  39. private final float _absorbPart;
  40. private final int _absorbAbs;
  41. public L2SkillDrain(StatsSet set)
  42. {
  43. super(set);
  44. _absorbPart = set.getFloat("absorbPart", 0.f);
  45. _absorbAbs = set.getInteger("absorbAbs", 0);
  46. }
  47. @Override
  48. public void useSkill(L2Character activeChar, L2Object[] targets)
  49. {
  50. if (activeChar.isAlikeDead())
  51. {
  52. return;
  53. }
  54. boolean ss = false;
  55. boolean bss = false;
  56. for (L2Character target : (L2Character[]) targets)
  57. {
  58. if (target.isAlikeDead() && (getTargetType() != L2TargetType.TARGET_CORPSE_MOB))
  59. {
  60. continue;
  61. }
  62. if ((activeChar != target) && target.isInvul())
  63. {
  64. continue; // No effect on invulnerable chars unless they cast it themselves.
  65. }
  66. L2ItemInstance weaponInst = activeChar.getActiveWeaponInstance();
  67. if (weaponInst != null)
  68. {
  69. if (weaponInst.getChargedSpiritshot() == L2ItemInstance.CHARGED_BLESSED_SPIRITSHOT)
  70. {
  71. bss = true;
  72. weaponInst.setChargedSpiritshot(L2ItemInstance.CHARGED_NONE);
  73. }
  74. else if (weaponInst.getChargedSpiritshot() == L2ItemInstance.CHARGED_SPIRITSHOT)
  75. {
  76. ss = true;
  77. weaponInst.setChargedSpiritshot(L2ItemInstance.CHARGED_NONE);
  78. }
  79. }
  80. // If there is no weapon equipped, check for an active summon.
  81. else if (activeChar instanceof L2Summon)
  82. {
  83. L2Summon activeSummon = (L2Summon) activeChar;
  84. if (activeSummon.getChargedSpiritShot() == L2ItemInstance.CHARGED_BLESSED_SPIRITSHOT)
  85. {
  86. bss = true;
  87. activeSummon.setChargedSpiritShot(L2ItemInstance.CHARGED_NONE);
  88. }
  89. else if (activeSummon.getChargedSpiritShot() == L2ItemInstance.CHARGED_SPIRITSHOT)
  90. {
  91. ss = true;
  92. activeSummon.setChargedSpiritShot(L2ItemInstance.CHARGED_NONE);
  93. }
  94. }
  95. boolean mcrit = Formulas.calcMCrit(activeChar.getMCriticalHit(target, this));
  96. byte shld = Formulas.calcShldUse(activeChar, target, this);
  97. int damage = isStaticDamage() ? (int) getPower() : (int) Formulas.calcMagicDam(activeChar, target, this, shld, ss, bss, mcrit);
  98. int _drain = 0;
  99. int _cp = (int) target.getCurrentCp();
  100. int _hp = (int) target.getCurrentHp();
  101. if (_cp > 0)
  102. {
  103. if (damage < _cp)
  104. {
  105. _drain = 0;
  106. }
  107. else
  108. {
  109. _drain = damage - _cp;
  110. }
  111. }
  112. else if (damage > _hp)
  113. {
  114. _drain = _hp;
  115. }
  116. else
  117. {
  118. _drain = damage;
  119. }
  120. double hpAdd = _absorbAbs + (_absorbPart * _drain);
  121. double hp = ((activeChar.getCurrentHp() + hpAdd) > activeChar.getMaxHp() ? activeChar.getMaxHp() : (activeChar.getCurrentHp() + hpAdd));
  122. activeChar.setCurrentHp(hp);
  123. StatusUpdate suhp = new StatusUpdate(activeChar);
  124. suhp.addAttribute(StatusUpdate.CUR_HP, (int) hp);
  125. activeChar.sendPacket(suhp);
  126. // Check to see if we should damage the target
  127. if ((damage > 0) && (!target.isDead() || (getTargetType() != L2TargetType.TARGET_CORPSE_MOB)))
  128. {
  129. // Manage attack or cast break of the target (calculating rate, sending message...)
  130. if (!target.isRaid() && Formulas.calcAtkBreak(target, damage))
  131. {
  132. target.breakAttack();
  133. target.breakCast();
  134. }
  135. activeChar.sendDamageMessage(target, damage, mcrit, false, false);
  136. if (Config.LOG_GAME_DAMAGE && (activeChar instanceof L2Playable) && (damage > Config.LOG_GAME_DAMAGE_THRESHOLD))
  137. {
  138. LogRecord record = new LogRecord(Level.INFO, "");
  139. record.setParameters(new Object[]
  140. {
  141. activeChar, " did damage ", damage, this, " to ", target
  142. });
  143. record.setLoggerName("mdam");
  144. _logDamage.log(record);
  145. }
  146. if (hasEffects() && (getTargetType() != L2TargetType.TARGET_CORPSE_MOB))
  147. {
  148. // ignoring vengance-like reflections
  149. if ((Formulas.calcSkillReflect(target, this) & Formulas.SKILL_REFLECT_SUCCEED) > 0)
  150. {
  151. activeChar.stopSkillEffects(getId());
  152. getEffects(target, activeChar);
  153. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
  154. sm.addSkillName(getId());
  155. activeChar.sendPacket(sm);
  156. }
  157. else
  158. {
  159. // activate attacked effects, if any
  160. target.stopSkillEffects(getId());
  161. if (Formulas.calcSkillSuccess(activeChar, target, this, shld, false, ss, bss))
  162. {
  163. getEffects(activeChar, target);
  164. }
  165. else
  166. {
  167. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_RESISTED_YOUR_S2);
  168. sm.addCharName(target);
  169. sm.addSkillName(this);
  170. activeChar.sendPacket(sm);
  171. }
  172. }
  173. }
  174. target.reduceCurrentHp(damage, activeChar, this);
  175. }
  176. // Check to see if we should do the decay right after the cast
  177. if (target.isDead() && (getTargetType() == L2TargetType.TARGET_CORPSE_MOB) && (target instanceof L2Npc))
  178. {
  179. ((L2Npc) target).endDecayTask();
  180. }
  181. }
  182. // effect self :]
  183. L2Effect effect = activeChar.getFirstEffect(getId());
  184. if ((effect != null) && effect.isSelfEffect())
  185. {
  186. // Replace old effect with new one.
  187. effect.exit();
  188. }
  189. // cast self effect if any
  190. getEffectsSelf(activeChar);
  191. }
  192. public void useCubicSkill(L2CubicInstance activeCubic, L2Object[] targets)
  193. {
  194. if (Config.DEBUG)
  195. {
  196. _log.info("L2SkillDrain: useCubicSkill()");
  197. }
  198. for (L2Character target : (L2Character[]) targets)
  199. {
  200. if (target.isAlikeDead() && (getTargetType() != L2TargetType.TARGET_CORPSE_MOB))
  201. {
  202. continue;
  203. }
  204. boolean mcrit = Formulas.calcMCrit(activeCubic.getMCriticalHit(target, this));
  205. byte shld = Formulas.calcShldUse(activeCubic.getOwner(), target, this);
  206. int damage = (int) Formulas.calcMagicDam(activeCubic, target, this, mcrit, shld);
  207. if (Config.DEBUG)
  208. {
  209. _log.info("L2SkillDrain: useCubicSkill() -> damage = " + damage);
  210. }
  211. double hpAdd = _absorbAbs + (_absorbPart * damage);
  212. L2PcInstance owner = activeCubic.getOwner();
  213. double hp = ((owner.getCurrentHp() + hpAdd) > owner.getMaxHp() ? owner.getMaxHp() : (owner.getCurrentHp() + hpAdd));
  214. owner.setCurrentHp(hp);
  215. StatusUpdate suhp = new StatusUpdate(owner);
  216. suhp.addAttribute(StatusUpdate.CUR_HP, (int) hp);
  217. owner.sendPacket(suhp);
  218. // Check to see if we should damage the target
  219. if ((damage > 0) && (!target.isDead() || (getTargetType() != L2TargetType.TARGET_CORPSE_MOB)))
  220. {
  221. target.reduceCurrentHp(damage, activeCubic.getOwner(), this);
  222. // Manage attack or cast break of the target (calculating rate, sending message...)
  223. if (!target.isRaid() && Formulas.calcAtkBreak(target, damage))
  224. {
  225. target.breakAttack();
  226. target.breakCast();
  227. }
  228. owner.sendDamageMessage(target, damage, mcrit, false, false);
  229. }
  230. }
  231. }
  232. }