132 lines
5.4 KiB
GDScript3
132 lines
5.4 KiB
GDScript3
|
|
class_name BoardChecks
|
||
|
|
extends RefCounted
|
||
|
|
## Checks a board without rolling anything: bad expressions and effects,
|
||
|
|
## range gaps, threads to missing tables, choice cards with no options and
|
||
|
|
## endless cycles. Also picks the tables worth starting a test from.
|
||
|
|
## BoardTester delegates here.
|
||
|
|
|
||
|
|
|
||
|
|
## Problems found without rolling anything. Array of {kind, table_id,
|
||
|
|
## entry_id ("" for the card), table (display name), text}, in board order.
|
||
|
|
## kinds: expression, effect, range, cycle, missing_pipe, empty_choice, pipes,
|
||
|
|
## check (check cards: bad dice, modifier or target, missing results) and
|
||
|
|
## inputs (card inputs and pipe "with" values; see Roller.lint_inputs()).
|
||
|
|
## Items from Roller.lint_pipes() also carry its "level" when it has one
|
||
|
|
## ("error" or "hint").
|
||
|
|
static func static_checks(board: TowerBoard) -> Array[Dictionary]:
|
||
|
|
var out: Array[Dictionary] = []
|
||
|
|
var add := func(kind: String, table: RollTable, entry_id: String, text: String) -> void:
|
||
|
|
out.append({"kind": kind, "table_id": table.id, "entry_id": entry_id, "table": Formatter.strip_tags(table.name), "text": text})
|
||
|
|
for table in board.tables:
|
||
|
|
_check_table(board, table, add)
|
||
|
|
for id in Roller.endless_cycles(board):
|
||
|
|
var table := board.get_table(id)
|
||
|
|
add.call("cycle", table, "", "Can never finish: every result leads back into an endless cycle.")
|
||
|
|
_add_lint(board, out, add)
|
||
|
|
return out
|
||
|
|
|
||
|
|
|
||
|
|
## The checks for one card and its results; `add` records each problem.
|
||
|
|
static func _check_table(board: TowerBoard, table: RollTable, add: Callable) -> void:
|
||
|
|
var expr := func(what: String, src: String, entry_id: String = "") -> void:
|
||
|
|
if src.strip_edges() == "":
|
||
|
|
return
|
||
|
|
var err := Expr.check(src)
|
||
|
|
if err != "":
|
||
|
|
add.call("expression", table, entry_id, "%s \"%s\": %s" % [what, src, err])
|
||
|
|
if table.is_expr_dice():
|
||
|
|
expr.call("Dice", table.dice)
|
||
|
|
if table.is_ranged():
|
||
|
|
expr.call("Modifier", table.dice_mod)
|
||
|
|
if table.is_ranged():
|
||
|
|
var range_problem := table.range_problems()
|
||
|
|
if range_problem != "":
|
||
|
|
add.call("range", table, "", range_problem)
|
||
|
|
match table.kind:
|
||
|
|
RollTable.KIND_LOOP:
|
||
|
|
expr.call("Repeat rule", table.loop_expr)
|
||
|
|
RollTable.KIND_CHOICE:
|
||
|
|
if table.choice == "pick" and table.entries.is_empty():
|
||
|
|
add.call("empty_choice", table, "", "Choice card has no options.")
|
||
|
|
if table.choice == "number":
|
||
|
|
expr.call("Minimum", table.min_expr)
|
||
|
|
expr.call("Maximum", table.max_expr)
|
||
|
|
_check_braces(table.prompt, func(src: String) -> void: expr.call("Prompt", src))
|
||
|
|
RollTable.KIND_VARIABLE:
|
||
|
|
var init := table.initial.strip_edges()
|
||
|
|
if table.var_type == "number" and init != "" and not init.is_valid_float():
|
||
|
|
expr.call("Initial value", init)
|
||
|
|
for entry in table.entries:
|
||
|
|
_check_braces(entry.text, func(src: String) -> void: expr.call("Text", src, entry.id))
|
||
|
|
for problem in Effects.validate(entry.effects):
|
||
|
|
add.call("effect", table, entry.id, "Change \"%s\": %s" % [problem["src"], problem["error"]])
|
||
|
|
for target in entry.pipes:
|
||
|
|
if board.get_table(target) == null:
|
||
|
|
add.call("missing_pipe", table, entry.id, "Thread to a table that doesn't exist (%s)." % target)
|
||
|
|
var count_expr := entry.count_expr(target)
|
||
|
|
if count_expr != "":
|
||
|
|
expr.call("Thread count", count_expr, entry.id)
|
||
|
|
|
||
|
|
|
||
|
|
## Adds the items from Roller.lint_pipes(), when the Roller has it.
|
||
|
|
static func _add_lint(board: TowerBoard, out: Array[Dictionary], add: Callable) -> void:
|
||
|
|
var roller: GDScript = Roller
|
||
|
|
var has_lint := false
|
||
|
|
for method in roller.get_script_method_list():
|
||
|
|
if method["name"] == "lint_pipes":
|
||
|
|
has_lint = true
|
||
|
|
if not has_lint:
|
||
|
|
return
|
||
|
|
var lint: Variant = roller.call("lint_pipes", board)
|
||
|
|
if not (lint is Array):
|
||
|
|
return
|
||
|
|
for item: Variant in lint:
|
||
|
|
var table: RollTable = null
|
||
|
|
var entry_id := ""
|
||
|
|
var text := str(item)
|
||
|
|
var lint_kind := "pipes"
|
||
|
|
var level := ""
|
||
|
|
if item is Dictionary:
|
||
|
|
var details: Dictionary = item
|
||
|
|
table = board.get_table(str(details.get("table_id", details.get("table", ""))))
|
||
|
|
entry_id = str(details.get("entry_id", details.get("entry", "")))
|
||
|
|
text = str(details.get("text", details.get("message", details.get("problem", text))))
|
||
|
|
lint_kind = str(details.get("kind", "pipes"))
|
||
|
|
level = str(details.get("level", ""))
|
||
|
|
if table == null:
|
||
|
|
table = RollTable.new("", "")
|
||
|
|
add.call(lint_kind, table, entry_id, text)
|
||
|
|
if level != "":
|
||
|
|
out.back()["level"] = level
|
||
|
|
|
||
|
|
|
||
|
|
## Calls `on_expr(src)` for each `{= expr}` in the text.
|
||
|
|
static func _check_braces(text: String, on_expr: Callable) -> void:
|
||
|
|
var brace_re := RegEx.create_from_string("\\{=([^}]*)\\}")
|
||
|
|
for found in brace_re.search_all(text):
|
||
|
|
on_expr.call(found.get_string(1))
|
||
|
|
|
||
|
|
|
||
|
|
## Tables worth starting a test from: not variable cards, with nothing piping
|
||
|
|
## into them and nothing referencing them by [[name]]. If `last_rolled` is a
|
||
|
|
## table it comes first (even if it isn't a root). Ids in board order.
|
||
|
|
static func start_candidates(board: TowerBoard, last_rolled: String = "") -> Array[String]:
|
||
|
|
var used := {}
|
||
|
|
for table in board.tables:
|
||
|
|
for entry in table.entries:
|
||
|
|
for pipe in entry.pipes:
|
||
|
|
used[pipe] = true
|
||
|
|
for ref in Roller.ref_targets(board, entry.text):
|
||
|
|
used[ref] = true
|
||
|
|
for ref in Roller.ref_targets(board, table.prompt):
|
||
|
|
used[ref] = true
|
||
|
|
var out: Array[String] = []
|
||
|
|
for table in board.tables:
|
||
|
|
if table.kind != RollTable.KIND_VARIABLE and not used.has(table.id):
|
||
|
|
out.append(table.id)
|
||
|
|
var last_table := board.get_table(last_rolled)
|
||
|
|
if last_table != null and last_table.kind != RollTable.KIND_VARIABLE:
|
||
|
|
out.erase(last_table.id)
|
||
|
|
out.push_front(last_table.id)
|
||
|
|
return out
|