extends SceneTree ## Plays whole games through the game scene with the animations on (sped up), so every engine ## event goes through the presenter and the table's motion paths in real frames. The bot runs on ## its thread as in play, and waits for each show to end; your moves are random picks made once ## the show is over. Fails on an engine error or a stuck game. ## ## godot --headless --script res://tests/present_smoke.gd -- [--games N] [--seed N] [--speed X] var _game: Node var _games := 2 var _seed := 1 var _speed := 0.08 var _played := 0 var _moves := 0 var _shows := 0 var _events := 0 var _rng := RandomNumberGenerator.new() var _started := 0 var _idle_frames := 0 var _failed := false var _frames := 0 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]) "--speed": i += 1 _speed = float(args[i]) i += 1 _rng.seed = _seed _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 _frames += 1 if _frames == 1: Motion.speed_scale = _speed _new_game() return false 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" % [m.setup.seed, Hud.status_line(m.view(), m), m.actions.size()]) _played += 1 if _played >= _games: return _finish() _new_game() return false var presenter: Presenter = _game.presenter if presenter.busy(): _idle_frames = 0 return false _idle_frames += 1 if m.human_to_act() and _idle_frames > 1: _idle_frames = 0 var before := m.event_count() _pick(m) _events += m.event_count() - before _shows += 1 elif _idle_frames > 3000: print("stuck: no move for 3000 frames (bot to act: %s)" % m.bot_to_act()) _failed = true return _finish() return false func _new_game() -> void: _game.hud.close_modal() _game.settings.botDelay = 0.0 _game.start_game({"seed": _seed + _played, "human": _played % 2, "style": "greedy"}) Util.errors.clear() func _pick(_m: LocalMatch) -> void: _moves += 1 var moves: Array = _game.moves if moves.is_empty(): return var others := moves.filter(func(mv): return mv.action.type not in ["end", "pass"]) var pick: Dictionary if not others.is_empty() and _rng.randf() < 0.65: pick = others[_rng.randi() % others.size()] else: var stop := moves.filter(func(mv): return mv.action.type in ["end", "pass"]) pick = stop[0] if not stop.is_empty() else moves[_rng.randi() % moves.size()] _game._play(pick) func _finish() -> bool: var secs := (Time.get_ticks_msec() - _started) / 1000.0 print("present smoke: %d game(s), %d of your moves, %d events shown, %d frames, %.1fs" % [_played, _moves, _events, _frames, secs]) quit(1 if _failed else 0) return false