files-game/tests/screenshot.gd
2026-10-01 17:22:18 -04:00

88 lines
2.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]
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 _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]
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
while n < _moves 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()
if _top:
_game.table.toggle_view()
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 == 70:
var img := root.get_viewport().get_texture().get_image()
img.save_png(_out)
print("saved %s" % _out)
return true
return false