-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdice.cpp
More file actions
266 lines (251 loc) · 6.92 KB
/
dice.cpp
File metadata and controls
266 lines (251 loc) · 6.92 KB
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
#include "dice.h"
#include <queue>
#include <assert.h>
#include <stdio.h>
#include <chrono>
#include <unordered_set>
// Inspired by https://github.com/cdanek/KaimiraWeightedList
void const_roll_table::init(const std::vector<int>& weights)
{
size = (int)weights.size();
alias.assign(size, -1);
probability.resize(size);
sum = std::accumulate(weights.cbegin(), weights.cend(), 0);
std::queue<int> small;
std::queue<int> large;
std::vector<int> w;
w.reserve(size);
for (int i : weights)
{
w.push_back(i * size);
}
for (int i = 0; i < size; i++)
{
if (w[i] < sum) { small.push(i); }
else { large.push(i); }
}
while (small.size() > 0 && large.size() > 0)
{
const int lv = large.front(); large.pop();
const int sv = small.front(); small.pop();
probability[sv] = w.at(sv);
alias[sv] = lv;
const int tmp = w[lv] + w[sv] - sum;
w[lv] = tmp;
if (tmp < sum) small.push(lv);
else large.push(lv);
}
while (large.size() > 0)
{
int lv = large.front(); large.pop();
probability[lv] = sum;
}
}
const_roll_table::const_roll_table(const std::vector<int>& weights) : const_roll_table(0) { init(weights); }
filtered_const_roll_table::filtered_const_roll_table(const std::vector<int>& weights, const std::vector<bool>& mask) : const_roll_table(0)
{
assert(weights.size() == mask.size());
std::vector<int> filtered;
filtered.reserve(mask.size());
indices.reserve(mask.size());
for (size_t i = 0; i < mask.size(); i++)
{
if (mask[i])
{
filtered.push_back(weights[i]);
indices.push_back((int)i);
}
}
assert(!filtered.empty());
init(filtered);
}
// here so we do not have to include the chrono header in our public header
seed seed_random()
{
uint64_t v;
do
{
v = static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count());
} while (v == 0);
return seed(v);
}
roll_table::roll_table(const seed& orig, const std::vector<int>& input) : s(orig)
{
// Sort by weight
std::multimap<int, int> tmp;
for (unsigned i = 0; i < input.size(); i++)
{
tmp.emplace(input[i], i);
}
// Create roll table
size = -1; // to account for a zero roll result
for (auto iter = tmp.rbegin(); iter != tmp.rend(); ++iter)
{
size += (*iter).first;
table[size] = (*iter).second;
}
size--;
}
luck_type luck_combine(luck_type l, luck_type against)
{
switch (against)
{
case luck_type::mediocre:
if (l == luck_type::uncommon) l = luck_type::normal;
else if (l == luck_type::very_lucky) l = luck_type::lucky;
else if (l == luck_type::lucky) l = luck_type::normal;
else l = against;
break;
case luck_type::uncommon:
if (l == luck_type::mediocre) l = luck_type::normal;
else if (l == luck_type::very_unlucky) l = luck_type::unlucky;
else if (l == luck_type::unlucky) l = luck_type::normal;
else l = against;
break;
case luck_type::lucky:
if (l == luck_type::normal) l = luck_type::unlucky;
else if (l == luck_type::lucky) l = luck_type::normal;
else if (l == luck_type::very_lucky) l = luck_type::lucky;
else if (l == luck_type::unlucky) l = luck_type::very_unlucky;
break;
case luck_type::unlucky:
if (l == luck_type::normal) l = luck_type::lucky;
else if (l == luck_type::lucky) l = luck_type::very_lucky;
else if (l == luck_type::very_unlucky) l = luck_type::unlucky;
else if (l == luck_type::unlucky) l = luck_type::normal;
break;
case luck_type::very_lucky:
if (l == luck_type::normal || l == luck_type::unlucky) l = luck_type::very_unlucky;
else if (l == luck_type::very_lucky) l = luck_type::normal;
else if (l == luck_type::lucky) l = luck_type::unlucky;
break;
case luck_type::very_unlucky:
if (l == luck_type::normal || l == luck_type::lucky) l = luck_type::very_lucky;
else if (l == luck_type::very_unlucky) l = luck_type::normal;
else if (l == luck_type::unlucky) l = luck_type::lucky;
break;
default: break;
}
return l;
}
int seed::roll(int low, int high, luck_type luck, int jackpot_chance, int jackpot_low, int jackpot_high, luck_type jackpot_luck)
{
const int avg = (high - low) / 2;
int v1 = roll(low, high);
if (luck == luck_type::normal)
{
// nothing extra
}
else if (luck == luck_type::mediocre)
{
const int v2 = roll(low, high);
const int dist1 = v1 - low - avg;
const int dist2 = v2 - low - avg;
if (dist1*dist1 > dist2*dist2) v1 = v2;
}
else if (luck == luck_type::uncommon)
{
const int v2 = roll(low, high);
const int dist1 = v1 - low - avg;
const int dist2 = v2 - low - avg;
if (dist1*dist1 < dist2*dist2) v1 = v2;
}
else if (luck == luck_type::lucky)
{
const int v2 = roll(low, high);
if (v2 > v1) v1 = v2;
}
else if (luck == luck_type::unlucky)
{
const int v2 = roll(low, high);
if (v2 < v1) v1 = v2;
}
else if (luck == luck_type::very_lucky)
{
const int v2 = roll(low, high);
const int v3 = roll(low, high);
if (v2 > v1) v1 = v2;
if (v3 > v1) v1 = v3;
}
else if (luck == luck_type::very_unlucky)
{
const int v2 = roll(low, high);
const int v3 = roll(low, high);
if (v2 < v1) v1 = v2;
if (v3 < v1) v1 = v3;
}
if (v1 > high - jackpot_chance)
{
v1 += roll(jackpot_low, jackpot_high, jackpot_luck);
}
return v1;
}
int roll_table::unique_rolls(int count, int* results, luck_type rollee_luck, int roll_weight, int start_index)
{
if (count <= 0) return 0;
assert(size > 0);
assert(!table.empty());
roll_weight = std::clamp(roll_weight, 0, 128);
roll_weight = (size * roll_weight) >> 7;
roll_weight = std::min(roll_weight, size / 2);
const bool use_set = (count + start_index) > 16;
std::unordered_set<int> seen;
if (use_set)
{
seen.reserve(count + start_index);
for (int j = 0; j < start_index; j++)
{
seen.insert(results[j]);
}
}
for (int i = 0; i < count; i++)
{
if ((int)table.size() <= i + start_index) return i; // ran out of options
repeat:
const int r = s.roll(roll_weight, size - roll_weight, rollee_luck);
const int k = lookup(r);
if (use_set)
{
if (!seen.insert(k).second) goto repeat;
}
else
{
for (int j = 0; j < start_index + i; j++)
{
if (results[j] == k) goto repeat;
}
}
results[start_index + i] = k;
}
return count;
}
int roll_table::rolls(int count, int* results, luck_type rollee_luck, int roll_weight)
{
if (count <= 0) return 0;
assert(size > 0);
assert(!table.empty());
roll_weight = std::clamp(roll_weight, 0, 128);
roll_weight = (size * roll_weight) >> 7;
roll_weight = std::min(roll_weight, size / 2);
for (int i = 0; i < count; i++)
{
const int r = s.roll(roll_weight, size - roll_weight, rollee_luck);
const int k = lookup(r);
results[i] = k;
}
return count;
}
int roll_table::boxgacha(luck_type rollee_luck, int roll_weight)
{
if (table.size() == 0) return -1;
assert(size > 0);
roll_weight = std::clamp(roll_weight, 0, 128);
roll_weight = (size * roll_weight) >> 7;
roll_weight = std::min(roll_weight, size / 2);
const int r = s.roll(roll_weight, size - roll_weight, rollee_luck);
auto it = table.upper_bound(r);
if (it == table.end()) it = table.begin();
const int ret = (*it).second;
table.erase((*it).first);
return ret;
}