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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
##
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
## the simulation takes nothing else in. See MatchReplay.
|
||||
##
|
||||
## Each record is the simulation tick the command arrived on, the handle it came from
|
||||
## and the command's own 14 bytes. Commands are logged whether or not the simulation
|
||||
## and the command's own 15 bytes. Commands are logged whether or not the simulation
|
||||
## accepted them, so a replay turns the same ones down.
|
||||
##
|
||||
## On disk: "TWCL", a u32 header length, the header as JSON, a u32 length of the raw
|
||||
@@ -13,7 +13,7 @@ class_name CommandLog
|
||||
extends RefCounted
|
||||
|
||||
const MAGIC: String = "TWCL"
|
||||
const VERSION: int = 1
|
||||
const VERSION: int = 2
|
||||
## at-tick u32, handle s32, then the command.
|
||||
const RECORD_SIZE: int = 8 + PlayerCommand.ENCODED_SIZE
|
||||
## Refuse files that claim more than this many raw bytes: about four full matches.
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
## Where every player's body has been over the last few ticks, so a shot can be judged
|
||||
## against the moment the shooter saw. See "Lag compensation" in Weapons.
|
||||
##
|
||||
## note() stores a player's state before a tick runs, which is what a snapshot of that
|
||||
## tick carries, so the moment a client was drawing and the moment looked up here are on
|
||||
## the same clock.
|
||||
class_name HitHistory
|
||||
extends RefCounted
|
||||
|
||||
## Ticks kept per player: as far back as compensation goes, and the tick under way.
|
||||
const SIZE: int = Tuning.LAG_COMPENSATION_MAX_TICKS + 2
|
||||
|
||||
|
||||
class Track:
|
||||
extends RefCounted
|
||||
var ticks: PackedInt32Array = PackedInt32Array()
|
||||
var positions: PackedVector3Array = PackedVector3Array()
|
||||
var heights: PackedFloat32Array = PackedFloat32Array()
|
||||
|
||||
func _init() -> void:
|
||||
ticks.resize(SIZE)
|
||||
ticks.fill(-1)
|
||||
positions.resize(SIZE)
|
||||
heights.resize(SIZE)
|
||||
|
||||
|
||||
## Player entity ID -> Track.
|
||||
var _tracks: Dictionary = {}
|
||||
|
||||
|
||||
## Stores where `player` is before `tick` runs.
|
||||
func note(tick: int, player: SimEntity) -> void:
|
||||
var track: Track = _tracks.get(player.id)
|
||||
if track == null:
|
||||
track = Track.new()
|
||||
_tracks[player.id] = track
|
||||
var slot: int = tick % SIZE
|
||||
track.ticks[slot] = tick
|
||||
track.positions[slot] = player.position
|
||||
track.heights[slot] = Movement.body_height(player)
|
||||
|
||||
|
||||
func forget(player_id: int) -> void:
|
||||
_tracks.erase(player_id)
|
||||
|
||||
|
||||
## The box `player`'s body occupied at `time`, in ticks, between the two ticks around
|
||||
## it. Where nothing was stored for that moment, the nearest stored tick stands in, and
|
||||
## failing that the player as they are now.
|
||||
func box_at(player: SimEntity, time: float) -> AABB:
|
||||
var track: Track = _tracks.get(player.id)
|
||||
if track == null:
|
||||
return Weapons.body_box(player)
|
||||
var from: int = floori(time)
|
||||
var weight: float = time - from
|
||||
var a: int = from % SIZE if from >= 0 else -1
|
||||
var b: int = (from + 1) % SIZE
|
||||
var has_a: bool = a >= 0 and track.ticks[a] == from
|
||||
var has_b: bool = weight > 0.0 and track.ticks[b] == from + 1
|
||||
var feet: Vector3
|
||||
var height: float
|
||||
if has_a and has_b:
|
||||
feet = track.positions[a].lerp(track.positions[b], weight)
|
||||
height = track.heights[a] if weight < 0.5 else track.heights[b]
|
||||
elif has_a:
|
||||
feet = track.positions[a]
|
||||
height = track.heights[a]
|
||||
elif has_b:
|
||||
feet = track.positions[b]
|
||||
height = track.heights[b]
|
||||
else:
|
||||
return Weapons.body_box(player)
|
||||
var radius: float = Tuning.PLAYER_RADIUS
|
||||
return AABB(feet - Vector3(radius, 0.0, radius), Vector3(radius * 2.0, height, radius * 2.0))
|
||||
@@ -0,0 +1 @@
|
||||
uid://c2l66rp08rbww
|
||||
@@ -3,7 +3,7 @@
|
||||
class_name PlayerCommand
|
||||
extends RefCounted
|
||||
|
||||
const ENCODED_SIZE: int = 14
|
||||
const ENCODED_SIZE: int = 15
|
||||
|
||||
const BUTTON_FIRE: int = 1 << 0
|
||||
const BUTTON_AIM: int = 1 << 1
|
||||
@@ -19,6 +19,8 @@ const BUTTON_DROP: int = 1 << 8
|
||||
const AXIS_MAX: int = 127
|
||||
const YAW_STEPS: int = 65536
|
||||
const PITCH_MAX: int = 32767
|
||||
## view_lag counts in steps of this fraction of a tick.
|
||||
const VIEW_LAG_STEPS_PER_TICK: int = 4
|
||||
|
||||
var tick: int = 0
|
||||
var move_x: int = 0
|
||||
@@ -28,6 +30,11 @@ var pitch: int = 0
|
||||
var buttons: int = 0
|
||||
var selection: int = 0
|
||||
var material: int = 0
|
||||
## How far in the past the player was seeing everyone else when this command was made,
|
||||
## counted back from `tick` in quarter ticks. A network client draws others late, and
|
||||
## says how late here, so the server can judge a shot against what was on its screen.
|
||||
## 0 for bots and anyone playing on the server itself. See Weapons and HitHistory.
|
||||
var view_lag: int = 0
|
||||
|
||||
|
||||
## Builds a command from real-valued input, clamping every field into range.
|
||||
@@ -81,6 +88,7 @@ func encode() -> PackedByteArray:
|
||||
bytes.encode_u16(10, buttons & 0xFFFF)
|
||||
bytes.encode_u8(12, selection & 0xFF)
|
||||
bytes.encode_u8(13, material & 0xFF)
|
||||
bytes.encode_u8(14, clampi(view_lag, 0, 255))
|
||||
return bytes
|
||||
|
||||
|
||||
@@ -98,4 +106,5 @@ static func decode(bytes: PackedByteArray, offset: int = 0) -> PlayerCommand:
|
||||
command.buttons = bytes.decode_u16(offset + 10)
|
||||
command.selection = bytes.decode_u8(offset + 12)
|
||||
command.material = bytes.decode_u8(offset + 13)
|
||||
command.view_lag = bytes.decode_u8(offset + 14)
|
||||
return command
|
||||
|
||||
@@ -58,6 +58,9 @@ var use_ticks: int = 0
|
||||
var bloom: float = 0.0
|
||||
## Buttons held last tick, so pick up and drop act once per press.
|
||||
var prev_buttons: int = 0
|
||||
## The latest moment this player has claimed to be seeing, in quarter ticks. It only
|
||||
## moves forward, so nobody can look further back than they already have. See Weapons.
|
||||
var view_quarters: int = 0
|
||||
## Set once health reaches 0. Eliminated players take no more commands.
|
||||
var eliminated: bool = false
|
||||
## The player who last damaged this one, or 0.
|
||||
|
||||
@@ -50,6 +50,10 @@ var projectiles: Array[int] = []
|
||||
## Each is a Dictionary with a "type". Cleared at the start of every step.
|
||||
var events: Array[Dictionary] = []
|
||||
|
||||
## Where every player has been over the last half second, for judging shots against
|
||||
## what a network client saw. Rebuilt identically by a replay, so not part of the hash.
|
||||
var hit_history: HitHistory = HitHistory.new()
|
||||
|
||||
## Player entity ID -> { command tick -> PlayerCommand }
|
||||
var _pending: Dictionary = {}
|
||||
## How many entries of props.destroyed have been checked for crate loot.
|
||||
@@ -103,6 +107,7 @@ func add_player() -> int:
|
||||
## Removes a player and any commands queued for it.
|
||||
func remove_player(player_id: int) -> void:
|
||||
entities.remove(player_id)
|
||||
hit_history.forget(player_id)
|
||||
_players.erase(player_id)
|
||||
_pending.erase(player_id)
|
||||
|
||||
@@ -172,6 +177,10 @@ func step() -> void:
|
||||
structures.advance_building()
|
||||
if tick % Tuning.TICK_RATE_HZ == Tuning.TICK_RATE_HZ - 1:
|
||||
structures.tide_damage(sea_level, ground, events, flood)
|
||||
for player_id: int in _players:
|
||||
var standing: SimEntity = entities.find(player_id)
|
||||
if not standing.eliminated:
|
||||
hit_history.note(tick, standing)
|
||||
for player_id: int in _players:
|
||||
var queued: Dictionary = _pending[player_id]
|
||||
var command: PlayerCommand = queued.get(tick)
|
||||
@@ -203,6 +212,8 @@ func _apply_command(player: SimEntity, command: PlayerCommand) -> void:
|
||||
Water.update(player, water_level_at(player.position), drown_dps, sprinting)
|
||||
if player.action_cooldown > 0:
|
||||
player.action_cooldown -= 1
|
||||
if command != null:
|
||||
Weapons.note_view(player, command, tick)
|
||||
Weapons.update(player)
|
||||
Consumables.update(self, player, command)
|
||||
var held: int = command.buttons if command != null else 0
|
||||
@@ -343,6 +354,7 @@ func state_hash() -> String:
|
||||
buffer.put_64(roundi(entity.bloom * 1000.0))
|
||||
buffer.put_32(entity.prev_buttons)
|
||||
buffer.put_u8(1 if entity.eliminated else 0)
|
||||
buffer.put_64(entity.view_quarters)
|
||||
buffer.put_32(entity.last_attacker)
|
||||
buffer.put_u8(entity.last_cause)
|
||||
buffer.put_32(entity.item)
|
||||
|
||||
@@ -434,6 +434,10 @@ const WEAPONS: Dictionary = {
|
||||
"spread": 0.0, "bloom": 0.0,
|
||||
},
|
||||
}
|
||||
## A network client sees other players late. The server judges a hitscan shot against
|
||||
## where they were when the shooter saw them, going back at most this many ticks (half
|
||||
## a second). Past that the shooter has to lead the target.
|
||||
const LAG_COMPENSATION_MAX_TICKS: int = 15
|
||||
## Spread multiplier while aiming, unless a weapon sets its own `aim_spread`.
|
||||
const AIM_SPREAD_FACTOR: float = 0.4
|
||||
## Extra spread while moving, and while airborne or swimming, in degrees.
|
||||
|
||||
+33
-6
@@ -10,6 +10,16 @@
|
||||
## air. Hitscan pellets trace at once to the weapon's range; projectile pellets become
|
||||
## entities that fly under gravity and trace each tick's path.
|
||||
##
|
||||
## Lag compensation. A network client draws other players a little in the past, and its
|
||||
## commands take time to arrive, so by the time the server fires a shot the target has
|
||||
## moved on. Each command says how far back the shooter was seeing (view_lag). A hitscan
|
||||
## shot is traced against other players where HitHistory had them at that moment, and
|
||||
## against the world as it is now. Three limits keep that honest: 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. The client says only when
|
||||
## it was looking and where it aimed; whether that hit is the server's to work out.
|
||||
## Projectiles fly in the server's present and aren't rewound.
|
||||
##
|
||||
## A trace stops at the first of terrain, a solid (a built piece, a prop or a point of
|
||||
## interest) or another player's body. Players lose armor first, then health; the top
|
||||
## Tuning.HEAD_HEIGHT of the body takes the weapon's headshot multiplier. Damage falls
|
||||
@@ -79,6 +89,21 @@ static func _finish_reload(player: SimEntity) -> void:
|
||||
player.ammo[index] -= loaded
|
||||
|
||||
|
||||
## Takes a command's word for when its player was seeing the others, within the limits.
|
||||
static func note_view(player: SimEntity, command: PlayerCommand, tick: int) -> void:
|
||||
var steps: int = PlayerCommand.VIEW_LAG_STEPS_PER_TICK
|
||||
var lag: int = clampi(command.view_lag, 0, Tuning.LAG_COMPENSATION_MAX_TICKS * steps)
|
||||
player.view_quarters = maxi(player.view_quarters, tick * steps - lag)
|
||||
|
||||
|
||||
## The moment a player's shots are judged against, in ticks, or -1 for the present.
|
||||
static func view_time(player: SimEntity, tick: int) -> float:
|
||||
var steps: int = PlayerCommand.VIEW_LAG_STEPS_PER_TICK
|
||||
var earliest: int = (tick - Tuning.LAG_COMPENSATION_MAX_TICKS) * steps
|
||||
var quarters: int = maxi(player.view_quarters, earliest)
|
||||
return -1.0 if quarters >= tick * steps else float(quarters) / steps
|
||||
|
||||
|
||||
## Fires the held weapon if it's ready. Returns true if a shot was fired. An empty
|
||||
## magazine starts a reload instead. Thrown weapons are handled by explosives (B8).
|
||||
static func fire(sim: Simulation, player: SimEntity, aiming: bool) -> bool:
|
||||
@@ -99,6 +124,7 @@ static func fire(sim: Simulation, player: SimEntity, aiming: bool) -> bool:
|
||||
var from: Vector3 = Harvest.eye(player)
|
||||
var look: Vector3 = Harvest.look_direction(player)
|
||||
var stream: RandomNumberGenerator = sim.rng.stream("weapons")
|
||||
var seen_at: float = view_time(player, sim.tick)
|
||||
sim.events.append({"type": "shot", "player": player.id, "item": def.code, "from": from, "direction": look})
|
||||
for _pellet: int in def.pellets:
|
||||
var direction: Vector3 = scatter(look, cone, stream)
|
||||
@@ -106,7 +132,7 @@ static func fire(sim: Simulation, player: SimEntity, aiming: bool) -> bool:
|
||||
_launch(sim, player, def, slot, from, direction)
|
||||
else:
|
||||
var to: Vector3 = from + direction * def.range_max
|
||||
var hit: Hit = trace(sim, from, to, player.id)
|
||||
var hit: Hit = trace(sim, from, to, player.id, seen_at)
|
||||
_apply_hit(sim, player.id, def, player.slot_tiers[slot], hit, from.distance_to(hit.point))
|
||||
return true
|
||||
|
||||
@@ -139,14 +165,15 @@ static func scatter(look: Vector3, cone_degrees: float, stream: RandomNumberGene
|
||||
return (look * cos(angle) + offset).normalized()
|
||||
|
||||
|
||||
## The first thing between `from` and `to`, skipping the player `ignore_id`.
|
||||
static func trace(sim: Simulation, from: Vector3, to: Vector3, ignore_id: int = 0) -> Hit:
|
||||
## The first thing between `from` and `to`, skipping the player `ignore_id`. With
|
||||
## `seen_at` set, other players are where they were at that tick; the world is as it is.
|
||||
static func trace(sim: Simulation, from: Vector3, to: Vector3, ignore_id: int = 0, seen_at: float = -1.0) -> Hit:
|
||||
var length: float = from.distance_to(to)
|
||||
var pieces: int = maxi(ceili(length / TRACE_SEGMENT_METRES), 1)
|
||||
for index: int in pieces:
|
||||
var start: float = float(index) / pieces
|
||||
var end: float = float(index + 1) / pieces
|
||||
var hit: Hit = _trace_segment(sim, from.lerp(to, start), from.lerp(to, end), ignore_id)
|
||||
var hit: Hit = _trace_segment(sim, from.lerp(to, start), from.lerp(to, end), ignore_id, seen_at)
|
||||
if hit.kind != HIT_NONE:
|
||||
hit.fraction = lerpf(start, end, hit.fraction)
|
||||
return hit
|
||||
@@ -155,7 +182,7 @@ static func trace(sim: Simulation, from: Vector3, to: Vector3, ignore_id: int =
|
||||
return miss
|
||||
|
||||
|
||||
static func _trace_segment(sim: Simulation, from: Vector3, to: Vector3, ignore_id: int) -> Hit:
|
||||
static func _trace_segment(sim: Simulation, from: Vector3, to: Vector3, ignore_id: int, seen_at: float) -> Hit:
|
||||
var hit: Hit = Hit.new()
|
||||
var ground: SimGround = sim.ground
|
||||
# The nearest solid, and what it belongs to.
|
||||
@@ -186,7 +213,7 @@ static func _trace_segment(sim: Simulation, from: Vector3, to: Vector3, ignore_i
|
||||
continue
|
||||
if entity.drop_state == Drop.ABOARD:
|
||||
continue
|
||||
var box: AABB = body_box(entity)
|
||||
var box: AABB = body_box(entity) if seen_at < 0.0 else sim.hit_history.box_at(entity, seen_at)
|
||||
var t: float = ground.ray_box(from, to, box)
|
||||
if t < hit.fraction:
|
||||
hit.fraction = t
|
||||
|
||||
Reference in New Issue
Block a user