ChanceCondition.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 enum TriggerType
  38. {
  39. // You hit an enemy
  40. ON_HIT(1),
  41. // You hit an enemy - was crit
  42. ON_CRIT(2),
  43. // You cast a skill
  44. ON_CAST(4),
  45. // You cast a skill - it was a physical one
  46. ON_PHYSICAL(8),
  47. // You cast a skill - it was a magic one
  48. ON_MAGIC(16),
  49. // You cast a skill - it was a magic one - good magic
  50. ON_MAGIC_GOOD(32),
  51. // You cast a skill - it was a magic one - offensive magic
  52. ON_MAGIC_OFFENSIVE(64),
  53. // You are attacked by enemy
  54. ON_ATTACKED(128),
  55. // You are attacked by enemy - by hit
  56. ON_ATTACKED_HIT(256),
  57. // You are attacked by enemy - by hit - was crit
  58. ON_ATTACKED_CRIT(512),
  59. // A skill was casted on you
  60. ON_HIT_BY_SKILL(1024),
  61. // An evil skill was casted on you
  62. ON_HIT_BY_OFFENSIVE_SKILL(2048),
  63. // A good skill was casted on you
  64. ON_HIT_BY_GOOD_MAGIC(4096);
  65. private int _mask;
  66. private TriggerType(int mask)
  67. {
  68. _mask = mask;
  69. }
  70. public boolean check(int event)
  71. {
  72. return (_mask & event) != 0; // Trigger (sub-)type contains event (sub-)type
  73. }
  74. }
  75. private TriggerType _triggerType;
  76. private int _chance;
  77. private ChanceCondition(TriggerType trigger, int chance)
  78. {
  79. _triggerType = trigger;
  80. _chance = chance;
  81. }
  82. public static ChanceCondition parse(StatsSet set)
  83. {
  84. try
  85. {
  86. TriggerType trigger = set.getEnum("chanceType", TriggerType.class, null);
  87. int chance = set.getInteger("activationChance", 0);
  88. if (trigger != null && chance > 0)
  89. return new ChanceCondition(trigger, chance);
  90. }
  91. catch (Exception e)
  92. {}
  93. return null;
  94. }
  95. public boolean trigger(int event)
  96. {
  97. return _triggerType.check(event) && Rnd.get(100) < _chance;
  98. }
  99. @Override
  100. public String toString()
  101. {
  102. return "Trigger["+_chance+";"+_triggerType.toString()+"]";
  103. }
  104. }