## 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. ## hud and hud_build (issue A9): once landed, the server hands the player a carbine, a ## fowling piece and bandages, wounds them a little and takes three bots out in ## different ways, so every part of the HUD has something to show; hud_build then goes ## into build mode. hud_downed (issue M7) plays duos, with the first bot as the player's ## crewmate, and after the hud staging a bot's shot downs the player. Every stage draws ## the HUD as the client's HudState has it. 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 if stage == "hud_downed": rules.team_size = Tuning.DUO_TEAM_SIZE _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", "hud", "hud_build", "hud_downed": 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 elif stage.begins_with("hud"): basin_text = _stage_hud() if basin_text.is_empty(): return if stage == "hud_build" and _build_mode(true).is_empty(): return if stage == "hud_downed": var downed_text: String = _down_player() if downed_text.is_empty(): return basin_text += downed_text _session.hud.fade_feed = false _session.hud.show_state(_session.hud_state()) _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 if stage.begins_with("hud"): _caption("HUD · %s · seed %d · %s%s" % [stage.to_upper(), match_seed, flow.phase_name(), basin_text], Vector2(18.0, 132.0), 15) return _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) ## Gives the player things to carry and a wound, takes three bots out (one by the ## player's carbine, one drowned, one to the tide), and plays on a second so the client ## hears of it. Returns a caption line, or "" after reporting an error. func _stage_hud() -> String: var flow: Match = _loop.match_flow var sim: Simulation = _loop.sim var mine: SimEntity = 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 for the HUD") return "" Inventory.give(mine, ItemCatalog.code_of("dock_carbine"), 1, -1, 2) Inventory.give(mine, ItemCatalog.code_of("fowling_piece"), 1, -1, 0) Inventory.give(mine, ItemCatalog.code_of("sailcloth_bandage"), 3) Inventory.give(mine, ItemCatalog.code_of("cartridges"), 45) Inventory.select(mine, 1) mine.materials = PackedInt32Array([140, 60, 12]) mine.health = 64 mine.armor = 35 var causes: Array[int] = [SimEntity.CAUSE_WEAPON, SimEntity.CAUSE_DROWN, SimEntity.CAUSE_TIDE] var taken: int = 0 for bot: int in flow.bot_handles.keys(): var player: SimEntity = sim.entities.find(int(flow.entity_of.get(bot, 0))) if player == null or player.eliminated or sim.are_teammates(mine, player): continue player.health = 0 player.last_cause = causes[taken] player.last_attacker = mine.id if causes[taken] == SimEntity.CAUSE_WEAPON else 0 taken += 1 for _round: int in 3: _game.poll() _loop.run_tick() _game.after_tick() _session.pump(DT) if taken == causes.size(): break for _round: int in Tuning.TICK_RATE_HZ: _game.poll() _loop.run_tick() _game.after_tick() _session.pump(DT) var hud: HudState = _session.hud_state() if hud.feed.size() < 3 or not hud.playing or hud.tide.is_empty(): push_error("client capture: the HUD has %d feed lines, playing %s, tide %s" % [hud.feed.size(), hud.playing, hud.tide]) return "" return " · %d of %d standing, %d feed lines" % [hud.standing, hud.total, hud.feed.size()] ## Duos: a bot's shot takes the player's last health with their crewmate still up, so ## they go down. Returns a caption line, or "" after reporting an error. func _down_player() -> String: var flow: Match = _loop.match_flow var sim: Simulation = _loop.sim var mine: SimEntity = sim.entities.find(int(flow.entity_of.get(_session.link.handle, 0))) var foe: SimEntity = null for bot: int in flow.bot_handles.keys(): var player: SimEntity = sim.entities.find(int(flow.entity_of.get(bot, 0))) if player != null and not player.eliminated and not sim.are_teammates(mine, player): foe = player break if mine == null or foe == null or not sim.has_standing_teammate(mine): push_error("client capture: no foe, or no crewmate standing, to down the player") return "" mine.armor = 0 Weapons.damage_player(sim, mine, mine.health, foe.id, false, ItemCatalog.code_of("dock_carbine")) for _round: int in Tuning.TICK_RATE_HZ: _game.poll() _loop.run_tick() _game.after_tick() _session.pump(DT) var hud: HudState = _session.hud_state() if not mine.downed or not hud.downed: push_error("client capture: the player isn't down (server %s, HUD %s)" % [mine.downed, hud.downed]) return "" return " · downed, %d health to bleed" % mine.health ## 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, at: Vector2 = Vector2(18.0, 14.0), font_size: int = 20) -> void: var settings: LabelSettings = LabelSettings.new() settings.font_size = font_size 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 = at var layer: CanvasLayer = CanvasLayer.new() layer.add_child(label) add_child(layer)