Repository navigation
Expand file tree
/
Copy pathpack.cpp
More file actions
445 lines (409 loc) · 13.4 KB
/
Copy pathpack.cpp
File metadata and controls
445 lines (409 loc) · 13.4 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2012 Twan van Laarhoven and Sean Hunt |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <data/pack.hpp>
#include <data/set.hpp>
#include <data/game.hpp>
#include <data/card.hpp>
#include <queue>
using boost::indeterminate;
using std::max;
using std::min;
using std::priority_queue;
// ----------------------------------------------------------------------------- : PackType
IMPLEMENT_REFLECTION_ENUM(PackSelectType) {
VALUE_N("auto", SELECT_AUTO);
VALUE_N("all", SELECT_ALL);
VALUE_N("no replace", SELECT_NO_REPLACE);
VALUE_N("replace", SELECT_REPLACE);
VALUE_N("proportional", SELECT_PROPORTIONAL);
VALUE_N("nonempty", SELECT_NONEMPTY);
VALUE_N("equal", SELECT_EQUAL);
VALUE_N("equal proportional", SELECT_EQUAL_PROPORTIONAL);
VALUE_N("equal nonempty", SELECT_NONEMPTY);
VALUE_N("first", SELECT_FIRST);
}
IMPLEMENT_REFLECTION(PackType) {
REFLECT(name);
REFLECT(enabled);
REFLECT(selectable);
REFLECT(summary);
REFLECT(select);
REFLECT(filter);
REFLECT(items);
}
void after_reading(PackType& pt, Version) {
if (pt.select == SELECT_AUTO) {
if (pt.filter) pt.select = SELECT_NO_REPLACE;
else if (!pt.items.empty()) pt.select = SELECT_ALL;
}
if (indeterminate(pt.summary)) {
if (pt.filter) pt.summary = true;
else if (!pt.items.empty()) pt.summary = false;
}
if (indeterminate(pt.selectable)) {
if (pt.filter) pt.selectable = false;
else if (!pt.items.empty()) pt.selectable = true;
}
}
IMPLEMENT_REFLECTION(PackItem) {
REFLECT_IF_READING_COMPOUND_OR(true) {
REFLECT(name);
REFLECT(amount);
REFLECT(weight);
} else {
REFLECT_NAMELESS(name);
}
}
PackType::PackType()
: enabled(true)
, selectable(indeterminate)
, summary(indeterminate)
, select(SELECT_AUTO)
{}
PackItem::PackItem()
: amount(1)
, weight(1)
{}
PackItem::PackItem(const String& name, int amount)
: name(name)
, amount(amount)
, weight(1)
{}
bool PackType::update(Context& ctx) {
bool change = enabled.update(ctx);
for(auto& item : items) {
change |= item->update(ctx);
}
return change;
}
bool PackItem::update(Context& ctx) {
return amount.update(ctx)
| weight.update(ctx);
}
// ----------------------------------------------------------------------------- : PackInstance
PackInstance::PackInstance(const PackType& pack_type, PackGenerator& parent)
: pack_type(pack_type)
, parent(parent)
, requested_copies(0)
, card_copies(0)
, expected_copies(0)
{
// Filter cards
if (pack_type.filter) {
for(auto& card : parent.set->cards) {
Context& ctx = parent.set->getContext(card);
bool keep = pack_type.filter.invoke(ctx)->toBool();
if (keep) {
cards.push_back(card);
}
}
}
// Sum of weights
if (pack_type.select == SELECT_FIRST) {
total_weight = cards.empty() ? 0 : 1;
} else {
total_weight = cards.size();
}
for(const auto& item : pack_type.items) {
if (pack_type.select == SELECT_PROPORTIONAL || pack_type.select == SELECT_EQUAL_PROPORTIONAL) {
total_weight += item->weight * parent.get(item->name).total_weight;
} else if (pack_type.select == SELECT_NONEMPTY || pack_type.select == SELECT_EQUAL_NONEMPTY) {
if (parent.get(item->name).total_weight > 0) {
total_weight += item->weight;
}
} else if (pack_type.select == SELECT_FIRST) {
if (total_weight <= 0) {
total_weight = item->weight;
break;
}
} else {
total_weight += item->weight;
}
}
// Depth
depth = 0;
for(const auto& item : pack_type.items) {
depth = max(depth, 1 + parent.get(item->name).depth);
}
}
void PackInstance::expect_copy(double copies) {
this->expected_copies += copies;
// propagate
for(const auto& item : pack_type.items) {
PackInstance& i = parent.get(item->name);
if (pack_type.select == SELECT_ALL) {
i.expect_copy(copies * item->amount);
} else if (pack_type.select == SELECT_PROPORTIONAL || pack_type.select == SELECT_EQUAL_PROPORTIONAL) {
i.expect_copy(copies * item->amount * item->weight * i.total_weight / total_weight);
} else if (pack_type.select == SELECT_NONEMPTY || pack_type.select == SELECT_EQUAL_NONEMPTY) {
if (i.total_weight > 0) {
i.expect_copy(copies * item->amount * item->weight / total_weight);
}
} else if (pack_type.select == SELECT_FIRST) {
if (i.total_weight > 0 && cards.empty()) {
i.expect_copy(copies * item->amount);
break;
}
} else {
i.expect_copy(copies * item->amount * item->weight / total_weight);
}
}
}
void PackInstance::request_copy(size_t copies) {
requested_copies += copies;
}
/// Random generator with random numbers in a range
template <typename Gen>
struct RandomRange {
RandomRange(Gen& gen) : gen(gen) {}
unsigned operator () (unsigned max) { return gen() % max; }
Gen& gen;
};
struct WeightedItem {
size_t weight;
int count;
uint_fast32_t tiebreaker;
};
struct CompareWeightedItems{
inline bool operator () (WeightedItem* a, WeightedItem* b) {
// compare (a->count+1)/a->weight <> (b->count+1)/b->weight
// prefer the one where this is lower, return true if b is prefered
double delta = b->weight * (a->count + 1) - a->weight * (b->count + 1);
if (delta < 0) return false;
if (delta > 0) return true;
return b->tiebreaker < a->tiebreaker;
}
};
/// Distribute 'total' among the weighted items, higher weight items get chosen more often
void weighted_equal_divide(vector<WeightedItem>& items, int total) {
assert(!items.empty());
if (items.size() == 1) {
items.front().count = total;
} else {
priority_queue<WeightedItem*,vector<WeightedItem*>,CompareWeightedItems> pq;
for (size_t i = 0 ; i < items.size() ; ++i) {
pq.push(&items[i]);
}
while (total > 0) {
// repeatedly pick the item that minimizes, after incrementing count:
// max_wi wi->count/wi->weight
WeightedItem* wi = pq.top();pq.pop();
wi->count++;
total--;
pq.push(wi);
}
}
}
void PackInstance::generate(vector<CardP>* out) {
card_copies = 0;
if (requested_copies == 0) return;
if (pack_type.select == SELECT_ALL) {
// add all cards
generate_all(out, requested_copies);
} else if (pack_type.select == SELECT_REPLACE
|| pack_type.select == SELECT_PROPORTIONAL
|| pack_type.select == SELECT_NONEMPTY) {
// multiple copies
for (size_t i = 0 ; i < requested_copies ; ++i) {
generate_one_random(out);
}
} else if (pack_type.select == SELECT_NO_REPLACE) {
if (!pack_type.items.empty()) {
throw Error(_("'select:no replace' is not yet supported in combination with 'items', only with 'filter'."));
}
card_copies += requested_copies;
// NOTE: there is no way to pick items without replacement
if (out && !cards.empty()) {
// to prevent us from being too predictable for small sets, periodically reshuffle
RandomRange<boost::mt19937> gen_range(parent.gen);
int max_per_batch = ((int)cards.size() + 1) / 2;
int rem = (int)requested_copies;
while (rem > 0) {
random_shuffle(cards.begin(), cards.end(), gen_range);
out->insert(out->end(), cards.begin(), cards.begin() + min(rem, max_per_batch));
rem -= max_per_batch;
}
}
} else if (pack_type.select == SELECT_EQUAL
|| pack_type.select == SELECT_EQUAL_PROPORTIONAL
|| pack_type.select == SELECT_EQUAL_NONEMPTY) {
// equal selection instead of random
if (requested_copies == 1) {
// somewhat of a hack to keep things fair: just pick at random
// otherwise we would end up picking the lowest weight item
generate_one_random(out);
} else {
// 1. the weights of each item, and of the cards
vector<WeightedItem> weighted_items;
for(const auto& item : pack_type.items) {
WeightedItem wi = {0,0,parent.gen()};
if (pack_type.select == SELECT_EQUAL_PROPORTIONAL) {
wi.weight = item->weight * parent.get(item->name).total_weight;
} else if (pack_type.select == SELECT_EQUAL_NONEMPTY) {
wi.weight = parent.get(item->name).total_weight > 0 ? static_cast<int>(item->weight) : 0;
} else {
wi.weight = item->weight;
}
weighted_items.push_back(wi);
}
WeightedItem wi = {cards.size(),0,parent.gen()};
weighted_items.push_back(wi);
// 2. divide the requested_copies among the cards and the items, taking the weights into account
weighted_equal_divide(weighted_items, (int)requested_copies);
// 3a. propagate to items
for (size_t j = 0 ; j < pack_type.items.size() ; ++j) {
const PackItem& item = *pack_type.items[j];
PackInstance& i = parent.get(item.name);
i.request_copy(item.amount * weighted_items[j].count);
}
// 3b. pick some cards
int new_card_copies = weighted_items.back().count;
card_copies += new_card_copies;
if (out && !cards.empty()) {
int div = new_card_copies / (int)cards.size();
int rem = new_card_copies % (int)cards.size();
// some copies of all cards
for (int i = 0 ; i < div ; ++i) {
out->insert(out->end(), cards.begin(), cards.end());
}
// pick the remainder at random
for (int i = 0 ; i < rem ; ++i) {
int nr = parent.gen() % cards.size();
out->push_back(cards.at(nr));
}
}
}
} else if (pack_type.select == SELECT_FIRST) {
if (!cards.empty()) {
// there is a card, pick it
card_copies += requested_copies;
if (out) out->insert(out->end(), requested_copies, cards.front());
} else {
// pick first nonempty item
for(const auto& item : pack_type.items) {
PackInstance& i = parent.get(item->name);
if (i.total_weight > 0) {
i.request_copy(requested_copies * item->amount);
break;
}
}
}
}
requested_copies = 0;
}
void PackInstance::generate_all(vector<CardP>* out, size_t copies) {
card_copies += copies * cards.size();
if (out) {
for (size_t i = 0 ; i < copies ; ++i) {
out->insert(out->end(), cards.begin(), cards.end());
}
}
// and all items
for(const auto& item : pack_type.items) {
PackInstance& i = parent.get(item->name);
i.request_copy(copies * item->amount);
}
}
void PackInstance::generate_one_random(vector<CardP>* out) {
double r = parent.gen() * total_weight / parent.gen.max();
if (r < cards.size()) {
// pick a card
card_copies++;
if (out) {
int i = (int)r;
out->push_back(cards[i]);
}
} else {
// pick an item
r -= cards.size();
for(const auto& item : pack_type.items) {
PackInstance& i = parent.get(item->name);
if (pack_type.select == SELECT_PROPORTIONAL || pack_type.select == SELECT_EQUAL_PROPORTIONAL) {
r -= item->weight * i.total_weight;
} else if (pack_type.select == SELECT_NONEMPTY || pack_type.select == SELECT_EQUAL_NONEMPTY) {
if (i.total_weight > 0) r -= item->weight;
} else {
r -= item->weight;
}
// have we reached the item we were looking for?
if (r < 0) {
i.request_copy(item->amount);
break;
}
}
}
}
// ----------------------------------------------------------------------------- : PackGenerator
void PackGenerator::reset(const SetP& set, int seed) {
this->set = set;
gen.seed((unsigned)seed);
max_depth = 0;
instances.clear();
}
void PackGenerator::reset(int seed) {
gen.seed((unsigned)seed);
}
PackInstance& PackGenerator::get(const String& name) {
assert(set);
PackInstanceP& instance = instances[name];
if (instance) {
return *instance;
} else {
for(const auto& type : set->pack_types) {
if (type->name == name) {
instance = PackInstanceP(new PackInstance(*type,*this));
max_depth = max(max_depth, instance->get_depth());
return *instance;
}
}
for(const auto& type : set->game->pack_types) {
if (type->name == name) {
instance = PackInstanceP(new PackInstance(*type,*this));
max_depth = max(max_depth, instance->get_depth());
return *instance;
}
}
throw Error(_ERROR_1_("pack type not found",name));
}
}
PackInstance& PackGenerator::get(const PackTypeP& type) {
return get(type->name);
}
void PackGenerator::generate(vector<CardP>& out) {
if (!set) return;
// We generate from depth max_depth to 0
// instances can refer to other instances of lower depth, and generate
// can change the number of copies of those lower depth instances
for (int depth = max_depth ; depth >= 0 ; --depth) {
// in game file order
for(const auto& type : set->game->pack_types) {
PackInstance& i = get(type);
if (i.get_depth() == depth) {
i.generate(&out);
}
}
// ...and then set file order
for(const auto& type : set->pack_types) {
PackInstance& i = get(type);
if (i.get_depth() == depth) {
i.generate(&out);
}
}
}
}
void PackGenerator::update_card_counts() {
if (!set) return;
// update card_counts by using generate()
for (int depth = max_depth ; depth >= 0 ; --depth) {
for(const auto& i :instances) {
if (i.second->get_depth() == depth) {
i.second->generate(nullptr);
}
}
}
}