Deck: plain decks weigh results by copies left; test
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
f575b58551
commit
9b68da43d8
2 changed files with 40 additions and 8 deletions
|
|
@ -78,13 +78,13 @@ static func pick_node(t: RollTable, ctx: RollContext, dice: String = "", draw: b
|
||||||
return Roller._stop(t, t.empty_text(), {"deck_empty": true})
|
return Roller._stop(t, t.empty_text(), {"deck_empty": true})
|
||||||
deck.reshuffle()
|
deck.reshuffle()
|
||||||
reshuffled = true
|
reshuffled = true
|
||||||
var node := _roll_entry(t, ctx, dice)
|
var node := _roll_entry(t, ctx, dice, deck)
|
||||||
if deck != null:
|
if deck != null:
|
||||||
var tries := 1
|
var tries := 1
|
||||||
while not node.has("stop") and deck.remaining(t, node["entry"]) <= 0:
|
while not node.has("stop") and deck.remaining(t, node["entry"]) <= 0:
|
||||||
if tries >= MAX_REROLLS:
|
if tries >= MAX_REROLLS:
|
||||||
return Roller._stop(t, "No card left in the deck can come up with these dice")
|
return Roller._stop(t, "No card left in the deck can come up with these dice")
|
||||||
node = _roll_entry(t, ctx, dice)
|
node = _roll_entry(t, ctx, dice, deck)
|
||||||
tries += 1
|
tries += 1
|
||||||
if reshuffled:
|
if reshuffled:
|
||||||
node["reshuffled"] = true
|
node["reshuffled"] = true
|
||||||
|
|
@ -93,8 +93,9 @@ static func pick_node(t: RollTable, ctx: RollContext, dice: String = "", draw: b
|
||||||
return node
|
return node
|
||||||
|
|
||||||
|
|
||||||
## One roll of the card's own way (card mode ignored): ranged dice, or the weights.
|
## One roll of the card's own way: ranged dice, or the weights (a deck's plain
|
||||||
static func _roll_entry(t: RollTable, ctx: RollContext, dice: String) -> Dictionary:
|
## table weighs each result by its copies left).
|
||||||
|
static func _roll_entry(t: RollTable, ctx: RollContext, dice: String, deck: DeckState = null) -> Dictionary:
|
||||||
var rng := ctx.rng
|
var rng := ctx.rng
|
||||||
var index: int
|
var index: int
|
||||||
var value: int
|
var value: int
|
||||||
|
|
@ -132,7 +133,7 @@ static func _roll_entry(t: RollTable, ctx: RollContext, dice: String) -> Diction
|
||||||
if index < 0:
|
if index < 0:
|
||||||
return Roller._stop(t, "No result for %s" % t.fmt(value), {"value": value})
|
return Roller._stop(t, "No result for %s" % t.fmt(value), {"value": value})
|
||||||
else:
|
else:
|
||||||
index = pick_weighted(t, rng)
|
index = pick_weighted(t, rng, deck)
|
||||||
if index < 0:
|
if index < 0:
|
||||||
return Roller._stop(t, "No result can come up")
|
return Roller._stop(t, "No result can come up")
|
||||||
value = index + 1
|
value = index + 1
|
||||||
|
|
@ -163,18 +164,26 @@ static func _roll_entry(t: RollTable, ctx: RollContext, dice: String) -> Diction
|
||||||
|
|
||||||
|
|
||||||
## Index of a weighted random entry, or -1 if every weight is 0.
|
## Index of a weighted random entry, or -1 if every weight is 0.
|
||||||
static func pick_weighted(t: RollTable, rng: RandomNumberGenerator) -> int:
|
## In Deck mode (`deck` given) each result counts once per copy still in the deck.
|
||||||
var total := t.total_weight()
|
static func pick_weighted(t: RollTable, rng: RandomNumberGenerator, deck: DeckState = null) -> int:
|
||||||
|
var total := 0
|
||||||
|
for entry in t.entries:
|
||||||
|
total += _weight_now(t, entry, deck)
|
||||||
if total <= 0:
|
if total <= 0:
|
||||||
return -1
|
return -1
|
||||||
var pick := rng.randi_range(0, total - 1)
|
var pick := rng.randi_range(0, total - 1)
|
||||||
for i in t.entries.size():
|
for i in t.entries.size():
|
||||||
pick -= maxi(0, t.entries[i].weight)
|
pick -= _weight_now(t, t.entries[i], deck)
|
||||||
if pick < 0:
|
if pick < 0:
|
||||||
return i
|
return i
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
|
||||||
|
## A result's weight for this pick: its copies left in the deck, or its weight.
|
||||||
|
static func _weight_now(t: RollTable, entry: TableEntry, deck: DeckState) -> int:
|
||||||
|
return deck.remaining(t, entry) if deck != null else maxi(0, entry.weight)
|
||||||
|
|
||||||
|
|
||||||
# ---------- Routers ----------
|
# ---------- Routers ----------
|
||||||
|
|
||||||
## The index of the first router case whose condition is true (an empty
|
## The index of the first router case whose condition is true (an empty
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ func run() -> void:
|
||||||
_test_limits()
|
_test_limits()
|
||||||
_test_deck_dice_rules()
|
_test_deck_dice_rules()
|
||||||
_test_deck_rerolls()
|
_test_deck_rerolls()
|
||||||
|
_test_deck_copies_left()
|
||||||
finish()
|
finish()
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -318,3 +319,25 @@ func _test_deck_rerolls() -> void:
|
||||||
var stop := _draw(left[0], left[1], rng, left_decks, {"reroll": ["c1"]})
|
var stop := _draw(left[0], left[1], rng, left_decks, {"reroll": ["c1"]})
|
||||||
_eq(stop.get("stop", ""), RollCards.ALL_REROLLED, "a thread that skips the last card left in the deck stops the roll")
|
_eq(stop.get("stop", ""), RollCards.ALL_REROLLED, "a thread that skips the last card left in the deck stops the roll")
|
||||||
_eq((left_decks["deck"] as DeckState).cards_left(left[1]), Vector2i(1, 6), "and nothing is drawn or reshuffled")
|
_eq((left_decks["deck"] as DeckState).cards_left(left[1]), Vector2i(1, 6), "and nothing is drawn or reshuffled")
|
||||||
|
|
||||||
|
|
||||||
|
## Spec deck-mode: "a plain deck holds one copy of a result per point of weight".
|
||||||
|
## A result with copies drawn comes up less often, not at its full weight.
|
||||||
|
func _test_deck_copies_left() -> void:
|
||||||
|
var table := RollTable.new("deck", "Deck")
|
||||||
|
table.card_mode = true
|
||||||
|
var three_copies := TableEntry.new("three", "Three copies")
|
||||||
|
three_copies.weight = 3
|
||||||
|
var one_copy := TableEntry.new("one", "One copy")
|
||||||
|
table.entries.append(three_copies)
|
||||||
|
table.entries.append(one_copy)
|
||||||
|
var deck := DeckState.new()
|
||||||
|
deck.take(three_copies)
|
||||||
|
deck.take(three_copies)
|
||||||
|
var rng := RandomNumberGenerator.new()
|
||||||
|
rng.seed = 7
|
||||||
|
var three_count := 0
|
||||||
|
for i in 2000:
|
||||||
|
three_count += int(RollCards.pick_weighted(table, rng, deck) == 0)
|
||||||
|
var share := three_count / 2000.0
|
||||||
|
_check(share > 0.4 and share < 0.6, "1 copy left each: an even chance, not 3 to 1 (got %.2f)" % share)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue