ChanceCondition.java 6.3 KB

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