Files
tidewall/test/client/test_figure_rig.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

255 lines
9.1 KiB
GDScript

extends TestCase
## A2: the player figure's parts and joints, its animations, and that it animates from
## replicated state only: a client's mirrored entity (snapshot fields and FLAG_ bits)
## and FX records.
var tree: SceneTree
var figure: FigureRig
var player: SimEntity
func before_each() -> void:
super.before_each()
tree = Engine.get_main_loop() as SceneTree
figure = FigureRig.new(Palette.OCHRE_RED)
tree.root.add_child(figure)
player = SimEntity.new(7, SimEntity.ARCHETYPE_PLAYER)
player.health = 100
player.on_ground = true
func after_each() -> void:
if is_instance_valid(figure):
figure.free()
FigureRig.clear()
super.after_each()
func test_every_part_is_built_from_the_generated_models() -> void:
var layout: Dictionary = FigureRig.layout()
assert_eq(layout.get("generator", ""), "tools/models/figure.py")
for part: String in FigureRig.PART_JOINT:
var mesh: ArrayMesh = FigureRig.part_mesh(part, Palette.OILSKIN)
assert_not_null(mesh, "%s has a model" % part)
if mesh == null:
continue
for surface: int in mesh.get_surface_count():
var name: String = mesh.surface_get_name(surface)
assert_true(name.begins_with("COAT") or FigureRig.COLOURS.has(name), "%s's %s is a Palette colour" % [part, name])
assert_eq(FigureRig.colour_of("COAT", Palette.SEA_PAINT), Palette.SEA_PAINT, "the coat is the player's own")
assert_eq(FigureRig.colour_of("SKIN", Palette.SEA_PAINT), Palette.SKIN)
func test_standing_figure_is_player_sized_with_feet_on_the_ground() -> void:
figure.drive(player, 0.0)
figure.advance(0.0)
var bounds: AABB = _bounds()
assert_near(bounds.position.y, 0.0, 0.06, "feet at the origin")
assert_near(bounds.end.y, Tuning.PLAYER_HEIGHT, 0.12, "as tall as the player's body")
assert_true(bounds.size.x < 0.9 and bounds.size.z < 1.0, "no bigger round than a person with arms out: %s" % bounds.size)
assert_true(figure.relative(figure.hand()).origin.x < 0.0, "the right hand is on the -X side")
func test_crouching_brings_the_head_down_to_crouch_height() -> void:
player.crouching = true
figure.drive(player, 0.0)
_run(0.6)
var top: float = _bounds().end.y
assert_true(top < Tuning.CROUCH_HEIGHT + 0.2 and top > Tuning.CROUCH_HEIGHT - 0.35, "crouched head height %f" % top)
func test_states_follow_the_replicated_flags() -> void:
assert_eq(FigureRig.state_of(player, false), "ground")
player.crouching = true
assert_eq(FigureRig.state_of(player, false), "crouch")
assert_eq(FigureRig.state_of(player, true), "air")
player.swimming = true
assert_eq(FigureRig.state_of(player, true), "swim")
for drop: Array in [[Drop.ABOARD, "aboard"], [Drop.FREEFALL, "skydive"], [Drop.GLIDING, "glide"]]:
player.drop_state = drop[0]
assert_eq(FigureRig.state_of(player, false), drop[1])
func test_a_short_hop_off_the_ground_doesnt_flicker_into_the_air() -> void:
player.on_ground = false
figure.drive(player, 0.05)
assert_eq(figure.state, "ground", "under AIR_DELAY")
figure.drive(player, 0.1)
assert_eq(figure.state, "air")
player.on_ground = true
figure.drive(player, 0.02)
assert_eq(figure.state, "ground")
func test_speed_comes_from_movement_when_velocity_isnt_replicated() -> void:
# A mirrored player: only positions arrive, velocity stays zero.
for step: int in 40:
player.position = Vector3(0.0, 0.0, step * Tuning.WALK_SPEED / 30.0)
figure.drive(player, 1.0 / 30.0)
assert_near(figure.speed, Tuning.WALK_SPEED, 0.3)
player.position += Vector3(80.0, 0.0, 0.0)
figure.drive(player, 1.0 / 30.0)
assert_true(figure.speed < Tuning.WALK_SPEED, "a teleport isn't a sprint")
func test_walking_swings_the_legs_and_standing_still_doesnt() -> void:
figure.drive(player, 0.0)
var still: Array[float] = _leg_range(1.5)
assert_true(still[1] - still[0] < 0.05, "idle legs barely move: %s" % [still])
player.velocity = Vector3(0.0, 0.0, Tuning.SPRINT_SPEED)
figure.drive(player, 0.0)
var running: Array[float] = _leg_range(1.5)
assert_true(running[1] - running[0] > 1.0, "running legs swing: %s" % [running])
func test_every_state_poses_differently() -> void:
var poses: Dictionary = {}
var states: Array = [
["ground", func() -> void: pass],
["crouch", func() -> void: player.crouching = true],
["swim", func() -> void: player.swimming = true],
["skydive", func() -> void: player.drop_state = Drop.FREEFALL],
["glide", func() -> void: player.drop_state = Drop.GLIDING],
]
for entry: Array in states:
player = SimEntity.new(7, SimEntity.ARCHETYPE_PLAYER)
player.on_ground = true
(entry[1] as Callable).call()
figure.drive(player, 0.0)
_run(0.8)
assert_eq(figure.state, entry[0])
poses[entry[0]] = _signature()
for a: String in poses:
for b: String in poses:
if a < b:
assert_true((poses[a] as Vector3).distance_to(poses[b]) > 0.15, "%s and %s look different" % [a, b])
func test_glide_shows_the_sail_and_hides_the_held_item() -> void:
player.drop_state = Drop.GLIDING
player.on_ground = false
figure.drive(player, 0.0)
assert_true(figure.sail().visible)
assert_false(figure.hand().visible)
func test_fx_play_the_actions_on_the_upper_body() -> void:
figure.drive(player, 0.0)
_run(0.3)
var before: Quaternion = (figure.joints["shoulder_r"] as Node3D).quaternion
figure.hear({"kind": NetFx.SHOT, "item": -1, "player": 7})
assert_false(figure.acting(), "starts on the next step")
_run(0.16)
assert_true(figure.acting(), "a boathook swing plays")
var during: Quaternion = (figure.joints["shoulder_r"] as Node3D).quaternion
assert_true(before.angle_to(during) > 0.5, "the arm swings back")
_run(0.6)
assert_false(figure.acting(), "and finishes")
figure.hear({"kind": NetFx.PLACE, "item": -1, "player": 7, "piece": 3})
_run(0.1)
assert_true(figure.acting(), "placing a piece plays the mallet")
_run(0.6)
figure.hear({"kind": NetFx.SHOT, "item": ItemCatalog.code_of("dock_carbine"), "player": 7})
_run(0.05)
assert_true(figure.acting(), "a shot kicks")
func test_swimming_figures_dont_act() -> void:
player.swimming = true
figure.drive(player, 0.0)
figure.hear({"kind": NetFx.SHOT, "item": -1, "player": 7})
_run(0.1)
assert_false(figure.acting())
func test_losing_health_flinches_and_reloading_moves_the_left_arm() -> void:
figure.drive(player, 0.0)
_run(0.3)
player.health = 80
figure.drive(player, 0.0)
_run(0.05)
assert_true(figure.acting(), "a flinch")
_run(0.5)
player.slot_items = PackedInt32Array([ItemCatalog.code_of("dock_carbine")])
player.slot_tiers = PackedInt32Array([1])
player.selected = 1
figure.drive(player, 0.0)
_run(0.4)
var resting: Quaternion = (figure.joints["elbow_l"] as Node3D).quaternion
player.reload_ticks = 1
figure.drive(player, 0.0)
_run(0.4)
assert_true(resting.angle_to((figure.joints["elbow_l"] as Node3D).quaternion) > 0.4, "the left hand goes to the breech")
func test_holds_by_item() -> void:
assert_eq(FigureRig.hold_of(-1), "long", "the boathook in both hands")
assert_eq(FigureRig.hold_of(ItemCatalog.code_of("whalers_longgun")), "long")
assert_eq(FigureRig.hold_of(ItemCatalog.code_of("signal_revolver")), "short")
assert_eq(FigureRig.hold_of(ItemCatalog.code_of("brine_tonic")), "carry")
assert_eq(FigureRig.hold_of(ItemCatalog.code_of("powder_keg")), "carry")
func test_the_held_item_points_along_the_look_whatever_the_arm_does() -> void:
player.pitch = -PlayerCommand.PITCH_MAX / 3
player.velocity = Vector3(0.0, 0.0, Tuning.SPRINT_SPEED)
figure.drive(player, 0.0)
var pitch: float = float(player.pitch) / PlayerCommand.PITCH_MAX * (PI * 0.5)
for step: int in 20:
_run(0.05)
var muzzle: Vector3 = figure.hand_basis() * Vector3(0.0, 0.0, 1.0)
assert_near(muzzle.y, sin(pitch), 0.02, "step %d" % step)
assert_near(muzzle.x, 0.0, 0.02)
func test_mirrored_players_carry_reload_and_use_flags() -> void:
var record: PackedInt32Array = NetSnapshot.record_of(player)
player.reload_ticks = 9
record = NetSnapshot.record_of(player)
assert_true(record[NetSnapshot.R_FLAGS] & NetSnapshot.FLAG_RELOADING != 0)
func _run(seconds: float) -> void:
var left: float = seconds
while left > 0.0:
var step: float = minf(left, 1.0 / 30.0)
figure.advance(step)
left -= step
## The lowest and highest the right thigh turns over a time.
func _leg_range(seconds: float) -> Array[float]:
var low: float = INF
var high: float = -INF
var left: float = seconds
while left > 0.0:
figure.advance(1.0 / 30.0)
left -= 1.0 / 30.0
var turn: float = (figure.joints["hip_r"] as Node3D).rotation.x
low = minf(low, turn)
high = maxf(high, turn)
return [low, high]
## Where the head and both hands are, summed into one vector to compare poses.
func _signature() -> Vector3:
var head: Vector3 = figure.relative(figure.joints["neck"]).origin
var hand_l: Vector3 = figure.relative(figure.joints["elbow_l"]).origin
var knee: Vector3 = figure.relative(figure.joints["knee_r"]).origin
return head * 1.0 + hand_l * 0.5 + knee * 0.25
## The figure's drawn bounds, in its own space, from every part's mesh.
func _bounds() -> AABB:
var out: AABB = AABB()
var first: bool = true
for node: Node in figure.find_children("*", "MeshInstance3D", true, false):
var part: MeshInstance3D = node
if part.mesh == null or not part.is_visible_in_tree() or part.name == "sail":
continue
var box: AABB = figure.relative(part) * part.mesh.get_aabb()
out = box if first else out.merge(box)
first = false
return out