Group cards and templates
Groups: an optional groups list on a collection (frame rect, collapsed,
members, details), saved by TowerBoard and kept by JsonIO. Expanded groups
draw a frame behind their cards with a title bar; collapsed ones become one
compact card that lists the members' lit results, with pipes re-routed to
its inlet and right edge. Build mode adds multi-select (Ctrl/Shift-click,
Ctrl-drag), Group (Ctrl+G), drop in/out of frames, rename, ungroup, delete
with undo and a details editor.
Templates: TemplateKit saves cards, groups or selections with relative
positions, internal pipes and outside pipes by name, detects {$name}
parameters and inserts with fresh ids. TemplateLibrary keeps user templates
in user://templates and lists the built-in ones in templates/.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 17:01:34 -06:00
|
|
|
|
class_name TemplateKit
|
|
|
|
|
|
extends RefCounted
|
|
|
|
|
|
## Templates: reusable blocks of cards saved from a board and dropped into any
|
|
|
|
|
|
## collection. Pure data work, no UI; storage is TemplateLibrary.
|
|
|
|
|
|
##
|
|
|
|
|
|
## A template file:
|
|
|
|
|
|
## {"format_version": 1, "name", "description", "kind": "card"|"group"|"selection",
|
|
|
|
|
|
## "params": [{"name", "label", "default"}], "tables": [table, ...], "groups": [group, ...]}
|
|
|
|
|
|
## Tables and groups use the collection format (JsonIO), with positions
|
|
|
|
|
|
## relative to the template's top-left corner. Pipes between the template's own
|
|
|
|
|
|
## tables keep their table ids. A pipe to a table outside the template is
|
|
|
|
|
|
## recorded by that table's name, as {"table": "<name>", "outside": true, ...},
|
|
|
|
|
|
## and is relinked by name when the template is inserted.
|
|
|
|
|
|
##
|
|
|
|
|
|
## Parameters are placeholders written {$name} in any text: table and entry
|
|
|
|
|
|
## names, entry text, details, effects, expressions, prompts, variable names,
|
|
|
|
|
|
## save_as. Inserting asks for their values and substitutes them everywhere.
|
|
|
|
|
|
|
|
|
|
|
|
const FORMAT_VERSION := 1
|
|
|
|
|
|
const KINDS: Array[String] = ["card", "group", "selection"]
|
|
|
|
|
|
const _PARAM_RE := "\\{\\$([A-Za-z_][A-Za-z0-9_]*)\\}"
|
|
|
|
|
|
## Rough size of a card, for placing templates without groups.
|
|
|
|
|
|
const CARD_SIZE := Vector2(256, 180)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------- save
|
|
|
|
|
|
|
|
|
|
|
|
## Builds a template from tables on `board`. `table_ids` are the tables to
|
|
|
|
|
|
## include (a group's members are added for every id in `group_ids`). Params
|
|
|
|
|
|
## are detected from the {$name} placeholders, with an empty label and default.
|
|
|
|
|
|
static func make(board: TowerBoard, table_ids: Array, kind: String, tname: String,
|
|
|
|
|
|
description: String = "", group_ids: Array = []) -> Dictionary:
|
|
|
|
|
|
var want := {}
|
|
|
|
|
|
for tid: Variant in table_ids:
|
|
|
|
|
|
if board.get_table(str(tid)):
|
|
|
|
|
|
want[str(tid)] = true
|
|
|
|
|
|
var groups: Array[TableGroup] = []
|
|
|
|
|
|
for gid: Variant in group_ids:
|
2026-11-10 18:47:05 -06:00
|
|
|
|
# A group brings its inner groups along.
|
|
|
|
|
|
for g in board.groups_in(str(gid)):
|
|
|
|
|
|
if not groups.has(g):
|
|
|
|
|
|
groups.append(g)
|
|
|
|
|
|
for m in g.members:
|
|
|
|
|
|
want[m] = true
|
Group cards and templates
Groups: an optional groups list on a collection (frame rect, collapsed,
members, details), saved by TowerBoard and kept by JsonIO. Expanded groups
draw a frame behind their cards with a title bar; collapsed ones become one
compact card that lists the members' lit results, with pipes re-routed to
its inlet and right edge. Build mode adds multi-select (Ctrl/Shift-click,
Ctrl-drag), Group (Ctrl+G), drop in/out of frames, rename, ungroup, delete
with undo and a details editor.
Templates: TemplateKit saves cards, groups or selections with relative
positions, internal pipes and outside pipes by name, detects {$name}
parameters and inserts with fresh ids. TemplateLibrary keeps user templates
in user://templates and lists the built-in ones in templates/.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 17:01:34 -06:00
|
|
|
|
# The top-left corner: the tables' corner, or a group frame's when it's further out.
|
|
|
|
|
|
var origin := Vector2(INF, INF)
|
|
|
|
|
|
for t in board.tables:
|
|
|
|
|
|
if want.has(t.id):
|
|
|
|
|
|
origin = Vector2(minf(origin.x, t.position.x), minf(origin.y, t.position.y))
|
|
|
|
|
|
for g in groups:
|
|
|
|
|
|
origin = Vector2(minf(origin.x, g.position.x), minf(origin.y, g.position.y))
|
|
|
|
|
|
if origin.x == INF:
|
|
|
|
|
|
origin = Vector2.ZERO
|
|
|
|
|
|
var tables: Array = []
|
|
|
|
|
|
for t in board.tables:
|
|
|
|
|
|
if not want.has(t.id):
|
|
|
|
|
|
continue
|
|
|
|
|
|
var d := JsonIO._canon_table(t.to_dict())
|
|
|
|
|
|
d["x"] = int(d["x"] - origin.x)
|
|
|
|
|
|
d["y"] = int(d["y"] - origin.y)
|
|
|
|
|
|
for ed: Dictionary in d["entries"]:
|
|
|
|
|
|
if not ed.has("pipes"):
|
|
|
|
|
|
continue
|
|
|
|
|
|
var pipes: Array = []
|
|
|
|
|
|
for p: Variant in ed["pipes"]:
|
|
|
|
|
|
var info := JsonIO.pipe_info(p)
|
|
|
|
|
|
var target: String = info["table"]
|
|
|
|
|
|
if want.has(target):
|
|
|
|
|
|
pipes.append(p)
|
|
|
|
|
|
continue
|
|
|
|
|
|
var other := board.get_table(target)
|
|
|
|
|
|
if other == null:
|
|
|
|
|
|
continue
|
|
|
|
|
|
var np := {"table": other.name, "outside": true}
|
|
|
|
|
|
if p is Dictionary:
|
Plain Build engine: Describe, Builder, checks, inputs, collections, why trace
Implements docs/PLAIN.md sections 1-5 (the engine half of the usability pass):
- core/describe.gd (Describe): rules in plain words, labelled from variable
cards, collections and card inputs, falling back to the original text.
- core/builder.gd (Builder): effect and condition models that round-trip
with the rule text, a rule tokenizer, and known_values(board).
- Collections: variable cards with var_type "collection" label their
members and don't declare their stem.
- Check cards (kind "check"): dice + modifier vs target, Success/Failure,
chain values nat/total/effect, node "check" info, lint and static checks.
- Card inputs and pipe "with" values, scoped over the pipe's subtree on the
explicit work stack (OP_RESTORE frames), with defaults and snapshot
evaluation.
- nat on every roll-table roll; dice faces recorded through ctx.faces.
- Why trace: facts on nodes and steps (input, roll, check, case, show_if,
options_where, effect, when, otherwise).
- StateStore rules_view setting; JSON for the new keys; LOGIC.md and
PLAIN.md updated (deviations in PLAIN.md 5a).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 21:01:24 -06:00
|
|
|
|
for k in ["count", "save_as", "with"]:
|
Group cards and templates
Groups: an optional groups list on a collection (frame rect, collapsed,
members, details), saved by TowerBoard and kept by JsonIO. Expanded groups
draw a frame behind their cards with a title bar; collapsed ones become one
compact card that lists the members' lit results, with pipes re-routed to
its inlet and right edge. Build mode adds multi-select (Ctrl/Shift-click,
Ctrl-drag), Group (Ctrl+G), drop in/out of frames, rename, ungroup, delete
with undo and a details editor.
Templates: TemplateKit saves cards, groups or selections with relative
positions, internal pipes and outside pipes by name, detects {$name}
parameters and inserts with fresh ids. TemplateLibrary keeps user templates
in user://templates and lists the built-in ones in templates/.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 17:01:34 -06:00
|
|
|
|
if (p as Dictionary).has(k):
|
|
|
|
|
|
np[k] = p[k]
|
|
|
|
|
|
pipes.append(np)
|
|
|
|
|
|
ed["pipes"] = pipes
|
|
|
|
|
|
tables.append(d)
|
|
|
|
|
|
var group_list: Array = []
|
|
|
|
|
|
for g in groups:
|
|
|
|
|
|
var gd := g.to_dict()
|
|
|
|
|
|
gd["x"] = int(gd["x"] - origin.x)
|
|
|
|
|
|
gd["y"] = int(gd["y"] - origin.y)
|
|
|
|
|
|
var members: Array = []
|
|
|
|
|
|
for m in g.members:
|
|
|
|
|
|
if want.has(m):
|
|
|
|
|
|
members.append(m)
|
|
|
|
|
|
gd["members"] = members
|
|
|
|
|
|
gd.erase("id")
|
2026-11-10 18:47:05 -06:00
|
|
|
|
# Inner groups point at their outer group by its place in this list.
|
|
|
|
|
|
gd.erase("parent")
|
|
|
|
|
|
var outer := board.get_group(g.parent)
|
|
|
|
|
|
if outer and groups.has(outer):
|
|
|
|
|
|
gd["parent_at"] = groups.find(outer)
|
Group cards and templates
Groups: an optional groups list on a collection (frame rect, collapsed,
members, details), saved by TowerBoard and kept by JsonIO. Expanded groups
draw a frame behind their cards with a title bar; collapsed ones become one
compact card that lists the members' lit results, with pipes re-routed to
its inlet and right edge. Build mode adds multi-select (Ctrl/Shift-click,
Ctrl-drag), Group (Ctrl+G), drop in/out of frames, rename, ungroup, delete
with undo and a details editor.
Templates: TemplateKit saves cards, groups or selections with relative
positions, internal pipes and outside pipes by name, detects {$name}
parameters and inserts with fresh ids. TemplateLibrary keeps user templates
in user://templates and lists the built-in ones in templates/.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 17:01:34 -06:00
|
|
|
|
group_list.append(gd)
|
|
|
|
|
|
var tpl := {
|
|
|
|
|
|
"format_version": FORMAT_VERSION,
|
|
|
|
|
|
"name": tname,
|
|
|
|
|
|
"description": description,
|
|
|
|
|
|
"kind": kind if KINDS.has(kind) else "selection",
|
|
|
|
|
|
"params": [],
|
|
|
|
|
|
"tables": tables,
|
|
|
|
|
|
"groups": group_list,
|
|
|
|
|
|
}
|
|
|
|
|
|
var params: Array = []
|
|
|
|
|
|
for n in detect_params(tpl):
|
|
|
|
|
|
params.append({"name": n, "label": n.capitalize(), "default": ""})
|
|
|
|
|
|
tpl["params"] = params
|
|
|
|
|
|
return tpl
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Every {$name} placeholder in a template's tables and groups, in the order
|
|
|
|
|
|
## they first appear.
|
|
|
|
|
|
static func detect_params(tpl: Dictionary) -> Array[String]:
|
|
|
|
|
|
var found: Array[String] = []
|
|
|
|
|
|
var re := RegEx.create_from_string(_PARAM_RE)
|
|
|
|
|
|
var stack: Array = [tpl.get("tables", []), tpl.get("groups", [])]
|
|
|
|
|
|
var strings: Array[String] = []
|
|
|
|
|
|
_collect_strings(stack, strings)
|
|
|
|
|
|
for s in strings:
|
|
|
|
|
|
for m in re.search_all(s):
|
|
|
|
|
|
var n := m.get_string(1)
|
|
|
|
|
|
if not found.has(n):
|
|
|
|
|
|
found.append(n)
|
|
|
|
|
|
return found
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Every String inside nested Arrays and Dictionaries (values only), in order.
|
|
|
|
|
|
static func _collect_strings(v: Variant, out: Array[String]) -> void:
|
|
|
|
|
|
# Iterative, so a deep template can't overflow the stack.
|
|
|
|
|
|
var stack: Array = [v]
|
|
|
|
|
|
while not stack.is_empty():
|
|
|
|
|
|
var x: Variant = stack.pop_back()
|
|
|
|
|
|
if x is String:
|
|
|
|
|
|
out.append(x)
|
|
|
|
|
|
elif x is Array:
|
|
|
|
|
|
for i in range((x as Array).size() - 1, -1, -1):
|
|
|
|
|
|
stack.append(x[i])
|
|
|
|
|
|
elif x is Dictionary:
|
|
|
|
|
|
var keys: Array = (x as Dictionary).keys()
|
|
|
|
|
|
for i in range(keys.size() - 1, -1, -1):
|
|
|
|
|
|
stack.append(x[keys[i]])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Replaces {$name} placeholders in every String of `v` (a deep copy is
|
|
|
|
|
|
## returned). Names missing from `values` are left as typed.
|
|
|
|
|
|
static func substitute(v: Variant, values: Dictionary) -> Variant:
|
|
|
|
|
|
if v is String:
|
|
|
|
|
|
return _sub_text(v, values)
|
|
|
|
|
|
if v is Array:
|
|
|
|
|
|
var a: Array = []
|
|
|
|
|
|
for x: Variant in v:
|
|
|
|
|
|
a.append(substitute(x, values))
|
|
|
|
|
|
return a
|
|
|
|
|
|
if v is Dictionary:
|
|
|
|
|
|
var d := {}
|
|
|
|
|
|
for k: Variant in v:
|
|
|
|
|
|
d[k] = substitute(v[k], values)
|
|
|
|
|
|
return d
|
|
|
|
|
|
return v
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static func _sub_text(s: String, values: Dictionary) -> String:
|
|
|
|
|
|
if not s.contains("{$"):
|
|
|
|
|
|
return s
|
|
|
|
|
|
var re := RegEx.create_from_string(_PARAM_RE)
|
|
|
|
|
|
var out := ""
|
|
|
|
|
|
var at := 0
|
|
|
|
|
|
for m in re.search_all(s):
|
|
|
|
|
|
out += s.substr(at, m.get_start() - at)
|
|
|
|
|
|
var n := m.get_string(1)
|
|
|
|
|
|
out += str(values[n]) if values.has(n) else m.get_string()
|
|
|
|
|
|
at = m.get_end()
|
|
|
|
|
|
return out + s.substr(at)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Text for checking an expression, name or effect that may hold
|
|
|
|
|
|
## placeholders: every {$name} reads as the plain name `p`, so `@{$skill}`,
|
|
|
|
|
|
## `2d6 + {$dm}` and a variable name `{$name}` all check as well formed.
|
|
|
|
|
|
static func probe(text: String) -> String:
|
|
|
|
|
|
if not text.contains("{$"):
|
|
|
|
|
|
return text
|
|
|
|
|
|
return RegEx.create_from_string(_PARAM_RE).sub(text, "p", true)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Parameter values with each param's default filled in where `given` lacks
|
|
|
|
|
|
## one. A param with neither stays as typed ({$name}), so it shows.
|
|
|
|
|
|
static func values_with_defaults(tpl: Dictionary, given: Dictionary = {}) -> Dictionary:
|
|
|
|
|
|
var out := {}
|
|
|
|
|
|
for p: Variant in tpl.get("params", []):
|
|
|
|
|
|
if p is Dictionary:
|
|
|
|
|
|
var n := str(p.get("name", ""))
|
|
|
|
|
|
if n != "" and (given.has(n) or str(p.get("default", "")) != ""):
|
|
|
|
|
|
out[n] = str(given.get(n, p.get("default", "")))
|
|
|
|
|
|
for k: Variant in given:
|
|
|
|
|
|
if not out.has(k):
|
|
|
|
|
|
out[k] = str(given[k])
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------- insert
|
|
|
|
|
|
|
|
|
|
|
|
## The template's size on the board: its group frames and cards (roughly).
|
|
|
|
|
|
static func extent(tpl: Dictionary) -> Vector2:
|
|
|
|
|
|
var e := Vector2.ZERO
|
|
|
|
|
|
for t: Dictionary in tpl.get("tables", []):
|
|
|
|
|
|
e = e.max(Vector2(float(t.get("x", 0)), float(t.get("y", 0))) + CARD_SIZE)
|
|
|
|
|
|
for g: Dictionary in tpl.get("groups", []):
|
|
|
|
|
|
e = e.max(Vector2(float(g.get("x", 0)) + float(g.get("w", 0)), float(g.get("y", 0)) + float(g.get("h", 0))))
|
|
|
|
|
|
return e
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Makes the template's tables and groups for `board`, without adding them:
|
|
|
|
|
|
## parameters substituted (defaults for missing values), fresh table, entry
|
|
|
|
|
|
## and group ids, positions offset by `origin`. Pipes to outside tables relink
|
|
|
|
|
|
## to a table of that name on `board`, or are dropped.
|
|
|
|
|
|
## Returns {"tables": Array[RollTable], "groups": Array[TableGroup],
|
|
|
|
|
|
## "relinked": Array[String], "dropped": Array[String]} (the last two describe
|
|
|
|
|
|
## each outside pipe, "Entry → Table").
|
|
|
|
|
|
static func instantiate(tpl: Dictionary, values: Dictionary, board: TowerBoard, origin: Vector2) -> Dictionary:
|
|
|
|
|
|
var vals := values_with_defaults(tpl, values)
|
|
|
|
|
|
var src: Dictionary = substitute({"tables": tpl.get("tables", []), "groups": tpl.get("groups", [])}, vals)
|
|
|
|
|
|
var id_map := {}
|
|
|
|
|
|
for td: Variant in src["tables"]:
|
|
|
|
|
|
if td is Dictionary:
|
|
|
|
|
|
id_map[str(td.get("id", ""))] = TowerBoard.new_id("t")
|
|
|
|
|
|
var by_name := {}
|
|
|
|
|
|
for t in board.tables:
|
|
|
|
|
|
var key := Formatter.strip_tags(t.name).strip_edges().to_lower()
|
|
|
|
|
|
if not by_name.has(key):
|
|
|
|
|
|
by_name[key] = t.id
|
|
|
|
|
|
var tables: Array[RollTable] = []
|
|
|
|
|
|
var relinked: Array[String] = []
|
|
|
|
|
|
var dropped: Array[String] = []
|
|
|
|
|
|
for td: Variant in src["tables"]:
|
|
|
|
|
|
if not (td is Dictionary):
|
|
|
|
|
|
continue
|
|
|
|
|
|
var d: Dictionary = (td as Dictionary).duplicate(true)
|
|
|
|
|
|
d["id"] = id_map[str(d.get("id", ""))]
|
|
|
|
|
|
d["x"] = roundi(float(d.get("x", 0)) + origin.x)
|
|
|
|
|
|
d["y"] = roundi(float(d.get("y", 0)) + origin.y)
|
|
|
|
|
|
var entries: Array = []
|
|
|
|
|
|
for ev: Variant in d.get("entries", []):
|
|
|
|
|
|
if not (ev is Dictionary):
|
|
|
|
|
|
continue
|
|
|
|
|
|
var ed: Dictionary = ev
|
|
|
|
|
|
ed["id"] = TowerBoard.new_id("e")
|
|
|
|
|
|
var pipes: Array = []
|
|
|
|
|
|
for p: Variant in ed.get("pipes", []):
|
|
|
|
|
|
var info := JsonIO.pipe_info(p)
|
|
|
|
|
|
var target: String = info["table"]
|
|
|
|
|
|
var outside: bool = p is Dictionary and bool((p as Dictionary).get("outside", false))
|
|
|
|
|
|
var what := "%s → %s" % [Formatter.strip_tags(str(ed.get("text", ""))), Formatter.strip_tags(target)]
|
|
|
|
|
|
var to := ""
|
|
|
|
|
|
if not outside and id_map.has(target):
|
|
|
|
|
|
to = id_map[target]
|
|
|
|
|
|
elif by_name.has(Formatter.strip_tags(target).strip_edges().to_lower()):
|
|
|
|
|
|
to = by_name[Formatter.strip_tags(target).strip_edges().to_lower()]
|
|
|
|
|
|
relinked.append(what)
|
|
|
|
|
|
else:
|
|
|
|
|
|
dropped.append(what)
|
|
|
|
|
|
continue
|
|
|
|
|
|
var np := {"table": to}
|
|
|
|
|
|
if p is Dictionary:
|
Plain Build engine: Describe, Builder, checks, inputs, collections, why trace
Implements docs/PLAIN.md sections 1-5 (the engine half of the usability pass):
- core/describe.gd (Describe): rules in plain words, labelled from variable
cards, collections and card inputs, falling back to the original text.
- core/builder.gd (Builder): effect and condition models that round-trip
with the rule text, a rule tokenizer, and known_values(board).
- Collections: variable cards with var_type "collection" label their
members and don't declare their stem.
- Check cards (kind "check"): dice + modifier vs target, Success/Failure,
chain values nat/total/effect, node "check" info, lint and static checks.
- Card inputs and pipe "with" values, scoped over the pipe's subtree on the
explicit work stack (OP_RESTORE frames), with defaults and snapshot
evaluation.
- nat on every roll-table roll; dice faces recorded through ctx.faces.
- Why trace: facts on nodes and steps (input, roll, check, case, show_if,
options_where, effect, when, otherwise).
- StateStore rules_view setting; JSON for the new keys; LOGIC.md and
PLAIN.md updated (deviations in PLAIN.md 5a).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 21:01:24 -06:00
|
|
|
|
for k in ["count", "save_as", "with"]:
|
Group cards and templates
Groups: an optional groups list on a collection (frame rect, collapsed,
members, details), saved by TowerBoard and kept by JsonIO. Expanded groups
draw a frame behind their cards with a title bar; collapsed ones become one
compact card that lists the members' lit results, with pipes re-routed to
its inlet and right edge. Build mode adds multi-select (Ctrl/Shift-click,
Ctrl-drag), Group (Ctrl+G), drop in/out of frames, rename, ungroup, delete
with undo and a details editor.
Templates: TemplateKit saves cards, groups or selections with relative
positions, internal pipes and outside pipes by name, detects {$name}
parameters and inserts with fresh ids. TemplateLibrary keeps user templates
in user://templates and lists the built-in ones in templates/.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 17:01:34 -06:00
|
|
|
|
if (p as Dictionary).has(k):
|
|
|
|
|
|
np[k] = p[k]
|
|
|
|
|
|
pipes.append(np)
|
|
|
|
|
|
ed["pipes"] = pipes
|
|
|
|
|
|
entries.append(ed)
|
|
|
|
|
|
d["entries"] = entries
|
|
|
|
|
|
tables.append(RollTable.from_dict(d))
|
|
|
|
|
|
var groups: Array[TableGroup] = []
|
|
|
|
|
|
for gv: Variant in src["groups"]:
|
|
|
|
|
|
if not (gv is Dictionary):
|
|
|
|
|
|
continue
|
|
|
|
|
|
var g := TableGroup.from_dict(gv)
|
|
|
|
|
|
g.id = TowerBoard.new_id("g")
|
|
|
|
|
|
g.position += origin
|
|
|
|
|
|
var members: Array[String] = []
|
|
|
|
|
|
for m in g.members:
|
|
|
|
|
|
if id_map.has(m):
|
|
|
|
|
|
members.append(id_map[m])
|
|
|
|
|
|
g.members = members
|
2026-11-10 18:47:05 -06:00
|
|
|
|
g.parent = ""
|
Group cards and templates
Groups: an optional groups list on a collection (frame rect, collapsed,
members, details), saved by TowerBoard and kept by JsonIO. Expanded groups
draw a frame behind their cards with a title bar; collapsed ones become one
compact card that lists the members' lit results, with pipes re-routed to
its inlet and right edge. Build mode adds multi-select (Ctrl/Shift-click,
Ctrl-drag), Group (Ctrl+G), drop in/out of frames, rename, ungroup, delete
with undo and a details editor.
Templates: TemplateKit saves cards, groups or selections with relative
positions, internal pipes and outside pipes by name, detects {$name}
parameters and inserts with fresh ids. TemplateLibrary keeps user templates
in user://templates and lists the built-in ones in templates/.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 17:01:34 -06:00
|
|
|
|
groups.append(g)
|
2026-11-10 18:47:05 -06:00
|
|
|
|
var gsrc: Array = src["groups"].filter(func(v: Variant) -> bool: return v is Dictionary)
|
|
|
|
|
|
for i in mini(gsrc.size(), groups.size()):
|
|
|
|
|
|
var at := int((gsrc[i] as Dictionary).get("parent_at", -1))
|
|
|
|
|
|
if at >= 0 and at < groups.size() and at != i:
|
|
|
|
|
|
groups[i].parent = groups[at].id
|
Group cards and templates
Groups: an optional groups list on a collection (frame rect, collapsed,
members, details), saved by TowerBoard and kept by JsonIO. Expanded groups
draw a frame behind their cards with a title bar; collapsed ones become one
compact card that lists the members' lit results, with pipes re-routed to
its inlet and right edge. Build mode adds multi-select (Ctrl/Shift-click,
Ctrl-drag), Group (Ctrl+G), drop in/out of frames, rename, ungroup, delete
with undo and a details editor.
Templates: TemplateKit saves cards, groups or selections with relative
positions, internal pipes and outside pipes by name, detects {$name}
parameters and inserts with fresh ids. TemplateLibrary keeps user templates
in user://templates and lists the built-in ones in templates/.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 17:01:34 -06:00
|
|
|
|
return {"tables": tables, "groups": groups, "relinked": relinked, "dropped": dropped}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Adds an instantiated template (see instantiate) to the board. Returns the
|
|
|
|
|
|
## same result.
|
|
|
|
|
|
static func insert(tpl: Dictionary, values: Dictionary, board: TowerBoard, origin: Vector2) -> Dictionary:
|
|
|
|
|
|
var r := instantiate(tpl, values, board, origin)
|
|
|
|
|
|
for t: RollTable in r["tables"]:
|
|
|
|
|
|
board.tables.append(t)
|
|
|
|
|
|
for g: TableGroup in r["groups"]:
|
|
|
|
|
|
board.groups.append(g)
|
|
|
|
|
|
board.clean_groups()
|
|
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------- checks
|
|
|
|
|
|
|
|
|
|
|
|
## Checks a parsed template file. Returns {"ok", "template", "errors"}; the
|
|
|
|
|
|
## template has every key, with params cleaned up and placeholders found in
|
|
|
|
|
|
## the text but not listed added.
|
|
|
|
|
|
static func validate(data: Variant) -> Dictionary:
|
|
|
|
|
|
var errors: Array[String] = []
|
|
|
|
|
|
if not (data is Dictionary):
|
|
|
|
|
|
errors.append("The file is not a template (expected a JSON object).")
|
|
|
|
|
|
return {"ok": false, "template": {}, "errors": errors}
|
|
|
|
|
|
var d: Dictionary = data
|
|
|
|
|
|
var fv: Variant = JsonIO._num(d.get("format_version", 1), 1)
|
|
|
|
|
|
if (fv is int or fv is float) and fv > FORMAT_VERSION:
|
|
|
|
|
|
errors.append("format_version %s is newer than this version supports (%d)." % [str(fv), FORMAT_VERSION])
|
|
|
|
|
|
if not (d.get("tables") is Array) or (d["tables"] as Array).is_empty():
|
|
|
|
|
|
errors.append("The template has no \"tables\" list.")
|
|
|
|
|
|
else:
|
|
|
|
|
|
for i in (d["tables"] as Array).size():
|
|
|
|
|
|
var t: Variant = d["tables"][i]
|
|
|
|
|
|
if not (t is Dictionary) or not (t.get("entries", []) is Array):
|
|
|
|
|
|
errors.append("Table #%d is not a table." % (i + 1))
|
|
|
|
|
|
if not errors.is_empty():
|
|
|
|
|
|
return {"ok": false, "template": {}, "errors": errors}
|
|
|
|
|
|
var groups: Array = []
|
|
|
|
|
|
if d.get("groups") is Array:
|
|
|
|
|
|
for g: Variant in d["groups"]:
|
|
|
|
|
|
if g is Dictionary:
|
|
|
|
|
|
groups.append(g)
|
|
|
|
|
|
var tpl := {
|
|
|
|
|
|
"format_version": FORMAT_VERSION,
|
|
|
|
|
|
"name": str(d.get("name", "")).strip_edges(),
|
|
|
|
|
|
"description": str(d.get("description", "")),
|
|
|
|
|
|
"kind": str(d.get("kind", "selection")) if KINDS.has(str(d.get("kind", ""))) else "selection",
|
|
|
|
|
|
"params": [],
|
|
|
|
|
|
"tables": d["tables"],
|
|
|
|
|
|
"groups": groups,
|
|
|
|
|
|
}
|
|
|
|
|
|
if tpl["name"] == "":
|
|
|
|
|
|
tpl["name"] = "Untitled template"
|
|
|
|
|
|
var params: Array = []
|
|
|
|
|
|
var seen := {}
|
|
|
|
|
|
if d.get("params") is Array:
|
|
|
|
|
|
for p: Variant in d["params"]:
|
|
|
|
|
|
if p is Dictionary and str(p.get("name", "")) != "" and not seen.has(str(p["name"])):
|
|
|
|
|
|
seen[str(p["name"])] = true
|
|
|
|
|
|
params.append({"name": str(p["name"]), "label": str(p.get("label", str(p["name"]).capitalize())),
|
|
|
|
|
|
"default": str(p.get("default", ""))})
|
|
|
|
|
|
for n in detect_params(tpl):
|
|
|
|
|
|
if not seen.has(n):
|
|
|
|
|
|
params.append({"name": n, "label": n.capitalize(), "default": ""})
|
|
|
|
|
|
tpl["params"] = params
|
|
|
|
|
|
return {"ok": true, "template": tpl, "errors": errors}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## "3 cards: roll, choice ×2 · 1 group" for the template list.
|
|
|
|
|
|
static func summary(tpl: Dictionary) -> String:
|
|
|
|
|
|
var kinds := {}
|
|
|
|
|
|
var order: Array[String] = []
|
|
|
|
|
|
var n := 0
|
|
|
|
|
|
for t: Variant in tpl.get("tables", []):
|
|
|
|
|
|
if not (t is Dictionary):
|
|
|
|
|
|
continue
|
|
|
|
|
|
n += 1
|
|
|
|
|
|
var k := str(t.get("kind", "roll"))
|
|
|
|
|
|
if not kinds.has(k):
|
|
|
|
|
|
kinds[k] = 0
|
|
|
|
|
|
order.append(k)
|
|
|
|
|
|
kinds[k] += 1
|
|
|
|
|
|
var parts := PackedStringArray()
|
|
|
|
|
|
for k in order:
|
|
|
|
|
|
parts.append(k if kinds[k] == 1 else "%s ×%d" % [k, kinds[k]])
|
|
|
|
|
|
var s := "%d %s: %s" % [n, "card" if n == 1 else "cards", ", ".join(parts)]
|
|
|
|
|
|
var g := (tpl.get("groups", []) as Array).size()
|
|
|
|
|
|
if g > 0:
|
|
|
|
|
|
s += " · %d %s" % [g, "group" if g == 1 else "groups"]
|
|
|
|
|
|
return s
|