files-game/tests/table_smoke.gd
2026-10-01 23:01:56 -04:00

150 lines
4.5 KiB
GDScript

extends SceneTree
## Plays whole games through the game scene, headless: you are driven by random picks from the
## move list, the bot is the greedy bot. Checks that every legal move can be reached by clicking
## the table: a screen point clear of the panels whose raycast hits the move's source, and one for
## its target (or a button on the source's menu). Also checks that the table builds for every
## view and that nothing raises an engine error.
##
## godot --headless --script res://tests/table_smoke.gd -- [--games N] [--seed N] [--style S] [--threaded]
##
## --threaded lets the game run the bot itself, on its worker thread, as it does in play.
var _game: Node
var _games := 3
var _seed := 1
var _style := "greedy"
var _played := 0
var _steps := 0
var _unreachable := {}
var _rng := RandomNumberGenerator.new()
var _started := 0
var _failed := false
var _threaded := false
## The physics frame to wait for before the next check, so the bodies have moved with the cards.
var _wait := -1
var _checks := 0
var _began := false
func _init() -> void:
var args := OS.get_cmdline_user_args()
var i := 0
while i < args.size():
match args[i]:
"--games":
i += 1
_games = int(args[i])
"--seed":
i += 1
_seed = int(args[i])
"--style":
i += 1
_style = args[i]
"--threaded":
_threaded = true
i += 1
_rng.seed = _seed
Card3D.instant = true
_game = load("res://scenes/game.tscn").instantiate()
root.add_child.call_deferred(_game)
_started = Time.get_ticks_msec()
func _process(_delta: float) -> bool:
if _game == null or not _game.is_node_ready():
return false
# Once: with --threaded, bot moves don't count as steps, so a step count can't tell.
if not _began:
_began = true
_new_game()
if Engine.get_physics_frames() < _wait:
return false
# Bot moves run until you decide; then wait for physics to catch up before checking.
for n in 200:
var m: LocalMatch = _game.match_
if m.human_to_act() and not m.is_over() and Util.errors.is_empty():
if _wait < 0:
_wait = Engine.get_physics_frames() + 2
return false
_check_reachable(m)
_wait = -1
if not _step():
return true
if _threaded and m.bot_to_act():
return false
if not m.bot_to_act():
_wait = -1
return false
return false
func _new_game() -> void:
_game._paused = not _threaded
_game.settings.botDelay = 0.0
_game.hud.close_modal()
_game.start_game({"seed": _seed + _played, "human": _played % 2, "style": _style})
Util.errors.clear()
## One move. Returns false when the run is over.
func _step() -> bool:
var m: LocalMatch = _game.match_
if not Util.errors.is_empty():
print("seed %d: engine error: %s" % [m.setup.seed, "; ".join(Util.errors)])
_failed = true
return _finish()
if m.is_over():
print("seed %d: %s after %d moves, turn %d" % [m.setup.seed, Hud.status_line(m.view(), m), m.actions.size(), m.s.turn])
_played += 1
if _played >= _games:
return _finish()
_new_game()
return true
if m.bot_to_act():
if _threaded:
return true
_steps += 1
m.bot_step()
return true
_steps += 1
_check_reachable(m)
var moves: Array = _game.moves
var others := moves.filter(func(mv): return mv.action.type != "end" and mv.action.type != "pass" and mv.action.type != "salvage")
var pick: Dictionary
if not others.is_empty() and _rng.randf() < 0.6:
pick = others[_rng.randi() % others.size()]
else:
var stop := moves.filter(func(mv): return mv.action.type == "end" or mv.action.type == "pass")
pick = stop[0] if not stop.is_empty() else moves[_rng.randi() % moves.size()]
_game._play(pick)
return true
func _check_reachable(_m: LocalMatch) -> void:
_checks += 1
var table: GameTable = _game.table
var found := {}
for mv in _game.moves:
if mv.source == "bar":
continue
var bad := ""
for key in [mv.source, mv.target]:
if key == "none" or found.has(key):
continue
if table.screen_point_for(key) != null:
found[key] = true
else:
bad = key
break
if bad != "":
var k := "%s (%s → %s, missing %s)" % [mv.group, mv.source.split(":")[0], mv.target.split(":")[0], bad.split(":")[0]]
if not _unreachable.has(k):
_unreachable[k] = mv.label
print("unreachable by clicking: %s: %s [%s]" % [k, mv.label, bad])
func _finish() -> bool:
var secs := (Time.get_ticks_msec() - _started) / 1000.0
print("table smoke: %d game(s), %d steps, %d click checks, %d unreachable move kinds, %.1fs" % [_played, _steps, _checks, _unreachable.size(), secs])
quit(1 if _failed or not _unreachable.is_empty() else 0)
return false