CancelDebuff.java 3.0 KB

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