From fc06585938b4c0a4386e3c43b7ff8352158e35df Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Tue, 27 Oct 2026 06:33:43 +0100 Subject: [PATCH 1/2] Send mob particles to clients the way vanilla does --- crates/horizon-client/src/game.rs | 1 + crates/horizon-client/src/game/creatures.rs | 17 +++++ .../horizon-client/src/game/lan_particles.rs | 34 ++++++++++ crates/horizon-client/src/game/remote.rs | 17 +++++ .../src/game/remote_mob_particles.rs | 50 +++++++++++++++ crates/horizon-client/src/mobs.rs | 17 +++++ crates/horizon-client/src/mobs/ai.rs | 10 +-- crates/horizon-client/src/mobs/cube.rs | 62 +++++++++++-------- crates/horizon-client/src/mobs/illusioner.rs | 57 ++++++++++++----- crates/horizon-client/src/mobs/nautilus.rs | 19 ++++-- crates/horizon-client/src/mobs/ravager.rs | 9 ++- crates/horizon-client/src/mobs/undead.rs | 21 ++++--- crates/horizon-client/src/mobs/wither.rs | 20 ++++++ 13 files changed, 277 insertions(+), 57 deletions(-) create mode 100644 crates/horizon-client/src/game/remote_mob_particles.rs diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 7699e83d7..b371d1a50 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -143,6 +143,7 @@ mod recipe_book; mod remote; mod remote_chunks; mod remote_mob_data; +mod remote_mob_particles; mod remote_mob_use; mod remote_objects; mod remote_projectiles; diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 2bc40a7af..02154e713 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -933,6 +933,23 @@ 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::LevelParticles { + kind, + at, + spread, + speed, + count, + } => { + if let Some(ty) = horizon_client::particles::ParticleType::from_name(kind) { + let options = horizon_client::particles::ParticleOptions::simple(ty); + self.particles + .send_particles(&options, at, spread, speed, count); + self.lan_level_particles(&options, at, spread, speed, count); + } + } MobEvent::Sound(name, at, volume, pitch) | MobEvent::LocalSound(name, at, volume, pitch) => { self.audio diff --git a/crates/horizon-client/src/game/lan_particles.rs b/crates/horizon-client/src/game/lan_particles.rs index 648cef962..909ef8c36 100644 --- a/crates/horizon-client/src/game/lan_particles.rs +++ b/crates/horizon-client/src/game/lan_particles.rs @@ -82,6 +82,40 @@ impl Game<'_> { } } + /// `ServerLevel.sendParticles` of a mob's particles: a packet to each + /// player within 32 blocks. The window draws its own copy. + pub(super) fn lan_level_particles( + &self, + options: &ParticleOptions, + at: [f64; 3], + spread: [f64; 3], + speed: f64, + count: u32, + ) { + let (Some(host), Some(particle)) = (&self.lan, self.network_particle(options, None)) else { + return; + }; + let packet = C::LevelParticles(play::LevelParticles { + particle, + override_limiter: false, + always_show: false, + pos: at, + dist: spread.map(|d| d as f32), + max_speed: [speed as f32; 3], + count: count as i32, + randomization: 0, + }); + for g in host.hub.guests.iter().filter(|g| host.hub.here(g)) { + // ServerLevel compares the player's block centre, strictly within 32. + let distance: f64 = (0..3) + .map(|k| (g.position[k].floor() + 0.5 - at[k]).powi(2)) + .sum(); + if distance < 32.0 * 32.0 { + g.send(&packet); + } + } + } + /// `LivingEntity.checkFallDamage`'s `ServerLevel.sendParticles` of the /// landing block's crumbs: drawn in this window, when it has one, and /// sent to each player within 32 blocks. `host` is the hub of a guest's diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 77e143f9a..7daffdc44 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); } + // Ravager.handleEntityEvent 69: the roar's puff and push. + if p.event == 69 { + self.remote_ravager_roar(p.id); + } 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 @@ -3537,6 +3541,7 @@ impl Game<'_> { let head_lerps = &mut r.head_lerps; let players = &r.level_players; let mut using = Vec::new(); + let mut mob_fx = Vec::new(); for m in &mut self.mobs { let player = players.contains_key(&(m.id as i32)); if !running && !player { @@ -3576,6 +3581,17 @@ impl Game<'_> { if m.species() == horizon_client::mobs::Species::PolarBear { horizon_client::mobs::polar_bear::client_tick(m); } + // The particles these mobs' own ticks draw on the client side. + { + use horizon_client::mobs::{Species as S, cube, illusioner, nautilus, wither}; + match m.species() { + S::Slime | S::MagmaCube | S::SulfurCube => cube::client_tick(m, &mut mob_fx), + S::Nautilus | S::ZombieNautilus => nautilus::client_tick(m, &mut mob_fx), + S::Illusioner => illusioner::client_tick(m, &mut mob_fx), + S::Wither => wither::client_tick(m, &mut mob_fx), + _ => {} + } + } if m.hurt_time > 0 { m.hurt_time -= 1; } @@ -3672,6 +3688,7 @@ impl Game<'_> { d.tick(world, facts, collision, false, &self.res.motion().fluids); } head_lerps.retain(|_, (_, steps)| *steps > 0); + let _ = self.handle_mob_events(mob_fx); for (item, ticks, at, eye, pitch, yaw) in using { self.remote_player_eating(&item, ticks, at, eye, pitch, yaw); } diff --git a/crates/horizon-client/src/game/remote_mob_particles.rs b/crates/horizon-client/src/game/remote_mob_particles.rs new file mode 100644 index 000000000..e3daf284d --- /dev/null +++ b/crates/horizon-client/src/game/remote_mob_particles.rs @@ -0,0 +1,50 @@ +//! Particles a client of a server draws for a mob's entity event, which the +//! server does not send as a particle packet. +use super::Game; +use horizon_client::{ + mobs::Species, + particles::{ParticleOptions, ParticleType}, +}; +use horizon_core::{geometry::Vec3, random::RandomSource}; + +impl Game<'_> { + /// `Ravager.handleEntityEvent(69)`: forty puffs of smoke from the middle + /// of its box, then `applyRoarKnockbackClient`: this client's own player + /// within 4 blocks is thrown away from it. + pub(super) fn remote_ravager_roar(&mut self, id: i32) { + let Some(m) = self + .mobs + .iter() + .find(|m| m.id == id as u32 && m.species() == Species::Ravager) + else { + return; + }; + let (center, at, area) = ( + m.center(), + m.position, + m.bounds().inflate(Vec3::new(4.0, 4.0, 4.0)), + ); + let options = ParticleOptions::simple(ParticleType::Poof); + for _ in 0..40 { + let r = self.particles.random(); + let v = [ + r.next_gaussian() * 0.2, + r.next_gaussian() * 0.2, + r.next_gaussian() * 0.2, + ]; + self.particles.add(&options, center, v); + } + if let Some(view) = self.player_view() + && view.alive + && view.bounds.intersects(area) + && let Some(p) = self.player.as_mut() + { + // strongKnockback. + let (xd, zd) = (p.position.x - at.x, p.position.z - at.z); + let dd = (xd * xd + zd * zd).max(0.001); + p.velocity = p + .velocity + .add_vector(Vec3::new(xd / dd * 4.0, 0.2, zd / dd * 4.0)); + } + } +} diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 2325c3f5b..cc9a34641 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -903,6 +903,23 @@ 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], + }, + /// `ServerLevel.sendParticles`: drawn here and sent to the players within + /// 32 blocks, which spread and speed the way the client does for a count + /// (a count of 0 is one particle moving by speed times spread). + LevelParticles { + kind: &'static str, + at: [f64; 3], + spread: [f64; 3], + speed: f64, + count: u32, + }, 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/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 26b94eeac..7790e2ce1 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -727,8 +727,8 @@ fn ai_step(m: &mut Mob, w: &MobWorld, level_random: &mut LegacyRandom, events: & if m.kind.flags.climber { m.climbing = m.horizontal_collision; } - // AgeableMob.aiStep: a forced age-up's particles (positions drawn on the - // server too), then the age. + // AgeableMob.aiStep: a forced age-up's particles (`sendParticles`, so the + // players are sent them), then the age. if m.extra.forced_age_timer > 0 { if m.extra.forced_age_timer % 4 == 0 { let (w, h) = (f64::from(m.width()), f64::from(m.height())); @@ -737,10 +737,12 @@ fn ai_step(m: &mut Mob, w: &MobWorld, level_random: &mut LegacyRandom, events: & let y = m.position.y + h * r.next_double() + 0.5; let z = m.position.z + w * (2.0 * r.next_double() - 1.0); // sendParticles(HAPPY_VILLAGER, x, y, z, 1, 0, 0, 0, 0): exactly there, at rest. - events.push(MobEvent::Particles { + events.push(MobEvent::LevelParticles { kind: "happy_villager", at: [x, y, z], - count: 0, + spread: [0.0; 3], + speed: 0.0, + count: 1, }); } m.extra.forced_age_timer -= 1; diff --git a/crates/horizon-client/src/mobs/cube.rs b/crates/horizon-client/src/mobs/cube.rs index ea8a05392..3cc16463c 100644 --- a/crates/horizon-client/src/mobs/cube.rs +++ b/crates/horizon-client/src/mobs/cube.rs @@ -151,37 +151,49 @@ pub fn jump(m: &mut Mob) { }; m.velocity.y = f64::from(m.jump_power() + boost); } +/// `AbstractCubeMob.tick`'s landing splash: crumbs of goo (or flames) around +/// the cube's feet, drawn by the side that ticks the mob. +fn landing_splash(m: &mut Mob, events: &mut Vec) { + let size = m.width() * 2.0; + let radius = size / 2.0; + let kind = match m.species() { + Species::MagmaCube => "flame", + Species::SulfurCube => "sulfur_cube_goo", + _ => "item_slime", + }; + let mut i = 0; + while (i as f32) < size * 16.0 { + let dir = m.random.next_float() * std::f64::consts::TAU as f32; + let d = m.random.next_float() * 0.5 + 0.5; + let xd = trig::sin(f64::from(dir)) * radius * d; + let zd = trig::cos(f64::from(dir)) * radius * d; + events.push(MobEvent::Particles { + kind, + at: [ + m.position.x + f64::from(xd), + m.position.y, + m.position.z + f64::from(zd), + ], + count: 0, + }); + i += 1; + } +} +/// `AbstractCubeMob.tick` on a client of a server: the landing splash follows +/// the on-ground flag the server sends. +pub fn client_tick(m: &mut Mob, events: &mut Vec) { + if m.on_ground && !m.extra.flag2 { + landing_splash(m, events); + } + m.extra.flag2 = m.on_ground; +} /// AbstractCubeMob.tick squish and landing sounds, plus contact damage. pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { let v = &mut m.anim.v; v[SQUISH] += (v[TARGET_SQUISH] - v[SQUISH]) * 0.5; let was = m.extra.flag2; if m.on_ground && !was { - // The landing splash: drawn on the server too, where it spawns nothing. - let size = m.width() * 2.0; - let radius = size / 2.0; - let kind = match m.species() { - Species::MagmaCube => "flame", - Species::SulfurCube => "sulfur_cube_goo", - _ => "item_slime", - }; - let mut i = 0; - while (i as f32) < size * 16.0 { - let dir = m.random.next_float() * std::f64::consts::TAU as f32; - let d = m.random.next_float() * 0.5 + 0.5; - let xd = trig::sin(f64::from(dir)) * radius * d; - let zd = trig::cos(f64::from(dir)) * radius * d; - events.push(MobEvent::Particles { - kind, - at: [ - m.position.x + f64::from(xd), - m.position.y, - m.position.z + f64::from(zd), - ], - count: 0, - }); - i += 1; - } + landing_splash(m, events); let squish = match (m.species(), tiny(m)) { (Species::SulfurCube, _) => super::sulfur_cube::squish_sound(m), (Species::MagmaCube, true) => "entity.magma_cube.squish_small", diff --git a/crates/horizon-client/src/mobs/illusioner.rs b/crates/horizon-client/src/mobs/illusioner.rs index ec17abad7..b98e56920 100644 --- a/crates/horizon-client/src/mobs/illusioner.rs +++ b/crates/horizon-client/src/mobs/illusioner.rs @@ -152,20 +152,7 @@ pub fn tick(m: &mut Mob, events: &mut Vec) { if !m.has_effect(INVISIBILITY) { return; } - let hurt = m.hurt_time; - let age = m.age; - let mut il = m.extra.illusions.take().unwrap_or_default(); - il.ticks = (il.ticks - 1).max(0); - if hurt == 1 || age.is_multiple_of(1200) { - il.ticks = 3; - for i in 0..COPIES { - il.offsets[0][i] = il.offsets[1][i]; - let r = &mut m.random; - let x = (-6 + r.next_int_bound(13).unwrap_or(0)) as f64 * 0.5; - let y = (r.next_int_bound(6).unwrap_or(0) - 4).max(0) as f64; - let z = (-6 + r.next_int_bound(13).unwrap_or(0)) as f64 * 0.5; - il.offsets[1][i] = [x, y, z]; - } + if step(m) { events.push(MobEvent::Particles { kind: "minecraft:cloud", at: [m.position.x, m.position.y + 0.9, m.position.z], @@ -177,6 +164,47 @@ pub fn tick(m: &mut Mob, events: &mut Vec) { 1.0, 1.0, )); + } +} + +/// `Illusioner.aiStep` on a client of a server: invisible is the synched +/// shared flag. The puff is drawn here exactly (sixteen `addParticle`s); the +/// sound comes from the server. +pub fn client_tick(m: &mut Mob, events: &mut Vec) { + if !m.invisible || !step(m) { + return; + } + let (w, h) = (f64::from(m.width()), f64::from(m.height())); + for _ in 0..16 { + // getRandomX(0.5), getRandomY() and getZ(0.5), which is not random. + let x = m.position.x + w * (2.0 * m.random.next_double() - 1.0) * 0.5; + let y = m.position.y + h * m.random.next_double(); + events.push(MobEvent::ParticleAt { + kind: "cloud", + at: [x, y, m.position.z + w * 0.5], + velocity: [0.0; 3], + }); + } +} + +/// The image offsets' clock of an invisible illusioner. True when the images +/// jumped to new offsets (hurt, or once a minute), which the puff goes with. +fn step(m: &mut Mob) -> bool { + let hurt = m.hurt_time; + let age = m.age; + let mut il = m.extra.illusions.take().unwrap_or_default(); + il.ticks = (il.ticks - 1).max(0); + let jumped = hurt == 1 || age.is_multiple_of(1200); + if jumped { + il.ticks = 3; + for i in 0..COPIES { + il.offsets[0][i] = il.offsets[1][i]; + let r = &mut m.random; + let x = (-6 + r.next_int_bound(13).unwrap_or(0)) as f64 * 0.5; + let y = (r.next_int_bound(6).unwrap_or(0) - 4).max(0) as f64; + let z = (-6 + r.next_int_bound(13).unwrap_or(0)) as f64 * 0.5; + il.offsets[1][i] = [x, y, z]; + } } else if hurt == 9 { il.ticks = 3; for i in 0..COPIES { @@ -185,6 +213,7 @@ pub fn tick(m: &mut Mob, events: &mut Vec) { } } m.extra.illusions = Some(il); + jumped } /// `Illusioner.getIllusionOffsets`. diff --git a/crates/horizon-client/src/mobs/nautilus.rs b/crates/horizon-client/src/mobs/nautilus.rs index 2fcf7eb46..864865846 100644 --- a/crates/horizon-client/src/mobs/nautilus.rs +++ b/crates/horizon-client/src/mobs/nautilus.rs @@ -293,19 +293,28 @@ fn bubbles(m: &mut Mob, events: &mut Vec) { f64::from(yaw.cos() * pitch.cos()), ]; let spread = m.random.next_double() * 0.8 * (1.0 + speed); - for _ in 0..3 { - let _ = (f64::from(m.random.next_float()) - 0.5) * spread; - } - events.push(MobEvent::Particles { + let velocity = [ + (f64::from(m.random.next_float()) - 0.5) * spread, + (f64::from(m.random.next_float()) - 0.5) * spread, + (f64::from(m.random.next_float()) - 0.5) * spread, + ]; + events.push(MobEvent::ParticleAt { kind: "bubble", at: [ m.position.x - dir[0] * 1.1, m.position.y - dir[1] + 0.25, m.position.z - dir[2] * 1.1, ], - count: 1, + velocity, }); } +/// `AbstractNautilus.tick` on a client of a server: the bubbles of a nautilus +/// in the water, from the motion the server sent. +pub fn client_tick(m: &mut Mob, events: &mut Vec) { + if m.fluids.in_water { + bubbles(m, events); + } +} fn in_water_at(w: &MobWorld, p: Vec3) -> bool { w.fluid(p.x.floor() as i32, p.y.floor() as i32, p.z.floor() as i32) == Fluid::Water } diff --git a/crates/horizon-client/src/mobs/ravager.rs b/crates/horizon-client/src/mobs/ravager.rs index 49bac4d9f..2db86e42a 100644 --- a/crates/horizon-client/src/mobs/ravager.rs +++ b/crates/horizon-client/src/mobs/ravager.rs @@ -197,7 +197,14 @@ fn roar(m: &mut Mob, w: &MobWorld, events: &mut Vec) { velocity: fling(s.position), }); } - // Entity event 69: a puff of smoke. + // Entity event 69: a puff of smoke, and the client's own push. A client + // of this server (the window of the server thread) gets the event; + // this window draws the puff itself. + events.push(MobEvent::EntityEvent { + mob: m.id, + event: 69, + at: from, + }); events.push(MobEvent::Particles { kind: "poof", at: [ diff --git a/crates/horizon-client/src/mobs/undead.rs b/crates/horizon-client/src/mobs/undead.rs index 3707f4bbe..51a5cb0d2 100644 --- a/crates/horizon-client/src/mobs/undead.rs +++ b/crates/horizon-client/src/mobs/undead.rs @@ -978,16 +978,21 @@ impl Goal for AttackTurtleEgg { pos: eat, drop: false, }); + // `sendParticles(POOF, ..., 1, xa, ya, za, 0.15)`, twenty times. for _ in 0..20 { - m.random.next_gaussian(); - m.random.next_gaussian(); - m.random.next_gaussian(); + let spread = [ + m.random.next_gaussian() * 0.02, + m.random.next_gaussian() * 0.02, + m.random.next_gaussian() * 0.02, + ]; + cx.events.push(MobEvent::LevelParticles { + kind: "poof", + at: [center[0], f64::from(eat[1]), center[2]], + spread, + speed: 0.15, + count: 1, + }); } - cx.events.push(MobEvent::Particles { - kind: "poof", - at: [center[0], f64::from(eat[1]), center[2]], - count: 20, - }); // The break sound's pitch draws from the level random. cx.events.push(MobEvent::Sound( "block.turtle_egg.break", diff --git a/crates/horizon-client/src/mobs/wither.rs b/crates/horizon-client/src/mobs/wither.rs index 6702686c5..f962f574f 100644 --- a/crates/horizon-client/src/mobs/wither.rs +++ b/crates/horizon-client/src/mobs/wither.rs @@ -169,6 +169,26 @@ fn target_pos(w: &MobWorld, id: i32) -> Option<([f64; 3], f64, bool)> { .map(|s| ([s.position.x, s.position.y, s.position.z], s.eye_y, false)), } } +/// `WitherBoss.aiStep` on a client of a server: the smoke about each head +/// (`radius` 0.3 of the wither's scale, which is 1 here). The heads' turning +/// and the coloured particles need the server's targets and invulnerable +/// ticks, which a client is not sent. +pub fn client_tick(m: &mut Mob, events: &mut Vec) { + for i in 0..3 { + let h = head_pos(m, i); + let r = &mut m.random; + let at = [ + h[0] + r.next_gaussian() * 0.3, + h[1] + r.next_gaussian() * 0.3, + h[2] + r.next_gaussian() * 0.3, + ]; + events.push(MobEvent::ParticleAt { + kind: "smoke", + at, + velocity: [0.0; 3], + }); + } +} /// `WitherBoss.aiStep` after `super.aiStep()`: the side heads turn, smoke /// rises from the heads (the particle offsets are drawn on the server too). pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { From 9914a8af4113d595361cad8c02c576e4f30e1667 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Tue, 27 Oct 2026 16:47:06 +0100 Subject: [PATCH 2/2] Name the wither's client smoke tick client_smoke #2685 adds wither::client_tick for the side heads' turning on a client; a distinct name lets both land without a clash. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/remote.rs | 2 +- crates/horizon-client/src/mobs/wither.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 7e145b952..8d6479076 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -3796,7 +3796,7 @@ impl Game<'_> { S::Slime | S::MagmaCube | S::SulfurCube => cube::client_tick(m, &mut mob_fx), S::Nautilus | S::ZombieNautilus => nautilus::client_tick(m, &mut mob_fx), S::Illusioner => illusioner::client_tick(m, &mut mob_fx), - S::Wither => wither::client_tick(m, &mut mob_fx), + S::Wither => wither::client_smoke(m, &mut mob_fx), _ => {} } } diff --git a/crates/horizon-client/src/mobs/wither.rs b/crates/horizon-client/src/mobs/wither.rs index c8248106c..0a429b135 100644 --- a/crates/horizon-client/src/mobs/wither.rs +++ b/crates/horizon-client/src/mobs/wither.rs @@ -173,7 +173,7 @@ fn target_pos(w: &MobWorld, id: i32) -> Option<([f64; 3], f64, bool)> { /// (`radius` 0.3 of the wither's scale, which is 1 here). The heads' turning /// and the coloured particles need the server's targets and invulnerable /// ticks, which a client is not sent. -pub fn client_tick(m: &mut Mob, events: &mut Vec) { +pub fn client_smoke(m: &mut Mob, events: &mut Vec) { for i in 0..3 { let h = head_pos(m, i); let r = &mut m.random;