Commit a24df85a authored by twanvl's avatar twanvl

while there is still time to make changes to the file format: s/probability/weight/

parent aa820659
...@@ -91,10 +91,10 @@ pack type: ...@@ -91,10 +91,10 @@ pack type:
select: proportional select: proportional
item: item:
name: mythic rare name: mythic rare
probability: 1 weight: 1
item: item:
name: rare name: rare
probability: 2 weight: 2
pack type: pack type:
name: shifted uncommon or rare name: shifted uncommon or rare
...@@ -102,10 +102,10 @@ pack type: ...@@ -102,10 +102,10 @@ pack type:
select: nonempty select: nonempty
item: item:
name: shifted uncommon name: shifted uncommon
probability: 3 weight: 3
item: item:
name: shifted rare name: shifted rare
probability: 1 weight: 1
############################################################## Common proportions of cards ############################################################## Common proportions of cards
......
...@@ -207,7 +207,7 @@ IMPLEMENT_REFLECTION(PackItem) { ...@@ -207,7 +207,7 @@ IMPLEMENT_REFLECTION(PackItem) {
} else { } else {
REFLECT(name); REFLECT(name);
REFLECT(amount); REFLECT(amount);
REFLECT(probability); REFLECT(weight);
} }
} }
...@@ -221,13 +221,13 @@ PackType::PackType() ...@@ -221,13 +221,13 @@ PackType::PackType()
PackItem::PackItem() PackItem::PackItem()
: amount(1) : amount(1)
, probability(1) , weight(1)
{} {}
PackItem::PackItem(const String& name, int amount) PackItem::PackItem(const String& name, int amount)
: name(name) : name(name)
, amount(amount) , amount(amount)
, probability(1) , weight(1)
{} {}
...@@ -241,7 +241,7 @@ bool PackType::update(Context& ctx) { ...@@ -241,7 +241,7 @@ bool PackType::update(Context& ctx) {
bool PackItem::update(Context& ctx) { bool PackItem::update(Context& ctx) {
return amount.update(ctx) return amount.update(ctx)
| probability.update(ctx); | weight.update(ctx);
} }
...@@ -281,17 +281,17 @@ PackInstance::PackInstance(const PackType& pack_type, PackGenerator& parent) ...@@ -281,17 +281,17 @@ PackInstance::PackInstance(const PackType& pack_type, PackGenerator& parent)
count += parent.get(item->name).count; count += parent.get(item->name).count;
} }
} }
// Sum of probabilities // Sum of weights
total_probability = cards.size(); total_weight = cards.size();
FOR_EACH_CONST(item, pack_type.items) { FOR_EACH_CONST(item, pack_type.items) {
if (pack_type.select == SELECT_PROPORTIONAL) { if (pack_type.select == SELECT_PROPORTIONAL) {
total_probability += item->probability * parent.get(item->name).count; total_weight += item->weight * parent.get(item->name).count;
} else if (pack_type.select == SELECT_NONEMPTY) { } else if (pack_type.select == SELECT_NONEMPTY) {
if (parent.get(item->name).count > 0) { if (parent.get(item->name).count > 0) {
total_probability += item->probability; total_weight += item->weight;
} }
} else { } else {
total_probability += item->probability; total_weight += item->weight;
} }
} }
// Depth // Depth
...@@ -309,10 +309,10 @@ void PackInstance::expect_copy(double copies) { ...@@ -309,10 +309,10 @@ void PackInstance::expect_copy(double copies) {
if (pack_type.select == SELECT_ALL) { if (pack_type.select == SELECT_ALL) {
i.expect_copy(copies * item->amount); i.expect_copy(copies * item->amount);
} else if (pack_type.select == SELECT_PROPORTIONAL) { } else if (pack_type.select == SELECT_PROPORTIONAL) {
i.expect_copy(copies * item->amount * item->probability * i.count / total_probability); i.expect_copy(copies * item->amount * item->weight * i.count / total_weight);
} else if (pack_type.select == SELECT_NONEMPTY) { } else if (pack_type.select == SELECT_NONEMPTY) {
if (i.count > 0) { if (i.count > 0) {
i.expect_copy(copies * item->amount * item->probability / total_probability); i.expect_copy(copies * item->amount * item->weight / total_weight);
} }
} else if (pack_type.select == SELECT_FIRST) { } else if (pack_type.select == SELECT_FIRST) {
if (i.count > 0 && cards.empty()) { if (i.count > 0 && cards.empty()) {
...@@ -320,7 +320,7 @@ void PackInstance::expect_copy(double copies) { ...@@ -320,7 +320,7 @@ void PackInstance::expect_copy(double copies) {
break; break;
} }
} else { } else {
i.expect_copy(copies * item->amount * item->probability / total_probability); i.expect_copy(copies * item->amount * item->weight / total_weight);
} }
} }
} }
...@@ -359,7 +359,7 @@ void PackInstance::generate(vector<CardP>* out) { ...@@ -359,7 +359,7 @@ void PackInstance::generate(vector<CardP>* out) {
|| pack_type.select == SELECT_NONEMPTY) { || pack_type.select == SELECT_NONEMPTY) {
// multiple copies // multiple copies
for (size_t i = 0 ; i < requested_copies ; ++i) { for (size_t i = 0 ; i < requested_copies ; ++i) {
double r = parent.gen() * total_probability / parent.gen.max(); double r = parent.gen() * total_weight / parent.gen.max();
if (r < cards.size()) { if (r < cards.size()) {
// pick a card // pick a card
card_copies++; card_copies++;
...@@ -373,11 +373,11 @@ void PackInstance::generate(vector<CardP>* out) { ...@@ -373,11 +373,11 @@ void PackInstance::generate(vector<CardP>* out) {
FOR_EACH_CONST(item, pack_type.items) { FOR_EACH_CONST(item, pack_type.items) {
PackInstance& i = parent.get(item->name); PackInstance& i = parent.get(item->name);
if (pack_type.select == SELECT_REPLACE) { if (pack_type.select == SELECT_REPLACE) {
r -= item->probability; r -= item->weight;
} else if (pack_type.select == SELECT_PROPORTIONAL) { } else if (pack_type.select == SELECT_PROPORTIONAL) {
r -= item->probability * i.count; r -= item->weight * i.count;
} else { // SELECT_NONEMPTY } else { // SELECT_NONEMPTY
if (i.count > 0) r -= item->probability; if (i.count > 0) r -= item->weight;
} }
// have we reached the item we were looking for? // have we reached the item we were looking for?
if (r < 0) { if (r < 0) {
...@@ -389,6 +389,9 @@ void PackInstance::generate(vector<CardP>* out) { ...@@ -389,6 +389,9 @@ void PackInstance::generate(vector<CardP>* out) {
} }
} else if (pack_type.select == SELECT_NO_REPLACE) { } 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; card_copies += requested_copies;
// 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()) {
......
...@@ -157,7 +157,7 @@ class PackItem : public IntrusivePtrBase<PackItem> { ...@@ -157,7 +157,7 @@ class PackItem : public IntrusivePtrBase<PackItem> {
String name; ///< Name of the pack to select cards from String name; ///< Name of the pack to select cards from
Scriptable<int> amount; ///< Number of cards of this type Scriptable<int> amount; ///< Number of cards of this type
Scriptable<double> probability; ///< Relative probability of picking this item Scriptable<double> weight; ///< Relative probability of picking this item
/// Update scripts, returns true if there is a change /// Update scripts, returns true if there is a change
bool update(Context& ctx); bool update(Context& ctx);
...@@ -201,7 +201,7 @@ class PackInstance : public IntrusivePtrBase<PackInstance> { ...@@ -201,7 +201,7 @@ class PackInstance : public IntrusivePtrBase<PackInstance> {
int depth; //< 0 = no items, otherwise 1+max depth of items refered to int depth; //< 0 = no items, otherwise 1+max depth of items refered to
vector<CardP> cards; //< All cards that pass the filter vector<CardP> cards; //< All cards that pass the filter
size_t count; //< Total number of non-empty cards/items size_t count; //< Total number of non-empty cards/items
double total_probability; //< Sum of item and card probabilities double total_weight; //< Sum of item and card weights
size_t requested_copies; //< The requested number of copies of this pack size_t requested_copies; //< The requested number of copies of this pack
size_t card_copies; //< The number of cards that were chosen to come from this pack size_t card_copies; //< The number of cards that were chosen to come from this pack
double expected_copies; double expected_copies;
......
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