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

136 lines
4 KiB
GDScript

extends SceneTree
## Renders the game scene after some moves and saves a screenshot, for checking the look. Needs a
## display (not --headless).
##
## godot --script res://tests/screenshot.gd -- [--seed N] [--moves N] [--out PATH] [--top] [--north] [--select KEY|auto]
## [--focus [N]] [--terminal] [--frames N] [--at SECONDS] [--zoom X] [--combat]
##
## --combat keeps playing past --moves until a combat is under way at one of your decisions.
## --at takes the shot that many seconds after the moves are made instead of at a frame, to catch
## an animation part way (use with --disable-vsync, as a hidden window's frames can be throttled).
##
## --focus opens the hand-focus row, with its Nth card under the mouse; --terminal opens the log.
var _game: Node
var _seed := 1
var _moves := 40
var _out := "/tmp/files-tcg-shot.png"
var _top := false
var _select := ""
var _human := 0
var _frames := 0
var _shot_at := 70
var _focus := -2
var _terminal := false
var _zoom := 1.0
var _combat := false
var _at := -1.0
var _dealt_at := 0
var _rng := RandomNumberGenerator.new()
func _init() -> void:
var args := OS.get_cmdline_user_args()
var i := 0
while i < args.size():
match args[i]:
"--seed":
i += 1
_seed = int(args[i])
"--moves":
i += 1
_moves = int(args[i])
"--out":
i += 1
_out = args[i]
"--top":
_top = true
"--north":
_human = 1
"--select":
i += 1
_select = args[i]
"--focus":
_focus = -1
if i + 1 < args.size() and args[i + 1].is_valid_int():
i += 1
_focus = int(args[i])
"--terminal":
_terminal = true
"--frames":
i += 1
_shot_at = int(args[i])
"--zoom":
i += 1
_zoom = float(args[i])
"--combat":
_combat = true
"--at":
i += 1
_at = float(args[i])
i += 1
_rng.seed = _seed
# Don't take keyboard focus or clicks from whatever you're doing while it renders.
DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_NO_FOCUS, true)
_game = load("res://scenes/game.tscn").instantiate()
_game.set_process_unhandled_input(false)
root.add_child.call_deferred(_game)
func _process(_delta: float) -> bool:
if not _game.is_node_ready():
return false
_frames += 1
if _frames == 2:
_game._paused = true
_game.hud.close_modal()
_game.start_game({"seed": _seed, "human": _human, "style": "greedy"})
var m: LocalMatch = _game.match_
var n := 0
var in_combat := func() -> bool:
var c = m.combat()
return c != null and not c.done and m.human_to_act()
while (n < _moves or (_combat and not in_combat.call() and n < 4000)) and not m.is_over():
n += 1
if m.bot_to_act():
m.bot_step()
continue
var moves: Array = _game.moves
var others := moves.filter(func(mv): return mv.action.type not in ["end", "pass", "salvage"])
if not others.is_empty() and _rng.randf() < 0.7:
_game._play(others[_rng.randi() % others.size()])
else:
var stop := moves.filter(func(mv): return mv.action.type in ["end", "pass"])
_game._play(stop[0] if not stop.is_empty() else moves[0])
m.run_bot()
_dealt_at = Time.get_ticks_msec()
if _combat:
print("combat under way: %s" % in_combat.call())
if _top:
_game.table.toggle_view()
if _zoom != 1.0:
_game.table.zoom(_zoom)
if _select == "auto":
# The first hand card with a targeted move.
for mv in _game.moves:
if mv.source.begins_with("hand:") and mv.target != "none":
_select = mv.source
break
if _select != "":
_game._select(_select)
var card := _select.substr(5) if _select.begins_with("hand:") else ""
_game._inspect({"key": _select, "card": card})
if _frames == 4:
if _terminal:
_game.hud.set_drawer(true)
if _focus >= -1:
_game.hud.set_hand_focus(true)
if _frames == 30 and _focus >= 0:
_game.hud._hover_focus(_focus)
var due := _frames == _shot_at if _at < 0 else _frames > 2 and Time.get_ticks_msec() - _dealt_at >= _at * 1000
if due:
var img := root.get_viewport().get_texture().get_image()
img.save_png(_out)
print("saved %s" % _out)
return true
return false