N4: Lag-compensated hit validation

A network client draws other players in the past and its commands take time
to arrive, so by the time the server fires a shot the target has moved on:
8 ticks at a 50 ms round trip, 12 at 200 ms.

Each PlayerCommand now carries view_lag: how far back its player was seeing
everyone else, in quarter ticks. The simulation keeps where every player's
body has been for the last 15 ticks (HitHistory), and a hitscan shot is
traced against other players where they were at the moment the shooter saw,
and against the world as it is now. Projectiles aren't rewound.

The client claims only when it was looking and where it aimed; the server
works out the hit from its own record. The moment is never more than
Tuning.LAG_COMPENSATION_MAX_TICKS back, never in the future, and never
earlier than a moment the same player has already claimed.

Commands are 15 bytes, so the command log's version is 2. The lag is in the
log, and replays judge every shot as the match did.

Measured in test/net/test_lag_compensation_link.gd over a simulated link:
a client firing at a walking target as drawn on its own screen hits 41 of
41 shots at 50, 100 and 200 ms round trips, 0 of 41 with the lag ignored,
and 0 of 41 at 500 ms, past the cap.

Closes #35

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-12 11:45:23 -05:00
co-authored by Claude Opus 5.5
parent 4111a285f0
commit 5e1704e224
16 changed files with 510 additions and 15 deletions
+4
View File
@@ -165,6 +165,10 @@ func _input_for(tick: int) -> PlayerCommand:
command.yaw = _last_yaw
command.pitch = _last_pitch
command.tick = tick
if remote.started:
# How far back everyone else is being drawn right now, for the server to judge
# shots by.
command.view_lag = clampi(roundi((float(tick) - remote.time) * PlayerCommand.VIEW_LAG_STEPS_PER_TICK), 0, 255)
_last_yaw = command.yaw
_last_pitch = command.pitch
return command
+1 -1
View File
@@ -17,7 +17,7 @@
## then the fields of each group in the mask
## client -> INPUT (channel 1, unreliable)
## u8 type, u32 tick of the newest snapshot it holds (0 = send a full one),
## u8 command count, then that many 14-byte PlayerCommands, oldest first
## u8 command count, then that many 15-byte PlayerCommands, oldest first
## server -> MATCH (channel 0, reliable)
## u8 type, u8 match phase, u32 match index, s64 match seed
##