Jester and coilhead sounds

This commit is contained in:
2026-11-22 07:54:31 -03:00
parent 8e587f6446
commit da44685f54
31 changed files with 249 additions and 62 deletions
+101 -1
View File
@@ -9,8 +9,10 @@ import { ItemStack } from "@/common/inventory.ts";
import { get_state_value, set_state_value } from "@/common/utils.ts";
import { create_entity } from "@/server/game/entity/entity_types.ts";
import type { Mob } from "@/server/game/entity/mob.ts";
import { copy_dir, errors_during, load_as_player, test_game, test_mob } from "./helpers.ts";
import { copy_dir, errors_during, ListOutput, load_as_player, test_game, test_mob, VOLUMES } from "./helpers.ts";
import { animation_for } from "@/client/rendering/entity_animators.ts";
import { sounds } from "@/client/sounds.ts";
import type { EntityData } from "@/common/protocol.ts";
const HALF = "dangerous_business:concrete_wall_slab";
const CONNECTS = "dangerous_business:floor_grate";
@@ -154,6 +156,26 @@ Deno.test("a coilhead can't move while a player looks at it, and comes for them
assertEquals(coilhead.client_data.frozen, undefined);
});
// the sounds conn was sent since last asked, as [sound, where]: the entity it follows, or the point it's at
const sounds_sent = (t: Awaited<ReturnType<typeof setup>>["t"], conn: number): [string, unknown][] =>
t.take(conn).filter((m) => m.type === "sound").map((m) => [m.sound, m.entity ?? m.at]);
Deno.test("a coilhead springs once when a player's look freezes it", async () => {
const { t } = await setup();
// looking south, away from it
t.send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: Math.PI, pitch: 0 });
const coilhead = spawn_coilhead(t);
for (let i = 0; i < 5; i++) t.game.tick();
assertEquals(sounds_sent(t, 1), []);
t.send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: 0, pitch: 0 });
for (let i = 0; i < 40; i++) t.game.tick();
assertEquals(sounds_sent(t, 1), [
["dangerous_business:coilhead/stop", coilhead.id],
["dangerous_business:coilhead/seen", coilhead.id],
]);
});
Deno.test("a player can't stop a coilhead through a wall", async () => {
const { t } = await setup();
for (let x = -3; x <= 3; x++) {
@@ -257,6 +279,24 @@ Deno.test("a coilhead seen while it waits at a closed door stops waiting, and do
assertEquals(coilhead.navigation.blocked_by, undefined);
});
Deno.test("a factory door sounds as it opens and closes, for everyone but the player whose click their client heard", async () => {
const { t, state } = await setup();
const { open } = door_in_wall(t, state);
t.join(2, "bob");
t.send(2, { type: "move", x: 1.5, y: 100, z: 2.5, yaw: 0, pitch: 0 });
t.game.tick();
t.take(1);
t.take(2);
const click = () => t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1] });
click();
assert(open());
assertEquals(sounds_sent(t, 1), []);
assertEquals(sounds_sent(t, 2), [["dangerous_business:door/open", [0.5, 101, 0.5]]]);
click();
assertEquals(sounds_sent(t, 2), [["dangerous_business:door/close", [0.5, 101, 0.5]]]);
});
// a jester 6 blocks north of alice, in a state from its client data like one that was saved
function spawn_jester(t: Awaited<ReturnType<typeof setup>>["t"], state?: number) {
const jester = create_entity(t.game.entities, JESTER)! as Mob;
@@ -302,6 +342,20 @@ Deno.test("a popped jester runs down the nearest player faster and faster, and b
assertEquals(speed(), walking);
});
Deno.test("a jester's crank clicks once a second while it winds, and it pops once", async () => {
const { t } = await setup();
const jester = spawn_jester(t, 2);
for (let i = 0; i < 100; i++) t.game.tick();
const cranking = sounds_sent(t, 1);
assertEquals(cranking.length, 5);
assertEquals(new Set(cranking.map(([sound]) => sound)), new Set(["dangerous_business:jester/turn_crank"]));
for (let i = 0; i < 1000 && jester.client_data.state !== 3; i++) t.game.tick();
assertEquals(sounds_sent(t, 1).filter(([sound]) => sound !== "dangerous_business:jester/turn_crank"), [
["dangerous_business:jester/pop", jester.id],
]);
});
// last, since it loads the mod the way a player gets it, in place of the server's
Deno.test("a coilhead's head springs back and forth when it freezes, and stays still when it stands", async () => {
const dir = Deno.makeTempDirSync({ prefix: "dangerous_business_client_" });
@@ -349,3 +403,49 @@ Deno.test("a jester keeps its tendrils in the box until it pops, and turns its c
assertNotEquals(pose(2, 5).get("crank")!.rotation, pose(2, 15).get("crank")!.rotation);
Deno.removeSync(dir, { recursive: true });
});
Deno.test("a jester's music box plays while it winds, and it screams while it's popped", async () => {
const dir = Deno.makeTempDirSync({ prefix: "dangerous_business_client_" });
copy_dir("mods/dangerous_business", `${dir}/dangerous_business`);
await load_as_player(dir);
const output = new ListOutput();
sounds.set_output(output);
const jester = {
id: "e1",
type: JESTER,
data: { client_data: { state: 2 } } as EntityData,
age: 0,
yaw: 0,
head_yaw: 0,
pitch: 0,
walk_animation: () => ({ position: 0, speed: 0 }),
attack_animation: () => 0,
};
const frame = () =>
sounds.frame(
{ x: 0, y: 0, z: 0, yaw: 0, pitch: 0 },
(id) => id === "e1" ? { x: 0, y: 0, z: 3 } : undefined,
VOLUMES,
);
const set_state = (state: number) => {
const before = jester.data;
jester.data = { client_data: { state } };
animation.data_changed(before);
frame();
};
// a player coming near while it already winds hears it too
const animation = animation_for(jester)!;
frame();
const [theme] = output.playing;
assertEquals([theme.file.id, theme.settings.loop], ["dangerous_business:jester/theme", false]);
set_state(3);
assert(theme.stopped);
const [scream] = output.playing;
assertEquals([scream.file.id, scream.settings.loop], ["dangerous_business:jester/scream", true]);
set_state(1);
assertEquals(output.playing, []);
sounds.stop_all();
Deno.removeSync(dir, { recursive: true });
});
+39
View File
@@ -36,6 +36,8 @@ import { RecipeBook } from "@/common/mod_loader.ts";
import type { EntityJson } from "@/common/mod_data.ts";
import { mod_entity_factory } from "@/server/game/entity/mod_entity.ts";
import type { Mob } from "@/server/game/entity/mob.ts";
import type { SoundFile, SoundOutput, Voice, VoiceSettings, Volumes } from "@/client/sounds.ts";
import type { Position } from "@/common/mod_api/shared.ts";
// the repo's own mods/, read once: reading and checking every mod's files is most of starting a game in a test. only
// mods/ is kept, since tests change the temporary folders they make between games
@@ -482,3 +484,40 @@ export async function errors_during(fn: () => unknown): Promise<string[]> {
}
return errors;
}
// a client's sound output that only keeps what it was told, see client/sounds.ts
export class ListVoice implements Voice {
ended = false;
stopped = false;
constructor(readonly file: SoundFile, public settings: VoiceSettings) {}
move(at: Position) {
this.settings = { ...this.settings, at };
}
set(volume: number, pitch: number, range: number) {
this.settings = { ...this.settings, volume, pitch, range };
}
stop() {
this.stopped = true;
}
}
export class ListOutput implements SoundOutput {
// false like a browser before the player's first click
ready = true;
voices: ListVoice[] = [];
start(file: SoundFile, settings: VoiceSettings) {
if (!this.ready) return undefined;
const voice = new ListVoice(file, settings);
this.voices.push(voice);
return voice;
}
listen() {}
set_volumes() {}
// the ones still going
get playing() {
return this.voices.filter((voice) => !voice.stopped && !voice.ended);
}
}
// every sound category at full volume
export const VOLUMES: Volumes = { master: 1, music: 1, ambient: 1, blocks: 1, entities: 1, players: 1, ui: 1 };
+20 -47
View File
@@ -10,17 +10,8 @@ import { read_mod, write_mod } from "@/common/mod.ts";
import { EverythingRegistry } from "@/common/everything_registry.ts";
import type { SoundJson } from "@/common/mod_data.ts";
import type { Position } from "@/common/mod_api/shared.ts";
import {
type Listener,
type SoundFile,
type SoundOutput,
Sounds,
sounds,
type Voice,
type VoiceSettings,
type Volumes,
} from "@/client/sounds.ts";
import { copy_dir, load_as_player, stone_floor, test_game, test_mob } from "./helpers.ts";
import { type Listener, Sounds, sounds } from "@/client/sounds.ts";
import { copy_dir, ListOutput, load_as_player, stone_floor, test_game, test_mob, VOLUMES } from "./helpers.ts";
// what an ogg file starts with. nothing decodes them outside the browser, so the rest doesn't have to be audio
const OGG = (n: number) => new Uint8Array([0x4f, 0x67, 0x67, 0x53, n]);
@@ -102,42 +93,8 @@ Deno.test("a player's client gets the sounds of the server's mods", async () =>
Deno.removeSync(dir, { recursive: true });
});
// the client, with an output that only keeps what it was told
// the client
class ListVoice implements Voice {
ended = false;
stopped = false;
constructor(readonly file: SoundFile, public settings: VoiceSettings) {}
move(at: Position) {
this.settings = { ...this.settings, at };
}
set(volume: number, pitch: number, range: number) {
this.settings = { ...this.settings, volume, pitch, range };
}
stop() {
this.stopped = true;
}
}
class ListOutput implements SoundOutput {
// false like a browser before the player's first click
ready = true;
voices: ListVoice[] = [];
start(file: SoundFile, settings: VoiceSettings) {
if (!this.ready) return undefined;
const voice = new ListVoice(file, settings);
this.voices.push(voice);
return voice;
}
listen() {}
set_volumes() {}
// the ones still going
get playing() {
return this.voices.filter((voice) => !voice.stopped && !voice.ended);
}
}
const VOLUMES: Volumes = { master: 1, music: 1, ambient: 1, blocks: 1, entities: 1, players: 1, ui: 1 };
const at = (x: number): Listener => ({ x, y: 0, z: 0, yaw: 0, pitch: 0 });
// a client's sounds with these definitions, each with one file of its own name, and a clock tests move
@@ -229,7 +186,6 @@ Deno.test("a sound following an entity moves with it and stops when it goes", ()
const entities = new Map<string, Position>([["jester", { x: 3, y: 0, z: 0 }]]);
const where = (id: string) => entities.get(id);
client.frame(at(0), where, VOLUMES);
assert(!client.play("noisy:theme", { follow: "nobody" }).playing);
const theme = client.play("noisy:theme", { follow: "jester", loop: true });
assertEquals(output.voices[0].settings.at, { x: 3, y: 0, z: 0 });
@@ -243,6 +199,23 @@ Deno.test("a sound following an entity moves with it and stops when it goes", ()
assert(output.voices[0].stopped);
});
Deno.test("a sound following an entity this client doesn't have yet waits a frame for it", () => {
const { client, output } = client_sounds([{ id: "noisy:theme" }]);
const entities = new Map<string, Position>();
const where = (id: string) => entities.get(id);
// like one played as the entity's animator is made, before the entity is in the level
const late = client.play("noisy:theme", { follow: "jester" });
const nobody = client.play("noisy:theme", { follow: "nobody" });
assert(late.playing);
assertEquals(output.voices.length, 0);
entities.set("jester", { x: 3, y: 0, z: 0 });
client.frame(at(0), where, VOLUMES);
assert(late.playing);
assertEquals(output.voices.map((voice) => voice.settings.at), [{ x: 3, y: 0, z: 0 }]);
assert(!nobody.playing);
});
Deno.test("at most 64 sounds play at once, and a loop waits for room", () => {
const { client, output } = client_sounds([{ id: "noisy:thud" }, { id: "noisy:hum" }]);
const thuds = Array.from({ length: 64 }, () => client.play("noisy:thud"));