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>
85 lines
3.0 KiB
GDScript
85 lines
3.0 KiB
GDScript
extends TestCase
|
|
|
|
|
|
func test_round_trip_preserves_every_field() -> void:
|
|
var command: PlayerCommand = PlayerCommand.new()
|
|
command.tick = 4_000_000_000
|
|
command.move_x = -127
|
|
command.move_y = 90
|
|
command.yaw = 65535
|
|
command.pitch = -32767
|
|
command.buttons = PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_BUILD | 0x8000
|
|
command.selection = 200
|
|
command.material = 3
|
|
|
|
var decoded: PlayerCommand = PlayerCommand.decode(command.encode())
|
|
assert_not_null(decoded)
|
|
assert_eq(decoded.tick, 4_000_000_000)
|
|
assert_eq(decoded.move_x, -127)
|
|
assert_eq(decoded.move_y, 90)
|
|
assert_eq(decoded.yaw, 65535)
|
|
assert_eq(decoded.pitch, -32767)
|
|
assert_eq(decoded.buttons, command.buttons)
|
|
assert_eq(decoded.selection, 200)
|
|
assert_eq(decoded.material, 3)
|
|
|
|
|
|
func test_encoding_is_compact_and_fixed_size() -> void:
|
|
assert_eq(PlayerCommand.new().encode().size(), PlayerCommand.ENCODED_SIZE)
|
|
assert_eq(PlayerCommand.ENCODED_SIZE, 15)
|
|
|
|
|
|
func test_view_lag_round_trips_and_is_clamped_to_a_byte() -> void:
|
|
var command: PlayerCommand = PlayerCommand.new()
|
|
command.view_lag = 37
|
|
assert_eq(PlayerCommand.decode(command.encode()).view_lag, 37)
|
|
command.view_lag = 9000
|
|
assert_eq(PlayerCommand.decode(command.encode()).view_lag, 255)
|
|
command.view_lag = -5
|
|
assert_eq(PlayerCommand.decode(command.encode()).view_lag, 0)
|
|
|
|
|
|
func test_decode_at_an_offset() -> void:
|
|
var first: PlayerCommand = PlayerCommand.new()
|
|
first.tick = 7
|
|
var second: PlayerCommand = PlayerCommand.new()
|
|
second.tick = 8
|
|
var stream: PackedByteArray = first.encode() + second.encode()
|
|
assert_eq(PlayerCommand.decode(stream, PlayerCommand.ENCODED_SIZE).tick, 8)
|
|
|
|
|
|
func test_truncated_buffer_returns_null_without_error() -> void:
|
|
var bytes: PackedByteArray = PlayerCommand.new().encode()
|
|
bytes.resize(PlayerCommand.ENCODED_SIZE - 1)
|
|
assert_null(PlayerCommand.decode(bytes))
|
|
assert_null(PlayerCommand.decode(PackedByteArray()))
|
|
assert_null(PlayerCommand.decode(PlayerCommand.new().encode(), 1))
|
|
assert_null(PlayerCommand.decode(PlayerCommand.new().encode(), -1))
|
|
|
|
|
|
func test_hostile_wire_values_are_clamped() -> void:
|
|
var bytes: PackedByteArray = PlayerCommand.new().encode()
|
|
bytes.encode_s8(4, -128)
|
|
bytes.encode_s16(8, -32768)
|
|
var decoded: PlayerCommand = PlayerCommand.decode(bytes)
|
|
assert_eq(decoded.move_x, -PlayerCommand.AXIS_MAX)
|
|
assert_eq(decoded.pitch, -PlayerCommand.PITCH_MAX)
|
|
|
|
|
|
func test_from_input_clamps_and_wraps() -> void:
|
|
var command: PlayerCommand = PlayerCommand.from_input(1, Vector2(5.0, -5.0), -PI * 0.5, 9.0)
|
|
assert_eq(command.move_x, PlayerCommand.AXIS_MAX)
|
|
assert_eq(command.move_y, -PlayerCommand.AXIS_MAX)
|
|
assert_eq(command.yaw, PlayerCommand.YAW_STEPS * 3 / 4)
|
|
assert_eq(command.pitch, PlayerCommand.PITCH_MAX)
|
|
|
|
|
|
func test_input_helpers_read_back() -> void:
|
|
var command: PlayerCommand = PlayerCommand.from_input(
|
|
1, Vector2(0.5, 0.0), PI, 0.0, PlayerCommand.BUTTON_JUMP
|
|
)
|
|
assert_near(command.yaw_radians(), PI, 0.001)
|
|
assert_near(command.move_vector().x, 0.5, 0.01)
|
|
assert_true(command.is_pressed(PlayerCommand.BUTTON_JUMP))
|
|
assert_false(command.is_pressed(PlayerCommand.BUTTON_FIRE))
|