ChanceCondition.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 net.sf.l2j.gameserver.model;
  16. import net.sf.l2j.gameserver.templates.StatsSet;
  17. import net.sf.l2j.util.Rnd;
  18. /**
  19. *
  20. * @author kombat
  21. */
  22. public final class ChanceCondition
  23. {
  24. public static final int EVT_HIT = 1;
  25. public static final int EVT_CRIT = 2;
  26. public static final int EVT_CAST = 4;
  27. public static final int EVT_PHYSICAL = 8;
  28. public static final int EVT_MAGIC = 16;
  29. public static final int EVT_MAGIC_GOOD = 32;
  30. public static final int EVT_MAGIC_OFFENSIVE = 64;
  31. public static final int EVT_ATTACKED = 128;
  32. public static final int EVT_ATTACKED_HIT = 256;
  33. public static final int EVT_ATTACKED_CRIT = 512;
  34. public static final int EVT_HIT_BY_SKILL = 1024;
  35. public static final int EVT_HIT_BY_OFFENSIVE_SKILL = 2048;
  36. public static final int EVT_HIT_BY_GOOD_MAGIC = 4096;
  37. public static final int EVT_EVADED_HIT = 8192;
  38. public static enum TriggerType
  39. {
  40. // You hit an enemy
  41. ON_HIT(1),
  42. // You hit an enemy - was crit
  43. ON_CRIT(2),
  44. // You cast a skill
  45. ON_CAST(4),
  46. // You cast a skill - it was a physical one
  47. ON_PHYSICAL(8),
  48. // You cast a skill - it was a magic one
  49. ON_MAGIC(16),
  50. // You cast a skill - it was a magic one - good magic
  51. ON_MAGIC_GOOD(32),
  52. // You cast a skill - it was a magic one - offensive magic
  53. ON_MAGIC_OFFENSIVE(64),
  54. // You are attacked by enemy
  55. ON_ATTACKED(128),
  56. // You are attacked by enemy - by hit
  57. ON_ATTACKED_HIT(256),
  58. // You are attacked by enemy - by hit - was crit
  59. ON_ATTACKED_CRIT(512),
  60. // A skill was casted on you
  61. ON_HIT_BY_SKILL(1024),
  62. // An evil skill was casted on you
  63. ON_HIT_BY_OFFENSIVE_SKILL(2048),
  64. // A good skill was casted on you
  65. ON_HIT_BY_GOOD_MAGIC(4096),
  66. // Evading melee attack
  67. ON_EVADED_HIT(8192);
  68. private int _mask;
  69. private TriggerType(int mask)
  70. {
  71. _mask = mask;
  72. }
  73. public boolean check(int event)
  74. {
  75. return (_mask & event) != 0; // Trigger (sub-)type contains event (sub-)type
  76. }
  77. }
  78. private TriggerType _triggerType;
  79. private int _chance;
  80. private ChanceCondition(TriggerType trigger, int chance)
  81. {
  82. _triggerType = trigger;
  83. _chance = chance;
  84. }
  85. public static ChanceCondition parse(StatsSet set)
  86. {
  87. try
  88. {
  89. TriggerType trigger = set.getEnum("chanceType", TriggerType.class, null);
  90. int chance = set.getInteger("activationChance", 0);
  91. if (trigger != null && chance > 0)
  92. return new ChanceCondition(trigger, chance);
  93. }
  94. catch (Exception e)
  95. {}
  96. return null;
  97. }
  98. public static ChanceCondition parse(String chanceType, int chance)
  99. {
  100. try
  101. {
  102. if (chance <= 0 || chanceType == null)
  103. return null;
  104. TriggerType trigger = Enum.valueOf(TriggerType.class, chanceType);
  105. if (trigger != null)
  106. return new ChanceCondition(trigger, chance);
  107. }
  108. catch (Exception e)
  109. {
  110. e.printStackTrace();
  111. }
  112. return null;
  113. }
  114. public boolean trigger(int event)
  115. {
  116. return _triggerType.check(event) && Rnd.get(100) < _chance;
  117. }
  118. @Override
  119. public String toString()
  120. {
  121. return "Trigger["+_chance+";"+_triggerType.toString()+"]";
  122. }
  123. }