Merge pull request #2162 from kacpherek/fix/zombie-reinforcements
🤖 Agent: Gate check by Ludovic's AI agent (Claude). Checked against 26.3: `Zombie.hurtServer` on Hard rolls `spawnReinforcementsChance` and tries 50 spots around the target (`checkSpawnRules`, no player close, unobstructed), and both zombies lose 0.05 of the chance. Two small differences are left for later: the roll is drawn at the end of the tick rather than inside the hurt, and the new zombie draws one extra yaw. Gate: this wave's 55 PRs merged on main in one tree: clippy `-D warnings` clean, 1186 horizon-client and 163 horizon-world tests pass, and of the 101 harnesses `parity.py --changed-files` picked, 100 pass (`block_removal` failed on one spawner case from #2199's orb merging, fixed there). `lan_e2e.py` passes 24 checks with the host on its server thread and in-frame (chest, xp, pvp, doors, villagers, beds, frames, projectiles, arrows, fishing, damage, mobs, blast, drops, elytra, breath, predict, cracks, barter, kills, hurt, contacts, underwater, plates), plus forced-levels on the server thread. Thanks @kacpherek!
This commit is contained in:
@@ -186,6 +186,7 @@ mod win;
|
||||
mod wind;
|
||||
mod wither;
|
||||
mod world_loading;
|
||||
mod zombie_reinforcements;
|
||||
mod zombie_villagers;
|
||||
use horizon_client::{
|
||||
Result,
|
||||
@@ -7917,6 +7918,9 @@ impl Game<'_> {
|
||||
if let horizon_client::mobs::MobEvent::Sound(s, at, volume, pitch) = event {
|
||||
self.audio
|
||||
.play(&format!("minecraft:{s}"), Some(at), volume, pitch);
|
||||
} else {
|
||||
// Left for the end of the tick.
|
||||
self.mobs[index].outbox.push(event);
|
||||
}
|
||||
}
|
||||
self.weapon_hit(index);
|
||||
|
||||
@@ -943,6 +943,9 @@ impl Game<'_> {
|
||||
volume,
|
||||
pitch,
|
||||
);
|
||||
} else {
|
||||
// Left for the end of the tick.
|
||||
t.outbox.push(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1200,6 +1203,7 @@ impl Game<'_> {
|
||||
self.creaking_tear_down(center, spread)
|
||||
}
|
||||
MobEvent::CureZombieVillager { mob } => self.cure_zombie_villager(mob),
|
||||
MobEvent::Reinforcements { mob, target } => self.call_reinforcements(mob, target),
|
||||
MobEvent::VillagerMeet { mob, other } => self.villager_meet(mob, other),
|
||||
MobEvent::VillagerBirth {
|
||||
parent,
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
//! `Zombie.hurtServer`'s reinforcements: a hurt zombie on Hard may call
|
||||
//! another of its kind from 7 to 40 blocks away to fight its target.
|
||||
use super::Game;
|
||||
use horizon_client::mobs::{
|
||||
MobType, Species, Who,
|
||||
finalize::{self, Attr, Op},
|
||||
spawn_rules::{self, Reason},
|
||||
spawn_selection::{self, NaturalLevel},
|
||||
spawner::SpawnerLevel,
|
||||
};
|
||||
use horizon_core::random::{LegacyRandom, RandomSource};
|
||||
use horizon_world::food::Difficulty;
|
||||
|
||||
/// `Zombie.REINFORCEMENT_ATTEMPTS`.
|
||||
const ATTEMPTS: usize = 50;
|
||||
/// `Zombie.REINFORCEMENT_RANGE_MIN` and `REINFORCEMENT_RANGE_MAX`.
|
||||
const RANGE_MIN: i32 = 7;
|
||||
const RANGE_MAX: i32 = 40;
|
||||
|
||||
/// One axis of the offset: `Mth.nextInt(random, 7, 40) * Mth.nextInt(random, -1, 1)`.
|
||||
fn offset(random: &mut LegacyRandom) -> i32 {
|
||||
let distance = random
|
||||
.next_int_between_inclusive(RANGE_MIN, RANGE_MAX)
|
||||
.unwrap_or(RANGE_MIN);
|
||||
distance * random.next_int_between_inclusive(-1, 1).unwrap_or(0)
|
||||
}
|
||||
|
||||
impl Game<'_> {
|
||||
/// `Zombie.hurtServer` after the hit: on Hard, with the zombie's chance
|
||||
/// and monsters spawning, a copy of it appears out of sight and takes
|
||||
/// `target`; both then call less often.
|
||||
pub(super) fn call_reinforcements(&mut self, caller: u32, target: Who) {
|
||||
let Some(index) = self.mobs.iter().position(|m| m.id == caller) else {
|
||||
return;
|
||||
};
|
||||
if self.world.health_rules.difficulty != Difficulty::Hard {
|
||||
return;
|
||||
}
|
||||
let zombie = &mut self.mobs[index];
|
||||
if f64::from(zombie.random.next_float()) >= zombie.attrs.spawn_reinforcements {
|
||||
return;
|
||||
}
|
||||
// ServerLevel.isSpawningMonsters.
|
||||
if !self.ui.rules.bool("spawn_mobs") || !self.ui.rules.bool("spawn_monsters") {
|
||||
return;
|
||||
}
|
||||
let kind = zombie.kind.clone();
|
||||
let from = [
|
||||
zombie.position.x.floor() as i32,
|
||||
zombie.position.y.floor() as i32,
|
||||
zombie.position.z.floor() as i32,
|
||||
];
|
||||
let mut own = std::mem::replace(&mut zombie.random, LegacyRandom::new(0));
|
||||
let mut rules = std::mem::replace(&mut self.world.random, LegacyRandom::new(0));
|
||||
let spot = self.reinforcement_spot(&kind, from, &mut own, &mut rules);
|
||||
self.mobs[index].random = own;
|
||||
self.world.random = rules;
|
||||
let Some(spot) = spot else {
|
||||
return;
|
||||
};
|
||||
let Some(id) = self.spawn_mob(&kind.id, spot.map(f64::from), false) else {
|
||||
return;
|
||||
};
|
||||
if let Some(reinforcement) = self.mobs.iter_mut().find(|m| m.id == id) {
|
||||
reinforcement.ai.target = Some(target);
|
||||
// ZOMBIE_REINFORCEMENT_CALLEE_CHARGE.
|
||||
finalize::add_modifier(
|
||||
reinforcement,
|
||||
Attr::SpawnReinforcements,
|
||||
"minecraft:reinforcement_callee_charge",
|
||||
-f64::from(0.05f32),
|
||||
Op::AddValue,
|
||||
);
|
||||
}
|
||||
// The caller's charge grows by 0.05 each time it calls.
|
||||
let caller = &mut self.mobs[index];
|
||||
let charged = finalize::modifier_amount(
|
||||
caller,
|
||||
Attr::SpawnReinforcements,
|
||||
"minecraft:reinforcement_caller_charge",
|
||||
)
|
||||
.unwrap_or(0.0);
|
||||
finalize::add_modifier(
|
||||
caller,
|
||||
Attr::SpawnReinforcements,
|
||||
"minecraft:reinforcement_caller_charge",
|
||||
charged - 0.05,
|
||||
Op::AddValue,
|
||||
);
|
||||
}
|
||||
/// The block `Zombie.hurtServer` puts the reinforcement at: the first of
|
||||
/// 50 random offsets with a valid spawn surface, no alive player within
|
||||
/// 7 blocks, and room for the zombie standing on the block's corner.
|
||||
fn reinforcement_spot(
|
||||
&self,
|
||||
kind: &MobType,
|
||||
from: [i32; 3],
|
||||
own: &mut LegacyRandom,
|
||||
rules: &mut LegacyRandom,
|
||||
) -> Option<[i32; 3]> {
|
||||
let level = self.natural_level();
|
||||
let (width, height) = (kind.width as f32, kind.height as f32);
|
||||
for _ in 0..ATTEMPTS {
|
||||
let [x, y, z] = from.map(|v| v + offset(own));
|
||||
if !spawn_rules::is_spawn_position_ok(kind.species, &level, x, y, z)
|
||||
|| !spawn_rules::check_spawn_rules(
|
||||
kind.species,
|
||||
&level,
|
||||
Reason::Reinforcement,
|
||||
rules,
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
// setPos(x, y, z): the box is centred on the block's corner.
|
||||
let bounds =
|
||||
spawn_selection::spawn_box(f64::from(x), f64::from(y), f64::from(z), width, height);
|
||||
// Drowned.canSpawnInLiquids.
|
||||
let wet_ok = kind.species == Species::Drowned
|
||||
|| !spawn_selection::contains_any_liquid(&level, bounds);
|
||||
if !level.alive_player_near(f64::from(x), f64::from(y), f64::from(z), 7.0)
|
||||
&& level.unobstructed(bounds)
|
||||
&& level.no_collision(bounds)
|
||||
&& wet_ok
|
||||
{
|
||||
return Some([x, y, z]);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -1166,6 +1166,12 @@ pub enum MobEvent {
|
||||
DigParticles {
|
||||
at: [f64; 3],
|
||||
},
|
||||
/// `Zombie.hurtServer`: a zombie hurt with a target in sight (its own,
|
||||
/// else the attacker) may call a reinforcement against it on Hard.
|
||||
Reinforcements {
|
||||
mob: u32,
|
||||
target: Who,
|
||||
},
|
||||
/// `Warden.applyDarknessAround`: darkness for survival players nearby.
|
||||
Darkness {
|
||||
at: [f64; 3],
|
||||
|
||||
@@ -1324,6 +1324,11 @@ pub fn after_hurt(m: &mut Mob, source: &Damage) {
|
||||
Species::Warden => super::warden::hurt(m, source),
|
||||
Species::Wolf => super::tame::after_hurt(m, source),
|
||||
Species::Nautilus | Species::ZombieNautilus => super::nautilus::after_hurt(m, source),
|
||||
Species::Zombie
|
||||
| Species::Husk
|
||||
| Species::Drowned
|
||||
| Species::ZombieVillager
|
||||
| Species::ZombifiedPiglin => super::undead::after_hurt(m, source),
|
||||
// NeutralMob: an iron golem hit by the player stays angry a while.
|
||||
// PiglinAi.wasHurtBy: drop what is being admired, no admiring for
|
||||
// a while after a player's hit, and anger.
|
||||
|
||||
@@ -590,6 +590,14 @@ pub fn has_modifier(m: &Mob, a: Attr, id: &str) -> bool {
|
||||
.as_ref()
|
||||
.is_some_and(|s| s.modifiers.iter().any(|x| x.attr == a && x.id == id))
|
||||
}
|
||||
/// The amount of a permanent modifier, if the mob has it.
|
||||
pub fn modifier_amount(m: &Mob, a: Attr, id: &str) -> Option<f64> {
|
||||
m.extra
|
||||
.attributes
|
||||
.as_ref()
|
||||
.and_then(|s| s.modifiers.iter().find(|x| x.attr == a && x.id == id))
|
||||
.map(|x| x.amount)
|
||||
}
|
||||
/// `addOrReplacePermanentModifier`.
|
||||
pub fn add_modifier(m: &mut Mob, a: Attr, id: &'static str, amount: f64, op: Op) {
|
||||
let s = m.extra.attributes.get_or_insert_with(Box::default);
|
||||
@@ -605,6 +613,59 @@ pub fn add_modifier(m: &mut Mob, a: Attr, id: &'static str, amount: f64, op: Op)
|
||||
}
|
||||
refresh(m, a);
|
||||
}
|
||||
/// `Zombie.handleAttributes`, from the zombie's own random.
|
||||
pub fn zombie_attributes(m: &mut Mob, special: f32, reason: Reason) {
|
||||
let r = &mut m.random;
|
||||
let reinforcements = if m.kind.species == Species::ZombifiedPiglin {
|
||||
0.0
|
||||
} else {
|
||||
r.next_double() * f64::from(0.1f32)
|
||||
};
|
||||
let knockback = r.next_double() * f64::from(0.05f32);
|
||||
let follow = r.next_double() * 1.5 * f64::from(special);
|
||||
let leader = r.next_float() < special * 0.05;
|
||||
let leader_rolls = leader.then(|| (r.next_double() * 0.25 + 0.5, r.next_double() * 3.0 + 1.0));
|
||||
set_base(m, Attr::SpawnReinforcements, reinforcements);
|
||||
add_modifier(
|
||||
m,
|
||||
Attr::KnockbackResistance,
|
||||
"minecraft:random_spawn_bonus",
|
||||
knockback,
|
||||
Op::AddValue,
|
||||
);
|
||||
if follow > 1.0 {
|
||||
add_modifier(
|
||||
m,
|
||||
Attr::FollowRange,
|
||||
"minecraft:zombie_random_spawn_bonus",
|
||||
follow,
|
||||
Op::AddMultipliedTotal,
|
||||
);
|
||||
}
|
||||
if let Some((reinforce, health)) = leader_rolls {
|
||||
add_modifier(
|
||||
m,
|
||||
Attr::SpawnReinforcements,
|
||||
"minecraft:leader_zombie_bonus",
|
||||
reinforce,
|
||||
Op::AddValue,
|
||||
);
|
||||
add_modifier(
|
||||
m,
|
||||
Attr::MaxHealth,
|
||||
"minecraft:leader_zombie_bonus",
|
||||
health,
|
||||
Op::AddMultipliedTotal,
|
||||
);
|
||||
if !matches!(
|
||||
reason,
|
||||
Reason::Conversion | Reason::Load | Reason::DimensionTravel
|
||||
) {
|
||||
m.health = max_health(m);
|
||||
}
|
||||
m.extra.can_break_doors = true;
|
||||
}
|
||||
}
|
||||
pub fn max_health(m: &Mob) -> f32 {
|
||||
value(m, Attr::MaxHealth) as f32
|
||||
}
|
||||
@@ -1151,63 +1212,9 @@ impl<L: FinalizeLevel + ?Sized, R: RandomSource> Ctx<'_, L, R> {
|
||||
}
|
||||
}
|
||||
self.halloween(m);
|
||||
self.zombie_attributes(m, special, reason);
|
||||
zombie_attributes(m, special, reason);
|
||||
g
|
||||
}
|
||||
/// `Zombie.handleAttributes`, from the zombie's own random.
|
||||
fn zombie_attributes(&mut self, m: &mut Mob, special: f32, reason: Reason) {
|
||||
let r = &mut m.random;
|
||||
let reinforcements = if m.kind.species == Species::ZombifiedPiglin {
|
||||
0.0
|
||||
} else {
|
||||
r.next_double() * f64::from(0.1f32)
|
||||
};
|
||||
let knockback = r.next_double() * f64::from(0.05f32);
|
||||
let follow = r.next_double() * 1.5 * f64::from(special);
|
||||
let leader = r.next_float() < special * 0.05;
|
||||
let leader_rolls =
|
||||
leader.then(|| (r.next_double() * 0.25 + 0.5, r.next_double() * 3.0 + 1.0));
|
||||
set_base(m, Attr::SpawnReinforcements, reinforcements);
|
||||
add_modifier(
|
||||
m,
|
||||
Attr::KnockbackResistance,
|
||||
"minecraft:random_spawn_bonus",
|
||||
knockback,
|
||||
Op::AddValue,
|
||||
);
|
||||
if follow > 1.0 {
|
||||
add_modifier(
|
||||
m,
|
||||
Attr::FollowRange,
|
||||
"minecraft:zombie_random_spawn_bonus",
|
||||
follow,
|
||||
Op::AddMultipliedTotal,
|
||||
);
|
||||
}
|
||||
if let Some((reinforce, health)) = leader_rolls {
|
||||
add_modifier(
|
||||
m,
|
||||
Attr::SpawnReinforcements,
|
||||
"minecraft:leader_zombie_bonus",
|
||||
reinforce,
|
||||
Op::AddValue,
|
||||
);
|
||||
add_modifier(
|
||||
m,
|
||||
Attr::MaxHealth,
|
||||
"minecraft:leader_zombie_bonus",
|
||||
health,
|
||||
Op::AddMultipliedTotal,
|
||||
);
|
||||
if !matches!(
|
||||
reason,
|
||||
Reason::Conversion | Reason::Load | Reason::DimensionTravel
|
||||
) {
|
||||
m.health = max_health(m);
|
||||
}
|
||||
m.extra.can_break_doors = true;
|
||||
}
|
||||
}
|
||||
fn husk(&mut self, m: &mut Mob, g: Option<Group>, reason: Reason) -> Option<Group> {
|
||||
let mut g = self.zombie(m, g, reason);
|
||||
if reason != Reason::Conversion {
|
||||
|
||||
@@ -264,6 +264,18 @@ pub fn finalize(m: &mut Mob, biome: &BiomeFacts, difficulty: &LocalDifficulty) {
|
||||
S::Slime | S::MagmaCube => super::cube::finalize(m),
|
||||
_ => {}
|
||||
}
|
||||
// Zombie.finalizeSpawn ends with handleAttributes: the reinforcement
|
||||
// chance (and a leader's bonus) that `Zombie.hurtServer` reads.
|
||||
if matches!(
|
||||
m.species(),
|
||||
S::Zombie | S::Husk | S::Drowned | S::ZombieVillager | S::ZombifiedPiglin
|
||||
) {
|
||||
super::finalize::zombie_attributes(
|
||||
m,
|
||||
difficulty.special(),
|
||||
super::finalize::Reason::Natural,
|
||||
);
|
||||
}
|
||||
}
|
||||
/// VillagerType.byBiome
|
||||
pub fn villager_type(b: &BiomeFacts) -> u8 {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Zombies (zombie, husk, drowned, zombie villager) and skeletons (skeleton,
|
||||
//! stray, bogged, wither skeleton): goals, bow attacks, water conversion.
|
||||
use super::{
|
||||
Mob, MobEvent, Species, Who,
|
||||
Damage, Mob, MobEvent, Species, Who,
|
||||
goals::{
|
||||
Avoid, AvoidEntity, Cx, FleeSun, Goal, HurtByTarget, LOOK, LookAtPlayer, MOVE, MeleeAttack,
|
||||
NearestTarget, RandomLookAround, RestrictSun, Stroll, goal_clone,
|
||||
@@ -10,6 +10,16 @@ use super::{
|
||||
use crate::projectiles::{Projectile, ProjectileKind};
|
||||
use horizon_core::{geometry::Vec3, random::RandomSource};
|
||||
|
||||
/// `Zombie.hurtServer`, once the hit landed: a zombie with a target (its own,
|
||||
/// else a living attacker) asks the game to try calling a reinforcement; the
|
||||
/// difficulty, chance and spawn tests are the game's (`call_reinforcements`).
|
||||
pub fn after_hurt(m: &mut Mob, source: &Damage) {
|
||||
if let Some(target) = m.ai.target.or(source.attacker) {
|
||||
m.outbox
|
||||
.push(MobEvent::Reinforcements { mob: m.id, target });
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register(m: &mut Mob) {
|
||||
use Species as S;
|
||||
match m.species() {
|
||||
|
||||
Reference in New Issue
Block a user