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 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-11 19:18:23 +02:00
parent a466af6adc
commit c7a2fbb182
7 changed files with 127 additions and 43 deletions
@@ -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",
@@ -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)
}
+13
View File
@@ -595,6 +595,9 @@ pub struct Mob {
pub ai: Box<ai::Ai>,
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,
+2
View File
@@ -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<MobEvent>) {
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,
_ => {}
+34 -5
View File
@@ -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<MobEvent>) {
+6 -5
View File
@@ -208,7 +208,8 @@ pub fn drowned_travel(
input: Vec3,
events: &mut Vec<MobEvent>,
) -> 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)
+60 -33
View File
@@ -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<MobEvent>) {
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<MobEvent>) {
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<MobEvent>) {
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<MobEvent>) {
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<MobEvent>) {
[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<MobEvent>) {
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<MobEvent>) {
}
}
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,
});
}
}
}