-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdie.go
237 lines (204 loc) · 5.3 KB
/
die.go
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package dice
import (
"bytes"
"context"
"fmt"
"sync/atomic"
)
// Die represents an internally-typed die. If Result is a non-nil pointer, it
// is considered rolled.
type Die struct {
// Generic properties
Type DieType `json:"type,omitempty" mapstructure:"type"`
Size int `json:"size" mapstructure:"size"`
Rerolls int `json:"rerolls" mapstructure:"rerolls"`
*Result `json:"result,omitempty" mapstructure:"result"`
Modifiers ModifierList `json:"modifiers,omitempty" mapstructure:"modifiers"`
parent Roller
}
// NewDie creates a new die off of a properties list. It will tweak the
// properties list to better suit reuse.
func NewDie(props *RollerProperties) (Roller, error) {
return NewDieWithParent(props, nil)
}
// MustNewDie creates a new die off of a properties list. It will tweak the
// properties list to better suit reuse. Panics on a non-nil error.
func MustNewDie(props *RollerProperties) Roller {
if r, err := NewDieWithParent(props, nil); err == nil {
return r
} else {
panic(err)
}
}
// NewDieWithParent creates a new die off of a properties list. It will tweak the
// properties list to better suit reuse.
func NewDieWithParent(props *RollerProperties, parent Roller) (Roller, error) {
if props.Size < 0 {
return nil, ErrImpossibleDie
}
// If the property set was for a default fudge die set, set a default size
// of 1.
if props.Type == TypeFudge && props.Size == 0 {
props.Size = 1
}
die := &Die{
Type: props.Type,
Size: props.Size,
Result: props.Result,
Modifiers: props.DieModifiers,
}
if parent != nil {
die.SetParent(parent)
}
return die, nil
}
// Roll rolls a die based on the die's size and type and calculates a value.
func (d *Die) Roll(ctx context.Context) error {
if d == nil {
return ErrNilDie
}
// Check if rolled too many times already
var maxRolls = int64(MaxRolls)
if ctxMaxRolls, ok := ctx.Value(CtxKeyMaxRolls).(int64); ok {
maxRolls = ctxMaxRolls
}
if *CtxTotalRolls(ctx) >= uint64(maxRolls) {
return ErrMaxRolls
}
// bump context roll count
atomic.AddUint64(CtxTotalRolls(ctx), 1)
if d.Size == 0 {
d.Result = NewResult(0)
return nil
}
switch d.Type {
case TypeFudge:
d.Result = NewResult(float64(Source.Intn(int(d.Size*2+1)) - int(d.Size)))
if d.Result.Value == -float64(d.Size) {
d.CritFailure = true
}
default:
d.Result = NewResult(float64(1 + Source.Intn(int(d.Size))))
if d.Result.Value == 1 {
d.CritFailure = true
}
}
// default critical success on max roll; override via modifiers
if d.Result.Value == float64(d.Size) {
d.CritSuccess = true
}
return nil
}
// reset resets a Die's properties so that it can be re-rolled from scratch.
func (d *Die) reset() {
d.Result = nil
d.Dropped = false
}
// FullRoll rolls the Die. The die will be reset if it had been rolled
// previously.
func (d *Die) FullRoll(ctx context.Context) error {
if d == nil {
return ErrNilDie
}
// Check if rolled too many times already
if *CtxTotalRolls(ctx) >= CtxMaxRolls(ctx) {
return ErrMaxRolls
}
if err := d.Roll(ctx); err != nil {
return err
}
// Apply modifiers
for i := 0; i < len(d.Modifiers); i++ {
err := d.Modifiers[i].Apply(ctx, d)
switch {
// die rerolled, so restart validation checks with new modifiers
case err == ErrRerolled:
i = -1
// i++ => 0 to restart from first modifier
break
case err != nil:
return err
}
}
return nil
}
// Reroll performs a reroll after resetting a Die.
func (d *Die) Reroll(ctx context.Context) error {
if d == nil {
return ErrNilDie
}
if d.Result == nil {
return ErrUnrolled
}
d.Result = nil
d.Rerolls++
// reroll without reapplying all modifiers
return d.Roll(ctx)
}
// String returns an expression-like representation of a rolled die or its type,
// if it has not been rolled.
func (d *Die) String() string {
if d == nil {
return ""
}
if d.Result != nil {
total, _ := d.Total(context.Background())
return fmt.Sprintf("%.0f", total)
}
switch d.Type {
case TypePolyhedron:
return fmt.Sprintf("d%d%s", d.Size, d.Modifiers)
case TypeFudge:
if d.Size == 1 {
return fmt.Sprintf("dF%s", d.Modifiers)
}
return fmt.Sprintf("f%d%s", d.Size, d.Modifiers)
default:
return d.Type.String()
}
}
// Total implements the Total method. An ErrUnrolled error will be returned if
// the die has not been rolled.
func (d *Die) Total(ctx context.Context) (float64, error) {
if d == nil {
return 0.0, ErrNilDie
}
if d.Result == nil {
return 0.0, ErrUnrolled
}
return d.Result.Total(ctx)
}
// Value returns the Result.Value of a Die, regardless of whether the Die was
// dropped.
func (d *Die) Value(ctx context.Context) (float64, error) {
if d == nil {
return 0.0, ErrNilDie
}
if d.Result == nil {
return 0.0, ErrUnrolled
}
return d.Result.Value, nil
}
// Parent returns the Die's parent, which will be nil if an orphan.
func (d *Die) Parent() Roller {
return d.parent
}
func (d *Die) SetParent(r Roller) {
d.parent = r
}
// Add causes a panic as a single Die cannot have a descendent.
func (d *Die) Add(r Roller) {
panic("impossible action")
}
func (d *Die) ToGraphviz() string {
if d == nil {
return ""
}
var b bytes.Buffer
write := fmt.Fprintf
write(&b, "\"%p\" [label=\"%v\"];\n", d, d.String())
if d.Parent() != nil {
fmt.Fprintf(&b, "\"%p\" -> \"%p\" [dir=back style=dotted color=red];\n", d.Parent(), d)
}
return b.String()
}