extends SceneTree ## Plays games through the game scene, headless, with you driven by the greedy bot. Whenever it ## would attack, it does so by clicking instead: the attacker, then the middle of the target ## location, where the defenders' cards stand. Checks that the click attacks. ## ## godot --headless --script res://tests/attack_click.gd -- [--games N] [--seed N] var _game: Node var _games := 4 var _seed := 7 var _played := 0 var _hb := GreedyBot.new(GreedyBot.STYLES.greedy.params) var _wait := -1 var _attacks := 0 var _on_defender := 0 var _missed := 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]) i += 1 Card3D.instant = true _game = load("res://scenes/game.tscn").instantiate() root.add_child.call_deferred(_game) func _process(_d: float) -> bool: if _game == null or not _game.is_node_ready(): return false if not _began: _began = true _new_game() return false if Engine.get_physics_frames() < _wait: return false var m: LocalMatch = _game.match_ for n in 400: if not Util.errors.is_empty(): print("seed %d: engine error: %s" % [m.setup.seed, "; ".join(Util.errors)]) return _finish(true) if m.is_over(): _played += 1 if _played >= _games: return _finish(false) _new_game() return false if m.bot_to_act(): m.bot_step() continue # Let physics catch up with the table before clicking it. if _wait < 0: _wait = Engine.get_physics_frames() + 2 return false _wait = -1 var a: Dictionary = _hb.choose(RulesEngine.view_for(m.s, m.human), m.legal(), m.human) if a.type == "attack": _click_attack(m, a) else: m.act(a) return false return false func _click_attack(m: LocalMatch, a: Dictionary) -> void: var table: GameTable = _game.table var mv: Dictionary = _game.moves.filter(func(x): return x.action == a)[0] _attacks += 1 _game._select(mv.source) var sp := table.camera.unproject_position(BoardLayout.at[a.to] + Vector3(0, 0.01, 0)) var key := table.key_at(sp) if key.begins_with("anomaly:"): _on_defender += 1 var before := m.actions.size() _game._pick(key, sp) if _game.hud.popover_open(): _game.hud.hide_popover() if m.actions.size() == before: _missed += 1 print("seed %d: clicking %s (at %s's middle) didn't attack" % [m.setup.seed, key, a.to]) m.act(a) func _new_game() -> void: _game._paused = true _game.hud.close_modal() _game.start_game({"seed": _seed + _played, "human": _played % 2, "style": "greedy"}) Util.errors.clear() func _finish(failed: bool) -> bool: print("attack click: %d game(s), %d attacks clicked (%d on a defender's card), %d missed" % [_played, _attacks, _on_defender, _missed]) quit(1 if failed or _missed > 0 else 0) return false