Hoglins keep away from warped fungus, portals and respawn anchors

Hoglins now sense the nearest block from the hoglin repellents tag within 8 blocks sideways and 4 up or down. While one is near they become passive for 200 ticks (the attack target is dropped and no new one is picked), walk away from it, and avoid picking random walk spots within 8 blocks of it. Being hit ends the passive state.
This commit is contained in:
Kacpherek
2026-10-26 14:32:02 +01:00
parent b78daab11c
commit fd1d857f1b
4 changed files with 129 additions and 3 deletions
+3
View File
@@ -709,6 +709,9 @@ pub struct Extra {
/// A copper golem's `openedChestPos`.
pub opened_chest: Option<[i32; 3]>,
pub point: Option<[f64; 3]>,
/// `NEAREST_REPELLENT`: the nearest repelling block a hoglin or piglin
/// sensed, looked up every 20 ticks.
pub repellent: Option<[i32; 3]>,
pub vector: [f64; 3],
/// Active status effects: (effect id, ticks left, amplifier).
pub effects: Vec<(&'static str, i32, i32)>,
+14
View File
@@ -1242,6 +1242,20 @@ pub fn walk_target_value(m: &Mob, w: &MobWorld, p: [i32; 3]) -> f32 {
if m.species() == Species::Strider {
return super::strider::walk_target_value(m, w, p);
}
// Hoglin.getWalkTargetValue: nowhere near a repellent, crimson nylium best.
if m.species() == Species::Hoglin {
let near = m.extra.repellent.is_some_and(|r| {
let d = |i: usize| i64::from(r[i] - p[i]);
d(0) * d(0) + d(1) * d(1) + d(2) * d(2) < 64
});
return if near {
-1.0
} else if w.block_name(p[0], p[1] - 1, p[2]) == "minecraft:crimson_nylium" {
10.0
} else {
0.0
};
}
// Bee.getWalkTargetValue: any air block.
if m.species() == Species::Bee {
return if w.facts.is_air(w.world.block(p[0], p[1], p[2])) {
+83 -3
View File
@@ -9,7 +9,7 @@ use super::{
NearestTarget, Prey, RandomLookAround, Stroll, goal_clone,
},
};
use horizon_core::geometry::trig::Mth;
use horizon_core::geometry::{Vec3, trig::Mth};
use horizon_core::random::RandomSource;
use std::f32::consts::PI;
@@ -83,12 +83,16 @@ pub fn register_hoglin(m: &mut Mob) {
g.add(1, BabyPanic(super::goals::Panic::new(1.25)));
g.add(2, MeleeAttack::new(1.0, true));
g.add(3, super::goals::Breed::new(1.0));
g.add(3, HoglinAvoidRepellent);
g.add(4, super::goals::FollowParent::new(1.1));
g.add(5, Stroll::avoiding_water(0.4));
g.add(6, LookAtPlayer::new(8.0));
m.ai.targets.add(1, HurtByTarget::new().alert());
m.ai.targets
.add(2, NearestTarget::player(true).when(|m, _| !m.is_baby()));
// `HoglinAi.findNearestValidAttackTarget`: nothing while pacified.
m.ai.targets.add(
2,
NearestTarget::player(true).when(|m, _| !m.is_baby() && m.extra.ints[0] <= 0),
);
}
pub fn register_zoglin(m: &mut Mob) {
let g = &mut m.ai.goals;
@@ -108,6 +112,49 @@ pub fn register_zoglin(m: &mut Mob) {
.interval(10),
);
}
/// `SetWalkTargetAwayFrom.pos(NEAREST_REPELLENT, 1.0, 8, true)` of the hoglin's
/// idle activity: closer than 8 blocks to a repellent, walk away from it.
#[derive(Clone, Debug)]
struct HoglinAvoidRepellent;
impl Goal for HoglinAvoidRepellent {
goal_clone!();
fn flags(&self) -> u8 {
MOVE
}
fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
let Some(r) = m.extra.repellent else {
return false;
};
// Vec3.atBottomCenterOf.
let avoid = [
f64::from(r[0]) + 0.5,
f64::from(r[1]),
f64::from(r[2]) + 0.5,
];
if m.distance_sqr(Vec3::new(avoid[0], avoid[1], avoid[2])) >= 64.0 {
return false;
}
for _ in 0..10 {
if let Some(p) = m.land_random_pos_away(cx.w, 16, 7, avoid) {
let at = [
p[0].floor() as i32,
p[1].floor() as i32,
p[2].floor() as i32,
];
m.ai.pending_path = m.create_path_to(cx.w, at, 0);
return m.ai.pending_path.is_some();
}
}
false
}
fn can_continue(&mut self, m: &mut Mob, _: &mut Cx) -> bool {
!m.ai.nav.is_done()
}
fn start(&mut self, m: &mut Mob, _: &mut Cx) {
let path = m.ai.pending_path.take();
m.nav_move_to_path(path, 1.0);
}
}
/// Baby hoglins flee what hurts them; adults stand their ground.
#[derive(Clone, Debug)]
struct BabyPanic(super::goals::Panic);
@@ -165,12 +212,45 @@ impl Goal for GoToWantedItem {
m.extra.point = None;
}
}
/// `HoglinSpecificSensor.findNearestRepellent`: the nearest `#hoglin_repellents`
/// block within 8 blocks sideways and 4 up or down.
fn find_repellent(m: &Mob, w: &MobWorld) -> Option<[i32; 3]> {
let tag = w.block_tags.get("minecraft:hoglin_repellents")?;
let at = [
m.position.x.floor() as i32,
m.position.y.floor() as i32,
m.position.z.floor() as i32,
];
w.nearest_block(at, 8, 4, |_, name| tag.contains(name))
}
/// The hoglin's repellent memories: the sensor (every 20 ticks), the end of
/// `PACIFIED` (`ints[0]`), a hit ending it (`HoglinAi.wasHurtBy`, `ints[1]`
/// remembers the hit seen) and `BecomePassiveIfMemoryPresent(NEAREST_REPELLENT, 200)`.
fn hoglin_repellent(m: &mut Mob, w: &MobWorld) {
if m.extra.ints[0] > 0 {
m.extra.ints[0] -= 1;
}
if m.ai.last_hurt_by.is_some() && m.ai.last_hurt_by_time != m.extra.ints[1] {
m.extra.ints[1] = m.ai.last_hurt_by_time;
m.extra.ints[0] = 0;
}
if m.age.wrapping_add(m.id) % 20 == 0 {
m.extra.repellent = find_repellent(m, w);
}
if m.extra.ints[0] <= 0 && m.extra.repellent.is_some() {
m.extra.ints[0] = 200;
m.ai.target = None;
}
}
/// Per-tick piglin and hoglin state: admiring, anger, zombification and the
/// hoglin attack animation. Returns a conversion when it happens.
pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
if m.extra.anim_ticks > 0 {
m.extra.anim_ticks -= 1;
}
if m.species() == Species::Hoglin && m.alive() {
hoglin_repellent(m, w);
}
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) {
+29
View File
@@ -65,6 +65,35 @@ impl MobWorld<'_> {
pub fn has_tag(&self, tag: &str, block: &str) -> bool {
self.block_tags.get(tag).is_some_and(|s| s.contains(block))
}
/// `findBlocksInBoxByManhattanDistance(at, range_xz, range_y).filterState(..).findFirst()`:
/// the first block `matches` accepts (given its position and name), going
/// out from `at` by Manhattan distance as `BlockPos.withinBoxByManhattanDistance` does.
pub fn nearest_block(
&self,
at: [i32; 3],
range_xz: i32,
range_y: i32,
matches: impl Fn([i32; 3], &str) -> bool,
) -> Option<[i32; 3]> {
// Visiting order: distance, then x, then y, then +z before -z.
let mut best: Option<((i32, i32, i32, bool), [i32; 3])> = None;
for dx in -range_xz..=range_xz {
for dy in -range_y..=range_y {
for dz in -range_xz..=range_xz {
let key = (dx.abs() + dy.abs() + dz.abs(), dx, dy, dz < 0);
if best.is_some_and(|(k, _)| k <= key) {
continue;
}
let p = [at[0] + dx, at[1] + dy, at[2] + dz];
let state = self.world.block(p[0], p[1], p[2]);
if !self.facts.is_air(state) && matches(p, &self.facts.get(state).name) {
best = Some((key, p));
}
}
}
}
best.map(|(_, p)| p)
}
/// Level.clip with COLLIDER shapes (optionally stopping at any fluid): true on a miss.
pub fn clear_between(&self, from: Vec3, to: Vec3, fluids_block: bool) -> bool {
let mut scene = SceneCollisions {