From 5352b593585b9183b41dea779076005d8b66bd37 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 12 Oct 2026 16:36:44 +0200 Subject: [PATCH] Tick the sniffer's and the ghastling's brains The level random is passed down to the mob AI step as the integration branch does, the brain runs in customServerAiStep's place, and the mob AI probe builds the brain after seeding the mob and prints its activities and memories. The oracle seeds every GateBehavior's shuffle with the mob's seed (the same change the entity-ai branch makes) so RunOne picks are reproducible. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/examples/mob_ai_probe.rs | 11 +++++- crates/horizon-client/src/mobs.rs | 4 +-- crates/horizon-client/src/mobs/ai.rs | 30 ++++++++++++---- .../horizonlab/parity/VanillaMobAiOracle.java | 36 +++++++++++++++++++ 4 files changed, 72 insertions(+), 9 deletions(-) diff --git a/crates/horizon-client/examples/mob_ai_probe.rs b/crates/horizon-client/examples/mob_ai_probe.rs index 261eef40f..39d3d7437 100644 --- a/crates/horizon-client/examples/mob_ai_probe.rs +++ b/crates/horizon-client/examples/mob_ai_probe.rs @@ -316,7 +316,11 @@ fn print(out: &mut String, t: i32, i: usize, m: &Mob) { f(m.ai.speed), u8::from(m.ai.jumping) ); - let _ = writeln!(out, "\trng {:x}", m.random.state()); + let _ = write!(out, "\trng {:x}", m.random.state()); + if let Some((acts, mem)) = horizon_client::mobs::animal_brain::describe(m) { + let _ = write!(out, "\tact {acts}\tmem {mem}"); + } + out.push('\n'); } fn main() -> horizon_client::Result<()> { @@ -429,6 +433,11 @@ fn main() -> horizon_client::Result<()> { } } m.random = LegacyRandom::new(spec.seed); + // The oracle seeds the random after the brain is built, then + // delays the sensors again and seeds the gates' shuffles. + if horizon_client::mobs::animal_brain::kind_of(&m).is_some() { + horizon_client::mobs::animal_brain::init(&mut m, spec.seed); + } mobs.push(m); } let player = s.player.map(|p| { diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 34ccac776..5754bfae9 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -1173,10 +1173,10 @@ impl Mob { pub fn tick( &mut self, w: &MobWorld, - _random: &mut LegacyRandom, + random: &mut LegacyRandom, events: &mut Vec, ) -> bool { - ai::tick(self, w, events) + ai::tick(self, w, random, events) } pub fn distance_to(&self, p: [f64; 3]) -> f64 { let d = [ diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 2d0a32a80..d1415d73e 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -12,7 +12,10 @@ use super::{ undead, }; use crate::blocks::Fluid; -use horizon_core::{geometry::Vec3, random::RandomSource}; +use horizon_core::{ + geometry::Vec3, + random::{LegacyRandom, RandomSource}, +}; #[derive(Clone, Debug, Default)] pub struct Ai { @@ -264,7 +267,12 @@ fn generic(m: &mut Mob) { } } /// One full entity tick. Returns false when the mob is gone. -pub fn tick(m: &mut Mob, w: &MobWorld, events: &mut Vec) -> bool { +pub fn tick( + m: &mut Mob, + w: &MobWorld, + level_random: &mut LegacyRandom, + events: &mut Vec, +) -> bool { if m.removed { return false; } @@ -299,7 +307,7 @@ pub fn tick(m: &mut Mob, w: &MobWorld, events: &mut Vec) -> bool { return false; } if !m.no_ai { - ai_step(m, w, events); + ai_step(m, w, level_random, events); } // Mob.tickHeadTurn -> BodyRotationControl.clientTick if m.alive() { @@ -489,7 +497,7 @@ fn play_ambient(m: &mut Mob, w: &MobWorld, events: &mut Vec) { } } /// LivingEntity.aiStep (and Mob/AgeableMob/Animal additions). -fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { +fn ai_step(m: &mut Mob, w: &MobWorld, level_random: &mut LegacyRandom, events: &mut Vec) { // AbstractHorse.aiStep: the tail swishes now and then (moveTail). if super::equine::is_equine(m.species()) { let _ = m.random.next_int_bound(200); @@ -513,7 +521,7 @@ fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { let immobile = m.species() == Species::Ravager && super::ravager::immobile(m) || super::villager::sleeping(m); if m.alive() && !immobile { - server_ai_step(m, w, events); + server_ai_step(m, w, level_random, events); } else { m.ai.jumping = false; m.ai.xxa = 0.0; @@ -576,7 +584,12 @@ fn sun_burn_tick(m: &mut Mob, w: &MobWorld) -> bool { br > 0.5 && m.random.next_float() * 30.0 < (br - 0.4) * 2.0 && !wet && w.can_see_sky(x, y, z) } /// Mob.serverAiStep -fn server_ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { +fn server_ai_step( + m: &mut Mob, + w: &MobWorld, + level_random: &mut LegacyRandom, + events: &mut Vec, +) { m.ai.no_action_time += 1; // A creaking looked at stands still. if super::creaking::is_creaking(m) && !super::creaking::can_move(m) { @@ -605,6 +618,11 @@ fn server_ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { m.ai.targets = targets; m.ai.goals = goals; m.nav_tick(w); + // Sniffer and HappyGhast.customServerAiStep: the brain (a happy ghast's + // only while it is a ghastling), then updateActivity. + if m.species() == Species::Sniffer || m.species() == Species::HappyGhast && m.is_baby() { + super::animal_brain::tick(m, w, level_random, events); + } { let mut cx = Cx { w, events }; custom_server_ai_step(m, &mut cx); 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 ca1b2f299..782c5d698 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaMobAiOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaMobAiOracle.java @@ -14,6 +14,7 @@ import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.UUID; import java.util.function.BooleanSupplier; import net.minecraft.SharedConstants; @@ -65,7 +66,10 @@ import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.AgeableMob; import net.minecraft.world.entity.ai.Brain; +import net.minecraft.world.entity.ai.behavior.BehaviorControl; import net.minecraft.world.entity.ai.behavior.BlockPosTracker; +import net.minecraft.world.entity.ai.behavior.GateBehavior; +import net.minecraft.world.entity.ai.behavior.ShufflingList; import net.minecraft.world.entity.ai.behavior.EntityTracker; import net.minecraft.world.entity.ai.control.LookControl; import net.minecraft.world.entity.ai.control.MoveControl; @@ -428,6 +432,8 @@ public final class VanillaMobAiOracle { player.snapTo(s.originX + 5000, -60, s.originZ, 0.0F, 0.0F); } List mobs = new ArrayList<>(); + List> brains = new ArrayList<>(); + List seeds = new ArrayList<>(); int id = 100000; for (MobSpec spec : s.mobs) { EntityType type = BuiltInRegistries.ENTITY_TYPE.getValue(net.minecraft.resources.Identifier.parse(spec.type)); @@ -452,7 +458,10 @@ public final class VanillaMobAiOracle { @SuppressWarnings("unchecked") Map> sensors = (Map>) get(brain, "sensors"); for (Sensor sensor : sensors.values()) sensor.randomlyDelayStart(mob.getRandom()); + seedGates(brain, spec.seed); mobs.add(mob); + brains.add(brain); + seeds.add(spec.seed); } level.getRandom().setSeed(s.levelSeed); SOUNDS.clear(); @@ -474,6 +483,13 @@ public final class VanillaMobAiOracle { TracingRandom.tick = t; TracingRandom.on = tracing(s) && t >= traceFrom() && t <= traceTo(); SOUNDS.clear(); + for (int i = 0; i < mobs.size(); i++) { + // Villager.refreshBrain builds a new brain with new gates: seed those too. + if (mobs.get(i).getBrain() != brains.get(i)) { + brains.set(i, mobs.get(i).getBrain()); + seedGates(brains.get(i), seeds.get(i)); + } + } for (Mob mob : mobs) { if (mob.isRemoved()) continue; mob.checkDespawn(); @@ -491,6 +507,26 @@ public final class VanillaMobAiOracle { out.flush(); } + /** + * Every GateBehavior (RunOne and the gates of TradeWithVillager and VillagerMakeLove) shuffles + * its behaviours with its own ShufflingList random, which RandomSource.create() seeds from the + * clock: give each a LegacyRandomSource seeded with the mob's seed so runs are reproducible. + */ + static void seedGates(Brain brain, long seed) throws Exception { + @SuppressWarnings("unchecked") + Map>>> byPriority = (Map>>>) get(brain, "availableBehaviorsByPriority"); + for (Map>> byActivity : byPriority.values()) + for (Set> behaviors : byActivity.values()) + for (BehaviorControl behavior : behaviors) seedGate(behavior, seed); + } + + static void seedGate(Object behavior, long seed) throws Exception { + if (!(behavior instanceof GateBehavior)) return; + ShufflingList list = (ShufflingList) field(GateBehavior.class, "behaviors").get(behavior); + field(ShufflingList.class, "random").set(list, new net.minecraft.world.level.levelgen.LegacyRandomSource(seed)); + for (Object child : list) seedGate(child, seed); + } + static String who(Entity e) { if (e == null) return "none"; if (e instanceof ServerPlayer) return "player";