From 7fdbdcb3cb5568480c3ffc74fc811b37b12083ab Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Tue, 13 Oct 2026 01:06:33 +0200 Subject: [PATCH] Warden footsteps and hearing, kills in the probe, and the ghastling's fly stroll - Warden.nextStep: a step every 0.55 walked; mob steps raise a step game event, which the mob AI probe delivers to wardens through the listener the game uses (warden::listen). - Warden.tick runs the vibration ticker before super.tick. - The probe applies melee hits on mobs, reads a warden's target from its brain, and plays the player's death sound for a lethal hit; warden_player and warden_cow stop before the killing blow. - getViewVector(0) uses the head's previous yaw, and AirAndWaterRandomPos the float quarter turn: tempted ghastlings stroll as vanilla's. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/examples/mob_ai_probe.rs | 55 ++++++++- crates/horizon-client/src/game/sculk.rs | 59 ++-------- crates/horizon-client/src/mobs.rs | 9 ++ crates/horizon-client/src/mobs/ai.rs | 1 + crates/horizon-client/src/mobs/goals.rs | 8 +- crates/horizon-client/src/mobs/living.rs | 23 +++- crates/horizon-client/src/mobs/warden.rs | 108 ++++++++++++++++-- tools/test_mob_ai_parity.py | 8 +- 8 files changed, 197 insertions(+), 74 deletions(-) diff --git a/crates/horizon-client/examples/mob_ai_probe.rs b/crates/horizon-client/examples/mob_ai_probe.rs index 7fe19f5b1..0601e6c6a 100644 --- a/crates/horizon-client/examples/mob_ai_probe.rs +++ b/crates/horizon-client/examples/mob_ai_probe.rs @@ -358,6 +358,13 @@ fn print_heart( ); } +/// The damage of a mob's direct hit on the player (others count as none). +fn player_damage(e: &MobEvent) -> f32 { + match e { + MobEvent::AttackPlayer(amount, _) | MobEvent::DamagePlayer { amount, .. } => *amount, + _ => 0.0, + } +} /// Whether an event hurts the scenario's player (survival, normal difficulty, /// no armour, made hurtable again every tick by the oracle). fn player_hurt(player: Option<&PlayerView>, e: &MobEvent) -> bool { @@ -378,7 +385,9 @@ fn player_hurt(player: Option<&PlayerView>, e: &MobEvent) -> bool { } } -fn print(out: &mut String, t: i32, i: usize, m: &Mob) { +/// `player_down`: the scenario's player is no valid target (creative, or +/// killed this tick). +fn print(out: &mut String, t: i32, i: usize, m: &Mob, player_down: bool) { let _ = write!(out, "{t} {i}"); if m.removed { // Entity.RemovalReason: the end of the death animation, else a discard. @@ -418,7 +427,13 @@ fn print(out: &mut String, t: i32, i: usize, m: &Mob) { "\tgoals {}\ttargets {}\ttarget {}", goals(m, m.ai.goals.running_names()), goals(m, m.ai.targets.running_names()), - who(m.ai.target) + // Warden.getTarget: the brain's attack target, if still valid. + who(match horizon_client::mobs::warden::brain_target(m) { + Some(Who::Player) if player_down => None, + Some(target) => Some(target), + None if m.species() == Species::Warden => None, + None => m.ai.target, + }) ); let _ = write!(out, "\tnav "); match &m.ai.nav.path { @@ -674,7 +689,7 @@ fn main() -> horizon_client::Result<()> { }); let mut scratch = LegacyRandom::new(0); for (i, m) in mobs.iter().enumerate() { - print(&mut out, 0, i, m); + print(&mut out, 0, i, m, s.creative); } // The MobWorld every step sees, over a snapshot of the mobs. macro_rules! mob_world { @@ -801,6 +816,26 @@ fn main() -> horizon_client::Result<()> { m.removed = true; } horizon_client::mobs::apply_brain_events(&mut mobs, &mut events, from); + horizon_client::mobs::warden::hear_mob_events(&mut mobs, &events, from, &w); + // Melee hits on other mobs land during the attacker's tick + // (LivingEntity.hurtServer), with the sounds they raise. + let mut k = from; + while k < events.len() { + if let MobEvent::AttackMob { + target, + amount, + source, + } = events[k] + && let Some(t) = mobs.iter_mut().find(|m| m.id == target) + && t.damage(amount, source) + { + let raised = std::mem::take(&mut t.outbox); + let n = raised.len(); + events.splice(k + 1..k + 1, raised); + k += n; + } + k += 1; + } // ServerLevel ticks entities one after another: the ones // after this mob see it as it is now. if let Some(e) = snapshot.iter_mut().find(|e| e.id == mobs[i].id) { @@ -844,8 +879,13 @@ fn main() -> horizon_client::Result<()> { _ => {} } } + // A hit for the player's whole health kills it until the oracle + // heals it next tick. + let player_killed = events + .iter() + .any(|e| player_hurt(player.as_ref(), e) && player_damage(e) >= 20.0); for (i, m) in mobs.iter().enumerate() { - print(&mut out, t, i, m); + print(&mut out, t, i, m, s.creative || player_killed); } for e in &events { if let MobEvent::Sound(name, _, volume, pitch) = e { @@ -860,9 +900,14 @@ fn main() -> horizon_client::Result<()> { } else if player_hurt(player.as_ref(), e) { // The oracle's survival player, healed and made hurtable every tick, // takes the damage (LivingEntity.playHurtSound). + let sound = if player_damage(e) >= 20.0 { + "death" + } else { + "hurt" + }; let _ = writeln!( out, - "{t} sound minecraft:entity.player.hurt 3f800000 3f800000" + "{t} sound minecraft:entity.player.{sound} 3f800000 3f800000" ); } } diff --git a/crates/horizon-client/src/game/sculk.rs b/crates/horizon-client/src/game/sculk.rs index 4f5af29c3..c42a3aedf 100644 --- a/crates/horizon-client/src/game/sculk.rs +++ b/crates/horizon-client/src/game/sculk.rs @@ -638,54 +638,13 @@ impl Game<'_> { /// `Warden.VibrationUser`: accept a vibration for the warden at `index`. fn warden_listen(&mut self, index: usize, event: &'static str, pos: [f64; 3], ctx: &Context) { let now = self.world.game_time; - let (eye, busy) = { - let m = &self.mobs[index]; - let Some(st) = m.extra.warden.as_ref() else { - return; - }; - ( - [m.position.x, m.position.y + m.kind.eye_height, m.position.z], - st.vibration.current.is_some(), - ) - }; - if busy { - return; - } - let source_block = block_of(pos); - let eye_block = block_of(eye); - let d = [ - i64::from(source_block[0]) - i64::from(eye_block[0]), - i64::from(source_block[1]) - i64::from(eye_block[1]), - i64::from(source_block[2]) - i64::from(eye_block[2]), - ]; - if d[0] * d[0] + d[1] * d[1] + d[2] * d[2] > 256 { - return; - } - if !sculk::valid_vibration(sculk::warden_can_listen(event), event, ctx) { - return; - } - if !warden::can_receive_vibration(&self.mobs[index], i64::from(self.mobs[index].age), ctx) { - return; - } - if self.occluded(pos, eye) { - return; - } - let distance = { - let d = [pos[0] - eye[0], pos[1] - eye[1], pos[2] - eye[2]]; - (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt() as f32 - }; - if let Some(st) = self.mobs[index].extra.warden.as_mut() { - st.vibration.schedule( - Info { - event, - distance, - pos, - source: ctx.source, - owner: ctx.owner, - }, - now, - ); - } + let (occludes, world) = (&self.sculk.tables.occludes, &self.world); + warden::listen(&mut self.mobs[index], now, event, pos, ctx, |a, b| { + sculk::is_occluded(a, b, |p| { + let state = world.block(p.x, p.y, p.z); + occludes.get(state.0 as usize).copied().unwrap_or(false) + }) + }); } // ---- ticking ---- @@ -1627,10 +1586,6 @@ impl Game<'_> { } else { (move_dist as i32).wrapping_add(1) as f32 }; - if warden { - self.audio - .play("minecraft:entity.warden.step", Some(at), 10.0, 1.0); - } let ctx = self.who_context(Some(who), Some(state)); self.post_game_event("minecraft:step", at, ctx); } else if in_water { diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index b3c1ea7e1..986142b15 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -669,6 +669,15 @@ pub enum MobEvent { volume: f32, spread: f32, }, + /// `Level.gameEvent` from a mob's own movement (`GameEvent.STEP`): the + /// game's sculk layer tracks steps itself, the mob AI probe hears them + /// through the wardens (`warden::listen`). + GameEvent { + event: &'static str, + at: [f64; 3], + source: u32, + state: horizon_world::registry::BlockStateId, + }, AttackPlayer(f32, [f64; 3]), /// Damage of a given type to the player (guardian lasers, thorns). DamagePlayer { diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 218056146..534c32d25 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -350,6 +350,7 @@ pub fn tick( if m.removed { return false; } + super::warden::pre_tick(m, w, events); base_tick(m, w, events); if m.death_time >= super::creaking::death_limit(m) { // Creaking.tickDeath: a heart's creaking crumbles away. diff --git a/crates/horizon-client/src/mobs/goals.rs b/crates/horizon-client/src/mobs/goals.rs index 4dcd401c1..472154d35 100644 --- a/crates/horizon-client/src/mobs/goals.rs +++ b/crates/horizon-client/src/mobs/goals.rs @@ -346,12 +346,13 @@ impl Mob { let water = w.fluid(p[0], p[1], p[2]) == crate::blocks::Fluid::Water; (!water && self.malus(w.path_type_static(p[0], p[1], p[2])) == 0.0).then_some(p) } - /// `Entity.getViewVector(0)`: `calculateViewVector(xRotO, yRotO)`. + /// `Entity.getViewVector(0)`: `calculateViewVector(xRotO, yHeadRotO)`. pub fn view_vector(&self) -> [f64; 3] { use horizon_core::geometry::trig::Mth; let to_rad = std::f32::consts::PI / 180.0; let x = self.previous_head_pitch * to_rad; - let y = -self.previous_yaw * to_rad; + // LivingEntity.getViewYRot(0): the head's yaw of the last tick. + let y = -self.previous_head_yaw * to_rad; let (y_cos, y_sin, x_cos, x_sin) = (y.mcos(), y.msin(), x.mcos(), x.msin()); [ f64::from(y_sin * x_cos), @@ -378,7 +379,8 @@ impl Mob { fly_height, xd, zd, - std::f64::consts::FRAC_PI_2, + // Every caller passes `(float) (Math.PI / 2)`. + f64::from(std::f32::consts::FRAC_PI_2), )?; let mut p = m.toward(f64::from(h), d); if p[1] < crate::chunk::MIN_Y || p[1] >= 320 || m.restricted(f64::from(h), p) { diff --git a/crates/horizon-client/src/mobs/living.rs b/crates/horizon-client/src/mobs/living.rs index 03d2f201f..a0b4b03a0 100644 --- a/crates/horizon-client/src/mobs/living.rs +++ b/crates/horizon-client/src/mobs/living.rs @@ -367,9 +367,18 @@ impl Mob { let at = [self.position.x, self.position.y, self.position.z]; events.push(MobEvent::Sound(sound, at, volume, pitch)); } - self.ai.steps.next_step = (self.ai.steps.move_dist as i32).wrapping_add(1) as f32; + // Entity.vibrationAndSoundEffectsFromBlock: the step's game + // event, with the supporting block's state when the effect + // block is another. + events.push(MobEvent::GameEvent { + event: "minecraft:step", + at: [self.position.x, self.position.y, self.position.z], + source: self.id, + state: if legacy == on { legacy_state } else { state }, + }); + self.ai.steps.next_step = self.next_step(); } else if self.fluids.in_water { - self.ai.steps.next_step = (self.ai.steps.move_dist as i32).wrapping_add(1) as f32; + self.ai.steps.next_step = self.next_step(); // Entity.waterSwimSound let v = self.velocity; let volume = 1.0_f32.min( @@ -393,6 +402,16 @@ impl Mob { } } } + /// `Entity.nextStep`: the next whole metre walked; a warden's steps come + /// every 0.55. + fn next_step(&self) -> f32 { + let walked = self.ai.steps.move_dist; + if self.kind.species == super::Species::Warden { + walked + 0.55 + } else { + (walked as i32).wrapping_add(1) as f32 + } + } /// Entity.isSwimming (mobs never swim in the player sense). fn fluids_swimming(&self) -> bool { false diff --git a/crates/horizon-client/src/mobs/warden.rs b/crates/horizon-client/src/mobs/warden.rs index 546952d9c..454fbf10d 100644 --- a/crates/horizon-client/src/mobs/warden.rs +++ b/crates/horizon-client/src/mobs/warden.rs @@ -441,6 +441,11 @@ pub(super) fn do_hurt_target(m: &mut Mob, target: Who, w: &MobWorld, events: &mu super::ai::do_hurt_target(m, target, &mut cx); } +/// The brain's attack target (`Warden.getTarget` before `asValidTarget`). +pub fn brain_target(m: &Mob) -> Option { + warden_brain::mem_ref(m).and_then(|mem| mem.attack_target.value()) +} + // ---- vibrations ---- /// `Warden.VibrationUser.canReceiveVibration`. @@ -454,6 +459,85 @@ pub fn can_receive_vibration(m: &Mob, _now: i64, ctx: &Context) -> bool { && !digging_or_emerging(m) && !(ctx.living && !ctx.targetable) } +/// `VibrationSystem.Listener.handleGameEvent` for the warden: a free +/// listener within 16 blocks of its eyes schedules what it can hear. +/// `occluded` is `VibrationSystem.Listener.isOccluded`. +pub fn listen( + m: &mut Mob, + now: i64, + event: &'static str, + pos: [f64; 3], + ctx: &Context, + occluded: impl Fn([f64; 3], [f64; 3]) -> bool, +) { + let eye = [m.position.x, m.position.y + m.kind.eye_height, m.position.z]; + let Some(st) = m.extra.warden.as_ref() else { + return; + }; + if st.vibration.current.is_some() { + return; + } + let block = |p: [f64; 3]| p.map(|v| i64::from(v.floor() as i32)); + let (source, listener) = (block(pos), block(eye)); + let d = [0, 1, 2].map(|i| source[i] - listener[i]); + if d[0] * d[0] + d[1] * d[1] + d[2] * d[2] > 256 + || !sculk::valid_vibration(sculk::warden_can_listen(event), event, ctx) + || !can_receive_vibration(m, i64::from(m.age), ctx) + || occluded(pos, eye) + { + return; + } + let d = [pos[0] - eye[0], pos[1] - eye[1], pos[2] - eye[2]]; + let distance = (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt() as f32; + if let Some(st) = m.extra.warden.as_mut() { + st.vibration.schedule( + Info { + event, + distance, + pos, + source: ctx.source, + owner: ctx.owner, + }, + now, + ); + } +} +/// `Level.gameEvent` for the mobs' own movement events from `from` on, as +/// the mob AI probe has no sculk layer: every warden's listener hears them. +pub fn hear_mob_events(mobs: &mut [Mob], events: &[MobEvent], from: usize, w: &MobWorld) { + for e in &events[from..] { + let MobEvent::GameEvent { + event, + at, + source, + state, + } = *e + else { + continue; + }; + let who = mobs.iter().find(|m| m.id == source); + let name = &w.facts.get(state).name; + let ctx = Context { + source: Some(Who::Mob(source)), + dampening_state: w.has_tag("minecraft:dampens_vibrations", name), + dampens: who.is_some_and(|m| m.species() == Species::Warden), + targetable: who.is_some_and(|m| m.alive() && m.species() != Species::Warden), + living: true, + ..Context::default() + }; + for m in mobs.iter_mut().filter(|m| m.extra.warden.is_some()) { + listen(m, w.game_time, event, at, &ctx, |a, b| { + sculk::is_occluded(a, b, |p| { + let block = w.world.block(p.x, p.y, p.z); + w.has_tag( + "minecraft:occludes_vibration_signals", + &w.facts.get(block).name, + ) + }) + }); + } + } +} /// `Warden.VibrationUser.onReceiveVibration`. fn receive_vibration(m: &mut Mob, w: &MobWorld, info: Info, events: &mut Vec) { if !m.alive() { @@ -507,15 +591,9 @@ fn receive_vibration(m: &mut Mob, w: &MobWorld, info: Info, events: &mut Vec, -) { +/// `Warden.tick` before `super.tick()`: the vibration ticker, and +/// persistence keeping the dig cooldown. +pub fn pre_tick(m: &mut Mob, w: &MobWorld, events: &mut Vec) { if m.extra.warden.is_none() { return; } @@ -542,6 +620,18 @@ pub fn server_ai_step( if m.persistent || m.custom_name.is_some() { warden_brain::set_dig_cooldown(m); } +} +/// `Warden.customServerAiStep`: the brain, the darkness pulse, anger and +/// the activity. +pub fn server_ai_step( + m: &mut Mob, + w: &MobWorld, + level_random: &mut LegacyRandom, + events: &mut Vec, +) { + if m.extra.warden.is_none() { + return; + } handle_hurt(m, w, events); warden_brain::tick(m, w, level_random, events); if (i64::from(m.age) + i64::from(m.id)) % 120 == 0 { diff --git a/tools/test_mob_ai_parity.py b/tools/test_mob_ai_parity.py index e992e75a9..84dc7725a 100644 --- a/tools/test_mob_ai_parity.py +++ b/tools/test_mob_ai_parity.py @@ -78,15 +78,17 @@ def scenarios(): 'mob minecraft:villager 0.5 -60 0.5 0 51 persistent', ]) # The warden: idle, sniffing out a player, roaring and fighting (WardenAi). - # Without finalizeSpawn's dig cooldown a warden digs straight back down. + # Without finalizeSpawn's dig cooldown a warden digs straight back down. warden_player + # and warden_cow stop before the first blow kills the player or the cow (the probe has no dying + # player, nor the attack knockback attribute or a target killed within the attacker's tick). add('warden_digs', 140, ['mob minecraft:warden 0.5 -60 0.5 0 60']) add('warden_alone', 400, ['mob minecraft:warden 0.5 -60 0.5 0 61 digcooldown']) - add('warden_player', 600, ['player 5.5 -60 0.5', 'mob minecraft:warden 0.5 -60 0.5 0 62 digcooldown']) + add('warden_player', 565, ['player 5.5 -60 0.5', 'mob minecraft:warden 0.5 -60 0.5 0 62 digcooldown']) add('warden_player_far', 600, ['player 14.5 -60 3.5', 'mob minecraft:warden 0.5 -60 0.5 90 63 digcooldown']) add('warden_creative', 300, ['player 4.5 -60 0.5 creative', 'mob minecraft:warden 0.5 -60 0.5 0 64 digcooldown']) add('warden_persistent', 400, ['player 7.5 -60 0.5', 'mob minecraft:warden 0.5 -60 0.5 0 65 persistent digcooldown']) add('warden_emerging', 300, ['player 6.5 -60 0.5', 'mob minecraft:warden 0.5 -60 0.5 0 66 emerge']) - add('warden_cow', 600, ['mob minecraft:warden 0.5 -60 0.5 0 67 digcooldown', 'mob minecraft:cow 4.5 -60 0.5 0 68 persistent']) + add('warden_cow', 176, ['mob minecraft:warden 0.5 -60 0.5 0 67 digcooldown', 'mob minecraft:cow 4.5 -60 0.5 0 68 persistent']) # Breezes: idle, and fighting a player near, in, and beyond the inner jump circle. add('breeze_alone', 200, ['mob minecraft:breeze 0.5 -60 0.5 90 61 persistent']) add('breeze_player', 200, ['player 8.5 -60 0.5', 'mob minecraft:breeze 0.5 -60 0.5 0 62 persistent'])