summaryrefslogtreecommitdiff
path: root/dice/src/main/java/bjc/dicelang/dicev2/CompoundDieMod.java
blob: 519170b00077dc68419fcab37e6d3829416cf93b (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
package bjc.dicelang.dicev2;

import bjc.utils.funcutils.ListUtils;

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

		this.dice = dice;

		this.compound = compound;

		this.penetrate = penetrate;
	}

	@Override
	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);
	}

	@Override
	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 */
	@Override
	public boolean canOptimize() {
		return false;
	}

	@Override
	public long optimize() {
		throw new UnsupportedOperationException("Compounding dice can't be optimized");
	}
}