class_name BuilderValues extends RefCounted ## Finds every value a board uses (variable cards, collections, effect ## targets, saved answers, card inputs, values rules only read, and chain ## names), so value pickers can offer them. Builder.known_values() delegates ## here. ## Every value the board uses, for value pickers: Array of {"name", "label", ## "kind", "source"} with optional extras, variable cards first, then ## collections, then the rest in board order, then chain names. One item per ## name (the first source wins). ## name "@luck" (board value), "@skill_*" (a collection), "tier" (chain) ## label Describe.label(name, board) ## kind "number", "text", "list", "collection" or "scale" ## source "variable" (a variable card), "collection", "effect" (an effect ## target), "save_as" (a pipe's or choice's save_as, or ## [[Table -> name]]), "input" (a card input), "read" (only read by ## a rule), "chain" (answer, option, i, total, nat, effect) ## table_id the card that declares, sets or reads it first ## collection on members of a collection: its pattern ("@skill_*") ## Chain names are listed only when the board has a card that sets them ## (answer: number/text choices; option: options from variables; i: loops; ## nat: roll tables and checks; total/effect: checks). static func known_values(board: TowerBoard) -> Array[Dictionary]: var out: Array[Dictionary] = [] if board == null: return out var seen := {} var stems: Array = [] # [stem, pattern] var add := func(name: String, kind: String, source: String, table_id: String) -> void: if name == "" or seen.has(name): return seen[name] = true var item := {"name": name, "label": Describe.label(name, board), "kind": kind, "source": source, "table_id": table_id} if name.begins_with("@") and not name.ends_with("*"): for stem_pair: Array in stems: if name.begins_with("@" + str(stem_pair[0]) + "_") and name.length() > str(stem_pair[0]).length() + 2: item["collection"] = stem_pair[1] break out.append(item) for table in board.tables: if table.kind == RollTable.KIND_VARIABLE and table.var_name != "" and not table.is_collection(): add.call("@" + table.var_name, table.var_type, "variable", table.id) for table in board.tables: if table.is_collection() and table.var_name != "": stems.append([table.var_name, "@%s_*" % table.var_name]) stems.sort_custom(func(left: Array, right: Array) -> bool: return str(left[0]).length() > str(right[0]).length()) for table in board.tables: if table.is_collection() and table.var_name != "": add.call("@%s_*" % table.var_name, "collection", "collection", table.id) var reads: Array = [] var chain := {} for table in board.tables: _add_table_values(table, add, reads, chain) for read: Array in reads: var src := str(read[0]).strip_edges() if src == "" or not src.contains("@"): continue for token: Dictionary in RuleTokens.tokenize(src): if token["t"] == "var" and not str(token["s"]).begins_with("@["): add.call(str(token["s"]), "number", "read", str(read[1])) for name: String in Builder.CHAIN_NAMES: if chain.has(name): add.call(name, "text" if name == "option" else "number", "chain", "") return out ## One card's values: calls `add` for what it sets, appends [rule, table id] ## to `reads` for every rule it reads, and marks in `chain` which chain ## names it sets. static func _add_table_values(table: RollTable, add: Callable, reads: Array, chain: Dictionary) -> void: for col: Dictionary in table.columns: if str(col.get("save_as", "")) != "": add.call(_value_name(str(col["save_as"])), "text", "save_as", table.id) reads.append([str(col.get("when", "")), table.id]) match table.kind: RollTable.KIND_CHOICE: if table.choice != "pick": chain["answer"] = true if table.save_as != "": add.call(_value_name(table.save_as), "number" if table.choice == "number" else "text", "save_as", table.id) else: if table.is_dynamic(): chain["option"] = true if table.save_as != "": add.call(_value_name(table.save_as), "text", "save_as", table.id) reads.append([table.options_where, table.id]) reads.append([table.min_expr, table.id]) reads.append([table.max_expr, table.id]) RollTable.KIND_LOOP: chain["i"] = true reads.append([table.loop_expr, table.id]) RollTable.KIND_CHECK: chain["nat"] = true chain["total"] = true chain["effect"] = true reads.append([table.check_dice, table.id]) reads.append([table.check_mod, table.id]) reads.append([table.check_target, table.id]) RollTable.KIND_ROLL: chain["nat"] = true if table.is_expr_dice(): reads.append([table.dice, table.id]) if table.has_mod(): reads.append([table.dice_mod, table.id]) for inp: Dictionary in table.inputs: add.call(str(inp.get("name", "")), _kind_of_expr(str(inp.get("default", ""))), "input", table.id) for entry in table.thread_sources(): _add_entry_values(table, entry, add, reads) ## One result's values: its effect targets, saved pipe answers and ## [[Table -> name]] saves, and the rules it reads. static func _add_entry_values(table: RollTable, entry: TableEntry, add: Callable, reads: Array) -> void: for model in Builder.parse_effects(entry.effects): if not model["ok"]: continue var target := str(model["var"]) if target.begins_with("@[") or target.ends_with("*"): continue var kind := "number" match str(model["op"]): "append", "remove": kind = "list" "set": kind = "text" if RuleTokens.is_text_literal(str(model["amount"])) else "number" add.call(target, kind, "effect", table.id) if model["op"] != "reset": reads.append([str(model["amount"]), table.id]) for pipe in entry.pipes: var save_name := entry.save_for(pipe) if save_name != "": add.call(_value_name(save_name), "text", "save_as", table.id) reads.append([entry.when_for(pipe), table.id]) reads.append([entry.count_expr(pipe), table.id]) var with_values := entry.with_for(pipe) for name: String in with_values: reads.append([str(with_values[name]), table.id]) for ref in JsonIO.find_refs(entry.text): if str(ref["var"]) != "": add.call(_value_name(str(ref["var"])), "text", "save_as", table.id) reads.append([entry.when, table.id]) reads.append([entry.show_if, table.id]) static func _value_name(text: String) -> String: var trimmed := text.strip_edges() return "@" + trimmed.substr(1).to_lower() if trimmed.begins_with("@") else trimmed static func _kind_of_expr(src: String) -> String: return "text" if RuleTokens.is_text_literal(src) else "number"