ChanceCondition.java 6.5 KB

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