Files
transcience/tests/unit/test_emergency_escape.gd
T
claude 005679f1b5 Fix bullet/ship desync; rework death, escape, arrival and hub awareness
(1) Bullets appeared to trail the ship. Two independent causes, measured with
the new tools/diag_prediction.gd rather than guessed at:
  - ServerRuntime ticked before ClientRuntime, so input sampled on frame N was
    not consumed until frame N+1, leaving the drawn ship a constant one tick
    (4.00px at 240 u/s) ahead of the authoritative one that bullets spawn from.
    ClientRuntime now sets process_physics_priority = -10. Gap on a listen
    server: 4.00px -> 0.10px mean, 0.30px worst.
  - PLAYER_MUZZLE_OFFSET was PLAYER_RADIUS + 6 = 12px against a 13px drawn
    ship, so bullets were born inside the sprite. Regression from the previous
    commit's hitbox shrink; it now derives from PLAYER_VISUAL_RADIUS.

(2) No more timed respawn. A downed player stays down until they ask for the
hub (E), which is an ordinary input -- the server has no "revive me" message.

(3) Escape channel 3s -> 1s, and damage no longer cancels it. An interruptible
channel makes killing the process strictly better than using the button, so a
dropped connection now runs the same channel: the player stays in the world as
linkdead, still killable, and is only released once it completes. Instances
refuse to close while a linkdead body is resolving, or a solo drop would delete
it on the next tick and hand the exploit straight back.

(4) Escape opens an in-game menu: return to hub (routed through the same held-
escape channel, not a new message), disconnect, quit.

(5) Server pushes a roster so the hub shows who is online and which dungeon
they are in. Entering a dungeon grants 2s arrival protection -- invulnerable
AND weapons-cold, since invulnerability alone would make the spawn a free
firing position -- flagged in the snapshot and drawn on every protected ship.

(6) Cleared dungeons hold the party 30s (was 5s) with a visible countdown.

(7) The hub's grey circle was a 100k-HP target dummy that read as scenery. Now
drawn as a bullseye so its purpose is legible.

Protocol version 1 -> 2. 91 tests (was 78); smoke.sh gains a bot that is
SIGKILLed mid-dungeon to prove the disconnect path end to end. check.sh,
test.sh and smoke.sh all pass.
2026-09-03 18:43:19 +02:00

118 lines
4.4 KiB
GDScript

