summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Culkin <scorpress@gmail.com>2020-04-13 18:32:11 -0400
committerBen Culkin <scorpress@gmail.com>2020-04-13 18:32:11 -0400
commit5a26fb2c0d899b1c80fabb39366a7b7db4d8ca41 (patch)
tree19e19dab7c72aaee504a07e9434cde8c379092c8
parent25fbdca78518df0cb096544e912cdb3913f348fd (diff)
Add some javadoc
Add some javadoc comments
-rw-r--r--dice/src/main/java/bjc/dicelang/dicev2/ComparePoints.java24
-rw-r--r--dice/src/main/java/bjc/dicelang/dicev2/CompositeDie.java93
-rw-r--r--dice/src/main/java/bjc/dicelang/dicev2/CompoundDieMod.java35
-rw-r--r--dice/src/main/java/bjc/dicelang/dicev2/ComputedDie.java46
-rw-r--r--dice/src/main/java/bjc/dicelang/dicev2/ConcatDieMod.java16
-rw-r--r--dice/src/main/java/bjc/dicelang/dicev2/CountDieMod.java31
-rw-r--r--dice/src/main/java/bjc/dicelang/dicev2/Die.java29
-rw-r--r--dice/src/main/java/bjc/dicelang/dicev2/DieMods.java193
-rw-r--r--dice/src/main/java/bjc/dicelang/dicev2/Dies.java96
-rw-r--r--dice/src/main/java/bjc/dicelang/dicev2/ExplodingDieMod.java29
-rw-r--r--dice/src/main/java/bjc/dicelang/dicev2/FilterDieMod.java5
11 files changed, 569 insertions, 28 deletions
diff --git a/dice/src/main/java/bjc/dicelang/dicev2/ComparePoints.java b/dice/src/main/java/bjc/dicelang/dicev2/ComparePoints.java
index 4e08c3e..81b5cbe 100644
--- a/dice/src/main/java/bjc/dicelang/dicev2/ComparePoints.java
+++ b/dice/src/main/java/bjc/dicelang/dicev2/ComparePoints.java
@@ -2,15 +2,35 @@ package bjc.dicelang.dicev2;
import java.util.function.LongPredicate;
+/**
+ * Utility class for creating compare points.
+ * @author Ben Culkin
+ *
+ */
public class ComparePoints {
+ /**
+ * Create a compare point for checking 'less than'
+ * @param val The value to check if we are less than.
+ * @return A compare point that does the specified check.
+ */
public static LongPredicate isLess(long val) {
return (arg) -> arg < val;
}
-
+
+ /**
+ * Create a compare point for checking 'equals'
+ * @param val The value to check if we are equal to.
+ * @return A compare point that does the specified check.
+ */
public static LongPredicate isEqual(long val) {
return (arg) -> arg == val;
}
-
+
+ /**
+ * Create a compare point for checking 'greater than'
+ * @param val The value to check if we are greater than.
+ * @return A compare point that does the specified check.
+ */
public static LongPredicate isGreater(long val) {
return (arg) -> arg > val;
}
diff --git a/dice/src/main/java/bjc/dicelang/dicev2/CompositeDie.java b/dice/src/main/java/bjc/dicelang/dicev2/CompositeDie.java
index e9bb585..ab29842 100644
--- a/dice/src/main/java/bjc/dicelang/dicev2/CompositeDie.java
+++ b/dice/src/main/java/bjc/dicelang/dicev2/CompositeDie.java
@@ -2,66 +2,130 @@ package bjc.dicelang.dicev2;
import java.util.Random;
+/**
+ * A dice where both the number of dice to roll and the number of sides of the
+ * dice are specified by other dice.
+ *
+ * @author Ben Culkin
+ *
+ */
public class CompositeDie extends Die {
+ /**
+ * The die that provides the number of dice to roll.
+ */
public final Die numDice;
+ /**
+ * The die that provides the number of sides of the dice to roll.
+ */
public final Die numSides;
+ /**
+ * Should the number of sides per dice be rerolled after every dice is rolled?
+ *
+ * By default, the number of sides will be rolled once per roll of this die.
+ */
public final boolean rerollSides;
+ /**
+ * Create a new composite dice that rolls the number of sides once.
+ *
+ * @param numDice
+ * The number of dice to roll.
+ * @param numSides
+ * The number of sides on the dice.
+ */
public CompositeDie(Die numDice, Die numSides) {
this(numDice, numSides, false);
}
+ /**
+ * Create a new composite dice with the specified side-rolling behavior.
+ *
+ * @param numDice
+ * The number of dice to roll.
+ * @param numSides
+ * The number of sides on the dice.
+ * @param rerollSides
+ * Whether to rolls the sides once per roll, or once per
+ * dice.
+ */
public CompositeDie(Die numDice, Die numSides, boolean rerollSides) {
super();
- this.numDice = numDice;
+ this.numDice = numDice;
this.numSides = numSides;
this.rerollSides = rerollSides;
}
+ /**
+ * Create a new composite dice using a specific RNG, rolling dice one per side.
+ *
+ * @param rnd
+ * The RNG to use.
+ * @param numDice
+ * The number of dice to use.
+ * @param numSides
+ * The number of sides for the dice.
+ */
public CompositeDie(Random rnd, Die numDice, Die numSides) {
this(rnd, numDice, numSides, false);
}
+ /**
+ * Create a new composite dice using a specific RNG and side-rolling behavior.
+ *
+ * @param rnd
+ * The RNG to use.
+ * @param numDice
+ * The number of dice to use.
+ * @param numSides
+ * The number of sides on the dice.
+ * @param rerollSides
+ * Whether to rolls the sides once per roll, or once per
+ * dice.
+ */
public CompositeDie(Random rnd, Die numDice, Die numSides, boolean rerollSides) {
super(rnd);
- this.numDice = numDice;
+ this.numDice = numDice;
this.numSides = numSides;
this.rerollSides = rerollSides;
}
+ @Override
public long[] roll() {
- int target = (int)numDice.rollSingle();
- int sides = (int)numSides.rollSingle();
+ int target = (int) numDice.rollSingle();
+ int sides = (int) numSides.rollSingle();
long[] res = new long[target];
- for(int i = 0; i < target; i++) {
+ for (int i = 0; i < target; i++) {
res[i] = rng.nextInt(sides) + 1;
- if(rerollSides) sides = (int)numSides.rollSingle();
+ if (rerollSides)
+ sides = (int) numSides.rollSingle();
}
return res;
}
+ @Override
public long rollSingle() {
- return rng.nextInt((int)numSides.rollSingle());
+ return rng.nextInt((int) numSides.rollSingle());
}
+ @Override
public boolean canOptimize() {
- if(numSides.canOptimize()) {
- if(numSides.optimize() <= 1) {
+ if (numSides.canOptimize()) {
+ if (numSides.optimize() <= 1) {
return true;
}
}
- if(numDice.canOptimize()) {
- if(numDice.optimize() == 0) {
+ if (numDice.canOptimize()) {
+ if (numDice.optimize() == 0) {
return true;
}
}
@@ -69,9 +133,12 @@ public class CompositeDie extends Die {
return false;
}
+ @Override
public long optimize() {
- if(numDice.canOptimize()) return 0;
- if(numSides.canOptimize() && numSides.optimize() == 0) return 0;
+ if (numDice.canOptimize())
+ return 0;
+ if (numSides.canOptimize() && numSides.optimize() == 0)
+ return 0;
return numDice.rollSingle();
}
diff --git a/dice/src/main/java/bjc/dicelang/dicev2/CompoundDieMod.java b/dice/src/main/java/bjc/dicelang/dicev2/CompoundDieMod.java
index 6b752f2..519170b 100644
--- a/dice/src/main/java/bjc/dicelang/dicev2/CompoundDieMod.java
+++ b/dice/src/main/java/bjc/dicelang/dicev2/CompoundDieMod.java
@@ -6,17 +6,44 @@ import java.util.ArrayList;
import java.util.List;
import java.util.function.LongPredicate;
+/**
+ * Create a compounding dice.
+ *
+ * Compounding dice are rolled more than once, if they pass a given predicate.
+ * @author Ben Culkin
+ *
+ */
public class CompoundDieMod extends Die {
+ /**
+ * The pool of dice that make up this compound die mod.
+ */
public final Die[] dice;
+ /**
+ * The compare point to determine when to compound.
+ */
public final LongPredicate compound;
+ /**
+ * Whether or not the compounding should 'penetrate' (subtract 1 before exploding)
+ */
public final boolean penetrate;
+ /**
+ * Create a new compound die.
+ * @param compound The predicate to compound on.
+ * @param dice The pool of dice to roll.
+ */
public CompoundDieMod(LongPredicate compound, Die... dice) {
this(compound, false, dice);
}
-
+
+ /**
+ * Create a new compound die.
+ * @param compound The predicate to compound on.
+ * @param penetrate Whether or not compounding should penetrate.
+ * @param dice The pool of dice to roll.
+ */
public CompoundDieMod(LongPredicate compound, boolean penetrate, Die... dice) {
super();
@@ -27,6 +54,7 @@ public class CompoundDieMod extends Die {
this.penetrate = penetrate;
}
+ @Override
public long[] roll() {
List<Long> lst = new ArrayList<>(5);
@@ -51,6 +79,7 @@ public class CompoundDieMod extends Die {
return ListUtils.toPrimitive(lst);
}
+ @Override
public long rollSingle() {
Die die = dice[0];
@@ -70,11 +99,13 @@ public class CompoundDieMod extends Die {
}
/* :UnoptimizableDice */
+ @Override
public boolean canOptimize() {
return false;
}
+ @Override
public long optimize() {
- throw new UnsupportedOperationException("Exploding dice can't be optimized");
+ throw new UnsupportedOperationException("Compounding dice can't be optimized");
}
}
diff --git a/dice/src/main/java/bjc/dicelang/dicev2/ComputedDie.java b/dice/src/main/java/bjc/dicelang/dicev2/ComputedDie.java
index 89405fe..4963fcb 100644
--- a/dice/src/main/java/bjc/dicelang/dicev2/ComputedDie.java
+++ b/dice/src/main/java/bjc/dicelang/dicev2/ComputedDie.java
@@ -3,16 +3,41 @@ package bjc.dicelang.dicev2;
import java.util.Random;
import java.util.function.IntSupplier;
+/**
+ * Create a computed die, which gets its values from arbitrary functions.
+ * @author Ben Culkin
+ *
+ */
public class ComputedDie extends Die {
+ /**
+ * The function that provides the number of dice to roll.
+ */
public final IntSupplier numDice;
+ /**
+ * The function that provides the number of sides for the dice.
+ */
public final IntSupplier numSides;
+ /**
+ * Whether or not the number of sides should be rolled once per die.
+ */
public final boolean rerollSides;
+ /**
+ * Create a new computed die.
+ * @param numDice The number of dice to roll.
+ * @param numSides The number of sides on the dice.
+ */
public ComputedDie(IntSupplier numDice, IntSupplier numSides) {
this(numDice, numSides, false);
}
+ /**
+ * Create a new computed die with specified side-roll behavior.
+ * @param numDice The number of dice to roll.
+ * @param numSides The number of sides on the dice.
+ * @param rerollSides Controls whether the number of sides should be rerolled once per die.
+ */
public ComputedDie(IntSupplier numDice, IntSupplier numSides, boolean rerollSides) {
super();
@@ -21,11 +46,24 @@ public class ComputedDie extends Die {
this.rerollSides = rerollSides;
}
-
+
+ /**
+ * Create a new computed die using a specified number of RNGs.
+ * @param rnd The RNG to use.
+ * @param numDice The number of dice to roll.
+ * @param numSides The number of sides on the dice.
+ */
public ComputedDie(Random rnd, IntSupplier numDice, IntSupplier numSides) {
this(rnd, numDice, numSides, false);
}
-
+
+ /**
+ * Create a new computed die using a specified number of RNGs and side-roll behavior.
+ * @param rnd The RNG to use.
+ * @param numDice The number of dice to roll.
+ * @param numSides The number of sides on the dice.
+ * @param rerollSides Controls whether the number of sides should be rerolled once per die.
+ */
public ComputedDie(Random rnd, IntSupplier numDice, IntSupplier numSides, boolean rerollSides) {
super(rnd);
@@ -35,6 +73,7 @@ public class ComputedDie extends Die {
this.rerollSides = rerollSides;
}
+ @Override
public long[] roll() {
int target = numDice.getAsInt();
int sides = numSides.getAsInt();
@@ -50,6 +89,7 @@ public class ComputedDie extends Die {
return res;
}
+ @Override
public long rollSingle() {
return rng.nextInt(numSides.getAsInt());
}
@@ -65,10 +105,12 @@ public class ComputedDie extends Die {
* behavior, otherwise you wouldn't need ComputedDie.
*/
+ @Override
public boolean canOptimize() {
return false;
}
+ @Override
public long optimize() {
throw new UnsupportedOperationException("ComputedDie cannot be optimized");
}
diff --git a/dice/src/main/java/bjc/dicelang/dicev2/ConcatDieMod.java b/dice/src/main/java/bjc/dicelang/dicev2/ConcatDieMod.java
index 94815d8..e356bac 100644
--- a/dice/src/main/java/bjc/dicelang/dicev2/ConcatDieMod.java
+++ b/dice/src/main/java/bjc/dicelang/dicev2/ConcatDieMod.java
@@ -1,18 +1,32 @@
package bjc.dicelang.dicev2;
+/**
+ * Concatentate a series of dice together.
+ * @author Ben Culkin
+ *
+ */
public class ConcatDieMod extends Die {
+ /**
+ * The dice to concatenate together.
+ */
public final Die[] dice;
+ /**
+ * Create a new concatenative die pool.
+ * @param dice The pool of dice to concatenate.
+ */
public ConcatDieMod(Die... dice) {
super();
this.dice = dice;
}
+ @Override
public long[] roll() {
return new long[] { rollSingle() };
}
+ @Override
public long rollSingle() {
StringBuilder sb = new StringBuilder();
@@ -25,6 +39,7 @@ public class ConcatDieMod extends Die {
return Long.parseLong(sb.toString());
}
+ @Override
public boolean canOptimize() {
for(Die die : dice) {
if(!die.canOptimize()) return false;
@@ -33,6 +48,7 @@ public class ConcatDieMod extends Die {
return true;
}
+ @Override
public long optimize() {
StringBuilder sb = new StringBuilder();
diff --git a/dice/src/main/java/bjc/dicelang/dicev2/CountDieMod.java b/dice/src/main/java/bjc/dicelang/dicev2/CountDieMod.java
index 0b5ec5b..c74e0bf 100644
--- a/dice/src/main/java/bjc/dicelang/dicev2/CountDieMod.java
+++ b/dice/src/main/java/bjc/dicelang/dicev2/CountDieMod.java
@@ -2,17 +2,44 @@ package bjc.dicelang.dicev2;
import java.util.function.LongPredicate;
+/**
+ * Create a die pool that will count successes/failures.
+ * @author Ben Culkin
+ *
+ */
public class CountDieMod extends Die {
+ /**
+ * The pool of dice that will be rolled.
+ */
public final Die[] dice;
+ /**
+ * The predicate for counting successes.
+ */
public final LongPredicate success;
+ /**
+ * The predicate for counting failures.
+ */
public LongPredicate failure;
+ /**
+ * Create a new counted die mod with a specified success criteria.
+ *
+ * @param success The predicate for determining a success.
+ * @param dice The pool of dice to roll.
+ */
public CountDieMod(LongPredicate success, Die... dice) {
this(success, null, dice);
}
+ /**
+ * Create a new counted die mod with a specified success criteria.
+ *
+ * @param success The predicate for determining a success.
+ * @param failure The predicate for determining a failure.
+ * @param dice The pool of dice to roll.
+ */
public CountDieMod(LongPredicate success, LongPredicate failure, Die... dice) {
super();
@@ -22,10 +49,12 @@ public class CountDieMod extends Die {
this.dice = dice;
}
+ @Override
public long[] roll() {
return new long[] { rollSingle() };
}
+ @Override
public long rollSingle() {
long count = 0;
@@ -42,10 +71,12 @@ public class CountDieMod extends Die {
/* :UnoptimizableDice */
+ @Override
public boolean canOptimize() {
return false;
}
+ @Override
public long optimize() {
throw new UnsupportedOperationException("Counted dice can't be optimized");
}
diff --git a/dice/src/main/java/bjc/dicelang/dicev2/Die.java b/dice/src/main/java/bjc/dicelang/dicev2/Die.java
index d688cfa..ab4f880 100644
--- a/dice/src/main/java/bjc/dicelang/dicev2/Die.java
+++ b/dice/src/main/java/bjc/dicelang/dicev2/Die.java
@@ -2,6 +2,11 @@ package bjc.dicelang.dicev2;
import java.util.Random;
+/**
+ * An abstract class that represents a single pool of dice.
+ * @author Ben Culkin
+ *
+ */
public abstract class Die {
private static final Random BASE = new Random();
@@ -15,13 +20,37 @@ public abstract class Die {
rng = rnd;
}
+ /**
+ * Set the RNG this die pool uses.
+ * @param rnd The RNG used by the die pool.
+ */
public void setRandom(Random rnd) {
rng = rnd;
}
+ /**
+ * Roll the entire die pool.
+ * @return The results from rolling the dice.
+ */
public abstract long[] roll();
+ /**
+ * Roll a single die in the pool.
+ *
+ * For pools with multiple die, this may be somewhat arbitrary.
+ * @return Result from rolling a single die in the pool.
+ */
public abstract long rollSingle();
+ /**
+ * Can this pool be optimized?
+ * @return Is the pool optimizable?
+ */
public abstract boolean canOptimize();
+ /**
+ * Optimize the die pool.
+ *
+ * Is undefined if called while canOptimize is false.
+ * @return The optimized version of the pool.
+ */
public abstract long optimize();
}
diff --git a/dice/src/main/java/bjc/dicelang/dicev2/DieMods.java b/dice/src/main/java/bjc/dicelang/dicev2/DieMods.java
index 8a951a9..5e82097 100644
--- a/dice/src/main/java/bjc/dicelang/dicev2/DieMods.java
+++ b/dice/src/main/java/bjc/dicelang/dicev2/DieMods.java
@@ -17,81 +17,237 @@ import java.util.function.LongUnaryOperator;
* performance issues, replace these with custom classes.
*/
+/**
+ * Utility constructor class for die mods.
+ *
+ * @author Ben Culkin
+ *
+ */
public class DieMods {
+ /**
+ * Return a die pool that performs a reduction over a given pool.
+ *
+ * @param fold
+ * The fold to use.
+ * @param initial
+ * The initial value for the fold.
+ * @param dice
+ * The die pool.
+ * @return A die that performs a reduction over the pool.
+ */
public Die reduce(LongBinaryOperator fold, long initial, Die... dice) {
return new ReduceDieMod(fold, initial, dice);
}
+ /**
+ * Create a concatenative die.
+ *
+ * @param dice
+ * The input die pool.
+ * @return A die that concatenates the provided pool.
+ */
public Die concat(Die... dice) {
return new ConcatDieMod(dice);
}
+ /**
+ * Create a die pool for tracking successes.
+ *
+ * @param success
+ * The threshold for success.
+ * @param dice
+ * The input die pool.
+ * @return A die that counts the successes.
+ */
public Die counted(LongPredicate success, Die... dice) {
return new CountDieMod(success, dice);
}
+ /**
+ * Create a die pool for tracking successes/failures.
+ *
+ * @param success
+ * The threshold for success.
+ * @param failure
+ * The threshold for failure.
+ * @param dice
+ * The input die pool.
+ * @return A die that counts the successes.
+ */
public Die counted(LongPredicate success, LongPredicate failure, Die... dice) {
return new CountDieMod(success, failure, dice);
}
+ /**
+ * Create an exploding die pool.
+ *
+ * @param explode
+ * The criteria to explode on.
+ * @param dice
+ * The input die pool.
+ * @return An exploding variant of the die pool.
+ */
public Die explode(LongPredicate explode, Die... dice) {
return new ExplodingDieMod(explode, dice);
}
+ /**
+ * Create an exploding die pool.
+ *
+ * @param explode
+ * The criteria to explode on.
+ * @param penetrate
+ * Whether the explosions should penetrate.
+ * @param dice
+ * The input die pool.
+ * @return An exploding variant of the die pool.
+ */
public Die explode(LongPredicate explode, boolean penetrate, Die... dice) {
return new ExplodingDieMod(explode, penetrate, dice);
}
+ /**
+ * Create an compounding die pool.
+ *
+ * @param compound
+ * The criteria to compound on.
+ * @param dice
+ * The input die pool.
+ * @return An compounding variant of the die pool.
+ */
public Die compound(LongPredicate compound, Die... dice) {
return new CompoundDieMod(compound, dice);
}
+ /**
+ * Create an compounding die pool.
+ *
+ * @param compound
+ * The criteria to compound on.
+ * @param penetrate
+ * Whether the compounding should penetrate.
+ * @param dice
+ * The input die pool.
+ * @return An compounding variant of the die pool.
+ */
public Die compound(LongPredicate compound, boolean penetrate, Die... dice) {
return new CompoundDieMod(compound, penetrate, dice);
}
+ /**
+ * Gather a series of dice together into a pool.
+ *
+ * @param dice
+ * The die that will form the pool.
+ * @return A pooled variant of the dice.
+ */
public Die pool(Die... dice) {
return new PoolDiceMod(dice);
}
+ /**
+ * Filter out certain dice from the pool.
+ *
+ * @param filter
+ * The criteria to filter on.
+ * @param dice
+ * The input die pool.
+ * @return The die pool with certain dice filtered out.
+ */
public Die filter(LongPredicate filter, Die... dice) {
return new FilterDieMod(filter, dice);
}
+ /**
+ * Create a sorted variant of a die pool.
+ *
+ * @param sorter
+ * A sorted variant of a die pool.
+ * @param die
+ * The input die pool.
+ * @return A sorted die pool.
+ */
public Die sort(Comparator<Long> sorter, Die die) {
return new SortDieMod(sorter, die);
}
+ /**
+ * Create a die pool that applies a given transform.
+ *
+ * @param map
+ * The transform to apply.
+ * @param die
+ * The input die pool.
+ * @return The die pool with the transform applied.
+ */
public Die map(LongUnaryOperator map, Die die) {
return new MapDieMod(map, die);
}
/* :SyntheticMod */
+ /**
+ * Perform a summation over a die pool.
+ *
+ * @param dice
+ * The input die pool.
+ * @return A die that sums the input dice.
+ */
public Die sum(Die... dice) {
return reduce((l, r) -> l + r, 0, dice);
}
/* :SyntheticMod */
+ /**
+ * Perform a subtraction over a die pool.
+ *
+ * @param dice
+ * The input die pool.
+ * @return A die that subtracts the input dice.
+ */
public Die subtract(Die... dice) {
return reduce((l, r) -> l - r, 0, dice);
}
/* :SyntheticMod */
+ /**
+ * Perform a multiplication over a die pool.
+ *
+ * @param dice
+ * The input die pool.
+ * @return A die that multiplies the input dice.
+ */
public Die multiply(Die... dice) {
return reduce((l, r) -> l * r, 1, dice);
}
/* :SyntheticMod */
+ /**
+ * Perform a division over a die pool.
+ *
+ * @param dice
+ * The input die pool.
+ * @return A die that divides the input dice.
+ */
public Die divide(Die... dice) {
return reduce((l, r) -> l / r, 1, dice);
}
/* :SyntheticMod */
+ /**
+ * Create a die pool that sorts the dice in ascending order.
+ * @param die The input die pool.
+ * @return A die pool that sorts in ascending order.
+ */
public Die ascending(Die die) {
return new SortDieMod(Comparator.naturalOrder(), die);
}
/* :SyntheticMod */
+ /**
+ * Create a die pool that sorts the dice in descending order.
+ * @param die The input die pool.
+ * @return A die pool that sorts in descending order.
+ */
public Die descending(Die die) {
return new SortDieMod((v1, v2) -> {
return Long.compare(v1, v2);
@@ -99,13 +255,19 @@ public class DieMods {
}
/* :SyntheticMod */
+ /**
+ * Create a die pool that takes the first n dice.
+ * @param num The number of dice to take.
+ * @param die The input die pool.
+ * @return The die pool that takes the first n dice.
+ */
public Die take(int num, Die die) {
GeneratingIterator<Integer> itr = new GeneratingIterator<>(num, (val) -> {
return val - 1;
- }, (val) -> val == 0);
+ }, (val) -> val == 0);
return filter((val) -> {
- if(itr.hasNext()) {
+ if (itr.hasNext()) {
itr.next();
return true;
}
@@ -115,13 +277,19 @@ public class DieMods {
}
/* :SyntheticMod */
+ /**
+ * Create a die pool that drops the first n dice.
+ * @param num The number of dice to drops.
+ * @param die The input die pool.
+ * @return The die pool that drops the first n dice.
+ */
public Die drop(int num, Die die) {
GeneratingIterator<Integer> itr = new GeneratingIterator<>(num, (val) -> {
return val - 1;
- }, (val) -> val == 0);
+ }, (val) -> val == 0);
return filter((val) -> {
- if(itr.hasNext()) {
+ if (itr.hasNext()) {
itr.next();
return false;
}
@@ -131,20 +299,33 @@ public class DieMods {
}
/* :SyntheticMod */
+ /**
+ * Create a die pool that rerolls dice once if they are below a given threshold.
+ * @param reroll The point at which to reload.
+ * @param die The input die pool.
+ * @return A die pool that rerolls certain dice once.
+ */
public Die rerollOnce(LongPredicate reroll, Die die) {
return map((val) -> {
- if(reroll.test(val)) return die.rollSingle();
+ if (reroll.test(val))
+ return die.rollSingle();
return val;
}, die);
}
/* :SyntheticMod */
+ /**
+ * Create a die pool that rerolls dice if they are below a given threshold.
+ * @param reroll The point at which to reload.
+ * @param die The input die pool.
+ * @return A die pool that rerolls certain dice.
+ */
public Die reroll(LongPredicate reroll, Die die) {
return map((val) -> {
long nVal = val;
- while(reroll.test(nVal)) {
+ while (reroll.test(nVal)) {
nVal = die.rollSingle();
}
diff --git a/dice/src/main/java/bjc/dicelang/dicev2/Dies.java b/dice/src/main/java/bjc/dicelang/dicev2/Dies.java
index 1d29b55..7446767 100644
--- a/dice/src/main/java/bjc/dicelang/dicev2/Dies.java
+++ b/dice/src/main/java/bjc/dicelang/dicev2/Dies.java
@@ -3,55 +3,145 @@ package bjc.dicelang.dicev2;
import java.util.Random;
import java.util.function.IntSupplier;
+/**
+ * Utility class for creating die pools.
+ * @author Ben Culkin
+ *
+ */
public class Dies {
+ /**
+ * Create a die that always returns a given value.
+ * @param val The value.
+ * @return A die that always returns the specified value.
+ */
public static Die scalar(long val) {
return new ScalarDie(val);
}
+ /**
+ * Create a random polyhedral dice.
+ * @param dice The number of dice in the pool.
+ * @param sides The nuumber of sides on the dice.
+ * @return A polyhedral dice, in the given configuration.
+ */
public static Die polyhedral(int dice, int sides) {
return new PolyhedralDie(dice, sides);
}
+ /**
+ * Create a random polyhedral dice.
+ * @param rnd The RNG to use.
+ * @param dice The number of dice in the pool.
+ * @param sides The nuumber of sides on the dice.
+ * @return A polyhedral dice, in the given configuration.
+ */
public static Die polyhedral(Random rnd, int dice, int sides) {
return new PolyhedralDie(rnd, dice, sides);
}
+ /**
+ * Create a number of fudge dice.
+ * @param dice The number of dice.
+ * @return A die pool with the fudge dice to use.
+ */
public static Die fudge(int dice) {
return new FudgeDie(dice);
}
+ /**
+ * Create a number of fudge dice.
+ * @param rnd The RNG to use.
+ * @param dice The number of dice.
+ * @return A die pool with the fudge dice to use.
+ */
public static Die fudge(Random rnd, int dice) {
return new FudgeDie(rnd, dice);
}
+ /**
+ * Create a composite series of dice.
+ * @param numDice The number of dice to roll.
+ * @param numSides The number of sides on the dice.
+ * @return A composite dice with the given parameters.
+ */
public static Die composite(Die numDice, Die numSides) {
return new CompositeDie(numDice, numSides);
}
+ /**
+ * Create a composite series of dice.
+ * @param numDice The number of dice to roll.
+ * @param numSides The number of sides on the dice.
+ * @param rerollSides Should the number of dice be rolled more than once per pool?
+ * @return A composite dice with the given parameters.
+ */
public static Die composite(Die numDice, Die numSides, boolean rerollSides) {
return new CompositeDie(numDice, numSides, rerollSides);
}
+ /**
+ * Create a composite series of dice.
+ * @param rnd The RNG to use.
+ * @param numDice The number of dice to roll.
+ * @param numSides The number of sides on the dice.
+ * @return A composite dice with the given parameters.
+ */
public static Die composite(Random rnd, Die numDice, Die numSides) {
return new CompositeDie(rnd, numDice, numSides);
}
+ /**
+ * Create a composite series of dice.
+ * @param rnd The RNG to use.
+ * @param numDice The number of dice to roll.
+ * @param numSides The number of sides on the dice.
+ * @param rerollSides Should the number of dice be rolled more than once per pool?
+ * @return A composite dice with the given parameters.
+ */
public static Die composite(Random rnd, Die numDice, Die numSides, boolean rerollSides) {
return new CompositeDie(rnd, numDice, numSides, rerollSides);
}
+ /**
+ * A computed die pool.
+ * @param numDice The provider for the number of dice.
+ * @param numSides The provider for the number of sides.
+ * @return The computed die.
+ */
public static Die computed(IntSupplier numDice, IntSupplier numSides) {
return new ComputedDie(numDice, numSides);
}
-
+
+ /**
+ * A computed die pool.
+ * @param numDice The provider for the number of dice.
+ * @param numSides The provider for the number of sides.
+ * @param rerollSides Should the number of dice be rolled more than once per pool?
+ * @return The computed die.
+ */
public static Die computed(IntSupplier numDice, IntSupplier numSides, boolean rerollSides) {
return new ComputedDie(numDice, numSides, rerollSides);
}
-
+
+ /**
+ * A computed die pool.
+ * @param rnd The RNG to use.
+ * @param numDice The provider for the number of dice.
+ * @param numSides The provider for the number of sides.
+ * @return The computed die.
+ */
public static Die computed(Random rnd, IntSupplier numDice, IntSupplier numSides) {
return new ComputedDie(rnd, numDice, numSides);
}
-
+
+ /**
+ * A computed die pool.
+ * @param rnd The RNG to use.
+ * @param numDice The provider for the number of dice.
+ * @param numSides The provider for the number of sides.
+ * @param rerollSides Should the number of dice be rolled more than once per pool?
+ * @return The computed die.
+ */
public static Die computed(Random rnd, IntSupplier numDice, IntSupplier numSides, boolean rerollSides) {
return new ComputedDie(rnd, numDice, numSides, rerollSides);
}
diff --git a/dice/src/main/java/bjc/dicelang/dicev2/ExplodingDieMod.java b/dice/src/main/java/bjc/dicelang/dicev2/ExplodingDieMod.java
index 885e02d..895a53c 100644
--- a/dice/src/main/java/bjc/dicelang/dicev2/ExplodingDieMod.java
+++ b/dice/src/main/java/bjc/dicelang/dicev2/ExplodingDieMod.java
@@ -6,17 +6,42 @@ import java.util.ArrayList;
import java.util.List;
import java.util.function.LongPredicate;
+/**
+ * An exploding die pool.
+ * @author Ben Culkin
+ *
+ */
public class ExplodingDieMod extends Die {
+ /**
+ * The pool of dice.
+ */
public final Die[] dice;
+ /**
+ * The predicate which says when to explode.
+ */
public final LongPredicate explode;
+ /**
+ * Should the explosion penetrate.
+ */
public final boolean penetrate;
+ /**
+ * Create an exploding die pool.
+ * @param explode The predicate for determining an explosion.
+ * @param dice The die pool.
+ */
public ExplodingDieMod(LongPredicate explode, Die... dice) {
this(explode, false, dice);
}
+ /**
+ * Create an exploding die pool.
+ * @param explode The predicate for determining an explosion.
+ * @param penetrate Whether the explosion should penetrate.
+ * @param dice The die pool.
+ */
public ExplodingDieMod(LongPredicate explode, boolean penetrate, Die... dice) {
super();
@@ -27,6 +52,7 @@ public class ExplodingDieMod extends Die {
this.penetrate = penetrate;
}
+ @Override
public long[] roll() {
List<Long> lst = new ArrayList<>(dice.length);
@@ -57,15 +83,18 @@ public class ExplodingDieMod extends Die {
* It makes no sense to roll a 'single' exploding dice, since it
* exploding adds another die. Use compounding dice for that case.
*/
+ @Override
public long rollSingle() {
throw new UnsupportedOperationException("Exploding dice can't be rolled singly");
}
/* :UnoptimizableDice */
+ @Override
public boolean canOptimize() {
return false;
}
+ @Override
public long optimize() {
throw new UnsupportedOperationException("Exploding dice can't be optimized");
}
diff --git a/dice/src/main/java/bjc/dicelang/dicev2/FilterDieMod.java b/dice/src/main/java/bjc/dicelang/dicev2/FilterDieMod.java
index 47917c4..89e5749 100644
--- a/dice/src/main/java/bjc/dicelang/dicev2/FilterDieMod.java
+++ b/dice/src/main/java/bjc/dicelang/dicev2/FilterDieMod.java
@@ -7,6 +7,11 @@ import java.util.List;
import java.util.function.LongPredicate;
+/**
+ * A filtered die pool.
+ * @author Ben Culkin
+ *
+ */
public class FilterDieMod extends Die {
public final Die[] dice;