Sounds on shared and server

This commit is contained in:
2026-11-22 07:44:13 -03:00
parent 47934b272c
commit 8e587f6446
16 changed files with 386 additions and 30 deletions
+24
View File
@@ -8,6 +8,7 @@ import { is_tint_color, type TintColor } from "./worldgen/biome_colors.ts";
import {
type BlockRegistry,
type BlockStateDefinition,
EverythingRegistry,
FLUID_DEFAULTS,
FLUID_LEVEL,
type FluidContainerProperties,
@@ -25,6 +26,7 @@ import {
import type { BlockPart, BlockVariant, ModelFileJson } from "./block_models.ts";
import { expand_items, list_matches } from "./tags.ts";
import { GAME_VERSION } from "./constants.ts";
import type { PlayOptions } from "./mod_api/shared.ts";
import {
by_kind,
color,
@@ -636,6 +638,28 @@ export function validate_sound(json: unknown): Problems {
return find_problems(sound_schema, json);
}
// how far away a sound is heard at volume 1 when its definition doesn't say
const DEFAULT_SOUND_RANGE = 16;
// how far a sound played at volume is heard: its range, further for a louder one, like twice as far at volume 2
export function sound_range(json: SoundJson, volume = 1) {
return (json.range ?? DEFAULT_SOUND_RANGE) * Math.max(1, (json.volume ?? 1) * volume);
}
// a mod playing sound with options: its definition, or an error saying what's wrong with them
export function sound_to_play(sound: unknown, options: PlayOptions = {}): SoundJson {
const json = typeof sound === "string" ? EverythingRegistry.get<SoundJson>("sounds", sound) : undefined;
if (!json) throw new Error(`unknown sound ${sound}`);
const { volume = 1, pitch = 1 } = options;
if (typeof volume !== "number" || !(volume >= 0) || !Number.isFinite(volume)) {
throw new Error(`a sound's volume must be 0 or more`);
}
if (typeof pitch !== "number" || !(pitch > 0) || !Number.isFinite(pitch)) {
throw new Error(`a sound's pitch must be more than 0`);
}
return json;
}
// whether a file starts the way every ogg does, so a wav or mp3 renamed to .ogg is caught when the mod is checked
// instead of failing to play
export function is_ogg(bytes: Uint8Array): boolean {