mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 13:06:59 -04:00
random pack generation works
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1045 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
+101
-9
@@ -8,6 +8,13 @@
|
||||
|
||||
#include <util/prec.hpp>
|
||||
#include <data/pack.hpp>
|
||||
#include <data/set.hpp>
|
||||
#include <data/game.hpp>
|
||||
#include <data/card.hpp>
|
||||
|
||||
DECLARE_TYPEOF_COLLECTION(PackItemRefP);
|
||||
DECLARE_TYPEOF_COLLECTION(PackItemP);
|
||||
DECLARE_TYPEOF_COLLECTION(CardP);
|
||||
|
||||
// ----------------------------------------------------------------------------- : PackType
|
||||
|
||||
@@ -21,27 +28,84 @@ IMPLEMENT_REFLECTION(PackType) {
|
||||
REFLECT(items);
|
||||
}
|
||||
|
||||
void PackType::generate(Set& set, vector<CardP>& out) const {
|
||||
//%FOR_EACH(card_type, card_types) {
|
||||
//% card_type->generate(set,out);
|
||||
//%}
|
||||
bool PackType::update(Context& ctx) {
|
||||
bool change = enabled.update(ctx);
|
||||
FOR_EACH(item, items) {
|
||||
change |= item->update(ctx);
|
||||
}
|
||||
return change;
|
||||
}
|
||||
|
||||
void PackType::generate(PackItemCache& packs, boost::mt19937& gen, vector<CardP>& out) const {
|
||||
FOR_EACH_CONST(item, items) {
|
||||
item->generate(packs,gen,out);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : PackRefType
|
||||
|
||||
IMPLEMENT_REFLECTION_ENUM(PackRefType) {
|
||||
VALUE_N("replace", PACK_REF_REPLACE);
|
||||
VALUE_N("no replace", PACK_REF_NO_REPLACE);
|
||||
VALUE_N("cyclic", PACK_REF_CYCLIC);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : PackItemRef
|
||||
|
||||
PackItemRef::PackItemRef()
|
||||
: amount(1)
|
||||
, type(PACK_REF_REPLACE)
|
||||
{}
|
||||
|
||||
IMPLEMENT_REFLECTION(PackItemRef) {
|
||||
REFLECT(name);
|
||||
REFLECT(amount);
|
||||
REFLECT(type);
|
||||
}
|
||||
|
||||
bool PackItemRef::update(Context& ctx) {
|
||||
return amount.update(ctx);
|
||||
}
|
||||
|
||||
/// 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;
|
||||
};
|
||||
|
||||
void PackItemRef::generate(PackItemCache& packs, boost::mt19937& gen, vector<CardP>& out) const {
|
||||
vector<CardP>& cards = packs.cardsFor(name);
|
||||
// generate 'amount' cards and add them to out
|
||||
if (cards.empty()) return;
|
||||
if (type == PACK_REF_REPLACE) {
|
||||
// amount random numbers
|
||||
for (int i = 0 ; i < amount ; ++i) {
|
||||
size_t index = gen() % cards.size();
|
||||
out.push_back(cards[index]);
|
||||
}
|
||||
} else if (type == PACK_REF_NO_REPLACE) {
|
||||
// random shuffle
|
||||
// to prevent us from being too predictable for small sets, periodically reshuffle
|
||||
RandomRange<boost::mt19937> gen_range(gen);
|
||||
size_t max_per_batch = (cards.size() + 1) / 2;
|
||||
int rem = amount;
|
||||
while (rem > 0) {
|
||||
random_shuffle(cards.begin(), cards.end(), gen_range);
|
||||
out.insert(out.end(), cards.begin(), cards.begin() + min((size_t)rem, max_per_batch));
|
||||
rem -= (int)max_per_batch;
|
||||
}
|
||||
} else if (type == PACK_REF_CYCLIC) {
|
||||
// multiple copies
|
||||
size_t copies = amount / cards.size();
|
||||
FOR_EACH_CONST(card, cards) {
|
||||
out.insert(out.end(),copies,card);
|
||||
}
|
||||
// TODO: what if amount is not a multiple of the number of cards?
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : PackItem
|
||||
|
||||
IMPLEMENT_REFLECTION(PackItem) {
|
||||
@@ -50,9 +114,37 @@ IMPLEMENT_REFLECTION(PackItem) {
|
||||
}
|
||||
|
||||
void PackItem::generate(Set& set, vector<CardP>& out) const {
|
||||
//%Context& ctx = set.getContext();
|
||||
//%amount.update(ctx);
|
||||
//%FOR_EACH(card_type, card_types) {
|
||||
//% card_type->generate(set,out);
|
||||
//%}
|
||||
FOR_EACH(card, set.cards) {
|
||||
Context& ctx = set.getContext(card);
|
||||
bool keep = *filter.invoke(ctx);
|
||||
if (keep) {
|
||||
out.push_back(card);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : PackItemCache
|
||||
|
||||
PackItemCache::PackItemCache(Set& set)
|
||||
: set(set)
|
||||
{}
|
||||
|
||||
vector<CardP>& PackItemCache::cardsFor(const String& name) {
|
||||
// lookup name
|
||||
map<String,Cards>::iterator it = item_cards.find(name);
|
||||
if (it != item_cards.end()) {
|
||||
return *it->second;
|
||||
} else {
|
||||
// not used before, generate list and cache
|
||||
FOR_EACH(item, set.game->pack_items) {
|
||||
if (item->name == name) {
|
||||
Cards cards(new vector<CardP>);
|
||||
item->generate(set,*cards);
|
||||
item_cards.insert(make_pair(name,cards));
|
||||
return *cards;
|
||||
}
|
||||
}
|
||||
// still not found
|
||||
throw Error(_ERROR_1_("pack item not found",name));
|
||||
}
|
||||
}
|
||||
|
||||
+33
-5
@@ -12,10 +12,12 @@
|
||||
#include <util/prec.hpp>
|
||||
#include <util/reflect.hpp>
|
||||
#include <script/scriptable.hpp>
|
||||
#include <boost/random/mersenne_twister.hpp>
|
||||
|
||||
DECLARE_POINTER_TYPE(PackItemRef);
|
||||
DECLARE_POINTER_TYPE(Card);
|
||||
class Set;
|
||||
class PackItemCache;
|
||||
|
||||
// ----------------------------------------------------------------------------- : PackType
|
||||
|
||||
@@ -28,8 +30,11 @@ class PackType : public IntrusivePtrBase<PackType> {
|
||||
Scriptable<bool> enabled; ///< Is this pack enabled?
|
||||
vector<PackItemRefP> items; ///< Cards in this pack
|
||||
|
||||
/// Update scripts, returns true if there is a change
|
||||
bool update(Context& ctx);
|
||||
|
||||
/// Generate a random pack of cards, add them to out
|
||||
void generate(Set& set, vector<CardP>& out) const;
|
||||
void generate(PackItemCache& packs, boost::mt19937& gen, vector<CardP>& out) const;
|
||||
|
||||
private:
|
||||
DECLARE_REFLECTION();
|
||||
@@ -37,18 +42,25 @@ class PackType : public IntrusivePtrBase<PackType> {
|
||||
|
||||
// ----------------------------------------------------------------------------- : PackItemRef
|
||||
|
||||
enum PackRefType
|
||||
{ PACK_REF_REPLACE
|
||||
, PACK_REF_NO_REPLACE
|
||||
, PACK_REF_CYCLIC
|
||||
};
|
||||
|
||||
class PackItemRef : public IntrusivePtrBase<PackItemRef> {
|
||||
public:
|
||||
PackItemRef();
|
||||
|
||||
String name; ///< Name of this type of cards
|
||||
Scriptable<int> amount; ///< Number of cards of this type
|
||||
PackRefType type;
|
||||
|
||||
/// Update scripts, returns true if there is a change
|
||||
bool update(Context& ctx);
|
||||
|
||||
/// Generate random cards, add them to out
|
||||
void generate(Set& set, vector<CardP>& out) const;
|
||||
void generate(PackItemCache& packs, boost::mt19937& gen, vector<CardP>& out) const;
|
||||
|
||||
private:
|
||||
DECLARE_REFLECTION();
|
||||
@@ -59,15 +71,31 @@ class PackItemRef : public IntrusivePtrBase<PackItemRef> {
|
||||
/// A card type description for playtesting
|
||||
class PackItem : public IntrusivePtrBase<PackItem> {
|
||||
public:
|
||||
String name; ///< Name of this type of cards
|
||||
OptionalScript filter; ///< Filter to select this type of cards
|
||||
String name; ///< Name of this type of cards
|
||||
OptionalScript filter; ///< Filter to select this type of cards
|
||||
|
||||
/// Generate random cards, add them to out
|
||||
/// Select *all* cards matching the filter
|
||||
void generate(Set& set, vector<CardP>& out) const;
|
||||
|
||||
private:
|
||||
DECLARE_REFLECTION();
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : PackItemCache
|
||||
|
||||
class PackItemCache {
|
||||
public:
|
||||
PackItemCache(Set& set);
|
||||
|
||||
/// The cards for a given PackItem
|
||||
vector<CardP>& cardsFor(const String& name);
|
||||
|
||||
private:
|
||||
Set& set;
|
||||
/// Cards for each PackItem
|
||||
typedef shared_ptr<vector<CardP> > Cards;
|
||||
map<String,Cards> item_cards;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : EOF
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user