- Build ghost: ClientSession shows a BuildGhost in build mode while the client plays, asking Building.check() of its own world and predicted player with the mouse's newest look, so it agrees with the server. - Flooded basins: ClientMirror.follow_flood() spreads the client's flood as the server's tide tick does, from the tide and the pieces it has. WaterView passes a mask of flooded cells (Flood.pours says when it changed) to the sea shader, which leaves dry cells undrawn. - Shots, impacts and swings: a new FX message (NetFx) goes unreliably on the state channel next to each snapshot, with what happened within NET_FX_RADIUS_METRES of what the client watches or involving that player. ClientGame keeps them in `fx` for views and sound. They change no state, so a lost one is a missed flash; sending them reliably would add to the traffic a bad link can least afford (N8). The fuzzer covers the new parser. - Captures: client_build and client_build_refused show the ghost in its allowed and refused tints; client_basin rings shore ground with breakwaters and plays until the tide stands over it, and the server, the client's flood and the drawn sea all keep the basin dry. - Tests: NetFx round trips, limits and malformed input; a landed player's shots and impacts reach a client, each matching a server event; the client keeps a ringed basin dry through the first rise, floods it with no ring, catches up after a long frame; the water mask follows pours. The held item is still not drawn; that needs the models (A4, #44). Closes #78 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
245 lines
10 KiB
GDScript
245 lines
10 KiB
GDScript
## Capture scenario for the network client (issue N10): a headless server listening
|
|
## on loopback UDP in this process, and the real ClientSession connected to it, played
|
|
## by its autoplay script. The picture is the client's own: its copy of the world, its
|
|
## predicted player, everyone else as the server's snapshots have them.
|
|
##
|
|
## Stages: barge (a few seconds into the drop), glide, landed, and basin: once landed,
|
|
## the server rings a patch of low ground near the shore with breakwaters and play goes
|
|
## on until the tide has risen past it, so the sea stands outside the ring and the
|
|
## ground inside, below the sea, stays dry on the client too (issue N11). build and
|
|
## build_refused: once landed, the client goes into build mode for a driftwood wall,
|
|
## with plenty of materials or none, so the ghost shows in its allowed or refused tint.
|
|
extends Node3D
|
|
|
|
const DT: float = 1.0 / Tuning.TICK_RATE_HZ
|
|
const HOST: String = "127.0.0.1"
|
|
const LIMIT_ROUNDS: int = 200 * Tuning.TICK_RATE_HZ
|
|
|
|
@export var stage: String = "barge"
|
|
@export var match_seed: int = 2
|
|
@export var bots: int = 23
|
|
|
|
var _loop: ServerLoop
|
|
var _net: NetServer
|
|
var _game: NetGame
|
|
var _session: ClientSession
|
|
|
|
|
|
func _ready() -> void:
|
|
var rules: MatchRules = MatchRules.new()
|
|
rules.max_players = bots + 1
|
|
rules.lobby_ticks = 6
|
|
rules.countdown_ticks = 6
|
|
_loop = ServerLoop.new(match_seed, 0, bots, rules)
|
|
_net = NetServer.new()
|
|
if _net.listen(0, HOST) != OK:
|
|
push_error("client capture: the server couldn't listen on loopback")
|
|
return
|
|
_game = NetGame.new(_loop, _net)
|
|
_session = ClientSession.new()
|
|
_session.input.autoplay = true
|
|
if _session.connect_to(HOST, _net.port(), "Mara") != OK:
|
|
push_error("client capture: the client couldn't open a connection")
|
|
return
|
|
add_child(_session)
|
|
# The picture is of one moment, so the session isn't left running behind it.
|
|
_session.set_process(false)
|
|
var me: SimEntity = _session.game.prediction.player
|
|
var since: int = -1
|
|
for _round: int in LIMIT_ROUNDS:
|
|
_game.poll()
|
|
if _session.game.building_world():
|
|
# The client makes its island in the background. A real lobby is far
|
|
# longer than that takes; this one is 6 ticks, so the server waits.
|
|
_session.pump(DT)
|
|
OS.delay_msec(5)
|
|
continue
|
|
_loop.run_tick()
|
|
_game.after_tick()
|
|
_session.pump(DT)
|
|
var reached: bool = false
|
|
if _session.game.playing():
|
|
match stage:
|
|
"barge":
|
|
reached = me.drop_state == Drop.ABOARD and _session.game.prediction.next_tick > 3 * Tuning.TICK_RATE_HZ
|
|
"glide":
|
|
reached = me.drop_state == Drop.GLIDING
|
|
"landed", "basin", "build", "build_refused":
|
|
reached = me.drop_state == Drop.NONE
|
|
if reached and since < 0:
|
|
since = 0
|
|
elif since >= 0:
|
|
since += 1
|
|
if since >= (2 * Tuning.TICK_RATE_HZ if stage != "barge" else 1):
|
|
break
|
|
if since < 0:
|
|
push_error("client capture: never reached stage '%s'" % stage)
|
|
return
|
|
var basin_text: String = ""
|
|
if stage == "basin":
|
|
basin_text = _basin()
|
|
if basin_text.is_empty():
|
|
return
|
|
elif stage.begins_with("build"):
|
|
basin_text = _build_mode(stage == "build")
|
|
if basin_text.is_empty():
|
|
return
|
|
_session.view.settle()
|
|
var others: int = 0
|
|
var camera: Camera3D = _session.view.camera
|
|
for entity: SimEntity in _session.game.world.entities.entities():
|
|
if entity.archetype != SimEntity.ARCHETYPE_PLAYER or entity.id == _session.mirror.own_id or entity.eliminated:
|
|
continue
|
|
var figure: Node3D = _session.view.entities.figure_of(entity.id)
|
|
var to: Vector3 = (figure.position if figure != null else entity.position) + Vector3(0.0, 1.0, 0.0) - camera.position
|
|
if to.length() < 150.0 and to.normalized().dot(-camera.basis.z) > 0.8:
|
|
others += 1
|
|
var flow: Match = _loop.match_flow
|
|
_caption("NETWORK CLIENT · %s · %s · seed %d\njoined %s over UDP as \"%s\" · %d players drawn, %d others ahead of the camera\nsimulation tick %d on the server, %d predicted here · %d snapshots, %d world messages, %d corrections" % [
|
|
stage.to_upper(), flow.phase_name(), match_seed, HOST, _net.name_of(_session.link.handle),
|
|
_session.view.entities.figure_count(), others, _loop.sim.tick, _session.game.prediction.next_tick,
|
|
_session.game.receiver.applied, _session.game.replica.applied, _session.game.prediction.mispredictions,
|
|
] + basin_text)
|
|
|
|
|
|
## Stops the autoplay where the player stands and goes into build mode for a driftwood
|
|
## wall, after the server has given the player plenty of materials or taken them all.
|
|
## Returns a caption line, or "" after reporting an error.
|
|
func _build_mode(afford: bool) -> String:
|
|
var flow: Match = _loop.match_flow
|
|
var mine: SimEntity = _loop.sim.entities.find(int(flow.entity_of.get(_session.link.handle, 0)))
|
|
if mine == null or mine.eliminated:
|
|
push_error("client capture: the player isn't in the match to build")
|
|
return ""
|
|
mine.materials = PackedInt32Array([200, 200, 200] if afford else [0, 0, 0])
|
|
var me: SimEntity = _session.game.prediction.player
|
|
_session.input.autoplay = false
|
|
_session.input.build_mode = true
|
|
_session.input.build_kind = StructureSet.WALL
|
|
_session.input.material = Materials.DRIFTWOOD
|
|
_session.input.yaw = float(me.yaw) / PlayerCommand.YAW_STEPS * TAU
|
|
_session.input.pitch = -0.3
|
|
for _round: int in 45:
|
|
_game.poll()
|
|
_loop.run_tick()
|
|
_game.after_tick()
|
|
_session.pump(DT)
|
|
var ghost: BuildGhost = _session.ghost
|
|
if ghost == null or not ghost.visible:
|
|
push_error("client capture: no build ghost is showing")
|
|
return ""
|
|
if ghost.valid != afford:
|
|
push_error("client capture: the ghost says %s, expected %s" % [Building.Result.keys()[ghost.result], "OK" if afford else "refused"])
|
|
return ""
|
|
return "\nBUILD MODE · driftwood wall · ghost %s (%s) · driftwood carried %d" % [
|
|
"allowed" if ghost.valid else "refused", Building.Result.keys()[ghost.result], _session.game.prediction.player.materials[Materials.DRIFTWOOD],
|
|
]
|
|
|
|
|
|
## Rings low ground with breakwaters on the server, plays on until the tide stands over
|
|
## it, and frames the ring. Returns a caption line, or "" after reporting an error.
|
|
func _basin() -> String:
|
|
var sim: Simulation = _loop.sim
|
|
var flood: Flood = sim.flood
|
|
var level: float = sim.tide.levels[0]
|
|
var ring: Vector2i = _find_basin(flood, level, _session.game.prediction.player.position)
|
|
if ring.x < 0:
|
|
push_error("client capture: no low ground to ring below the first tide level %.1f m" % level)
|
|
return ""
|
|
# Two breakwaters a side on every edge of the 3 x 3 cells, based at the edge's floor.
|
|
var stone: int = Materials.code_of("stone")
|
|
var piece_levels: int = roundi(Tuning.GRID_CELL_METRES / Tuning.BUILD_LEVEL_METRES)
|
|
for along: int in range(3):
|
|
for wall: Vector4i in [
|
|
Vector4i(ring.x, 0, ring.y + along, StructureSet.SLOT_WALL_X),
|
|
Vector4i(ring.x + 3, 0, ring.y + along, StructureSet.SLOT_WALL_X),
|
|
Vector4i(ring.x + along, 0, ring.y, StructureSet.SLOT_WALL_Z),
|
|
Vector4i(ring.x + along, 0, ring.y + 3, StructureSet.SLOT_WALL_Z),
|
|
]:
|
|
var a: int = wall.z * flood.side + wall.x
|
|
var b: int = a - 1 if wall.w == StructureSet.SLOT_WALL_X else a - flood.side
|
|
var base: int = floori(maxf(flood.floors[a], flood.floors[b]) / Tuning.BUILD_LEVEL_METRES)
|
|
for stack: int in 2:
|
|
sim.structures.add(StructureSet.BREAKWATER, stone, Vector4i(wall.x, base + stack * piece_levels, wall.z, wall.w), 0, 0, sim.tick, sim.ground)
|
|
var inside: Vector3 = Vector3((ring.x + 1.5) * flood.cell, 0.0, (ring.y + 1.5) * flood.cell)
|
|
var outside: Vector3 = Vector3((ring.x - 1.5) * flood.cell, 0.0, (ring.y + 1.5) * flood.cell)
|
|
var floor_inside: float = flood.floors[flood.cell_of(inside.x, inside.z)]
|
|
var client_flood: Flood = null
|
|
for _round: int in 400 * Tuning.TICK_RATE_HZ:
|
|
_game.poll()
|
|
_loop.run_tick()
|
|
_game.after_tick()
|
|
_session.pump(DT)
|
|
client_flood = _session.game.world.flood
|
|
if sim.sea_level > floor_inside + 1.0 and _session.game.world.sea_level > floor_inside + 1.0 and client_flood.is_flooded(outside.x, outside.z):
|
|
break
|
|
if not (sim.sea_level > floor_inside + 1.0):
|
|
push_error("client capture: the tide never rose over the ringed ground")
|
|
return ""
|
|
_session.pump(DT)
|
|
var ground: float = sim.ground.terrain_height(inside.x, inside.z)
|
|
_session.view.look_from(inside + Vector3(-19.0, ground + 15.0, -17.0), Vector3(inside.x, ground, inside.z))
|
|
return "\nBASIN · ring at (%.0f, %.0f), floor inside %.1f m, sea %.1f m\ninside dry: server %s, client %s, drawn %s · outside flooded on the client: %s" % [
|
|
inside.x, inside.z, floor_inside, _session.game.world.sea_level,
|
|
not flood.is_flooded(inside.x, inside.z), not client_flood.is_flooded(inside.x, inside.z),
|
|
not _session.view.water.shows_water_at(inside.x, inside.z), client_flood.is_flooded(outside.x, outside.z),
|
|
]
|
|
|
|
|
|
## The corner cell of a 3 x 3 patch of dry ground whose floor lies between 0.6 m and
|
|
## 1.5 m under `level` everywhere in it and in the ring of cells around it, closest to
|
|
## `near`, or (-1, -1).
|
|
func _find_basin(flood: Flood, level: float, near: Vector3) -> Vector2i:
|
|
var best: Vector2i = Vector2i(-1, -1)
|
|
var best_distance: float = INF
|
|
for cz: int in range(2, flood.side - 6):
|
|
for cx: int in range(2, flood.side - 6):
|
|
var fits: bool = true
|
|
for dz: int in range(-1, 4):
|
|
for dx: int in range(-1, 4):
|
|
var index: int = (cz + dz) * flood.side + cx + dx
|
|
var cell_floor: float = flood.floors[index]
|
|
if flood.flooded[index] != 0 or cell_floor < 0.6 or cell_floor > level - 1.5:
|
|
fits = false
|
|
break
|
|
if not fits:
|
|
break
|
|
if not fits:
|
|
continue
|
|
# Open sea must reach the patch's surroundings: a flooded cell near it.
|
|
var shore: bool = false
|
|
for dz: int in range(-3, 6):
|
|
for dx: int in range(-3, 6):
|
|
var index: int = (cz + dz) * flood.side + cx + dx
|
|
if index >= 0 and index < flood.flooded.size() and flood.flooded[index] != 0:
|
|
shore = true
|
|
if not shore:
|
|
continue
|
|
var distance: float = Vector2((cx + 1.5) * flood.cell - near.x, (cz + 1.5) * flood.cell - near.z).length()
|
|
if distance < best_distance:
|
|
best_distance = distance
|
|
best = Vector2i(cx, cz)
|
|
return best
|
|
|
|
|
|
func _exit_tree() -> void:
|
|
if _session != null:
|
|
_session.leave()
|
|
if _net != null:
|
|
_net.close()
|
|
|
|
|
|
func _caption(text: String) -> void:
|
|
var settings: LabelSettings = LabelSettings.new()
|
|
settings.font_size = 20
|
|
settings.font_color = Color(0.96, 0.94, 0.88)
|
|
settings.outline_size = 6
|
|
settings.outline_color = Color(0.08, 0.09, 0.1)
|
|
var label: Label = Label.new()
|
|
label.label_settings = settings
|
|
label.text = text
|
|
label.position = Vector2(18.0, 14.0)
|
|
var layer: CanvasLayer = CanvasLayer.new()
|
|
layer.add_child(label)
|
|
add_child(layer)
|