From c7a2fbb182e2188448e2fb27e2ea84b5de9df391 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 11 Oct 2026 19:18:23 +0200 Subject: [PATCH] Match vanilla wither tick order, underwater checks and gravity The wither's customServerAiStep (charging, side heads, block breaking, healing) now runs inside serverAiStep, and aiStep draws the heads' smoke particle offsets after travel as vanilla does; its steering uses the 0.6F and Mth.atan2 float maths, and its level-event sounds (shoot, spawn, block break) no longer draw from its random: MobEvent::LevelEventSound lets the client pick their pitch. Entity.getGravity is zero while isNoGravity, which the flying move control sets. Drowned swim with isUnderWater (the previous tick's eye state, Entity.wasEyeInWater), and the eyes count as in water per 26.3's EntityFluidInteraction (between a water block's bottom and its camera height) rather than the older 0.11 offset. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/examples/mob_ai_probe.rs | 1 + crates/horizon-client/src/game/creatures.rs | 11 +++ crates/horizon-client/src/mobs.rs | 13 +++ crates/horizon-client/src/mobs/ai.rs | 2 + crates/horizon-client/src/mobs/living.rs | 39 +++++++- crates/horizon-client/src/mobs/undead.rs | 11 ++- crates/horizon-client/src/mobs/wither.rs | 93 ++++++++++++------- 7 files changed, 127 insertions(+), 43 deletions(-) diff --git a/crates/horizon-client/examples/mob_ai_probe.rs b/crates/horizon-client/examples/mob_ai_probe.rs index 9f083e2d5..dda20095b 100644 --- a/crates/horizon-client/examples/mob_ai_probe.rs +++ b/crates/horizon-client/examples/mob_ai_probe.rs @@ -173,6 +173,7 @@ fn goal_name(m: &Mob, name: &str) -> String { "TryFindWater" => "TryFindWaterGoal", "RangedAttack" => "RangedAttackGoal", "MoveTowardsTarget" => "MoveTowardsTargetGoal", + "DrownedGoToWater" => "DrownedGoToWaterGoal", "TakeBlock" => "EndermanTakeBlockGoal", "LeaveBlock" => "EndermanLeaveBlockGoal", "FreezeWhenLookedAt" => "EndermanFreezeWhenLookedAt", diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 38c56d408..999ee2025 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -635,6 +635,17 @@ impl Game<'_> { self.audio .play(&format!("minecraft:{name}"), Some(at), volume, pitch) } + MobEvent::LevelEventSound { + sound, + at, + volume, + spread, + } => { + let r = &mut self.world.random; + let pitch = (r.next_float() - r.next_float()) * spread + 1.0; + self.audio + .play(&format!("minecraft:{sound}"), Some(at), volume, pitch) + } MobEvent::SoundId(name, at, volume, pitch) => { self.audio.play(&name, Some(at), volume, pitch) } diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index efd9bc88a..48d6833d4 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -595,6 +595,9 @@ pub struct Mob { pub ai: Box, pub extra: Extra, pub fluids: living::Fluids, + /// Entity.wasEyeInWater: the eyes were in water before this tick's fluid + /// update (`isUnderWater` reads it). + pub was_eye_in_water: bool, pub no_gravity: bool, /// `NoAI`: no goals and no movement. pub no_ai: bool, @@ -626,6 +629,15 @@ pub enum MobEvent { Sound(&'static str, [f64; 3], f32, f32), /// A sound event id that is not a static string (variant sounds). SoundId(String, [f64; 3], f32, f32), + /// A level event's sound (`levelEvent`/`globalLevelEvent`): the client + /// plays it with its own random pitch spread (`(a - b) * spread + 1`), + /// nothing is drawn from the mob. + LevelEventSound { + sound: &'static str, + at: [f64; 3], + volume: f32, + spread: f32, + }, AttackPlayer(f32, [f64; 3]), /// Damage of a given type to the player (guardian lasers, thorns). DamagePlayer { @@ -1009,6 +1021,7 @@ impl Mob { ai: Box::default(), extra: Extra::default(), fluids: living::Fluids::default(), + was_eye_in_water: false, no_gravity: false, no_ai: false, silent: false, diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 180fcfe0a..fafa371ed 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -371,6 +371,7 @@ fn tick_swing(m: &mut Mob) { } /// Entity.baseTick + LivingEntity.baseTick + Mob.baseTick. fn base_tick(m: &mut Mob, w: &MobWorld, events: &mut Vec) { + m.was_eye_in_water = m.fluids.eye_in_water; m.fluids = w.fluids(m.bounds(), m.eye_y()); if m.fluids.in_water { m.fall_distance = 0.0; @@ -761,6 +762,7 @@ fn custom_server_ai_step(m: &mut Mob, cx: &mut Cx) { Species::Blaze => super::nether::blaze_server_ai_step(m, cx), Species::ElderGuardian => super::guardian::elder_server_ai_step(m, cx), Species::Warden => super::warden::server_ai_step(m, cx), + Species::Wither if m.alive() => super::wither::custom_server_ai_step(m, cx.w, cx.events), // SpellcasterIllager.customServerAiStep. Species::Evoker if m.extra.counter > 0 => m.extra.counter -= 1, _ => {} diff --git a/crates/horizon-client/src/mobs/living.rs b/crates/horizon-client/src/mobs/living.rs index 99d7417b1..8756ae6be 100644 --- a/crates/horizon-client/src/mobs/living.rs +++ b/crates/horizon-client/src/mobs/living.rs @@ -86,10 +86,34 @@ impl MobWorld<'_> { } } } - let (fx, fz) = ((lo.x + hi.x) / 2.0, (lo.z + hi.z) / 2.0); - let ey = eye_y.floor() as i32; - let (fluid, h) = self.fluid_height(fx.floor() as i32, ey, fz.floor() as i32); - out.eye_in_water = fluid == Fluid::Water && eye_y < f64::from(ey) + h + 0.111_111_11; + // EntityFluidInteraction.update: the eyes are in a fluid block of + // the box in the entity's column, between its bottom and its height + // for the camera. + let (ex, ez) = ( + ((lo.x + hi.x) / 2.0).floor() as i32, + ((lo.z + hi.z) / 2.0).floor() as i32, + ); + for y in lo.y.floor() as i32..=(hi.y.ceil() as i32 - 1) { + let (fluid, h) = self.fluid_height(ex, y, ez); + if fluid != Fluid::Water || f64::from(y) + h < lo.y || eye_y < f64::from(y) { + continue; + } + let state = self.world.block(ex, y, ez); + let source = self + .registry + .state(state) + .and_then(|s| s.properties.get("level")) + .is_none_or(|l| l == "0"); + let above = self.facts.get(self.world.block(ex, y + 1, ez)); + let camera = if source && above.sturdy_full & 1 != 0 { + 1.0 + } else { + h + }; + if eye_y <= f64::from(y) + camera { + out.eye_in_water = true; + } + } out } pub fn no_collision(&self, b: Aabb) -> bool { @@ -362,8 +386,13 @@ impl Mob { fn flying_speed(&self) -> f32 { 0.02 } + /// Entity.getGravity: none while isNoGravity. fn gravity(&self) -> f64 { - self.kind.gravity + if self.no_gravity { + 0.0 + } else { + self.kind.gravity + } } /// LivingEntity.travel. pub fn travel(&mut self, w: &MobWorld, input: Vec3, events: &mut Vec) { diff --git a/crates/horizon-client/src/mobs/undead.rs b/crates/horizon-client/src/mobs/undead.rs index 50ac9934f..b6986f6d1 100644 --- a/crates/horizon-client/src/mobs/undead.rs +++ b/crates/horizon-client/src/mobs/undead.rs @@ -208,7 +208,8 @@ pub fn drowned_travel( input: Vec3, events: &mut Vec, ) -> bool { - if !(m.fluids.eye_in_water && m.fluids.in_water && drowned_wants_to_swim(m, w)) { + // isUnderWater: wasEyeInWater && isInWater. + if !(m.was_eye_in_water && m.fluids.in_water && drowned_wants_to_swim(m, w)) { return false; } m.move_relative(0.01, input); @@ -260,7 +261,7 @@ impl Goal for DrownedTridentAttack { self.0.flags() } fn name(&self) -> &'static str { - "Drowned$DrownedTridentAttackGoal" + "DrownedTridentAttackGoal" } fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { self.0.can_use(m, cx) && m.extra.main_hand.as_deref() == Some("minecraft:trident") @@ -292,7 +293,7 @@ impl Goal for DrownedAttack { self.0.flags() } fn name(&self) -> &'static str { - "Drowned$DrownedAttackGoal" + "DrownedAttackGoal" } fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { self.0.can_use(m, cx) && drowned_ok_target(m, cx.w) @@ -333,7 +334,7 @@ impl Goal for DrownedGoToBeach { MOVE | super::goals::JUMP } fn name(&self) -> &'static str { - "Drowned$DrownedGoToBeachGoal" + "DrownedGoToBeachGoal" } fn every_tick(&self) -> bool { true @@ -372,7 +373,7 @@ impl Goal for DrownedSwimUp { 0 } fn name(&self) -> &'static str { - "Drowned$DrownedSwimUpGoal" + "DrownedSwimUpGoal" } fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { !cx.w.daylight && m.fluids.in_water && m.position.y < f64::from(cx.w.sea_level - 2) diff --git a/crates/horizon-client/src/mobs/wither.rs b/crates/horizon-client/src/mobs/wither.rs index 5782723d1..eda2b76c4 100644 --- a/crates/horizon-client/src/mobs/wither.rs +++ b/crates/horizon-client/src/mobs/wither.rs @@ -111,13 +111,15 @@ fn head_pos(m: &Mob, i: usize) -> [f64; 3] { /// `performRangedAttack`: a skull from a head toward a point. fn shoot(m: &mut Mob, head: usize, at: [f64; 3], dangerous: bool, events: &mut Vec) { let h = head_pos(m, head); - // Level event 1024: the shoot sound. - events.push(MobEvent::Sound( - "entity.wither.shoot", - h, - 2.0, - (m.random.next_float() - m.random.next_float()) * 0.2 + 1.0, - )); + // Level event 1024: the shoot sound (the client's pitch). + if !m.silent { + events.push(MobEvent::LevelEventSound { + sound: "entity.wither.shoot", + at: [m.position.x, m.position.y, m.position.z], + volume: 2.0, + spread: 0.2, + }); + } let dir = [at[0] - h[0], at[1] - h[1], at[2] - h[2]]; let p = Projectile::fireball( ProjectileKind::WitherSkull { dangerous }, @@ -135,7 +137,7 @@ pub fn steer(m: &mut Mob) { let mut yd = v.y; if m.position.y < t[1] || !powered(m) && m.position.y < t[1] + 5.0 { yd = yd.max(0.0); - yd += 0.3 - yd * 0.6; + yd += 0.3 - yd * f64::from(0.6_f32); } v.y = yd; let (dx, dz) = (t[0] - m.position.x, t[2] - m.position.z); @@ -147,7 +149,10 @@ pub fn steer(m: &mut Mob) { } m.velocity = v; if v.x * v.x + v.z * v.z > 0.05 { - m.yaw = (v.z.atan2(v.x).to_degrees() as f32) - 90.0; + // (float)Mth.atan2(z, x) * (180.0F / (float)Math.PI) - 90.0F + m.yaw = (horizon_core::geometry::trig::atan2(v.z, v.x) as f32) + * (180.0_f32 / std::f32::consts::PI) + - 90.0; } } fn target_pos(w: &MobWorld, id: i32) -> Option<([f64; 3], f64, bool)> { @@ -165,14 +170,9 @@ 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` (heads, particles) and `customServerAiStep`. +/// `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) { - if !m.alive() { - return; - } - m.extra.point = m - .target_info(w) - .map(|t| [t.position.x, t.position.y, t.position.z]); // Side heads turn toward their targets, else back to the body. for i in 0..2 { match target_pos(w, m.extra.ints[i]) { @@ -180,22 +180,42 @@ pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { let h = head_pos(m, i + 1); let (xd, yd, zd) = (p[0] - h[0], eye - h[1], p[2] - h[2]); let sd = (xd * xd + zd * zd).sqrt(); - let y_rot = (zd.atan2(xd).to_degrees() as f32) - 90.0; - let x_rot = -(yd.atan2(sd).to_degrees() as f32); + let y_rot = horizon_core::geometry::trig::atan2_degrees(zd, xd) - 90.0; + let x_rot = -horizon_core::geometry::trig::atan2_degrees(yd, sd); m.anim.v[2 + i] = rotlerp(m.anim.v[2 + i], x_rot, 40.0); m.anim.v[i] = rotlerp(m.anim.v[i], y_rot, 10.0); } None => m.anim.v[i] = rotlerp(m.anim.v[i], m.body_yaw, 10.0), } } + let powered = powered(m); for i in 0..3 { let h = head_pos(m, i); + for _ in 0..3 { + m.random.next_gaussian(); + } events.push(MobEvent::Particles { kind: "smoke", at: h, count: 0, }); + // The entity-effect particles roll the level random, not the wither's. + let _ = powered; } + if m.extra.counter > 0 { + for _ in 0..3 { + m.random.next_gaussian(); + m.random.next_float(); + m.random.next_gaussian(); + } + } +} +/// `WitherBoss.customServerAiStep`: charging, then the side heads' targets +/// and volleys, block breaking and healing. +pub fn custom_server_ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { + m.extra.point = m + .target_info(w) + .map(|t| [t.position.x, t.position.y, t.position.z]); if m.extra.counter > 0 { let left = m.extra.counter - 1; if left <= 0 { @@ -203,12 +223,15 @@ pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { [m.position.x, m.eye_y(), m.position.z], 7.0, )); - events.push(MobEvent::Sound( - "entity.wither.spawn", - [m.position.x, m.position.y, m.position.z], - 1.0, - 1.0, - )); + // Global level event 1023. + if !m.silent { + events.push(MobEvent::LevelEventSound { + sound: "entity.wither.spawn", + at: [m.position.x, m.position.y, m.position.z], + volume: 1.0, + spread: 0.0, + }); + } } m.extra.counter = left; if m.age.is_multiple_of(10) { @@ -226,11 +249,14 @@ pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { m.extra.ints[2 + k] = age + 10 + m.random.next_int_bound(10).unwrap_or(0); m.extra.ints[4 + k] += 1; if m.extra.ints[4 + k] > 16 { + // Mth.nextDouble(random, min, max) = nextDouble * (max - min) + min. + let p = m.position; let r = &mut m.random; + let mut between = |lo: f64, hi: f64| r.next_double() * (hi - lo) + lo; let at = [ - m.position.x - 10.0 + r.next_double() * 20.0, - m.position.y - 5.0 + r.next_double() * 10.0, - m.position.z - 10.0 + r.next_double() * 20.0, + between(p.x - 10.0, p.x + 10.0), + between(p.y - 5.0, p.y + 5.0), + between(p.z - 10.0, p.z + 10.0), ]; shoot(m, i + 1, at, true, events); m.extra.ints[4 + k] = 0; @@ -307,12 +333,13 @@ pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { } } if destroyed { - events.push(MobEvent::Sound( - "entity.wither.break_block", - [m.position.x, m.position.y, m.position.z], - 1.0, - 1.0, - )); + // Level event 1022. + events.push(MobEvent::LevelEventSound { + sound: "entity.wither.break_block", + at: [m.position.x, m.position.y, m.position.z], + volume: 1.0, + spread: 0.0, + }); } } }