Sounds on the client
This commit is contained in:
+31
-1
@@ -13,10 +13,13 @@ import {
|
||||
type EntityJson,
|
||||
type EntityModelJson,
|
||||
type FluidJson,
|
||||
is_ogg,
|
||||
type ItemJson,
|
||||
type ManifestJson,
|
||||
type OreJson,
|
||||
type RecipeJson,
|
||||
SOUND_PATH_PATTERN,
|
||||
type SoundJson,
|
||||
type SpawnJson,
|
||||
type TagJson,
|
||||
validate_block,
|
||||
@@ -30,6 +33,7 @@ import {
|
||||
validate_model,
|
||||
validate_ore,
|
||||
validate_recipe,
|
||||
validate_sound,
|
||||
validate_spawn,
|
||||
validate_tag,
|
||||
} from "./mod_data.ts";
|
||||
@@ -71,6 +75,8 @@ export interface Mod {
|
||||
textures: Map<string, Uint8Array>;
|
||||
// the .json of the textures that have one, like animated ones, by texture id
|
||||
texture_json?: Map<string, TextureJson>;
|
||||
// ogg bytes by path in its sounds/ without .ogg, like jester/stomp_1, what its sounds.json's files name
|
||||
sounds?: Map<string, Uint8Array>;
|
||||
credits?: string;
|
||||
}
|
||||
|
||||
@@ -88,6 +94,7 @@ export class ModError extends Error {
|
||||
// script.js
|
||||
// textures/<name>.png the texture <mod id>:<name>
|
||||
// textures/<name>.json its animation, if it has one
|
||||
// sounds/<path>.ogg a sound file, see SoundJson
|
||||
// credits.md
|
||||
export function write_mod(mod: Mod): Uint8Array<ArrayBuffer> {
|
||||
const text = (value: string) => new TextEncoder().encode(value);
|
||||
@@ -110,6 +117,9 @@ export function write_mod(mod: Mod): Uint8Array<ArrayBuffer> {
|
||||
for (const [id, json] of mod.texture_json ?? []) {
|
||||
files[`textures/${id.split(":")[1]}.json`] = text(JSON.stringify(json));
|
||||
}
|
||||
for (const [path, ogg] of mod.sounds ?? []) {
|
||||
files[`sounds/${path}.ogg`] = ogg;
|
||||
}
|
||||
files["manifest.json"] = text(JSON.stringify(manifest, null, "\t"));
|
||||
files["data.json"] = text(JSON.stringify(mod.data));
|
||||
|
||||
@@ -192,6 +202,7 @@ export function read_mod(bytes: Uint8Array, file: string): Mod {
|
||||
problems,
|
||||
) as EntityModelJson[],
|
||||
animations: checked(raw_data.animations, "animations", validate_clip, problems) as ClipJson[],
|
||||
sounds: checked(raw_data.sounds, "sounds", validate_sound, problems) as SoundJson[],
|
||||
effects: checked(raw_data.effects, "effects", validate_effect, problems) as EffectJson[],
|
||||
fluids: checked(raw_data.fluids, "fluids", validate_fluid, problems) as FluidJson[],
|
||||
tags: checked(raw_data.tags, "tags", validate_tag, problems) as TagJson[],
|
||||
@@ -232,6 +243,7 @@ export function read_mod(bytes: Uint8Array, file: string): Mod {
|
||||
...data.entities,
|
||||
...data.entity_models,
|
||||
...data.animations!,
|
||||
...data.sounds!,
|
||||
...data.effects,
|
||||
...data.fluids,
|
||||
];
|
||||
@@ -302,8 +314,26 @@ export function read_mod(bytes: Uint8Array, file: string): Mod {
|
||||
}
|
||||
}
|
||||
|
||||
const sounds = new Map<string, Uint8Array>();
|
||||
for (const [name, content] of Object.entries(files)) {
|
||||
const match = name.match(/^sounds\/(.+)\.ogg$/);
|
||||
if (!match) continue;
|
||||
if (!SOUND_PATH_PATTERN.test(match[1])) {
|
||||
problems.push(`${name}: sound paths must be a-z, 0-9 and _ in folders`);
|
||||
} else if (!is_ogg(content)) {
|
||||
problems.push(`${name}: isn't an ogg file`);
|
||||
} else {
|
||||
sounds.set(match[1], content);
|
||||
}
|
||||
}
|
||||
for (const sound of data.sounds!) {
|
||||
for (const path of Array.isArray(sound.files) ? sound.files : []) {
|
||||
if (!sounds.has(path)) problems.push(`sound ${sound.id}: there's no sounds/${path}.ogg`);
|
||||
}
|
||||
}
|
||||
|
||||
if (problems.length > 0) throw new ModError(file, problems);
|
||||
return { manifest, data, script, textures, texture_json, credits };
|
||||
return { manifest, data, script, textures, texture_json, sounds, credits };
|
||||
}
|
||||
|
||||
// the same mod without the terrain's data, its schematics and its config, what players get. the server sends them the world. its
|
||||
|
||||
@@ -9,6 +9,7 @@ import type {
|
||||
ItemStack,
|
||||
KeyCode,
|
||||
ModInfo,
|
||||
PlayOptions,
|
||||
Position,
|
||||
RecipeLookup,
|
||||
Season,
|
||||
@@ -24,6 +25,7 @@ export type {
|
||||
ItemStack,
|
||||
KeyCode,
|
||||
ModInfo,
|
||||
PlayOptions,
|
||||
Position,
|
||||
Season,
|
||||
TagApi,
|
||||
@@ -124,9 +126,39 @@ export interface ClientContext {
|
||||
tags: TagApi;
|
||||
// blocks drawn with code, like a shaft that turns, see docs/modding/blocks/visuals.md
|
||||
render: ClientRender;
|
||||
// sounds from the mods' sounds.json, played on this client only, see docs/modding/resources/sounds.md
|
||||
sounds: ClientSounds;
|
||||
log(...args: unknown[]): void;
|
||||
}
|
||||
|
||||
export interface ClientSounds {
|
||||
// plays a sound on this client. without at or follow it's heard as it is wherever the player is, like a click in a
|
||||
// screen. throws for a sound no mod defines
|
||||
play(sound: Id, options?: ClientPlayOptions): PlayingSound;
|
||||
exists(sound: Id): boolean;
|
||||
}
|
||||
|
||||
export interface ClientPlayOptions extends PlayOptions {
|
||||
// where it comes from, fading out with distance until its range
|
||||
at?: Position;
|
||||
// an entity's id: it moves with the entity, and stops when the entity goes
|
||||
follow?: string;
|
||||
// plays again from the start until it's stopped. one out of the player's range waits silently, and comes back in
|
||||
// as far into the file as it would have got
|
||||
loop?: boolean;
|
||||
}
|
||||
|
||||
// a sound ctx.sounds.play started
|
||||
export interface PlayingSound {
|
||||
// false once it finished, was stopped, or what it followed went. a sound that couldn't play at all, like one
|
||||
// played out of range or before the player first clicked, is never playing, unless it loops
|
||||
readonly playing: boolean;
|
||||
stop(): void;
|
||||
// what play was given, changed while it plays, like a machine speeding up
|
||||
volume: number;
|
||||
pitch: number;
|
||||
}
|
||||
|
||||
export interface ClientRender {
|
||||
// every block of this id gets a visual from create while its chunk is shown, like a block entity renderer or
|
||||
// flywheel's visuals. only during setup, and one per block. replaces_model leaves the block's model out of the
|
||||
|
||||
@@ -116,6 +116,14 @@ export interface Position {
|
||||
z: number;
|
||||
}
|
||||
|
||||
// how a sound plays, on top of what its definition in sounds.json says. see docs/modding/resources/sounds.md
|
||||
export interface PlayOptions {
|
||||
// multiplies the sound's own. over 1 it's heard further away, not louder
|
||||
volume?: number;
|
||||
// multiplies the sound's own: 2 is an octave up and twice as fast
|
||||
pitch?: number;
|
||||
}
|
||||
|
||||
export type Face = "west" | "east" | "bottom" | "top" | "north" | "south";
|
||||
|
||||
// KeyboardEvent.code values, like "KeyE" or "Digit1"
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
type OreJson,
|
||||
type RecipeJson,
|
||||
SHARED_TAG_NAMESPACE,
|
||||
type SoundJson,
|
||||
type SpawnJson,
|
||||
type TagJson,
|
||||
type TagRegistry,
|
||||
@@ -67,6 +68,8 @@ export interface LoadedMod {
|
||||
entity_models: { file: string; json: EntityModelJson }[];
|
||||
// clips, see ANIMATION.md
|
||||
animations: { file: string; json: ClipJson }[];
|
||||
// its sounds.json, see docs/modding/resources/sounds.md
|
||||
sounds: { file: string; json: SoundJson }[];
|
||||
effects: { file: string; json: EffectJson }[];
|
||||
fluids: { file: string; json: FluidJson }[];
|
||||
tags: { file: string; json: TagJson }[];
|
||||
@@ -88,6 +91,8 @@ export interface LoadedMod {
|
||||
texture_files: Map<string, string>;
|
||||
// textures/<name>.json by texture id, for the textures that have one (see texture_animation.ts)
|
||||
texture_json: Map<string, TextureJson>;
|
||||
// absolute paths of its sounds/ files by path in sounds/ without .ogg, like jester/stomp_1
|
||||
sound_files: Map<string, string>;
|
||||
}
|
||||
|
||||
// what a mod's data files hold, as packed into its .cubi
|
||||
@@ -102,6 +107,7 @@ export function mod_data(mod: LoadedMod): ModData {
|
||||
entities: mod.entities.map((e) => e.json),
|
||||
entity_models: mod.entity_models.map((m) => m.json),
|
||||
animations: mod.animations.map((a) => a.json),
|
||||
sounds: mod.sounds.map((s) => s.json),
|
||||
effects: mod.effects.map((e) => e.json),
|
||||
fluids: mod.fluids.map((f) => f.json),
|
||||
tags: mod.tags.map((t) => t.json),
|
||||
|
||||
@@ -605,6 +605,43 @@ const clip_schema = z.object({
|
||||
|
||||
export type ClipJson = z.infer<typeof clip_schema>;
|
||||
|
||||
// which volume slider a sound follows, each under the player's master volume. see docs/modding/resources/sounds.md
|
||||
export const SOUND_CATEGORIES = ["music", "ambient", "blocks", "entities", "players", "ui"] as const;
|
||||
export type SoundCategory = typeof SOUND_CATEGORIES[number];
|
||||
|
||||
// a path in a mod's sounds/ without .ogg, like jester/stomp_1, and a sound's name in sounds.json
|
||||
export const SOUND_PATH_PATTERN = /^[a-z0-9_]+(\/[a-z0-9_]+)*$/;
|
||||
|
||||
// one of the sounds in a mod's sounds.json, with its id: the key "jester/step" in dangerous_business's is
|
||||
// dangerous_business:jester/step
|
||||
const sound_schema = z.object({
|
||||
id: z.string('must be a sound id like "my_mod:thing/step"').regex(TAG_ID_PATTERN),
|
||||
// paths in the mod's own sounds/, one picked at random each time it plays
|
||||
files: z.array(
|
||||
z.string('must be a path in sounds/ without .ogg, like "thing/step_1"').regex(SOUND_PATH_PATTERN),
|
||||
).min(1),
|
||||
// at most 1, plays multiply it. 1 when not given
|
||||
volume: z.number().min(0).max(1).optional(),
|
||||
// 1 when not given
|
||||
pitch: z.number().gt(0).optional(),
|
||||
// how far away it's heard at volume 1, in blocks. 16 when not given
|
||||
range: z.number().gt(0).optional(),
|
||||
// only the master volume when not given
|
||||
category: z.enum(SOUND_CATEGORIES).optional(),
|
||||
});
|
||||
|
||||
export type SoundJson = z.infer<typeof sound_schema>;
|
||||
|
||||
export function validate_sound(json: unknown): Problems {
|
||||
return find_problems(sound_schema, 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 {
|
||||
return bytes.length >= 4 && bytes[0] === 0x4f && bytes[1] === 0x67 && bytes[2] === 0x67 && bytes[3] === 0x53;
|
||||
}
|
||||
|
||||
// something in a tag file's values or remove: an id, a "#tag", or either with whether it must exist. a plain one must
|
||||
const tag_entry = z.union(
|
||||
[id_or_tag, z.object({ id: id_or_tag, required: z.boolean().optional() })],
|
||||
|
||||
@@ -24,6 +24,7 @@ import {
|
||||
ModRecipeJson,
|
||||
OreJson,
|
||||
RecipeJson,
|
||||
SoundJson,
|
||||
SpawnJson,
|
||||
stack_problems,
|
||||
TagJson,
|
||||
@@ -62,6 +63,8 @@ export interface ModData {
|
||||
entity_models: EntityModelJson[];
|
||||
// clips, see ANIMATION.md. missing from mods packed before there were any
|
||||
animations?: ClipJson[];
|
||||
// its sounds.json, see docs/modding/resources/sounds.md. the files are in the .cubi's sounds/
|
||||
sounds?: SoundJson[];
|
||||
// status effects, see EffectJson
|
||||
effects: EffectJson[];
|
||||
// kinds of fluid, see FluidJson
|
||||
@@ -230,6 +233,9 @@ export function register_mod_data(mods: { id: string; data: ModData }[]): Recipe
|
||||
for (const clip of data.animations ?? []) {
|
||||
EverythingRegistry.register<ClipJson>("animations", clip.id, clip);
|
||||
}
|
||||
for (const json of data.sounds ?? []) {
|
||||
EverythingRegistry.register<SoundJson>("sounds", json.id, json);
|
||||
}
|
||||
for (const json of data.entities) {
|
||||
EverythingRegistry.register<EntityJson>("entities", json.id, json);
|
||||
}
|
||||
|
||||
@@ -99,6 +99,7 @@ export async function load_bundle(bundle: WorldgenBundle): Promise<LoadedBundle>
|
||||
entities: [],
|
||||
entity_models: [],
|
||||
animations: [],
|
||||
sounds: [],
|
||||
effects: [],
|
||||
fluids: [],
|
||||
loot_tables: [],
|
||||
|
||||
Reference in New Issue
Block a user