From ed5427197f78d0118a81a712d79069d210c3cffd Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 22:57:01 +0200 Subject: [PATCH] Show trial spawner and vault visuals LevelEventHandler 3011-3021 (spawn, detect, eject, ominous and item spawner particles and sounds, vault activation and deactivation), the trial spawner's client tick (state particles, the spinning display mob, the ambient sound) and the vault's client tick (idle smoke and flames, the connection particles towards the player, the ambient sound). Part of #223. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 6 + crates/horizon-client/src/game/spawner.rs | 41 ++- .../horizon-client/src/game/trial_spawner.rs | 37 +- .../horizon-client/src/game/trial_visuals.rs | 333 ++++++++++++++++++ crates/horizon-client/src/game/vault.rs | 6 +- crates/horizon-client/src/mobs/spawner.rs | 25 +- 6 files changed, 411 insertions(+), 37 deletions(-) create mode 100644 crates/horizon-client/src/game/trial_visuals.rs diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 8416a624e..4b7c45141 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -122,6 +122,7 @@ mod tnt; mod totem; mod trading; mod trial_spawner; +mod trial_visuals; mod trident; mod tweaks; mod ui_control; @@ -1818,6 +1819,11 @@ impl Game<'_> { pos, data, } => self.composter_fill_event(pos, data == 1), + LevelEvent::LevelEvent { + id: id @ 3011..=3021, + pos, + data, + } => self.trial_level_event(id, [pos.x, pos.y, pos.z], data), LevelEvent::LevelEvent { id, pos, .. } => { let c = Some([ f64::from(pos.x) + 0.5, diff --git a/crates/horizon-client/src/game/spawner.rs b/crates/horizon-client/src/game/spawner.rs index 30b8cc67b..3fcd30e8c 100644 --- a/crates/horizon-client/src/game/spawner.rs +++ b/crates/horizon-client/src/game/spawner.rs @@ -92,9 +92,12 @@ impl Game<'_> { self.client_tick_spawner(key); } let entities = &self.world.block_entities; - self.spawners - .displays - .retain(|k, _| matches!(entities.get(k), Some(BlockEntity::Spawner(_)))); + self.spawners.displays.retain(|k, _| { + matches!( + entities.get(k), + Some(BlockEntity::Spawner(_) | BlockEntity::TrialSpawner(_)) + ) + }); Ok(()) } fn server_tick_spawner(&mut self, key: Pos, spawner: &mut Spawner) -> Result<()> { @@ -323,7 +326,7 @@ impl Game<'_> { } /// `getOrCreateDisplayEntity`: the mob shown in the spawner, made again /// when its type changes. False when there is none. - fn update_spawner_display(&mut self, key: Pos) -> bool { + pub(super) fn update_spawner_display(&mut self, key: Pos) -> bool { let Some(kind) = self.spawner_display_kind(key) else { self.spawners.displays.remove(&key); return false; @@ -344,11 +347,16 @@ impl Game<'_> { } /// The type of the mob a spawner shows: its next data's id, when the game /// knows the mob. + /// A trial spawner shows one only in the states with a spinning mob. fn spawner_display_kind(&self, key: Pos) -> Option { - let Some(BlockEntity::Spawner(s)) = self.world.block_entities.get(&key) else { - return None; + let id = match self.world.block_entities.get(&key)? { + BlockEntity::Spawner(s) => s.next_data()?.id()?, + BlockEntity::TrialSpawner(s) => { + self.trial_spawner_block(key)?.0.spinning_mob_speed()?; + s.next_spawn_data.as_ref()?.id()? + } + _ => return None, }; - let id = s.next_data()?.id()?; self.res.mob_types.contains_key(&id).then_some(id) } /// `SpawnEggItem.useOn` on a spawner: the egg sets its mob. None when the @@ -383,8 +391,8 @@ impl Game<'_> { self.post_block_game_event("minecraft:block_change", pos, Some(Who::Player), None); Some(true) } - /// `SpawnerRenderer`: the next mob of every spawner near the camera, - /// small and turning. + /// `SpawnerRenderer` and `TrialSpawnerRenderer`: the next mob of every + /// spawner near the camera, small and turning. pub(super) fn spawner_geometry( &self, camera: glam::DVec3, @@ -398,10 +406,19 @@ impl Game<'_> { if at.distance_squared(camera) >= 64.0 * 64.0 { continue; } - let Some(BlockEntity::Spawner(s)) = self.world.block_entities.get(key) else { - continue; + let (spin, scale) = match self.world.block_entities.get(key) { + Some(BlockEntity::Spawner(s)) => { + s.display_pose(alpha as f32, mob.width(), mob.height()) + } + Some(BlockEntity::TrialSpawner(s)) => horizon_client::mobs::spawner::display_pose( + s.o_spin, + s.spin, + alpha as f32, + mob.width(), + mob.height(), + ), + _ => continue, }; - let (spin, scale) = s.display_pose(alpha as f32, mob.width(), mob.height()); let origin = glam::Mat4::from_translation( (at - camera).as_vec3() + glam::Vec3::new(0.5, 0.4, 0.5), ) * glam::Mat4::from_rotation_y(spin.to_radians()) diff --git a/crates/horizon-client/src/game/trial_spawner.rs b/crates/horizon-client/src/game/trial_spawner.rs index 0d0d726f5..243a0cafe 100644 --- a/crates/horizon-client/src/game/trial_spawner.rs +++ b/crates/horizon-client/src/game/trial_spawner.rs @@ -64,33 +64,46 @@ impl Game<'_> { .filter(|(_, e)| matches!(e, BlockEntity::TrialSpawner(_))) .map(|(k, _)| *k) .collect(); - for key in keys { - let state = self.world.block(key.0, key.1, key.2); - let Some(block) = self.res.assets.registry.state(state).cloned() else { + for &key in &keys { + let Some((current, ominous)) = self.trial_spawner_block(key) else { continue; }; - if block.name != "minecraft:trial_spawner" { - continue; - } let Some(BlockEntity::TrialSpawner(mut spawner)) = self.world.block_entities.remove(&key) else { continue; }; - let current = block - .properties - .get("trial_spawner_state") - .and_then(|s| TrialState::from_name(s)) - .unwrap_or_default(); - let ominous = block.properties.get("ominous").is_some_and(|v| v == "true"); let result = self.server_tick_trial_spawner(key, &mut spawner, current, ominous); self.world .block_entities .insert(key, BlockEntity::TrialSpawner(spawner)); result?; } + for key in keys { + self.client_tick_trial_spawner(key); + } Ok(()) } + /// The trial spawner block at `key`: its state and ominous flag. + pub(super) fn trial_spawner_block(&self, key: Pos) -> Option<(TrialState, bool)> { + let block = self + .res + .assets + .registry + .state(self.world.block(key.0, key.1, key.2))?; + if block.name != "minecraft:trial_spawner" { + return None; + } + let state = block + .properties + .get("trial_spawner_state") + .and_then(|s| TrialState::from_name(s)) + .unwrap_or_default(); + Some(( + state, + block.properties.get("ominous").is_some_and(|v| v == "true"), + )) + } fn server_tick_trial_spawner( &mut self, key: Pos, diff --git a/crates/horizon-client/src/game/trial_visuals.rs b/crates/horizon-client/src/game/trial_visuals.rs new file mode 100644 index 000000000..062c61521 --- /dev/null +++ b/crates/horizon-client/src/game/trial_visuals.rs @@ -0,0 +1,333 @@ +//! The client side of trial spawners and vaults: `LevelEventHandler` 3011 to +//! 3021, `TrialSpawner.tickClient` (the state's particles, the spinning mob, +//! the ambient sound) and `VaultBlockEntity.Client.tick` (idle smoke and +//! flames, the connection particles towards the player, the ambient sound). +//! Every draw comes from the client level's random. +use super::Game; +use horizon_client::{ + containers::BlockEntity, + mobs::trial_spawner::TrialState, + particles::{ParticleOptions, ParticleType as T}, +}; +use horizon_core::random::{LegacyRandom, RandomSource}; + +type Pos = (i32, i32, i32); + +/// `Mth.nextDouble`. +fn next_double(random: &mut LegacyRandom, min: f64, max: f64) -> f64 { + if min >= max { + min + } else { + random.next_double() * (max - min) + min + } +} +/// `Vec3.offsetRandom`. +fn offset_random(v: [f64; 3], random: &mut LegacyRandom, amount: f32) -> [f64; 3] { + v.map(|c| c + f64::from((random.next_float() - 0.5) * amount)) +} +/// The pitch of `playLocalSound`'s block event sounds. +fn event_pitch(random: &mut LegacyRandom) -> f32 { + (random.next_float() - random.next_float()) * 0.2 + 1.0 +} +fn center(pos: [i32; 3]) -> [f64; 3] { + pos.map(|v| f64::from(v) + 0.5) +} +fn flame(ominous: bool) -> T { + if ominous { + T::SoulFireFlame + } else { + T::SmallFlame + } +} + +impl Game<'_> { + fn particle(&mut self, ty: T, p: [f64; 3], v: [f64; 3]) { + self.particles.add(&ParticleOptions::simple(ty), p, v); + } + fn local_sound(&mut self, name: &str, pos: [i32; 3], volume: f32, pitch: f32) { + self.audio.play( + &format!("minecraft:{name}"), + Some(center(pos)), + volume, + pitch, + ); + } + /// `LevelEventHandler.levelEvent` 3011 to 3021. + pub(super) fn trial_level_event(&mut self, id: i32, pos: [i32; 3], data: i32) { + // TrialSpawner.FlameParticle.decode. + let spawn_flame = if data == 1 { + T::SoulFireFlame + } else { + T::Flame + }; + let sound = |s: &mut Self, name: &str, volume: f32| { + let pitch = event_pitch(s.particles.level_random()); + s.local_sound(name, pos, volume, pitch); + }; + match id { + 3011 => self.trial_spawn_particles(pos, spawn_flame), + 3012 => { + sound(self, "block.trial_spawner.spawn_mob", 1.0); + self.trial_spawn_particles(pos, spawn_flame); + } + 3013 | 3019 => { + sound(self, "block.trial_spawner.detect_player", 1.0); + let ty = if id == 3013 { + T::TrialSpawnerDetection + } else { + T::TrialSpawnerDetectionOminous + }; + self.detect_player_particles(pos, data, ty); + } + 3014 => { + sound(self, "block.trial_spawner.eject_item", 1.0); + self.eject_item_particles(pos); + } + 3015 => { + if matches!( + self.world.block_entities.get(&(pos[0], pos[1], pos[2])), + Some(BlockEntity::Vault(_)) + ) { + self.vault_activation_particles(pos, data != 0); + sound(self, "block.vault.activate", 1.0); + } + } + 3016 => { + // emitDeactivationParticles. + let ty = flame(data != 0); + for _ in 0..20 { + let random = self.particles.level_random(); + let p = [0.4, 0.4, 0.4].map(|lo| next_double(random, lo, 0.6)); + let v = [0; 3].map(|_| random.next_gaussian() * 0.02); + let at = std::array::from_fn(|i| f64::from(pos[i]) + p[i]); + self.particle(ty, at, v); + } + sound(self, "block.vault.deactivate", 1.0); + } + 3017 => self.eject_item_particles(pos), + 3018 => { + for _ in 0..10 { + let random = self.particles.level_random(); + let v = [0; 3].map(|_| random.next_gaussian() * 0.02); + let at = pos.map(|c| f64::from(c) + random.next_double()); + self.particle(T::Poof, at, v); + } + sound(self, "block.cobweb.place", 1.0); + } + 3020 => { + sound( + self, + "block.trial_spawner.ominous_activate", + if data == 0 { 0.3 } else { 1.0 }, + ); + self.detect_player_particles(pos, 0, T::TrialSpawnerDetectionOminous); + // addBecomeOminousParticles. + for _ in 0..20 { + let random = self.particles.level_random(); + let at = pos.map(|c| f64::from(c) + 0.5 + (random.next_double() - 0.5) * 2.0); + let v = [0; 3].map(|_| random.next_gaussian() * 0.02); + self.particle(T::TrialOmen, at, v); + self.particle(T::SoulFireFlame, at, v); + } + } + 3021 => { + sound(self, "block.trial_spawner.spawn_item", 1.0); + self.trial_spawn_particles(pos, spawn_flame); + } + _ => {} + } + } + /// `TrialSpawner.addSpawnParticles`. + fn trial_spawn_particles(&mut self, pos: [i32; 3], ty: T) { + for _ in 0..20 { + let random = self.particles.level_random(); + let at = pos.map(|c| f64::from(c) + 0.5 + (random.next_double() - 0.5) * 2.0); + self.particle(T::Smoke, at, [0.0; 3]); + self.particle(ty, at, [0.0; 3]); + } + } + /// `TrialSpawner.addDetectPlayerParticles`. + fn detect_player_particles(&mut self, pos: [i32; 3], data: i32, ty: T) { + for _ in 0..30 + data.min(10) * 5 { + let random = self.particles.level_random(); + let x = f64::from(2.0 * random.next_float() - 1.0) * 0.65; + let z = f64::from(2.0 * random.next_float() - 1.0) * 0.65; + let y = f64::from(pos[1]) + 0.1 + f64::from(random.next_float()) * 0.8; + let at = [f64::from(pos[0]) + 0.5 + x, y, f64::from(pos[2]) + 0.5 + z]; + self.particle(ty, at, [0.0; 3]); + } + } + /// `TrialSpawner.addEjectItemParticles`. + fn eject_item_particles(&mut self, pos: [i32; 3]) { + for _ in 0..20 { + let random = self.particles.level_random(); + let at = pos.map(|c| f64::from(c) + 0.4 + random.next_double() * 0.2); + let v = [0; 3].map(|_| random.next_gaussian() * 0.02); + self.particle(T::SmallFlame, at, [v[0], v[1], v[2] * 0.25]); + self.particle(T::Smoke, at, v); + } + } + /// `TrialSpawner.tickClient`: the state's particles, the spin of the + /// mob shown inside and the ambient sound. + pub(super) fn client_tick_trial_spawner(&mut self, key: Pos) { + let Some((state, ominous)) = self.trial_spawner_block(key) else { + return; + }; + let pos = [key.0, key.1, key.2]; + let time = self.world.game_time; + let c = center(pos); + let random = self.particles.level_random(); + // TrialSpawnerState.ParticleEmission. + let mut emitted: Vec<(T, [f64; 3])> = Vec::new(); + match state { + TrialState::WaitingForPlayers + | TrialState::WaitingForRewardEjection + | TrialState::EjectingReward => { + if random.next_int_bound(2) == Ok(0) { + emitted.push((flame(ominous), offset_random(c, random, 0.9))); + } + } + TrialState::Active => { + let at = offset_random(c, random, 1.0); + let ty = if ominous { T::SoulFireFlame } else { T::Flame }; + emitted.extend([(T::Smoke, at), (ty, at)]); + } + TrialState::Cooldown => { + let at = offset_random(c, random, 0.9); + if random.next_int_bound(3) == Ok(0) { + emitted.push((T::Smoke, at)); + } + if time % 20 == 0 { + let top = [c[0], c[1] + 0.5, c[2]]; + let count = random.next_int_bound(4).unwrap_or(0) + 20; + emitted.extend((0..count).map(|_| (T::Smoke, top))); + } + } + TrialState::Inactive => {} + } + let ambient = state.capable_of_spawning() && random.next_float() <= 0.02; + let ambient = + ambient.then(|| (random.next_float() * 0.25 + 0.75, random.next_float() + 0.5)); + for (ty, at) in emitted { + self.particle(ty, at, [0.0; 3]); + } + if let Some(BlockEntity::TrialSpawner(s)) = self.world.block_entities.get_mut(&key) + && let Some(speed) = state.spinning_mob_speed() + { + let delay = (s.next_mob_spawns_at - time).max(0) as f64; + s.o_spin = s.spin; + s.spin = (s.spin + speed / (delay + 200.0)) % 360.0; + } + if let Some((volume, pitch)) = ambient { + let name = if ominous { + "block.trial_spawner.ambient_ominous" + } else { + "block.trial_spawner.ambient" + }; + self.local_sound(name, pos, volume, pitch); + } + self.update_spawner_display(key); + } + /// `VaultBlockEntity.Client.tick`. + pub(super) fn client_tick_vault(&mut self, key: Pos) { + let Some((mut vault, _, ominous)) = self.take_vault(key) else { + return; + }; + vault.update_display_item_spin(); + let shows = vault.display_item.is_some(); + self.world + .block_entities + .insert(key, BlockEntity::Vault(vault)); + let pos = [key.0, key.1, key.2]; + if self.world.game_time % 20 == 0 { + self.vault_connection_particles(pos); + } + // emitIdleParticles. + let random = self.particles.level_random(); + if random.next_float() <= 0.5 { + let at = cage_point(pos, random); + self.particle(T::Smoke, at, [0.0; 3]); + if shows { + self.particle(flame(ominous), at, [0.0; 3]); + } + } + // playIdleSounds. + if shows { + let random = self.particles.level_random(); + if random.next_float() <= 0.02 { + let volume = random.next_float() * 0.25 + 0.75; + let pitch = random.next_float() + 0.5; + self.local_sound("block.vault.ambient", pos, volume, pitch); + } + } + } + /// `VaultBlockEntity.Client.emitActivationParticles`. + fn vault_activation_particles(&mut self, pos: [i32; 3], ominous: bool) { + self.vault_connection_particles(pos); + for _ in 0..20 { + let at = cage_point(pos, self.particles.level_random()); + self.particle(T::Smoke, at, [0.0; 3]); + self.particle(flame(ominous), at, [0.0; 3]); + } + } + /// `emitConnectionParticlesForNearbyPlayers`: from the keyhole towards + /// each connected player within the particles' range. + fn vault_connection_particles(&mut self, pos: [i32; 3]) { + let key = (pos[0], pos[1], pos[2]); + let Some(BlockEntity::Vault(vault)) = self.world.block_entities.get(&key) else { + return; + }; + let me = self.level.player_uuid.unwrap_or_default(); + let range = vault.connected_particles_range; + let (Some(p), true) = (self.player.as_ref(), vault.connected_players.contains(&me)) else { + return; + }; + let block = [p.position.x, p.position.y, p.position.z].map(|v| v.floor() as i32); + let d = [0, 1, 2].map(|i| f64::from(block[i] - pos[i])); + if d[0] * d[0] + d[1] * d[1] + d[2] * d[2] > range * range { + return; + } + let target = [ + p.position.x, + p.position.y + f64::from(p.dimensions.height / 2.0), + p.position.z, + ]; + let facing = self + .res + .assets + .registry + .state(self.world.block(pos[0], pos[1], pos[2])) + .and_then(|b| b.properties.get("facing").cloned()); + let step = match facing.as_deref() { + Some("south") => [0.0, 1.0], + Some("west") => [-1.0, 0.0], + Some("east") => [1.0, 0.0], + _ => [0.0, -1.0], + }; + // keyholePos: the bottom centre, 1.75 up and half a block out the front. + let keyhole = [ + f64::from(pos[0]) + 0.5 + step[0] * 0.5, + f64::from(pos[1]) + 1.75, + f64::from(pos[2]) + 0.5 + step[1] * 0.5, + ]; + let direction: [f64; 3] = std::array::from_fn(|i| target[i] - keyhole[i]); + let random = self.particles.level_random(); + let count = random.next_int_bound(4).unwrap_or(0) + 2; + for _ in 0..count { + let v = offset_random(direction, self.particles.level_random(), 1.0); + self.particle(T::VaultConnection, keyhole, v); + } + } +} + +/// `randomPosInsideCage`. +fn cage_point(pos: [i32; 3], random: &mut LegacyRandom) -> [f64; 3] { + let x = next_double(random, 0.1, 0.9); + let y = next_double(random, 0.25, 0.75); + let z = next_double(random, 0.1, 0.9); + [ + f64::from(pos[0]) + x, + f64::from(pos[1]) + y, + f64::from(pos[2]) + z, + ] +} diff --git a/crates/horizon-client/src/game/vault.rs b/crates/horizon-client/src/game/vault.rs index 0ed008add..35330ab9b 100644 --- a/crates/horizon-client/src/game/vault.rs +++ b/crates/horizon-client/src/game/vault.rs @@ -44,7 +44,7 @@ impl Game<'_> { } } /// The vault at `key` with its block's state and ominous flag. - fn take_vault(&mut self, key: Pos) -> Option<(Box, VaultState, bool)> { + pub(super) fn take_vault(&mut self, key: Pos) -> Option<(Box, VaultState, bool)> { let state = self.world.block(key.0, key.1, key.2); let block = self.res.assets.registry.state(state)?; if block.name != "minecraft:vault" { @@ -116,7 +116,7 @@ impl Game<'_> { .insert(key, BlockEntity::Vault(vault)); Some(self.vault_events(key, events).map(|()| out)) } - /// Every vault's server tick, then its client tick's spin. + /// Every vault's server tick, then its client tick. pub(super) fn tick_vaults(&mut self) -> Result<()> { let keys: Vec = self .world @@ -129,11 +129,11 @@ impl Game<'_> { let pos = [key.0, key.1, key.2]; let ticked = self.with_vault(key, |v, l, random, state, ominous, roll, events| { v.server_tick(l, random, pos, state, ominous, roll, events); - v.update_display_item_spin(); }); if let Some(result) = ticked { result?; } + self.client_tick_vault(key); } Ok(()) } diff --git a/crates/horizon-client/src/mobs/spawner.rs b/crates/horizon-client/src/mobs/spawner.rs index 5ed23b44a..78b091b7b 100644 --- a/crates/horizon-client/src/mobs/spawner.rs +++ b/crates/horizon-client/src/mobs/spawner.rs @@ -640,20 +640,25 @@ impl Spawner { pub fn client_loaded(&mut self) { self.client_delay = self.spawn_delay; } - /// `TrialSpawnerRenderer.extractSpawnerData`: the spin in degrees and - /// the scale of a display mob of the given size. + /// The display mob's spin in degrees and scale ([`display_pose`]). pub fn display_pose(&self, alpha: f32, width: f32, height: f32) -> (f32, f32) { - let spin = (self.o_spin + f64::from(alpha) * (self.spin - self.o_spin)) as f32 * 10.0; - let longest = width.max(height); - let scale = if longest > 1.0 { - 0.53125 / longest - } else { - 0.53125 - }; - (spin, scale) + display_pose(self.o_spin, self.spin, alpha, width, height) } } +/// `TrialSpawnerRenderer.extractSpawnerData`: the spin in degrees and the +/// scale of a display mob of the given size, between the last two spins. +pub fn display_pose(o_spin: f64, spin: f64, alpha: f32, width: f32, height: f32) -> (f32, f32) { + let spin = (o_spin + f64::from(alpha) * (spin - o_spin)) as f32 * 10.0; + let longest = width.max(height); + let scale = if longest > 1.0 { + 0.53125 / longest + } else { + 0.53125 + }; + (spin, scale) +} + /// `WeightedList.getRandom`: one draw over the total weight, none when the /// list weighs nothing. pub fn weighted_index(