class_name RollNodes extends RefCounted ## Small helpers that build the Roller's result nodes: a stop node, hanging a ## node under its parent, counting actions against the limit, and adding ## effect-log lines and why-trace facts. Roller._stop, _attach, _take_action, ## _log and _why hand their work to the functions here. ## A node that says why the chain couldn't continue (`why` is its summary). static func stop(table: RollTable, why: String, extra: Dictionary = {}) -> Dictionary: var node := {"table": table, "stop": why, "summary": why, "children": []} node.merge(extra, true) return node ## Hangs `node` under the frame's parent, with the frame's save name and card ## inputs. A top-level node also becomes ctx.root. static func attach(frame: Dictionary, node: Dictionary, ctx: RollContext) -> void: if str(frame.get("save", "")) != "": node["save_as"] = frame["save"] if frame.has("inputs"): node["inputs"] = frame["inputs"] var facts: Array = frame["pre_why"] if node.has("why"): facts.append_array(node["why"]) node["why"] = facts add_log(node, frame["pre_log"]) (frame["parent"]["children"] as Array).append(node) if frame.get("top", false): ctx.root = node ## Counts one action, or stops the roll at the limit (attaching the marker ## under `frame.parent`). Returns false when the roll has to stop. static func take_action(frame: Dictionary, table: RollTable, ctx: RollContext) -> bool: if ctx.actions >= ctx.max_actions: var message := Roller.limit_text(ctx.max_actions) attach(frame, stop(table, message, {"limit": true}), ctx) ctx.halted = message return false ctx.actions += 1 return true ## Appends why-trace facts to a node's "why". static func add_why(node: Dictionary, facts: Array) -> void: if facts.is_empty(): return if not node.has("why"): node["why"] = facts else: (node["why"] as Array).append_array(facts) ## Appends effect-log lines to a node's "effects". static func add_log(node: Dictionary, lines: Array) -> void: if lines.is_empty(): return if not node.has("effects"): node["effects"] = [] (node["effects"] as Array).append_array(lines)