- A deck row with no copies left is greyed out; one partly drawn gets an accent tint (Play and Build) and, in Play, shows <left> / <weight>. - Each step of the current result has a Reroll this button: that card and the cards after it roll again in place. Earlier keeps no copy; the old branch's deck draws go back. - Tests in test_small_ui and test_fast_roll. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
78 lines
2.1 KiB
GDScript
78 lines
2.1 KiB
GDScript
class_name DeckState
|
|
extends RefCounted
|
|
## Which cards have been drawn from one card-mode table.
|
|
##
|
|
## Weighted tables treat an entry's weight as its number of copies. Ranged
|
|
## tables treat each ranged entry as one card, drawn in proportion to the
|
|
## probability of its range. State is kept per entry id, so edits behave:
|
|
## deleted entries drop out, new entries start undrawn, raising a weight adds
|
|
## copies.
|
|
|
|
## entry id -> number of copies drawn.
|
|
var drawn: Dictionary = {}
|
|
|
|
|
|
func to_dict() -> Dictionary:
|
|
return {"drawn": drawn.duplicate()}
|
|
|
|
|
|
static func from_dict(d: Dictionary) -> DeckState:
|
|
var s := DeckState.new()
|
|
var src: Variant = d.get("drawn", {})
|
|
if src is Dictionary:
|
|
for k: Variant in src:
|
|
var n: int = int(src[k])
|
|
if n > 0:
|
|
s.drawn[str(k)] = n
|
|
return s
|
|
|
|
|
|
func reshuffle() -> void:
|
|
drawn.clear()
|
|
|
|
|
|
## Copies of this entry that are in the deck in total.
|
|
static func copies_of(table: RollTable, e: TableEntry) -> int:
|
|
if table.is_ranged():
|
|
return 1 if table.can_come_up(e) else 0
|
|
return maxi(0, e.weight)
|
|
|
|
|
|
## How many copies of this result are still in the deck.
|
|
func remaining(table: RollTable, e: TableEntry) -> int:
|
|
return _remaining(table, e)
|
|
|
|
|
|
func _remaining(table: RollTable, e: TableEntry) -> int:
|
|
return maxi(0, copies_of(table, e) - int(drawn.get(e.id, 0)))
|
|
|
|
|
|
## Vector2i(cards left, total cards) for the table as it is now.
|
|
func cards_left(table: RollTable) -> Vector2i:
|
|
var left := 0
|
|
var total := 0
|
|
for e in table.entries:
|
|
total += copies_of(table, e)
|
|
left += _remaining(table, e)
|
|
return Vector2i(left, total)
|
|
|
|
|
|
## Draws one copy of this result: it leaves the deck until a reshuffle.
|
|
func take(e: TableEntry) -> void:
|
|
drawn[e.id] = int(drawn.get(e.id, 0)) + 1
|
|
|
|
|
|
## Puts one drawn copy of this result back (its roll was replaced).
|
|
func put_back(entry_id: String) -> void:
|
|
var count := int(drawn.get(entry_id, 0)) - 1
|
|
if count > 0:
|
|
drawn[entry_id] = count
|
|
else:
|
|
drawn.erase(entry_id)
|
|
|
|
|
|
## Forgets draws for entries that no longer exist.
|
|
func _prune(table: RollTable) -> void:
|
|
for k: Variant in drawn.keys():
|
|
if table.find_entry(str(k)) == null:
|
|
drawn.erase(k)
|