From 8393b8de0f93b510b0f66662e1124a6c1df78b0d Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 12 Oct 2026 17:15:34 +0200 Subject: [PATCH] Match vanilla breeding sniffers and tempted happy ghasts Mobs set each other's brain memories within the tick (the breed target and lockGazeAndWalkToEachOther), the partner rests and forgets at the birth, and an animal in love draws its heart particle randoms. The adult happy ghast's TemptGoal.ForNonPathfinders ticks every other tick and follows creative players. The mob AI scenarios can give the player an item and a mob love, taming, an age or a llama strength. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/examples/mob_ai_probe.rs | 17 +++++++- crates/horizon-client/src/game/creatures.rs | 10 ++++- crates/horizon-client/src/mobs.rs | 39 +++++++++++++++++++ crates/horizon-client/src/mobs/ai.rs | 15 ++++--- .../horizon-client/src/mobs/animal_brain.rs | 20 ++++++++++ crates/horizon-client/src/mobs/happy_ghast.rs | 16 ++++---- .../horizonlab/parity/VanillaMobAiOracle.java | 15 ++++++- tools/test_mob_ai_parity.py | 15 +++++++ 8 files changed, 131 insertions(+), 16 deletions(-) diff --git a/crates/horizon-client/examples/mob_ai_probe.rs b/crates/horizon-client/examples/mob_ai_probe.rs index 39d3d7437..7d11285be 100644 --- a/crates/horizon-client/examples/mob_ai_probe.rs +++ b/crates/horizon-client/examples/mob_ai_probe.rs @@ -43,6 +43,7 @@ struct Scenario { blocks: Vec>, player: Option<[f64; 3]>, creative: bool, + hand: Option, mobs: Vec, } @@ -79,7 +80,13 @@ fn parse(text: &str) -> Vec { f[2].parse().unwrap(), f[3].parse().unwrap(), ]); - s.creative = f.get(4) == Some(&"creative"); + for x in &f[4..] { + match *x { + "creative" => s.creative = true, + x if x.starts_with("hand=") => s.hand = Some(x[5..].to_owned()), + other => panic!("unknown player option {other}"), + } + } } "mob" => s.mobs.push(MobSpec { kind: f[1].to_owned(), @@ -429,6 +436,10 @@ fn main() -> horizon_client::Result<()> { match flag.as_str() { "persistent" => m.persistent = true, "noai" => m.no_ai = true, + "inlove" => m.extra.in_love = 600, + "tame" => m.extra.tame = true, + x if x.starts_with("age=") => m.extra.age = x[4..].parse().unwrap(), + x if x.starts_with("strength=") => m.extra.variant2 = x[9..].parse().unwrap(), other => panic!("unknown mob flag {other}"), } } @@ -453,7 +464,7 @@ fn main() -> horizon_client::Result<()> { creative: s.creative, sneaking: false, look: [0.0, 0.0, 1.0], - main_hand: None, + main_hand: s.hand.as_deref().map(Into::into), off_hand: None, head: None, armor_cover: 0.0, @@ -500,9 +511,11 @@ fn main() -> horizon_client::Result<()> { end: false, world_seed: 0, }; + let from = events.len(); if !mobs[i].tick(&w, &mut scratch, &mut events) { mobs[i].removed = true; } + horizon_client::mobs::apply_brain_events(&mut mobs, &mut events, from); // 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) { diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index cfa159dd4..09f09b2e1 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -445,8 +445,10 @@ impl Game<'_> { { let mut scratch = self.world.random.clone(); let mut keep = Vec::with_capacity(mobs.len()); - for (i, m) in mobs.iter_mut().enumerate() { + for i in 0..mobs.len() { + let from = events.len(); let w = self.mob_world_base(&snapshot); + let m = &mut mobs[i]; if !w .world .loaded(m.position.x.floor() as i32, m.position.z.floor() as i32) @@ -463,6 +465,7 @@ impl Game<'_> { // after this mob see it as it is now. snapshot[i] = seen(m, uuid); snapshot[i].alive &= kept; + horizon_client::mobs::apply_brain_events(&mut mobs, &mut events, from); } let mut flags = keep.into_iter(); mobs.retain(|_| flags.next().unwrap_or(true)); @@ -858,6 +861,11 @@ impl Game<'_> { } }, MobEvent::Breed { parent, partner } => self.breed(parent, partner), + MobEvent::LockGaze { mob, partner } => { + if let Some(m) = self.mobs.iter_mut().find(|m| m.id == mob) { + horizon_client::mobs::animal_brain::lock_gaze(m, partner); + } + } MobEvent::BreedTarget { mob, partner } => { if let Some(m) = self.mobs.iter_mut().find(|m| m.id == mob) { horizon_client::mobs::animal_brain::set_breed_partner(m, partner); diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 5754bfae9..48a578292 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -629,6 +629,12 @@ pub enum MobEvent { mob: u32, partner: u32, }, + /// `BehaviorUtils.lockGazeAndWalkToEachOther`'s other half: the partner + /// looks at this mob and walks to it. + LockGaze { + mob: u32, + partner: u32, + }, /// A sound event id that is not a static string (variant sounds). SoundId(String, [f64; 3], f32, f32), AttackPlayer(f32, [f64; 3]), @@ -886,6 +892,39 @@ pub struct Seen { /// A villager's reputation of the player (`getPlayerReputation`). pub player_rep: i32, } +/// Applies the brain memories a mob's tick set on other mobs (from event +/// index `from` on), at once: vanilla sets them on the other entity during +/// the tick, before the mobs after it tick. +pub fn apply_brain_events(mobs: &mut [Mob], events: &mut Vec, from: usize) { + let mut i = from; + while i < events.len() { + match events[i] { + MobEvent::BreedTarget { mob, partner } => { + if let Some(m) = mobs.iter_mut().find(|m| m.id == mob) { + animal_brain::set_breed_partner(m, partner); + } + events.remove(i); + } + MobEvent::LockGaze { mob, partner } => { + if let Some(m) = mobs.iter_mut().find(|m| m.id == mob) { + animal_brain::lock_gaze(m, partner); + } + events.remove(i); + } + // Animal.finalizeSpawnChildFromBreeding: the partner rests and + // forgets it at once (the game still makes the baby). + MobEvent::Breed { parent, partner } => { + if let Some(m) = mobs.iter_mut().find(|m| m.id == partner) { + m.extra.age = 6000; + m.extra.in_love = 0; + animal_brain::forget_breed_target(m, parent); + } + i += 1; + } + _ => i += 1, + } + } +} /// The player as mobs see it. #[derive(Clone, Debug)] pub struct PlayerView { diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index b174d3f1a..792d61eee 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -552,13 +552,18 @@ fn ai_step(m: &mut Mob, w: &MobWorld, level_random: &mut LegacyRandom, events: & if m.extra.in_love > 0 { m.extra.in_love -= 1; if m.extra.in_love % 10 == 0 { + // The heart's speed and getRandomX/Y/Z, drawn even on the server. + let (w, h) = (f64::from(m.width()), f64::from(m.height())); + let r = &mut m.random; + for _ in 0..3 { + let _ = r.next_gaussian(); + } + let x = m.position.x + w * (2.0 * r.next_double() - 1.0); + let y = m.position.y + h * r.next_double() + 0.5; + let z = m.position.z + w * (2.0 * r.next_double() - 1.0); events.push(MobEvent::Particles { kind: "heart", - at: [ - m.position.x, - m.position.y + f64::from(m.height()) + 0.5, - m.position.z, - ], + at: [x, y, z], count: 1, }); } diff --git a/crates/horizon-client/src/mobs/animal_brain.rs b/crates/horizon-client/src/mobs/animal_brain.rs index ad2952891..0c7bd6983 100644 --- a/crates/horizon-client/src/mobs/animal_brain.rs +++ b/crates/horizon-client/src/mobs/animal_brain.rs @@ -369,6 +369,20 @@ pub fn set_breed_partner(m: &mut Mob, partner: u32) { lock_gaze_and_walk(m, who, 1.0, 2); } +/// `AnimalMakeLove.tick` after the birth: the partner forgets this mob. +pub fn forget_breed_target(m: &mut Mob, partner: u32) { + if m.extra.animal_brain.is_some() && mem(m).breed_target.value() == Some(Who::Mob(partner)) { + mem(m).breed_target.clear(); + } +} +/// The partner's half of `lockGazeAndWalkToEachOther`. +pub fn lock_gaze(m: &mut Mob, partner: u32) { + if m.extra.animal_brain.is_none() { + return; + } + lock_gaze_and_walk(m, Who::Mob(partner), 1.0, 2); +} + /// What behaviours read and emit during a brain tick. pub struct Bx<'a, 'w> { pub w: &'a MobWorld<'w>, @@ -1282,6 +1296,12 @@ impl B { return; } lock_gaze_and_walk(m, partner, 1.0, 2); + if let Who::Mob(id) = partner { + cx.events.push(MobEvent::LockGaze { + mob: id, + partner: m.id, + }); + } if let Some(t) = cx.w.target_info(partner) && m.distance_sqr(t.position) < 9.0 && now >= *spawn_at diff --git a/crates/horizon-client/src/mobs/happy_ghast.rs b/crates/horizon-client/src/mobs/happy_ghast.rs index 963915902..b09383d67 100644 --- a/crates/horizon-client/src/mobs/happy_ghast.rs +++ b/crates/horizon-client/src/mobs/happy_ghast.rs @@ -338,22 +338,24 @@ impl Goal for GhastTempt { return false; } let Some(p) = &cx.w.player else { return false }; - // Attributes.TEMPT_RANGE 16. + // getNearestPlayer(TargetingConditions.forNonCombat() + // .ignoreLineOfSight().range(TEMPT_RANGE 16)): creative players too. + let visibility = if p.sneaking { 0.8 } else { 1.0 }; + let range = (16.0_f64 * visibility).max(2.0); p.alive - && !p.creative + && !p.spectator && (Self::wants(m, &p.main_hand) || Self::wants(m, &p.off_hand)) - && m.distance_sqr(p.position) <= 256.0 + && m.distance_sqr(p.position) <= range * range } fn stop(&mut self, m: &mut Mob, _: &mut Cx) { + // stopNavigation: setWait; calmDown = reducedTickDelay(100). m.ai.mov.operation = Operation::Wait; - self.calm_down = 50; - } - fn every_tick(&self) -> bool { - true + self.calm_down = super::goals::reduced(100); } fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { let Some(p) = cx.w.player.clone() else { return }; m.look_at(p.eye); + // stopDistance 7. if m.distance_sqr(p.position) < 49.0 { m.ai.mov.operation = Operation::Wait; } else { diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaMobAiOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaMobAiOracle.java index 782c5d698..d658c1430 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaMobAiOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaMobAiOracle.java @@ -155,6 +155,8 @@ public final class VanillaMobAiOracle { List blocks = new ArrayList<>(); double[] player; boolean creative; + /** What the player holds in its main hand (an item id), or null. */ + String hand; List mobs = new ArrayList<>(); long levelSeed = 0; } @@ -185,7 +187,11 @@ public final class VanillaMobAiOracle { case "fill", "block" -> s.blocks.add(f); case "player" -> { s.player = new double[] {Double.parseDouble(f[1]), Double.parseDouble(f[2]), Double.parseDouble(f[3])}; - s.creative = f.length > 4 && f[4].equals("creative"); + for (int i = 4; i < f.length; i++) { + if (f[i].equals("creative")) s.creative = true; + else if (f[i].startsWith("hand=")) s.hand = f[i].substring(5); + else throw new IllegalArgumentException(line); + } } case "mob" -> { MobSpec m = new MobSpec(); @@ -426,6 +432,8 @@ public final class VanillaMobAiOracle { this.getPlayerList().placeNewPlayer(connection, player, cookie); } player.setGameMode(s.creative ? GameType.CREATIVE : GameType.SURVIVAL); + player.setItemInHand(net.minecraft.world.InteractionHand.MAIN_HAND, s.hand == null ? net.minecraft.world.item.ItemStack.EMPTY + : new net.minecraft.world.item.ItemStack(BuiltInRegistries.ITEM.getValue(net.minecraft.resources.Identifier.parse(s.hand)))); player.snapTo(s.player[0], s.player[1], s.player[2], 0.0F, 0.0F); player.setDeltaMovement(Vec3.ZERO); } else if (player != null) { @@ -450,6 +458,11 @@ public final class VanillaMobAiOracle { for (String flag : spec.flags) { if (flag.equals("persistent")) mob.setPersistenceRequired(); else if (flag.equals("noai")) mob.setNoAi(true); + else if (flag.equals("inlove")) ((net.minecraft.world.entity.animal.Animal) mob).setInLoveTime(600); + else if (flag.equals("tame") && mob instanceof net.minecraft.world.entity.animal.equine.AbstractHorse horse) horse.setTamed(true); + else if (flag.startsWith("age=")) ((AgeableMob) mob).setAge(Integer.parseInt(flag.substring(4))); + else if (flag.startsWith("strength=")) method(net.minecraft.world.entity.animal.equine.Llama.class, "setStrength", int.class) + .invoke(mob, Integer.parseInt(flag.substring(9))); else throw new IllegalArgumentException(flag); } level.addFreshEntity(mob); diff --git a/tools/test_mob_ai_parity.py b/tools/test_mob_ai_parity.py index 957605492..5c43c123f 100644 --- a/tools/test_mob_ai_parity.py +++ b/tools/test_mob_ai_parity.py @@ -94,6 +94,21 @@ def scenarios(): add('ghastling_alone', 200, ['mob minecraft:happy_ghast 0.5 -57 0.5 0 84 persistent baby']) add('ghastling_adult', 200, ['mob minecraft:happy_ghast 10.5 -55 0.5 0 85 persistent', 'mob minecraft:happy_ghast 0.5 -57 0.5 0 86 persistent baby']) + # Breeding, temptation and growing up. + add('sniffer_love', 300, ['mob minecraft:sniffer 0.5 -60 0.5 0 91 persistent inlove', + 'mob minecraft:sniffer 4.5 -60 0.5 0 92 persistent inlove']) + add('sniffer_tempted', 200, ['player 7.5 -60 0.5 hand=minecraft:torchflower_seeds', + 'mob minecraft:sniffer 0.5 -60 0.5 0 93 persistent']) + add('llama_love', 300, ['mob minecraft:llama 0.5 -60 0.5 0 94 persistent tame inlove strength=3', + 'mob minecraft:llama 4.5 -60 0.5 0 95 persistent tame inlove strength=2']) + add('llama_tempted', 200, ['player 7.5 -60 0.5 hand=minecraft:hay_block', + 'mob minecraft:llama 0.5 -60 0.5 0 96 persistent']) + add('ghastling_tempted', 200, ['player 8.5 -60 0.5 hand=minecraft:snowball', + 'mob minecraft:happy_ghast 0.5 -57 0.5 0 97 persistent baby']) + add('happy_ghast_tempted', 200, ['player 12.5 -60 0.5 hand=minecraft:snowball', + 'mob minecraft:happy_ghast 0.5 -55 0.5 0 98 persistent']) + add('ghastling_grows', 200, ['player 8.5 -60 0.5', + 'mob minecraft:happy_ghast 0.5 -57 0.5 0 99 persistent age=-40']) return s