N10: Playable network client

The client program now turns a connection into a match someone can play.

- ClientSession: a ClientGame, the keyboard and mouse as commands
  (ClientInput), and a MatchView of the client's own copy of the world.
- ClientMirror writes what the client is showing into that world every
  frame: other players from the recent past, its own player where it was
  predicted, items, the barge, the clock and the sea. The existing views
  draw it unchanged.
- LocalServer: offline play is the same ServerLoop and NetGame in the same
  process, with packets handed across in memory, so there is one path.
- main.gd: --connect=HOST[:PORT] joins a server, otherwise an offline
  match; --autoplay and --client-seconds=N for scripts and checks.
- The camera aboard the barge follows the player's own place on the deck.

Tests drive a scripted client through a whole match on a real server over
loopback UDP, lobby to results, and through an offline match. `make
client-check` does it with two real processes, and `make client-captures`
renders what the client sees.

Closes #77

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-12 14:50:52 -05:00
co-authored by Claude Opus 5.5
parent 6b21e9d4f1
commit 9a36ae8615
25 changed files with 1089 additions and 13 deletions
+131
View File
@@ -0,0 +1,131 @@
## Turns the keyboard and mouse into the local player's commands, one per tick.
##
## Keys (physical positions, so they sit the same on any layout):
## W A S D move · Space jump · Ctrl crouch · Shift sprint · left mouse use or fire ·
## right mouse aim · R reload · E pick up or open · G drop the held item ·
## 1 the boathook, 2 to 6 the five slots · B build mode on and off ·
## in build mode 1 to 5 pick wall, floor, ramp, roof or breakwater, Tab the material,
## and left mouse places.
## Rebinding and gamepads come with the settings menu.
##
## With `autoplay` on, a simple script plays instead: it leaps from the barge over
## land, glides, and walks about. That is for tests and for checking a connection with
## nobody at the keyboard.
class_name ClientInput
extends RefCounted
var settings: InputSettings = InputSettings.new()
## The look direction, in radians, in the simulation's convention.
var yaw: float = 0.0
var pitch: float = 0.0
## What is held down now: Vector2(strafe right, forward) and PlayerCommand.BUTTON_ bits.
var move: Vector2 = Vector2.ZERO
var buttons: int = 0
## The held selection outside build mode: 0 the boathook, 1 to 5 a slot.
var slot: int = 0
var build_mode: bool = false
## The piece kind and material placed in build mode.
var build_kind: int = StructureSet.WALL
var material: int = Materials.DRIFTWOOD
var autoplay: bool = false
func aiming() -> bool:
return buttons & PlayerCommand.BUTTON_AIM != 0 and not build_mode
## Reads what is held down from the keyboard and mouse. Call once a frame.
func poll() -> void:
if autoplay:
return
move = Vector2(
_axis(KEY_D, KEY_A),
_axis(KEY_W, KEY_S),
)
buttons = 0
var keys: Dictionary = {
KEY_SPACE: PlayerCommand.BUTTON_JUMP, KEY_CTRL: PlayerCommand.BUTTON_CROUCH,
KEY_SHIFT: PlayerCommand.BUTTON_SPRINT, KEY_R: PlayerCommand.BUTTON_RELOAD,
KEY_E: PlayerCommand.BUTTON_INTERACT, KEY_G: PlayerCommand.BUTTON_DROP,
}
for key: Key in keys:
if Input.is_physical_key_pressed(key):
buttons |= int(keys[key])
if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
buttons |= PlayerCommand.BUTTON_FIRE
if Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT):
buttons |= PlayerCommand.BUTTON_AIM
## Takes one input event: mouse motion turns the look, and keys that act once per
## press do. `captured` says whether the mouse belongs to the game just now.
func handle(event: InputEvent, captured: bool) -> void:
if autoplay:
return
var motion: InputEventMouseMotion = event as InputEventMouseMotion
if motion != null and captured:
turn(motion.relative)
return
var key: InputEventKey = event as InputEventKey
if key != null and key.pressed and not key.echo:
press(key.physical_keycode)
## Turns the look by a mouse motion, in pixels.
func turn(relative: Vector2) -> void:
var look: Vector2 = settings.apply_mouse(relative, yaw, pitch, aiming())
yaw = look.x
pitch = look.y
## A key that acts once per press: the number row, build mode and the material.
func press(keycode: Key) -> void:
if keycode == KEY_B:
build_mode = not build_mode
elif keycode == KEY_TAB and build_mode:
material = (material + 1) % Materials.NAMES.size()
elif keycode >= KEY_1 and keycode <= KEY_9:
var number: int = keycode - KEY_1
if build_mode:
if number < StructureSet.KINDS.size():
build_kind = number
elif number <= Inventory.slot_count():
slot = number
## The command for `tick`, from what is held now.
func command(tick: int) -> PlayerCommand:
var pressed: int = buttons
var selection: int = slot
if build_mode:
pressed |= PlayerCommand.BUTTON_BUILD
selection = build_kind
return PlayerCommand.from_input(tick, move, yaw, pitch, pressed, selection, material)
## The script's command for `tick`: `me` is the client's copy of its own player and
## `ground` the island under it.
func autoplay_command(tick: int, me: SimEntity, ground: SimGround) -> PlayerCommand:
var centre: float = Tuning.WORLD_SIZE_METRES * 0.5
yaw = atan2(centre - me.position.x, centre - me.position.z)
pitch = deg_to_rad(-12.0)
move = Vector2.ZERO
buttons = 0
if me.drop_state == Drop.ABOARD:
# Over dry land, with a moment to look about first.
if tick > 3 * Tuning.TICK_RATE_HZ and ground.terrain_height(me.position.x, me.position.z) > Tuning.ISLAND_BEACH_TOP + 2.0:
buttons = PlayerCommand.BUTTON_JUMP
elif me.drop_state != Drop.NONE:
move = Vector2(0.0, 1.0)
else:
# Walk a slow circle about the middle of the island, with a hop now and then.
var seconds: float = float(tick) / Tuning.TICK_RATE_HZ
yaw += sin(seconds * 0.4) * 1.2
move = Vector2(0.0, 1.0) if fmod(seconds, 9.0) < 6.0 else Vector2.ZERO
if tick % (4 * Tuning.TICK_RATE_HZ) == 0:
buttons = PlayerCommand.BUTTON_JUMP
return command(tick)
func _axis(positive: Key, negative: Key) -> float:
return (1.0 if Input.is_physical_key_pressed(positive) else 0.0) - (1.0 if Input.is_physical_key_pressed(negative) else 0.0)
+1
View File
@@ -0,0 +1 @@
uid://x752jdh6pmnn
+4 -1
View File
@@ -25,6 +25,9 @@ const DECK_ROWS: int = 4
## The entity drawn in the brightest oilskin, or 0.
var local_id: int = 0
## Where to draw the barge, when its owner knows better than the simulation's tick: a
## network client, which draws between ticks. Vector3.INF leaves it to the tick.
var barge_at: Vector3 = Vector3.INF
var _sim: Simulation
## Entity ID -> the figure's root node.
@@ -70,7 +73,7 @@ func sync() -> void:
heading = atan2(along.x, along.z)
_barge.visible = crossing
if crossing:
_barge.position = drop.barge_position(_sim.tick)
_barge.position = barge_at if barge_at != Vector3.INF else drop.barge_position(_sim.tick)
_barge.rotation.y = heading
var seen: Dictionary = {}
+14 -6
View File
@@ -2,8 +2,9 @@
## built pieces and everything that moves, all drawn from one Simulation, with the
## third-person camera on a player's shoulder. It only reads the simulation.
##
## A match gets a new Simulation, so it gets a new MatchView. Until snapshots arrive
## (v0.5) the simulation it reads is the local server's or a replay's.
## A match gets a new Simulation, so it gets a new MatchView. In play the simulation is
## the client's own copy of the world, which ClientMirror fills with what the server
## has sent; captures and tests also point it at a server's or a replay's.
class_name MatchView
extends Node3D
@@ -86,14 +87,21 @@ func sync() -> void:
## Puts the camera on a player's shoulder, looking where they look. `pitch` replaces
## their own pitch when given, in radians, for views such as looking down from a glide.
func follow(entity_id: int, pitch: float = NAN) -> bool:
## their own pitch when given, in radians, for views such as looking down from a glide,
## and `look_yaw` their yaw, for the local player, whose look is newer than any command.
func follow(entity_id: int, pitch: float = NAN, look_yaw: float = NAN) -> bool:
var player: SimEntity = sim.entities.find(entity_id)
if player == null:
return false
var yaw: float = float(player.yaw) / PlayerCommand.YAW_STEPS * TAU
var yaw: float = look_yaw if not is_nan(look_yaw) else float(player.yaw) / PlayerCommand.YAW_STEPS * TAU
var look_pitch: float = pitch if not is_nan(pitch) else float(player.pitch) / PlayerCommand.PITCH_MAX * (PI * 0.5)
camera.transform = rig.solve(player.position, yaw, look_pitch, player.crouching, sim.ground)
# Where the figure is drawn: on the barge's deck that is its own place, not the
# one point the simulation carries every passenger at.
var feet: Vector3 = player.position
var figure: Node3D = entities.figure_of(entity_id)
if figure != null and player.drop_state == Drop.ABOARD:
feet = figure.position
camera.transform = rig.solve(feet, yaw, look_pitch, player.crouching, sim.ground)
camera.fov = rig.fov()
_focus()
return true