Skip to content

feat: add netcode ticks, input sequencing and reconciliation - #145

Merged
Timerk merged 5 commits into
mainfrom
feat/netcode-ticks
Nov 26, 2026
Merged

Timerk merged 5 commits into
mainfrom
feat/netcode-ticks

Conversation

@Timerk

@Timerk Timerk commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Phase 2 of 6 of the real-time netcode rework (GAME_PLAN.md, "Real-time netcode rework").

Depends on #144 (phase 1, network simulator) and targets its branch feat/network-simulator. Merge #144 first, then retarget this PR to main.

Behaviour

  • Ticks. The server numbers its 60 Hz simulation ticks; world snapshots carry the tick in their header.
  • Inputs. Clients sample one input per physics tick: a per-connection sequence number, the client tick, quantized movement (i8 per axis), boost and the requested facing (u16 yaw, i16 pitch), plus the latest fire/encounter/target intent. They predict with exactly the quantized values. Every second tick (30 packets/s) one unsequenced packet carries the last ten unacknowledged inputs (102 bytes), so each input travels in up to five packets: 5% random loss and a three-packet burst lose nothing. command_flight is replaced by flight_inputs(PackedByteArray); NETWORK_SCHEMA is 24.
  • Server input buffer (InputBuffer, per pilot). Deduplicates by sequence, accepts reordering and consumes exactly one input per server tick through Pilot.simulate_input(), which keeps the 720°/s turn limit. The server never reads a client position or velocity, and all life, docking, death, map and jump checks stay.
    • Starvation: with nothing buffered, the tick reuses the last input without advancing the sequence, so the ship holds its course through a late packet. Each starved tick delays the inputs behind it by a tick, so the buffer grows to cover the jitter it sees. After TIMEOUT_TICKS (30) the pilot is idle (no thrust, facing held) and brakes. This replaces the wall-clock COMMAND_TIMEOUT, and fire intent also ages in server ticks.
    • Catch-up: if the smallest depth over a 2 s window exceeds SPARE_DEPTH (2), the oldest surplus inputs are dropped. Depth is capped at MAX_DEPTH (12 ticks, 200 ms).
    • Counters per connection: applied, duplicates, late, lost (gaps never applied), dropped (catch-up) and starved. They go to the owner in each snapshot, so phase 5 can assert "no lost inputs".
  • Ack. Each owner entry carries the last processed input, the buffer depth, the turn budget and the counters.
  • Reconciliation (FlightPrediction). On each new owner snapshot, the next client physics tick drops acknowledged inputs, resets position, velocity, energy, simulation facing and turn budget to the authoritative state, and replays the rest through the server's step, including move_and_slide() collisions. The body moves with the simulation facing on both sides, so station collisions replay the same contacts. The view facing (rotation) is never rewound, so mouse steering stays immediate; only the turn-limited simulation facing used for thrust follows the server.
  • Visual smoothing. Corrections become a world-space offset on the model and camera that decays with FlightPrediction.SMOOTHING_TIME = 0.12 s and snaps above SNAP_DISTANCE = 10 m. Respawn, jump, dock, launch and connect reset it. Exhaust plumes track the drawn position. Remote ships and aliens keep their current interpolation (phase 3).
  • ENet throttle. ENet's default throttle deceleration discarded unreliable packets at the sender at high, steady latency: with steady added delay its round-trip variance is tiny, so frame-timing noise kept decelerating it. Every server/client peer now uses throttle_configure(5000, 2, 0) (FlightSession.ENET_THROTTLE). Reliable traffic keeps ENet's windowing and resends.
  • F8 overlay / NetStats. Unacknowledged and replayed inputs, ack lag in ticks, server buffer depth, server lost/late/duplicate/starved counts, the simulation error at reconciliation and the visual offset. The server ghost still marks the authoritative position.

Playtesting settings: FlightPrediction.SMOOTHING_TIME, SNAP_DISTANCE, SEND_INTERVAL_TICKS; FlightInput.REDUNDANCY; InputBuffer.SPARE_DEPTH, CATCH_UP_WINDOW, MAX_DEPTH, TIMEOUT_TICKS.

Before / after (tests/netcode_benchmark.gd, same scripted flight)

