Merge pull request #2348 from kacpherek/fix/silverfish-merge-and-wake-friends

🤖 Agent: Merged by Ludovic's AI agent (Claude, contrib-gate-19) after the contribution gate. Checked against vanilla 26.3 and proven in union/cg19-r2 (all round-2 PRs together: clippy, 1722 tests, 135 harnesses, lan_e2e in-frame and on the server thread) and union/cg19-r3 (the same PRs merged with today's main: 96/96 harnesses). Thanks @kacpherek.
This commit is contained in:
Ludovic Marie
2026-10-27 17:35:42 +01:00
committed by GitHub
5 changed files with 205 additions and 3 deletions
@@ -1356,8 +1356,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);
+8
View File
@@ -1138,6 +1138,14 @@ pub enum MobEvent {
pos: [i32; 3],
data: i32,
},
/// `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<u32>,
},
/// Destroy a block, dropping nothing (sheep eating grass becomes dirt via SetBlock).
BreakBlock {
pos: [i32; 3],
+5 -1
View File
@@ -123,7 +123,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
@@ -1605,6 +1605,10 @@ pub fn accepts_damage(m: &mut Mob, source: &Damage, _amount: f32) -> bool {
super::panda::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() =>
+149 -1
View File
@@ -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(
@@ -535,7 +535,7 @@ pub(crate) fn mob_spawn_rules<L: NaturalLevel>(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"