Let a piglin celebrate a killed target and dance over a hoglin

This commit is contained in:
Kacpherek
2026-10-26 20:41:38 +01:00
parent e704a8dccb
commit 36c06d6133
4 changed files with 98 additions and 1 deletions
+7
View File
@@ -223,6 +223,13 @@ fn mob_meta(
}
defaults.push((16, serializer::BYTE, Value::Byte(0)));
}
// Piglin.DATA_IS_DANCING (after the baby flag and the crossbow charge).
"minecraft:piglin" => {
if e.scratch[0] > 0 {
meta.push((19, serializer::BOOLEAN, Value::Bool(true)));
}
defaults.push((19, serializer::BOOLEAN, Value::Bool(false)));
}
// Sheep.DATA_WOOL_ID: the colour in the low nibble, 16 when sheared.
"minecraft:sheep" => {
let value = (e.color & 15) | if e.sheared { 16 } else { 0 };
@@ -66,6 +66,8 @@ fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin, local_enti
m.extra.color = (*wool & 15) as u8;
m.extra.sheared = wool & 16 != 0;
}
// Piglin.DATA_IS_DANCING: the dance pose.
(Species::Piglin, 19, Value::Bool(dancing)) => m.extra.scratch[0] = i32::from(*dancing),
// TamableAnimal.DATA_FLAGS_ID: sitting 1, tame 4.
(Species::Wolf | Species::Cat | Species::Parrot, 18, Value::Byte(flags)) => {
m.extra.sitting = flags & 1 != 0;
+3
View File
@@ -668,6 +668,9 @@ fn ai_step(m: &mut Mob, w: &MobWorld, level_random: &mut LegacyRandom, events: &
// serverAiStep has no goals, so it only falls and is pushed.
let immobile = (m.species() == Species::Ravager && super::ravager::immobile(m))
|| m.species() == Species::Mannequin;
if m.species() == Species::Piglin && m.alive() {
super::piglin::celebrate(m, w);
}
if m.alive() && !immobile {
server_ai_step(m, w, level_random, events);
} else {
+86 -1
View File
@@ -20,6 +20,9 @@ pub const ADMIRE: i32 = 119;
pub const ATE_RECENTLY: i32 = 200;
/// `AbstractPiglin.CONVERSION_TIME` (and the hoglin's).
const CONVERSION: i32 = 300;
/// `PiglinAi.CELEBRATION_TIME`: how long the `CELEBRATE_LOCATION` and
/// `DANCING` memories last.
const CELEBRATION: i32 = 300;
fn hostile_to_player(m: &Mob, w: &MobWorld) -> bool {
if m.is_baby() {
@@ -50,6 +53,7 @@ pub fn register_piglin(m: &mut Mob) {
g.add(3, MeleeAttack::new(1.0, true));
g.add(4, PiglinAvoidRepellent);
g.add(4, GoToWantedItem);
g.add(4, Celebrate);
g.add(5, Stroll::avoiding_water(0.6));
g.add(6, LookAtPlayer::new(8.0));
g.add(7, RandomLookAround::default());
@@ -243,6 +247,45 @@ impl Goal for Admire {
m.nav_stop();
}
}
/// The celebrate activity: `GoToTargetLocation` on the spot a killed target
/// fell (within 2 blocks, or 4 and slower while dancing).
#[derive(Clone, Debug)]
struct Celebrate;
impl Celebrate {
fn walk(m: &mut Mob, cx: &mut Cx) {
let Some(at) = m.extra.home else { return };
let (close, speed) = if m.extra.scratch[0] > 0 {
(4.0, 0.6)
} else {
(2.0, 1.0)
};
let center = Vec3::new(
f64::from(at[0]) + 0.5,
f64::from(at[1]),
f64::from(at[2]) + 0.5,
);
if m.distance_sqr(center) >= close * close {
m.nav_move_to(cx.w, [center.x, center.y, center.z], speed);
}
}
}
impl Goal for Celebrate {
goal_clone!();
fn flags(&self) -> u8 {
MOVE
}
fn can_use(&mut self, m: &mut Mob, _: &mut Cx) -> bool {
m.extra.scratch[1] > 0 && m.extra.home.is_some() && m.extra.using_item == 0
}
fn start(&mut self, m: &mut Mob, cx: &mut Cx) {
Self::walk(m, cx);
}
fn tick(&mut self, m: &mut Mob, cx: &mut Cx) {
if m.ai.nav.is_done() {
Self::walk(m, cx);
}
}
}
/// `GoToWantedItem`: walk to the gold the game spotted.
#[derive(Clone, Debug)]
struct GoToWantedItem;
@@ -294,6 +337,45 @@ fn hoglin_repellent(m: &mut Mob, w: &MobWorld) {
m.ai.target = None;
}
}
/// `StartCelebratingIfTargetDead` for a piglin (`scratch[0]` dancing,
/// `scratch[1]` ticks of celebration left, `scratch[2]` the hit seen, `home`
/// the `CELEBRATE_LOCATION`): a killed target starts a celebration at its
/// spot, and a hoglin's death makes the piglin dance one time in ten.
/// `PiglinAi.wasHurtBy` ends both. Runs before the target goals drop the
/// dead target.
pub fn celebrate(m: &mut Mob, w: &MobWorld) {
if m.extra.scratch[1] > 0 {
m.extra.scratch[1] -= 1;
if m.extra.scratch[1] == 0 {
m.extra.scratch[0] = 0;
m.extra.home = None;
}
}
if m.ai.last_hurt_by.is_some() && m.ai.last_hurt_by_time != m.extra.scratch[2] {
m.extra.scratch[2] = m.ai.last_hurt_by_time;
m.extra.scratch[0] = 0;
m.extra.scratch[1] = 0;
m.extra.home = None;
}
if m.extra.scratch[1] > 0 {
return;
}
let Some(target) = m.ai.target else { return };
let Some(t) = w.target_info(target).filter(|t| !t.alive) else {
return;
};
let hoglin =
matches!(target, Who::Mob(id) if w.seen(id).is_some_and(|s| s.species == Species::Hoglin));
if hoglin && m.random.next_float() < 0.1 {
m.extra.scratch[0] = 1;
}
m.extra.scratch[1] = CELEBRATION;
m.extra.home = Some([
t.position.x.floor() as i32,
t.position.y.floor() as i32,
t.position.z.floor() as i32,
]);
}
/// 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>) {
@@ -420,7 +502,10 @@ pub fn ears(p: &mut Poses, s: &State) {
/// Piglins' and brutes' ears flap (`AbstractPiglinModel`), arms follow the
/// arm pose (`PiglinModel`).
pub fn piglin_anim(m: &Mob, s: &State, p: &mut Poses) {
let pose = if m.extra.using_item > 0 {
// Piglin.getArmPose: dancing comes first.
let pose = if m.extra.scratch[0] > 0 {
PiglinPose::Dancing
} else if m.extra.using_item > 0 {
PiglinPose::AdmiringItem
} else if m.extra.flag {
PiglinPose::CrossbowCharge