EffectCancelDebuff.java 3.8 KB

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