Add a headless board simulator tool and its test
tools/simulate_board.gd rolls a collection N times with a random (or
regex-preferred) player answering every choice card, each run on fresh
board variables, and reports completions, halt reasons, action stats,
effect/expression errors with table and entry, dead ends, unset or
failed markers in text and prompts, unreached tables, never-chosen
entries, tracked variable ranges, the longest run's busiest tables and
a sample final sheet. tests/test_simulate.gd runs it on an inline board.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:15:21 -06:00
|
|
|
extends SceneTree
|
|
|
|
|
## Rolls a collection many times headlessly, answering every choice card at
|
|
|
|
|
## random, and reports what happened: how many rolls finished, why the others
|
|
|
|
|
## stopped, how many actions they took, every effect or expression error, the
|
|
|
|
|
## tables and entries that were never reached, and a sample final sheet.
|
|
|
|
|
##
|
|
|
|
|
## Run:
|
|
|
|
|
## godot --headless --path . -s res://tools/simulate_board.gd -- \
|
|
|
|
|
## --board=<collection.json> --start=<table id> --runs=N [--seed=S] \
|
|
|
|
|
## [--prefer=<regex>] [--track=a,b] [--max-actions=N] [--list=N] [--quiet]
|
|
|
|
|
##
|
|
|
|
|
## --prefer on a pick card, choose an option whose text matches this
|
|
|
|
|
## regular expression (case-insensitive) whenever there is one,
|
|
|
|
|
## e.g. --prefer="^Continue|another career"; otherwise random.
|
|
|
|
|
## --list how many lines to print per section (default 40).
|
|
|
|
|
## --quiet leave the sample sheet out of the report.
|
|
|
|
|
## --track comma-separated board variables (no "@") whose final values
|
|
|
|
|
## are summarised over all runs (min, mean, max), e.g.
|
|
|
|
|
## --track=terms,age
|
|
|
|
|
##
|
|
|
|
|
## Every run starts with a fresh, empty set of board variables (its own
|
|
|
|
|
## persistent-vars dictionary), so runs never leak state into each other.
|
|
|
|
|
## The exit code is 1 when any run hit an error, the action limit, or a
|
|
|
|
|
## dead end ("No result for ..."), else 0.
|
2026-11-08 17:49:35 -06:00
|
|
|
## --check also print the static checks (BoardTester.static_checks).
|
Add a headless board simulator tool and its test
tools/simulate_board.gd rolls a collection N times with a random (or
regex-preferred) player answering every choice card, each run on fresh
board variables, and reports completions, halt reasons, action stats,
effect/expression errors with table and entry, dead ends, unset or
failed markers in text and prompts, unreached tables, never-chosen
entries, tracked variable ranges, the longest run's busiest tables and
a sample final sheet. tests/test_simulate.gd runs it on an inline board.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:15:21 -06:00
|
|
|
##
|
2026-11-08 17:49:35 -06:00
|
|
|
## A thin wrapper around BoardTester (core/board_tester.gd); simulate(),
|
|
|
|
|
## answer(), is_clean() and format_report() forward to it.
|
Add a headless board simulator tool and its test
tools/simulate_board.gd rolls a collection N times with a random (or
regex-preferred) player answering every choice card, each run on fresh
board variables, and reports completions, halt reasons, action stats,
effect/expression errors with table and entry, dead ends, unset or
failed markers in text and prompts, unreached tables, never-chosen
entries, tracked variable ranges, the longest run's busiest tables and
a sample final sheet. tests/test_simulate.gd runs it on an inline board.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:15:21 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func _init() -> void:
|
|
|
|
|
var args := _parse_args(OS.get_cmdline_user_args())
|
|
|
|
|
var path := str(args.get("board", ""))
|
|
|
|
|
var start := str(args.get("start", ""))
|
|
|
|
|
if path == "" or start == "":
|
2026-11-08 17:49:35 -06:00
|
|
|
print("Usage: godot --headless --path . -s res://tools/simulate_board.gd -- --board=<json> --start=<table id> --runs=N [--seed=S] [--prefer=<regex>] [--track=a,b] [--max-actions=N] [--list=N] [--quiet] [--check]")
|
Add a headless board simulator tool and its test
tools/simulate_board.gd rolls a collection N times with a random (or
regex-preferred) player answering every choice card, each run on fresh
board variables, and reports completions, halt reasons, action stats,
effect/expression errors with table and entry, dead ends, unset or
failed markers in text and prompts, unreached tables, never-chosen
entries, tracked variable ranges, the longest run's busiest tables and
a sample final sheet. tests/test_simulate.gd runs it on an inline board.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:15:21 -06:00
|
|
|
quit(2)
|
|
|
|
|
return
|
|
|
|
|
var text := FileAccess.get_file_as_string(path)
|
|
|
|
|
if text == "":
|
|
|
|
|
print("Can't read %s" % path)
|
|
|
|
|
quit(2)
|
|
|
|
|
return
|
|
|
|
|
var board := TowerBoard.new()
|
|
|
|
|
if not board.load_json(text):
|
|
|
|
|
print("%s is not a TableTower collection" % path)
|
|
|
|
|
quit(2)
|
|
|
|
|
return
|
|
|
|
|
if board.get_table(start) == null:
|
|
|
|
|
print("No table with id \"%s\" in %s" % [start, path])
|
|
|
|
|
quit(2)
|
|
|
|
|
return
|
|
|
|
|
var opts := {
|
|
|
|
|
"runs": int(args.get("runs", "100")),
|
|
|
|
|
"seed": int(args.get("seed", "1")),
|
|
|
|
|
"prefer": str(args.get("prefer", "")),
|
|
|
|
|
"max_actions": int(args.get("max-actions", str(RollContext.DEFAULT_MAX_ACTIONS))),
|
|
|
|
|
"track": str(args.get("track", "")).split(",", false),
|
|
|
|
|
}
|
|
|
|
|
var report: Dictionary = await simulate(board, start, opts)
|
|
|
|
|
print(format_report(report, int(args.get("list", "40")), not args.has("quiet")))
|
2026-11-08 17:49:35 -06:00
|
|
|
if args.has("check"):
|
|
|
|
|
print("\n".join(BoardTester.format_problems(BoardTester.static_checks(board), int(args.get("list", "40")))))
|
Add a headless board simulator tool and its test
tools/simulate_board.gd rolls a collection N times with a random (or
regex-preferred) player answering every choice card, each run on fresh
board variables, and reports completions, halt reasons, action stats,
effect/expression errors with table and entry, dead ends, unset or
failed markers in text and prompts, unreached tables, never-chosen
entries, tracked variable ranges, the longest run's busiest tables and
a sample final sheet. tests/test_simulate.gd runs it on an inline board.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:15:21 -06:00
|
|
|
quit(0 if is_clean(report) else 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static func _parse_args(list: PackedStringArray) -> Dictionary:
|
|
|
|
|
var out := {}
|
|
|
|
|
for a in list:
|
|
|
|
|
if not a.begins_with("--"):
|
|
|
|
|
continue
|
|
|
|
|
var s := a.substr(2)
|
|
|
|
|
var eq := s.find("=")
|
|
|
|
|
if eq < 0:
|
|
|
|
|
out[s] = "true"
|
|
|
|
|
else:
|
|
|
|
|
out[s.substr(0, eq)] = s.substr(eq + 1)
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static func simulate(board: TowerBoard, start: String, opts: Dictionary = {}) -> Dictionary:
|
2026-11-08 17:49:35 -06:00
|
|
|
return await BoardTester.simulate(board, start, opts)
|
Add a headless board simulator tool and its test
tools/simulate_board.gd rolls a collection N times with a random (or
regex-preferred) player answering every choice card, each run on fresh
board variables, and reports completions, halt reasons, action stats,
effect/expression errors with table and entry, dead ends, unset or
failed markers in text and prompts, unreached tables, never-chosen
entries, tracked variable ranges, the longest run's busiest tables and
a sample final sheet. tests/test_simulate.gd runs it on an inline board.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:15:21 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
static func answer(req: Dictionary, rng: RandomNumberGenerator, prefer: RegEx = null) -> Variant:
|
2026-11-08 17:49:35 -06:00
|
|
|
return BoardTester.answer(req, rng, prefer)
|
Add a headless board simulator tool and its test
tools/simulate_board.gd rolls a collection N times with a random (or
regex-preferred) player answering every choice card, each run on fresh
board variables, and reports completions, halt reasons, action stats,
effect/expression errors with table and entry, dead ends, unset or
failed markers in text and prompts, unreached tables, never-chosen
entries, tracked variable ranges, the longest run's busiest tables and
a sample final sheet. tests/test_simulate.gd runs it on an inline board.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:15:21 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
static func is_clean(report: Dictionary) -> bool:
|
2026-11-08 17:49:35 -06:00
|
|
|
return BoardTester.is_clean(report)
|
Add a headless board simulator tool and its test
tools/simulate_board.gd rolls a collection N times with a random (or
regex-preferred) player answering every choice card, each run on fresh
board variables, and reports completions, halt reasons, action stats,
effect/expression errors with table and entry, dead ends, unset or
failed markers in text and prompts, unreached tables, never-chosen
entries, tracked variable ranges, the longest run's busiest tables and
a sample final sheet. tests/test_simulate.gd runs it on an inline board.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:15:21 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
static func format_report(r: Dictionary, list: int = 40, show_sheet: bool = true) -> String:
|
2026-11-08 17:49:35 -06:00
|
|
|
return BoardTester.format_report(r, list, show_sheet)
|