From 034af457778bab980cc95ca1c76c8e50471d49fc Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Mon, 26 Oct 2026 19:15:33 +0100 Subject: [PATCH 1/2] Make adult piglins hunt adult hoglins --- crates/horizon-client/src/game/creatures.rs | 8 ++ crates/horizon-client/src/mobs.rs | 10 ++ crates/horizon-client/src/mobs/piglin.rs | 103 ++++++++++++++++++-- 3 files changed, 111 insertions(+), 10 deletions(-) diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 2b65a3289..a9aa16a1b 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -1009,6 +1009,14 @@ impl Game<'_> { m.ai.target = Some(target); } } + MobEvent::HuntHoglin { mob, target, ticks } => { + if let Some(m) = self.mobs.iter_mut().find(|m| m.id == mob) { + m.extra.ints[0] = ticks; + if m.ai.target.is_none() { + m.ai.target = Some(Who::Mob(target)); + } + } + } MobEvent::Projectile(mut p) => { self.next_entity += 1; p.id = self.next_entity; diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 00c21633e..cd00a12c4 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -985,6 +985,13 @@ pub enum MobEvent { mob: u32, target: Who, }, + /// `StartHuntingHoglin` for a nearby adult piglin: it takes the hoglin as + /// its anger target (when it has none) and starts its `HUNTED_RECENTLY` wait. + HuntHoglin { + mob: u32, + target: u32, + ticks: i32, + }, Projectile(crate::projectiles::Projectile), /// Spawn a new mob (breeding, slime splitting, chicken eggs...). Spawn(Box), @@ -1251,6 +1258,8 @@ pub struct Seen { /// `MAX_HEALTH`, `JUMP_STRENGTH` and `MOVEMENT_SPEED` base values (a /// horse's breeding stock). pub stats: [f64; 3], + /// A piglin's `HUNTED_RECENTLY` memory. + pub hunted_recently: bool, } impl Mob { pub fn burning_time(&self) -> f64 { @@ -1323,6 +1332,7 @@ impl Mob { m.extra.vector[0], m.attrs.speed, ], + hunted_recently: m.species() == Species::Piglin && m.extra.ints[0] > 0, } } /// `EntityType.getCategory`'s name. diff --git a/crates/horizon-client/src/mobs/piglin.rs b/crates/horizon-client/src/mobs/piglin.rs index fff536932..5b8c17fb3 100644 --- a/crates/horizon-client/src/mobs/piglin.rs +++ b/crates/horizon-client/src/mobs/piglin.rs @@ -6,7 +6,8 @@ use super::{ anim::{Poses, State}, goals::{ Avoid, AvoidEntity, Cx, Float, Goal, HurtByTarget, LOOK, LookAtPlayer, MOVE, MeleeAttack, - NearestTarget, Prey, RandomLookAround, Stroll, goal_clone, + NearestTarget, Prey, RandomLookAround, Stroll, TARGET, TargetState, goal_clone, + target_continue, }, }; use horizon_core::geometry::{Vec3, trig::Mth}; @@ -54,15 +55,94 @@ pub fn register_piglin(m: &mut Mob) { m.ai.targets.add(1, hurt); m.ai.targets .add(2, NearestTarget::player(true).when(hostile_to_player)); - m.ai.targets.add( - 3, - NearestTarget::new( - Prey::Filter(|s| s.species == Species::Hoglin && s.baby), - true, - ) - .interval(10) - .when(|m, _| !m.is_baby()), - ); + m.ai.targets.add(3, HuntHoglin::default()); + // `PiglinAi.initMemories`: the first hunt waits too. + m.extra.ints[0] = time_between_hunts(m); +} +/// `PiglinAi.TIME_BETWEEN_HUNTS`: 30 to 120 seconds. +fn time_between_hunts(m: &mut Mob) -> i32 { + m.random + .next_int_between_inclusive(600, 2400) + .unwrap_or(600) +} +/// `StartHuntingHoglin`: an adult piglin that is not angry, has not hunted +/// recently and sees no adult piglin that has, goes for the nearest adult +/// hoglin it sees; then it and the adult piglins near it wait before the next +/// hunt, and the others join in (`broadcastAngerTarget`). +#[derive(Clone, Debug)] +struct HuntHoglin { + found: Option, + st: TargetState, +} +impl Default for HuntHoglin { + fn default() -> Self { + let mut st = TargetState::default(); + st.unseen_memory = 60; + Self { found: None, st } + } +} +impl Goal for HuntHoglin { + goal_clone!(); + fn flags(&self) -> u8 { + TARGET + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + if m.is_baby() || m.extra.ints[0] > 0 || m.extra.anger > 0 || m.ai.target.is_some() { + return false; + } + let range = m.attrs.follow_range; + let pos = Vec3::new(m.position.x, m.eye_y(), m.position.z); + let id = m.id; + if cx + .w + .nearby(pos, range, |s| { + s.id != id && s.species == Species::Piglin && !s.baby && s.hunted_recently + }) + .next() + .is_some() + { + return false; + } + let hoglins: Vec<(u32, Vec3)> = + cx.w.nearby(pos, range, |s| s.species == Species::Hoglin && !s.baby) + .map(|s| (s.id, s.position)) + .collect(); + self.found = hoglins + .into_iter() + .find(|&(hoglin, at)| { + m.distance_sqr(at) <= range * range && m.sees(cx.w, Who::Mob(hoglin)) + }) + .map(|(hoglin, _)| hoglin); + self.found.is_some() + } + fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + target_continue(&mut self.st, true, m, cx) + } + fn start(&mut self, m: &mut Mob, cx: &mut Cx) { + let Some(hoglin) = self.found else { return }; + m.ai.target = Some(Who::Mob(hoglin)); + m.extra.ints[0] = time_between_hunts(m); + let range = m.attrs.follow_range; + let pos = Vec3::new(m.position.x, m.eye_y(), m.position.z); + let id = m.id; + let others: Vec = + cx.w.nearby(pos, range, |s| { + s.id != id && s.species == Species::Piglin && !s.baby + }) + .map(|s| s.id) + .collect(); + for mob in others { + let ticks = time_between_hunts(m); + cx.events.push(MobEvent::HuntHoglin { + mob, + target: hoglin, + ticks, + }); + } + } + fn stop(&mut self, m: &mut Mob, _: &mut Cx) { + m.ai.target = None; + } } pub fn register_brute(m: &mut Mob) { let g = &mut m.ai.goals; @@ -251,6 +331,9 @@ pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { if m.species() == Species::Hoglin && m.alive() { hoglin_repellent(m, w); } + if m.species() == Species::Piglin && m.extra.ints[0] > 0 { + m.extra.ints[0] -= 1; + } if m.extra.anger > 0 { m.extra.anger -= 1; if m.extra.anger == 0 && m.ai.target == Some(Who::Player) && !hostile_to_player(m, w) { From 4b4361fbc052751a5118192b312d857b4607c5c4 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Mon, 26 Oct 2026 21:48:20 +0100 Subject: [PATCH 2/2] Clear a piglin's hoglin target once it dies or disappears --- crates/horizon-client/src/mobs/piglin.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/mobs/piglin.rs b/crates/horizon-client/src/mobs/piglin.rs index 5b8c17fb3..0644b40e6 100644 --- a/crates/horizon-client/src/mobs/piglin.rs +++ b/crates/horizon-client/src/mobs/piglin.rs @@ -331,8 +331,18 @@ pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { if m.species() == Species::Hoglin && m.alive() { hoglin_repellent(m, w); } - if m.species() == Species::Piglin && m.extra.ints[0] > 0 { - m.extra.ints[0] -= 1; + if m.species() == Species::Piglin { + if m.extra.ints[0] > 0 { + m.extra.ints[0] -= 1; + } + // `StopAttackingIfTargetInvalid`: a hoglin handed over by a hunt + // broadcast has no target goal of its own to drop it when it dies. + if let Some(who @ Who::Mob(_)) = m.ai.target + && w.target_info(who) + .is_none_or(|t| !t.alive || !t.can_be_seen_as_enemy) + { + m.ai.target = None; + } } if m.extra.anger > 0 { m.extra.anger -= 1;