Merge pull request #2677 from kacpherek/fix/mob-event-particles-to-clients

🤖 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 (all round-2 PRs together: clippy, 1722 tests, 135 harnesses, lan_e2e in-frame and on the server thread) and union/cg19-r3 (the same PRs merged with today's main: 96/96 harnesses). Thanks @kacpherek.
This commit is contained in:
Ludovic Marie
2026-10-27 17:41:18 +01:00
committed by GitHub
13 changed files with 267 additions and 74 deletions
+1
View File
@@ -150,6 +150,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;
@@ -1072,6 +1072,20 @@ impl Game<'_> {
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
@@ -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
+17 -17
View File
@@ -2293,6 +2293,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
@@ -3871,15 +3875,12 @@ impl Game<'_> {
let head_lerps = &mut r.head_lerps;
let players = &r.level_players;
let mut using = Vec::new();
let mut wither_heads = 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 {
continue;
}
if m.species() == horizon_client::mobs::Species::Wither {
wither_heads.extend((0..3).map(|i| horizon_client::mobs::wither::head_pos(m, i)));
}
m.previous = m.position;
m.previous_yaw = m.yaw;
m.previous_body_yaw = m.body_yaw;
@@ -3914,6 +3915,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_smoke(m, &mut mob_fx),
_ => {}
}
}
if m.hurt_time > 0 {
m.hurt_time -= 1;
}
@@ -4019,19 +4031,7 @@ impl Game<'_> {
d.tick(world, facts, collision, false, &self.res.motion().fluids);
}
head_lerps.retain(|_, (_, steps)| *steps > 0);
// WitherBoss.aiStep runs on a client too: smoke around each head.
let smoke = horizon_client::particles::ParticleOptions::simple(
horizon_client::particles::ParticleType::Smoke,
);
for h in wither_heads {
let r = self.particles.random();
let at = [
h[0] + r.next_gaussian() * 0.3,
h[1] + r.next_gaussian() * 0.3,
h[2] + r.next_gaussian() * 0.3,
];
self.particles.add(&smoke, at, [0.0; 3]);
}
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);
}
@@ -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));
}
}
}
+10
View File
@@ -967,6 +967,16 @@ pub enum MobEvent {
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).
+6 -4
View File
@@ -902,8 +902,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()));
@@ -912,10 +912,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;
+37 -25
View File
@@ -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<MobEvent>) {
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<MobEvent>) {
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<MobEvent>) {
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",
+43 -14
View File
@@ -152,20 +152,7 @@ pub fn tick(m: &mut Mob, events: &mut Vec<MobEvent>) {
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<MobEvent>) {
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<MobEvent>) {
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<MobEvent>) {
}
}
m.extra.illusions = Some(il);
jumped
}
/// `Illusioner.getIllusionOffsets`.
+14 -5
View File
@@ -293,19 +293,28 @@ fn bubbles(m: &mut Mob, events: &mut Vec<MobEvent>) {
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<MobEvent>) {
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
}
+8 -1
View File
@@ -236,7 +236,14 @@ fn roar(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
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: [
+13 -8
View File
@@ -1041,16 +1041,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",
+20
View File
@@ -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_smoke(m: &mut Mob, events: &mut Vec<MobEvent>) {
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<MobEvent>) {