Three metrics changed meaning (explained in docs/netcode-baseline.md and the benchmark header). Error is now the simulation error at reconciliation, not the distance to a round-trip-old position. Correction is now the visible offset, not the 20% lerp jump. Lost now counts per-tick inputs.

Added RTT Loss Server to client before → after Snapshot jitter p95 before → after Error max before → after Correction (visible) max before → after Gap to server now before → after Lost before → after
0 ms 0% 13.6 → 14.4 KB/s 16.0 → 16.3 ms 0.87 → 0.00 m 0.17 → 0.00 m 0.20 → 2.00 m 0 of 400 commands → 0 of 1201 inputs
100 ms 0% 14.0 → 14.9 KB/s 13.7 → 10.1 ms 0.81 → 0.01 m 0.16 → 0.01 m 1.55 → 2.99 m 1 of 401 → 0 of 1200
200 ms 0% 6.3 → 14.4 KB/s 156.8 → 12.1 ms 2.78 → 0.00 m 0.56 → 0.00 m 3.26 → 4.59 m 139 of 400 (34.8%) → 0 of 1200
0 ms 5% 13.6 → 15.0 KB/s 45.9 → 22.9 ms 0.79 → 0.00 m 0.16 → 0.00 m 0.22 → 2.90 m 15 of 400 → 0 of 1200
100 ms 5% 14.4 → 14.4 KB/s 30.6 → 18.8 ms 1.57 → 1.21 m 0.31 → 1.05 m (smoothed) 1.55 → 4.45 m 11 of 400 → 0 of 1200
200 ms 5% 10.6 → 14.4 KB/s 64.1 → 48.8 ms 1.42 → 0.62 m 0.28 → 0.54 m (smoothed) 3.05 → 5.75 m 101 of 400 (25.3%) → 0 of 1200
150 ± 30 ms 5% n/a → 14.4 KB/s n/a → 33.1 ms n/a → 0.00 m n/a → 0.00 m n/a → 4.96 m n/a → 0 of 1200

Error and correction p95 are 0.00 m in every after row. The remaining maxima are single catch-up drops or starved ticks, one tick of motion each, hidden over about 0.3 s. Client upload rose from 1.1 to 3.5 KB/s for the redundant 30 Hz packets. The gap grows by design: the local ship now leads the server by the upstream delay plus 2–5 buffered ticks, instead of being dragged back toward old snapshots.

ENet throttle, same phase 2 build at 200 ms with 5% loss: with ENet's default deceleration the server sent 10.5 KB/s (−27%), snapshot jitter p95 was 127 ms, sim error max 2.16 m and the server starved 11 ticks. With deceleration 0: 14.4 KB/s, 49 ms, 0.62 m and 1 tick. Full tables are in docs/netcode-baseline.md.

Validation

  • ./tools/dev.ps1 check (Windows suite): passed (exit 0, 1011 s, every group including the new netcode_reconciliation).
  • New tests, in both the Windows and Linux lists (netcode_reconciliation group):
    • tests/input_buffer_test.gd: duplicates, reordering, redundancy with 5% loss plus three-packet bursts (0 lost), gaps and late arrivals, starvation reuse, tick timeout, catch-up to the spare depth, the MAX_DEPTH and flood bounds, life resets, packet round trips and malformed packets.
    • tests/reconciliation_test.gd: a scripted client through the relay. On a clean link sim error p95 is 0.004 m and max 0.035 m, with no lost inputs. Flying into Outpost 01's collision envelope (81 colliding server ticks) gives max 0.0013 m. At 150 ± 30 ms with 5% loss, p95 is 0.005 m and max 0.63 m, with 0 lost and 0 late inputs. Bounds: p95 ≤ 0.02 m clean and ≤ 0.25 m degraded, max ≤ 1.5 m and ≤ 3.0 m. A client-side teleport does not move the server ship and is undone. An instant 180° flip never exceeds the per-tick turn limit and still converges.
  • Updated tests that forged command_flight to use FlightSession.send_command(), which builds a real input packet. The non-finite command check became malformed-packet and non-finite-value checks. The slow-frame fire-timeout regression in alien_sector_test now advances server ticks, because intents age in ticks.
  • Linux suites run in CI only (no WSL distro on this machine).
  • Windowed run: a local dedicated server plus a real windowed client (rendered, 132–140 fps) through the relay at 150 ± 30 ms with 5% loss, flying a scripted pattern with F8 open. Over 12.5 s: 60.1 client ticks/s, 0 lost inputs, 1 starved tick, sim error max 0.38 m, visual offset max 0.33 m, replays of 15–17 ticks and a server buffer of about 6. The overlay renders every new reading within its 356 px layout, and the ghost trails the predicted ship at the authoritative position. Flight inputs were scripted rather than hand-flown. The user's desktop was in active use, so I did not take foreground keyboard control. Controls are prediction-driven every tick, so input response no longer depends on latency; hands-on feel still needs a playtest. Screenshots stay in the local scratchpad, not the repo.

