## A headless dedicated server. It runs the match at the real tick rate, or back to back ## with `--unthrottled`, and writes structured logs: one JSON object per line on stdout, ## each carrying the match seed so any run can be reproduced. ## ## Exits 0 after a clean run, 1 if any engine or script error happened, and 2 if the ## command line was invalid. class_name HeadlessServer extends Node ## Catch-up limit from docs/ARCHITECTURE.md. const MAX_TICKS_PER_FRAME: int = 5 ## Ticks per frame when unthrottled. Keeps the engine's main loop responsive. const UNTHROTTLED_BATCH: int = 250 ## Engine frame cap in real-time mode, so an idle server doesn't spin a core. const REALTIME_MAX_FPS: int = 120 const FAULT_TICK: int = 10 var config: ServerConfig var loop: ServerLoop var _errors: ErrorCounter = ErrorCounter.new() var _accumulator: float = 0.0 var _started_usec: int = 0 var _finished: bool = false func _init(server_config: ServerConfig) -> void: config = server_config func _ready() -> void: OS.add_logger(_errors) _started_usec = Time.get_ticks_usec() if not config.is_valid(): for problem: String in config.errors: _log("config_error", {"message": problem}) _finish(2) return if not config.has_seed: config.match_seed = _fresh_seed() Engine.max_fps = 0 if config.unthrottled else REALTIME_MAX_FPS loop = ServerLoop.new(config.match_seed, config.players, config.bots) var started: Dictionary = config.to_dictionary() started["engine"] = Engine.get_version_info()["string"] started["tick_rate_hz"] = Tuning.TICK_RATE_HZ started["world_hash"] = loop.sim.island.heightmap_hash() started["match_phase"] = loop.match_flow.phase_name() started["listening"] = false started["note"] = "networking arrives in v0.5; players join as idle slots until then" _log("start", started) func _process(delta: float) -> void: if _finished or loop == null: return var due: int = UNTHROTTLED_BATCH if not config.unthrottled: _accumulator += delta due = mini(floori(_accumulator * Tuning.TICK_RATE_HZ), MAX_TICKS_PER_FRAME) _accumulator = maxf(_accumulator - float(due) / Tuning.TICK_RATE_HZ, 0.0) if due == MAX_TICKS_PER_FRAME: # Too far behind to catch up: drop the backlog rather than spiral. _accumulator = minf(_accumulator, 1.0 / Tuning.TICK_RATE_HZ) for _index: int in due: if config.ticks > 0 and loop.ticks >= config.ticks: break _tick() if config.ticks > 0 and loop.ticks >= config.ticks: _finish(0) func _exit_tree() -> void: # Stopped from outside, for example by SIGTERM: still report what happened. if not _finished and loop != null: _finish(0) func _tick() -> void: if config.fault == "script-error" and loop.ticks == FAULT_TICK: _deliberate_script_error() loop.run_tick() for event: Dictionary in loop.match_flow.events: if event["type"] in ["phase", "match_started", "match_ended"]: _log("match", event) elif event["type"] == "placed": _log("eliminated", event) if event["type"] == "match_ended": _log("results", {"match": event["match"], "summary": loop.match_flow.summary()}) if config.log_every > 0 and loop.ticks % config.log_every == 0: var stats: Dictionary = loop.timing_stats() _log("progress", {"tick": loop.ticks, "p95_us": stats["p95_us"], "max_us": stats["max_us"]}) func _finish(requested_code: int) -> void: _finished = true var code: int = requested_code if loop != null: var stats: Dictionary = loop.timing_stats() stats["state_hash"] = loop.sim.state_hash() stats["entities"] = loop.sim.entities.count() _log("stats", stats) if code == 0 and _errors.count() > 0: code = 1 var stopped: Dictionary = { "exit_code": code, "errors": _errors.count(), "wall_ms": roundi((Time.get_ticks_usec() - _started_usec) / 1000.0), } if _errors.count() > 0: stopped["first_error"] = _errors.first() _log("stop", stopped) OS.remove_logger(_errors) if is_inside_tree(): get_tree().quit(code) func _log(event: String, fields: Dictionary) -> void: var record: Dictionary = { "t_ms": roundi((Time.get_ticks_usec() - _started_usec) / 1000.0), "event": event, "seed": config.match_seed, } if loop != null: record["tick"] = loop.ticks record.merge(fields, false) print(JSON.stringify(record)) ## A non-negative 63-bit seed. Only the server picks seeds; the simulation never does. static func _fresh_seed() -> int: var rng: RandomNumberGenerator = RandomNumberGenerator.new() rng.randomize() return (rng.randi() << 31 | rng.randi()) & 0x7FFFFFFFFFFFFFFF ## Calls a method on a null object, the most common real script error. func _deliberate_script_error() -> void: var missing: Object = null missing.call("deliberate_fault")