A9: In-game HUD, with a FEED message for the kill feed and who stands
The HUD (src/client/hud) is drawn in the paper-and-ink motifs of the style
guide:
- top centre: the tide post, the most prominent thing on screen. It is a
notched post with the sea filled in, the next level's rust notch and the
player's own height. Beside it are the phase, the time to the rise, both
levels and how far the player stands above or below the next line;
- top right: how many are standing, and the kill feed in words;
- bottom left: health and armour, each with a number, a word, quarter notches
and its own hatching. Low health adds LOW;
- bottom centre: the boathook and five slots, with names on two lines, rounds
and reserve or counts, and tier pips;
- bottom right: materials, and the build line ("NOT ENOUGH" when short).
Snapshots only carry nearby players, so the server now sends FEED (NetFeed,
reliable) when anyone is placed and at each phase change. It carries who is
standing, the match size and the newest 16 entries with names. Bots get crew
names (CrewNames). HudState works out what to show from the client, or from a
server's match for captures and tests, so the drawing never reaches into
either.
The project now stretches canvas items from a 1280x720 base, so the HUD scales
to any screen. make hud-captures renders it at 1280x720 and 3840x2160. The
parser fuzzer covers FEED.
Closes #49
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
[gd_scene load_steps=2 format=3]
|
||||
|
||||
[ext_resource type="Script" path="res://test/capture/client_scene.gd" id="1"]
|
||||
|
||||
[node name="CaptureClient" type="Node3D"]
|
||||
script = ExtResource("1")
|
||||
stage = "hud"
|
||||
@@ -0,0 +1,7 @@
|
||||
[gd_scene load_steps=2 format=3]
|
||||
|
||||
[ext_resource type="Script" path="res://test/capture/client_scene.gd" id="1"]
|
||||
|
||||
[node name="CaptureClient" type="Node3D"]
|
||||
script = ExtResource("1")
|
||||
stage = "hud_build"
|
||||
@@ -9,6 +9,10 @@
|
||||
## 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. Every stage draws the HUD as the client's HudState has it.
|
||||
extends Node3D
|
||||
|
||||
const DT: float = 1.0 / Tuning.TICK_RATE_HZ
|
||||
@@ -64,7 +68,7 @@ func _ready() -> void:
|
||||
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":
|
||||
"landed", "basin", "build", "build_refused", "hud", "hud_build":
|
||||
reached = me.drop_state == Drop.NONE
|
||||
if reached and since < 0:
|
||||
since = 0
|
||||
@@ -84,6 +88,14 @@ func _ready() -> void:
|
||||
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
|
||||
_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
|
||||
@@ -95,6 +107,9 @@ func _ready() -> void:
|
||||
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,
|
||||
@@ -102,6 +117,53 @@ func _ready() -> void:
|
||||
] + 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:
|
||||
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()]
|
||||
|
||||
|
||||
## 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.
|
||||
@@ -229,16 +291,16 @@ func _exit_tree() -> void:
|
||||
_net.close()
|
||||
|
||||
|
||||
func _caption(text: String) -> void:
|
||||
func _caption(text: String, at: Vector2 = Vector2(18.0, 14.0), font_size: int = 20) -> void:
|
||||
var settings: LabelSettings = LabelSettings.new()
|
||||
settings.font_size = 20
|
||||
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 = Vector2(18.0, 14.0)
|
||||
label.position = at
|
||||
var layer: CanvasLayer = CanvasLayer.new()
|
||||
layer.add_child(label)
|
||||
add_child(layer)
|
||||
|
||||
@@ -0,0 +1,203 @@
|
||||
extends TestCase
|
||||
|
||||
## Issue A9: the in-game HUD, the FEED message that tells it who is standing and who
|
||||
## fell, and HudState, which works out what it shows.
|
||||
|
||||
const FRAME: float = 1.0 / 60.0
|
||||
|
||||
|
||||
func _rules() -> MatchRules:
|
||||
var rules: MatchRules = MatchRules.new()
|
||||
rules.max_players = 4
|
||||
rules.min_players = 1
|
||||
rules.lobby_ticks = 6
|
||||
rules.countdown_ticks = 6
|
||||
rules.results_ticks = 6
|
||||
return rules
|
||||
|
||||
|
||||
func test_feed_round_trips() -> void:
|
||||
var entries: Array[Dictionary] = [
|
||||
{"victim": -3, "by": 1, "cause": "weapon", "item": 2, "place": 4, "tied": false, "victim_name": "Thole", "by_name": "Ada"},
|
||||
{"victim": 1, "by": 0, "cause": "tide", "item": -1, "place": 3, "tied": true, "victim_name": "Ada", "by_name": ""},
|
||||
]
|
||||
var told: Dictionary = NetFeed.decode(NetFeed.encode(3, 24, entries))
|
||||
assert_eq(told["standing"], 3)
|
||||
assert_eq(told["total"], 24)
|
||||
assert_eq(told["entries"].size(), 2)
|
||||
for index: int in 2:
|
||||
for key: String in entries[index]:
|
||||
assert_eq(told["entries"][index][key], entries[index][key], "%s of entry %d" % [key, index])
|
||||
|
||||
|
||||
func test_feed_keeps_only_the_newest_entries_and_cuts_long_names_on_characters() -> void:
|
||||
var entries: Array[Dictionary] = []
|
||||
for index: int in NetFeed.MAX_ENTRIES + 5:
|
||||
entries.append({"victim": -index - 1, "cause": "drown", "place": 24 - index, "victim_name": "ÆØÅ".repeat(20)})
|
||||
var told: Dictionary = NetFeed.decode(NetFeed.encode(1, 24, entries))
|
||||
assert_eq(told["entries"].size(), NetFeed.MAX_ENTRIES)
|
||||
assert_eq(told["entries"][NetFeed.MAX_ENTRIES - 1]["victim"], -(NetFeed.MAX_ENTRIES + 5), "the newest is last")
|
||||
var name: String = told["entries"][0]["victim_name"]
|
||||
assert_true(name.to_utf8_buffer().size() <= NetFeed.MAX_NAME_BYTES)
|
||||
assert_true(name.begins_with("ÆØÅ") and name.length() == NetFeed.MAX_NAME_BYTES / 2, "cut between characters: %s" % name)
|
||||
|
||||
|
||||
func test_malformed_feed_is_refused() -> void:
|
||||
var good: PackedByteArray = NetFeed.encode(2, 4, [{"victim": -1, "cause": "fall", "victim_name": "Kelp"}] as Array[Dictionary])
|
||||
assert_false(NetFeed.decode(good).is_empty())
|
||||
for cut: int in good.size():
|
||||
assert_true(NetFeed.decode(good.slice(0, cut)).is_empty(), "cut at %d" % cut)
|
||||
var longer: PackedByteArray = good.duplicate()
|
||||
longer.append(0)
|
||||
assert_true(NetFeed.decode(longer).is_empty(), "trailing bytes")
|
||||
var bad_cause: PackedByteArray = good.duplicate()
|
||||
bad_cause[NetFeed.HEADER_BYTES + 8] = NetFeed.CAUSES.size()
|
||||
assert_true(NetFeed.decode(bad_cause).is_empty(), "unknown cause")
|
||||
var many: PackedByteArray = good.duplicate()
|
||||
many[3] = NetFeed.MAX_ENTRIES + 1
|
||||
assert_true(NetFeed.decode(many).is_empty(), "too many entries")
|
||||
|
||||
|
||||
func test_feed_lines_are_words_and_name_you() -> void:
|
||||
var flint: int = ItemCatalog.code_of(String(Tuning.WEAPONS.keys()[1]))
|
||||
var entries: Array[Dictionary] = [
|
||||
{"victim": -2, "by": 1, "cause": "weapon", "item": flint, "victim_name": "Skerry", "by_name": "Ada"},
|
||||
{"victim": 1, "by": -4, "cause": "weapon", "item": -1, "victim_name": "Ada", "by_name": "Scupper"},
|
||||
{"victim": -5, "by": 0, "cause": "drown", "victim_name": "Cockle"},
|
||||
{"victim": -6, "by": 0, "cause": "tide", "victim_name": "Fathom"},
|
||||
{"victim": -7, "by": 0, "cause": "left", "victim_name": "Lugworm"},
|
||||
{"victim": -8, "by": 0, "cause": "fall", "victim_name": "Pollack"},
|
||||
]
|
||||
var lines: Array[Dictionary] = HudState.feed_lines(entries, 1)
|
||||
assert_eq(lines.size(), HudState.FEED_LINES, "only the newest")
|
||||
assert_eq(lines[0]["text"], "Scupper took you")
|
||||
assert_true(lines[0]["you"])
|
||||
assert_eq(lines[1]["text"], "Cockle drowned")
|
||||
assert_eq(lines[2]["text"], "The tide took Fathom")
|
||||
assert_eq(lines[3]["text"], "Lugworm left")
|
||||
assert_eq(lines[4]["text"], "Pollack fell")
|
||||
assert_false(lines[4]["you"])
|
||||
var first: Array[Dictionary] = HudState.feed_lines(entries.slice(0, 1), 1)
|
||||
assert_eq(first[0]["text"], "You took Skerry with %s" % HudState.item_name(ItemCatalog.get_def(flint).id).to_lower())
|
||||
|
||||
|
||||
func test_crew_names_are_stable_and_distinct() -> void:
|
||||
assert_eq(CrewNames.for_bot(-1), CrewNames.NAMES[0])
|
||||
var seen: Dictionary = {}
|
||||
for handle: int in range(-1, -49, -1):
|
||||
var name: String = CrewNames.for_bot(handle)
|
||||
assert_false(seen.has(name), "%s used twice" % name)
|
||||
seen[name] = true
|
||||
|
||||
|
||||
func test_offline_client_is_told_who_stands_and_who_fell() -> void:
|
||||
var game: ClientGame = ClientGame.new()
|
||||
var local: LocalServer = LocalServer.new(5, 3, _rules())
|
||||
for _tick: int in 200:
|
||||
local.tick(game)
|
||||
if game.playing():
|
||||
break
|
||||
assert_true(game.playing(), "the match began")
|
||||
assert_eq(game.match_size, 4)
|
||||
assert_eq(game.standing, 4)
|
||||
var flow: Match = local.loop.match_flow
|
||||
var bot: int = flow.bot_handles.keys()[0]
|
||||
var bot_player: SimEntity = flow.sim.entities.find(flow.entity_of[bot])
|
||||
bot_player.health = 0
|
||||
bot_player.last_cause = SimEntity.CAUSE_DROWN
|
||||
local.tick(game)
|
||||
assert_eq(game.standing, 3, "told at once")
|
||||
assert_eq(game.feed.size(), 1)
|
||||
assert_eq(game.feed[0]["victim"], bot)
|
||||
assert_eq(game.feed[0]["victim_name"], CrewNames.for_bot(bot))
|
||||
assert_eq(game.feed[0]["cause"], "drown")
|
||||
assert_eq(game.bad_packets, 0)
|
||||
var state: HudState = HudState.from_client(game, ClientInput.new(), LocalServer.HANDLE)
|
||||
assert_eq(state.feed[0]["text"], "%s drowned" % CrewNames.for_bot(bot))
|
||||
assert_true(state.playing)
|
||||
assert_eq(state.health, Tuning.PLAYER_MAX_HEALTH)
|
||||
assert_eq(state.slots.size(), Tuning.INVENTORY_SLOTS + 1, "the boathook and the slots")
|
||||
assert_true(state.slots[0]["selected"], "the boathook is held at first")
|
||||
assert_false(state.tide.is_empty())
|
||||
assert_eq(state.tide["phase"], 1)
|
||||
assert_true(state.tide["next"] >= state.tide["sea"])
|
||||
|
||||
|
||||
func test_from_match_reads_the_server_player_and_build_mode() -> void:
|
||||
var loop: ServerLoop = ServerLoop.new(5, 1, 3, _rules())
|
||||
loop.play_humans_by_script()
|
||||
while not loop.match_flow.in_match():
|
||||
loop.run_tick()
|
||||
for _tick: int in 30:
|
||||
loop.run_tick()
|
||||
var flow: Match = loop.match_flow
|
||||
var handle: int = flow.participants.filter(func(h: int) -> bool: return h > 0)[0]
|
||||
var me: SimEntity = flow.sim.entities.find(flow.entity_of[handle])
|
||||
me.materials[Materials.STONE] = 5
|
||||
var state: HudState = HudState.from_match(flow, handle, true, StructureSet.WALL, Materials.STONE)
|
||||
assert_true(state.playing)
|
||||
assert_eq(state.build["kind"], "wall")
|
||||
assert_eq(state.build["material"], "stone")
|
||||
assert_false(state.build["affordable"], "5 stone isn't a wall")
|
||||
me.materials[Materials.STONE] = 50
|
||||
assert_true(HudState.from_match(flow, handle, true, StructureSet.WALL, Materials.STONE).build["affordable"])
|
||||
assert_true(HudState.from_match(flow, handle).build.is_empty(), "not building")
|
||||
assert_eq(state.standing, 4)
|
||||
assert_false(is_nan(float(state.tide["feet"])))
|
||||
|
||||
|
||||
func test_hud_shows_only_what_the_state_holds() -> void:
|
||||
var hud: Hud = Hud.new()
|
||||
var empty: HudState = HudState.new()
|
||||
hud.show_state(empty)
|
||||
assert_eq(_texts(hud), [] as Array[String], "nothing to show before a match")
|
||||
var state: HudState = HudState.new()
|
||||
state.playing = true
|
||||
state.health = 20
|
||||
state.armor = 50
|
||||
state.standing = 7
|
||||
state.total = 24
|
||||
state.tide = {"phase": 2, "phases": 6, "stage": Tide.HOLD, "sea": 3.0, "next": 6.5, "rise_in": 42.0, "feet": 4.0}
|
||||
state.slots = [{"selected": true, "empty": false, "name": "Boathook", "count": 1, "rounds": -1, "reserve": -1, "tier": -1}] as Array[Dictionary]
|
||||
state.feed = [{"text": "Kelp drowned", "you": false}] as Array[Dictionary]
|
||||
hud.show_state(state)
|
||||
var texts: Array[String] = _texts(hud)
|
||||
assert_true(texts.has("7 of 24 standing"), "%s" % [texts])
|
||||
assert_true(texts.has("Kelp drowned"))
|
||||
assert_true(texts.has("Driftwood 0"))
|
||||
hud.show_state(empty)
|
||||
texts = _texts(hud)
|
||||
assert_false(texts.has("Kelp drowned"), "an empty feed clears the lines")
|
||||
hud.free()
|
||||
|
||||
|
||||
func test_session_shows_a_hud_while_playing_offline() -> void:
|
||||
var session: ClientSession = ClientSession.new()
|
||||
session.input.autoplay = true
|
||||
session._ready()
|
||||
session.play_offline(2, 3, _rules())
|
||||
for _frame: int in 60 * 20:
|
||||
session.pump(FRAME)
|
||||
if session.game.playing():
|
||||
break
|
||||
assert_true(session.game.playing())
|
||||
var state: HudState = session.hud_state()
|
||||
assert_true(state.playing)
|
||||
assert_eq(state.total, 4)
|
||||
session.hud.show_state(state)
|
||||
assert_true(_texts(session.hud).has("4 of 4 standing"))
|
||||
session.free()
|
||||
|
||||
|
||||
## The text of every label shown: visible itself and in every parent up to the HUD.
|
||||
func _texts(node: Node) -> Array[String]:
|
||||
var found: Array[String] = []
|
||||
var item: CanvasItem = node as CanvasItem
|
||||
if item != null and not item.visible:
|
||||
return found
|
||||
var label: Label = node as Label
|
||||
if label != null:
|
||||
found.append(label.text)
|
||||
for child: Node in node.get_children():
|
||||
found.append_array(_texts(child))
|
||||
return found
|
||||
@@ -0,0 +1 @@
|
||||
uid://dd8x7krt170h0
|
||||
@@ -1,5 +1,5 @@
|
||||
## Feeds malformed messages to every parser that reads bytes from the network: the
|
||||
## handshake, inputs, snapshots, match notices, world and inventory messages, and
|
||||
## handshake, inputs, snapshots, match notices, world and inventory messages, feeds, and
|
||||
## discovery, plus the stateful snapshot receiver and the list server's handler.
|
||||
## Messages are mutations of real ones taken from a running match (bit flips,
|
||||
## truncation, extension, splices, runs of 0x00 and 0xFF, a different type byte) and
|
||||
@@ -57,6 +57,10 @@ func collect_seeds() -> void:
|
||||
{"kind": NetFx.HARVEST, "tick": 92, "player": 4, "item": -1, "position": Vector3(50.0, 9.0, 60.0), "prop": 12, "piece": 0},
|
||||
] as Array[Dictionary]))
|
||||
var command: PlayerCommand = PlayerCommand.from_input(12, Vector2(0.3, 1.0), 1.0, 0.2, PlayerCommand.BUTTON_FIRE, 2, 1)
|
||||
seeds.append(NetFeed.encode(9, 24, [
|
||||
{"victim": -3, "by": 2, "cause": "weapon", "item": 2, "place": 10, "victim_name": "Thole", "by_name": "Fuzzer"},
|
||||
{"victim": 2, "by": 0, "cause": "tide", "item": -1, "place": 9, "tied": true, "victim_name": "Fuzzer", "by_name": ""},
|
||||
] as Array[Dictionary]))
|
||||
seeds.append(NetSnapshot.encode_input(40, [command, command] as Array[PlayerCommand]))
|
||||
seeds.append(NetProtocol.encode_hello("Fuzzer"))
|
||||
seeds.append(NetProtocol.encode_welcome(3, 77, 24, "Fuzzer"))
|
||||
@@ -105,7 +109,7 @@ func feed(bytes: PackedByteArray) -> void:
|
||||
var results: Array = [
|
||||
NetProtocol.decode_hello(bytes), NetProtocol.decode_welcome(bytes), NetProtocol.decode_reject(bytes),
|
||||
NetSnapshot.decode_input(bytes), NetSnapshot.decode_snapshot(bytes), NetSnapshot.decode_match(bytes),
|
||||
NetWorld.decode_world(bytes), NetWorld.decode_own(bytes), NetFx.decode(bytes),
|
||||
NetWorld.decode_world(bytes), NetWorld.decode_own(bytes), NetFx.decode(bytes), NetFeed.decode(bytes),
|
||||
NetDiscovery.decode_query(bytes), NetDiscovery.decode_info(bytes), NetDiscovery.decode_register(bytes),
|
||||
NetDiscovery.decode_challenge(bytes), NetDiscovery.decode_list(bytes),
|
||||
]
|
||||
@@ -157,6 +161,6 @@ func _mutate(bytes: PackedByteArray) -> void:
|
||||
var types: Array[int] = [
|
||||
NetProtocol.MSG_HELLO, NetProtocol.MSG_WELCOME, NetProtocol.MSG_REJECT, NetSnapshot.MSG_MATCH,
|
||||
NetWorld.MSG_WORLD, NetWorld.MSG_OWN, NetSnapshot.MSG_SNAPSHOT, NetSnapshot.MSG_INPUT,
|
||||
NetFx.MSG_FX,
|
||||
NetFx.MSG_FX, NetFeed.MSG_FEED,
|
||||
]
|
||||
bytes[0] = types[rng.randi_range(0, types.size() - 1)]
|
||||
|
||||
Reference in New Issue
Block a user