- RollJournal: each roll logs the values it saves or erases; a reroll undoes the old branch's writes before rolling again (a number a later step changed keeps that later change). - Style.turned_accent: the accent with its hue turned (OK HSL). Partly drawn deck rows use it turned 200 degrees, so they don't look like a lit row. - Tests in test_roller and test_fast_roll. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
95 lines
3.8 KiB
GDScript
95 lines
3.8 KiB
GDScript
class_name RollJournal
|
|
extends RefCounted
|
|
## The values one roll changed, in the order it changed them, so part of the
|
|
## roll can be undone ("Reroll this": the old result never happened).
|
|
##
|
|
## Each write is {"key": "@name" or a chain name, "had": bool, "before":
|
|
## value, "before_source": table id, "after": value}. Every result node keeps
|
|
## "journal_at", the number of writes made before it started; a node's branch
|
|
## runs in one piece, so its writes sit between its own journal_at and the
|
|
## next branch's.
|
|
|
|
|
|
## Notes one write, before it happens. `persistent` and `chain` are the
|
|
## RollContext's dictionaries; `key` starts with "@" for a persistent value.
|
|
static func record(journal: Array, key: String, after: Variant, persistent: Dictionary, chain: Dictionary) -> void:
|
|
var write := {"key": key, "had": false, "before": null, "after": after}
|
|
if key.begins_with("@"):
|
|
var old: Variant = persistent.get(key.substr(1))
|
|
if old is Dictionary:
|
|
write["had"] = true
|
|
write["before"] = (old as Dictionary).get("value")
|
|
write["before_source"] = str((old as Dictionary).get("source", ""))
|
|
elif chain.has(key):
|
|
write["had"] = true
|
|
write["before"] = chain[key]
|
|
journal.append(write)
|
|
|
|
|
|
## Undoes writes [from, to). A value no later write touched goes back to what
|
|
## it was before the first of them. A number a later write changed again keeps
|
|
## that later change: only the undone writes' net change comes off it. Anything
|
|
## else a later write changed is left alone. Returns true if a persistent
|
|
## value changed.
|
|
static func undo(journal: Array, from: int, to: int, persistent: Dictionary, chain: Dictionary) -> bool:
|
|
var first := {}
|
|
var last := {}
|
|
for i in range(from, to):
|
|
var write: Dictionary = journal[i]
|
|
var key := str(write["key"])
|
|
if not first.has(key):
|
|
first[key] = write
|
|
last[key] = write
|
|
var later := {}
|
|
for i in range(to, journal.size()):
|
|
later[str((journal[i] as Dictionary)["key"])] = true
|
|
var persistent_changed := false
|
|
for key: String in first:
|
|
var start: Dictionary = first[key]
|
|
if not later.has(key):
|
|
_put(key, start["had"], start["before"], str(start.get("before_source", "")), persistent, chain)
|
|
else:
|
|
var before: Variant = start["before"] if start["had"] else 0
|
|
var after: Variant = (last[key] as Dictionary)["after"]
|
|
var now: Variant = _value_of(key, persistent, chain)
|
|
if not (_is_number(before) and _is_number(after) and _is_number(now)):
|
|
continue
|
|
var source := str(start.get("before_source", ""))
|
|
_put(key, true, Expr.normalize(float(now) - (float(after) - float(before))), source, persistent, chain)
|
|
persistent_changed = persistent_changed or key.begins_with("@")
|
|
return persistent_changed
|
|
|
|
|
|
## Replaces writes [from, to) with `fresh` (a reroll's own writes) and moves
|
|
## the journal_at of every step from `after_index` on to match.
|
|
static func splice(journal: Array, from: int, to: int, fresh: Array, steps: Array, after_index: int) -> Array:
|
|
var out: Array = journal.slice(0, from) + fresh + journal.slice(to)
|
|
var shift := fresh.size() - (to - from)
|
|
for i in range(after_index, steps.size()):
|
|
var step: Dictionary = steps[i]
|
|
if step.has("journal_at"):
|
|
step["journal_at"] = int(step["journal_at"]) + shift
|
|
return out
|
|
|
|
|
|
static func _put(key: String, had: bool, value: Variant, source: String, persistent: Dictionary, chain: Dictionary) -> void:
|
|
if key.begins_with("@"):
|
|
if had:
|
|
persistent[key.substr(1)] = {"value": value, "source": source}
|
|
else:
|
|
persistent.erase(key.substr(1))
|
|
elif had:
|
|
chain[key] = value
|
|
else:
|
|
chain.erase(key)
|
|
|
|
|
|
static func _value_of(key: String, persistent: Dictionary, chain: Dictionary) -> Variant:
|
|
if key.begins_with("@"):
|
|
var entry: Variant = persistent.get(key.substr(1))
|
|
return (entry as Dictionary).get("value") if entry is Dictionary else null
|
|
return chain.get(key)
|
|
|
|
|
|
static func _is_number(value: Variant) -> bool:
|
|
return value is int or value is float
|