Cancel.java 5.4 KB

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