summaryrefslogtreecommitdiff
path: root/dice/src/main/java/bjc/dicelang/dicev2/CompoundDieMod.java
diff options
context:
space:
mode:
authorBenjamin J. Culkin <bjculkin@mix.wvu.edu>2018-06-02 17:32:24 -0300
committerBenjamin J. Culkin <bjculkin@mix.wvu.edu>2018-06-02 17:32:24 -0300
commit775394cf6b4a3c6a7e7345affeb45bb34f6e4517 (patch)
treea193472620f796b3701fa979cba84d4361eef0c0 /dice/src/main/java/bjc/dicelang/dicev2/CompoundDieMod.java
parent09f161528619f9396aa07c784c15bc1d33f5c047 (diff)
Finish die rewrite
This adds all of the remaining missing die modifiers, as well as a few base die types that were missing.
Diffstat (limited to 'dice/src/main/java/bjc/dicelang/dicev2/CompoundDieMod.java')
-rw-r--r--dice/src/main/java/bjc/dicelang/dicev2/CompoundDieMod.java80
1 files changed, 80 insertions, 0 deletions
diff --git a/dice/src/main/java/bjc/dicelang/dicev2/CompoundDieMod.java b/dice/src/main/java/bjc/dicelang/dicev2/CompoundDieMod.java
new file mode 100644
index 0000000..6b752f2
--- /dev/null
+++ b/dice/src/main/java/bjc/dicelang/dicev2/CompoundDieMod.java
@@ -0,0 +1,80 @@
+package bjc.dicelang.dicev2;
+
+import bjc.utils.funcutils.ListUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.LongPredicate;
+
+public class CompoundDieMod extends Die {
+ public final Die[] dice;
+
+ public final LongPredicate compound;
+
+ public final boolean penetrate;
+
+ public CompoundDieMod(LongPredicate compound, Die... dice) {
+ this(compound, false, dice);
+ }
+
+ public CompoundDieMod(LongPredicate compound, boolean penetrate, Die... dice) {
+ super();
+
+ this.dice = dice;
+
+ this.compound = compound;
+
+ this.penetrate = penetrate;
+ }
+
+ public long[] roll() {
+ List<Long> lst = new ArrayList<>(5);
+
+ for(Die die : dice) {
+ for(long val : die.roll()) {
+ long res = val;
+
+ long newVal = die.rollSingle();
+
+ while(compound.test(newVal)) {
+ if(penetrate) newVal -= 1;
+
+ res += newVal;
+
+ newVal = die.rollSingle();
+ }
+
+ lst.add(res);
+ }
+ }
+
+ return ListUtils.toPrimitive(lst);
+ }
+
+ public long rollSingle() {
+ Die die = dice[0];
+
+ long newVal = die.rollSingle();
+
+ long res = newVal;
+
+ while(compound.test(newVal)) {
+ newVal = die.rollSingle();
+
+ if(penetrate) newVal -= 1;
+
+ res += newVal;
+ }
+
+ return res;
+ }
+
+ /* :UnoptimizableDice */
+ public boolean canOptimize() {
+ return false;
+ }
+
+ public long optimize() {
+ throw new UnsupportedOperationException("Exploding dice can't be optimized");
+ }
+}