EffectCancel.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 java.util.logging.Logger;
  17. import com.l2jserver.Config;
  18. import com.l2jserver.gameserver.model.L2Effect;
  19. import com.l2jserver.gameserver.model.actor.L2Character;
  20. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  21. import com.l2jserver.gameserver.skills.Env;
  22. import com.l2jserver.gameserver.skills.Formulas;
  23. import com.l2jserver.gameserver.templates.effects.EffectTemplate;
  24. import com.l2jserver.gameserver.templates.skills.L2EffectType;
  25. import com.l2jserver.util.Rnd;
  26. import com.l2jserver.util.StringUtil;
  27. /**
  28. *
  29. * @author DS
  30. *
  31. */
  32. public class EffectCancel extends L2Effect
  33. {
  34. protected static final Logger _log = Logger.getLogger(EffectCancel.class.getName());
  35. public EffectCancel(Env env, EffectTemplate template)
  36. {
  37. super(env, template);
  38. }
  39. /**
  40. *
  41. * @see com.l2jserver.gameserver.model.L2Effect#getEffectType()
  42. */
  43. @Override
  44. public L2EffectType getEffectType()
  45. {
  46. return L2EffectType.CANCEL;
  47. }
  48. /**
  49. *
  50. * @see com.l2jserver.gameserver.model.L2Effect#onStart()
  51. */
  52. @Override
  53. public boolean onStart()
  54. {
  55. return cancel(getEffector(), getEffected(), this);
  56. }
  57. /**
  58. *
  59. * @see com.l2jserver.gameserver.model.L2Effect#onActionTime()
  60. */
  61. @Override
  62. public boolean onActionTime()
  63. {
  64. return false;
  65. }
  66. private static boolean cancel(L2Character caster, L2Character target, L2Effect effect)
  67. {
  68. if (!(target instanceof L2PcInstance)|| target.isDead())
  69. return false;
  70. final int cancelLvl = effect.getSkill().getMagicLevel();
  71. int count = effect.getSkill().getMaxNegatedEffects();
  72. double rate = effect.getEffectPower();
  73. final double vulnModifier = Formulas.calcSkillTypeVulnerability(0, target, effect.getSkillType());
  74. final double profModifier = Formulas.calcSkillTypeProficiency(0, caster, target, effect.getSkillType());
  75. double res = vulnModifier + profModifier;
  76. double resMod = 1;
  77. if (res != 0)
  78. {
  79. if (res < 0)
  80. {
  81. resMod = 1 - 0.075 * res;
  82. resMod = 1 / resMod;
  83. }
  84. else
  85. resMod = 1 + 0.02 * res;
  86. rate *= resMod;
  87. }
  88. if (caster.isDebug())
  89. {
  90. final StringBuilder stat = new StringBuilder(100);
  91. StringUtil.append(stat,
  92. effect.getSkill().getName(),
  93. " power:", String.valueOf((int)effect.getEffectPower()),
  94. " lvl:", String.valueOf(cancelLvl),
  95. " res:", String.format("%1.2f", resMod), "(",
  96. String.format("%1.2f", profModifier), "/",
  97. String.format("%1.2f", vulnModifier),
  98. ") total:", String.valueOf(rate)
  99. );
  100. final String result = stat.toString();
  101. if (caster.isDebug())
  102. caster.sendDebugMessage(result);
  103. if (Config.DEVELOPER)
  104. _log.info(result);
  105. }
  106. L2Effect eff;
  107. int lastCanceledSkillId = 0;
  108. final L2Effect[] effects = target.getAllEffects();
  109. for (int i = effects.length; --i >= 0;)
  110. {
  111. eff = effects[i];
  112. if (eff == null)
  113. continue;
  114. if (!eff.canBeStolen())
  115. {
  116. effects[i] = null;
  117. continue;
  118. }
  119. // first pass - dances/songs only
  120. if (!eff.getSkill().isDance())
  121. continue;
  122. if (eff.getSkill().getId() == lastCanceledSkillId)
  123. {
  124. eff.exit(); // this skill already canceled
  125. continue;
  126. }
  127. if (!calcCancelSuccess(eff, cancelLvl, (int)rate))
  128. continue;
  129. lastCanceledSkillId = eff.getSkill().getId();
  130. eff.exit();
  131. count--;
  132. if (count == 0)
  133. break;
  134. }
  135. if (count != 0)
  136. {
  137. lastCanceledSkillId = 0;
  138. for (int i = effects.length; --i >= 0;)
  139. {
  140. eff = effects[i];
  141. if (eff == null)
  142. continue;
  143. // second pass - all except dances/songs
  144. if (eff.getSkill().isDance())
  145. continue;
  146. if (eff.getSkill().getId() == lastCanceledSkillId)
  147. {
  148. eff.exit(); // this skill already canceled
  149. continue;
  150. }
  151. if (!calcCancelSuccess(eff, cancelLvl, (int)rate))
  152. continue;
  153. lastCanceledSkillId = eff.getSkill().getId();
  154. eff.exit();
  155. count--;
  156. if (count == 0)
  157. break;
  158. }
  159. }
  160. return true;
  161. }
  162. private static boolean calcCancelSuccess(L2Effect effect, int cancelLvl, int baseRate)
  163. {
  164. int rate = 2 * (cancelLvl - effect.getSkill().getMagicLevel());
  165. rate += effect.getAbnormalTime()/120;
  166. rate += baseRate;
  167. if (rate < 25)
  168. rate = 25;
  169. else if (rate > 75)
  170. rate = 75;
  171. return Rnd.get(100) < rate;
  172. }
  173. }