From 97d119b4e8995800f9e0e1de497af4c460758760 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Tue, 27 Oct 2026 06:39:31 +0100 Subject: [PATCH] Loop the bee buzz and the guardian attack hum like vanilla --- crates/horizon-client/src/game.rs | 7 ++ crates/horizon-client/src/game/creatures.rs | 3 + crates/horizon-client/src/game/lan.rs | 5 + crates/horizon-client/src/game/mob_sounds.rs | 82 +++++++++++++ crates/horizon-client/src/game/music.rs | 2 + crates/horizon-client/src/game/remote.rs | 4 + crates/horizon-client/src/mobs.rs | 7 ++ crates/horizon-client/src/mobs/guardian.rs | 25 ++-- crates/horizon-client/src/sound_engine.rs | 123 +++++++++++++++++++ 9 files changed, 250 insertions(+), 8 deletions(-) create mode 100644 crates/horizon-client/src/game/mob_sounds.rs diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 7699e83d7..8e1de77e4 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -103,6 +103,7 @@ mod maps; mod minecarts; mod mob_blocks; mod mob_gear; +mod mob_sounds; mod mod_browser; #[cfg(feature = "mods")] mod mod_browser_recipes; @@ -1379,6 +1380,10 @@ struct Game<'a> { cart_sounds: HashSet, /// The minecart the riding sounds were started for (`LocalPlayer.startRiding`). riding_cart_sounds: Option, + /// Bees whose buzz is playing, and whether it is the aggressive one. + bee_sounds: HashMap, + /// Guardians whose attack hum is playing. + guardian_sounds: HashSet, dropped: Vec, next_entity: u32, particles: horizon_client::particles::ParticleEngine, @@ -12908,6 +12913,8 @@ impl<'a> Game<'a> { sound_paused: false, cart_sounds: HashSet::new(), riding_cart_sounds: None, + bee_sounds: HashMap::new(), + guardian_sounds: HashSet::new(), dropped: Vec::new(), next_entity: 0, particles, diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 2bc40a7af..cf26174c5 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -933,6 +933,9 @@ impl Game<'_> { match e { MobEvent::SquidCycle { mob, at } => self.lan_entity_event(mob as i32, 19, at), MobEvent::RabbitHop { mob, at } => self.lan_entity_event(mob as i32, 1, at), + MobEvent::EntityEvent { mob, event, at } => { + self.lan_entity_event(mob as i32, event, at) + } MobEvent::Sound(name, at, volume, pitch) | MobEvent::LocalSound(name, at, volume, pitch) => { self.audio diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 28ec3fe8a..a29de78e6 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -1302,6 +1302,11 @@ impl Game<'_> { if self.audio.capture.is_some() { self.lan_entity_events.push((id, event, at)); } + // Event 21: a window that simulates the world itself is its own + // client, and hears the guardian's hum too. + if event == 21 && self.options.execution_role.is_client() { + self.start_guardian_sound(id as u32); + } } /// `ServerPlayer.crit` (or `magicCrit` when `magic`): this window's /// emitter, and an animation for the attacker's client and observers once diff --git a/crates/horizon-client/src/game/mob_sounds.rs b/crates/horizon-client/src/game/mob_sounds.rs new file mode 100644 index 000000000..50065d94f --- /dev/null +++ b/crates/horizon-client/src/game/mob_sounds.rs @@ -0,0 +1,82 @@ +//! The looping sound instances of mobs: a bee's buzz +//! (`ClientPacketListener.postAddEntitySoundInstance`) and a guardian's +//! attack hum (entity event 21). The sound engine ticks them with what +//! `start_mob_sounds` reads off the mobs. +use super::Game; +use horizon_client::{ + mobs::{Species, bee, guardian}, + sound_engine::{MobSound, SoundEngine, SoundInstance, Sounds}, +}; + +impl Game<'_> { + /// Start the buzz of each bee that has none (or whose anger changed, which + /// swaps the flying loop for the aggressive one and back), forget the mobs + /// that are gone, and read what the instances follow. + pub(super) fn start_mob_sounds(&mut self) -> Vec { + let game_time = self.world.game_time; + let mobs: Vec = self + .mobs + .iter() + .filter(|m| { + !m.removed + && matches!( + m.species(), + Species::Bee | Species::Guardian | Species::ElderGuardian + ) + }) + .map(|m| MobSound { + id: m.id, + position: [m.position.x, m.position.y, m.position.z], + // Vec3.horizontalDistance. + speed: m.velocity.x.hypot(m.velocity.z), + baby: m.is_baby(), + // NeutralMob.isAngry: the anger end time is ahead. + angry: m.species() == Species::Bee + && bee::get(m).is_some_and(|b| b.anger_end > 0 && b.anger_end - game_time > 0), + silent: m.silent, + attack_scale: guardian::attack_scale(m), + }) + .collect(); + self.guardian_sounds + .retain(|id| mobs.iter().any(|m| m.id == *id)); + self.bee_sounds + .retain(|id, _| mobs.iter().any(|m| m.id == *id)); + for sound in &mobs { + let is_bee = self + .mobs + .iter() + .any(|m| m.id == sound.id && m.species() == Species::Bee); + if is_bee && self.bee_sounds.get(&sound.id) != Some(&sound.angry) { + self.bee_sounds.insert(sound.id, sound.angry); + let instance = SoundInstance::bee(sound, sound.angry, SoundEngine::unseeded()); + self.audio.play_instance(instance, false); + } + } + mobs + } + + /// `ClientPacketListener.handleEntityEvent` 21: a guardian's attack + /// hum starts (one loop per guardian; the loops of its earlier beams are + /// the same loop, as each one follows the guardian's charge). + pub(super) fn start_guardian_sound(&mut self, id: u32) { + let Some(m) = self.mobs.iter().find(|m| { + m.id == id && matches!(m.species(), Species::Guardian | Species::ElderGuardian) + }) else { + return; + }; + if !self.guardian_sounds.insert(id) { + return; + } + let sound = MobSound { + id, + position: [m.position.x, m.position.y, m.position.z], + speed: 0.0, + baby: false, + angry: false, + silent: m.silent, + attack_scale: guardian::attack_scale(m), + }; + let instance = SoundInstance::guardian_attack(&sound, SoundEngine::unseeded()); + self.audio.play_instance(instance, false); + } +} diff --git a/crates/horizon-client/src/game/music.rs b/crates/horizon-client/src/game/music.rs index 97ca3763e..6e303e7d3 100644 --- a/crates/horizon-client/src/game/music.rs +++ b/crates/horizon-client/src/game/music.rs @@ -34,11 +34,13 @@ impl Game<'_> { self.sound_paused = self.pause; } let (vehicle, carts) = self.start_cart_sounds(); + let mobs = self.start_mob_sounds(); let ctx = TickContext { player_underwater: self.ambience.was_underwater(), player_removed: self.player.is_none(), vehicle, carts, + mobs, frozen: !self.clock.runs_normally(), }; self.audio.tick(self.pause, &ctx); diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 77e143f9a..9c0927d11 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -2130,6 +2130,10 @@ impl Game<'_> { self.ui.registries.permission = (p.event - 24) as u8; self.remote_world_options_permission(p.event >= 26); } + // ClientPacketListener.handleEntityEvent 21: a guardian's hum. + if p.event == 21 { + self.start_guardian_sound(p.id as u32); + } if p.event == 1 && let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) && m.species() == horizon_client::mobs::Species::Rabbit diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 2325c3f5b..1cb338bee 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -903,6 +903,13 @@ pub enum MobEvent { mob: u32, at: [f64; 3], }, + /// `Level.broadcastEntityEvent` for a mob: guests run its + /// `handleEntityEvent`. + EntityEvent { + mob: u32, + event: i8, + at: [f64; 3], + }, Sound(&'static str, [f64; 3], f32, f32), /// `ChestBlockEntity.startOpen` / `stopOpen` by a copper golem: the /// chest's opener count goes up or down (interaction range 3). diff --git a/crates/horizon-client/src/mobs/guardian.rs b/crates/horizon-client/src/mobs/guardian.rs index 880655361..fdd596b54 100644 --- a/crates/horizon-client/src/mobs/guardian.rs +++ b/crates/horizon-client/src/mobs/guardian.rs @@ -112,14 +112,14 @@ impl Goal for GuardianAttack { if self.attack_time == 0 { m.extra.flag = true; m.extra.counter2 = 0; - // Entity event 21: the attack sound loop starts. - let sound = "entity.guardian.attack"; - cx.events.push(MobEvent::Sound( - sound, - [m.position.x, m.position.y, m.position.z], - 1.0, - 1.0, - )); + // Entity event 21: the attack sound loop starts on the clients. + if !m.silent { + cx.events.push(MobEvent::EntityEvent { + mob: m.id, + event: 21, + at: [m.position.x, m.position.y, m.position.z], + }); + } } else if self.attack_time >= attack_duration(m) { // Indirect magic (1, +2 on Hard, +2 for the elder), then // doHurtTarget. @@ -459,3 +459,12 @@ pub fn anim(m: &Mob, s: &State, p: &mut Poses) { p.yrot("tail1", tail.msin() * PI * 0.1); p.yrot("tail2", tail.msin() * PI * 0.15); } +/// `Guardian.getAttackAnimationScale(0)`: how far the beam has charged. A +/// beam that ended counts from zero again. +pub fn attack_scale(m: &Mob) -> f32 { + if m.extra.flag { + m.extra.counter2 as f32 / attack_duration(m) as f32 + } else { + 0.0 + } +} diff --git a/crates/horizon-client/src/sound_engine.rs b/crates/horizon-client/src/sound_engine.rs index 562b14100..f3e2dd75d 100644 --- a/crates/horizon-client/src/sound_engine.rs +++ b/crates/horizon-client/src/sound_engine.rs @@ -53,6 +53,11 @@ pub enum Ticking { Minecart { cart: u32 }, /// `RidingMinecartSoundInstance` for the player riding a minecart. RidingMinecart { cart: u32, underwater: bool }, + /// `BeeFlyingSoundInstance`, or `BeeAggressiveSoundInstance` when + /// `aggressive`. + Bee { bee: u32, aggressive: bool }, + /// `GuardianAttackSoundInstance`. + GuardianAttack { guardian: u32 }, } /// What a minecart's sound instances read about it. #[derive(Clone, Copy, Debug)] @@ -62,6 +67,21 @@ pub struct CartSound { /// `getDeltaMovement().horizontalDistance()`. pub speed: f64, } +/// What a bee's or a guardian's sound instance reads about it. +#[derive(Clone, Copy, Debug)] +pub struct MobSound { + pub id: u32, + pub position: [f64; 3], + /// `getDeltaMovement().horizontalDistance()`. + pub speed: f64, + pub baby: bool, + /// `Bee.isAngry`. + pub angry: bool, + /// `isSilent`. + pub silent: bool, + /// `Guardian.getAttackAnimationScale(0)`. + pub attack_scale: f32, +} /// What tickable instances read about the player and the entities they follow. #[derive(Clone, Debug, Default)] pub struct TickContext { @@ -71,6 +91,8 @@ pub struct TickContext { pub vehicle: Option, /// The minecarts in the level (a missing one has been removed). pub carts: Vec, + /// The bees and guardians in the level (a missing one has been removed). + pub mobs: Vec, /// `!tickRateManager().runsNormally()`. pub frozen: bool, } @@ -315,6 +337,59 @@ impl SoundInstance { }; s } + /// `new BeeFlyingSoundInstance(bee)` (or `BeeAggressiveSoundInstance`): + /// silent until the bee moves. + pub fn bee(bee: &MobSound, aggressive: bool, random: LegacyRandom) -> Self { + let id = if aggressive { + "minecraft:entity.bee.loop_aggressive" + } else { + "minecraft:entity.bee.loop" + }; + let [x, y, z] = bee.position; + let mut s = Self::new( + id, + NEUTRAL, + 0.0, + 1.0, + random, + true, + 0, + Attenuation::Linear, + [ + f64::from(x as f32), + f64::from(y as f32), + f64::from(z as f32), + ], + false, + ); + s.can_start_silent = true; + s.ticking = Ticking::Bee { + bee: bee.id, + aggressive, + }; + s + } + /// `new GuardianAttackSoundInstance(guardian)`: the guardian's own volume + /// and pitch until its first tick, heard everywhere. + pub fn guardian_attack(guardian: &MobSound, random: LegacyRandom) -> Self { + let id = "minecraft:entity.guardian.attack"; + let mut s = Self::new( + id, + crate::settings::sound_source(id), + 1.0, + 1.0, + random, + true, + 0, + Attenuation::None, + guardian.position, + false, + ); + s.ticking = Ticking::GuardianAttack { + guardian: guardian.id, + }; + s + } pub fn tickable(&self) -> bool { self.ticking != Ticking::No } @@ -389,6 +464,54 @@ impl SoundInstance { 0.0 }; } + Ticking::Bee { bee, aggressive } => { + let (id, aggressive) = (*bee, *aggressive); + // canPlaySound, then shouldSwitchSounds: the other kind of + // instance is started in this one's place. + let Some(bee) = ctx + .mobs + .iter() + .find(|m| m.id == id) + .filter(|m| !m.silent && m.angry == aggressive) + else { + self.stop(); + return; + }; + let [x, y, z] = bee.position; + self.position = [ + f64::from(x as f32), + f64::from(y as f32), + f64::from(z as f32), + ]; + let (min, max) = if bee.baby { (1.1, 1.5) } else { (0.7, 1.1) }; + let speed = bee.speed as f32; + if speed >= 0.01 { + // Mth.lerp(Mth.clamp(speed, min, max), min, max). + self.pitch = min + speed.clamp(min, max) * (max - min); + self.volume = speed.clamp(0.0, 0.5) * 1.2; + } else { + self.pitch = 0.0; + self.volume = 0.0; + } + } + Ticking::GuardianAttack { guardian } => { + let id = *guardian; + // The client's guardian never has a target of its own, so + // only its removal or silence ends the loop. + let Some(guardian) = ctx.mobs.iter().find(|m| m.id == id && !m.silent) else { + self.stop(); + return; + }; + let [x, y, z] = guardian.position; + self.position = [ + f64::from(x as f32), + f64::from(y as f32), + f64::from(z as f32), + ]; + let scale = guardian.attack_scale; + self.volume = 0.0 + 1.0 * scale * scale; + self.pitch = 0.7 + 0.5 * scale; + } Ticking::RidingMinecart { cart, underwater } => { let (id, underwater) = (*cart, *underwater); let found = ctx.carts.iter().find(|c| c.id == id);