diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 3145c2f75..17504f56d 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -1279,8 +1279,10 @@ impl Game<'_> { MobEvent::Hiss { mob } => { if let Some(m) = self.mobs.iter().find(|m| m.id == mob) { let at = m.center(); + let sound = horizon_client::mobs::finalize::variant_sound(m, "hiss") + .unwrap_or("entity.cat.hiss"); self.audio - .play("minecraft:entity.cat.hiss", Some(at), 1.0, 1.0); + .play(&format!("minecraft:{sound}"), Some(at), 1.0, 1.0); } } MobEvent::Tamed { .. } | MobEvent::Buck { .. } => self.mount_event(&e), diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index eaadf14a4..6bd5a73df 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -828,17 +828,28 @@ fn play_ambient(m: &mut Mob, w: &MobWorld, events: &mut Vec) { // Nautilus/ZombieNautilus.getAmbientSound: by age, under water or on land. Species::Nautilus | Species::ZombieNautilus => super::nautilus::sounds(m)[0], Species::Enderman if m.ai.target.is_some() => "entity.enderman.scream", - Species::Wolf if m.ai.target.is_some() => "entity.wolf.growl", + Species::Wolf if m.ai.target.is_some() => { + super::finalize::variant_sound(m, "growl").unwrap_or("entity.wolf.growl") + } Species::Wolf if m.random.next_int_bound(3).unwrap_or(1) == 0 => { - if m.extra.tame && m.health < m.attrs.max_health / 2.0 { - "entity.wolf.whine" + let kind = if m.extra.tame && m.health < m.attrs.max_health / 2.0 { + "whine" } else { - "entity.wolf.pant" - } + "pant" + }; + super::finalize::variant_sound(m, kind).unwrap_or("entity.wolf.pant") } Species::Parrot => super::parrot::ambient(m, w), - Species::Cat if m.extra.tame && m.extra.in_love > 0 => "entity.cat.purr", - Species::Cat if !m.extra.tame => "entity.cat.stray_ambient", + // Cat.getAmbientSound: purr in love, purreow one time in four, else ambient. + Species::Cat if m.extra.tame && m.extra.in_love > 0 => { + super::finalize::variant_sound(m, "purr").unwrap_or("entity.cat.purr") + } + Species::Cat if m.extra.tame && m.random.next_int_bound(4).unwrap_or(1) == 0 => { + super::finalize::variant_sound(m, "purreow").unwrap_or("entity.cat.purreow") + } + Species::Cat if !m.extra.tame => { + super::finalize::variant_sound(m, "stray_ambient").unwrap_or("entity.cat.stray_ambient") + } Species::PolarBear if m.is_baby() => "entity.polar_bear.ambient_baby", Species::Dolphin if !m.fluids.in_water => "entity.dolphin.ambient", Species::Guardian if !m.fluids.in_water => "entity.guardian.ambient_land", @@ -1455,17 +1466,14 @@ pub fn voice_sounds(m: &Mob) -> [&'static str; 3] { }; [ambient, m.kind.sounds[1], m.kind.sounds[2]] } - // The sound variant's baby_sounds (classic sets, like the rest here). - Species::Pig if m.is_baby() => [ - "entity.baby_pig.ambient", - "entity.baby_pig.hurt", - "entity.baby_pig.death", - ], - Species::Chicken if m.is_baby() => [ - "entity.baby_chicken.ambient", - "entity.baby_chicken.hurt", - "entity.baby_chicken.death", - ], + // The mob's sound variant set, the baby one for a baby. + Species::Pig | Species::Cow | Species::Chicken | Species::Wolf | Species::Cat => { + use super::finalize::variant_sound as v; + match (v(m, "ambient"), v(m, "hurt"), v(m, "death")) { + (Some(a), Some(h), Some(d)) => [a, h, d], + _ => m.kind.sounds, + } + } _ => m.kind.sounds, } } @@ -1528,16 +1536,19 @@ pub fn eating_sound( Species::Armadillo | Species::Pig | Species::SulfurCube => { let sound = match m.species() { Species::Armadillo => "entity.armadillo.eat", - Species::Pig if baby => "entity.baby_pig.eat", - Species::Pig => "entity.pig.eat", + Species::Pig => super::finalize::variant_sound(m, "eat").unwrap_or("entity.pig.eat"), _ => "entity.small_sulfur_cube.eat", }; let pitch = m.voice_pitch(); (sound, sound_volume(m), pitch, true) } // playSound(sound, 1, 1). - Species::Cat if baby => ("entity.baby_cat.eat", 1.0, 1.0, true), - Species::Cat => ("entity.cat.eat", 1.0, 1.0, true), + Species::Cat => ( + super::finalize::variant_sound(m, "eat").unwrap_or("entity.cat.eat"), + 1.0, + 1.0, + true, + ), Species::Fox => ("entity.fox.eat", 1.0, 1.0, true), // level().playSound(null, this, sound, NEUTRAL, volume, pitch). Species::Frog => ("entity.frog.eat", 2.0, 1.0, false), @@ -2013,12 +2024,10 @@ pub fn step_sound( | S::ElderGuardian | S::Breeze => None, S::Armadillo => quiet("entity.armadillo.step"), - S::Chicken if baby => quiet("entity.baby_chicken.step"), - S::Chicken => quiet("entity.chicken.step"), - S::Cow | S::Mooshroom => quiet("entity.cow.step"), - S::Pig if baby => quiet("entity.baby_pig.step"), - S::Pig => quiet("entity.pig.step"), - S::Wolf => quiet("entity.wolf.step"), + S::Chicken | S::Cow | S::Pig | S::Wolf => { + super::finalize::variant_sound(m, "step").and_then(quiet) + } + S::Mooshroom => quiet("entity.cow.step"), S::Llama | S::TraderLlama => quiet("entity.llama.step"), S::Frog => quiet("entity.frog.step"), S::Goat => quiet("entity.goat.step"), diff --git a/crates/horizon-client/src/mobs/finalize.rs b/crates/horizon-client/src/mobs/finalize.rs index 52f3d575f..8bc9a0b98 100644 --- a/crates/horizon-client/src/mobs/finalize.rs +++ b/crates/horizon-client/src/mobs/finalize.rs @@ -2046,6 +2046,43 @@ pub fn sound_variants(s: Species) -> &'static [&'static str] { _ => &[], } } +/// A sound of the mob's `*_sound_variant` set (`Pig.getSoundSet`, +/// `Wolf.getSoundSet`, `Cat.getSoundSet`, ...): `kind` is the sound's key +/// (`ambient`, `hurt`, `step`, `growl`, `purreow`...). Babies use the species' +/// baby set (cows have none); an adult's set is `entity..*` for +/// `classic` and `entity._.*` otherwise, except the +/// footsteps that every variant shares (not the cow's). None for a species +/// without sound variants. +pub fn variant_sound(m: &Mob, kind: &str) -> Option<&'static str> { + use std::{collections::HashMap, sync::Mutex}; + let species = m.species(); + let name = match species { + Species::Pig => "pig", + Species::Cow => "cow", + Species::Chicken => "chicken", + Species::Wolf => "wolf", + Species::Cat => "cat", + _ => return None, + }; + let variant = m + .extra + .sound_variant + .and_then(|i| sound_variants(species).get(i as usize)) + .copied() + .unwrap_or("classic"); + let id = if m.is_baby() && species != Species::Cow { + format!("entity.baby_{name}.{kind}") + } else if variant == "classic" || (kind == "step" && species != Species::Cow) { + format!("entity.{name}.{kind}") + } else { + format!("entity.{name}_{variant}.{kind}") + }; + // A handful of ids in all: keep each one as a `&'static str`. + static IDS: Mutex>> = Mutex::new(None); + let mut ids = IDS.lock().ok()?; + let ids = ids.get_or_insert_with(HashMap::new); + Some(*ids.entry(id).or_insert_with_key(|k| Box::leak(k.clone().into_boxed_str()))) +} /// The registry name of the game's variant index. pub fn variant_name(s: Species, index: u8) -> &'static str { let i = index as usize; diff --git a/crates/horizon-client/src/mobs/spawning.rs b/crates/horizon-client/src/mobs/spawning.rs index 867869070..b7930f259 100644 --- a/crates/horizon-client/src/mobs/spawning.rs +++ b/crates/horizon-client/src/mobs/spawning.rs @@ -168,6 +168,12 @@ pub fn finalize(m: &mut Mob, biome: &BiomeFacts, difficulty: &LocalDifficulty) { Climate::Warm => 1, Climate::Cold => 2, }; + // `Registry.getRandom` of the `*_sound_variant` registry (pig, cow, + // chicken, cat and wolf), drawn by their finalizeSpawn. + let sounds = super::finalize::sound_variants(m.species()).len(); + if sounds > 0 { + m.extra.sound_variant = Some(m.random.next_int_bound(sounds as i32).unwrap_or(0) as u8); + } match m.species() { // Warden.finalizeSpawn: a minute before it digs back down. S::Warden => super::warden::finalize(m, false, &mut Vec::new()), diff --git a/crates/horizon-client/src/mobs/tame.rs b/crates/horizon-client/src/mobs/tame.rs index 53d15dba1..48e5a8890 100644 --- a/crates/horizon-client/src/mobs/tame.rs +++ b/crates/horizon-client/src/mobs/tame.rs @@ -829,7 +829,8 @@ impl Goal for CatTempt { // Cat.tick: a wild cat being tempted begs every 100 ticks. if m.age.is_multiple_of(100) { cx.events.push(MobEvent::Sound( - "entity.cat.beg_for_food", + super::finalize::variant_sound(m, "beg_for_food") + .unwrap_or("entity.cat.beg_for_food"), m.center(), 1.0, 1.0, @@ -1197,7 +1198,8 @@ pub fn cat_ai_step(m: &mut Mob, events: &mut Vec) { cat_amounts(m); if (m.extra.flag || m.extra.relax_one) && m.age.is_multiple_of(5) { let volume = 0.6 + 0.4 * (m.random.next_float() - m.random.next_float()); - events.push(MobEvent::Sound("entity.cat.purr", m.center(), volume, 1.0)); + let sound = super::finalize::variant_sound(m, "purr").unwrap_or("entity.cat.purr"); + events.push(MobEvent::Sound(sound, m.center(), volume, 1.0)); } } /// The lying and relaxing amounts of `Cat.tick` (also a client's, from the