The simulation now names what ended each player: weapon, fall, drown or tide, from the cause of the blow that took the last health. Hits carry the weapon's item code. Match keeps a kill feed (eliminator, cause, weapon, tick), per-player stats (damage dealt and taken between opponents, eliminations, never teammates), and a summary() with place, eliminations, damage and survival time. Players who fall on the same tick share the better place and are marked tied. When the last players fall together, the tie-break raises one team to first and the rest share the next place. Eliminated players spectate: first whoever knocked them out, then a living teammate, then the next living player in join order. When a target falls or leaves, the view follows its eliminator or moves on, on the same step. Spectators can cycle forwards and back. In results everyone watches the winner. The headless server logs every placement and the match summary. Closes #26 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
140 lines
4.6 KiB
GDScript
140 lines
4.6 KiB
GDScript
## 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")
|