diff --git a/crates/horizon-client/src/audio.rs b/crates/horizon-client/src/audio.rs index 6705235ae..54bd3bda8 100644 --- a/crates/horizon-client/src/audio.rs +++ b/crates/horizon-client/src/audio.rs @@ -617,6 +617,11 @@ impl Audio { let seed = self.random.next_long(); self.play_seeded(event, at, volume, pitch, seed); } + /// `LivingEntity.handleDamageEvent`'s pitch, `(r - r) * 0.2 + 1`, drawn on the + /// client (the entity's own random there, never the server's world random). + pub fn hurt_pitch(&mut self) -> f32 { + (self.random.next_float() - self.random.next_float()) * 0.2 + 1.0 + } /// `AbstractWidget.playButtonClickSound`: an unseeded, relative UI sound. pub fn play_button_click(&mut self) { self.play_instance( diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index a548c7c11..146e5c8f0 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2967,9 +2967,10 @@ impl Game<'_> { b.notify_written(level, ticks, position, level_flags::UPDATE_ALL) }); } - MotionEvent::Fall(horizon_client::landing::FallEvent::Damage { .. }) => { - self.audio - .play("minecraft:entity.player.hurt", Some(feet), 1.0, 1.0); + MotionEvent::Health(horizon_client::health::HealthEvent::DamageBroadcast { + source, + }) => { + self.player_hurt_sound(source, feet); } MotionEvent::Snowflake { position, velocity } => self.snowflake(position, velocity), MotionEvent::EnterBlock { state } => self.trigger_enter_block(state), diff --git a/crates/horizon-client/src/game/combat.rs b/crates/horizon-client/src/game/combat.rs index 73c6b7fc0..14a1133e0 100644 --- a/crates/horizon-client/src/game/combat.rs +++ b/crates/horizon-client/src/game/combat.rs @@ -54,6 +54,20 @@ impl Game<'_> { .push((damage_type.to_owned(), (mob, name))); } } + /// `LivingEntity.handleDamageEvent` for the player: the hurt sound of the + /// source's `DamageEffects` (`Player.getHurtSound`). + pub(super) fn player_hurt_sound( + &mut self, + source: horizon_world::damage_source::DamageTypeId, + at: [f64; 3], + ) { + let Some(d) = self.motion.health.damage.definition(source) else { + return; + }; + let sound = horizon_client::health::hurt_sound(&d.effects); + let pitch = self.audio.hurt_pitch(); + self.audio.play(sound, Some(at), 1.0, pitch); + } /// `CombatTracker.recordDamage` for the motion events of this tick. pub(super) fn record_combat(&mut self, events: &[MotionEvent]) { self.recheck_combat(); diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 7e2d20ecb..9d7bc79d6 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -1352,9 +1352,6 @@ impl Game<'_> { (player.velocity.y / 2.0 + 0.4).min(0.4), player.velocity.z / 2.0 + dz / l * 0.4, ); - let at = [player.position.x, player.position.y, player.position.z]; - self.audio - .play("minecraft:entity.player.hurt", Some(at), 1.0, 1.0); self.hurt_armor(amount); self.thorns(from); } diff --git a/crates/horizon-client/src/health.rs b/crates/horizon-client/src/health.rs index 46b4f4542..29cba4610 100644 --- a/crates/horizon-client/src/health.rs +++ b/crates/horizon-client/src/health.rs @@ -76,6 +76,18 @@ impl HealthResources { .expect("validated landing source") } } +/// `DamageEffects.sound()` by its serialized id: what `Player.getHurtSound` +/// plays for a damage type's `effects` (unknown ids fall back to `hurt`, the codec default). +pub fn hurt_sound(effects: &str) -> &'static str { + match effects { + "drowning" => "minecraft:entity.player.hurt_drown", + "burning" => "minecraft:entity.player.hurt_on_fire", + "poking" => "minecraft:entity.player.hurt_sweet_berry_bush", + "freezing" => "minecraft:entity.player.hurt_freeze", + // HURT and THORNS (both SoundEvents.PLAYER_HURT). + _ => "minecraft:entity.player.hurt", + } +} #[derive(Clone, Copy, Debug)] pub enum HealthEvent { StarvationDamage(f32), @@ -97,6 +109,11 @@ pub enum HealthEvent { amount: f32, }, Phase(&'static str), + /// `ServerLevel.broadcastDamageEvent`: the client's + /// `LivingEntity.handleDamageEvent` plays the source's hurt sound. + DamageBroadcast { + source: DamageTypeId, + }, Criterion { original: f32, amount: f32, @@ -119,6 +136,7 @@ impl HealthEvent { format!("record-damage {:08x}", amount.to_bits()) } Self::Phase(name) => name.into(), + Self::DamageBroadcast { .. } => "broadcast-damage".into(), Self::Criterion { .. } => "hurt-criterion".into(), } } @@ -559,7 +577,10 @@ impl HurtWorld for Environment<'_> { Ok(()) } fn broadcast_damage(&mut self, _: &mut State) -> io::Result<()> { - self.phase("broadcast-damage") + self.event(HealthEvent::DamageBroadcast { + source: self.source.type_id(), + }); + Ok(()) } fn mark_hurt(&mut self, _: &mut State) -> io::Result<()> { *self.sync_velocity = true; @@ -655,3 +676,21 @@ impl ServerPlayerHurtWorld for Environment<'_> { Ok(true) } } + +#[cfg(test)] +mod tests { + #[test] + fn hurt_sounds_follow_damage_effects() { + // DamageEffects: HURT, THORNS, DROWNING, BURNING, POKING, FREEZING. + for (effects, sound) in [ + ("hurt", "minecraft:entity.player.hurt"), + ("thorns", "minecraft:entity.player.hurt"), + ("drowning", "minecraft:entity.player.hurt_drown"), + ("burning", "minecraft:entity.player.hurt_on_fire"), + ("poking", "minecraft:entity.player.hurt_sweet_berry_bush"), + ("freezing", "minecraft:entity.player.hurt_freeze"), + ] { + assert_eq!(super::hurt_sound(effects), sound, "{effects}"); + } + } +}