summaryrefslogtreecommitdiff
path: root/dice/src/main/java/bjc/dicelang/dicev2/ComputedDie.java
blob: 4963fcb86da9b5b677eea058d5c3b7438a394d5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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();

		this.numDice  = numDice;
		this.numSides = numSides;

		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);

		this.numDice  = numDice;
		this.numSides = numSides;

		this.rerollSides = rerollSides;
	}

	@Override
	public long[] roll() {
		int target = numDice.getAsInt();
		int sides  = numSides.getAsInt();

		long[] res = new long[target];

		for(int i = 0; i < target; i++) {
			res[i] = rng.nextInt(sides) + 1;

			if(rerollSides) sides = numSides.getAsInt();
		}

		return res;
	}

	@Override
	public long rollSingle() {
		return rng.nextInt(numSides.getAsInt());
	}

	/*
	 * @NOTE
	 *
	 * :UnoptimizableDice
	 *
	 * Here, we assume that we can't optimize because we have no way
	 * of knowing that our suppliers will always return the same
	 * thing. As a matter of fact, they almost always will have that
	 * behavior, otherwise you wouldn't need ComputedDie.
	 */

	@Override
	public boolean canOptimize() {
		return false;
	}

	@Override
	public long optimize() {
		throw new UnsupportedOperationException("ComputedDie cannot be optimized");
	}
}