Files
tidewall/test/server/test_net_game.gd
T
claudeandClaude Opus 5.5 aa797580a7 #72: The packet ship, the lifeline mortar and the gull-kite
Eric chose option B on #72. The drop is now the GDD's launch:

- A packet ship sails a seeded straight line past the island, offshore,
  as close in as it can while keeping 45 m of open water from any land
  (eight-way probes; up to 32 headings, then the four world sides).
- Jump fires the lifeline mortar along the look heading. How high the
  player aims sets the charge (-10 to 45 degrees: 16 to 110 m/s over
  the ground; every shot climbs at 75 m/s). The climb can't be steered
  and the kite can't open until the arc tops out. Riders still aboard at
  the end are fired towards the middle of the island.
- Drop.reach() mirrors the fall for both ways down (kite at the top, or
  a fall with the kite opening by itself); bots, the soak and autoplay
  clients and the tutorial pilot aim with Drop.aim_charge().
- Bots fire at their landing spot once abeam of it, when in easy reach
  at their own staggered moment, or just before the route ends, and open
  the kite early only when the spot is beyond the rest of a fall.
- Art: VesselModels.ship() (two-masted packet, tanned square sails, a
  salt-white boot-top, a bowsprit and the mortar on its bed, mirrored to
  face the island), the figure's gull-kite (figure.py), and a lifeline
  cord from the muzzle to each player still climbing.
- Renamed barge to ship and wing-sail to gull-kite throughout code,
  tests, captures, docs and the tutorial text. make match now defaults to
  seed 2, where the scripted player still harvests and builds.

Closes #72

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:49:21 -05:00

231 lines
9.2 KiB
GDScript

