TableTower/core/roll_lint.gd

237 lines
9.5 KiB
GDScript3
Raw Permalink Normal View History

class_name RollLint
extends RefCounted
## Static checks of a board for the Roller's logic, nothing rolled: endless
## pipe cycles and the lint_* problem lists.
## Ids of tables that can never finish rolling: every result they can give
## leads, through pipes, back into such a table. A table that loops on only
## some of its entries is fine, and so is a pipe whose count is an expression
## (it may come to 0) or a conditional pipe (it may not run).
static func endless_cycles(board: TowerBoard) -> Array[String]:
var done := {}
var changed := true
while changed:
changed = false
for t in board.tables:
if done.has(t.id):
continue
if _can_finish(t, board, done):
done[t.id] = true
changed = true
var out: Array[String] = []
for t in board.tables:
if not done.has(t.id):
out.append(t.id)
return out
static func _entry_finishes(e: TableEntry, board: TowerBoard, done: Dictionary) -> bool:
for p in e.pipes:
if e.count_expr(p) != "" or e.is_conditional(p):
continue
if board.get_table(p) != null and not done.has(p):
return false
return true
static func _can_finish(t: RollTable, board: TowerBoard, done: Dictionary) -> bool:
if t.entries.is_empty() or t.kind == RollTable.KIND_VARIABLE:
return true
# A card thread that always runs follows every result (CardThreads).
if not _entry_finishes(t.outlet, board, done):
return false
if t.kind == RollTable.KIND_LOOP:
# Every entry runs (a while/until loop ends on its condition).
for e in t.entries:
if not _entry_finishes(e, board, done):
return false
return true
var player := t.kind != RollTable.KIND_ROLL
for e in t.entries:
if not player and not t.can_come_up(e):
continue
if _entry_finishes(e, board, done):
return true
if player:
return false
# No entry can come up at all: the roll just stops.
for e in t.entries:
if t.can_come_up(e):
return false
return true
## Problems with conditional pipes and router cards, for a board check. Pure:
## nothing is rolled. Each item: {table_id, entry_id ("" for the whole card),
## message}.
## - a pipe condition, or a router case condition, that doesn't parse
## - an entry whose "otherwise" pipe comes before one of its conditional
## pipes (a hint: it still works, since the snapshot is taken first)
## - an "otherwise" pipe on an entry with no conditional pipe (always runs)
## - a router with no cases, or whose Otherwise case isn't the last one
static func lint_pipes(board: TowerBoard) -> Array[Dictionary]:
var out: Array[Dictionary] = []
for t in board.tables:
out.append_array(lint_router(t))
out.append_array(lint_options(t))
out.append_array(lint_check(t))
out.append_array(lint_inputs(board, t))
out.append_array(RollLintBook.lint(board, t))
for e in t.thread_sources():
var first_other := ""
var has_when := false
var warned := false
for p in e.pipes:
var pw := e.when_for(p)
if pw != "":
has_when = true
if Expr.check(pw) != "":
out.append({"table_id": t.id, "entry_id": e.id,
"message": "Thread condition %s: %s" % [pw, Expr.check(pw)]})
if first_other != "" and not warned:
warned = true
out.append({"table_id": t.id, "entry_id": e.id,
"message": "The otherwise thread to %s comes before a conditional thread; put it last so it reads in order" % _table_name(board, first_other)})
elif e.is_otherwise(p) and first_other == "":
first_other = p
# A card's otherwise thread waits for the result's threads, so it
# needs no condition beside it (CardThreads).
if first_other != "" and not has_when and e != t.outlet:
out.append({"table_id": t.id, "entry_id": e.id,
"message": "The otherwise thread to %s always runs: no thread on this result has a condition" % _table_name(board, first_other)})
return out
## The router part of lint_pipes() for one card (empty for other kinds): no
## cases, an Otherwise case that isn't last, case conditions that don't parse.
static func lint_router(t: RollTable) -> Array[Dictionary]:
var out: Array[Dictionary] = []
if t.kind != RollTable.KIND_ROUTER:
return out
if t.entries.is_empty():
out.append({"table_id": t.id, "entry_id": "", "message": "This branch has no cases"})
for i in t.entries.size():
var e := t.entries[i]
var w := e.when.strip_edges()
if w == "":
if i < t.entries.size() - 1:
out.append({"table_id": t.id, "entry_id": e.id,
"message": "The Otherwise case isn't last, so the cases after it can never be chosen"})
elif Expr.check(w) != "":
out.append({"table_id": t.id, "entry_id": e.id, "message": "Case %s: %s" % [w, Expr.check(w)]})
return out
## Problems with a card's dynamic and conditional options (empty when fine):
## a bad options_from pattern or options_where filter, a filter without
## options_from, options_from or show_if on a card that isn't a pick choice
## (they do nothing there), show_if conditions that don't parse, and a
## dynamic card with no Chosen entry.
static func lint_options(t: RollTable) -> Array[Dictionary]:
var out: Array[Dictionary] = []
var pick := t.kind == RollTable.KIND_CHOICE and t.choice == "pick"
var from := t.options_from.strip_edges()
var where := t.options_where.strip_edges()
if from != "":
if not pick:
out.append({"table_id": t.id, "entry_id": "", "message": "Options from %s does nothing: only Which option cards take options from variables" % from})
else:
var p := RollTable.parse_options_from(from)
if str(p["error"]) != "":
out.append({"table_id": t.id, "entry_id": "", "message": "Options from %s: %s" % [from, p["error"]]})
if t.entries.is_empty():
out.append({"table_id": t.id, "entry_id": "", "message": "This card takes options from %s but has no Chosen result" % from})
if where != "":
if from == "":
out.append({"table_id": t.id, "entry_id": "", "message": "Only where %s does nothing without Options from" % where})
elif Expr.check(where) != "":
out.append({"table_id": t.id, "entry_id": "", "message": "Only where %s: %s" % [where, Expr.check(where)]})
for e in t.entries:
var w := e.show_if.strip_edges()
if w == "":
continue
if not pick and t.kind != RollTable.KIND_ROLL:
out.append({"table_id": t.id, "entry_id": e.id, "message": "Only if %s does nothing here: only results and Which options can be held back" % w})
elif Expr.check(w) != "":
out.append({"table_id": t.id, "entry_id": e.id, "message": "Show only if %s: %s" % [w, Expr.check(w)]})
return out
## Problems with a check card (empty for other kinds): dice, modifier or
## target expressions that don't parse or are empty, and a missing Success or
## Failure result. Items also carry "kind": "check" and "level": "error".
static func lint_check(t: RollTable) -> Array[Dictionary]:
var out: Array[Dictionary] = []
if t.kind != RollTable.KIND_CHECK:
return out
for pair: Array in [["Check dice", t.check_dice], ["Check modifier", t.check_mod], ["Check target", t.check_target]]:
var src := str(pair[1]).strip_edges()
var msg := ""
if src == "":
if pair[0] != "Check modifier":
msg = "%s is empty" % pair[0]
elif Expr.check(src) != "":
msg = "%s %s: %s" % [pair[0], src, Expr.check(src)]
if msg != "":
out.append({"table_id": t.id, "entry_id": "", "message": msg, "kind": "check", "level": "error"})
if t.entries.size() < 2:
out.append({"table_id": t.id, "entry_id": "", "kind": "check", "level": "error",
"message": "A check needs a Success result and a Failure result"})
return out
## Problems with card inputs and the pipes that pass them (docs/PLAIN.md
## §4.2): declared inputs with a bad or repeated name or a default that
## doesn't parse ("level": "error"), pipe "with" expressions that don't parse
## (error), and a "with" naming an input the target doesn't declare
## ("level": "hint": it still works, as a chain value). Items carry "kind":
## "inputs".
static func lint_inputs(board: TowerBoard, t: RollTable) -> Array[Dictionary]:
var out: Array[Dictionary] = []
var seen := {}
for inp: Dictionary in t.inputs:
var nm := str(inp.get("name", ""))
if not _is_name(nm):
out.append({"table_id": t.id, "entry_id": "", "kind": "inputs", "level": "error",
"message": "Input \"%s\" isn't a name (lowercase letters, digits and _)" % nm})
elif seen.has(nm):
out.append({"table_id": t.id, "entry_id": "", "kind": "inputs", "level": "error",
"message": "Input %s is declared twice" % nm})
seen[nm] = true
var def := str(inp.get("default", "")).strip_edges()
if def != "" and Expr.check(def) != "":
out.append({"table_id": t.id, "entry_id": "", "kind": "inputs", "level": "error",
"message": "Input %s default %s: %s" % [nm, def, Expr.check(def)]})
for e in t.thread_sources():
if e.pipe_with.is_empty():
continue
for p in e.pipes:
var w := e.with_for(p)
if w.is_empty():
continue
var target := board.get_table(p)
for nm: String in w:
var src := str(w[nm])
if Expr.check(src) != "":
out.append({"table_id": t.id, "entry_id": e.id, "kind": "inputs", "level": "error",
"message": "Thread input %s = %s: %s" % [nm, src, Expr.check(src)]})
if target != null and target.find_input(nm).is_empty():
out.append({"table_id": t.id, "entry_id": e.id, "kind": "inputs", "level": "hint",
"message": "The thread to %s passes %s, but that card has no input called %s" % [_table_name(board, p), nm, nm]})
return out
static var _name_re: RegEx = null
static func _is_name(s: String) -> bool:
if _name_re == null:
_name_re = RegEx.create_from_string("^[a-z_][a-z0-9_]*$")
return _name_re.search(s) != null
static func _table_name(board: TowerBoard, id: String) -> String:
var t := board.get_table(id)
return Formatter.strip_tags(t.name) if t else id