From b92dd805668a9a6a3ab5deee65054fcad0c54407 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Mon, 26 Oct 2026 18:55:02 +0100 Subject: [PATCH] Let silverfish hide in stone and wake their friends when hurt --- crates/horizon-client/src/game/creatures.rs | 42 +++++ crates/horizon-client/src/game/spawner.rs | 2 +- crates/horizon-client/src/mobs.rs | 8 + crates/horizon-client/src/mobs/ai.rs | 6 +- crates/horizon-client/src/mobs/hostile.rs | 150 +++++++++++++++++- .../src/mobs/spawn_selection.rs | 2 +- 6 files changed, 206 insertions(+), 4 deletions(-) diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 2b65a3289..6f5937a81 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -1065,8 +1065,50 @@ impl Game<'_> { self.world.set(pos[0], pos[1], pos[2], state)?; } } + MobEvent::Infest { + pos, + infested, + poof, + } => { + let registry = &self.pack.assets.registry; + let old = self.world.block(pos[0], pos[1], pos[2]); + if let Some(mut state) = registry.state(old).cloned() { + let (from, to) = if infested { + ("minecraft:", "minecraft:infested_") + } else { + ("minecraft:infested_", "minecraft:") + }; + state.name = state.name.replacen(from, to, 1); + if let Ok(id) = registry.resolve(&state) { + self.world.set(pos[0], pos[1], pos[2], id)?; + } + } + if let Some(mob) = poof { + self.mob_poof(mob); + } + } MobEvent::BreakBlock { pos, drop } => { let old = self.world.block(pos[0], pos[1], pos[2]); + // InfestedBlock.spawnAfterBreak: its silverfish comes out + // when blocks drop. + if drop + && self + .res + .facts() + .get(old) + .name + .starts_with("minecraft:infested_") + && self.ui.rules.bool("block_drops") + { + let at = [ + f64::from(pos[0]) + 0.5, + f64::from(pos[1]), + f64::from(pos[2]) + 0.5, + ]; + if let Some(id) = self.spawn_mob("minecraft:silverfish", at, false) { + self.mob_poof(id); + } + } // Block.spawnDestroyParticles: fire breaks leave nothing. if !self.res.facts().get(old).has_class("BaseFireBlock") { self.destroy_particles(pos, old); diff --git a/crates/horizon-client/src/game/spawner.rs b/crates/horizon-client/src/game/spawner.rs index 538cb39e7..5dd907afb 100644 --- a/crates/horizon-client/src/game/spawner.rs +++ b/crates/horizon-client/src/game/spawner.rs @@ -253,7 +253,7 @@ impl Game<'_> { self.world.modified.insert((key.0 >> 4, key.2 >> 4)); } /// `LivingEntity.makePoofParticles` (entity event 20) for a mob. - fn mob_poof(&mut self, id: u32) { + pub(super) fn mob_poof(&mut self, id: u32) { let Some(m) = self.mobs.iter().find(|m| m.id == id) else { return; }; diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 00c21633e..7bf034632 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -1009,6 +1009,14 @@ pub enum MobEvent { pos: [i32; 3], open: bool, }, + /// `InfestedBlock.infestedStateByHost` (`infested`) or `hostStateByInfested` + /// for the block there, keeping its axis; `poof` is the silverfish that + /// hid in it (`spawnAnim`). + Infest { + pos: [i32; 3], + infested: bool, + poof: Option, + }, /// Destroy a block, dropping nothing (sheep eating grass becomes dirt via SetBlock). BreakBlock { pos: [i32; 3], diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 2676c3a37..fe3cdafb3 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -121,7 +121,7 @@ pub fn family(s: Species) -> Family { | S::Parched | S::WitherSkeleton | S::ZombifiedPiglin => Family::Undead, - S::Creeper | S::Spider | S::CaveSpider => Family::Hostile, + S::Creeper | S::Spider | S::CaveSpider | S::Silverfish => Family::Hostile, S::Squid | S::GlowSquid | S::Cod @@ -1306,6 +1306,10 @@ pub fn accepts_damage(m: &mut Mob, source: &Damage, _amount: f32) -> bool { super::bee::hurt(m); true } + Species::Silverfish => { + super::hostile::silverfish_hurt(m, source); + true + } Species::Enderman => super::enderman::on_damage(m, source), Species::WitherSkeleton | Species::Blaze | Species::MagmaCube | Species::Strider if source.is_fire() => diff --git a/crates/horizon-client/src/mobs/hostile.rs b/crates/horizon-client/src/mobs/hostile.rs index 0d3feaa6c..a58e4a909 100644 --- a/crates/horizon-client/src/mobs/hostile.rs +++ b/crates/horizon-client/src/mobs/hostile.rs @@ -1,7 +1,7 @@ //! Creepers (swelling, explosions) and spiders (wall climbing, daylight //! neutrality, leaping). use super::{ - Mob, MobEvent, MobWorld, Species, Who, + Damage, DamageKind, Mob, MobEvent, MobWorld, Species, Who, goals::{ Avoid, AvoidEntity, Cx, Float, Goal, HurtByTarget, LeapAtTarget, LookAtPlayer, MOVE, MeleeAttack, NearestTarget, RandomLookAround, Stroll, goal_clone, @@ -54,9 +54,157 @@ pub fn register(m: &mut Mob) { ); m.ai.nav.kind = super::nav::NavKind::WallClimber; } + S::Silverfish => { + let g = &mut m.ai.goals; + g.add(1, Float); + g.add(3, SilverfishWakeUpFriends); + g.add(4, MeleeAttack::new(1.0, false)); + g.add(5, SilverfishMergeWithStone::default()); + m.ai.targets.add(1, HurtByTarget::new().alert()); + m.ai.targets.add(2, NearestTarget::player(true)); + } _ => {} } } +/// `Silverfish.hurtServer`: a hit by an entity (or magic damage) starts the +/// countdown of its wake-up goal, kept in `extra.counter`. +pub fn silverfish_hurt(m: &mut Mob, source: &Damage) { + if (source.attacker.is_some() || source.kind == DamageKind::Magic) && m.extra.counter == 0 { + m.extra.counter = super::goals::reduced(20); + } +} +/// `Silverfish.SilverfishWakeUpFriendsGoal`: 20 ticks after being hurt, the +/// infested blocks within 10 blocks sideways and 5 up or down break (or turn +/// back into stone with mob griefing off), until a coin flip stops it. +#[derive(Clone, Debug)] +struct SilverfishWakeUpFriends; +impl Goal for SilverfishWakeUpFriends { + goal_clone!(); + fn name(&self) -> &'static str { + "SilverfishWakeUpFriendsGoal" + } + fn flags(&self) -> u8 { + 0 + } + fn can_use(&mut self, m: &mut Mob, _cx: &mut Cx) -> bool { + m.extra.counter > 0 + } + fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { + m.extra.counter -= 1; + if m.extra.counter > 0 { + return; + } + let base = [ + m.position.x.floor() as i32, + m.position.y.floor() as i32, + m.position.z.floor() as i32, + ]; + // The loops' offsets go 0, 1, -1, 2, -2 and so on. + let offsets = |limit: i32| std::iter::once(0).chain((1..=limit).flat_map(|i| [i, -i])); + for y in offsets(5) { + for x in offsets(10) { + for z in offsets(10) { + let pos = [base[0] + x, base[1] + y, base[2] + z]; + if !cx + .w + .block_name(pos[0], pos[1], pos[2]) + .starts_with("minecraft:infested_") + { + continue; + } + if cx.w.mob_griefing { + cx.events.push(MobEvent::BreakBlock { pos, drop: true }); + } else { + cx.events.push(MobEvent::Infest { + pos, + infested: false, + poof: None, + }); + } + if m.random.next_boolean() { + return; + } + } + } + } + } +} +/// `Silverfish.SilverfishMergeWithStoneGoal`: a stroll that, now and then +/// with mob griefing on, hides in a stone block next to the silverfish. +#[derive(Clone, Debug)] +struct SilverfishMergeWithStone { + stroll: Stroll, + merge: Option<[i32; 3]>, +} +impl Default for SilverfishMergeWithStone { + fn default() -> Self { + Self { + stroll: Stroll::new(1.0).interval(10), + merge: None, + } + } +} +impl Goal for SilverfishMergeWithStone { + goal_clone!(); + fn name(&self) -> &'static str { + "SilverfishMergeWithStoneGoal" + } + fn flags(&self) -> u8 { + MOVE + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + if m.ai.target.is_some() || !m.ai.nav.is_done() { + return false; + } + if cx.w.mob_griefing + && m.random + .next_int_bound(super::goals::reduced(10)) + .unwrap_or(1) + == 0 + { + // Direction.getRandom: down, up, north, south, west, east. + const DIRECTIONS: [[i32; 3]; 6] = [ + [0, -1, 0], + [0, 1, 0], + [0, 0, -1], + [0, 0, 1], + [-1, 0, 0], + [1, 0, 0], + ]; + let d = DIRECTIONS[m.random.next_int_bound(6).unwrap_or(0) as usize]; + let pos = [ + m.position.x.floor() as i32 + d[0], + (m.position.y + 0.5).floor() as i32 + d[1], + m.position.z.floor() as i32 + d[2], + ]; + if super::spawn_selection::infested_host(cx.w.block_name(pos[0], pos[1], pos[2])) { + self.merge = Some(pos); + return true; + } + } + self.merge = None; + self.stroll.can_use(m, cx) + } + fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + self.merge.is_none() && self.stroll.can_continue(m, cx) + } + fn start(&mut self, m: &mut Mob, cx: &mut Cx) { + let Some(pos) = self.merge else { + return self.stroll.start(m, cx); + }; + if super::spawn_selection::infested_host(cx.w.block_name(pos[0], pos[1], pos[2])) { + cx.events.push(MobEvent::Infest { + pos, + infested: true, + poof: Some(m.id), + }); + m.removed = true; + } + } + fn stop(&mut self, m: &mut Mob, cx: &mut Cx) { + self.stroll.stop(m, cx); + } +} /// Entity.getLightLevelDependentMagicValue at the eye. pub fn mob_brightness(m: &Mob, w: &MobWorld) -> f32 { w.brightness( diff --git a/crates/horizon-client/src/mobs/spawn_selection.rs b/crates/horizon-client/src/mobs/spawn_selection.rs index 4e51722f1..dc6a2fc72 100644 --- a/crates/horizon-client/src/mobs/spawn_selection.rs +++ b/crates/horizon-client/src/mobs/spawn_selection.rs @@ -535,7 +535,7 @@ pub(crate) fn mob_spawn_rules(s: Species, l: &L, x: i32, y: i32 value >= 0.0 } /// `InfestedBlock.isCompatibleHostBlock`. -fn infested_host(block: &str) -> bool { +pub fn infested_host(block: &str) -> bool { matches!( block, "minecraft:stone"