-
Notifications
You must be signed in to change notification settings - Fork 0
/
roll-engine.c
283 lines (267 loc) · 7.39 KB
/
roll-engine.c
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include <math.h>
#include <signal.h>
#include <limits.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <omp.h>
#include "parse.h"
#include "io.h"
#include "roll-engine.h"
#include "util.h"
bool break_print_loop = false;
void sigint_handler(int sig) {
break_print_loop = true;
}
void print_dice_specs(const struct roll_encoding *d) {
switch(d->dir) {
case pos:
fprintf(stderr, "+");
break;
case neg:
fprintf(stderr, "-");
break;
default:
fprintf(stderr, "?");
}
fprintf(stderr, "%ldd%ld%s", d->ndice, d->nsides, d->explode ? "!" : "");
if(d->next != NULL) {
print_dice_specs(d->next);
}
}
void free_dice(struct roll_encoding *);
void free_dice(struct roll_encoding *d) {
if(d->next != NULL) {
free_dice(d->next);
}
dice_init(d);
free(d);
}
void dice_reset(struct roll_encoding *d) {
if(d->next != NULL) {
dice_reset(d->next);
}
dice_init(d);
free(d);
}
void dice_init(struct roll_encoding *d) {
d->ndice = 0;
d->nsides = 0;
d->dir = pos;
d->explode = false;
d->keep = false;
d->discard = 0;
d->next = NULL;
}
double runif() {
return (double)random()/RAND_MAX;
}
long single_dice_outcome(struct roll_encoding *d) {
if(d->nsides < 1) {
fprintf(stderr, "Invalid number of sides: %ld\n", d->nsides);
return LONG_MIN;
} else if(d->nsides == 1) {
return 1;
} else {
long roll = ceil(runif()*d->nsides);
long sum = roll;
if(d->explode) {
while(roll == d->nsides) {
roll = ceil(runif()*d->nsides);
sum += roll;
}
}
return sum;
}
}
long parallelised_total_dice_outcome(struct roll_encoding *d) {
if(d->nsides == 1) { // Optimise for non-random parts eg the "+1" in "d4+1".
return d->ndice;
}
long sum = 0;
bool keep_going = true;
long *rolls = malloc(sizeof(long)*d->ndice);
long roll_num;
#pragma omp parallel for private(roll_num) shared(keep_going, rolls)
for(roll_num = 0; roll_num < d->ndice && keep_going; ++roll_num) {
if(keep_going) {
rolls[roll_num] = single_dice_outcome(d);
sum += rolls[roll_num];
}
if(break_print_loop) {
keep_going = false;
}
}
if(d->discard > 0) {
qsort_r(rolls, d->ndice, sizeof(long), integer_difference_sign, NULL);
long remove = 0;
for(roll_num = 0; roll_num < d->ndice && roll_num < d->discard; ++roll_num) {
remove += rolls[roll_num];
}
sum -= remove;
}
free(rolls);
return sum;
}
long serial_total_dice_outcome(struct roll_encoding *d) {
if(d->nsides == 1) { // Optimise for non-random parts eg the "+1" in "d4+1".
return d->ndice;
}
long roll_num = 0;
long sum = 0;
long *rolls = malloc(sizeof(long)*d->ndice);
for(roll_num = 0; roll_num < d->ndice; ++roll_num) {
rolls[roll_num] = single_dice_outcome(d);
sum += rolls[roll_num];
if(break_print_loop) {
break;
}
}
if(d->discard > 0) {
qsort_r(rolls, d->ndice, sizeof(long), integer_difference_sign, NULL);
long remove = 0;
for(roll_num = 0; roll_num < d->ndice && roll_num < d->discard; ++roll_num) {
remove += rolls[roll_num];
}
sum -= remove;
}
free(rolls);
return sum;
}
void check_roll_sanity(const struct parse_tree* t) {
signal(SIGINT, sigint_handler);
break_print_loop = false;
if(t->dice_specs == NULL) {
return;
}
struct roll_encoding *d = t->dice_specs;
long cumulative_dice_range[] = { 0, 0 };
bool continuing = true;
bool warning = false;
while(continuing) {
long ndice = d->ndice;
if(d->keep) {
ndice = d->ndice - d->discard < 0 ? 0 : d->ndice - d->discard;
}
switch(d->dir) {
case pos:
if(cumulative_dice_range[1] > 0 && ndice > 0 && (LONG_MAX-cumulative_dice_range[1])/d->ndice < d->nsides) {
warning = true;
} else {
cumulative_dice_range[0] += d->nsides;
cumulative_dice_range[1] += ndice*d->nsides;
}
break;
case neg:
if(cumulative_dice_range[1] < 0 && ndice > 0 && (LONG_MIN-cumulative_dice_range[0])/d->ndice > -d->nsides) {
warning = true;
} else {
cumulative_dice_range[0] -= ndice*d->nsides;
cumulative_dice_range[1] -= d->nsides;
}
break;
default: {}
}
if(d->next != NULL && !warning && !break_print_loop) {
d = d->next;
} else {
continuing = false;
}
}
if(warning) {
fprintf(stderr, "Warning: ");
print_dice_specs(t->dice_specs);
fprintf(stderr, " are prone to integer overflow.\n");
}
}
void parallelised_rep_rolls(const struct parse_tree *t) {
if(t->dice_specs == NULL) {
return;
}
long rep = 0;
long nsuccess = 0;
bool keep_going = true;
#pragma omp parallel for private(rep) shared(keep_going)
for(rep = 0; rep < t->nreps; ++rep) {
if(rep != 0 && !t->use_threshold) {
printf(" ");
}
struct roll_encoding *d = t->dice_specs;
long result = 0;
while(d != NULL && keep_going) {
if(d->ndice > 0 && d->nsides > 0) {
result += d->dir * serial_total_dice_outcome(d);
}
if(d->next != NULL) {
d = d->next;
} else {
d = NULL;
}
if(break_print_loop) {
keep_going = false;
}
}
if(t->use_threshold) {
nsuccess += result >= t->threshold;
} else {
printf("%ld", result);
}
}
if(t->use_threshold) {
printf("%ld", nsuccess);
}
}
void serial_rep_rolls(const struct parse_tree *t) {
if(t->dice_specs == NULL) {
return;
}
long rep = 0;
long nsuccess = 0;
for(rep = 0; rep < t->nreps; ++rep) {
if(rep != 0 && !t->use_threshold) {
printf(" ");
}
struct roll_encoding *d = t->dice_specs;
long result = 0;
while(d != NULL) {
if(d->ndice > 0 && d->nsides > 0) {
result += d->dir * parallelised_total_dice_outcome(d);
}
if(d->next != NULL) {
d = d->next;
} else {
d = NULL;
}
if(break_print_loop) {
break;
}
}
if(t->use_threshold) {
nsuccess += result >= t->threshold;
} else {
printf("%ld", result);
}
}
if(t->use_threshold) {
printf("%ld", nsuccess);
}
}
void roll(const struct parse_tree *t) {
signal(SIGINT, sigint_handler);
break_print_loop = false;
check_roll_sanity(t);
if(t->nreps > t->ndice) {
parallelised_rep_rolls(t);
} else {
serial_rep_rolls(t);
}
printf("\n");
#pragma omp flush
}