L2SkillDrain.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 net.sf.l2j.gameserver.skills.l2skills;
  16. import net.sf.l2j.gameserver.model.L2Character;
  17. import net.sf.l2j.gameserver.model.L2Effect;
  18. import net.sf.l2j.gameserver.model.L2ItemInstance;
  19. import net.sf.l2j.gameserver.model.L2Object;
  20. import net.sf.l2j.gameserver.model.L2Skill;
  21. import net.sf.l2j.gameserver.model.L2Summon;
  22. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  23. import net.sf.l2j.gameserver.network.SystemMessageId;
  24. import net.sf.l2j.gameserver.serverpackets.StatusUpdate;
  25. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  26. import net.sf.l2j.gameserver.skills.Formulas;
  27. import net.sf.l2j.gameserver.templates.StatsSet;
  28. public class L2SkillDrain extends L2Skill {
  29. private float _absorbPart;
  30. private int _absorbAbs;
  31. public L2SkillDrain(StatsSet set)
  32. {
  33. super(set);
  34. _absorbPart = set.getFloat ("absorbPart", 0.f);
  35. _absorbAbs = set.getInteger("absorbAbs", 0);
  36. }
  37. @Override
  38. public void useSkill(L2Character activeChar, L2Object[] targets)
  39. {
  40. if (activeChar.isAlikeDead())
  41. return;
  42. boolean ss = false;
  43. boolean bss = false;
  44. for(int index = 0;index < targets.length;index++)
  45. {
  46. L2Character target = (L2Character)targets[index];
  47. if (target.isAlikeDead() && getTargetType() != SkillTargetType.TARGET_CORPSE_MOB)
  48. continue;
  49. if (activeChar != target && target.isInvul())
  50. continue; // No effect on invulnerable chars unless they cast it themselves.
  51. L2ItemInstance weaponInst = activeChar.getActiveWeaponInstance();
  52. if (weaponInst != null)
  53. {
  54. if (weaponInst.getChargedSpiritshot() == L2ItemInstance.CHARGED_BLESSED_SPIRITSHOT)
  55. {
  56. bss = true;
  57. weaponInst.setChargedSpiritshot(L2ItemInstance.CHARGED_NONE);
  58. }
  59. else if (weaponInst.getChargedSpiritshot() == L2ItemInstance.CHARGED_SPIRITSHOT)
  60. {
  61. ss = true;
  62. weaponInst.setChargedSpiritshot(L2ItemInstance.CHARGED_NONE);
  63. }
  64. }
  65. // If there is no weapon equipped, check for an active summon.
  66. else if (activeChar instanceof L2Summon)
  67. {
  68. L2Summon activeSummon = (L2Summon)activeChar;
  69. if (activeSummon.getChargedSpiritShot() == L2ItemInstance.CHARGED_BLESSED_SPIRITSHOT)
  70. {
  71. bss = true;
  72. activeSummon.setChargedSpiritShot(L2ItemInstance.CHARGED_NONE);
  73. }
  74. else if (activeSummon.getChargedSpiritShot() == L2ItemInstance.CHARGED_SPIRITSHOT)
  75. {
  76. ss = true;
  77. activeSummon.setChargedSpiritShot(L2ItemInstance.CHARGED_NONE);
  78. }
  79. }
  80. else if (activeChar instanceof L2NpcInstance)
  81. {
  82. bss = ((L2NpcInstance)activeChar).isUsingShot(false);
  83. ss = ((L2NpcInstance)activeChar).isUsingShot(true);
  84. }
  85. boolean mcrit = Formulas.getInstance().calcMCrit(activeChar.getMCriticalHit(target, this));
  86. int damage = (int)Formulas.getInstance().calcMagicDam(
  87. activeChar, target, this, ss, bss, mcrit);
  88. int _drain = 0;
  89. int _cp = (int)target.getCurrentCp();
  90. int _hp = (int)target.getCurrentHp();
  91. if (_cp > 0)
  92. {
  93. if (damage < _cp)
  94. _drain = 0;
  95. else
  96. _drain = damage - _cp;
  97. }
  98. else if (damage > _hp)
  99. _drain = _hp;
  100. else
  101. _drain = damage;
  102. double hpAdd = _absorbAbs + _absorbPart * _drain;
  103. double hp = ((activeChar.getCurrentHp() + hpAdd) > activeChar.getMaxHp() ? activeChar.getMaxHp() : (activeChar.getCurrentHp() + hpAdd));
  104. activeChar.setCurrentHp(hp);
  105. StatusUpdate suhp = new StatusUpdate(activeChar.getObjectId());
  106. suhp.addAttribute(StatusUpdate.CUR_HP, (int)hp);
  107. activeChar.sendPacket(suhp);
  108. // Check to see if we should damage the target
  109. if (damage > 0 && (!target.isDead() || getTargetType() != SkillTargetType.TARGET_CORPSE_MOB))
  110. {
  111. // Manage attack or cast break of the target (calculating rate, sending message...)
  112. if (!target.isRaid() && Formulas.getInstance().calcAtkBreak(target, damage))
  113. {
  114. target.breakAttack();
  115. target.breakCast();
  116. }
  117. activeChar.sendDamageMessage(target, damage, mcrit, false, false);
  118. if (hasEffects() && getTargetType() != SkillTargetType.TARGET_CORPSE_MOB)
  119. {
  120. if (target.reflectSkill(this))
  121. {
  122. activeChar.stopSkillEffects(getId());
  123. getEffects(null, activeChar);
  124. SystemMessage sm = new SystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
  125. sm.addSkillName(getId());
  126. activeChar.sendPacket(sm);
  127. }
  128. else
  129. {
  130. // activate attacked effects, if any
  131. target.stopSkillEffects(getId());
  132. if (Formulas.getInstance().calcSkillSuccess(activeChar, target, this, false, ss, bss))
  133. getEffects(activeChar, target);
  134. else
  135. {
  136. SystemMessage sm = new SystemMessage(SystemMessageId.S1_WAS_UNAFFECTED_BY_S2);
  137. sm.addString(target.getName());
  138. sm.addSkillName(getDisplayId());
  139. activeChar.sendPacket(sm);
  140. }
  141. }
  142. }
  143. target.reduceCurrentHp(damage, activeChar);
  144. }
  145. // Check to see if we should do the decay right after the cast
  146. if (target.isDead() && getTargetType() == SkillTargetType.TARGET_CORPSE_MOB && target instanceof L2NpcInstance) {
  147. ((L2NpcInstance)target).endDecayTask();
  148. }
  149. }
  150. //effect self :]
  151. L2Effect effect = activeChar.getFirstEffect(getId());
  152. if (effect != null && effect.isSelfEffect())
  153. {
  154. //Replace old effect with new one.
  155. effect.exit();
  156. }
  157. // cast self effect if any
  158. getEffectsSelf(activeChar);
  159. }
  160. }