Entity and block sounds !
This commit is contained in:
@@ -6,6 +6,7 @@ import { LocalPlayer } from "@/client/entity/local_player.ts";
|
||||
import { InputManager } from "@/client/input_manager.ts";
|
||||
import { Options } from "@/client/options.ts";
|
||||
import type { Client } from "@/client/client.ts";
|
||||
import { AIR } from "@/common/constants.ts";
|
||||
|
||||
type Blocks = (x: number, y: number, z: number) => boolean;
|
||||
|
||||
@@ -20,6 +21,8 @@ function setup(ceiling = Infinity, world?: { solid: Blocks; water: Blocks }) {
|
||||
// its water is all still sources
|
||||
fluid_at: (x: number, y: number, z: number) =>
|
||||
world?.water(x, y, z) ? { id: "a:water", height: 1 } : undefined,
|
||||
// blocks without sounds to step on
|
||||
get_block: () => AIR,
|
||||
},
|
||||
screen: undefined,
|
||||
options: new Options(),
|
||||
|
||||
+163
-15
@@ -12,6 +12,12 @@ import type { SoundJson } from "@/common/mod_data.ts";
|
||||
import type { Position } from "@/common/mod_api/shared.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";
|
||||
import { client_level } from "./client_level.ts";
|
||||
import { RemoteEntity } from "@/client/entity/remote_entity.ts";
|
||||
import { DEFAULT_SHAPE } from "@/common/world_shape.ts";
|
||||
import { ChunkColumn } from "@/common/section.ts";
|
||||
import { CHUNK_AREA } from "@/common/constants.ts";
|
||||
import type { EntityInfo } from "@/common/protocol.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]);
|
||||
@@ -264,23 +270,49 @@ export function server(ctx: ServerContext) {
|
||||
// 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 } });
|
||||
// the noisy mod's folder: its bell, a rock whose sounds are "noisy:rock", a walker whose sounds are "noisy:walker"
|
||||
// and a quiet entity without any
|
||||
function noisy_dir() {
|
||||
const dir = sound_mod(
|
||||
Object.fromEntries(["ring", "rock_break", "rock_place", "rock_step", "walker_step", "walker_hurt"].map(
|
||||
(name, i) => [`${name}.ogg`, OGG(i)],
|
||||
)),
|
||||
{
|
||||
ring: { files: ["ring"], range: 10 },
|
||||
"rock/break": { files: ["rock_break"] },
|
||||
"rock/place": { files: ["rock_place"] },
|
||||
"rock/step": { files: ["rock_step"] },
|
||||
"walker/step": { files: ["walker_step"] },
|
||||
"walker/hurt": { files: ["walker_hurt"] },
|
||||
},
|
||||
);
|
||||
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 data = (path: string, key: string, json: Record<string, unknown>) =>
|
||||
Deno.writeTextFileSync(`${dir}/noisy/${path}`, JSON.stringify({ format_version: 1, [key]: json }));
|
||||
data("blocks/example_block.json", "block", {
|
||||
id: "noisy:bell",
|
||||
textures: "noisy:example_block",
|
||||
interactive: true,
|
||||
components: { "noisy:bell": {} },
|
||||
});
|
||||
data("blocks/rock.json", "block", {
|
||||
id: "noisy:rock",
|
||||
textures: "noisy:example_block",
|
||||
mining: { toughness: 1 },
|
||||
sounds: "noisy:rock",
|
||||
});
|
||||
Deno.mkdirSync(`${dir}/noisy/entities`);
|
||||
const entity = { texture: "noisy:example_block", behavior: "scripted", width: 0.6, height: 1.8 };
|
||||
data("entities/walker.json", "entity", { id: "noisy:walker", ...entity, sounds: "noisy:walker" });
|
||||
data("entities/quiet.json", "entity", { id: "noisy:quiet", ...entity });
|
||||
return dir;
|
||||
}
|
||||
|
||||
// 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,
|
||||
// the rock 16
|
||||
async function noisy_game() {
|
||||
const dir = noisy_dir();
|
||||
const t = await test_game(dir);
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
stone_floor(t.game, 16);
|
||||
@@ -349,3 +381,119 @@ Deno.test("a shared rule's sound isn't sent to the player whose click ran it, wh
|
||||
assertEquals(heard(1), []);
|
||||
assertEquals(heard(2), [{ type: "sound", sound: "noisy:ring", at: [2.5, 100.5, 0.5] }]);
|
||||
});
|
||||
|
||||
// blocks' and entities' "sounds"
|
||||
|
||||
Deno.test("check-mods reports a block's or entity's sounds that have none of what they can have", async () => {
|
||||
const dir = noisy_dir();
|
||||
const rock = JSON.parse(Deno.readTextFileSync(`${dir}/noisy/blocks/rock.json`));
|
||||
rock.block.sounds = "noisy:rok";
|
||||
Deno.writeTextFileSync(`${dir}/noisy/blocks/rock.json`, JSON.stringify(rock));
|
||||
const walker = JSON.parse(Deno.readTextFileSync(`${dir}/noisy/entities/walker.json`));
|
||||
walker.entity.sounds = "noisy:walkr";
|
||||
Deno.writeTextFileSync(`${dir}/noisy/entities/walker.json`, JSON.stringify(walker));
|
||||
|
||||
const errors = (await checked(dir)).report.errors;
|
||||
assertEquals(errors, [
|
||||
"blocks/rock.json: sounds noisy:rok has none of noisy:rok/break, noisy:rok/place, noisy:rok/step, noisy:rok/hit",
|
||||
"entities/walker.json: sounds noisy:walkr has none of noisy:walkr/hurt, noisy:walkr/death, noisy:walkr/step, " +
|
||||
"noisy:walkr/ambient",
|
||||
]);
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
});
|
||||
|
||||
Deno.test("a player breaking or placing a block is heard by everyone near but them, whose client played it", async () => {
|
||||
const { game, send, heard } = await noisy_game();
|
||||
const rock_at = [-2, 100, 0] as const;
|
||||
game.set_block(...rock_at, "noisy:rock");
|
||||
send(1, { type: "break_block", x: rock_at[0], y: rock_at[1], z: rock_at[2], seq: 1 });
|
||||
assertEquals(game.world.get_block_id(...rock_at), "cubi:air");
|
||||
assertEquals(heard(1), []);
|
||||
assertEquals(heard(2), [{ type: "sound", sound: "noisy:rock/break", at: [-1.5, 100.5, 0.5] }]);
|
||||
|
||||
send(1, { type: "chat", text: "/give noisy:rock" });
|
||||
send(1, { type: "use_block", x: rock_at[0], y: 99, z: rock_at[2], face: "top", hit: [0.5, 1, 0.5], seq: 2 });
|
||||
assertEquals(game.world.get_block_id(...rock_at), "noisy:rock");
|
||||
assertEquals(heard(1), []);
|
||||
assertEquals(heard(2), [{ type: "sound", sound: "noisy:rock/place", at: [-1.5, 100.5, 0.5] }]);
|
||||
|
||||
// broken by something that isn't a player, like a script, everyone hears it from the server
|
||||
game.destroy_block(...rock_at);
|
||||
assertEquals(heard(1).length, 1);
|
||||
assertEquals(heard(2).length, 1);
|
||||
});
|
||||
|
||||
// a client with the noisy mod, a floor of its rock under y 100 and the listener just above it, its sounds going to
|
||||
// output
|
||||
async function noisy_client() {
|
||||
const dir = noisy_dir();
|
||||
await load_as_player(dir);
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
const { level } = client_level(DEFAULT_SHAPE);
|
||||
const rock = EverythingRegistry.get_id("blocks", "noisy:rock")!;
|
||||
const flat = new Uint32Array(DEFAULT_SHAPE.volume).fill(rock, 99 * CHUNK_AREA, 100 * CHUNK_AREA);
|
||||
level.set_chunk(ChunkColumn.from_flat(0, 0, DEFAULT_SHAPE, flat, new Array(CHUNK_AREA).fill("bworld:plains")));
|
||||
const output = new ListOutput();
|
||||
sounds.stop_all();
|
||||
sounds.set_output(output);
|
||||
sounds.frame({ x: 8, y: 101, z: 8, yaw: 0, pitch: 0 }, () => undefined, VOLUMES);
|
||||
const entity = (type: string) =>
|
||||
new RemoteEntity(level, {
|
||||
kind: "entity",
|
||||
id: type,
|
||||
type,
|
||||
x: 8.5,
|
||||
y: 100,
|
||||
z: 8.5,
|
||||
yaw: 0,
|
||||
pitch: 0,
|
||||
data: {},
|
||||
} as Extract<EntityInfo, { kind: "entity" }>);
|
||||
// walks entity along x, distance blocks a tick for ticks
|
||||
const walk = (entity: RemoteEntity, distance: number, ticks: number) => {
|
||||
for (let i = 0; i < ticks; i++) {
|
||||
entity.save_previous_position();
|
||||
entity.x += distance;
|
||||
entity.tick();
|
||||
}
|
||||
};
|
||||
// what's played since last asked, as [file, volume]
|
||||
const played = () => {
|
||||
const voices = output.voices.map((voice) => [voice.file.id, voice.settings.volume]);
|
||||
output.voices.length = 0;
|
||||
return voices;
|
||||
};
|
||||
return { entity, walk, played };
|
||||
}
|
||||
|
||||
Deno.test("an entity steps every 1.6 blocks it walks on a block: its own step, or the block's quieter one", async () => {
|
||||
const { entity, walk, played } = await noisy_client();
|
||||
const walker = entity("noisy:walker");
|
||||
walk(walker, 0.4, 8);
|
||||
assertEquals(played(), [["noisy:walker_step", 1], ["noisy:walker_step", 1]]);
|
||||
|
||||
const quiet = entity("noisy:quiet");
|
||||
walk(quiet, 0.4, 5);
|
||||
assertEquals(played(), [["noisy:rock_step", 0.15]]);
|
||||
|
||||
// in the air there's nothing to step on
|
||||
quiet.set_position(8.5, 104, 8.5);
|
||||
walk(quiet, 0.4, 8);
|
||||
assertEquals(played(), []);
|
||||
sounds.stop_all();
|
||||
});
|
||||
|
||||
Deno.test("an entity sounds hurt when it's hurt, and only what it has", async () => {
|
||||
const { entity, played } = await noisy_client();
|
||||
entity("noisy:walker").on_event("hurt");
|
||||
assertEquals(played(), []);
|
||||
// heard following it, once the frame finds where it is
|
||||
sounds.frame({ x: 8, y: 101, z: 8, yaw: 0, pitch: 0 }, () => ({ x: 8.5, y: 100, z: 8.5 }), VOLUMES);
|
||||
assertEquals(played(), [["noisy:walker_hurt", 1]]);
|
||||
// it has no death sound, and the quiet one has none at all
|
||||
entity("noisy:walker").on_event("death");
|
||||
entity("noisy:quiet").on_event("hurt");
|
||||
sounds.frame({ x: 8, y: 101, z: 8, yaw: 0, pitch: 0 }, () => ({ x: 8.5, y: 100, z: 8.5 }), VOLUMES);
|
||||
assertEquals(played(), []);
|
||||
sounds.stop_all();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user