107 lines
2.8 KiB
GDScript3
107 lines
2.8 KiB
GDScript3
|
|
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 e.has_range and table.chance(e) > 0.0 else 0
|
||
|
|
return maxi(0, e.weight)
|
||
|
|
|
||
|
|
|
||
|
|
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 card. Returns {"index": int, "value": int} or {} if nothing is left.
|
||
|
|
func draw(table: RollTable, rng: RandomNumberGenerator) -> Dictionary:
|
||
|
|
_prune(table)
|
||
|
|
var weights: Array[float] = []
|
||
|
|
var sum := 0.0
|
||
|
|
for e in table.entries:
|
||
|
|
var w := float(_remaining(table, e))
|
||
|
|
if table.is_ranged():
|
||
|
|
w *= table.chance(e)
|
||
|
|
weights.append(w)
|
||
|
|
sum += w
|
||
|
|
if sum <= 0.0:
|
||
|
|
return {}
|
||
|
|
var pick := rng.randf() * sum
|
||
|
|
var index := -1
|
||
|
|
for i in weights.size():
|
||
|
|
if weights[i] <= 0.0:
|
||
|
|
continue
|
||
|
|
index = i
|
||
|
|
if pick < weights[i]:
|
||
|
|
break
|
||
|
|
pick -= weights[i]
|
||
|
|
var chosen := table.entries[index]
|
||
|
|
drawn[chosen.id] = int(drawn.get(chosen.id, 0)) + 1
|
||
|
|
var value := index + 1
|
||
|
|
if table.is_ranged():
|
||
|
|
value = _value_in(table, chosen, rng)
|
||
|
|
return {"index": index, "value": value}
|
||
|
|
|
||
|
|
|
||
|
|
## A die value inside the entry's range, weighted by the dice distribution.
|
||
|
|
func _value_in(table: RollTable, e: TableEntry, rng: RandomNumberGenerator) -> int:
|
||
|
|
var dist := table.dice_obj().distribution()
|
||
|
|
var vals: Array[int] = []
|
||
|
|
var mass := 0.0
|
||
|
|
for v in range(e.range_lo, e.range_hi + 1):
|
||
|
|
if dist.has(v):
|
||
|
|
vals.append(v)
|
||
|
|
mass += float(dist[v])
|
||
|
|
var pick := rng.randf() * mass
|
||
|
|
for v in vals:
|
||
|
|
pick -= float(dist[v])
|
||
|
|
if pick < 0.0:
|
||
|
|
return v
|
||
|
|
return vals[vals.size() - 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)
|