CancelDebuff.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 handlers.effecthandlers;
  16. import com.l2jserver.Config;
  17. import com.l2jserver.gameserver.model.actor.L2Character;
  18. import com.l2jserver.gameserver.model.effects.EffectTemplate;
  19. import com.l2jserver.gameserver.model.effects.L2Effect;
  20. import com.l2jserver.gameserver.model.effects.L2EffectType;
  21. import com.l2jserver.gameserver.model.skills.L2Skill;
  22. import com.l2jserver.gameserver.model.stats.Env;
  23. import com.l2jserver.util.Rnd;
  24. /**
  25. * @author UnAfraid
  26. */
  27. public class CancelDebuff extends L2Effect
  28. {
  29. public CancelDebuff(Env env, EffectTemplate template)
  30. {
  31. super(env, template);
  32. }
  33. @Override
  34. public L2EffectType getEffectType()
  35. {
  36. return L2EffectType.CANCEL_DEBUFF;
  37. }
  38. @Override
  39. public boolean onStart()
  40. {
  41. return cancel(getEffector(), getEffected(), getSkill(), getEffectPower());
  42. }
  43. @Override
  44. public boolean onActionTime()
  45. {
  46. return false;
  47. }
  48. private static boolean cancel(L2Character caster, L2Character target, L2Skill skill, double baseRate)
  49. {
  50. if (target.isDead())
  51. {
  52. return false;
  53. }
  54. final int cancelLvl = skill.getMagicLevel();
  55. int count = skill.getMaxNegatedEffects();
  56. L2Effect effect;
  57. int lastCanceledSkillId = 0;
  58. final L2Effect[] effects = target.getAllEffects();
  59. for (int i = effects.length; --i >= 0;)
  60. {
  61. effect = effects[i];
  62. if (effect == null)
  63. {
  64. continue;
  65. }
  66. if (!effect.getSkill().isDebuff() || !effect.getSkill().canBeDispeled())
  67. {
  68. effects[i] = null;
  69. continue;
  70. }
  71. if (effect.getSkill().getId() == lastCanceledSkillId)
  72. {
  73. effect.exit(); // this skill already canceled
  74. continue;
  75. }
  76. if (!calcCancelSuccess(effect, cancelLvl, (int) baseRate))
  77. {
  78. continue;
  79. }
  80. lastCanceledSkillId = effect.getSkill().getId();
  81. effect.exit();
  82. count--;
  83. if (count == 0)
  84. {
  85. break;
  86. }
  87. }
  88. return true;
  89. }
  90. private static boolean calcCancelSuccess(L2Effect effect, int cancelLvl, int baseRate)
  91. {
  92. int rate = 2 * (cancelLvl - effect.getSkill().getMagicLevel());
  93. rate += (effect.getAbnormalTime() - effect.getTime()) / 1200;
  94. rate += baseRate;
  95. if (rate < Config.MIN_DEBUFF_CHANCE)
  96. {
  97. rate = Config.MIN_DEBUFF_CHANCE;
  98. }
  99. else if (rate > Config.MAX_DEBUFF_CHANCE)
  100. {
  101. rate = Config.MAX_DEBUFF_CHANCE;
  102. }
  103. return Rnd.get(100) < rate;
  104. }
  105. }