ChanceCondition.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model;
  20. import java.util.Arrays;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.gameserver.enums.TriggerType;
  24. import com.l2jserver.gameserver.model.skills.L2Skill;
  25. import com.l2jserver.util.Rnd;
  26. /**
  27. * @author kombat
  28. */
  29. public final class ChanceCondition
  30. {
  31. private static final Logger _log = Logger.getLogger(ChanceCondition.class.getName());
  32. public static final int EVT_HIT = 1;
  33. public static final int EVT_CRIT = 2;
  34. public static final int EVT_CAST = 4;
  35. public static final int EVT_PHYSICAL = 8;
  36. public static final int EVT_MAGIC = 16;
  37. public static final int EVT_MAGIC_GOOD = 32;
  38. public static final int EVT_MAGIC_OFFENSIVE = 64;
  39. public static final int EVT_ATTACKED = 128;
  40. public static final int EVT_ATTACKED_HIT = 256;
  41. public static final int EVT_ATTACKED_CRIT = 512;
  42. public static final int EVT_HIT_BY_SKILL = 1024;
  43. public static final int EVT_HIT_BY_OFFENSIVE_SKILL = 2048;
  44. public static final int EVT_HIT_BY_GOOD_MAGIC = 4096;
  45. public static final int EVT_EVADED_HIT = 8192;
  46. public static final int EVT_ON_START = 16384;
  47. public static final int EVT_ON_ACTION_TIME = 32768;
  48. public static final int EVT_ON_EXIT = 65536;
  49. private final TriggerType _triggerType;
  50. private final int _chance;
  51. private final int _mindmg;
  52. private final byte[] _elements;
  53. private final int[] _activationSkills;
  54. private final boolean _pvpOnly;
  55. private ChanceCondition(TriggerType trigger, int chance, int mindmg, byte[] elements, int[] activationSkills, boolean pvpOnly)
  56. {
  57. _triggerType = trigger;
  58. _chance = chance;
  59. _mindmg = mindmg;
  60. _elements = elements;
  61. _pvpOnly = pvpOnly;
  62. _activationSkills = activationSkills;
  63. }
  64. public static ChanceCondition parse(StatsSet set)
  65. {
  66. try
  67. {
  68. TriggerType trigger = set.getEnum("chanceType", TriggerType.class, null);
  69. int chance = set.getInt("activationChance", -1);
  70. int mindmg = set.getInt("activationMinDamage", -1);
  71. String elements = set.getString("activationElements", null);
  72. String activationSkills = set.getString("activationSkills", null);
  73. boolean pvpOnly = set.getBoolean("pvpChanceOnly", false);
  74. if (trigger != null)
  75. {
  76. return new ChanceCondition(trigger, chance, mindmg, parseElements(elements), parseActivationSkills(activationSkills), pvpOnly);
  77. }
  78. }
  79. catch (Exception e)
  80. {
  81. _log.log(Level.WARNING, "", e);
  82. }
  83. return null;
  84. }
  85. public static ChanceCondition parse(String chanceType, int chance, int mindmg, String elements, String activationSkills, boolean pvpOnly)
  86. {
  87. try
  88. {
  89. if (chanceType == null)
  90. {
  91. return null;
  92. }
  93. TriggerType trigger = Enum.valueOf(TriggerType.class, chanceType);
  94. if (trigger != null)
  95. {
  96. return new ChanceCondition(trigger, chance, mindmg, parseElements(elements), parseActivationSkills(activationSkills), pvpOnly);
  97. }
  98. }
  99. catch (Exception e)
  100. {
  101. _log.log(Level.WARNING, "", e);
  102. }
  103. return null;
  104. }
  105. public static final byte[] parseElements(String list)
  106. {
  107. if (list == null)
  108. {
  109. return null;
  110. }
  111. String[] valuesSplit = list.split(",");
  112. byte[] elements = new byte[valuesSplit.length];
  113. for (int i = 0; i < valuesSplit.length; i++)
  114. {
  115. elements[i] = Byte.parseByte(valuesSplit[i]);
  116. }
  117. Arrays.sort(elements);
  118. return elements;
  119. }
  120. public static final int[] parseActivationSkills(String list)
  121. {
  122. if (list == null)
  123. {
  124. return null;
  125. }
  126. String[] valuesSplit = list.split(",");
  127. int[] skillIds = new int[valuesSplit.length];
  128. for (int i = 0; i < valuesSplit.length; i++)
  129. {
  130. skillIds[i] = Integer.parseInt(valuesSplit[i]);
  131. }
  132. return skillIds;
  133. }
  134. public boolean trigger(int event, int damage, byte element, boolean playable, L2Skill skill)
  135. {
  136. if (_pvpOnly && !playable)
  137. {
  138. return false;
  139. }
  140. if ((_elements != null) && (Arrays.binarySearch(_elements, element) < 0))
  141. {
  142. return false;
  143. }
  144. if ((_activationSkills != null) && (skill != null) && (Arrays.binarySearch(_activationSkills, skill.getId()) < 0))
  145. {
  146. return false;
  147. }
  148. // if the skill has "activationMinDamage" setted to higher than -1(default)
  149. // and if "activationMinDamage" is still higher than the recieved damage, the skill wont trigger
  150. if ((_mindmg > -1) && (_mindmg > damage))
  151. {
  152. return false;
  153. }
  154. return _triggerType.check(event) && ((_chance < 0) || (Rnd.get(100) < _chance));
  155. }
  156. @Override
  157. public String toString()
  158. {
  159. return "Trigger[" + _chance + ";" + _triggerType.toString() + "]";
  160. }
  161. }