Files
tidewall/test/client/test_hud.gd
T
claudeandClaude Opus 5.5 8142e39ab3 A13: Gamepad and accessibility
Gamepad: PadInput reads pad events in one fixed layout (sticks move and
look, triggers aim and fire, bumpers step slots and build pieces, Start
pauses), fed through ClientInput alongside the keys and mouse. Menus take
South to choose and East or Start to go back. The tutorial names pad
buttons once the pad is used.

Aim assist (option, on by default, pad only): friction across an
opponent in plain sight near the look, and a pull of at most 9 deg/s
while aiming. Client look only; never through cover.

Accessibility (new Access tab): colour-vision sets for the signal colours
(red-green, blue-yellow), checked by simulated colour vision in
test_palette; interface size 90-130%, with the HUD hotbar refitting at the
larger sizes; captions for key sounds with direction; reduce motion
(calmer aim zoom and hurt flash).

Tests for the pad, aim assist, captions, palettes, settings, the menus
driven from a pad, and a pad playing through a real offline session.
make access-captures renders the new screens and options.

Closes #53

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 08:53:30 -05:00

239 lines
10 KiB
GDScript

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
func test_a_down_in_duos_travels_in_the_feed_and_reads_as_a_down() -> void:
var entries: Array[Dictionary] = [
{"victim": -3, "by": 1, "cause": "weapon", "item": -1, "place": 0, "tied": false, "downed": true, "victim_name": "Thole", "by_name": "Ada"},
{"victim": -4, "by": 0, "cause": "fall", "item": -1, "place": 0, "tied": false, "downed": true, "victim_name": "Brine", "by_name": ""},
]
var told: Dictionary = NetFeed.decode(NetFeed.encode(3, 24, entries))
assert_true(told["entries"][0]["downed"])
var lines: Array[Dictionary] = HudState.feed_lines(told["entries"], 1)
assert_eq(lines[0]["text"], "You knocked Thole down")
assert_eq(lines[1]["text"], "Brine is down")
assert_eq(HudState.feed_lines(told["entries"], -3)[0]["text"], "Ada knocked you down")
func test_the_bottom_row_never_overlaps_at_any_interface_size() -> void:
# A13: the interface size makes the canvas narrower; the hotbar must stay clear of
# the vitals and the materials, and inside the screen.
for scale: float in GameSettings.UI_SCALES:
var hud: Hud = Hud.new()
var canvas: Vector2 = Vector2(1280.0, 720.0) / scale
hud.size = canvas
var state: HudState = HudState.new()
state.playing = true
for index: int in 6:
state.slots.append({"id": "", "name": "Slot", "count": 0, "rounds": 0, "reserve": 0, "magazine": 0, "tier": -1, "selected": index == 0, "empty": true})
hud.show_state(state)
var bar: Rect2 = hud.hotbar_rect()
assert_true(bar.position.x >= hud.vitals_rect().end.x + 1.0, "at %d%% the hotbar starts after the vitals: %s" % [roundi(scale * 100.0), bar])
var materials_left: float = canvas.x - Hud.MARGIN - hud.materials_rect().size.x
assert_true(bar.end.x <= materials_left - 1.0, "at %d%% the hotbar ends before the materials (%.0f, %.0f)" % [roundi(scale * 100.0), bar.end.x, materials_left])
assert_true(bar.size.x >= 6.0 * Hud.SLOT_MIN_WIDTH, "slots no narrower than the least")
if is_equal_approx(scale, 1.0):
assert_near(bar.get_center().x, canvas.x * 0.5, 1.0, "centred at 100%")
hud.free()