Commit 0e61ac11 authored by twanvl's avatar twanvl

Removed old random pack code. It was already disabled by the USE_NEW_PACK_SYSTEM flag.

parent 9fe5bc3e
......@@ -54,11 +54,7 @@ IMPLEMENT_REFLECTION(Game) {
REFLECT_NO_SCRIPT(card_list_color_script);
REFLECT_NO_SCRIPT(statistics_dimensions);
REFLECT_NO_SCRIPT(statistics_categories);
#if USE_NEW_PACK_SYSTEM
REFLECT_ALIAS(308, "pack item", "pack type");
#else
REFLECT_NO_SCRIPT(pack_items);
#endif
REFLECT_NO_SCRIPT(pack_types);
REFLECT_NO_SCRIPT(keyword_match_script);
REFLECT(has_keywords);
......
......@@ -14,14 +14,12 @@
#include <script/scriptable.hpp>
#include <script/dependency.hpp>
#include <util/dynamic_arg.hpp>
#include <data/pack.hpp> // for USE_NEW_PACK_SYSTEM
DECLARE_POINTER_TYPE(Field);
DECLARE_POINTER_TYPE(Style);
DECLARE_POINTER_TYPE(Game);
DECLARE_POINTER_TYPE(StatsDimension);
DECLARE_POINTER_TYPE(StatsCategory);
DECLARE_POINTER_TYPE(PackItem);
DECLARE_POINTER_TYPE(PackType);
DECLARE_POINTER_TYPE(KeywordParam);
DECLARE_POINTER_TYPE(KeywordMode);
......@@ -47,9 +45,6 @@ class Game : public Packaged {
OptionalScript card_list_color_script; ///< Script that determines the color of items in the card list
vector<StatsDimensionP> statistics_dimensions; ///< (Additional) statistics dimensions
vector<StatsCategoryP> statistics_categories; ///< (Additional) statistics categories
#if !USE_NEW_PACK_SYSTEM
vector<PackItemP> pack_items; ///< Types of cards in packs
#endif
vector<PackTypeP> pack_types; ///< Types of random card packs to generate
vector<WordListP> word_lists; ///< Word lists for editing with a drop down list
vector<AddCardsScriptP> add_cards_scripts; ///< Scripts for adding multiple cards to the set
......
......@@ -13,152 +13,6 @@
#include <data/card.hpp>
#include <queue>
#if !USE_NEW_PACK_SYSTEM
// =================================================================================================== OLD
DECLARE_TYPEOF_COLLECTION(PackItemRefP);
DECLARE_TYPEOF_COLLECTION(PackItemP);
DECLARE_TYPEOF_COLLECTION(CardP);
// ----------------------------------------------------------------------------- : PackType
PackType::PackType()
: enabled(true)
{}
IMPLEMENT_REFLECTION(PackType) {
REFLECT(name);
REFLECT(enabled);
REFLECT(items);
}
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_NO_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) {
REFLECT(name);
REFLECT(filter);
}
void PackItem::generate(Set& set, vector<CardP>& out) const {
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));
}
}
#else
// =================================================================================================== NEW
DECLARE_TYPEOF_COLLECTION(PackTypeP);
DECLARE_TYPEOF_COLLECTION(PackItemP);
DECLARE_TYPEOF_COLLECTION(CardP);
......@@ -588,5 +442,3 @@ void PackGenerator::update_card_counts() {
}
}
}
#endif
......@@ -15,100 +15,6 @@
#include <boost/random/mersenne_twister.hpp>
#include <boost/logic/tribool.hpp>
#define USE_NEW_PACK_SYSTEM 1
#if !USE_NEW_PACK_SYSTEM
// =================================================================================================== OLD
DECLARE_POINTER_TYPE(PackItemRef);
DECLARE_POINTER_TYPE(Card);
class Set;
class PackItemCache;
// ----------------------------------------------------------------------------- : PackType
/// A card pack description for playtesting
class PackType : public IntrusivePtrBase<PackType> {
public:
PackType();
String name; ///< Name of this pack
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(PackItemCache& packs, boost::mt19937& gen, vector<CardP>& out) const;
private:
DECLARE_REFLECTION();
};
// ----------------------------------------------------------------------------- : 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(PackItemCache& packs, boost::mt19937& gen, vector<CardP>& out) const;
private:
DECLARE_REFLECTION();
};
// ----------------------------------------------------------------------------- : PackItem
/// 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
/// 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;
};
#else
// =================================================================================================== NEW
DECLARE_POINTER_TYPE(PackType);
DECLARE_POINTER_TYPE(PackItem);
DECLARE_POINTER_TYPE(PackInstance);
......@@ -240,4 +146,3 @@ class PackGenerator {
// ----------------------------------------------------------------------------- : EOF
#endif
#endif
......@@ -12,6 +12,7 @@
#include <data/stylesheet.hpp>
#include <data/card.hpp>
#include <data/keyword.hpp>
#include <data/pack.hpp>
#include <data/field.hpp>
#include <data/field/text.hpp> // for 0.2.7 fix
#include <data/field/information.hpp>
......
......@@ -23,9 +23,6 @@
DECLARE_TYPEOF_COLLECTION(PackTypeP);
DECLARE_TYPEOF_COLLECTION(PackItemP);
#if !USE_NEW_PACK_SYSTEM
DECLARE_TYPEOF_COLLECTION(PackItemRefP);
#endif
DECLARE_TYPEOF_COLLECTION(CardP);
DECLARE_TYPEOF_COLLECTION(PackAmountPicker);
......@@ -38,25 +35,15 @@ class RandomCardList : public CardListBase {
/// Reset the list
void reset();
#if !USE_NEW_PACK_SYSTEM
/// Add a pack of cards
void add(PackItemCache& packs, boost::mt19937& gen, const PackType& pack);
#endif
using CardListBase::rebuild;
const vector<CardP>* getCardsPtr() const { return &cards; }
vector<CardP> cards;
protected:
virtual void getItems(vector<VoidP>& out) const;
virtual void onChangeSet();
#if USE_NEW_PACK_SYSTEM
public:
#else
private:
#endif
vector<CardP> cards;
};
RandomCardList::RandomCardList(Window* parent, int id, long style)
......@@ -67,12 +54,6 @@ void RandomCardList::reset() {
cards.clear();
}
#if !USE_NEW_PACK_SYSTEM
void RandomCardList::add(PackItemCache& packs, boost::mt19937& gen, const PackType& pack) {
pack.generate(packs,gen,cards);
}
#endif
void RandomCardList::onChangeSet() {
reset();
CardListBase::onChangeSet();
......@@ -90,18 +71,9 @@ void RandomCardList::getItems(vector<VoidP>& out) const {
class PackTotalsPanel : public wxPanel {
public:
#if USE_NEW_PACK_SYSTEM
PackTotalsPanel(Window* parent, int id, PackGenerator& generator, bool show_all = false)
: wxPanel(parent,id), generator(generator), show_all(show_all) {}
#else
PackTotalsPanel(Window* parent, int id) : wxPanel(parent,id) {}
#endif
void setGame(const GameP& game);
#if !USE_NEW_PACK_SYSTEM
void clear();
void addPack(PackType& pack, int copies);
void addItemRef(PackItemRef& item, int copies);
#endif
virtual wxSize DoGetBestSize() const;
private:
DECLARE_EVENT_TABLE();
......@@ -109,12 +81,8 @@ class PackTotalsPanel : public wxPanel {
void onPaint(wxPaintEvent&);
void draw(DC& dc);
void drawItem(DC& dc, int& y, const String& name, double value);
#if USE_NEW_PACK_SYSTEM
PackGenerator& generator;
bool show_all;
#else
map<String,int> amounts;
#endif
};
void PackTotalsPanel::onPaint(wxPaintEvent&) {
......@@ -132,7 +100,6 @@ void PackTotalsPanel::draw(DC& dc) {
dc.SetFont(*wxNORMAL_FONT);
int y = 0;
int total = 0;
#if USE_NEW_PACK_SYSTEM
FOR_EACH(pack, game->pack_types) {
PackInstance& i = generator.get(pack);
if (pack->summary && (show_all || i.has_cards())) {
......@@ -140,13 +107,6 @@ void PackTotalsPanel::draw(DC& dc) {
total += (int)i.get_card_copies();
}
}
#else
FOR_EACH(item, game->pack_items) {
int value = amounts[item->name];
drawItem(dc, y, tr(*game, item->name, capitalize), value);
total += value;
}
#endif
// draw total
dc.SetPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW));
dc.DrawLine(0, y-3, size.x, y-3);
......@@ -168,8 +128,7 @@ void PackTotalsPanel::drawItem(DC& dc, int& y, const String& name, double value
wxSize PackTotalsPanel::DoGetBestSize() const {
// count lines
int lines = 0;
#if USE_NEW_PACK_SYSTEM
if (game && generator.set) {
if (game && generator.set) {
FOR_EACH(pack, game->pack_types) {
PackInstance& i = generator.get(pack);
if (pack->summary && (show_all || i.has_cards())) {
......@@ -177,9 +136,6 @@ wxSize PackTotalsPanel::DoGetBestSize() const {
}
}
}
#else
lines = game ? (int)game->pack_items.size() : 0;
#endif
// don't forget the total
lines++;
// size
......@@ -190,23 +146,7 @@ wxSize PackTotalsPanel::DoGetBestSize() const {
void PackTotalsPanel::setGame(const GameP& game) {
this->game = game;
#if !USE_NEW_PACK_SYSTEM
clear();
#endif
}
#if !USE_NEW_PACK_SYSTEM
void PackTotalsPanel::clear() {
amounts.clear();
}
void PackTotalsPanel::addPack(PackType& pack, int copies) {
FOR_EACH(item,pack.items) {
addItemRef(*item, copies * item->amount);
}
}
void PackTotalsPanel::addItemRef(PackItemRef& item, int copies) {
amounts[item.name] += copies;
}
#endif
BEGIN_EVENT_TABLE(PackTotalsPanel, wxPanel)
EVT_PAINT(PackTotalsPanel::onPaint)
......@@ -293,7 +233,6 @@ void PackAmountPicker::destroy(wxFlexGridSizer* sizer) {
}
// ----------------------------------------------------------------------------- : CustomPackDialog
#if USE_NEW_PACK_SYSTEM
class CustomPackDialog : public wxDialog {
public:
......@@ -433,7 +372,6 @@ BEGIN_EVENT_TABLE(CustomPackDialog, wxDialog)
EVT_SPINCTRL (ID_PACK_AMOUNT, CustomPackDialog::onAmountChange)
END_EVENT_TABLE()
#endif
// ----------------------------------------------------------------------------- : RandomPackPanel
RandomPackPanel::RandomPackPanel(Window* parent, int id)
......@@ -450,11 +388,7 @@ void RandomPackPanel::initControls() {
seed_random = new wxRadioButton(this, ID_SEED_RANDOM, _BUTTON_("random seed"));
seed_fixed = new wxRadioButton(this, ID_SEED_FIXED, _BUTTON_("fixed seed"));
seed = new wxTextCtrl(this, wxID_ANY);
#if USE_NEW_PACK_SYSTEM
totals = new PackTotalsPanel(this, wxID_ANY, generator);
#else
totals = new PackTotalsPanel(this, wxID_ANY);
#endif
set_help_text(seed_random, _HELP_("random seed"));
set_help_text(seed_fixed, _HELP_("fixed seed"));
set_help_text(seed, _HELP_("seed"));
......@@ -513,7 +447,6 @@ void RandomPackPanel::onChangeSet() {
pickers.clear();
// add pack controls
#if USE_NEW_PACK_SYSTEM
FOR_EACH(pack, set->game->pack_types) {
if (pack->selectable) {
pickers.push_back(PackAmountPicker(this,packsSizer,pack,false));
......@@ -524,11 +457,6 @@ void RandomPackPanel::onChangeSet() {
pickers.push_back(PackAmountPicker(this,packsSizer,pack,true));
}
}
#else
FOR_EACH(pack, set->game->pack_types) {
pickers.push_back(PackAmountPicker(this,packsSizer,pack,false));
}
#endif
Layout();
......@@ -542,9 +470,7 @@ void RandomPackPanel::onChangeSet() {
pick.value->SetValue(gs.pack_amounts[pick.pack->name]);
}
#if USE_NEW_PACK_SYSTEM
generator.reset(set,last_seed=getSeed());
#endif
updateTotals();
}
......@@ -575,10 +501,8 @@ void RandomPackPanel::initUI(wxToolBar* tb, wxMenuBar* mb) {
onChangeSet();
}
// this is a good moment to update, because the set has likely changed
#if USE_NEW_PACK_SYSTEM
generator.reset(set,last_seed);
updateTotals();
#endif
}
void RandomPackPanel::destroyUI(wxToolBar* tb, wxMenuBar* mb) {}
......@@ -635,24 +559,14 @@ void RandomPackPanel::onPackTypeClick(wxCommandEvent& ev) {
// ----------------------------------------------------------------------------- : Generating
void RandomPackPanel::updateTotals() {
#if USE_NEW_PACK_SYSTEM
generator.reset(last_seed);
#else
totals->clear();
#endif
int total_packs = 0;
FOR_EACH(pick,pickers) {
int copies = pick.value->GetValue();
total_packs += copies;
#if USE_NEW_PACK_SYSTEM
generator.get(pick.pack).request_copy(copies);
#else
totals->addPack(*pick.pack, copies);
#endif
}
#if USE_NEW_PACK_SYSTEM
generator.update_card_counts();
#endif
// update UI
totals->Refresh(false);
generate_button->Enable(total_packs > 0);
......@@ -684,23 +598,14 @@ void RandomPackPanel::setSeed(int seed) {
}
void RandomPackPanel::generate() {
#if USE_NEW_PACK_SYSTEM
generator.reset(set,last_seed=getSeed());
#else
boost::mt19937 gen((unsigned)getSeed());
PackItemCache pack_cache(*set);
#endif
// add packs to card list
card_list->reset();
FOR_EACH(pick,pickers) {
int copies = pick.value->GetValue();
for (int i = 0 ; i < copies ; ++i) {
#if USE_NEW_PACK_SYSTEM
generator.get(pick.pack).request_copy();
generator.generate(card_list->cards);
#else
card_list->add(pack_cache, gen, *pick.pack);
#endif
}
}
card_list->rebuild();
......
......@@ -76,10 +76,8 @@ class RandomPackPanel : public SetWindowPanel {
PackTotalsPanel* totals;
vector<PackAmountPicker> pickers;
#if USE_NEW_PACK_SYSTEM
PackGenerator generator;
int last_seed;
#endif
PackGenerator generator;
int last_seed;
/// Actual intialization of the controls
void initControls();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment