From df0c1ad36e72fcb905bd4b58eaff680218ceeb54 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Tue, 13 Oct 2026 00:16:52 +0200 Subject: [PATCH] Seed a summoned warden before its first tick, and keep its heartbeat off the server random The shrieker summons the warden during the block ticks, so it ticks in the same tick's entity phase: the oracle and the fixture now seed it (its random, sensor delays and RunOne shuffles) before that phase, not after. The heartbeat is a client sound: its pitch no longer draws from the level random, and the fixture leaves it out as vanilla's server never sends it. sculk: 435/435. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/creatures.rs | 5 +- .../horizon-client/src/game/sculk_fixture.rs | 50 ++++++++++++++--- crates/horizon-client/src/mobs/warden.rs | 5 +- .../horizon-client/src/mobs/warden_brain.rs | 56 ++++++++++++++----- .../horizonlab/parity/VanillaSculkOracle.java | 42 +++++++++++--- 5 files changed, 128 insertions(+), 30 deletions(-) diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 94231b9be..d7f710d40 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -698,7 +698,10 @@ impl Game<'_> { volume, spread, } => { - let r = &mut self.world.random; + // The client's own level random: nothing drawn from the server's. + let mut r = horizon_core::random::LegacyRandom::new( + self.world.game_time.wrapping_mul(341_873_128_712), + ); let pitch = (r.next_float() - r.next_float()) * spread + 1.0; self.audio .play(&format!("minecraft:{sound}"), Some(at), volume, pitch) diff --git a/crates/horizon-client/src/game/sculk_fixture.rs b/crates/horizon-client/src/game/sculk_fixture.rs index 740d42e93..2cdb820b0 100644 --- a/crates/horizon-client/src/game/sculk_fixture.rs +++ b/crates/horizon-client/src/game/sculk_fixture.rs @@ -130,6 +130,13 @@ fn parse(text: &str) -> Vec { out } +/// An entity's own random is unseeded in vanilla, and so are its sensors' +/// first delays and its brain's `RunOne` shuffles: the oracle seeds what +/// appeared, and so does this. +fn seed_fresh(m: &mut Mob, seed: i64) { + m.random = LegacyRandom::new(seed); + horizon_client::mobs::warden::init_brain(m, seed); +} fn d(v: f64) -> String { format!("{:x}", v.to_bits()) } @@ -138,10 +145,12 @@ fn f(v: f32) -> String { } /// Sounds the client plays for level events (3006 sculk charge, 3007 -/// shriek): vanilla's server never sends them. +/// shriek) and for itself (the warden's heartbeat): vanilla's server never +/// sends them. const CLIENT_SOUNDS: &[&str] = &[ "minecraft:block.sculk_shrieker.shriek", "minecraft:block.sculk.charge", + "minecraft:entity.warden.heartbeat", ]; impl Game<'_> { @@ -469,6 +478,7 @@ impl Game<'_> { self.world.random = LegacyRandom::new(s.level_seed); self.audio.capture = Some(Vec::new()); let mut known: HashSet = self.mobs.iter().map(|m| m.id).collect(); + let mut seeded: HashSet = HashSet::new(); let mut known_items = self.dropped.len(); let mut known_orbs = self.orbs.len(); let mut last: HashMap<[i32; 3], String> = HashMap::new(); @@ -478,7 +488,7 @@ impl Game<'_> { for t in 0..=s.ticks { dispense_fixture::start_recording(); if t > 0 { - self.sculk_fixture_tick(t, s, &sources)?; + self.sculk_fixture_tick(t, s, &sources, &known, names.len(), &mut seeded)?; } let mut lines: Vec = Vec::new(); for p in &s.watch { @@ -540,12 +550,11 @@ impl Game<'_> { for (id, line) in fresh { known.insert(id); names.insert(id, format!("n{}", names.len())); - // An entity's own random is unseeded in vanilla: the oracle - // seeds what appeared, and so does this. let seed = 0x5eed + names.len() as i64; - if let Some(m) = self.mobs.iter_mut().find(|m| m.id == id) { - m.random = LegacyRandom::new(seed); - horizon_client::mobs::warden::delay_sensors(m); + if seeded.insert(id) { + if let Some(m) = self.mobs.iter_mut().find(|m| m.id == id) { + seed_fresh(m, seed); + } } lines.push(line); } @@ -599,7 +608,15 @@ impl Game<'_> { } /// One `ServerLevel.tick` of a scenario. - fn sculk_fixture_tick(&mut self, t: i32, s: &Scenario, sources: &[u32]) -> Result<()> { + fn sculk_fixture_tick( + &mut self, + t: i32, + s: &Scenario, + sources: &[u32], + known: &HashSet, + named: usize, + seeded: &mut HashSet, + ) -> Result<()> { self.world.game_time += 1; // Scheduled block and fluid ticks, then block events. self.tick_scheduled()?; @@ -662,6 +679,23 @@ impl Game<'_> { other => return Err(format!("unknown action {other}").into()), } } + // Mobs that appeared during the block ticks (a summoned warden) tick + // in this tick's entity phase: seed them now, as they are seeded + // after the tick. + let mut born: Vec = self + .mobs + .iter() + .map(|m| m.id) + .filter(|id| !known.contains(id)) + .collect(); + born.sort_unstable(); + for (i, id) in born.into_iter().enumerate() { + if seeded.insert(id) { + if let Some(m) = self.mobs.iter_mut().find(|m| m.id == id) { + seed_fresh(m, 0x5eed + (named + i + 1) as i64); + } + } + } // Entities: the player's warden spawn tracker, then the mobs. self.tick_sculk_entities(); // (ServerChunkCache.tick, natural spawning and its chunk shuffle, is not run.) diff --git a/crates/horizon-client/src/mobs/warden.rs b/crates/horizon-client/src/mobs/warden.rs index 44ef96dd0..546952d9c 100644 --- a/crates/horizon-client/src/mobs/warden.rs +++ b/crates/horizon-client/src/mobs/warden.rs @@ -523,7 +523,10 @@ pub fn server_ai_step( .extra .warden .as_mut() - .map(|st| st.vibration.tick(w.game_time, true, false, |d| d.floor() as i32)) + .map(|st| { + st.vibration + .tick(w.game_time, true, false, |d| d.floor() as i32) + }) .unwrap_or_default(); if let Some((info, travel)) = tick.started { let eye = [m.position.x, m.position.y + m.kind.eye_height, m.position.z]; diff --git a/crates/horizon-client/src/mobs/warden_brain.rs b/crates/horizon-client/src/mobs/warden_brain.rs index a513bd6b7..a79fe1477 100644 --- a/crates/horizon-client/src/mobs/warden_brain.rs +++ b/crates/horizon-client/src/mobs/warden_brain.rs @@ -324,23 +324,43 @@ fn activities(seed: i64) -> Vec { speed: 0.0, }, ), - (5, Act::Emerge, B::Emerging(Timed::new(EMERGE_DURATION, EMERGE_DURATION))), + ( + 5, + Act::Emerge, + B::Emerging(Timed::new(EMERGE_DURATION, EMERGE_DURATION)), + ), (0, Act::Dig, B::ForceUnmount(Timed::new(60, 60))), - (1, Act::Dig, B::Digging(Timed::new(DIGGING_DURATION, DIGGING_DURATION))), + ( + 1, + Act::Dig, + B::Digging(Timed::new(DIGGING_DURATION, DIGGING_DURATION)), + ), (10, Act::Idle, B::SetRoarTarget(false)), (11, Act::Idle, B::TryToSniff(false)), (12, Act::Idle, B::RunOne(Box::new(gate))), - (10, Act::Roar, B::Roar(Timed::new(ROAR_DURATION, ROAR_DURATION))), + ( + 10, + Act::Roar, + B::Roar(Timed::new(ROAR_DURATION, ROAR_DURATION)), + ), (10, Act::Fight, B::DigCooldownSetter(false)), (11, Act::Fight, B::StopAttackingIfTargetInvalid(false)), (12, Act::Fight, B::SetEntityLookTarget(false)), (13, Act::Fight, B::SetWalkTargetFromAttackTarget(false)), - (14, Act::Fight, B::SonicBoom(Timed::new(SONIC_DURATION, SONIC_DURATION))), + ( + 14, + Act::Fight, + B::SonicBoom(Timed::new(SONIC_DURATION, SONIC_DURATION)), + ), (15, Act::Fight, B::MeleeAttack(false)), (5, Act::Investigate, B::SetRoarTarget(false)), (6, Act::Investigate, B::GoToDisturbance(false)), (5, Act::Sniff, B::SetRoarTarget(false)), - (6, Act::Sniff, B::Sniffing(Timed::new(SNIFFING_DURATION, SNIFFING_DURATION))), + ( + 6, + Act::Sniff, + B::Sniffing(Timed::new(SNIFFING_DURATION, SNIFFING_DURATION)), + ), ]; // A stable sort keeps each activity's own order. list.sort_by_key(|(prio, act, _)| (*prio, act.slot(), act.order())); @@ -561,7 +581,8 @@ pub(super) fn set_disturbance(m: &mut Mob, w: &MobWorld, position: [i32; 3]) { mm.sniff_cooldown.set_ttl((), DISTURBANCE_EXPIRY); mm.look_target .set_ttl(Tracker::Block(position), DISTURBANCE_EXPIRY); - mm.disturbance_location.set_ttl(position, DISTURBANCE_EXPIRY); + mm.disturbance_location + .set_ttl(position, DISTURBANCE_EXPIRY); mm.walk_target.clear(); } /// `Warden.setAttackTarget`. @@ -977,10 +998,9 @@ impl B { let Some(target) = mm.attack_target.value() else { return false; }; - let close = cx - .w - .target_info(target) - .is_some_and(|i| closer_than(m, i.position, 15.0, 20.0)); + let close = + cx.w.target_info(target) + .is_some_and(|i| closer_than(m, i.position, 15.0, 20.0)); if !close { return false; } @@ -1288,7 +1308,11 @@ pub fn describe(m: &Mob) -> Option<(String, String)> { let unit = |_: &()| "u".to_string(); let list = |l: &Vec| format!("n{}", l.len()); slot!("nearest_players", mm.nearest_players, list); - slot!("nearest_visible_player", mm.nearest_visible_player, |e: &Who| who(*e)); + slot!( + "nearest_visible_player", + mm.nearest_visible_player, + |e: &Who| who(*e) + ); slot!( "nearest_visible_targetable_player", mm.nearest_visible_targetable_player, @@ -1303,7 +1327,9 @@ pub fn describe(m: &Mob) -> Option<(String, String)> { slot!("visible_mobs", mm.visible_mobs, |_: &Visible| { "NearestVisibleLivingEntities".to_string() }); - slot!("nearest_attackable", mm.nearest_attackable, |e: &Who| who(*e)); + slot!("nearest_attackable", mm.nearest_attackable, |e: &Who| who( + *e + )); slot!("look_target", mm.look_target, |t: &Tracker| tracker(*t)); slot!("walk_target", mm.walk_target, |t: &WalkTarget| format!( "{}/{:x}/{}", @@ -1336,7 +1362,11 @@ pub fn describe(m: &Mob) -> Option<(String, String)> { slot!("touch_cooldown", mm.touch_cooldown, unit); slot!("vibration_cooldown", mm.vibration_cooldown, unit); slot!("sonic_boom_cooldown", mm.sonic_boom_cooldown, unit); - slot!("sonic_boom_sound_cooldown", mm.sonic_boom_sound_cooldown, unit); + slot!( + "sonic_boom_sound_cooldown", + mm.sonic_boom_sound_cooldown, + unit + ); slot!("sonic_boom_sound_delay", mm.sonic_boom_sound_delay, unit); out.sort(); Some((acts.join("|"), out.join(" "))) diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaSculkOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaSculkOracle.java index 137d7a4a3..3b9127379 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaSculkOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaSculkOracle.java @@ -269,6 +269,32 @@ public final class VanillaSculkOracle { static Object get(Object o, String name) throws Exception { return field(o.getClass(), name).get(o); } + /** + * A mob's own random, its sensors' first delays and its gates' shuffles (RunOne's ShufflingList) + * are unseeded: seed what appeared, as Horizon does. + */ + static void seedFresh(Mob mob, long seed) throws Exception { + mob.getRandom().setSeed(seed); + @SuppressWarnings("unchecked") + Map> sensors = + (Map>) get(mob.getBrain(), "sensors"); + for (var sensor : sensors.values()) sensor.randomlyDelayStart(mob.getRandom()); + @SuppressWarnings("unchecked") + Map>>> byPriority = + (Map>>>) get(mob.getBrain(), "availableBehaviorsByPriority"); + for (var byActivity : byPriority.values()) + for (var behaviors : byActivity.values()) + for (var behavior : behaviors) seedGate(behavior, seed); + } + + static void seedGate(Object behavior, long seed) throws Exception { + if (!(behavior instanceof net.minecraft.world.entity.ai.behavior.GateBehavior)) return; + var list = (net.minecraft.world.entity.ai.behavior.ShufflingList) + field(net.minecraft.world.entity.ai.behavior.GateBehavior.class, "behaviors").get(behavior); + field(net.minecraft.world.entity.ai.behavior.ShufflingList.class, "random").set(list, new LegacyRandomSource(seed)); + for (Object child : list) seedGate(child, seed); + } + static long seedOf(Object random) throws Exception { return ((java.util.concurrent.atomic.AtomicLong) field(LegacyRandomSource.class, "seed").get(random)).get(); } @@ -538,6 +564,7 @@ public final class VanillaSculkOracle { Method managerTick = method(manager.getClass(), "tick"); managerTick.invoke(manager); Set known = new HashSet<>(); + Set seeded = new HashSet<>(); for (Entity e : allEntities(level)) known.add(e); if (System.getenv("SCULK_TRACE") != null) field(Level.class, "random").set(level, new TracingRandom(0L)); level.getRandom().setSeed(s.levelSeed); @@ -563,6 +590,13 @@ public final class VanillaSculkOracle { }); runBlockEvents.invoke(level); for (Action a : s.actions) if (a.tick == t) act(level, a.words, mobs); + // Mobs that appeared during the block ticks (a summoned warden) tick in this + // tick's entity phase: seed them now, as they are seeded below. + List born = new ArrayList<>(); + for (Entity e : allEntities(level)) if (!known.contains(e)) born.add(e); + born.sort(Comparator.comparingInt(Entity::getId)); + for (int i = 0; i < born.size(); i++) + if (born.get(i) instanceof Mob mob && seeded.add(mob)) seedFresh(mob, 0x5eedL + names.size() + i + 1); // The player first (it joined first): only its warden spawn tracker. player.getWardenSpawnTracker().tick(); tickList.forEach(e -> { @@ -611,13 +645,7 @@ public final class VanillaSculkOracle { known.add(e); names.put(e, "n" + names.size()); // An entity's own random is unseeded: seed what appeared, as Horizon does. - if (e instanceof Mob mob) { - mob.getRandom().setSeed(0x5eedL + names.size()); - @SuppressWarnings("unchecked") - Map> sensors = - (Map>) get(mob.getBrain(), "sensors"); - for (var sensor : sensors.values()) sensor.randomlyDelayStart(mob.getRandom()); - } + if (e instanceof Mob mob && seeded.add(mob)) seedFresh(mob, 0x5eedL + names.size()); if (e instanceof net.minecraft.world.entity.ExperienceOrb orb) { // Where an orb flies comes from its own unseeded random: its value only. lines.add("new minecraft:experience_orb value=" + orb.getValue());