Known limitations (for phases 3–5)

  • Client tick-rate shortfalls starve the server. A client that misses ticks (frame stalls beyond Godot's 8 physics steps per frame) sends fewer than 60 inputs per second. Each missing tick becomes a starved server tick and a correction of one tick of motion. In the windowed run, GPU readback stalls from taking screenshots caused 18 starved ticks and up to 5.5 m of correction, smoothed. Phase 5 could add a client tick-rate adjustment.
  • Server input latency settles at 2–5 ticks (33–83 ms) with loss. That latency is what lets inputs survive loss and jitter. It does not affect local feel, but it adds to what remote players see (phase 3). SPARE_DEPTH and CATCH_UP_WINDOW are the tuning knobs.
  • Turning leaves float-rounding sim error of a few millimetres; straight flight replays exactly. Energy and turn budget travel as float32.
  • Remote pilots and aliens still lerp toward the latest snapshot as before (phase 3). Snapshot encoding is unchanged apart from about 33 bytes of ack fields in the owner entry (phase 4).

🤖 Generated with Claude Code

Timerk and others added 5 commits November 26, 2026 20:21
FlightInput quantizes and packs a client's recent per-tick inputs (sequence,
movement, boost, requested facing) with fire and targeting intent.
InputBuffer orders and deduplicates them per pilot, consumes one per server
tick, reuses the last input while starved, idles after a tick timeout and
drops surplus depth to bound latency, counting applied, duplicate, late,
lost, dropped and starved inputs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The server numbers its 60 Hz simulation ticks and consumes exactly one
buffered input per remote pilot per tick through Pilot.simulate_input(),
keeping the 720°/s turn limit and every life, docking and map check. Fire
intent ages in server ticks instead of wall-clock time. Snapshots carry the
server tick; owner entries carry the last processed input, buffer depth,
turn budget and input counters (network schema 24).

Clients sample one input per physics tick, predict it with the same step,
send the last ten unacknowledged inputs 30 times per second and, on each
owner snapshot, rewind to the authoritative state and replay the rest. A
decaying render offset on the model and camera hides corrections; the view
facing is never rewound. The F8 overlay shows unacknowledged and replayed
inputs, ack lag, server buffer depth, input counters, simulation error and
the visual offset, and the benchmark reports them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ENet's default throttle deceleration discarded 25-35% of unreliable
commands and snapshots at 200 ms round trips: with steady added delay its
round-trip variance is tiny, so frame-timing noise kept decelerating it.
Clients and the server configure every server/client peer with deceleration
0. Reliable traffic keeps ENet's windowing and resends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The input buffer test covers duplicates, reordering, redundancy under 5%
loss and three-packet bursts, gaps, starvation, catch-up, the tick timeout,
resets and packet round trips. The reconciliation test flies a pilot through
the network simulator: replays match the server on a clean link and through
station collisions, corrections stay bounded at 150 ± 30 ms with 5% loss
with no lost inputs, client teleports never move the server ship, and an
instant 180° flip stays within the turn limit. Both run in the Windows and
Linux suites.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Timerk
Timerk force-pushed the feat/netcode-ticks branch from 8a63bb0 to 28d2af0 Compare November 26, 2026 19:21
@Timerk
Timerk changed the base branch from feat/network-simulator to main November 26, 2026 19:21
@Timerk
Timerk merged commit dc86a42 into main Nov 26, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant