mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-13 14:07:01 -04:00
Use std::shuffle instead of std::random_shuffle, since the latter is removed in C++17
This commit is contained in:
+1
-10
@@ -179,14 +179,6 @@ void PackInstance::request_copy(size_t copies) {
|
|||||||
requested_copies += 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 {
|
struct WeightedItem {
|
||||||
double weight;
|
double weight;
|
||||||
int count;
|
int count;
|
||||||
@@ -248,11 +240,10 @@ void PackInstance::generate(vector<CardP>* out) {
|
|||||||
// NOTE: there is no way to pick items without replacement
|
// NOTE: there is no way to pick items without replacement
|
||||||
if (out && !cards.empty()) {
|
if (out && !cards.empty()) {
|
||||||
// to prevent us from being too predictable for small sets, periodically reshuffle
|
// 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 max_per_batch = ((int)cards.size() + 1) / 2;
|
||||||
int rem = (int)requested_copies;
|
int rem = (int)requested_copies;
|
||||||
while (rem > 0) {
|
while (rem > 0) {
|
||||||
random_shuffle(cards.begin(), cards.end(), gen_range);
|
shuffle(cards.begin(), cards.end(), parent.gen);
|
||||||
out->insert(out->end(), cards.begin(), cards.begin() + min(rem, max_per_batch));
|
out->insert(out->end(), cards.begin(), cards.begin() + min(rem, max_per_batch));
|
||||||
rem -= max_per_batch;
|
rem -= max_per_batch;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
#include <data/set.hpp>
|
#include <data/set.hpp>
|
||||||
#include <data/card.hpp>
|
#include <data/card.hpp>
|
||||||
#include <data/game.hpp>
|
#include <data/game.hpp>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
DECLARE_TYPEOF_COLLECTION(pair<String COMMA ScriptValueP>);
|
DECLARE_TYPEOF_COLLECTION(pair<String COMMA ScriptValueP>);
|
||||||
|
|
||||||
@@ -595,6 +596,12 @@ SCRIPT_FUNCTION(sort_list) {
|
|||||||
return sort_script(ctx, input, *order_by, remove_duplicates);
|
return sort_script(ctx, input, *order_by, remove_duplicates);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename It>
|
||||||
|
void shuffle(It begin, It end) {
|
||||||
|
std::random_device rng;
|
||||||
|
std::mt19937 urng(rng());
|
||||||
|
std::shuffle(begin, end, urng);
|
||||||
|
}
|
||||||
|
|
||||||
SCRIPT_FUNCTION(random_shuffle) {
|
SCRIPT_FUNCTION(random_shuffle) {
|
||||||
SCRIPT_PARAM_C(ScriptValueP, input);
|
SCRIPT_PARAM_C(ScriptValueP, input);
|
||||||
@@ -605,7 +612,7 @@ SCRIPT_FUNCTION(random_shuffle) {
|
|||||||
ret->value.push_back(v);
|
ret->value.push_back(v);
|
||||||
}
|
}
|
||||||
// shuffle
|
// shuffle
|
||||||
random_shuffle(ret->value.begin(), ret->value.end());
|
shuffle(ret->value.begin(), ret->value.end());
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -643,7 +650,7 @@ SCRIPT_FUNCTION(random_select_many) {
|
|||||||
while (ScriptValueP v = it->next()) {
|
while (ScriptValueP v = it->next()) {
|
||||||
ret->value.push_back(v);
|
ret->value.push_back(v);
|
||||||
}
|
}
|
||||||
random_shuffle(ret->value.begin(), ret->value.end());
|
shuffle(ret->value.begin(), ret->value.end());
|
||||||
// keep only the first 'count'
|
// keep only the first 'count'
|
||||||
ret->value.resize(count);
|
ret->value.resize(count);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user