ChanceCondition.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.model;
  16. import java.util.Arrays;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import com.l2jserver.util.Rnd;
  20. /**
  21. *
  22. * @author kombat
  23. */
  24. public final class ChanceCondition
  25. {
  26. protected static final Logger _log = Logger.getLogger(ChanceCondition.class.getName());
  27. public static final int EVT_HIT = 1;
  28. public static final int EVT_CRIT = 2;
  29. public static final int EVT_CAST = 4;
  30. public static final int EVT_PHYSICAL = 8;
  31. public static final int EVT_MAGIC = 16;
  32. public static final int EVT_MAGIC_GOOD = 32;
  33. public static final int EVT_MAGIC_OFFENSIVE = 64;
  34. public static final int EVT_ATTACKED = 128;
  35. public static final int EVT_ATTACKED_HIT = 256;
  36. public static final int EVT_ATTACKED_CRIT = 512;
  37. public static final int EVT_HIT_BY_SKILL = 1024;
  38. public static final int EVT_HIT_BY_OFFENSIVE_SKILL = 2048;
  39. public static final int EVT_HIT_BY_GOOD_MAGIC = 4096;
  40. public static final int EVT_EVADED_HIT = 8192;
  41. public static final int EVT_ON_START = 16384;
  42. public static final int EVT_ON_ACTION_TIME = 32768;
  43. public static final int EVT_ON_EXIT = 65536;
  44. public static enum TriggerType
  45. {
  46. // You hit an enemy
  47. ON_HIT(1),
  48. // You hit an enemy - was crit
  49. ON_CRIT(2),
  50. // You cast a skill
  51. ON_CAST(4),
  52. // You cast a skill - it was a physical one
  53. ON_PHYSICAL(8),
  54. // You cast a skill - it was a magic one
  55. ON_MAGIC(16),
  56. // You cast a skill - it was a magic one - good magic
  57. ON_MAGIC_GOOD(32),
  58. // You cast a skill - it was a magic one - offensive magic
  59. ON_MAGIC_OFFENSIVE(64),
  60. // You are attacked by enemy
  61. ON_ATTACKED(128),
  62. // You are attacked by enemy - by hit
  63. ON_ATTACKED_HIT(256),
  64. // You are attacked by enemy - by hit - was crit
  65. ON_ATTACKED_CRIT(512),
  66. // A skill was casted on you
  67. ON_HIT_BY_SKILL(1024),
  68. // An evil skill was casted on you
  69. ON_HIT_BY_OFFENSIVE_SKILL(2048),
  70. // A good skill was casted on you
  71. ON_HIT_BY_GOOD_MAGIC(4096),
  72. // Evading melee attack
  73. ON_EVADED_HIT(8192),
  74. // Effect only - on start
  75. ON_START(16384),
  76. // Effect only - each second
  77. ON_ACTION_TIME(32768),
  78. // Effect only - on exit
  79. ON_EXIT(65536);
  80. private final int _mask;
  81. private TriggerType(int mask)
  82. {
  83. _mask = mask;
  84. }
  85. public final boolean check(int event)
  86. {
  87. return (_mask & event) != 0; // Trigger (sub-)type contains event (sub-)type
  88. }
  89. }
  90. private final TriggerType _triggerType;
  91. private final int _chance;
  92. private final int _mindmg;
  93. private final byte[] _elements;
  94. private final int[] _activationSkills;
  95. private final boolean _pvpOnly;
  96. private ChanceCondition(TriggerType trigger, int chance, int mindmg, byte[] elements, int[] activationSkills, boolean pvpOnly)
  97. {
  98. _triggerType = trigger;
  99. _chance = chance;
  100. _mindmg = mindmg;
  101. _elements = elements;
  102. _pvpOnly = pvpOnly;
  103. _activationSkills = activationSkills;
  104. }
  105. public static ChanceCondition parse(StatsSet set)
  106. {
  107. try
  108. {
  109. TriggerType trigger = set.getEnum("chanceType", TriggerType.class, null);
  110. int chance = set.getInteger("activationChance", -1);
  111. int mindmg = set.getInteger("activationMinDamage", -1);
  112. String elements = set.getString("activationElements", null);
  113. String activationSkills = set.getString("activationSkills", null);
  114. boolean pvpOnly = set.getBool("pvpChanceOnly", false);
  115. if (trigger != null)
  116. return new ChanceCondition(trigger, chance, mindmg, parseElements(elements), parseActivationSkills(activationSkills), pvpOnly);
  117. }
  118. catch (Exception e)
  119. {
  120. _log.log(Level.WARNING, "", e);
  121. }
  122. return null;
  123. }
  124. public static ChanceCondition parse(String chanceType, int chance, int mindmg, String elements, String activationSkills, boolean pvpOnly)
  125. {
  126. try
  127. {
  128. if (chanceType == null)
  129. return null;
  130. TriggerType trigger = Enum.valueOf(TriggerType.class, chanceType);
  131. if (trigger != null)
  132. return new ChanceCondition(trigger, chance, mindmg, parseElements(elements), parseActivationSkills(activationSkills), pvpOnly);
  133. }
  134. catch (Exception e)
  135. {
  136. _log.log(Level.WARNING, "", e);
  137. }
  138. return null;
  139. }
  140. public static final byte[] parseElements(String list)
  141. {
  142. if (list == null)
  143. return null;
  144. String[] valuesSplit = list.split(",");
  145. byte[] elements = new byte[valuesSplit.length];
  146. for (int i = 0; i < valuesSplit.length; i++)
  147. elements[i] = Byte.parseByte(valuesSplit[i]);
  148. Arrays.sort(elements);
  149. return elements;
  150. }
  151. public static final int[] parseActivationSkills(String list)
  152. {
  153. if (list == null)
  154. return null;
  155. String[] valuesSplit = list.split(",");
  156. int[] skillIds = new int[valuesSplit.length];
  157. for (int i = 0; i < valuesSplit.length; i++)
  158. skillIds[i] = Integer.parseInt(valuesSplit[i]);
  159. return skillIds;
  160. }
  161. public boolean trigger(int event, int damage, byte element, boolean playable, L2Skill skill)
  162. {
  163. if (_pvpOnly && !playable)
  164. return false;
  165. if (_elements != null && Arrays.binarySearch(_elements, element) < 0)
  166. return false;
  167. if (_activationSkills != null && skill != null && Arrays.binarySearch(_activationSkills, skill.getId()) < 0)
  168. return false;
  169. // if the skill has "activationMinDamage" setted to higher than -1(default)
  170. // and if "activationMinDamage" is still higher than the recieved damage, the skill wont trigger
  171. if (_mindmg > -1 && _mindmg > damage)
  172. return false;
  173. return _triggerType.check(event) && (_chance < 0 || Rnd.get(100) < _chance);
  174. }
  175. @Override
  176. public String toString()
  177. {
  178. return "Trigger["+_chance+";"+_triggerType.toString()+"]";
  179. }
  180. }