From 9d550408d8ec03e29623651fd86dd75e552ecea8 Mon Sep 17 00:00:00 2001 From: RobinFiredrake Date: Sat, 14 Nov 2026 13:42:11 -0600 Subject: [PATCH] Roll again and combine can apply to chosen columns only Co-Authored-By: Claude Opus 5.5 --- core/roll_columns.gd | 15 ++++++++++++--- core/table_entry_io.gd | 9 +++++++++ help/topics.txt | 1 + tests/test_book_collect.gd | 19 +++++++++++++++++++ ui/inspector_columns.gd | 25 +++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 3 deletions(-) diff --git a/core/roll_columns.gd b/core/roll_columns.gd index cb0b8a7..c1c3232 100644 --- a/core/roll_columns.gd +++ b/core/roll_columns.gd @@ -11,7 +11,8 @@ extends RefCounted ## happen as usual. ## ## A "roll twice and combine" row (TableEntry.repeat) that comes up in a -## column rolls that column again `count` times and combines the texts with +## column it applies to (repeat "columns": indices; none listed = every +## column) rolls that column again `count` times and combines the texts with ## REPEAT_JOIN ("Ash & Black"); a repeat row that comes up on those rolls adds ## its own count (so "roll twice" again means one more), up to MAX_REPEATS. ## @@ -59,7 +60,7 @@ static func fill(node: Dictionary, f: Dictionary, ctx: RollContext, depth: int) row["index"] = int(pick["index"]) row["value"] = pick.get("value") var e: TableEntry = pick["entry"] - if not e.repeat.is_empty(): + if repeats_in(e, k): text = await _repeat(node, t, k, e, f, ctx, depth, why) elif k == 0: text = str(node["summary"]) @@ -110,7 +111,7 @@ static func _repeat(node: Dictionary, t: RollTable, k: int, e: TableEntry, f: Di got.append(str(p["summary"])) continue var pe: TableEntry = p["entry"] - if not pe.repeat.is_empty(): + if repeats_in(pe, k): for _n in int(pe.repeat.get("count", 2)): queue.append(pe.repeat.get("reroll", [])) continue @@ -121,6 +122,14 @@ static func _repeat(node: Dictionary, t: RollTable, k: int, e: TableEntry, f: Di return REPEAT_JOIN.join(got) +## True when entry `e`'s "roll again and combine" applies in column `k`. +static func repeats_in(e: TableEntry, k: int) -> bool: + if e.repeat.is_empty(): + return false + var only: Variant = e.repeat.get("columns") + return not (only is Array) or (only as Array).is_empty() or (only as Array).has(k) + + static func _add_why(why: Array, pick: Dictionary, column: String) -> void: if pick.has("why"): for fact: Dictionary in pick["why"]: diff --git a/core/table_entry_io.gd b/core/table_entry_io.gd index 67894c0..d397792 100644 --- a/core/table_entry_io.gd +++ b/core/table_entry_io.gd @@ -98,6 +98,15 @@ static func clean_repeat(value: Variant) -> Dictionary: ids.append(str(item)) if not ids.is_empty(): out["reroll"] = ids + var columns: Array[int] = [] + if (value as Dictionary).get("columns") is Array: + for item: Variant in value["columns"]: + var index := int(JsonIO._num(item, -1)) + if index >= 0 and not columns.has(index): + columns.append(index) + if not columns.is_empty(): + columns.sort() + out["columns"] = columns return out diff --git a/help/topics.txt b/help/topics.txt index 863fd48..83d4cf4 100644 --- a/help/topics.txt +++ b/help/topics.txt @@ -61,6 +61,7 @@ A card can give each result more than one part, like a row of a printed table wi - [b]Roll each column separately[/b] rolls again for every column, for rules like "roll 1d10 for each of Resources, Water, Atmosphere and Life". After the roll, the part each column landed on is lit. - [b]Save as[/b] keeps a column's text as a value, so later text and rules can use it. - [b]Only roll this column if[/b] skips a column unless a condition is true, such as "only roll Life if the Atmosphere is at least Thin". Columns are rolled left to right, so a condition can use what an earlier column saved. +- A result can [b]roll that column again and combine[/b] ("roll twice and combine"). With columns rolled separately, tick [b]Only in[/b] to choose which columns it applies to; by default it applies to every column. - Remove a column with its ×: its text goes from every result. # Threads diff --git a/tests/test_book_collect.gd b/tests/test_book_collect.gd index b324777..c33d29b 100644 --- a/tests/test_book_collect.gd +++ b/tests/test_book_collect.gd @@ -9,6 +9,7 @@ extends TestBase func _init() -> void: await _test_join_and_repeat() await _test_repeat_reroll() + await _test_repeat_one_column() await _test_list_save() await _test_only_option() _test_save_load() @@ -88,6 +89,24 @@ func _test_repeat_reroll() -> void: _check(str(col["text"]).count(" & ") <= 1, "rolling again on the repeat row keeps it to two: %s" % col["text"]) +## A combine row set to the 1st column only: the 2nd column just shows its +## own cell when the row comes up there. +func _test_repeat_one_column() -> void: + var b := _surface_board() + var r := b.get_table("surface").find_entry("r") + r.repeat = TableEntry.clean_repeat({"count": 2, "reroll": ["r"], "columns": [0]}) + _eq(r.repeat.get("columns"), [0], "the columns list is kept") + var plain_second := false + for s in 60: + var tree := await Roller.roll_async(b, "start", _ctx(b, s)) + for kid: Dictionary in tree["children"]: + var second: Dictionary = kid["columns"][1] + _check(not str(second["text"]).contains(" & "), "the 2nd column doesn't roll again: %s" % second["text"]) + plain_second = plain_second or str(second["text"]) == "Roll twice and combine" + _check(plain_second, "it shows the row's own cell there instead") + _check(RollColumns.repeats_in(r, 0) and not RollColumns.repeats_in(r, 1), "repeats_in follows the list") + + func _test_list_save() -> void: var b := TowerBoard.new() var res := RollTable.new("res", "Resource") diff --git a/ui/inspector_columns.gd b/ui/inspector_columns.gd index b72f232..8276aec 100644 --- a/ui/inspector_columns.gd +++ b/ui/inspector_columns.gd @@ -213,9 +213,34 @@ static func _repeat_row(insp: Inspector, t: RollTable, e: TableEntry) -> Control insp.wrote(t)) v.add_child(skip) v.add_child(InspectorKit.note("Otherwise it adds another roll each time it comes up.", Style.MUTED, 12)) + if t.columns.size() > 1 and t.column_rolls == RollTable.COLUMNS_EACH: + v.add_child(_repeat_columns(insp, t, e)) return v +## "In these columns": one tick box per column. All ticked (the default) saves +## no list, so the row rolls again in every column it comes up in. +static func _repeat_columns(insp: Inspector, t: RollTable, e: TableEntry) -> Control: + var row := InspectorKit.flow() + row.add_child(InspectorKit.word("Only in")) + for index in t.columns.size(): + var column_name := str(t.columns[index].get("name", "Column %d" % (index + 1))) + var tick := InspectorKit.check_box(column_name, RollColumns.repeats_in(e, index)) + InspectorKit.tag(tick, "res.repeat.column.%d" % index) + tick.toggled.connect(func(on: bool) -> void: + var chosen: Array[int] = [] + for other in t.columns.size(): + if (other == index and on) or (other != index and RollColumns.repeats_in(e, other)): + chosen.append(other) + if chosen.is_empty() or chosen.size() == t.columns.size(): + e.repeat.erase("columns") + else: + e.repeat["columns"] = chosen + insp.wrote(t)) + row.add_child(tick) + return row + + # ---------- Editing (keeps every result's cells in step) ---------- ## Turns columns on: "Result" (the results' own text) plus one more.