Sounds on shared and server
This commit is contained in:
@@ -392,6 +392,8 @@ export function client_of(game: GameServer, area: Area) {
|
||||
|
||||
const rules = new BlockRules((_mod, _what, fn) => fn(), (x, y, z) => at(x, y, z)?.[0]);
|
||||
const recipes = new RecipeBook();
|
||||
// the sounds its rules played, as the client would play them
|
||||
const sounds: { sound: string; x: number; y: number; z: number }[] = [];
|
||||
const world: SharedWorld = {
|
||||
get_block: (x, y, z) => at(x, y, z)?.[0],
|
||||
get_state(x, y, z, name) {
|
||||
@@ -426,9 +428,11 @@ export function client_of(game: GameServer, area: Area) {
|
||||
// its recipes aren't looked at here
|
||||
recipes: () => recipes,
|
||||
running: () => true,
|
||||
play_sound: (sound, x, y, z) => sounds.push({ sound, x, y, z }),
|
||||
}));
|
||||
|
||||
return {
|
||||
sounds,
|
||||
// what game_mode does when this client places held against face of x y z, at hit
|
||||
place(id: string, x: number, y: number, z: number, yaw: number, face: Face, hit: [number, number, number]) {
|
||||
const value = rules.placement_value(info(id)!, yaw, 0, { x, y, z, face, hit, sneaking: false, item: id });
|
||||
|
||||
+121
-1
@@ -20,7 +20,7 @@ import {
|
||||
type VoiceSettings,
|
||||
type Volumes,
|
||||
} from "@/client/sounds.ts";
|
||||
import { copy_dir, load_as_player } from "./helpers.ts";
|
||||
import { copy_dir, load_as_player, stone_floor, test_game, test_mob } 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]);
|
||||
@@ -256,3 +256,123 @@ Deno.test("at most 64 sounds play at once, and a loop waits for room", () => {
|
||||
assertEquals(output.voices.at(-1)!.file.id, "noisy:hum");
|
||||
assertEquals(client.voices, 64);
|
||||
});
|
||||
|
||||
// the server: who it sends sounds to
|
||||
|
||||
// what the noisy mod's script does: its bell's shared rule rings when clicked, and /ring x y z [except] [volume] plays
|
||||
// the ring at x y z with ctx.sounds.play
|
||||
const NOISY_SCRIPT = `
|
||||
import type { ServerContext } from "cubi/server";
|
||||
import type { SharedContext } from "cubi/shared";
|
||||
|
||||
export function shared(ctx: SharedContext) {
|
||||
ctx.components.register_block("noisy:bell", {
|
||||
on_interact(block) {
|
||||
ctx.sounds.play("noisy:ring", block.x + 0.5, block.y + 0.5, block.z + 0.5);
|
||||
return true;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function server(ctx: ServerContext) {
|
||||
ctx.commands.register("ring", {
|
||||
description: "",
|
||||
usage: "/ring x y z [except] [volume]",
|
||||
run([x, y, z, except, volume], player) {
|
||||
ctx.sounds.play("noisy:ring", Number(x), Number(y), Number(z), {
|
||||
except: except === "except" ? player : undefined,
|
||||
volume: volume === undefined ? undefined : Number(volume),
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
`;
|
||||
|
||||
// a bell block next to where players stand
|
||||
const BELL = [2, 100, 0] as const;
|
||||
|
||||
// a game with the noisy mod, its bell, alice at 0 100 0 and bob 12 blocks along x. the ring is heard 10 blocks away
|
||||
async function noisy_game() {
|
||||
const dir = sound_mod({ "ring.ogg": OGG(1) }, { ring: { files: ["ring"], range: 10 } });
|
||||
Deno.writeTextFileSync(`${dir}/noisy/scripts/main.ts`, NOISY_SCRIPT);
|
||||
Deno.removeSync(`${dir}/noisy/recipes`, { recursive: true });
|
||||
Deno.writeTextFileSync(
|
||||
`${dir}/noisy/blocks/example_block.json`,
|
||||
JSON.stringify({
|
||||
format_version: 1,
|
||||
block: {
|
||||
id: "noisy:bell",
|
||||
textures: "noisy:example_block",
|
||||
interactive: true,
|
||||
components: { "noisy:bell": {} },
|
||||
},
|
||||
}),
|
||||
);
|
||||
const t = await test_game(dir);
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
stone_floor(t.game, 16);
|
||||
t.game.set_block(...BELL, "noisy:bell");
|
||||
t.join(1, "alice");
|
||||
t.join(2, "bob");
|
||||
const move = (conn: number, x: number) => t.send(conn, { type: "move", x, y: 100, z: 0.5, yaw: 0, pitch: 0 });
|
||||
move(1, 0.5);
|
||||
move(2, 12.5);
|
||||
for (let i = 0; i < 5; i++) t.game.tick();
|
||||
t.take(1);
|
||||
t.take(2);
|
||||
// the sounds conn was sent since last asked
|
||||
const heard = (conn: number) => t.take(conn).filter((m) => m.type === "sound");
|
||||
return { ...t, move, heard };
|
||||
}
|
||||
|
||||
Deno.test("ctx.sounds.play reaches the players in range, further for a louder sound, and never except", async () => {
|
||||
const { send, heard } = await noisy_game();
|
||||
send(1, { type: "chat", text: "/ring 0 101 0" });
|
||||
assertEquals(heard(1), [{ type: "sound", sound: "noisy:ring", at: [0, 101, 0] }]);
|
||||
assertEquals(heard(2), []);
|
||||
|
||||
send(1, { type: "chat", text: "/ring 0 101 0 nobody 2" });
|
||||
assertEquals(heard(1).length, 1);
|
||||
assertEquals(heard(2), [{ type: "sound", sound: "noisy:ring", at: [0, 101, 0], volume: 2 }]);
|
||||
|
||||
send(1, { type: "chat", text: "/ring 12 101 0 except" });
|
||||
assertEquals(heard(1), []);
|
||||
assertEquals(heard(2).length, 1);
|
||||
});
|
||||
|
||||
Deno.test("a player's sound is only theirs, and a block's comes from its middle", async () => {
|
||||
const { game, heard } = await noisy_game();
|
||||
game.mods.player(game.players()[1]).play_sound("noisy:ring", { pitch: 2 });
|
||||
assertEquals(heard(1), []);
|
||||
assertEquals(heard(2), [{ type: "sound", sound: "noisy:ring", pitch: 2 }]);
|
||||
|
||||
game.mods.block_ref(...BELL, "noisy:bell").play_sound("noisy:ring");
|
||||
assertEquals(heard(1), [{ type: "sound", sound: "noisy:ring", at: [2.5, 100.5, 0.5] }]);
|
||||
assertThrows(() => game.mods.player(game.players()[0]).play_sound("noisy:nothing"), Error, "unknown sound");
|
||||
assertThrows(() => game.mods.player(game.players()[0]).play_sound("noisy:ring", { volume: -1 }), Error, "volume");
|
||||
});
|
||||
|
||||
Deno.test("a mob's sound follows it, for the players who see it within range", async () => {
|
||||
const { game, heard } = await noisy_game();
|
||||
const mob = test_mob(game, {}, 4.5, 100, 0.5);
|
||||
game.tick();
|
||||
heard(1);
|
||||
heard(2);
|
||||
game.mods.mob(mob).play_sound("noisy:ring");
|
||||
assertEquals(heard(1), [{ type: "sound", sound: "noisy:ring", entity: mob.id }]);
|
||||
assertEquals(heard(2), [{ type: "sound", sound: "noisy:ring", entity: mob.id }]);
|
||||
|
||||
mob.set_position(-6.5, 100, 0.5);
|
||||
game.tick();
|
||||
game.mods.mob(mob).play_sound("noisy:ring");
|
||||
assertEquals(heard(1).length, 1);
|
||||
assertEquals(heard(2), []);
|
||||
});
|
||||
|
||||
Deno.test("a shared rule's sound isn't sent to the player whose click ran it, whose client played it", async () => {
|
||||
const { send, heard, move } = await noisy_game();
|
||||
move(2, 6.5);
|
||||
send(1, { type: "use_block", x: BELL[0], y: BELL[1], z: BELL[2], face: "west", hit: [0, 0.5, 0.5], seq: 1 });
|
||||
assertEquals(heard(1), []);
|
||||
assertEquals(heard(2), [{ type: "sound", sound: "noisy:ring", at: [2.5, 100.5, 0.5] }]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user