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:
@@ -0,0 +1,186 @@
|
||||
extends TestCase
|
||||
|
||||
## Issue N4, in the simulation: hitscan shots judged against where the target was when
|
||||
## the shooter saw it, and the limits on what a shooter may claim to have seen.
|
||||
##
|
||||
## A shooter stands still with a signal revolver, facing +z. A target 20 m away walks
|
||||
## across at 4 m/s. The shooter aims at where the target was some ticks ago, as a
|
||||
## client drawing the past would, and says how far back that was.
|
||||
|
||||
const SHOOTER_FEET: Vector3 = Vector3(500.0, 0.0, 500.0)
|
||||
const RANGE: float = 20.0
|
||||
const CAP: int = Tuning.LAG_COMPENSATION_MAX_TICKS
|
||||
const STEPS: int = PlayerCommand.VIEW_LAG_STEPS_PER_TICK
|
||||
|
||||
var sim: Simulation
|
||||
var shooter: SimEntity
|
||||
var target: SimEntity
|
||||
## Where the target was before each tick.
|
||||
var path: Array[Vector3] = []
|
||||
|
||||
|
||||
func before_each() -> void:
|
||||
sim = Simulation.new(5, SimGround.flat())
|
||||
shooter = sim.entities.find(sim.add_player())
|
||||
shooter.position = SHOOTER_FEET
|
||||
shooter.on_ground = true
|
||||
var revolver: int = ItemCatalog.code_of("signal_revolver")
|
||||
Inventory.give(shooter, revolver, 1)
|
||||
Inventory.give(shooter, ItemCatalog.get_def(revolver).ammo_code, 60)
|
||||
shooter.selected = 1
|
||||
target = sim.entities.find(sim.add_player())
|
||||
target.position = SHOOTER_FEET + Vector3(-7.0, 0.0, RANGE)
|
||||
target.on_ground = true
|
||||
path.clear()
|
||||
|
||||
|
||||
## Runs a tick: the target walks towards +x (or does `target_buttons` on the spot), and
|
||||
## the shooter looks at `aim` saying it sees `lag` quarter ticks back, firing if asked.
|
||||
## Returns the damage done to the target.
|
||||
func _tick(aim: Vector3, lag: int, fire: bool, target_moves: bool = true, target_buttons: int = 0) -> int:
|
||||
path.append(target.position)
|
||||
var eye: Vector3 = Harvest.eye(shooter)
|
||||
var to: Vector3 = aim - eye
|
||||
var buttons: int = PlayerCommand.BUTTON_AIM | (PlayerCommand.BUTTON_FIRE if fire else 0)
|
||||
var command: PlayerCommand = PlayerCommand.from_input(sim.tick, Vector2.ZERO, atan2(to.x, to.z), atan2(to.y, Vector2(to.x, to.z).length()), buttons, 1)
|
||||
command.view_lag = lag
|
||||
assert_true(sim.submit_command(shooter.id, command))
|
||||
# Facing +z, right is -x, so a negative strafe walks towards +x.
|
||||
sim.submit_command(target.id, PlayerCommand.from_input(sim.tick, Vector2(-1.0 if target_moves else 0.0, 0.0), 0.0, 0.0, target_buttons))
|
||||
sim.step()
|
||||
var damage: int = 0
|
||||
for event: Dictionary in sim.events:
|
||||
if event["type"] == "player_hit" and event["victim"] == target.id:
|
||||
damage += int(event["health"]) + int(event["armor"])
|
||||
target.health = Tuning.PLAYER_MAX_HEALTH
|
||||
return damage
|
||||
|
||||
|
||||
## The middle of the target's chest where it was `ticks` before the tick about to run.
|
||||
func _chest(ticks: float) -> Vector3:
|
||||
var at: float = sim.tick - ticks
|
||||
var from: int = floori(at)
|
||||
var feet: Vector3 = target.position if from >= path.size() else path[from]
|
||||
if from + 1 < path.size():
|
||||
feet = feet.lerp(path[from + 1], at - from)
|
||||
elif from + 1 == path.size():
|
||||
feet = feet.lerp(target.position, at - from)
|
||||
return feet + Vector3(0.0, 1.0, 0.0)
|
||||
|
||||
|
||||
## Lets the target walk for `ticks` while the shooter watches, seeing `lag` quarter
|
||||
## ticks back, as a client's every command says.
|
||||
func _walk(ticks: int, lag: int = 0) -> void:
|
||||
for _tick_index: int in ticks:
|
||||
_tick(SHOOTER_FEET + Vector3(0.0, 1.0, RANGE), lag, false)
|
||||
|
||||
|
||||
func test_the_target_really_walks_across() -> void:
|
||||
_walk(30)
|
||||
assert_near(target.position.x - path[0].x, Tuning.WALK_SPEED, 0.2)
|
||||
assert_near(target.position.z, SHOOTER_FEET.z + RANGE, 0.001)
|
||||
|
||||
|
||||
func test_a_shot_at_where_the_target_was_seen_hits_and_the_same_shot_unrewound_misses() -> void:
|
||||
for lag_ticks: float in [6.0, 9.25, 12.5, float(CAP)]:
|
||||
var lag: int = roundi(lag_ticks * STEPS)
|
||||
before_each()
|
||||
_walk(45, lag)
|
||||
assert_near(Weapons.view_time(shooter, sim.tick - 1), sim.tick - 1 - lag_ticks, 0.001)
|
||||
assert_true(_tick(_chest(lag_ticks), lag, true) > 0, "seen %.2f ticks back: a hit" % lag_ticks)
|
||||
before_each()
|
||||
_walk(45)
|
||||
var behind: float = _chest(lag_ticks).distance_to(_chest(0.0))
|
||||
assert_true(behind > 0.75)
|
||||
assert_eq(_tick(_chest(lag_ticks), 0, true), 0, "the same aim judged in the present misses by %.2f m" % behind)
|
||||
|
||||
|
||||
func test_aiming_at_the_present_with_no_lag_still_hits() -> void:
|
||||
_walk(45)
|
||||
assert_true(_tick(_chest(0.0), 0, true) > 0)
|
||||
assert_eq(Weapons.view_time(shooter, sim.tick - 1), -1.0, "nothing to rewind")
|
||||
|
||||
|
||||
func test_beyond_the_cap_the_claim_is_cut_to_the_cap() -> void:
|
||||
# Thirty ticks back the target was 4 m from where it is now and 2 m from where it
|
||||
# was at the cap. Claiming to see that far back doesn't make the shot land.
|
||||
_walk(45, 30 * STEPS)
|
||||
assert_near(Weapons.view_time(shooter, sim.tick - 1), float(sim.tick - 1 - CAP), 0.001, "judged at the cap, not at the claim")
|
||||
assert_eq(_tick(_chest(30.0), 30 * STEPS, true), 0, "30 ticks back")
|
||||
_walk(20, 255)
|
||||
assert_eq(_tick(_chest(30.0), 255, true), 0, "the largest claim a command can hold")
|
||||
# What the server did was judge both at the cap, where a shot does land.
|
||||
_walk(20, 255)
|
||||
assert_true(_tick(_chest(float(CAP)), 255, true) > 0)
|
||||
|
||||
|
||||
func test_nobody_can_look_further_back_than_they_already_have() -> void:
|
||||
_walk(45)
|
||||
# Seeing the present all along, then one shot claiming to see ten ticks back.
|
||||
assert_eq(_tick(_chest(10.0), 10 * STEPS, true), 0, "a sudden claim of the past is not believed")
|
||||
assert_true(Weapons.view_time(shooter, sim.tick - 1) >= sim.tick - 2.0, "still judged within a tick of the present")
|
||||
# A client really seeing ten ticks back says so with every command. Its view
|
||||
# stands still until time has caught up with the claim, then moves with it.
|
||||
_walk(20, 10 * STEPS)
|
||||
assert_near(Weapons.view_time(shooter, sim.tick - 1), sim.tick - 1 - 10.0, 0.001)
|
||||
assert_true(_tick(_chest(10.0), 10 * STEPS, true) > 0)
|
||||
|
||||
|
||||
func test_the_world_is_judged_as_it_is_now() -> void:
|
||||
_walk(45, 40)
|
||||
# A wall goes up between them after the shooter's view of the target was drawn.
|
||||
var wall: Vector3 = SHOOTER_FEET + Vector3(-20.0, 0.0, RANGE * 0.5)
|
||||
sim.ground.add_solid(AABB(wall, Vector3(40.0, 4.0, 0.4)))
|
||||
assert_eq(_tick(_chest(10.0), 40, true), 0, "the wall stops the shot")
|
||||
|
||||
|
||||
func test_a_target_eliminated_since_is_not_hit() -> void:
|
||||
_walk(45, 40)
|
||||
target.eliminated = true
|
||||
assert_eq(_tick(_chest(10.0), 40, true), 0)
|
||||
var hits: int = 0
|
||||
for event: Dictionary in sim.events:
|
||||
if event["type"] == "impact" and event["hit"] == Weapons.HIT_PLAYER:
|
||||
hits += 1
|
||||
assert_eq(hits, 0)
|
||||
|
||||
|
||||
func test_the_body_is_as_tall_as_it_was_then() -> void:
|
||||
# The target crouches on the spot, then stands up.
|
||||
for _crouched: int in 20:
|
||||
_tick(SHOOTER_FEET + Vector3(0.0, 1.0, RANGE), 0, false, false, PlayerCommand.BUTTON_CROUCH)
|
||||
for _standing: int in 6:
|
||||
_tick(SHOOTER_FEET + Vector3(0.0, 1.0, RANGE), 0, false, false)
|
||||
var head: Vector3 = target.position + Vector3(0.0, Tuning.PLAYER_HEIGHT - 0.15, 0.0)
|
||||
assert_true(_tick(head, 0, true, false) > 0, "standing now: the head is there")
|
||||
before_each()
|
||||
for _crouched: int in 20:
|
||||
_tick(SHOOTER_FEET + Vector3(0.0, 1.0, RANGE), 12 * STEPS, false, false, PlayerCommand.BUTTON_CROUCH)
|
||||
for _standing: int in 6:
|
||||
_tick(head, 12 * STEPS, false, false)
|
||||
assert_eq(_tick(head, 12 * STEPS, true, false), 0, "twelve ticks ago it was crouched: nothing at head height")
|
||||
|
||||
|
||||
func test_history_is_kept_only_as_far_back_as_the_cap_and_forgotten_with_the_player() -> void:
|
||||
_walk(60)
|
||||
var now: AABB = Weapons.body_box(target)
|
||||
var then: AABB = sim.hit_history.box_at(target, float(sim.tick - CAP))
|
||||
assert_near(now.position.x - then.position.x, Tuning.WALK_SPEED * CAP / Tuning.TICK_RATE_HZ, 0.05)
|
||||
assert_eq(sim.hit_history.box_at(target, float(sim.tick - 40)), now, "too old to be stored: the present stands in")
|
||||
var half: AABB = sim.hit_history.box_at(target, sim.tick - 4.5)
|
||||
var a: AABB = sim.hit_history.box_at(target, float(sim.tick - 5))
|
||||
var b: AABB = sim.hit_history.box_at(target, float(sim.tick - 4))
|
||||
assert_near(half.position.x, (a.position.x + b.position.x) * 0.5, 0.0001, "between ticks")
|
||||
sim.remove_player(target.id)
|
||||
assert_eq(sim.hit_history._tracks.has(target.id), false)
|
||||
|
||||
|
||||
func test_a_replay_of_the_same_commands_ends_in_the_same_state() -> void:
|
||||
var hashes: Array[String] = []
|
||||
for _run: int in 2:
|
||||
before_each()
|
||||
_walk(40, 8 * STEPS)
|
||||
for round: int in 60:
|
||||
_tick(_chest(8.0), 8 * STEPS, round % 20 == 5)
|
||||
hashes.append(sim.state_hash())
|
||||
assert_eq(hashes[0], hashes[1])
|
||||
Reference in New Issue
Block a user