class_name RollColumns extends RefCounted ## Cards with columns (a multi-column table, RollTable.columns): after a roll ## card with columns picks its result and expands its text, fill() works out ## the other columns. ## ## - "row" (RollTable.COLUMNS_ROW): every column reads the same result's cells. ## - "each" (COLUMNS_EACH): each column after the first is rolled on its own, ## the same way the card was (the thread's dice and rerolls included). The ## first column is the card's own roll, so its result's changes and threads ## 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 ## 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. ## ## Columns are worked out left to right. A column with "save_as" saves its ## text before the next one starts, so a later column's "when" (only roll it ## if …) can read it. A skipped column saves nothing. ## ## The node gets "columns": [{name, text, index, value?, skipped?}] and its ## summary becomes "Resources: Rich · Water: Plentiful", or with column_join ## the texts joined ("Ash Canyons"). const REPEAT_JOIN := " & " const MAX_REPEATS := 50 static func fill(node: Dictionary, f: Dictionary, ctx: RollContext, depth: int) -> void: var t: RollTable = node["table"] var rows: Array = [] var parts := PackedStringArray() var texts := PackedStringArray() var log: Array = [] var why: Array = [] for k in t.columns.size(): var col: Dictionary = t.columns[k] var nm := str(col.get("name", "")) var row := {"name": nm, "text": "", "index": int(node.get("index", -1))} if k > 0: var w := str(col.get("when", "")).strip_edges() if w != "": var r := Expr.evaluate(w, ctx) if r["error"] != "": log.append({"text": "Column %s, only if %s: %s" % [nm, w, r["error"]], "error": true}) if r["error"] != "" or not Expr.truthy(r["value"]): row["skipped"] = true rows.append(row) continue var text := "" var pick := node if k > 0 and t.column_rolls == RollTable.COLUMNS_EACH: pick = RollCards.pick_for(t, ctx, f) _add_why(why, pick, nm) if pick.has("stop"): text = str(pick["summary"]) else: row["index"] = int(pick["index"]) row["value"] = pick.get("value") var e: TableEntry = pick["entry"] if not e.repeat.is_empty(): text = await _repeat(node, t, k, e, f, ctx, depth, why) elif k == 0: text = str(node["summary"]) else: text = await _cell(node, e, k, ctx, depth) row["text"] = text rows.append(row) var save := str(col.get("save_as", "")).strip_edges() if save != "": ctx.save_result(save, plain(text), t.id) if Formatter.strip_tags(text).strip_edges() != "": parts.append("%s: %s" % [Formatter.escape_literal(nm), text]) texts.append(text) node["columns"] = rows node["summary"] = t.column_join.join(texts) if t.column_join != "" else " · ".join(parts) Roller._log(node, log) Roller._why(node, why) ## Column `k`'s text on entry `e`, expanded; inline rolls become children. static func _cell(node: Dictionary, e: TableEntry, k: int, ctx: RollContext, depth: int) -> String: ctx.depth = depth + 1 var ex: Dictionary = await TextExpander.expand_async(RollTable.cell_text(e, k), ctx) ctx.depth = depth for c: Dictionary in ex["children"]: c["inline"] = true (node["children"] as Array).append(c) return str(ex["text"]) ## A repeat row came up in column `k`: rolls the column `e.repeat.count` times ## (skipping the repeat's reroll ids) and combines the texts. Works through a ## queue, not recursion; stops after MAX_REPEATS rolls. static func _repeat(node: Dictionary, t: RollTable, k: int, e: TableEntry, f: Dictionary, ctx: RollContext, depth: int, why: Array) -> String: var nm := str(t.columns[k].get("name", "")) var queue: Array = [] for _n in int(e.repeat.get("count", 2)): queue.append(e.repeat.get("reroll", [])) var got := PackedStringArray() var facts: Array = [] var rolls := 0 while not queue.is_empty() and rolls < MAX_REPEATS: rolls += 1 var skip: Array = queue.pop_front() var p := RollCards.pick_for(t, ctx, {"dice": str(f.get("dice", "")), "reroll": skip}) _add_why(why, p, nm) if p.has("stop"): got.append(str(p["summary"])) continue var pe: TableEntry = p["entry"] if not pe.repeat.is_empty(): for _n in int(pe.repeat.get("count", 2)): queue.append(pe.repeat.get("reroll", [])) continue var s := await _cell(node, pe, k, ctx, depth) got.append(s) facts.append({"value": p.get("value"), "text": plain(s)}) why.append({"type": "repeat", "column": nm, "rolls": facts}) return REPEAT_JOIN.join(got) static func _add_why(why: Array, pick: Dictionary, column: String) -> void: if pick.has("why"): for fact: Dictionary in pick["why"]: var c := fact.duplicate() c["column"] = column why.append(c) ## A column's text as a value: no formatting, no internal markers. static func plain(text: String) -> String: return Formatter.strip_tags(Formatter.to_export_bbcode(text)).strip_edges()