extends GutTest
## The escape button is the player's safety valve, and it is also the one input
## with a real consequence attached, so its timing is server-owned end to end.
var world: SimWorld
const PEER := 3
func before_each() -> void:
world = SimWorld.new(1)
world.add_player(PEER, "tester")
world.players[PEER].iframes = 0
func _hold_escape(ticks: int) -> void:
for _i in ticks:
var frames: Array[InputFrame] = [
InputFrame.make(world.tick + 1, Vector2.ZERO, 0.0, InputFrame.BTN_ESCAPE)]
world.queue_input(PEER, frames)
world.step()
func _events_of(type: int) -> Array:
return world.events.filter(func(e: Dictionary) -> bool: return int(e["t"]) == type)
func test_escape_takes_the_full_channel_time() -> void:
_hold_escape(SimConfig.ESCAPE_CHANNEL_TICKS - 1)
assert_eq(_events_of(SimEvent.Type.ESCAPE_COMPLETED).size(), 0,
"the escape must not complete early")
_hold_escape(1)
assert_eq(_events_of(SimEvent.Type.ESCAPE_COMPLETED).size(), 1)
func test_releasing_the_button_cancels_the_channel() -> void:
# Deliberately short of ESCAPE_CHANNEL_TICKS: hold the full duration and the
# channel completes instead, which is a different test.
_hold_escape(SimConfig.ESCAPE_CHANNEL_TICKS / 2)
assert_gt(world.players[PEER].escape_ticks, 0)
var frames: Array[InputFrame] = [InputFrame.make(world.tick + 1, Vector2.ZERO, 0.0, 0)]
world.queue_input(PEER, frames)
world.step()
assert_eq(world.players[PEER].escape_ticks, 0)
assert_eq(_events_of(SimEvent.Type.ESCAPE_CANCELLED).size(), 1)
## The inverse of what this asserted originally. Interrupting on damage makes
## quitting the process strictly better than using the button, which turns the
## escape hatch into the exploit -- see SimConfig.ESCAPE_CHANNEL_TICKS.
func test_taking_damage_does_not_cancel_the_channel() -> void:
_hold_escape(20)
var before: int = world.players[PEER].escape_ticks
world.pool.spawn(world.players[PEER].pos, Vector2.ZERO, 6.0, 60, 10,
SimConfig.TEAM_ENEMY, SimConfig.KIND_ORB)
_hold_escape(1)
assert_lt(world.players[PEER].hp, SimConfig.PLAYER_MAX_HP, "setup: should have been hit")
assert_eq(world.players[PEER].escape_ticks, before + 1,
"the channel must keep running while under fire")
assert_eq(_events_of(SimEvent.Type.ESCAPE_CANCELLED).size(), 0)
## Dying is still an interruption -- it is the one thing the escape cannot beat,
## which is what keeps the one-second channel a real risk.
func test_dying_ends_the_channel() -> void:
_hold_escape(20)
var p: SimPlayer = world.players[PEER]
p.hp = 1
p.iframes = 0
world.pool.spawn(p.pos, Vector2.ZERO, 6.0, 60, 999,
SimConfig.TEAM_ENEMY, SimConfig.KIND_ORB)
world.step()
assert_false(p.alive)
assert_eq(p.escape_ticks, 0)
assert_eq(_events_of(SimEvent.Type.ESCAPE_COMPLETED).size(), 0)
## A dropped connection channels out on exactly the same timer, so pulling the
## plug is never cheaper than pressing the button.
func test_a_linkdead_player_channels_out_without_input() -> void:
var p: SimPlayer = world.players[PEER]
p.linkdead = true
for _i in SimConfig.ESCAPE_CHANNEL_TICKS - 1:
world.step()
assert_eq(_events_of(SimEvent.Type.ESCAPE_COMPLETED).size(), 0,
"a disconnect must not be an instant exit")
world.step()
assert_eq(_events_of(SimEvent.Type.ESCAPE_COMPLETED).size(), 1)
func test_a_linkdead_player_is_still_killable_while_channelling() -> void:
var p: SimPlayer = world.players[PEER]
p.linkdead = true
p.hp = 1
p.iframes = 0
world.step()
world.pool.spawn(p.pos, Vector2.ZERO, 6.0, 60, 999,
SimConfig.TEAM_ENEMY, SimConfig.KIND_ORB)
world.step()
assert_false(p.alive, "the whole point: disconnecting does not grant immunity")
func test_a_cancelled_channel_restarts_from_zero() -> void:
_hold_escape(SimConfig.ESCAPE_CHANNEL_TICKS / 2)
var frames: Array[InputFrame] = [InputFrame.make(world.tick + 1, Vector2.ZERO, 0.0, 0)]
world.queue_input(PEER, frames)
world.step()
_hold_escape(SimConfig.ESCAPE_CHANNEL_TICKS - 1)
assert_eq(_events_of(SimEvent.Type.ESCAPE_COMPLETED).size(), 0,
"progress must not carry over from an interrupted channel")
func test_escape_completion_never_reaches_the_client_directly() -> void:
_hold_escape(SimConfig.ESCAPE_CHANNEL_TICKS)
var packet := NetCodec.decode_events(NetCodec.encode_events(world.tick, world.events))
for ev: Dictionary in packet["events"]:
assert_ne(int(ev["t"]), SimEvent.Type.ESCAPE_COMPLETED,
"the transfer is the server's decision; clients only see the outcome")