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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user