extends TestCase
## Issue N2 end to end: a real NetServer, a ServerLoop and NetClients over loopback
## UDP. People join the lobby, the match starts, snapshots arrive and inputs steer.
const HOST: String = "127.0.0.1"
var loop: ServerLoop
var net: NetServer
var game: NetGame
var clients: Array[NetClient] = []
var receivers: Array[SnapshotReceiver] = []
## Per client: every MATCH notice it was sent.
var notices: Array[Array] = []
## Per client: the yaw it steers towards, in command steps.
var steer: Array[int] = []
var joined: Array[Dictionary] = []
var left: Array[Dictionary] = []
func before_each() -> void:
var rules: MatchRules = MatchRules.new()
rules.max_players = 4
rules.min_players = 2
rules.lobby_ticks = 6
rules.countdown_ticks = 6
rules.results_ticks = 6
loop = ServerLoop.new(31, 0, 2, rules)
net = NetServer.new()
assert_eq(net.listen(0, HOST), OK)
game = NetGame.new(loop, net)
clients.clear()
receivers.clear()
notices.clear()
steer.clear()
joined.clear()
left.clear()
func after_each() -> void:
for client: NetClient in clients:
client.leave()
net.close()
func _client(wanted: String) -> NetClient:
var client: NetClient = NetClient.new()
clients.append(client)
receivers.append(SnapshotReceiver.new())
notices.append([])
steer.append(0)
assert_eq(client.connect_to(HOST, net.port(), wanted), OK)
return client
## Runs the server and every client, a tick per round, until `done` or the time is up.
func _pump(done: Callable, limit_msec: int = 8000) -> bool:
var started: int = Time.get_ticks_msec()
while Time.get_ticks_msec() - started < limit_msec:
game.poll()
for event: Dictionary in game.events:
(joined if event["type"] == "joined" else left).append(event)
loop.run_tick()
game.after_tick()
for index: int in clients.size():
var client: NetClient = clients[index]
var receiver: SnapshotReceiver = receivers[index]
client.poll()
for packet: Dictionary in client.packets:
var bytes: PackedByteArray = packet["bytes"]
if packet["channel"] == NetProtocol.CHANNEL_CONTROL:
var notice: Dictionary = NetSnapshot.decode_match(bytes)
if not notice.is_empty():
notices[index].append(notice)
elif receiver.apply(bytes):
pass
if client.state == NetClient.JOINED:
var commands: Array[PlayerCommand] = []
for ahead: int in range(1, Tuning.NET_INPUT_REDUNDANCY + 1):
var command: PlayerCommand = PlayerCommand.new()
command.tick = receiver.sim_tick + ahead
command.yaw = steer[index]
commands.append(command)
client.send(NetProtocol.CHANNEL_STATE, NetSnapshot.encode_input(receiver.ack_tick(), commands), false)
if done.call():
return true
OS.delay_msec(1)
return bool(done.call())
func test_two_people_join_play_and_are_sent_the_match() -> void:
var mara: NetClient = _client("Mara")
var odo: NetClient = _client("Odo")
steer[0] = 12345
steer[1] = 54321
assert_true(_pump(func() -> bool: return loop.match_flow.in_match() and receivers[0].own_id != 0 and receivers[1].own_id != 0), "the match started and both were told")
var flow: Match = loop.match_flow
assert_eq(joined.size(), 2)
assert_eq(flow.participants.size(), 4, "two people and two bots")
assert_eq(receivers[0].own_id, flow.entity_of[mara.handle])
assert_eq(receivers[1].own_id, flow.entity_of[odo.handle])
# Each was told the seed and every phase on the way in.
for index: int in 2:
var phases: Array[int] = []
for notice: Dictionary in notices[index]:
phases.append(notice["phase"])
assert_eq(notice["seed"], flow.current_seed())
assert_eq(phases.slice(phases.size() - 2), [Match.COUNTDOWN, Match.DROP], "client %d's notices: %s" % [index, phases])
# Everyone starts aboard the ship together, so each sees all four players.
assert_true(_pump(func() -> bool: return receivers[0].entities.size() >= 4 and receivers[1].entities.size() >= 4))
# Inputs arrive and steer: the server took commands, and each player faces its way.
assert_true(_pump(func() -> bool:
var mine: PackedInt32Array = receivers[0].entities.get(receivers[0].own_id, PackedInt32Array())
var theirs: PackedInt32Array = receivers[0].entities.get(receivers[1].own_id, PackedInt32Array())
return not mine.is_empty() and not theirs.is_empty() and mine[NetSnapshot.R_YAW] == 12345 and theirs[NetSnapshot.R_YAW] == 54321
), "both yaws reached Mara's picture")
assert_true(receivers[0].command_tick > 0, "the server echoes the command tick it took")
assert_eq(game.bad_inputs, 0)
assert_true(receivers[0].applied > 5)
assert_eq(receivers[0].malformed + receivers[1].malformed, 0)
assert_true(game.sender.stats(mara.handle)["bytes"] > 0)
func test_commands_repeated_in_packets_reach_the_simulation_once() -> void:
var mara: NetClient = _client("Mara")
_client("Odo")
assert_true(_pump(func() -> bool: return loop.match_flow.phase == Match.COUNTDOWN))
var log: CommandLog = loop.record_commands()
assert_true(_pump(func() -> bool: return loop.match_flow.in_match() and loop.sim.tick > 40))
var ticks_seen: Dictionary = {}
var from_mara: int = 0
for index: int in log.count():
if log.handle_of(index) != mara.handle:
continue
from_mara += 1
var command_tick: int = log.command_of(index).tick
assert_false(ticks_seen.has(command_tick), "tick %d submitted twice" % command_tick)
ticks_seen[command_tick] = true
assert_true(from_mara > 10, "%d commands from Mara" % from_mara)
func test_leaving_takes_the_player_out_of_the_match() -> void:
var mara: NetClient = _client("Mara")
var odo: NetClient = _client("Odo")
assert_true(_pump(func() -> bool: return loop.match_flow.in_match() and receivers[1].own_id != 0))
var handle: int = mara.handle
var entity_id: int = loop.match_flow.entity_of[handle]
mara.leave()
assert_true(_pump(func() -> bool: return left.size() == 1))
assert_eq(left[0]["handle"], handle)
assert_false(loop.match_flow.participants.has(handle))
assert_false(game.sender.has_client(handle))
# Odo's picture loses her within a few snapshots.
assert_true(_pump(func() -> bool: return not receivers[1].entities.has(entity_id)))
assert_eq(odo.state, NetClient.JOINED)
func test_someone_arriving_mid_match_watches_a_living_player() -> void:
_client("Mara")
_client("Odo")
assert_true(_pump(func() -> bool: return loop.match_flow.in_match()))
# Bots filled the other two places, so the newcomer watches until the next match.
var late: NetClient = _client("Late")
var late_index: int = clients.size() - 1
assert_true(_pump(func() -> bool: return late.state == NetClient.JOINED and receivers[late_index].own_id != 0), "the newcomer is sent someone to watch")
var flow: Match = loop.match_flow
assert_eq(flow.participants.size(), 4)
assert_true(flow.watchers.has(late.handle))
assert_false(flow.entity_of.has(late.handle), "a watcher has no player of their own")
var watched: int = receivers[late_index].own_id
assert_true(flow.handle_of.has(watched), "the snapshot follows a player in the match")
assert_false(flow.sim.entities.find(watched).eliminated)
assert_true(receivers[late_index].own.is_empty(), "nobody else's inventory is sent")
assert_eq(joined.size(), 3)
assert_true(bool(joined[2]["watching"]))
assert_eq(net.player_count(), 3)
# Leaving while watching is tidy.
late.leave()
assert_true(_pump(func() -> bool: return left.size() == 1))
assert_false(flow.watchers.has(late.handle))
assert_false(flow.spectating.has(late.handle))
assert_false(game.sender.has_client(late.handle))
func test_more_people_than_the_server_holds_are_turned_away() -> void:
net.max_players = 2
_client("Mara")
_client("Odo")
assert_true(_pump(func() -> bool: return net.player_count() == 2))
var late: NetClient = _client("Late")
assert_true(_pump(func() -> bool: return late.state == NetClient.CLOSED))
assert_eq(late.close_reason, NetProtocol.REASON_FULL)
assert_eq(net.player_count(), 2)
func test_junk_on_the_game_channel_is_counted_and_changes_nothing() -> void:
var mara: NetClient = _client("Mara")
_client("Odo")
assert_true(_pump(func() -> bool: return loop.match_flow.in_match()))
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
rng.seed = 5
for _packet: int in 20:
var bytes: PackedByteArray = PackedByteArray()
bytes.resize(rng.randi_range(1, 200))
for index: int in bytes.size():
bytes[index] = rng.randi_range(0, 255)
bytes[0] = NetSnapshot.MSG_INPUT if rng.randf() < 0.5 else rng.randi_range(0, 255)
# Never a well-formed length for an INPUT.
if (bytes.size() - 6) % PlayerCommand.ENCODED_SIZE == 0:
bytes.append(0)
mara.send(NetProtocol.CHANNEL_STATE, bytes, true)
assert_true(_pump(func() -> bool: return game.bad_inputs >= 20), "%d counted" % game.bad_inputs)
assert_eq(mara.state, NetClient.JOINED)
assert_true(loop.match_flow.in_match())
func test_snapshots_are_spread_over_the_interval() -> void:
# Issue N8: building every client's snapshot on one tick put the whole cost there.
var interval: int = Tuning.NET_SNAPSHOT_INTERVAL_TICKS
var hits: Dictionary = {}
for _tick: int in interval * 10:
loop.run_tick()
var due: int = 0
for handle: int in range(1, 25):
if game.snapshot_due_for(handle):
due += 1
hits[handle] = int(hits.get(handle, 0)) + 1
assert_eq(due, 24 / interval, "an even share of 24 clients on every tick")
for handle: int in range(1, 25):
assert_eq(hits.get(handle, 0), 10, "one snapshot every interval for handle %d" % handle)