ChanceCondition.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 boolean _pvpOnly;
  96. private ChanceCondition(TriggerType trigger, int chance, int mindmg, byte[] elements, boolean pvpOnly)
  97. {
  98. _triggerType = trigger;
  99. _chance = chance;
  100. _mindmg = mindmg;
  101. _elements = elements;
  102. _pvpOnly = pvpOnly;
  103. }
  104. public static ChanceCondition parse(StatsSet set)
  105. {
  106. try
  107. {
  108. TriggerType trigger = set.getEnum("chanceType", TriggerType.class, null);
  109. int chance = set.getInteger("activationChance", -1);
  110. int mindmg = set.getInteger("activationMinDamage", -1);
  111. String elements = set.getString("activationElements", null);
  112. boolean pvpOnly = set.getBool("pvpChanceOnly", false);
  113. if (trigger != null)
  114. return new ChanceCondition(trigger, chance, mindmg, parseElements(elements), pvpOnly);
  115. }
  116. catch (Exception e)
  117. {
  118. _log.log(Level.WARNING, "", e);
  119. }
  120. return null;
  121. }
  122. public static ChanceCondition parse(String chanceType, int chance, int mindmg, String elements, boolean pvpOnly)
  123. {
  124. try
  125. {
  126. if (chanceType == null)
  127. return null;
  128. TriggerType trigger = Enum.valueOf(TriggerType.class, chanceType);
  129. if (trigger != null)
  130. return new ChanceCondition(trigger, chance, mindmg, parseElements(elements), pvpOnly);
  131. }
  132. catch (Exception e)
  133. {
  134. _log.log(Level.WARNING, "", e);
  135. }
  136. return null;
  137. }
  138. public static final byte[] parseElements(String list)
  139. {
  140. if (list == null)
  141. return null;
  142. String[] valuesSplit = list.split(",");
  143. byte[] elements = new byte[valuesSplit.length];
  144. for (int i = 0; i < valuesSplit.length; i++)
  145. elements[i] = Byte.parseByte(valuesSplit[i]);
  146. Arrays.sort(elements);
  147. return elements;
  148. }
  149. public boolean trigger(int event, int damage, byte element, boolean playable)
  150. {
  151. if (_pvpOnly && !playable)
  152. return false;
  153. if (_elements != null && Arrays.binarySearch(_elements, element) < 0)
  154. return false;
  155. // if the skill has "activationMinDamage" setted to higher than -1(default)
  156. // and if "activationMinDamage" is still higher than the recieved damage, the skill wont trigger
  157. if (_mindmg > -1 && _mindmg > damage)
  158. return false;
  159. return _triggerType.check(event) && (_chance < 0 || Rnd.get(100) < _chance);
  160. }
  161. @Override
  162. public String toString()
  163. {
  164. return "Trigger["+_chance+";"+_triggerType.toString()+"]";
  165. }
  166. }