extends TestCase ## A8: what the match sounds like, from the client's world. Sounds are placed where they ## happen (so they are heard from that direction), and each event picks its own sound. const FRAME: float = 1.0 / 60.0 var tree: SceneTree var director: SoundDirector var game: ClientGame var world: Simulation func before_each() -> void: super.before_each() tree = Engine.get_main_loop() as SceneTree director = SoundDirector.new() tree.root.add_child(director) # Headless, the director plays nothing (SoundBank.has_output()); these tests check # what it plays and where, so they ask it to play into the Dummy driver anyway. director.audible = true game = ClientGame.new() world = Simulation.new(5, SimGround.flat(0.0)) world.sea_level = -5.0 game.world = world game.match_phase = Match.PLAY director.update(game, 0, Vector3.ZERO, FRAME) func after_each() -> void: director.free() SoundBank.clear() super.after_each() func _sounds() -> Array[String]: var out: Array[String] = [] for cue: Dictionary in director.cues: out.append(String(cue["sound"])) return out func _player(at: Vector3) -> SimEntity: var player: SimEntity = world.entities.find(world.add_player()) player.position = at player.on_ground = true player.drop_state = Drop.NONE player.health = 100 return player func _fx(record: Dictionary) -> void: game.fx.append(record) game.fx_received += 1 func test_a_shot_is_placed_where_it_was_fired_with_that_weapons_report() -> void: var at: Vector3 = Vector3(30.0, 2.0, -12.0) _fx({"kind": NetFx.SHOT, "tick": 1, "player": 1, "item": ItemCatalog.code_of("whalers_longgun"), "position": at}) director.update(game, 0, Vector3.ZERO, FRAME) assert_eq(_sounds(), ["shot_whalers_longgun"] as Array[String]) assert_true(bool(director.cues[0]["spatial"]), "positional") var voice: AudioStreamPlayer3D = director.voice_playing("shot_whalers_longgun") assert_not_null(voice, "played on a 3D voice") assert_eq(voice.global_position, at, "at the muzzle, so the engine pans it from there") assert_true(voice.max_distance >= 400.0, "gunfire carries") director.update(game, 0, Vector3.ZERO, FRAME) assert_eq(director.cues.size(), 0, "each FX record is heard once") func test_each_weapon_has_its_own_report() -> void: var seen: Dictionary = {} for id: String in ["fowling_piece", "dock_carbine", "signal_revolver", "rivet_repeater", "whalers_longgun", "line_thrower", "harpoon_gun"]: var sound: String = SoundDirector.shot_sound(ItemCatalog.code_of(id)) assert_eq(sound, "shot_" + id) assert_true(SoundBank.has(sound), "%s exists" % sound) seen[sound] = true assert_eq(SoundDirector.shot_sound(-1), "swing_boathook", "the boathook, which has no item code") assert_eq(SoundDirector.shot_sound(250), "shot_dock_carbine", "an unknown code still makes a sound") func test_impacts_sound_like_what_was_hit() -> void: var stone: StructureSet.Piece = world.structures.add(StructureSet.WALL, Materials.STONE, Vector4i(10, 0, 10, StructureSet.SLOT_WALL_X), 0, 0, 0, world.ground) director.update(game, 0, Vector3.ZERO, FRAME) var cases: Array = [ [Weapons.HIT_PIECE, stone.id, "hit_stone"], [Weapons.HIT_PLAYER, 3, "hit_player"], [Weapons.HIT_PROP, 4, "hit_wood"], [Weapons.HIT_TERRAIN, 0, "hit_ground"], ] for entry: Array in cases: _fx({"kind": NetFx.IMPACT, "tick": 2, "player": 1, "item": ItemCatalog.code_of("dock_carbine"), "position": Vector3(1, 1, 1), "hit": entry[0], "target": entry[1]}) director.update(game, 0, Vector3.ZERO, FRAME) assert_eq(_sounds(), [entry[2]] as Array[String]) _fx({"kind": NetFx.HARVEST, "tick": 3, "player": 1, "item": -1, "position": Vector3.ZERO}) director.update(game, 0, Vector3.ZERO, FRAME) assert_eq(_sounds(), ["chop"] as Array[String]) func _walk(player: SimEntity, velocity: Vector3, seconds: float, listener: Vector3 = Vector3.ZERO) -> Array[String]: var heard: Array[String] = [] for frame: int in int(seconds / FRAME): player.position += velocity * FRAME director.update(game, player.id, listener, FRAME) heard.append_array(_sounds()) return heard func test_footsteps_follow_the_stride_and_alternate_feet() -> void: var player: SimEntity = _player(Vector3(5.0, 0.0, 5.0)) director.update(game, player.id, Vector3.ZERO, FRAME) var heard: Array[String] = _walk(player, Vector3(5.0, 0.0, 0.0), 2.0) # 10 m at a 1.6 m stride. assert_eq(heard.size(), 6, "%s" % [heard]) assert_eq(heard[0], "step_grass_2") assert_eq(heard[1], "step_grass_1") for cue: Dictionary in director.cues: assert_true(bool(cue["spatial"])) var standing: Array[String] = _walk(player, Vector3.ZERO, 1.0) assert_eq(standing.size(), 0, "standing still is silent") func test_footsteps_know_the_surface() -> void: var player: SimEntity = _player(Vector3(5.0, 2.0, 5.0)) director.update(game, player.id, Vector3.ZERO, FRAME) var on_planks: Array[String] = _walk(player, Vector3(4.0, 0.0, 0.0), 1.0) assert_true(on_planks.size() > 0 and on_planks[0].begins_with("step_wood"), "above the ground means on a built piece: %s" % [on_planks]) player.position.y = 0.0 world.sea_level = 0.4 director.update(game, player.id, Vector3.ZERO, FRAME) var wading: Array[String] = _walk(player, Vector3(4.0, 0.0, 0.0), 1.0) assert_true(wading.size() > 0 and wading[0].begins_with("step_shallows"), "%s" % [wading]) func test_footsteps_on_a_real_island_by_band() -> void: var island_world: Simulation = Simulation.new(2) var found: Dictionary = {} var size: float = Tuning.WORLD_SIZE_METRES for gz: int in 60: for gx: int in 60: var x: float = size * (gx + 0.5) / 60.0 var z: float = size * (gz + 0.5) / 60.0 var feet: Vector3 = Vector3(x, island_world.ground.terrain_height(x, z), z) if feet.y <= island_world.sea_level + 0.1: continue found[SoundDirector.surface_at(island_world, feet)] = true for surface: String in ["sand", "grass", "rock"]: assert_true(found.has(surface), "the island has %s to walk on: %s" % [surface, found.keys()]) func test_far_footsteps_are_not_heard_but_near_ones_are() -> void: var far: SimEntity = _player(Vector3(200.0, 0.0, 200.0)) var near: SimEntity = _player(Vector3(10.0, 0.0, 0.0)) director.update(game, 0, Vector3.ZERO, FRAME) var heard: int = 0 for frame: int in 120: far.position.x += 5.0 * FRAME near.position.z += 5.0 * FRAME director.update(game, 0, Vector3.ZERO, FRAME) for cue: Dictionary in director.cues: heard += 1 assert_true(Vector3(cue["at"]).distance_to(near.position) < 1.0, "only the near player's steps") assert_eq(heard, 6) func test_landing_and_splashing() -> void: var player: SimEntity = _player(Vector3(0.0, 10.0, 0.0)) player.on_ground = false director.update(game, player.id, Vector3.ZERO, FRAME) _walk(player, Vector3(0.0, -12.0, 0.0), 0.5) player.on_ground = true assert_eq(_walk(player, Vector3.ZERO, FRAME), ["land"] as Array[String]) player.swimming = true assert_eq(_walk(player, Vector3.ZERO, FRAME), ["splash"] as Array[String]) func test_building_and_breaking_by_material() -> void: var piece: StructureSet.Piece = world.structures.add(StructureSet.FLOOR, Materials.SALVAGE_IRON, Vector4i(20, 0, 20, StructureSet.SLOT_FLOOR), 0, 0, 0, world.ground) director.update(game, 0, Vector3.ZERO, FRAME) assert_eq(_sounds(), ["build_salvage_iron"] as Array[String]) assert_true(Vector3(director.cues[0]["at"]).distance_to(piece.boxes[0].get_center()) < 0.01, "at the piece") world.structures.remove(piece.id, world.ground) director.update(game, 0, Vector3.ZERO, FRAME) assert_eq(_sounds(), ["break_salvage_iron"] as Array[String]) func test_the_tide_rings_and_sounds_its_horn() -> void: world.tide = Tide.new(0) world.tick = 1 director.update(game, 0, Vector3.ZERO, FRAME) assert_eq(_sounds(), [] as Array[String], "nothing on the first look") world.tick = world.tide.ticks_until_rise(1) + 2 director.update(game, 0, Vector3.ZERO, FRAME) assert_true(_sounds().has("foghorn"), "the sea starts to rise: %s" % [_sounds()]) var heard: Dictionary = {} while world.tide.stage_at(world.tick) != Tide.SUDDEN_DEATH: world.tick += 20 director.update(game, 0, Vector3.ZERO, FRAME) for sound: String in _sounds(): heard[sound] = int(heard.get(sound, 0)) + 1 assert_eq(int(heard.get("foghorn", 0)), Tide.phase_count() - 1, "a horn for each later rise: %s" % [heard]) assert_eq(int(heard.get("bell", 0)), Tide.phase_count() - 1, "a bell as each new phase holds") assert_eq(int(heard.get("lighthouse", 0)), 1, "the lighthouse for the last land") for cue: Dictionary in director.cues: assert_false(bool(cue["spatial"]), "the tide is everywhere") func test_own_hurt_and_the_feed() -> void: var me: SimEntity = _player(Vector3.ZERO) director.update(game, me.id, Vector3.ZERO, FRAME) me.health = 70 director.update(game, me.id, Vector3.ZERO, FRAME) assert_eq(_sounds(), ["hurt"] as Array[String]) game.feed.append({"victim": 9, "by": 3, "place": 12}) director.update(game, me.id, Vector3.ZERO, FRAME) assert_eq(_sounds(), ["eliminated"] as Array[String]) func test_a_new_world_starts_quietly() -> void: world.structures.add(StructureSet.FLOOR, Materials.STONE, Vector4i(20, 0, 20, StructureSet.SLOT_FLOOR), 0, 0, 0, world.ground) _fx({"kind": NetFx.SHOT, "tick": 1, "player": 1, "item": 0, "position": Vector3.ZERO}) var next: Simulation = Simulation.new(6, SimGround.flat(0.0)) next.structures.add(StructureSet.FLOOR, Materials.STONE, Vector4i(20, 0, 20, StructureSet.SLOT_FLOOR), 0, 0, 0, next.ground) game.world = next director.update(game, 0, Vector3.ZERO, FRAME) assert_eq(_sounds(), [] as Array[String], "what was there before the client looked isn't news") func test_ambience_surf_near_the_water_and_wind_up_high() -> void: director.update(game, 0, Vector3(0.0, world.sea_level + 2.0, 0.0), FRAME) var low_surf: float = director.ambience_level("amb_surf") var low_wind: float = director.ambience_level("amb_wind") director.update(game, 0, Vector3(0.0, world.sea_level + 90.0, 0.0), FRAME) assert_true(low_surf > director.ambience_level("amb_surf") * 2.0, "the surf fades with height") assert_true(director.ambience_level("amb_wind") > low_wind * 2.0, "the wind grows with height") assert_near(director.ambience_level("amb_rain"), 0.0, 0.001, "no rain until weather asks for it")