69 lines
1.9 KiB
GDScript
69 lines
1.9 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
|
|
|
|
|
|
## 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)
|