From e5e1b74fd89abf3e8b8263c3bf42039a5fec0ed8 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Tue, 27 Oct 2026 06:44:37 +0100 Subject: [PATCH] Draw the enderman portal trail, phantom wing dust and bee nectar drips --- crates/horizon-client/src/game.rs | 5 ++ .../horizon-client/src/game/client_ambient.rs | 2 + .../src/game/level_particles.rs | 9 ++- crates/horizon-client/src/mobs/ambient.rs | 68 ++++++++++++++++++- crates/horizon-client/src/mobs/enderman.rs | 16 +++++ crates/horizon-client/src/particles.rs | 37 ++++++++++ 6 files changed, 134 insertions(+), 3 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 7699e83d7..527bd445a 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -3014,6 +3014,11 @@ impl Game<'_> { pos, data, } => self.consume_teleport_particles([pos.x, pos.y, pos.z], data), + LevelEvent::LevelEvent { + id: 2018, + pos, + data, + } => self.enderman_teleport_particles([pos.x, pos.y, pos.z], data), LevelEvent::LevelEvent { id: 2003, pos, .. } => { self.eye_break_particles([pos.x, pos.y, pos.z]); } diff --git a/crates/horizon-client/src/game/client_ambient.rs b/crates/horizon-client/src/game/client_ambient.rs index 1cf59733d..5efc86504 100644 --- a/crates/horizon-client/src/game/client_ambient.rs +++ b/crates/horizon-client/src/game/client_ambient.rs @@ -17,8 +17,10 @@ impl Game<'_> { return; } let mut events = Vec::new(); + let random = self.particles.level_random(); for m in self.mobs.iter_mut().filter(|m| !m.removed) { ambient::client_ai_step(m, &mut events); + ambient::client_particles(m, random, &mut events); } for event in events { match event { diff --git a/crates/horizon-client/src/game/level_particles.rs b/crates/horizon-client/src/game/level_particles.rs index 9fa53eb25..98f62bfc3 100644 --- a/crates/horizon-client/src/game/level_particles.rs +++ b/crates/horizon-client/src/game/level_particles.rs @@ -1,7 +1,7 @@ //! Level events whose client handler (`LevelEventHandler`) spawns particles //! on a block: bone meal's growth (1505), dragon egg teleportation (2015), a -//! consumable's teleport (2017), electric sparks (3002), waxing (3003), wax -//! off (3004) and scraping (3005). +//! consumable's teleport (2017), an enderman's (2018), electric sparks (3002), +//! waxing (3003), wax off (3004) and scraping (3005). use super::Game; use horizon_client::items::ItemStack; use horizon_client::particles::{self, ParticleOptions, ParticleType}; @@ -18,6 +18,11 @@ impl Game<'_> { self.particles .block_teleportation(pos, data, [127, 127, 127], self.settings.particles); } + /// Event 2018: an enderman's portal trail. + pub(super) fn enderman_teleport_particles(&mut self, pos: [i32; 3], data: i32) { + self.particles + .enderman_teleportation(pos, data, self.settings.particles); + } /// `LevelEventHandler.levelEvent` 3002 to 3005, drawing from the client /// level's random. pub(super) fn block_particle_event(&mut self, id: i32, pos: BlockPos, data: i32) { diff --git a/crates/horizon-client/src/mobs/ambient.rs b/crates/horizon-client/src/mobs/ambient.rs index cb4146e08..94589ffab 100644 --- a/crates/horizon-client/src/mobs/ambient.rs +++ b/crates/horizon-client/src/mobs/ambient.rs @@ -2,8 +2,10 @@ //! `level().isClientSide()` branches of `aiStep`: nothing of it reaches the //! server or goes over the network, so the window's game runs it, whether it //! simulates the world (in-frame) or follows a server. -use super::{Mob, MobEvent, Species}; +use super::{Mob, MobEvent, Species, bee}; +use horizon_core::geometry::trig::Mth; use horizon_core::random::RandomSource; +use std::f32::consts::PI; /// `Enderman.aiStep`, `Endermite.aiStep` and `Blaze.aiStep` on a client: /// two portal particles a tick around endermen and endermites; two large @@ -53,3 +55,67 @@ pub fn client_ai_step(m: &mut Mob, out: &mut Vec) { out.push(MobEvent::ParticleAt { kind, at, velocity }); } } + +/// `Phantom.tick` and `Bee.tick` on a client, which draw from the client +/// level's random (`random`) and never from the mob's own, so a window that +/// simulates the world keeps the mob's draws as the server has them: a +/// phantom's wings leave mycelium dust, a bee carrying nectar now and then +/// drips it. +pub fn client_particles(m: &Mob, random: &mut impl RandomSource, out: &mut Vec) { + match m.species() { + Species::Phantom => { + // The wings flap every phantom in a different phase + // (`getUniqueFlapTickOffset`). + let flap = ((m.id.wrapping_mul(3).wrapping_add(m.age)) as i32 as f32 + * 7.448451 + * (PI / 180.0) + + PI) + .mcos(); + let width = m.width() * 1.48; + let yaw = m.yaw * (PI / 180.0); + let (c, s) = (yaw.mcos() * width, yaw.msin() * width); + let h = (0.3 + flap * 0.45) * m.height() * 2.5; + let p = m.position; + for (dx, dz) in [(c, s), (-c, -s)] { + out.push(MobEvent::ParticleAt { + kind: "mycelium", + at: [p.x + f64::from(dx), p.y + f64::from(h), p.z + f64::from(dz)], + velocity: [0.0; 3], + }); + } + } + Species::Bee => { + if !bee::has_nectar(m) + || !bee::get(m).is_some_and(|b| b.crops_grown < 10) + || random.next_float() >= 0.05 + { + return; + } + // `for (i = 0; i < nextInt(2) + 1; i++)` rolls the bound each time. + let mut i = 0; + while i < random.next_int_bound(2).unwrap_or(0) + 1 { + // Mth.lerp(level.random.nextDouble(), x - 0.3F, x + 0.3F). + let spread = f64::from(0.3_f32); + let lerp = |delta: f64, from: f64, to: f64| from + delta * (to - from); + let x = lerp( + random.next_double(), + m.position.x - spread, + m.position.x + spread, + ); + let z = lerp( + random.next_double(), + m.position.z - spread, + m.position.z + spread, + ); + out.push(MobEvent::ParticleAt { + kind: "falling_nectar", + // getY(0.5). + at: [x, m.position.y + f64::from(m.height()) * 0.5, z], + velocity: [0.0; 3], + }); + i += 1; + } + } + _ => {} + } +} diff --git a/crates/horizon-client/src/mobs/enderman.rs b/crates/horizon-client/src/mobs/enderman.rs index 3ccd963a2..b05b15b94 100644 --- a/crates/horizon-client/src/mobs/enderman.rs +++ b/crates/horizon-client/src/mobs/enderman.rs @@ -97,10 +97,26 @@ pub fn teleport_to( return false; } let old = m.center(); + let old_block = [ + m.position.x.floor() as i32, + m.position.y.floor() as i32, + m.position.z.floor() as i32, + ]; m.position = Vec3::new(x, ny, z); m.previous = m.position; m.velocity = Vec3::ZERO; m.ai.nav.stop(); + if !m.silent { + // Level event 2018: the portal trail, from the old block to the new + // one, packed as `BlockUtil.clampedPackDifferenceInPosition` does. + let new_block = [x.floor() as i32, ny.floor() as i32, z.floor() as i32]; + let packed = |k: usize| ((new_block[k] - old_block[k]).clamp(-127, 127) + 127) & 255; + events.push(MobEvent::LevelEvent { + id: 2018, + pos: old_block, + data: packed(0) << 16 | packed(1) << 8 | packed(2), + }); + } events.push(MobEvent::Sound("entity.enderman.teleport", old, 1.0, 1.0)); events.push(MobEvent::Sound( "entity.enderman.teleport", diff --git a/crates/horizon-client/src/particles.rs b/crates/horizon-client/src/particles.rs index 5efc83621..cd655e170 100644 --- a/crates/horizon-client/src/particles.rs +++ b/crates/horizon-client/src/particles.rs @@ -1175,6 +1175,43 @@ impl ParticleEngine { } } } + /// `LevelEventHandler.levelEvent` 2018: an enderman's portal trail from + /// where it was (`from`) to where it went, which `data` packs within + /// (127, 127, 127). 128 portal particles along the way, spread about the + /// width and height of an enderman. + pub fn enderman_teleportation(&mut self, from: [i32; 3], data: i32, particle_level: u8) { + // EntityTypes.ENDERMAN's dimensions. + const WIDTH: f64 = 0.6_f32 as f64; + const HEIGHT: f64 = 2.9_f32 as f64; + let to = [ + from[0].wrapping_add(((data >> 16) & 255) - 127), + from[1].wrapping_add(((data >> 8) & 255) - 127), + from[2].wrapping_add((data & 255) - 127), + ] + .map(f64::from); + let from = from.map(f64::from); + let options = ParticleOptions::simple(ParticleType::Portal); + for _ in 0..128 { + let r = &mut self.level_random; + let t = r.next_double(); + let velocity = std::array::from_fn(|_| f64::from((r.next_float() - 0.5) * 0.2)); + // Mth.lerp(t, to, from). + let position = [ + to[0] + t * (from[0] - to[0]) + (r.next_double() - 0.5) * WIDTH * 2.0, + to[1] + t * (from[1] - to[1]) + r.next_double() * HEIGHT, + to[2] + t * (from[2] - to[2]) + (r.next_double() - 0.5) * WIDTH * 2.0, + ]; + // The option roll shares the level random and precedes the distance check. + let limited = match particle_level { + 1 => r.next_int_bound(3).unwrap_or(0) == 0, + 2 => true, + _ => false, + }; + if !limited { + self.add(&options, position, velocity); + } + } + } /// `ClientPacketListener.handleExplosion` + `ClientExplosionTracker`: the /// explosion particle (the emitter unless `ServerExplosion.isSmall`) and up /// to 512 poof/smoke particles thrown from air inside the radius.