summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/v2/dice/CompoundingDie.java
diff options
context:
space:
mode:
Diffstat (limited to 'dice-lang/src/bjc/dicelang/v2/dice/CompoundingDie.java')
-rw-r--r--dice-lang/src/bjc/dicelang/v2/dice/CompoundingDie.java63
1 files changed, 63 insertions, 0 deletions
diff --git a/dice-lang/src/bjc/dicelang/v2/dice/CompoundingDie.java b/dice-lang/src/bjc/dicelang/v2/dice/CompoundingDie.java
new file mode 100644
index 0000000..d5991ae
--- /dev/null
+++ b/dice-lang/src/bjc/dicelang/v2/dice/CompoundingDie.java
@@ -0,0 +1,63 @@
+package bjc.dicelang.v2.dice;
+
+import java.util.function.Predicate;
+
+public class CompoundingDie implements Die {
+ private Die source;
+
+ private Predicate<Long> compoundOn;
+ private String compoundPattern;
+
+ public CompoundingDie(Die src, Predicate<Long> compound) {
+ this(src, compound, null);
+ }
+
+ public CompoundingDie(Die src, Predicate<Long> compound, String patt) {
+ source = src;
+
+ compoundOn = compound;
+ compoundPattern = patt;
+ }
+
+ public boolean canOptimize() {
+ return source.canOptimize() && source.optimize() == 0;
+ }
+
+ public long optimize() {
+ return 0;
+ }
+
+ public long roll() {
+ long res = source.roll();
+ long oldRes = res;
+
+ while(compoundOn.test(oldRes)) {
+ oldRes = source.rollSingle();
+
+ res += oldRes;
+ }
+
+ return res;
+ }
+
+ public long rollSingle() {
+ long res = source.rollSingle();
+ long oldRes = res;
+
+ while(compoundOn.test(oldRes)) {
+ oldRes = source.rollSingle();
+
+ res += oldRes;
+ }
+
+ return res;
+ }
+
+ public String toString() {
+ if(compoundPattern == null) {
+ return source + "!!";
+ } else {
+ return source + "!!" + compoundPattern;
+ }
+ }
+} \ No newline at end of file