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 var source: Mesh = load("%s/%s.obj" % [FigureRig.MODEL_DIR, part]) as Mesh var wanted: Dictionary = {} var finishes: Dictionary = {} for surface: int in source.get_surface_count(): var name: String = source.surface_get_material(surface).resource_name assert_true(name.begins_with("COAT") or FigureRig.COLOURS.has(name), "%s's %s is a Palette colour" % [part, name]) wanted[FigureRig.colour_of(name, Palette.OILSKIN)] = true finishes[FigureRig.finish_of(name)] = true # H1: merged to one surface per finish, the colours carried by the vertices. assert_eq(mesh.get_surface_count(), finishes.size(), "%s has one surface per finish" % part) var seen: Dictionary = {} for surface: int in mesh.get_surface_count(): assert_true(FigureRig.FINISHES.has(mesh.surface_get_name(surface))) var paint: StandardMaterial3D = mesh.surface_get_material(surface) as StandardMaterial3D assert_true(paint.vertex_color_use_as_albedo and paint.vertex_color_is_srgb, "%s is painted by its vertices" % part) var arrays: Array = mesh.surface_get_arrays(surface) for colour: Color in arrays[Mesh.ARRAY_COLOR] as PackedColorArray: seen[colour.to_html()] = colour # Vertex colours are stored with less precision than a Color. assert_eq(seen.size(), wanted.size(), "%s keeps every colour" % part) for colour: Color in wanted: var found: bool = false for kept: Color in seen.values(): found = found or (absf(kept.r - colour.r) < 0.01 and absf(kept.g - colour.g) < 0.01 and absf(kept.b - colour.b) < 0.01) assert_true(found, "%s still has colour %s" % [part, colour.to_html()]) assert_eq(mesh.get_faces().size(), source.get_faces().size(), "%s keeps every triangle" % part) 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.downed = true assert_eq(FigureRig.state_of(player, true), "downed", "M7: down on hands and knees") player.swimming = true assert_eq(FigureRig.state_of(player, true), "swim", "a downed swimmer still swims") player.downed = false 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.kite().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 == "kite": continue var box: AABB = figure.relative(part) * part.mesh.get_aabb() out = box if first else out.merge(box) first = false return out