Merge pull request #2688 from kacpherek/fix/mob-looping-sounds
🤖 Agent: Merged by Ludovic's AI agent (Claude, contrib-gate-19) after the contribution gate. Checked against vanilla 26.3 and proven in union/cg19-r2 and union/cg19-r3. After this batch it was merged with main again and proven in union/cg19-r4 (clippy, 1726 tests, 121/121 harnesses). Thanks @kacpherek.
This commit is contained in:
@@ -107,6 +107,7 @@ mod maps;
|
||||
mod minecarts;
|
||||
mod mob_blocks;
|
||||
mod mob_gear;
|
||||
mod mob_sounds;
|
||||
mod mod_browser;
|
||||
#[cfg(feature = "mods")]
|
||||
mod mod_browser_recipes;
|
||||
@@ -1525,6 +1526,10 @@ struct Game<'a> {
|
||||
cart_sounds: HashSet<u32>,
|
||||
/// The minecart the riding sounds were started for (`LocalPlayer.startRiding`).
|
||||
riding_cart_sounds: Option<u32>,
|
||||
/// Bees whose buzz is playing, and whether it is the aggressive one.
|
||||
bee_sounds: HashMap<u32, bool>,
|
||||
/// Guardians whose attack hum is playing.
|
||||
guardian_sounds: HashSet<u32>,
|
||||
dropped: Vec<ItemEntity>,
|
||||
next_entity: u32,
|
||||
particles: horizon_client::particles::ParticleEngine,
|
||||
@@ -13596,6 +13601,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,
|
||||
|
||||
@@ -1853,6 +1853,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
|
||||
|
||||
@@ -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<MobSound> {
|
||||
let game_time = self.world.game_time;
|
||||
let mobs: Vec<MobSound> = 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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -2328,6 +2328,10 @@ impl Game<'_> {
|
||||
if p.event == 69 {
|
||||
self.remote_ravager_roar(p.id);
|
||||
}
|
||||
// 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
|
||||
|
||||
@@ -965,7 +965,8 @@ pub enum MobEvent {
|
||||
mob: u32,
|
||||
at: [f64; 3],
|
||||
},
|
||||
/// `Level.broadcastEntityEvent` for a mob: guests run `sync::entity_event`.
|
||||
/// `Level.broadcastEntityEvent` for a mob: guests run its
|
||||
/// `handleEntityEvent`.
|
||||
EntityEvent {
|
||||
mob: u32,
|
||||
event: i8,
|
||||
|
||||
@@ -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.
|
||||
@@ -476,3 +476,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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<u32>,
|
||||
/// The minecarts in the level (a missing one has been removed).
|
||||
pub carts: Vec<CartSound>,
|
||||
/// The bees and guardians in the level (a missing one has been removed).
|
||||
pub mobs: Vec<MobSound>,
|
||||
/// `!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);
|
||||
|
||||
Reference in New Issue
Block a user