diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index f78e33599..1949099d7 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -1343,8 +1343,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), @@ -1921,6 +1923,9 @@ impl Game<'_> { && let Some(child) = self.mobs.iter_mut().find(|m| m.id == id) { child.extra.age = -24000; + // getBreedOffspring creates the baby without finalizeSpawn: it keeps + // the default (`classic`) sound variant. + child.extra.sound_variant = None; // A llama's baby was drawn in the parent's tick, with the // experience. if let Some(o) = llama { @@ -2016,8 +2021,12 @@ impl Game<'_> { } // Offspring inherit a parent's variant (pigs/cows/chickens) or mixed wool. child.extra.variant = p.extra.variant; - // Fox.getBreedOffspring: either parent's variant. - if p.species() == Species::Fox && !coin { + // Fox, Pig, Cow and Chicken.getBreedOffspring: either parent's variant. + if matches!( + p.species(), + Species::Fox | Species::Pig | Species::Cow | Species::Chicken + ) && !coin + { child.extra.variant = q.as_ref().map_or(p.extra.variant, |q| q.extra.variant); } if p.species() == Species::Sheep { diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index eaadf14a4..5ed5fa0e1 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, } } @@ -1514,13 +1522,12 @@ pub fn age_up(m: &mut Mob, seconds: i32, forced: bool) { } } /// `Animal.playEatingSound` (empty) and its overrides, as (sound, volume, -/// pitch); `level` is the level's random. Pigs and cats use the classic sound -/// set, like their other sounds here. +/// pitch); `level` is the level's random. Pigs and cats play their sound +/// variant's set. pub fn eating_sound( m: &mut Mob, level: &mut impl RandomSource, ) -> Option<(&'static str, f32, f32)> { - let baby = m.is_baby(); // Mth.randomBetween(level.getRandom(), 0.8, 1.2). let mut between = || level.next_float() * 0.4 + 0.8; let (sound, volume, pitch, entity_sound) = match m.species() { @@ -1528,16 +1535,21 @@ 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 +2025,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/allay.rs b/crates/horizon-client/src/mobs/allay.rs index 22461945d..8c2ced406 100644 --- a/crates/horizon-client/src/mobs/allay.rs +++ b/crates/horizon-client/src/mobs/allay.rs @@ -95,7 +95,8 @@ impl Goal for Deliver { } } } -/// `StayCloseToTarget`: back within 4 blocks of the deposit. +/// `StayCloseToTarget.create(.., 4, 16, 2.25)`: acts only once the deposit is +/// 16 blocks away or more, then walks until within 4. #[derive(Clone, Debug)] struct StayClose; impl Goal for StayClose { @@ -109,7 +110,19 @@ impl Goal for StayClose { let d = (p[0] - m.position.x).powi(2) + (p[1] - m.position.y).powi(2) + (p[2] - m.position.z).powi(2); - d > 16.0 + d >= 256.0 + }) + } + /// `MoveToTargetSink.reachedTarget`: the walk target it set is reached + /// within a Manhattan block distance of 4 (`closeEnough`). + fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + m.extra.point.is_none() + && deposit(m, cx).is_some_and(|p| { + let at = [m.position.x, m.position.y, m.position.z]; + (0..3) + .map(|i| (p[i].floor() as i32 - at[i].floor() as i32).abs()) + .sum::() + > 4 }) } fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { diff --git a/crates/horizon-client/src/mobs/aquatic.rs b/crates/horizon-client/src/mobs/aquatic.rs index fdc543e08..a5e2bee3c 100644 --- a/crates/horizon-client/src/mobs/aquatic.rs +++ b/crates/horizon-client/src/mobs/aquatic.rs @@ -534,7 +534,7 @@ impl Goal for PufferfishPuff { cx.w.player .as_ref() .is_some_and(|p| p.alive && !p.creative && area.intersects(p.bounds)); - // TARGETING_CONDITIONS: not other fish (not_scary_for_pufferfish) and alive. + // TARGETING_CONDITIONS: not #not_scary_for_pufferfish, and alive. let mob = cx.w.others_near(area).any(|s| { s.id != m.id && s.alive @@ -547,7 +547,13 @@ impl Goal for PufferfishPuff { | Species::Squid | Species::GlowSquid | Species::Dolphin - | Species::Axolotl + | Species::Turtle + | Species::Guardian + | Species::ElderGuardian + | Species::Tadpole + | Species::Nautilus + | Species::ZombieNautilus + | Species::SulfurCube ) && area.intersects(s.bounds) }); diff --git a/crates/horizon-client/src/mobs/equine.rs b/crates/horizon-client/src/mobs/equine.rs index 8b0463849..b354a2908 100644 --- a/crates/horizon-client/src/mobs/equine.rs +++ b/crates/horizon-client/src/mobs/equine.rs @@ -114,28 +114,40 @@ pub fn register(m: &mut Mob) { g.add(3, Tempt::new(1.25, tempt, false)); } } -/// `generateMaxHealth`, `generateSpeed`, `generateJumpStrength` and the coat. +/// `randomizeAttributes` of each class: horses roll health, speed and jump, +/// donkeys and mules only health (their jump stays 0.5), skeleton horses only +/// the jump, zombie horses a smaller jump and their own speed. pub fn finalize(m: &mut Mob) { + let species = m.species(); let r = &mut m.random; - let health = - 15.0 + r.next_int_bound(8).unwrap_or(0) as f32 + r.next_int_bound(9).unwrap_or(0) as f32; - let speed = - (0.45 + r.next_double() * 0.3 + r.next_double() * 0.3 + r.next_double() * 0.3) * 0.25; - let jump = 0.4 + r.next_double() * 0.2 + r.next_double() * 0.2 + r.next_double() * 0.2; - match m.species() { + if matches!(species, Species::Horse | Species::Donkey | Species::Mule) { + m.attrs.max_health = 15.0 + + r.next_int_bound(8).unwrap_or(0) as f32 + + r.next_int_bound(9).unwrap_or(0) as f32; + } + match species { Species::Horse => { - m.attrs.max_health = health; - m.attrs.speed = speed; - m.extra.vector[0] = jump; - m.extra.variant = m.random.next_int_bound(7).unwrap_or(0) as u8; - m.extra.variant2 = m.random.next_int_bound(5).unwrap_or(0) as u8; + m.attrs.speed = + (0.45 + r.next_double() * 0.3 + r.next_double() * 0.3 + r.next_double() * 0.3) + * 0.25; + m.extra.vector[0] = + 0.4 + r.next_double() * 0.2 + r.next_double() * 0.2 + r.next_double() * 0.2; + m.extra.variant = r.next_int_bound(7).unwrap_or(0) as u8; + m.extra.variant2 = r.next_int_bound(5).unwrap_or(0) as u8; } - Species::Donkey | Species::Mule => { - // AbstractChestedHorse.randomizeAttributes: fixed speed, generated health. - m.attrs.max_health = health; - m.extra.vector[0] = jump; + Species::Donkey | Species::Mule => m.extra.vector[0] = 0.5, + Species::ZombieHorse => { + // ZombieHorse.generateZombieHorseJumpStrength and ...Speed. + let d = 0.066_666_666_666_666_67; + m.extra.vector[0] = + 0.5 + r.next_double() * d + r.next_double() * d + r.next_double() * d; + m.attrs.speed = + (9.0 + r.next_double() + r.next_double() + r.next_double()) / f64::from(42.16f32); + } + _ => { + m.extra.vector[0] = + 0.4 + r.next_double() * 0.2 + r.next_double() * 0.2 + r.next_double() * 0.2 } - _ => m.extra.vector[0] = jump, } m.health = m.attrs.max_health; } diff --git a/crates/horizon-client/src/mobs/finalize.rs b/crates/horizon-client/src/mobs/finalize.rs index 52f3d575f..b360864ed 100644 --- a/crates/horizon-client/src/mobs/finalize.rs +++ b/crates/horizon-client/src/mobs/finalize.rs @@ -2046,6 +2046,46 @@ 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/goals.rs b/crates/horizon-client/src/mobs/goals.rs index e4682b0de..6698152d0 100644 --- a/crates/horizon-client/src/mobs/goals.rs +++ b/crates/horizon-client/src/mobs/goals.rs @@ -1893,6 +1893,10 @@ impl Goal for Breed { if tamable && (!m.extra.tame || m.extra.sitting) { return false; } + // AbstractHorse.canMate is false and Mule keeps it: mules never breed. + if species == Species::Mule { + return false; + } // TurtleBreedGoal.canUse: not while carrying an egg. if species == Species::Turtle && super::turtle::has_egg(m) { return false; diff --git a/crates/horizon-client/src/mobs/spawning.rs b/crates/horizon-client/src/mobs/spawning.rs index 867869070..ca31c9ca6 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()), @@ -301,6 +307,17 @@ pub fn finalize(m: &mut Mob, biome: &BiomeFacts, difficulty: &LocalDifficulty) { } S::Fox | S::Panda | S::Dolphin => m.extra.pick_up_loot = true, S::TropicalFish => super::aquatic::finalize(m), + // Salmon.finalizeSpawn: a weighted size (small 30, medium 50, large 15). + S::Salmon => { + let r = m.random.next_int_bound(95).unwrap_or(0); + m.extra.variant = if r < 30 { + 0 + } else if r < 80 { + 1 + } else { + 2 + }; + } S::Slime | S::MagmaCube => super::cube::finalize(m), _ => {} } diff --git a/crates/horizon-client/src/mobs/species.rs b/crates/horizon-client/src/mobs/species.rs index b2e206d77..44f3d4adf 100644 --- a/crates/horizon-client/src/mobs/species.rs +++ b/crates/horizon-client/src/mobs/species.rs @@ -1602,6 +1602,8 @@ impl Def { pub fn water_slowdown(&self) -> f32 { match self.species { Species::PolarBear => 0.98, + // SkeletonHorse.getWaterSlowDown. + Species::SkeletonHorse => 0.96, _ => 0.8, } } 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