Files
tidewall/test/net/test_net_fx.gd
T
claudeandClaude Opus 5.5 07738330b5 A2: Player figure and animation
The islander is built in headless Blender (tools/models/figure.py,
`make figure`, `make art`) as twelve rigid parts with their origins at
their joints: an oilskin coat, a sou'wester, sea boots, a bedding roll
and the canvas wing-sail. Debian's Blender can't write glTF, so the
figure is jointed rather than skinned.

FigureRig hangs the parts on pivots, paints them from Palette with the
coat in the player's own oilskin, and plays FigurePoses' hand-written
key poses through one AnimationTree: idle, walk and run by speed,
crouch and creep, air, skydive, glide, swim and aboard; long, short and
carried holds; reload and use layers; fire, boathook swing, keg throw
and building as one-shots; and a flinch when health drops. The chest
takes a third of the pitch and the held item all of it.

Figures animate from replicated state only: positions, look, health,
the held item and the FLAG_ bits (reloading and using now reach
mirrored players), with actions from FX. Every boathook swing is now a
sim "swing" event, sent as a SHOT with no item, so misses are seen and
heard; the new FX kind PLACE shows pieces placed. Protocol version 4.

Tests: test_figure_rig (15), plus swing and PLACE cases in test_harvest
and test_net_fx. make check passes (604 tests); make client-check
passes. Captures: figure_states, figure_actions, figure_close
(make figure-captures), client_glide and client_landed.

Closes #42

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 17:35:53 -05:00

82 lines
4.2 KiB
GDScript

extends TestCase
## Issue N11: shots, impacts and swings on the wire (NetFx). A2 added boathook swings
## that miss (a SHOT with no item) and pieces placed (PLACE), for the figures.
func _records() -> Array[Dictionary]:
return [
{"kind": NetFx.SHOT, "tick": 400, "player": 3, "item": 2, "position": Vector3(100.25, 12.5, 200.0), "yaw": 1.2345, "pitch": -0.25},
{"kind": NetFx.IMPACT, "tick": 401, "player": 3, "item": 2, "position": Vector3(130.0, 11.0, 220.0), "hit": Weapons.HIT_PLAYER, "target": 700},
{"kind": NetFx.HARVEST, "tick": 402, "player": 4, "item": -1, "position": Vector3(50.0, 9.0, 60.0), "prop": 12, "piece": 0},
{"kind": NetFx.HARVEST, "tick": 402, "player": 5, "item": -1, "position": Vector3(51.0, 9.0, 61.0), "prop": -1, "piece": 33},
{"kind": NetFx.PLACE, "tick": 402, "player": 6, "item": -1, "position": Vector3(52.0, 10.5, 62.5), "piece": 41},
]
func test_records_survive_the_trip() -> void:
var bytes: PackedByteArray = NetFx.encode(_records())
var message: Dictionary = NetFx.decode(bytes)
assert_false(message.is_empty())
assert_eq(message["tick"], 402)
var events: Array = message["events"]
assert_eq(events.size(), 5)
for index: int in 5:
var sent: Dictionary = _records()[index]
var got: Dictionary = events[index]
for key: String in sent:
if key == "position":
assert_true((got[key] as Vector3).distance_to(sent[key]) <= 0.06, "%s within 6 cm" % key)
elif key == "yaw" or key == "pitch":
assert_near(float(got[key]), float(sent[key]), 0.0001)
else:
assert_eq(got[key], sent[key], "event %d %s" % [index, key])
assert_true(NetFx.aim(events[0]).is_normalized())
func test_an_event_from_the_simulation_becomes_a_record() -> void:
var direction: Vector3 = Vector3(0.3, -0.2, 0.9).normalized()
var shot: Dictionary = NetFx.from_event({"type": "shot", "player": 9, "item": 4, "from": Vector3(1.0, 2.0, 3.0), "direction": direction}, 77)
assert_eq(shot["kind"], NetFx.SHOT)
assert_true(NetFx.aim(shot).distance_to(direction) < 0.0001, "the aim comes back")
var swing: Dictionary = NetFx.from_event({"type": "harvest", "player": 9, "prop": 5, "material": 0, "amount": 3}, 77, {9: Vector3(4.0, 5.0, 6.0)})
assert_eq(swing["position"], Vector3(4.0, 5.0, 6.0))
assert_true(NetFx.from_event({"type": "pickup", "player": 9}, 77).is_empty(), "only FX events")
func test_too_many_keep_the_newest_and_the_byte_limit() -> void:
var many: Array[Dictionary] = []
for index: int in 300:
many.append({"kind": NetFx.SHOT, "tick": 1000 + index / 10, "player": index, "item": 1, "position": Vector3(index, 1.0, 2.0), "yaw": 0.0, "pitch": 0.0})
var bytes: PackedByteArray = NetFx.encode(many)
assert_true(bytes.size() <= Tuning.NET_SNAPSHOT_MAX_BYTES, "%d bytes" % bytes.size())
var events: Array = NetFx.decode(bytes)["events"]
assert_true(events.size() > 40)
assert_eq(int(events[events.size() - 1]["player"]), 299, "the newest is kept")
assert_eq(NetFx.encode([] as Array[Dictionary]).size(), 0)
func test_malformed_messages_are_refused() -> void:
var bytes: PackedByteArray = NetFx.encode(_records())
for cut: int in bytes.size():
assert_true(NetFx.decode(bytes.slice(0, cut)).is_empty(), "cut at %d" % cut)
var longer: PackedByteArray = bytes.duplicate()
longer.append(0)
assert_true(NetFx.decode(longer).is_empty(), "trailing bytes")
var bad_kind: PackedByteArray = bytes.duplicate()
bad_kind[NetFx.HEADER_BYTES] = 9
assert_true(NetFx.decode(bad_kind).is_empty(), "unknown kind")
func test_swings_and_placed_pieces_from_the_simulation() -> void:
var swing: Dictionary = NetFx.from_event({"type": "swing", "player": 9, "from": Vector3(1.0, 2.0, 3.0), "direction": Vector3.FORWARD}, 80)
assert_eq(swing["kind"], NetFx.SHOT)
assert_eq(swing["item"], -1, "a swing is a shot with no item")
var placed: Dictionary = NetFx.from_event({"type": "piece_placed", "player": 9, "piece": 12, "kind": 0, "material": 0}, 80, {9: Vector3.ONE}, {12: Vector3(4.0, 5.0, 6.0)})
assert_eq(placed["kind"], NetFx.PLACE)
assert_eq(placed["piece"], 12)
assert_eq(placed["position"], Vector3(4.0, 5.0, 6.0), "at the piece")
var unknown: Dictionary = NetFx.from_event({"type": "piece_placed", "player": 9, "piece": 13}, 80, {9: Vector3.ONE})
assert_eq(unknown["position"], Vector3.ONE, "at the builder, without the piece's centre")