From 2d3f5e9e3d14cb209b8f9a3701c32ad2c51c1804 Mon Sep 17 00:00:00 2001 From: RobinFiredrake Date: Tue, 1 Dec 2026 16:13:14 -0600 Subject: [PATCH] Reroll this undoes the old result's values; partly drawn tint from a turned accent - 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 --- core/roll_context.gd | 9 ++++ core/roll_journal.gd | 95 +++++++++++++++++++++++++++++++++++++++ core/roll_journal.gd.uid | 1 + core/roll_nodes.gd | 3 ++ core/roll_tree.gd | 2 +- core/roller.gd | 1 + docs/ARCHITECTURE.md | 3 +- tests/test_fast_roll.gd | 14 ++++++ tests/test_roller.gd | 23 ++++++++++ ui/main_rolling.gd | 2 +- ui/main_rolling_reroll.gd | 26 ++++++++++- ui/main_state.gd | 3 +- ui/style.gd | 8 ++++ ui/table_card_skin.gd | 7 ++- 14 files changed, 190 insertions(+), 7 deletions(-) create mode 100644 core/roll_journal.gd create mode 100644 core/roll_journal.gd.uid diff --git a/core/roll_context.gd b/core/roll_context.gd index 751e44b..883b855 100644 --- a/core/roll_context.gd +++ b/core/roll_context.gd @@ -46,6 +46,9 @@ var persistent_vars: Dictionary = {} ## True once save() has written a persistent variable in this roll, so the caller ## knows local state needs saving. var persistent_changed: bool = false +## Every value this roll saved or erased, in order (RollJournal), so a reroll +## can undo part of it. +var journal: Array = [] ## Depth of the text currently being expanded. References in that text are ## rolled at this depth. The roller sets it (depth + 1 for an entry's text). var depth: int = 0 @@ -111,9 +114,11 @@ func save(name: String, value: Variant, source_id: String = "") -> void: var steps := scale_steps(n) if not steps.is_empty(): value = Scales.store(steps, value) + RollJournal.record(journal, "@" + n, value, persistent_vars, chain_vars) persistent_vars[n] = {"value": value, "source": source_id} persistent_changed = true elif n != "": + RollJournal.record(journal, n, value, persistent_vars, chain_vars) chain_vars[n] = value @@ -157,9 +162,13 @@ func get_value(name: String) -> Variant: func erase(name: String) -> void: var n: String = name.strip_edges() if n.begins_with("@"): + if persistent_vars.has(n.substr(1)): + RollJournal.record(journal, n, null, persistent_vars, chain_vars) if persistent_vars.erase(n.substr(1)): persistent_changed = true else: + if chain_vars.has(n): + RollJournal.record(journal, n, null, persistent_vars, chain_vars) chain_vars.erase(n) diff --git a/core/roll_journal.gd b/core/roll_journal.gd new file mode 100644 index 0000000..866d0a1 --- /dev/null +++ b/core/roll_journal.gd @@ -0,0 +1,95 @@ +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 diff --git a/core/roll_journal.gd.uid b/core/roll_journal.gd.uid new file mode 100644 index 0000000..c04193a --- /dev/null +++ b/core/roll_journal.gd.uid @@ -0,0 +1 @@ +uid://clu2xrmhtfgim diff --git a/core/roll_nodes.gd b/core/roll_nodes.gd index 7576892..286a0e6 100644 --- a/core/roll_nodes.gd +++ b/core/roll_nodes.gd @@ -16,6 +16,9 @@ static func stop(table: RollTable, why: String, extra: Dictionary = {}) -> Dicti ## Hangs `node` under the frame's parent, with the frame's save name and card ## inputs. A top-level node also becomes ctx.root. static func attach(frame: Dictionary, node: Dictionary, ctx: RollContext) -> void: + # Where this node's writes start (RollJournal): from the frame's start, so + # a paid cost counts as the node's own. + node["journal_at"] = int(frame.get("journal_at", ctx.journal.size())) if str(frame.get("save", "")) != "": node["save_as"] = frame["save"] if frame.has("inputs"): diff --git a/core/roll_tree.gd b/core/roll_tree.gd index 4246c4b..c45c9a0 100644 --- a/core/roll_tree.gd +++ b/core/roll_tree.gd @@ -38,7 +38,7 @@ static func step_of(n: Dictionary, depth: int = 0) -> Dictionary: step["spread"] = n["spread"] if n.has("effects"): step["effects"] = (n["effects"] as Array).duplicate(true) - for k in ["answer", "passes", "pass", "after", "picked", "only_option", "random", "limit", "cancelled", "deck_empty", "dropped", "kept", "combined", "stopped_before", "case", "option", "check", "inputs", "columns", "card_thread"]: + for k in ["answer", "passes", "pass", "after", "picked", "only_option", "random", "limit", "cancelled", "deck_empty", "dropped", "kept", "combined", "stopped_before", "case", "option", "check", "inputs", "columns", "card_thread", "journal_at"]: if n.has(k): step[k] = n[k] if n.has("why"): diff --git a/core/roller.gd b/core/roller.gd index c1feb14..857ea3c 100644 --- a/core/roller.gd +++ b/core/roller.gd @@ -232,6 +232,7 @@ static func _do_roll(board: TowerBoard, f: Dictionary, ctx: RollContext, stack: return if not _take_action(f, t, ctx): return + f["journal_at"] = ctx.journal.size() RollPipes.bind_inputs(t, f, ctx, stack) if not await RollCosts.card_gate(board, t, f, ctx): return diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 6b77af4..018e2c4 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -228,7 +228,8 @@ Each original file keeps one-line delegators, so callers are unchanged. saved values), `ui/ledger_edits.gd` (add/rename/start/remove with undo), `ui/ledger_card_rows.gd` (Build-mode fields on the card). - **Rolling:** `ui/main_rolling_show.gd` (show a roll at once), `ui/main_rolling_reroll.gd` - ("Reroll this" on a tray step: that card and its branch roll again in place; 2026-12-01), `RollCosts.card_gate` (card costs), + ("Reroll this" on a tray step: that card and its branch roll again in place, with + `core/roll_journal.gd` undoing the values the old branch changed; 2026-12-01), `RollCosts.card_gate` (card costs), `RollPauses` pop-up, `SpreadLinks` (`core/spread_links.gd`, [[Spread::Card]]) with `ui/spread_loader.gd` and `ui/spread_link_dialog.gd`. - **Interface:** `ui/delete_key.gd`, `ui/sound_panel.gd`, `ui/format_menu.gd` (right-click Format), diff --git a/tests/test_fast_roll.gd b/tests/test_fast_roll.gd index a4c0f11..2f06e36 100644 --- a/tests/test_fast_roll.gd +++ b/tests/test_fast_roll.gd @@ -66,7 +66,21 @@ func _test_reroll_this() -> void: _check(not is_same(main.current["all"][0], before[0]), "the first step and the cards after it roll again") _eq(str(main.current["root"]), "wilderness", "it is still the same roll") + # The old result never happened: its effect is undone before the new one. var t := main.data.get_table("wilderness") + var own_effects: Array[String] = [] + for e in t.entries: + own_effects.append(e.effects) + e.effects = "@reroll_luck -= 1" + main.persistent_vars["reroll_luck"] = {"value": 5, "source": ""} + main.start_roll(t.id, true) + _eq(int(main.persistent_vars["reroll_luck"]["value"]), 4, "the roll's effect happens once") + await main.rolling.reroll.reroll(0) + _eq(int(main.persistent_vars["reroll_luck"]["value"]), 4, "a reroll undoes the old effect first") + for i in t.entries.size(): + t.entries[i].effects = own_effects[i] + main.persistent_vars.erase("reroll_luck") + t.card_mode = true main.reshuffle_table(t.id) main.start_roll(t.id, true) diff --git a/tests/test_roller.gd b/tests/test_roller.gd index 455dc27..dd60f95 100644 --- a/tests/test_roller.gd +++ b/tests/test_roller.gd @@ -81,4 +81,27 @@ func run() -> void: dangling = true _check(not dangling and copy.get_table("loot") == null, "removing a table removes threads into it") + _test_journal_undo() finish() + + +## RollJournal: undoing part of a roll puts values back. A number a later +## write changed again keeps that later change; a value first set by the undone +## part is erased. +func _test_journal_undo() -> void: + var persistent := {"luck": {"value": 5, "source": "a"}} + var ctx := RollContext.new(persistent) + ctx.save("@luck", 4) # 0: undone + ctx.save("@gold", 10) # 1: undone, new value + ctx.save("place", "cave") # 2: undone, chain value + ctx.save("@luck", 2) # 3: a later write (-2) + RollJournal.undo(ctx.journal, 0, 3, persistent, ctx.chain_vars) + _eq(int(persistent["luck"]["value"]), 3, "a later change stays; only the undone -1 comes off") + _check(not persistent.has("gold"), "a value the undone part made is gone") + _check(not ctx.chain_vars.has("place"), "a chain value the undone part made is gone") + var fresh := {"luck": {"value": 5, "source": "a"}} + var ctx2 := RollContext.new(fresh) + ctx2.save("@luck", 1) + ctx2.erase("@luck") + RollJournal.undo(ctx2.journal, 0, 2, fresh, ctx2.chain_vars) + _eq(int(fresh["luck"]["value"]), 5, "a value set then erased comes back as it was") diff --git a/ui/main_rolling.gd b/ui/main_rolling.gd index 7c0649f..83f3adb 100644 --- a/ui/main_rolling.gd +++ b/ui/main_rolling.gd @@ -93,7 +93,7 @@ func start_roll(tid: String, fast: bool = false) -> void: m.lit_texts.clear() m.lit_columns.clear() m.current = {"collection": m.data.id, "root": tid, "root_name": t.name, "time": m.current.get("time", Time.get_unix_time_from_system()), - "steps": [], "all": Roller.flatten(tree, []), "done": false, "chain_vars": ctx.chain_vars.duplicate()} + "steps": [], "all": Roller.flatten(tree, []), "done": false, "chain_vars": ctx.chain_vars.duplicate(), "journal": ctx.journal} if ctx.halted != "": m.current["halted"] = ctx.halted m.local_state._store_roll(ctx) diff --git a/ui/main_rolling_reroll.gd b/ui/main_rolling_reroll.gd index f42d06f..7207b42 100644 --- a/ui/main_rolling_reroll.gd +++ b/ui/main_rolling_reroll.gd @@ -2,8 +2,9 @@ class_name MainRollingReroll extends RefCounted ## "Reroll this" in the result tray: one step's card rolls again, and so do ## the cards after it along its threads. The new steps take the old ones' -## place in the current result; Earlier keeps no copy. Deck draws the old -## steps made go back into their decks first. The reroll shows at once. +## place in the current result; Earlier keeps no copy. The old result never +## happened: the values its branch changed are undone (RollJournal) and its +## deck draws go back, before the new roll. The reroll shows at once. ## Steps that can't be rerolled on their own: text rolls inside a result, ## runs dropped from or added into a total, a choice's answer, and the @@ -57,6 +58,10 @@ func reroll(index: int) -> void: m.sound.play("roll") var ctx := Roller.new_context(m.data, m.rng, m.persistent_vars, m.decks) ctx.chain_vars = (current.get("chain_vars", {}) as Dictionary).duplicate(true) + var journal: Array = current.get("journal", []) + var span := _journal_span(steps, index, end, journal) + if span.x >= 0: + ctx.persistent_changed = RollJournal.undo(journal, span.x, span.y, m.persistent_vars, ctx.chain_vars) ctx.prompt = _ask m.sheet_links.before_roll(Formatter.strip_tags(str(current.get("root_name", "")))) var depth := int(step.get("depth", 0)) @@ -69,6 +74,11 @@ func reroll(index: int) -> void: if step.has(key): (fresh[0] as Dictionary)[key] = step[key] var all: Array = steps.slice(0, index) + fresh + steps.slice(end) + if span.x >= 0: + for st: Dictionary in fresh: + if st.has("journal_at"): + st["journal_at"] = int(st["journal_at"]) + span.x + current["journal"] = RollJournal.splice(journal, span.x, span.y, ctx.journal, all, index + fresh.size()) current["all"] = all current["steps"] = all current["chain_vars"] = ctx.chain_vars.duplicate() @@ -77,6 +87,18 @@ func reroll(index: int) -> void: m.local_state.replace_current_roll(ctx) +## The old branch's writes in the journal, as Vector2i(from, to); x is -1 for +## a roll saved before journals were kept (its values can't be undone). +static func _journal_span(steps: Array, index: int, end: int, journal: Array) -> Vector2i: + var from := int((steps[index] as Dictionary).get("journal_at", -1)) + var to := journal.size() + if end < steps.size(): + to = int((steps[end] as Dictionary).get("journal_at", -1)) + if from < 0 or to < from or to > journal.size(): + return Vector2i(-1, -1) + return Vector2i(from, to) + + ## Deck mode: every card the old branch drew goes back into its deck. func _put_back_draws(old_steps: Array) -> void: for st: Dictionary in old_steps: diff --git a/ui/main_state.gd b/ui/main_state.gd index ef67627..2bd6d55 100644 --- a/ui/main_state.gd +++ b/ui/main_state.gd @@ -62,7 +62,8 @@ func _store_roll(ctx: RollContext, replace: bool = false) -> void: if ctx.persistent_changed: _store_variables() var record := {"collection": m.data.id, "root": m.current["root"], "root_name": m.current["root_name"], - "time": m.current["time"], "steps": m.current["all"], "chain_vars": m.current.get("chain_vars", {})} + "time": m.current["time"], "steps": m.current["all"], "chain_vars": m.current.get("chain_vars", {}), + "journal": m.current.get("journal", [])} if replace: m.state.replace_history(record) else: diff --git a/ui/style.gd b/ui/style.gd index dcc9edb..99b7186 100644 --- a/ui/style.gd +++ b/ui/style.gd @@ -127,6 +127,14 @@ static func caps() -> Font: # ---------- Styleboxes ---------- ## margins: [left, top, right, bottom] +## The accent with its hue turned `degrees` around the colour wheel, keeping +## how light and how strong it is (perceptual OK HSL). Colours made this way +## follow whatever accent the theme picks, yet never look like the accent. +static func turned_accent(degrees: float) -> Color: + var turned := fposmod(ACCENT.ok_hsl_h + degrees / 360.0, 1.0) + return Color.from_ok_hsl(turned, ACCENT.ok_hsl_s, ACCENT.ok_hsl_l, ACCENT.a) + + static func box(bg: Color, radius: int = 0, margins: Array = [0, 0, 0, 0]) -> StyleBoxFlat: var s := StyleBoxFlat.new() s.bg_color = bg diff --git a/ui/table_card_skin.gd b/ui/table_card_skin.gd index 9332a24..ee67f36 100644 --- a/ui/table_card_skin.gd +++ b/ui/table_card_skin.gd @@ -108,9 +108,14 @@ func dim_drawn() -> void: _show_copies_left(e, left, copies, partly) +## How far the partly drawn tint's hue is turned from the accent, so it isn't +## mistaken for a lit row (pale accent). +const PARTLY_DRAWN_TURN := 200.0 + + ## The tint on a row with some of its copies drawn. static func partly_drawn_tint() -> Color: - return Color.WHITE.lerp(Style.ACCENT, 0.55) + return Color.WHITE.lerp(Style.turned_accent(PARTLY_DRAWN_TURN), 0.55) ## Play mode: a partly drawn row's number becomes " / ".