TableTower/tools/simulate_board.gd

95 lines
3.7 KiB
GDScript3
Raw Permalink Normal View History

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.
## --check also print the static checks (BoardTester.static_checks).
##
## A thin wrapper around BoardTester (core/board_tester.gd); simulate(),
## answer(), is_clean() and format_report() forward to it.
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 == "":
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]")
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")))
if args.has("check"):
print("\n".join(BoardTester.format_problems(BoardTester.static_checks(board), int(args.get("list", "40")))))
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:
return await BoardTester.simulate(board, start, opts)
static func answer(req: Dictionary, rng: RandomNumberGenerator, prefer: RegEx = null) -> Variant:
return BoardTester.answer(req, rng, prefer)
static func is_clean(report: Dictionary) -> bool:
return BoardTester.is_clean(report)
static func format_report(r: Dictionary, list: int = 40, show_sheet: bool = true) -> String:
return BoardTester.format_report(r, list, show_sheet)