Cancel.java 5.3 KB

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