ChanceCondition.java 5.0 KB

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