2026-11-14 13:19:54 -06:00
|
|
|
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"]
|
2026-11-16 10:41:13 -06:00
|
|
|
if frame.get("card", false):
|
|
|
|
|
node["card_thread"] = true
|
2026-11-14 14:23:07 -06:00
|
|
|
# Facts from before the pick (inputs, a paid cost) come first.
|
|
|
|
|
if frame.has("pre_why"):
|
|
|
|
|
var facts: Array = (frame["pre_why"] as Array).duplicate()
|
2026-11-14 13:19:54 -06:00
|
|
|
if node.has("why"):
|
|
|
|
|
facts.append_array(node["why"])
|
|
|
|
|
node["why"] = facts
|
2026-11-14 14:23:07 -06:00
|
|
|
add_log(node, frame.get("pre_log", []))
|
2026-11-14 13:19:54 -06:00
|
|
|
(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)
|