ChanceCondition.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 enum TriggerType
  40. {
  41. // You hit an enemy
  42. ON_HIT(1),
  43. // You hit an enemy - was crit
  44. ON_CRIT(2),
  45. // You cast a skill
  46. ON_CAST(4),
  47. // You cast a skill - it was a physical one
  48. ON_PHYSICAL(8),
  49. // You cast a skill - it was a magic one
  50. ON_MAGIC(16),
  51. // You cast a skill - it was a magic one - good magic
  52. ON_MAGIC_GOOD(32),
  53. // You cast a skill - it was a magic one - offensive magic
  54. ON_MAGIC_OFFENSIVE(64),
  55. // You are attacked by enemy
  56. ON_ATTACKED(128),
  57. // You are attacked by enemy - by hit
  58. ON_ATTACKED_HIT(256),
  59. // You are attacked by enemy - by hit - was crit
  60. ON_ATTACKED_CRIT(512),
  61. // A skill was casted on you
  62. ON_HIT_BY_SKILL(1024),
  63. // An evil skill was casted on you
  64. ON_HIT_BY_OFFENSIVE_SKILL(2048),
  65. // A good skill was casted on you
  66. ON_HIT_BY_GOOD_MAGIC(4096),
  67. // Evading melee attack
  68. ON_EVADED_HIT(8192);
  69. private final int _mask;
  70. private TriggerType(int mask)
  71. {
  72. _mask = mask;
  73. }
  74. public final boolean check(int event)
  75. {
  76. return (_mask & event) != 0; // Trigger (sub-)type contains event (sub-)type
  77. }
  78. }
  79. private final TriggerType _triggerType;
  80. private final int _chance;
  81. private final byte[] _elements;
  82. private final boolean _pvpOnly;
  83. private ChanceCondition(TriggerType trigger, int chance, byte[] elements, boolean pvpOnly)
  84. {
  85. _triggerType = trigger;
  86. _chance = chance;
  87. _elements = elements;
  88. _pvpOnly = pvpOnly;
  89. }
  90. public static ChanceCondition parse(StatsSet set)
  91. {
  92. try
  93. {
  94. TriggerType trigger = set.getEnum("chanceType", TriggerType.class, null);
  95. int chance = set.getInteger("activationChance", 0);
  96. String elements = set.getString("activationElements", null);
  97. boolean pvpOnly = set.getBool("pvpChanceOnly", false);
  98. if (trigger != null && chance > 0)
  99. return new ChanceCondition(trigger, chance, parseElements(elements), pvpOnly);
  100. }
  101. catch (Exception e)
  102. {
  103. e.printStackTrace();
  104. }
  105. return null;
  106. }
  107. public static ChanceCondition parse(String chanceType, int chance, String elements, boolean pvpOnly)
  108. {
  109. try
  110. {
  111. if (chance <= 0 || chanceType == null)
  112. return null;
  113. TriggerType trigger = Enum.valueOf(TriggerType.class, chanceType);
  114. if (trigger != null)
  115. return new ChanceCondition(trigger, chance, parseElements(elements), pvpOnly);
  116. }
  117. catch (Exception e)
  118. {
  119. e.printStackTrace();
  120. }
  121. return null;
  122. }
  123. public static final byte[] parseElements(String list)
  124. {
  125. if (list == null)
  126. return null;
  127. String[] valuesSplit = list.split(",");
  128. byte[] elements = new byte[valuesSplit.length];
  129. for (int i = 0; i < valuesSplit.length; i++)
  130. elements[i] = Byte.parseByte(valuesSplit[i]);
  131. Arrays.sort(elements);
  132. return elements;
  133. }
  134. public boolean trigger(int event, byte element, boolean playable)
  135. {
  136. if (_pvpOnly && !playable)
  137. return false;
  138. if (_elements != null && Arrays.binarySearch(_elements, element) < 0)
  139. return false;
  140. return _triggerType.check(event) && Rnd.get(100) < _chance;
  141. }
  142. @Override
  143. public String toString()
  144. {
  145. return "Trigger["+_chance+";"+_triggerType.toString()+"]";
  146. }
  147. }