TableTower/core/roll_tree.gd
RobinFiredrake 200edfd67e Book tables: columns as one phrase, roll twice and combine, results saved into lists, one-option picks answer themselves
- column_join: a card with columns reads as one phrase ("Ash Canyons"), which threads save
- TableEntry.repeat: a result that rolls its column again N times and combines them
- Saving a result into a declared list value adds an item (RollContext.save_result)
- A pick with only one available option picks it without asking
- Lint, why facts, inspector controls, tests, LOGIC.md

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 09:06:55 -06:00

151 lines
5.4 KiB
GDScript

class_name RollTree
extends RefCounted
## Reading a Roller result tree: tray steps (step_of(), flatten()), searches
## over it, and the lit entry / pipe / reference keys the board highlights.
## One line of the result tray for a single node. Plain data only, because it
## is saved in the roll history. "text" is BBCode with internal markers.
## Logic-card keys (only when they apply): kind, effects (Array of {text,
## error?}), answer, passes, pass, after, picked, random, limit, cancelled,
## case (router nodes: the matched case's index), option, check ({nat, mod,
## total, target, success}), inputs ({name: value}) and why (the trace facts,
## a copy of the array; the facts themselves are shared, don't change them).
static func step_of(n: Dictionary, depth: int = 0) -> Dictionary:
var t: RollTable = n["table"]
var step := {
"table": t.name,
"table_id": t.id,
"num": t.fmt(n["value"]) if n.has("value") else "",
"die": t.die_size(),
"dice": t.roll_text() if t.has_mod() else t.dice_label(),
"text": str(n.get("summary", n.get("stop", ""))),
"depth": depth,
}
if n.has("entry"):
step["entry_id"] = (n["entry"] as TableEntry).id
if n.has("stop"):
step["stop"] = true
if n.get("inline", false):
step["inline"] = true
if n.get("reshuffled", false):
step["reshuffled"] = true
if n.has("save_as"):
step["save_as"] = n["save_as"]
if n.has("kind"):
step["kind"] = n["kind"]
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"]:
if n.has(k):
step[k] = n[k]
if n.has("why"):
step["why"] = (n["why"] as Array).duplicate()
return step
## The whole chain in the order it was rolled. Iterative, so any length is
## fine; works on a partial tree (ctx.root while a roll is running) too.
static func flatten(n: Dictionary, out: Array = [], depth: int = 0) -> Array:
if n.is_empty():
return out
var stack: Array = [[n, depth]]
while not stack.is_empty():
var top: Array = stack.pop_back()
var node: Dictionary = top[0]
var d: int = top[1]
out.append(step_of(node, d))
var kids: Array = node.get("children", [])
for k in range(kids.size() - 1, -1, -1):
stack.append([kids[k], d + 1])
return out
static func reaches(n: Dictionary, table_id: String) -> bool:
if n.is_empty():
return false
var stack: Array = [n]
while not stack.is_empty():
var node: Dictionary = stack.pop_back()
if (node["table"] as RollTable).id == table_id:
return true
stack.append_array(node.get("children", []))
return false
## The last sheet node reached in a tree, or {} (the result headline when a
## chain reaches a sheet card).
static func last_sheet(n: Dictionary) -> Dictionary:
var found := {}
if n.is_empty():
return found
var stack: Array = [n]
while not stack.is_empty():
var node: Dictionary = stack.pop_back()
if str(node.get("kind", "")) == RollTable.KIND_SHEET and not node.has("stop"):
found = node
var kids: Array = node.get("children", [])
for k in range(kids.size() - 1, -1, -1):
stack.append(kids[k])
return found
## Marks every entry, pipe and reference pipe along the chain as lit. When
## given, `texts` gets each lit entry's expanded text (entry key -> BBCode).
## Iterative. A loop's pass and Afterwards nodes light their entries but no
## pipe (they aren't reached through one).
static func collect_lit(n: Dictionary, entries: Dictionary, pipes: Dictionary, texts: Dictionary = {}) -> void:
if n.is_empty():
return
var stack: Array = [n]
while not stack.is_empty():
var node: Dictionary = stack.pop_back()
if node.is_empty() or node.has("stop"):
continue
var t: RollTable = node["table"]
var e: TableEntry = node["entry"]
entries[entry_key(t.id, e.id)] = true
texts[entry_key(t.id, e.id)] = str(node.get("summary", e.text))
var kids: Array = node["children"]
for c: Dictionary in kids:
if not (c.has("pass") or c.has("after")):
pipes[link_key(t.id, e.id, c)] = true
for k in range(kids.size() - 1, -1, -1):
stack.append(kids[k])
static func entry_key(table_id: String, entry_id: String) -> String:
return table_id + ":" + entry_id
static func pipe_key(table_id: String, entry_id: String, to_id: String) -> String:
return "%s:%s>%s" % [table_id, entry_id, to_id]
## Key of a reference pipe: a [[reference]] in an entry's text to another table.
static func ref_key(table_id: String, entry_id: String, to_id: String) -> String:
return "%s:%s~%s" % [table_id, entry_id, to_id]
static func is_ref_key(key: String) -> bool:
return key.contains("~")
## The pipe or reference-pipe key that led from an entry to child node `c`.
static func link_key(table_id: String, entry_id: String, c: Dictionary) -> String:
var to := (c["table"] as RollTable).id
return ref_key(table_id, entry_id, to) if c.get("inline", false) else pipe_key(table_id, entry_id, to)
## Ids of the tables an entry's text references, resolved against the board,
## without duplicates or missing tables.
static func ref_targets(board: TowerBoard, text: String) -> Array[String]:
var out: Array[String] = []
if not text.contains("[[") and not text.contains("|"):
return out
var res := func(name: String) -> String: return Roller.resolve(board, name)
for name in TextExpander.references(text, res):
var id := Roller.resolve(board, name)
if id != "" and not out.has(id):
out.append(id)
return out