EffectCancelDebuff.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.effects;
  16. import com.l2jserver.gameserver.model.L2Effect;
  17. import com.l2jserver.gameserver.model.L2Skill;
  18. import com.l2jserver.gameserver.model.actor.L2Character;
  19. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  20. import com.l2jserver.gameserver.skills.Env;
  21. import com.l2jserver.gameserver.skills.Formulas;
  22. import com.l2jserver.gameserver.templates.effects.EffectTemplate;
  23. import com.l2jserver.gameserver.templates.skills.L2EffectType;
  24. import com.l2jserver.gameserver.templates.skills.L2SkillType;
  25. import com.l2jserver.util.Rnd;
  26. /**
  27. *
  28. * @author UnAfraid
  29. *
  30. */
  31. public class EffectCancelDebuff extends L2Effect
  32. {
  33. public EffectCancelDebuff(Env env, EffectTemplate template)
  34. {
  35. super(env, template);
  36. }
  37. /**
  38. *
  39. * @see com.l2jserver.gameserver.model.L2Effect#getEffectType()
  40. */
  41. @Override
  42. public L2EffectType getEffectType()
  43. {
  44. return L2EffectType.CANCEL_DEBUFF;
  45. }
  46. /**
  47. *
  48. * @see com.l2jserver.gameserver.model.L2Effect#onStart()
  49. */
  50. @Override
  51. public boolean onStart()
  52. {
  53. return cancel(getEffector(), getEffected(), getSkill(), getEffectTemplate().effectType);
  54. }
  55. /**
  56. *
  57. * @see com.l2jserver.gameserver.model.L2Effect#onActionTime()
  58. */
  59. @Override
  60. public boolean onActionTime()
  61. {
  62. return false;
  63. }
  64. private static boolean cancel(L2Character caster, L2Character target, L2Skill skill, L2SkillType effectType)
  65. {
  66. if (!(target instanceof L2PcInstance)|| target.isDead())
  67. return false;
  68. final int cancelLvl = skill.getMagicLevel();
  69. int count = skill.getMaxNegatedEffects();
  70. double baseRate = Formulas.calcSkillTypeProficiency(skill.getPower(), caster, target, effectType);
  71. baseRate += Formulas.calcSkillTypeVulnerability(baseRate, caster, effectType);
  72. L2Effect effect;
  73. int lastCanceledSkillId = 0;
  74. final L2Effect[] effects = target.getAllEffects();
  75. for (int i = effects.length; --i >= 0;)
  76. {
  77. effect = effects[i];
  78. if (effect == null)
  79. continue;
  80. if (!effect.getSkill().isDebuff() || !effect.getSkill().canBeDispeled())
  81. {
  82. effects[i] = null;
  83. continue;
  84. }
  85. if (effect.getSkill().getId() == lastCanceledSkillId)
  86. {
  87. effect.exit(); // this skill already canceled
  88. continue;
  89. }
  90. if (!calcCancelSuccess(effect, cancelLvl, (int)baseRate))
  91. continue;
  92. lastCanceledSkillId = effect.getSkill().getId();
  93. effect.exit();
  94. count--;
  95. if (count == 0)
  96. break;
  97. }
  98. if (count != 0)
  99. {
  100. lastCanceledSkillId = 0;
  101. for (int i = effects.length; --i >= 0;)
  102. {
  103. effect = effects[i];
  104. if (effect == null)
  105. continue;
  106. if (!effect.getSkill().isDebuff() || !effect.getSkill().canBeDispeled())
  107. {
  108. effects[i] = null;
  109. continue;
  110. }
  111. if (effect.getSkill().getId() == lastCanceledSkillId)
  112. {
  113. effect.exit(); // this skill already canceled
  114. continue;
  115. }
  116. if (!calcCancelSuccess(effect, cancelLvl, (int)baseRate))
  117. continue;
  118. lastCanceledSkillId = effect.getSkill().getId();
  119. effect.exit();
  120. count--;
  121. if (count == 0)
  122. break;
  123. }
  124. }
  125. return true;
  126. }
  127. private static boolean calcCancelSuccess(L2Effect effect, int cancelLvl, int baseRate)
  128. {
  129. int rate = 2 * (cancelLvl - effect.getSkill().getMagicLevel());
  130. rate += (effect.getAbnormalTime() - effect.getTime()) / 1200;
  131. rate += baseRate;
  132. if (rate < 25)
  133. rate = 25;
  134. else if (rate > 75)
  135. rate = 75;
  136. return Rnd.get(100) < rate;
  137. }
  138. }