From 3aeb98c2fddadfd3594def73c0afdd71e176738a Mon Sep 17 00:00:00 2001 From: RobinFiredrake Date: Sat, 14 Nov 2026 13:40:20 -0600 Subject: [PATCH] Add to list: lists made there are lists, the result's text is the item, Allow repeats Making a new value from an Add to list row made a number, which the list picker then hid and which couldn't hold words. Now it makes a list. The item starts as the result's own text. List cards get Allow repeats (add every time, in order); off keeps adding only new items. Co-Authored-By: Claude Opus 5.5 --- core/effects.gd | 12 +++++----- core/roll_context.gd | 2 +- core/roll_table.gd | 3 +++ core/roll_table_io.gd | 3 +++ docs/LOGIC.md | 2 +- help/topics.txt | 4 ++-- io/json_canon.gd | 2 +- tests/test_lists.gd | 49 +++++++++++++++++++++++++++++++++++++++++ tests/test_lists.gd.uid | 1 + ui/inspector_card.gd | 8 +++++++ ui/inspector_effects.gd | 5 ++++- ui/inspector_result.gd | 1 + ui/value_picker.gd | 4 +++- 13 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 tests/test_lists.gd create mode 100644 tests/test_lists.gd.uid diff --git a/core/effects.gd b/core/effects.gd index 0bb0d76..ca7c230 100644 --- a/core/effects.gd +++ b/core/effects.gd @@ -20,17 +20,17 @@ const OPS := ["+=", "-=", "*=", "/=", "="] ## A comma-separated list with `item` added at the end (unless it's there -## already, ignoring capitals) or, with `remove`, taken out. Returns the new -## list text. Used by += / -= on lists and by saving a result into a list -## value (RollContext.save_result()). -static func list_with(list: Variant, item: String, remove: bool = false) -> String: +## already, ignoring capitals; with `repeats`, every time) or, with `remove`, +## taken out (the first match). Returns the new list text. Used by += / -= on +## lists and by saving a result into a list value (RollContext.save_result()). +static func list_with(list: Variant, item: String, remove: bool = false, repeats: bool = false) -> String: var items := Expr.list_items(list if list != null else "") var at := -1 for k in items.size(): if items[k].to_lower() == item.to_lower(): at = k break - if not remove and at < 0 and item != "": + if not remove and (at < 0 or repeats) and item != "": items.append(item) elif remove and at >= 0: items.remove_at(at) @@ -230,7 +230,7 @@ static func _assign(st: Dictionary, ctx: RollContext, source_id: String, facts: "+=", "-=": if as_list: var item := Expr.format_value(rhs).strip_edges() - value = list_with(cur, item, op == "-=") + value = list_with(cur, item, op == "-=", ctx.declared(target) != null and ctx.declared(target).list_repeats) line = "%s %s %s → %s" % [label, "+" if op == "+=" else "−", item, _shown(value)] else: var a: Variant = Expr.to_value(cur) if cur != null and str(cur).strip_edges() != "" else 0 diff --git a/core/roll_context.gd b/core/roll_context.gd index f0ad980..751e44b 100644 --- a/core/roll_context.gd +++ b/core/roll_context.gd @@ -128,7 +128,7 @@ func save_result(name: String, value: Variant, source_id: String = "") -> void: return var list: Variant = get_value(n) for item in Expr.list_items(Formatter.strip_tags(Expr.format_value(value))): - list = Effects.list_with(list, item) + list = Effects.list_with(list, item, false, card.list_repeats) save(n, list if list != null else "", source_id) diff --git a/core/roll_table.gd b/core/roll_table.gd index 7879b9a..e1a0d73 100644 --- a/core/roll_table.gd +++ b/core/roll_table.gd @@ -120,6 +120,9 @@ var var_type: String = "number" var initial: String = "" ## Scale variables: the ordered steps, lowest first. var steps: Array[String] = [] +## List cards: true when the list keeps repeats (an item added every time, in +## order); false adds an item only if it isn't there yet. +var list_repeats := false ## Ledger cards: the entries, [{name, start}] (LedgerMembers). var members: Array = [] ## Check cards: the dice, the modifier and the target (expressions) and the diff --git a/core/roll_table_io.gd b/core/roll_table_io.gd index be5d3ab..328ade9 100644 --- a/core/roll_table_io.gd +++ b/core/roll_table_io.gd @@ -72,6 +72,8 @@ static func to_dict(t: RollTable) -> Dictionary: d["initial"] = t.initial if t.var_type == RollTable.VAR_SCALE and not t.steps.is_empty(): d["steps"] = t.steps.duplicate() + if t.var_type == "list" and t.list_repeats: + d["repeats"] = true if t.is_collection() and not t.members.is_empty(): d["members"] = t.members.duplicate(true) elif t.kind == RollTable.KIND_CHECK: @@ -138,6 +140,7 @@ static func from_dict(d: Dictionary) -> RollTable: t.initial = RollTable.expr_text(d.get("initial", ""), "") t.steps = Scales.clean(d.get("steps", [])) t.members = LedgerMembers.clean(d.get("members", [])) + t.list_repeats = bool(d.get("repeats", false)) t.check_dice = RollTable.expr_text(d.get("check_dice", RollTable.CHECK_DEFAULT_DICE), RollTable.CHECK_DEFAULT_DICE).strip_edges() if t.check_dice == "": t.check_dice = RollTable.CHECK_DEFAULT_DICE diff --git a/docs/LOGIC.md b/docs/LOGIC.md index 96f1fd1..e4df559 100644 --- a/docs/LOGIC.md +++ b/docs/LOGIC.md @@ -401,7 +401,7 @@ follow. ``` @str = 2d6 set -@terms += 1 add (number) · append ", item" if not present (text/list) +@terms += 1 add (number) · append ", item" if not present (text/list; a list card with `"repeats": true` appends every time) @credits -= 1000 subtract (number) · remove the item (text/list) @age *= 2 multiply (/= divides, rounding down) @skill_pilot += 1 unset counts as 0, so this gives Pilot 1 or raises it diff --git a/help/topics.txt b/help/topics.txt index 8fbbbf8..863fd48 100644 --- a/help/topics.txt +++ b/help/topics.txt @@ -126,7 +126,7 @@ An option can have [b]Show only if[/b], so it's offered only when a condition is # Values and changes A spread can remember [b]values[/b] between rolls, such as Luck, Gold or the current town. In rules a value is written with @: `@luck`. -- Any result can have [b]changes[/b] that happen when it comes up: [b]Add 1 to Luck[/b], [b]Subtract 3 from Gold[/b], [b]Set Town to …[/b]. Edit them in the inspector as rows, or with Edit as text. +- Any result can have [b]changes[/b] that happen when it comes up: [b]Add 1 to Luck[/b], [b]Subtract 3 from Gold[/b], [b]Set Town to …[/b], [b]Add to list[/b] (the result's own text, unless you type another item). Edit them in the inspector as rows, or with Edit as text. - A [b]Variable[/b] card gives a value a name, a type and a starting value, and shows what it is now. The sample's [b]Luck[/b] card starts at 0. - A [b]Ledger[/b] card groups related values, such as Skills, so they can be listed and picked from together. In Build mode, add and rename its entries right on the card; each entry's number is where it starts. - The tray's [b]Variables[/b] tab shows every value, and lets you clear or reset them. [b]Reset all…[/b] puts every value back to its start. @@ -134,7 +134,7 @@ A spread can remember [b]values[/b] between rolls, such as Luck, Gold or the cur ## Writing changes as text - `@luck += 1` adds, `@luck -= 1` subtracts and `@gold = 2d6` sets. - `reset @luck` goes back to the starting value. `reset @skill_*` resets every value starting with skill_. -- Text values work as lists: `@traits += "Brave"` adds an item. +- Text values work as lists: `@traits += "Brave"` adds an item, and `-=` takes one out. A [b]List[/b] variable adds an item only if it isn't there yet, unless you tick [b]Allow repeats[/b]; then it adds it every time, in order. ## Conditions Conditions are used by Only if threads, branches, repeats and Show only if. In plain words they read "Luck is at least 2". As text: `@luck >= 2`, `@town = "Saltmere"`, `@gold > 0 and @luck < 3`. diff --git a/io/json_canon.gd b/io/json_canon.gd index 0029d93..26cd710 100644 --- a/io/json_canon.gd +++ b/io/json_canon.gd @@ -129,7 +129,7 @@ static func _canon_table(td: Dictionary) -> Dictionary: for e in src_entries: entries.append(_canon_entry(e)) t["entries"] = entries - _keep(td, t, ["color", "w", "list_h", "roll_floor", "roll_ceiling", "deck_empty_text", "columns", "column_rolls", "steps", "members"]) + _keep(td, t, ["color", "w", "list_h", "roll_floor", "roll_ceiling", "deck_empty_text", "columns", "column_rolls", "steps", "members", "repeats"]) # A separator may be just a space, so it isn't trimmed. if td.get("column_join") is String and td["column_join"] != "": t["column_join"] = td["column_join"] diff --git a/tests/test_lists.gd b/tests/test_lists.gd new file mode 100644 index 0000000..88504f4 --- /dev/null +++ b/tests/test_lists.gd @@ -0,0 +1,49 @@ +extends TestBase +## List values: the "Add to list" change row (picking a list, the result's own +## text as the item, a new value made there is a list), and "Allow repeats". +## Run: godot --headless --path . -s res://tests/test_lists.gd -- --sample + + +func _init() -> void: + if not await start_main(): + return + main.set_mode(Main.Mode.BUILD) + main.data.load_dict({"format_version": 2, "id": "list-test", "name": "List test", "tables": [ + {"id": "groc", "name": "Groceries", "kind": "variable", "var_name": "groceries", "var_type": "list", "x": 40, "y": 40, "entries": []}, + {"id": "shop", "name": "Shop", "x": 340, "y": 40, "entries": [{"id": "e1", "text": "[b]Eggs[/b]"}]}, + ]}) + main.persistent_vars.clear() + main._rebuild_cards() + await _frames(4) + await _test_add_to_list_row() + _test_repeats() + finish() + + +func _test_add_to_list_row() -> void: + main.select_result("shop", "e1") + await _frames(2) + var fx := main.inspector.effects + fx.add_row() + fx.set_row_op(0, "append") + _eq(fx.rows[0]["amount"], "Eggs", "Add to list offers the result's own text") + fx.set_row_var(0, "@groceries") + _eq(main.data.get_table("shop").find_entry("e1").effects, "@groceries += \"Eggs\"", "and writes it as the item") + var picker := ValuePicker.new(main) + picker.kinds = ["list", "text"] + var made := picker.create_value("Inventory", true) + var card := main.variable_card(made.trim_prefix("@")) + _check(card != null and card.var_type == "list", "a new value made from a list picker is a list") + picker.free() + + +func _test_repeats() -> void: + _eq(Effects.list_with("Eggs", "eggs"), "Eggs", "without repeats an item already there isn't added again") + _eq(Effects.list_with("Eggs", "Eggs", false, true), "Eggs, Eggs", "with repeats it's added every time, in order") + _eq(Effects.list_with("Eggs, Milk, Eggs", "Eggs", true), "Milk, Eggs", "removing takes out the first match") + var groc := main.data.get_table("groc") + groc.list_repeats = true + _check(RollTable.from_dict(groc.to_dict()).list_repeats, "Allow repeats is saved with the card") + var ctx := Roller.new_context(main.data, main.rng, {}, {}) + Effects.run("@groceries += \"Eggs\"\n@groceries += \"Eggs\"", ctx, "shop") + _eq(ctx.get_value("@groceries"), "Eggs, Eggs", "a list that allows repeats collects each one") diff --git a/tests/test_lists.gd.uid b/tests/test_lists.gd.uid new file mode 100644 index 0000000..299fa1f --- /dev/null +++ b/tests/test_lists.gd.uid @@ -0,0 +1 @@ +uid://dct2cpq3ic6dx diff --git a/ui/inspector_card.gd b/ui/inspector_card.gd index 342caf9..f75242f 100644 --- a/ui/inspector_card.gd +++ b/ui/inspector_card.gd @@ -304,6 +304,14 @@ static func _variable(insp: Inspector, box: VBoxContainer, t: RollTable) -> void InspectorScale.build(insp, box, t, err) else: box.add_child(InspectorKit.field("Starting value", setting_line(insp, "initial", t.initial, "0" if t.var_type == "number" else "nothing", err))) + if t.var_type == "list": + var repeats := InspectorKit.check_box("Allow repeats", t.list_repeats) + repeats.tooltip_text = "On: an item is added every time, in order. Off: only if it isn't on the list yet." + InspectorKit.tag(repeats, "card.repeats") + repeats.toggled.connect(func(on: bool) -> void: + t.list_repeats = on + insp.wrote(t)) + box.add_child(repeats) box.add_child(err) diff --git a/ui/inspector_effects.gd b/ui/inspector_effects.gd index 9a85666..8d705cc 100644 --- a/ui/inspector_effects.gd +++ b/ui/inspector_effects.gd @@ -20,6 +20,8 @@ var rows: Array[Dictionary] = [] var text_mode := false var _raw := "" var _key := "fx" +## The result's own text: what "Add to list" adds unless you type something else. +var item_hint := "" func setup(p_main: Main, text: String, p_table: RollTable, as_text: bool, key: String = "fx") -> void: @@ -114,7 +116,8 @@ func set_row_op(i: int, op: String) -> void: if op == "reset": m["amount"] = "" elif listy and not was_listy: - m["amount"] = Builder.unquote(amt) + # A fresh row's "1" isn't an item anyone means: offer the result itself. + m["amount"] = item_hint if item_hint != "" and (amt == "" or amt == "1") else Builder.unquote(amt) elif not listy and was_listy: m["amount"] = InspectorKit.amount_expr(amt, _known_names()) if amt != "" else "" elif amt == "" and was == "reset": diff --git a/ui/inspector_result.gd b/ui/inspector_result.gd index a5878a9..6bcaf8e 100644 --- a/ui/inspector_result.gd +++ b/ui/inspector_result.gd @@ -94,6 +94,7 @@ static func section(insp: Inspector, t: RollTable, e: TableEntry) -> void: insp._content.add_child(InspectorCard.case_block(insp, t, e)) insp.effects = EffectsBuilder.new() + insp.effects.item_hint = Formatter.strip_tags(e.text).strip_edges() insp.effects.setup(insp.main, e.effects, t, not plain, "res.fx") insp.effects.changed.connect(insp.set_entry_effects) insp._content.add_child(InspectorKit.field("When this comes up", insp.effects)) diff --git a/ui/value_picker.gd b/ui/value_picker.gd index fb389b7..09b414c 100644 --- a/ui/value_picker.gd +++ b/ui/value_picker.gd @@ -309,7 +309,9 @@ func _ask_new(initial: String) -> void: func create_value(label_text: String, on_board: bool) -> String: var nm := JsonIO.slug(label_text.strip_edges(), "value") if on_board and main: - var t := main.add_variable_card(label_text.strip_edges(), nm) + # A value made from a picker that only takes lists (Add to list) is a list. + var type := str(kinds[0]) if not kinds.is_empty() and RollTable.VAR_TYPES.has(str(kinds[0])) else "number" + var t := main.add_variable_card(label_text.strip_edges(), nm, type) nm = t.var_name var full := "@" + nm set_value(full)