Sounds on shared and server
This commit is contained in:
@@ -15,6 +15,7 @@ import type { Season } from "@/common/worldgen/biomes.ts";
|
||||
import { type Collisions, type FluidInCell, physics } from "@/common/physics.ts";
|
||||
import type { EntityData, EntityEvent, EntityInfo } from "@/common/protocol.ts";
|
||||
import type { ServerPlayer } from "../player.ts";
|
||||
import type { PlayOptions } from "@/common/mod_api/shared.ts";
|
||||
import type { LivingEntity } from "./living_entity.ts";
|
||||
|
||||
// this far below the bottom of the world, entities have fallen out of it
|
||||
@@ -102,6 +103,8 @@ export interface EntityLevel {
|
||||
take_item(item: Entity, by: string): void;
|
||||
// sent to clients tracking the entity, for the renderer: "hurt" flashes it red, "death" starts falling over
|
||||
entity_event(entity: Entity, event: EntityEvent): void;
|
||||
// a sound following it, for the players tracking it within its range, see EntityRef.play_sound
|
||||
play_sound(entity: Entity, sound: string, options: PlayOptions): void;
|
||||
// it died, after dropping its things
|
||||
on_death(entity: Entity, source: DamageSource): void;
|
||||
// what it drops when it dies, from its type's loot table
|
||||
|
||||
@@ -11,6 +11,8 @@ import { biome_season } from "@/common/worldgen/biomes.ts";
|
||||
import { Tags } from "@/common/tags.ts";
|
||||
import type { ItemStack } from "@/common/inventory.ts";
|
||||
import type { EntityEvent, EntityInfo, ServerMessage } from "@/common/protocol.ts";
|
||||
import type { PlayOptions } from "@/common/mod_api/shared.ts";
|
||||
import { sound_range, sound_to_play } from "@/common/mod_data.ts";
|
||||
import type { ServerPlayer } from "../player.ts";
|
||||
import type { ServerWorld } from "../world.ts";
|
||||
import type { DamageSource, Entity, EntityLevel, SavedEntity } from "./entity.ts";
|
||||
@@ -36,6 +38,21 @@ export interface EntityHost {
|
||||
inside_blocks?(entity: Entity): void;
|
||||
}
|
||||
|
||||
// a sound clients play, with only what's set. where is where it comes from, see the message in common/protocol.ts
|
||||
export function sound_message(
|
||||
sound: string,
|
||||
{ volume, pitch }: PlayOptions,
|
||||
where: { at?: [number, number, number]; entity?: string } = {},
|
||||
): ServerMessage {
|
||||
return {
|
||||
type: "sound",
|
||||
sound,
|
||||
...where,
|
||||
...(volume !== undefined && volume !== 1 && { volume }),
|
||||
...(pitch !== undefined && pitch !== 1 && { pitch }),
|
||||
};
|
||||
}
|
||||
|
||||
// what a client was last told about an entity, so only changes get sent
|
||||
interface Sent {
|
||||
x: number;
|
||||
@@ -189,6 +206,16 @@ export class EntityManager implements EntityLevel {
|
||||
}
|
||||
}
|
||||
|
||||
play_sound(entity: Entity, sound: string, options: PlayOptions) {
|
||||
const range = sound_range(sound_to_play(sound, options), options.volume);
|
||||
const message = sound_message(sound, options, { entity: entity.id });
|
||||
for (const [player, tracked] of this.#tracked) {
|
||||
if (tracked.has(entity.id) && player.hears(entity.x, entity.y, entity.z, range)) {
|
||||
this.#host.send(player, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
on_death(entity: Entity, source: DamageSource) {
|
||||
this.#host.on_entity_death?.(entity, source);
|
||||
}
|
||||
|
||||
+53
-14
@@ -48,7 +48,8 @@ import { physics } from "@/common/physics.ts";
|
||||
import { EXHAUSTION } from "./vitals.ts";
|
||||
import { entity_ref, ModRuntime, new_stack, run_guarded, type ServerCommandSource } from "./mod_runtime.ts";
|
||||
import type { LootContext, Position, SchematicPlacement, UseTarget } from "@/common/mod_api/server.ts";
|
||||
import type { EntityJson } from "@/common/mod_data.ts";
|
||||
import { type EntityJson, sound_range, sound_to_play } from "@/common/mod_data.ts";
|
||||
import type { PlayOptions } from "@/common/mod_api/shared.ts";
|
||||
import { encode_schematic } from "@/common/schematic.ts";
|
||||
import { capture_schematic, placed_schematic, type RegisteredSchematic } from "./schematics.ts";
|
||||
import {
|
||||
@@ -88,7 +89,7 @@ import {
|
||||
} from "./entity/item_entity.ts";
|
||||
import { Mob } from "./entity/mob.ts";
|
||||
import { LivingEntity } from "./entity/living_entity.ts";
|
||||
import { EntityManager } from "./entity/entity_manager.ts";
|
||||
import { EntityManager, sound_message } from "./entity/entity_manager.ts";
|
||||
import { NaturalSpawner } from "./natural_spawner.ts";
|
||||
import { create_entity, entity_type_ids, load_entity } from "./entity/entity_types.ts";
|
||||
import { damage_source, type DamageSource, type Entity, type SavedEntity } from "./entity/entity.ts";
|
||||
@@ -763,6 +764,40 @@ export class GameServer {
|
||||
}
|
||||
}
|
||||
|
||||
// a sound at x y z for every player within its range, except one whose client already played it. see
|
||||
// ServerSounds.play
|
||||
play_sound(sound: string, x: number, y: number, z: number, options: PlayOptions & { except?: ServerPlayer } = {}) {
|
||||
const range = sound_range(sound_to_play(sound, options), options.volume);
|
||||
const message = sound_message(sound, options, { at: [x, y, z] });
|
||||
for (const player of this.#players.values()) {
|
||||
if (player !== options.except && player.hears(x, y, z, range)) this.#send(player, message);
|
||||
}
|
||||
}
|
||||
|
||||
// a sound only player hears, as it is wherever they are, see Player.play_sound
|
||||
play_sound_to(player: ServerPlayer, sound: string, options: PlayOptions = {}) {
|
||||
sound_to_play(sound, options);
|
||||
this.#send(player, sound_message(sound, options));
|
||||
}
|
||||
|
||||
// the player whose break, place or use the shared rules are running for. their client ran the same rules to guess
|
||||
// it, so it already played what the rules play, see SharedContext.sounds
|
||||
get rules_player() {
|
||||
return this.#rules_player;
|
||||
}
|
||||
#rules_player: ServerPlayer | undefined;
|
||||
|
||||
// fn with the shared rules it runs running for player, see rules_player
|
||||
#as_player<T>(player: ServerPlayer, fn: () => T): T {
|
||||
const before = this.#rules_player;
|
||||
this.#rules_player = player;
|
||||
try {
|
||||
return fn();
|
||||
} finally {
|
||||
this.#rules_player = before;
|
||||
}
|
||||
}
|
||||
|
||||
// a script pushing a player, see Player.push. their client moves them, so it's added there, unless the server is
|
||||
// moving them while they're knocked back
|
||||
push_player(player: ServerPlayer, x: number, y: number, z: number) {
|
||||
@@ -1294,7 +1329,7 @@ export class GameServer {
|
||||
}
|
||||
|
||||
// creative breaks drop nothing, since placing doesn't use anything up
|
||||
this.destroy_block(x, y, z, player, { collect: player.creative });
|
||||
this.#as_player(player, () => this.destroy_block(x, y, z, player, { collect: player.creative }));
|
||||
player.vitals.add_exhaustion(EXHAUSTION.break_block);
|
||||
this.mods.after.block_break.emit(event);
|
||||
}
|
||||
@@ -1464,7 +1499,7 @@ export class GameServer {
|
||||
let handled = false;
|
||||
if (!(sneaking && held_slot.has_item())) {
|
||||
const interaction = { face, hit: [hit[0], hit[1], hit[2]] as [number, number, number], sneaking };
|
||||
handled = this.mods.rules.interact(used, x, y, z, interaction, player.yaw);
|
||||
handled = this.#as_player(player, () => this.mods.rules.interact(used, x, y, z, interaction, player.yaw));
|
||||
for (const { mod, id: component_id, component, params } of this.mods.components_of(used.id)) {
|
||||
if (component.on_interact) {
|
||||
handled = run_guarded(mod, `${component_id} on_interact`, () =>
|
||||
@@ -1509,15 +1544,19 @@ export class GameServer {
|
||||
}
|
||||
// solid blocks can't go where a mob or player is, the one placing it included, like minecraft
|
||||
const placing = EverythingRegistry.get<BlockRegistry>("blocks", held.block_id)!;
|
||||
const ruled_value = this.mods.rules.placement_value(placing, player.yaw, player.pitch, {
|
||||
x: tx,
|
||||
y: ty,
|
||||
z: tz,
|
||||
face,
|
||||
hit: [hit[0], hit[1], hit[2]],
|
||||
sneaking: player.sneaking,
|
||||
item: held_slot.type_id!,
|
||||
});
|
||||
const ruled_value = this.#as_player(
|
||||
player,
|
||||
() =>
|
||||
this.mods.rules.placement_value(placing, player.yaw, player.pitch, {
|
||||
x: tx,
|
||||
y: ty,
|
||||
z: tz,
|
||||
face,
|
||||
hit: [hit[0], hit[1], hit[2]],
|
||||
sneaking: player.sneaking,
|
||||
item: held_slot.type_id!,
|
||||
}),
|
||||
);
|
||||
// its rules say it can't go there
|
||||
if (ruled_value === undefined) {
|
||||
this.#correct(player, tx, ty, tz);
|
||||
@@ -1539,7 +1578,7 @@ export class GameServer {
|
||||
const place = this.#place_event(player, { id: held.block_id, x: tx, y: ty, z: tz }, face, hit);
|
||||
if (!place) return;
|
||||
|
||||
this.set_block(tx, ty, tz, held.block_id, player, state);
|
||||
this.#as_player(player, () => this.set_block(tx, ty, tz, held.block_id!, player, state));
|
||||
if (!player.creative) held_slot.amount = held_slot.amount! - 1;
|
||||
this.mods.after.block_place.emit(place);
|
||||
}
|
||||
|
||||
@@ -333,6 +333,8 @@ export class ModRuntime {
|
||||
fluid_rules: this.fluid_rules,
|
||||
recipes: () => this.game.recipes,
|
||||
running: () => this.#shared_setup,
|
||||
play_sound: (sound, x, y, z, options) =>
|
||||
this.game.play_sound(sound, x, y, z, { ...options, except: this.game.rules_player }),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -756,6 +758,9 @@ export class ModRuntime {
|
||||
const { [field]: _, ...rest } = mob.client_data;
|
||||
mob.client_data = value === undefined ? rest : { ...rest, [field]: structuredClone(value) };
|
||||
},
|
||||
play_sound(sound, options = {}) {
|
||||
mob.level.play_sound(mob, sound, options);
|
||||
},
|
||||
};
|
||||
return Object.defineProperties(living_ref(mob), Object.getOwnPropertyDescriptors(extra)) as MobRef;
|
||||
}
|
||||
@@ -787,6 +792,9 @@ export class ModRuntime {
|
||||
set_ticking(ticking) {
|
||||
game.set_tile_ticking(x, y, z, ticking);
|
||||
},
|
||||
play_sound(sound, options) {
|
||||
game.play_sound(sound, x + 0.5, y + 0.5, z + 0.5, options);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1250,6 +1258,14 @@ export class ModRuntime {
|
||||
game().world.dirty = true;
|
||||
},
|
||||
},
|
||||
sounds: {
|
||||
play: (sound, x, y, z, options = {}) => {
|
||||
const except = options.except && this.#server_players.get(options.except);
|
||||
if (options.except && !except) throw new Error("a sound's except must be a player");
|
||||
game().play_sound(sound, ...at_position(x, y, z), { ...options, except });
|
||||
},
|
||||
exists: (sound) => EverythingRegistry.get("sounds", sound) !== undefined,
|
||||
},
|
||||
log: (...args) => console.log(`[${mod}]`, ...args),
|
||||
};
|
||||
return ctx;
|
||||
@@ -1711,9 +1727,18 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
player.using = undefined;
|
||||
},
|
||||
is_using: () => player.using !== undefined,
|
||||
play_sound(sound, options) {
|
||||
game.play_sound_to(player, sound, options);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// a position a script gave, checked
|
||||
function at_position(x: unknown, y: unknown, z: unknown): [number, number, number] {
|
||||
if (![x, y, z].every(Number.isFinite)) throw new Error("a position is three numbers");
|
||||
return [x, y, z] as [number, number, number];
|
||||
}
|
||||
|
||||
function known_effect(id: string): EffectType {
|
||||
const type = effect_type(id);
|
||||
if (!type) throw new Error(`unknown effect ${id}`);
|
||||
@@ -1831,5 +1856,8 @@ export function entity_ref(entity: Entity): EntityRef {
|
||||
if (!item || item.count <= 0) entity.remove();
|
||||
else entity.item = new_stack(item);
|
||||
},
|
||||
play_sound(sound, options = {}) {
|
||||
entity.level.play_sound(entity, sound, options);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -202,6 +202,11 @@ export class ServerPlayer extends LivingEntity {
|
||||
return this.inventory.get_item(this.selected_slot);
|
||||
}
|
||||
|
||||
// whether a sound at x y z heard as far as range reaches their ears
|
||||
hears(x: number, y: number, z: number, range: number) {
|
||||
return Math.hypot(x - this.x, y - (this.y + this.eye_height), z - this.z) < range;
|
||||
}
|
||||
|
||||
// the short box and low eyes while swimming, so drowning, reach and what they're in the way of match
|
||||
set_swimming(swimming: boolean) {
|
||||
this.swimming = swimming;
|
||||
|
||||
Reference in New Issue
Block a user