2026-11-09 08:06:56 -06:00
|
|
|
class_name TableSettingsRules
|
|
|
|
|
extends RefCounted
|
|
|
|
|
## The editing rules behind TableSettingsDialog: checking a settings
|
|
|
|
|
## Dictionary and applying it to a table. Static, so they can be tested
|
|
|
|
|
## without a window.
|
|
|
|
|
|
|
|
|
|
const SAVE_PATTERN := "^@?[A-Za-z_][A-Za-z0-9_]*$"
|
|
|
|
|
const VAR_PATTERN := "^[a-z0-9_]+$"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Problems with the kind-specific settings (empty when fine).
|
|
|
|
|
static func kind_problems(t: RollTable, s: Dictionary) -> PackedStringArray:
|
|
|
|
|
var out := PackedStringArray()
|
|
|
|
|
match t.kind:
|
|
|
|
|
RollTable.KIND_CHOICE:
|
|
|
|
|
# Template placeholders ({$name}) check as a plain name (TemplateKit.probe).
|
|
|
|
|
var save := TemplateKit.probe(str(s.get("save_as", t.save_as)).strip_edges())
|
|
|
|
|
if save != "" and _re(SAVE_PATTERN).search(save) == null:
|
|
|
|
|
out.append("Save as: use a name like career, or @career to keep it.")
|
|
|
|
|
if str(s.get("choice", t.choice)) == "pick":
|
|
|
|
|
if str(s.get("options_mode", "vars" if t.options_from != "" else "fixed")) == "vars":
|
|
|
|
|
var ferr := options_from_error(str(s.get("options_from", t.options_from)))
|
|
|
|
|
if ferr != "":
|
|
|
|
|
out.append("Options from: " + ferr)
|
|
|
|
|
var werr := when_error(str(s.get("options_where", t.options_where)))
|
|
|
|
|
if werr != "":
|
|
|
|
|
out.append("Only where: " + werr)
|
|
|
|
|
var si: Dictionary = s.get("show_if", {})
|
|
|
|
|
for eid: String in si:
|
|
|
|
|
var serr := when_error(str(si[eid]))
|
|
|
|
|
if serr != "":
|
|
|
|
|
var e := t.find_entry(eid)
|
|
|
|
|
var at := t.entries.find(e) if e != null else -1
|
|
|
|
|
out.append("Show only if (%s): %s" % [("option %d" % (at + 1)) if at >= 0 else eid, serr])
|
|
|
|
|
if str(s.get("choice", t.choice)) == "number":
|
|
|
|
|
for k in [["min", "Lowest"], ["max", "Highest"]]:
|
|
|
|
|
var src := TemplateKit.probe(str(s.get(k[0], "")).strip_edges())
|
|
|
|
|
var err := Expr.check(src) if src != "" else "it's empty"
|
|
|
|
|
if err != "":
|
|
|
|
|
out.append("%s: %s" % [k[1], err])
|
|
|
|
|
RollTable.KIND_LOOP:
|
|
|
|
|
var src := TemplateKit.probe(str(s.get("loop_expr", t.loop_expr)).strip_edges())
|
|
|
|
|
if src == "":
|
2026-11-10 18:24:23 -06:00
|
|
|
out.append("The repeat needs a count or a condition.")
|
2026-11-09 08:06:56 -06:00
|
|
|
elif Expr.check(src) != "":
|
2026-11-10 18:24:23 -06:00
|
|
|
out.append("Repeat rule: " + Expr.check(src))
|
2026-11-09 08:06:56 -06:00
|
|
|
RollTable.KIND_ROUTER:
|
|
|
|
|
var cases: Array = s.get("cases", [])
|
|
|
|
|
for i in cases.size():
|
|
|
|
|
var err := when_error(str(cases[i]))
|
|
|
|
|
if err != "":
|
|
|
|
|
out.append("Case %d: %s" % [i + 1, err])
|
|
|
|
|
RollTable.KIND_VARIABLE:
|
|
|
|
|
var n := TemplateKit.probe(str(s.get("var_name", t.var_name)).strip_edges().trim_prefix("@"))
|
|
|
|
|
if _re(VAR_PATTERN).search(n) == null:
|
|
|
|
|
out.append("Variable name: lowercase letters, digits and _ only.")
|
|
|
|
|
var err := _initial_error(str(s.get("initial", t.initial)), str(s.get("var_type", t.var_type)))
|
|
|
|
|
if err != "":
|
|
|
|
|
out.append("Starting value: " + err)
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## "" when a router case condition is usable (empty is Otherwise), else why not.
|
|
|
|
|
static func when_error(src: String) -> String:
|
|
|
|
|
src = TemplateKit.probe(src.strip_edges())
|
|
|
|
|
return Expr.check(src) if src != "" else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## "" when an options_from pattern is usable, else why not ("" is refused).
|
|
|
|
|
static func options_from_error(src: String) -> String:
|
|
|
|
|
src = TemplateKit.probe(src.strip_edges())
|
|
|
|
|
if src == "":
|
|
|
|
|
return "write @prefix_* or @list"
|
|
|
|
|
return str(RollTable.parse_options_from(src)["error"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## A hint about the order of router cases (an Otherwise that isn't last), or "".
|
|
|
|
|
static func router_hint(cases: Array) -> String:
|
|
|
|
|
for i in cases.size():
|
|
|
|
|
if str(cases[i]).strip_edges() == "" and i < cases.size() - 1:
|
|
|
|
|
return "Case %d is Otherwise, so the cases after it can never be chosen. Move it to the end." % (i + 1)
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static func _initial_error(src: String, type: String) -> String:
|
|
|
|
|
src = TemplateKit.probe(src.strip_edges())
|
|
|
|
|
if src == "":
|
|
|
|
|
return ""
|
|
|
|
|
if type != "number" and RollTable._looks_literal(src):
|
|
|
|
|
return ""
|
|
|
|
|
return Expr.check(src)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static func _re(pattern: String) -> RegEx:
|
|
|
|
|
return RegEx.create_from_string(pattern)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Dice text for a ranged table: plain dice (2d6) or an expression that uses a
|
|
|
|
|
## variable, dice or a function ("2d6 + dm(@end)", "@terms"). A lone word
|
|
|
|
|
## such as "banana" is not accepted.
|
|
|
|
|
static func dice_ok_text(text: String) -> bool:
|
|
|
|
|
var s := TemplateKit.probe(text.strip_edges())
|
|
|
|
|
if Dice.is_valid_text(s):
|
|
|
|
|
return true
|
|
|
|
|
if s == "" or Expr.check(s) != "":
|
|
|
|
|
return false
|
|
|
|
|
return _re("^[A-Za-z_][A-Za-z0-9_]*$").search(s) == null
|
|
|
|
|
|
|
|
|
|
|
2026-11-09 10:01:10 -06:00
|
|
|
## The problem with a roll modifier's text, or "" when it's fine (or empty).
|
|
|
|
|
static func mod_error(mod: String) -> String:
|
|
|
|
|
if mod.strip_edges() == "":
|
|
|
|
|
return ""
|
|
|
|
|
var err := Expr.check(TemplateKit.probe(mod.strip_edges()))
|
|
|
|
|
return "The modifier “%s” doesn't work: %s." % [mod.strip_edges(), err] if err != "" else ""
|
|
|
|
|
|
|
|
|
|
|
2026-11-09 08:06:56 -06:00
|
|
|
## Applies the kind-specific settings. Turning a pick choice into a number or
|
|
|
|
|
## text one keeps only its first entry (as the Answer); the other way, the
|
|
|
|
|
## Answer becomes the first of three options.
|
|
|
|
|
static func _apply_kind(t: RollTable, s: Dictionary) -> void:
|
|
|
|
|
match t.kind:
|
|
|
|
|
RollTable.KIND_CHOICE:
|
|
|
|
|
var c := str(s.get("choice", t.choice))
|
|
|
|
|
if RollTable.CHOICES.has(c) and c != t.choice:
|
|
|
|
|
if c == "pick":
|
|
|
|
|
if not t.entries.is_empty():
|
|
|
|
|
t.entries[0].text = "Option 1"
|
|
|
|
|
for n in range(t.entries.size() + 1, 4):
|
|
|
|
|
t.entries.append(TableEntry.new(TowerBoard.new_id("e"), "Option %d" % n))
|
|
|
|
|
else:
|
|
|
|
|
while t.entries.size() > 1:
|
|
|
|
|
t.entries.remove_at(t.entries.size() - 1)
|
|
|
|
|
if t.entries.is_empty():
|
|
|
|
|
t.entries.append(TableEntry.new(TowerBoard.new_id("e"), "Answer"))
|
|
|
|
|
t.entries[0].text = "Answer"
|
|
|
|
|
t.entries[0].has_range = false
|
|
|
|
|
t.card_mode = false
|
|
|
|
|
t.choice = c
|
|
|
|
|
t.prompt = str(s.get("prompt", t.prompt))
|
|
|
|
|
t.save_as = str(s.get("save_as", t.save_as)).strip_edges()
|
|
|
|
|
var si: Dictionary = s.get("show_if", {})
|
|
|
|
|
for eid: String in si:
|
|
|
|
|
var se := t.find_entry(eid)
|
|
|
|
|
if se != null:
|
|
|
|
|
se.show_if = str(si[eid]).strip_edges()
|
|
|
|
|
if t.choice != "pick":
|
|
|
|
|
t.options_from = ""
|
|
|
|
|
t.options_where = ""
|
|
|
|
|
elif s.has("options_mode"):
|
|
|
|
|
_apply_options(t, str(s["options_mode"]) == "vars", str(s.get("options_from", "")), str(s.get("options_where", "")))
|
|
|
|
|
if t.choice == "number":
|
|
|
|
|
t.min_expr = str(s.get("min", t.min_expr)).strip_edges()
|
|
|
|
|
t.max_expr = str(s.get("max", t.max_expr)).strip_edges()
|
|
|
|
|
RollTable.KIND_LOOP:
|
|
|
|
|
var m := str(s.get("loop_mode", t.loop_mode))
|
|
|
|
|
if RollTable.LOOPS.has(m):
|
|
|
|
|
t.loop_mode = m
|
|
|
|
|
t.loop_expr = str(s.get("loop_expr", t.loop_expr)).strip_edges()
|
|
|
|
|
RollTable.KIND_VARIABLE:
|
|
|
|
|
t.var_name = str(s.get("var_name", t.var_name)).strip_edges().trim_prefix("@")
|
|
|
|
|
var vt := str(s.get("var_type", t.var_type))
|
|
|
|
|
if RollTable.VAR_TYPES.has(vt):
|
|
|
|
|
t.var_type = vt
|
|
|
|
|
t.initial = str(s.get("initial", t.initial)).strip_edges()
|
|
|
|
|
RollTable.KIND_ROUTER:
|
|
|
|
|
var cases: Array = s.get("cases", [])
|
|
|
|
|
for i in mini(cases.size(), t.entries.size()):
|
|
|
|
|
t.entries[i].when = str(cases[i]).strip_edges()
|
|
|
|
|
RollTable.KIND_SHEET:
|
|
|
|
|
if s.has("template"):
|
|
|
|
|
if t.entries.is_empty():
|
|
|
|
|
t.entries.append(TableEntry.new(TowerBoard.new_id("e"), ""))
|
|
|
|
|
t.entries[0].text = str(s["template"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Switches a pick card between a fixed list and options from variables.
|
|
|
|
|
## Turning variables on adds a Chosen entry first (untouched default
|
|
|
|
|
## "Option N" entries go; anything else stays as a fixed extra). Turning them
|
|
|
|
|
## off makes the Chosen entry an ordinary option, or drops it if it does
|
|
|
|
|
## nothing (no effects, pipes, details or text of its own).
|
|
|
|
|
static func _apply_options(t: RollTable, on: bool, from: String, where: String) -> void:
|
|
|
|
|
if on:
|
|
|
|
|
if not t.is_dynamic():
|
|
|
|
|
var keep: Array[TableEntry] = []
|
|
|
|
|
for e in t.entries:
|
|
|
|
|
var blank := e.effects.strip_edges() == "" and e.pipes.is_empty() and e.details.strip_edges() == "" \
|
|
|
|
|
and e.show_if.strip_edges() == "" and RegEx.create_from_string("^Option \\d+$").search(e.text.strip_edges()) != null
|
|
|
|
|
if not blank:
|
|
|
|
|
keep.append(e)
|
|
|
|
|
t.entries = keep
|
|
|
|
|
t.entries.insert(0, TableEntry.new(TowerBoard.new_id("e"), Roller.CHOSEN))
|
|
|
|
|
t.options_from = from.strip_edges()
|
|
|
|
|
t.options_where = where.strip_edges()
|
|
|
|
|
return
|
|
|
|
|
if t.is_dynamic() and not t.entries.is_empty():
|
|
|
|
|
var c := t.entries[0]
|
|
|
|
|
var txt := Formatter.strip_tags(c.text).strip_edges()
|
|
|
|
|
if c.effects.strip_edges() == "" and c.pipes.is_empty() and c.details.strip_edges() == "" and (txt == "" or txt == Roller.CHOSEN):
|
|
|
|
|
t.entries.remove_at(0)
|
|
|
|
|
t.options_from = ""
|
|
|
|
|
t.options_where = ""
|
|
|
|
|
if t.entries.is_empty():
|
|
|
|
|
t.entries.append(TableEntry.new(TowerBoard.new_id("e"), "Option 1"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------- Pure editing logic ----------
|
|
|
|
|
|
|
|
|
|
## Wraps text[sel.x:sel.y] in open/close_tag. With an empty range, inserts the
|
|
|
|
|
## pair. Returns {text, caret}: the caret sits inside an inserted pair, or
|
|
|
|
|
## after the closing tag of a wrapped selection.
|
|
|
|
|
static func wrap_text(text: String, sel: Vector2i, open: String, close_tag: String) -> Dictionary:
|
|
|
|
|
var a := clampi(mini(sel.x, sel.y), 0, text.length())
|
|
|
|
|
var b := clampi(maxi(sel.x, sel.y), 0, text.length())
|
|
|
|
|
var out := text.substr(0, a) + open + text.substr(a, b - a) + close_tag + text.substr(b)
|
|
|
|
|
var caret := a + open.length() if a == b else b + open.length() + close_tag.length()
|
|
|
|
|
return {"text": out, "caret": caret}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## "a, b ,, a" -> ["a", "b"].
|
|
|
|
|
static func parse_tags(text: String) -> Array[String]:
|
|
|
|
|
var out: Array[String] = []
|
|
|
|
|
for part in text.split(","):
|
|
|
|
|
var t := part.strip_edges()
|
|
|
|
|
if t != "" and not out.has(t):
|
|
|
|
|
out.append(t)
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Number of pipes beyond the first, summed over entries (lost when going ranged).
|
|
|
|
|
static func extra_pipe_count(t: RollTable) -> int:
|
|
|
|
|
var n := 0
|
|
|
|
|
for e in t.entries:
|
|
|
|
|
n += maxi(0, e.pipes.size() - 1)
|
|
|
|
|
return n
|
|
|
|
|
|
|
|
|
|
|
2026-11-09 10:01:10 -06:00
|
|
|
## Applies a settings Dictionary (name, details, tags, ranged, dice, dice_mod,
|
|
|
|
|
## card_mode, deck_empty) to the table. Returns {ok, error, deck_reset}: on a bad dice
|
2026-11-09 08:06:56 -06:00
|
|
|
## expression nothing is changed. deck_reset says the card deck no longer fits
|
|
|
|
|
## (type, dice or card mode changed), so a saved deck should be cleared.
|
|
|
|
|
static func apply_settings(t: RollTable, s: Dictionary) -> Dictionary:
|
|
|
|
|
var problems := kind_problems(t, s)
|
|
|
|
|
if not problems.is_empty():
|
|
|
|
|
return {"ok": false, "error": "\n".join(problems), "deck_reset": false}
|
|
|
|
|
var ranged: bool = s.get("ranged", false)
|
|
|
|
|
# Only roll tables and pick choices can be ranged or dealt as cards.
|
|
|
|
|
var choice := str(s.get("choice", t.choice))
|
|
|
|
|
var rolls := t.kind == RollTable.KIND_ROLL or (t.kind == RollTable.KIND_CHOICE and choice == "pick")
|
|
|
|
|
if not rolls:
|
|
|
|
|
ranged = false
|
|
|
|
|
var dice := str(s.get("dice", t.dice)).strip_edges()
|
|
|
|
|
if ranged and not dice_ok_text(dice):
|
|
|
|
|
return {"ok": false, "error": "“%s” is not a valid dice expression." % dice, "deck_reset": false}
|
2026-11-09 10:01:10 -06:00
|
|
|
var mod := str(s.get("dice_mod", t.dice_mod)).strip_edges()
|
|
|
|
|
var mod_err := mod_error(mod)
|
|
|
|
|
if ranged and mod_err != "":
|
|
|
|
|
return {"ok": false, "error": mod_err, "deck_reset": false}
|
2026-11-09 08:06:56 -06:00
|
|
|
var was_ranged := t.is_ranged()
|
|
|
|
|
var old_dice := t.dice
|
|
|
|
|
var old_card := t.card_mode
|
|
|
|
|
var new_name := str(s.get("name", t.name)).strip_edges()
|
|
|
|
|
if new_name != "":
|
|
|
|
|
t.name = new_name
|
|
|
|
|
t.details = str(s.get("details", t.details))
|
|
|
|
|
t.tags = parse_tags(str(s.get("tags", "")))
|
|
|
|
|
t.card_mode = bool(s.get("card_mode", t.card_mode)) and rolls
|
|
|
|
|
t.deck_empty = "stop" if str(s.get("deck_empty", t.deck_empty)) == "stop" else "reshuffle"
|
|
|
|
|
if ranged:
|
|
|
|
|
t.dice = dice
|
2026-11-09 10:01:10 -06:00
|
|
|
t.dice_mod = mod
|
2026-11-09 08:06:56 -06:00
|
|
|
if not was_ranged:
|
|
|
|
|
assign_ranges(t)
|
|
|
|
|
elif was_ranged:
|
|
|
|
|
drop_ranges(t)
|
|
|
|
|
_apply_kind(t, s)
|
|
|
|
|
var deck_reset := was_ranged != ranged or old_card != t.card_mode or (ranged and old_dice != t.dice)
|
|
|
|
|
return {"ok": true, "error": "", "deck_reset": deck_reset}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Gives entries ranges on the table's dice, sized by weight (at least one
|
|
|
|
|
## value each while there is room). Entries of weight 0 get no range. Each
|
|
|
|
|
## entry keeps only its first pipe, since a range takes one pipe.
|
|
|
|
|
static func assign_ranges(t: RollTable) -> void:
|
|
|
|
|
var d := t.dice_obj()
|
|
|
|
|
var lo := d.min_value()
|
|
|
|
|
var hi := d.max_value()
|
|
|
|
|
var live: Array[TableEntry] = []
|
|
|
|
|
var total := 0
|
|
|
|
|
for e in t.entries:
|
|
|
|
|
e.has_range = false
|
|
|
|
|
if e.weight > 0:
|
|
|
|
|
live.append(e)
|
|
|
|
|
total += e.weight
|
|
|
|
|
while e.pipes.size() > 1:
|
|
|
|
|
e.remove_pipe(e.pipes[e.pipes.size() - 1])
|
|
|
|
|
var span := hi - lo + 1
|
|
|
|
|
var cum := 0
|
|
|
|
|
var prev := lo - 1
|
|
|
|
|
for i in live.size():
|
|
|
|
|
var e := live[i]
|
|
|
|
|
if prev >= hi:
|
|
|
|
|
break
|
|
|
|
|
cum += e.weight
|
|
|
|
|
var end := lo - 1 + roundi(float(span) * cum / total)
|
|
|
|
|
end = mini(end, hi - (live.size() - 1 - i))
|
|
|
|
|
end = clampi(maxi(end, prev + 1), prev + 1, hi)
|
|
|
|
|
if i == live.size() - 1:
|
|
|
|
|
end = hi
|
|
|
|
|
e.has_range = true
|
|
|
|
|
e.range_lo = prev + 1
|
|
|
|
|
e.range_hi = end
|
|
|
|
|
prev = end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static func drop_ranges(t: RollTable) -> void:
|
|
|
|
|
for e in t.entries:
|
|
|
|
|
e.has_range = false
|
|
|
|
|
e.range_lo = 1
|
|
|
|
|
e.range_hi = 1
|