Make adult piglins hunt adult hoglins

This commit is contained in:
Kacpherek
2026-10-26 19:15:33 +01:00
parent cfc55bb6c6
commit 034af45777
3 changed files with 111 additions and 10 deletions
@@ -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;
+10
View File
@@ -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<Mob>),
@@ -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.
+93 -10
View File
@@ -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<u32>,
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<u32> =
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<MobEvent>) {
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) {