Let armadillos roll up when scared, peek out and take less damage as a ball

This commit is contained in:
Kacpherek
2026-10-26 22:59:54 +01:00
parent a45d0de43e
commit 11e1ff12d0
12 changed files with 472 additions and 5 deletions
@@ -1056,6 +1056,8 @@ pub fn main() -> horizon_client::Result<()> {
last_hurt: None,
velocity: Vec3::ZERO,
yaw: 0.0,
sprinting: false,
riding: false,
hero: false,
effects: Vec::new(),
uuid: PLAYER_UUID,
@@ -192,6 +192,8 @@ impl Game<'_> {
last_hurt: self.combat.last_hurt_mob.map(|(id, at)| (Who::Mob(id), at)),
velocity: p.velocity,
yaw: self.yaw,
sprinting: p.sprinting,
riding: self.riding.is_some(),
hero: self.potions.effects.get("hero_of_the_village").is_some(),
effects: self.potions.effects.list.iter().map(|e| e.id).collect(),
uuid: self.level.player_uuid.unwrap_or_default(),
+8
View File
@@ -399,6 +399,14 @@ fn mob_meta(
}
defaults.push((18, serializer::BYTE, Value::Byte(16)));
}
// Armadillo.ARMADILLO_STATE (after AgeableMob's two).
"minecraft:armadillo" => {
let state = horizon_client::mobs::armadillo::state(m);
if state != 0 {
meta.push((18, serializer::ARMADILLO_STATE, Value::Id(i32::from(state))));
}
defaults.push((18, serializer::ARMADILLO_STATE, Value::Id(0)));
}
// Enderman.DATA_CARRY_STATE.
"minecraft:enderman" => {
if let Some(state) = e.carried {
@@ -369,6 +369,8 @@ mod tests {
last_hurt: None,
velocity: Vec3::ZERO,
yaw: 0.0,
sprinting: false,
riding: false,
hero: false,
effects: Vec::new(),
uuid: [0; 4],
@@ -4,7 +4,7 @@
//! own simulation, which a client of a server doesn't run.
use super::Game;
use horizon_client::mobs::render::{PROFESSIONS, VILLAGER_TYPES};
use horizon_client::mobs::{Mob, Species, Who, bat, camel, sniffer, sync, tame};
use horizon_client::mobs::{Mob, Species, Who, armadillo, bat, camel, sniffer, sync, tame};
use horizon_protocol::entity_data::Value;
use horizon_protocol::registry::Builtin;
@@ -92,6 +92,11 @@ fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin, local_enti
(Species::Sniffer, 18, Value::Id(state)) => {
sniffer::set_state(m, (*state).clamp(0, 6) as u8);
}
// Armadillo.ARMADILLO_STATE (after AgeableMob's two): the shell and
// its animations follow it in `sync::client_tick`.
(Species::Armadillo, 18, Value::Id(state)) => {
armadillo::set_state(m, (*state).clamp(0, 3) as u8);
}
// Shulker.DATA_ATTACH_FACE_ID, DATA_PEEK_ID and DATA_COLOR_ID.
(Species::Shulker, 16, Value::Id(face)) => m.extra.variant3 = (*face).clamp(0, 5) as u8,
(Species::Shulker, 17, Value::Byte(peek)) => m.extra.counter = i32::from(*peek),
+9
View File
@@ -11,6 +11,7 @@ pub mod ambient;
pub mod anim;
pub mod animal_brain;
pub mod aquatic;
pub mod armadillo;
pub mod axolotl;
pub mod bat;
pub mod bee;
@@ -786,6 +787,8 @@ pub struct Extra {
pub brain: Option<Box<monster_brain::Brain>>,
/// A bee's nectar, anger, flower and hive state.
pub bee: Option<Box<bee::Bee>>,
/// An armadillo's state and scare brain.
pub armadillo: armadillo::State,
}
#[derive(Clone, Debug)]
pub struct Mob {
@@ -1545,6 +1548,9 @@ pub struct PlayerView {
pub last_hurt: Option<(Who, i64)>,
pub velocity: Vec3,
pub yaw: f32,
/// `Player.isSprinting` and `isPassenger` (what scares an armadillo).
pub sprinting: bool,
pub riding: bool,
/// The Hero of the Village effect.
pub hero: bool,
/// The active effects, without namespace (`slowness`, `poison`...).
@@ -1811,6 +1817,9 @@ impl Mob {
return false;
}
let mut amount = if reflected { 1000.0 } else { amount };
if self.species() == Species::Armadillo {
amount = armadillo::reduce_damage(self, amount);
}
if source.kind == DamageKind::FallingBlock
&& !species::body_slot(self.species())
&& self.extra.head.is_some()
+13 -1
View File
@@ -178,6 +178,7 @@ pub fn register(m: &mut Mob) {
Family::Undead => undead::register(m),
Family::Hostile => hostile::register(m),
Family::Aquatic => aquatic::register(m),
Family::Generic if m.species() == Species::Armadillo => super::armadillo::register(m),
Family::Generic => {
generic(m);
if m.species() == Species::Rabbit {
@@ -408,7 +409,10 @@ pub fn tick(
}
// Mob.tickHeadTurn -> BodyRotationControl.clientTick (a frozen
// creaking's waits).
if m.alive() && (!super::creaking::is_creaking(m) || super::creaking::can_move(m)) {
if m.alive()
&& (!super::creaking::is_creaking(m) || super::creaking::can_move(m))
&& !super::armadillo::scared(m)
{
m.body_rotation_tick();
}
events.append(&mut m.outbox);
@@ -964,6 +968,7 @@ fn custom_server_ai_step(m: &mut Mob, cx: &mut Cx) {
Species::Evoker if m.extra.counter > 0 => m.extra.counter -= 1,
Species::Bat => super::bat::server_ai_step(m, cx.w, cx.events),
Species::Bee => super::bee::server_ai_step(m, cx),
Species::Armadillo => super::armadillo::server_ai_step(m, cx),
_ => {}
}
}
@@ -1016,6 +1021,7 @@ fn species_ai_step(
}
Species::Nautilus | Species::ZombieNautilus => super::nautilus::ai_step(m, w, events),
Species::Chicken => farm::chicken_ai_step(m, w, events),
Species::Armadillo => super::armadillo::tick(m),
Species::Turtle => super::turtle::ai_step(m, w, events),
Species::Sheep if m.extra.anim_ticks > 0 => {
m.extra.anim_ticks -= 1;
@@ -1126,6 +1132,7 @@ pub fn voice_sounds(m: &Mob) -> [&'static str; 3] {
match m.species() {
Species::Nautilus | Species::ZombieNautilus => super::nautilus::sounds(m),
Species::SulfurCube => super::sulfur_cube::sounds(m),
Species::Armadillo => super::armadillo::sounds(m),
// The sound variant's baby_sounds (classic sets, like the rest here).
Species::Pig if m.is_baby() => [
"entity.baby_pig.ambient",
@@ -1148,6 +1155,10 @@ pub fn feed_animal(
m: &mut Mob,
level: &mut impl RandomSource,
) -> Option<Option<(&'static str, f32, f32)>> {
// Armadillo.mobInteract: a ball takes no food.
if super::armadillo::scared(m) {
return None;
}
if m.extra.age == 0 && m.extra.in_love <= 0 {
m.extra.in_love = 600;
m.extra.love_player = true;
@@ -1338,6 +1349,7 @@ pub fn after_hurt(m: &mut Mob, source: &Damage) {
m.ai.no_action_time = 0;
match m.species() {
Species::Enderman => super::enderman::after_hurt(m, source),
Species::Armadillo => super::armadillo::after_hurt(m, source),
Species::Shulker => super::shulker::after_hurt(m),
Species::Warden => super::warden::hurt(m, source),
Species::Wolf => super::tame::after_hurt(m, source),
+407
View File
@@ -0,0 +1,407 @@
//! Armadillos (`Armadillo`, `ArmadilloAi`): a player who sprints or rides
//! nearby, an undead mob or the last attacker scares one into a ball (IDLE,
//! ROLLING, SCARED, UNROLLING); it peeks out now and then, and a ball takes
//! less damage. The brain's `ARMADILLO_SCARE_DETECTED` sensor, its
//! `DANGER_DETECTED_RECENTLY` memory and the `ArmadilloBallUp` behaviour are
//! kept in `State`; the walking behaviours are the goals of `register`.
//! AnimState states: roll out, roll up, peek.
use super::{
Damage, DamageKind, Mob, MobEvent, MobWorld, Species, Who,
goals::{
Cx, Float, Goal, LOOK, LookAtPlayer, MOVE, Panic, RandomLookAround, Stroll, goal_clone,
},
};
use horizon_core::{geometry::Vec3, random::RandomSource};
pub const IDLE: u8 = 0;
pub const ROLLING: u8 = 1;
pub const SCARED: u8 = 2;
pub const UNROLLING: u8 = 3;
const ROLL_OUT: usize = 0;
const ROLL_UP: usize = 1;
const PEEK: usize = 2;
/// `ArmadilloState.animationDuration`.
fn animation_duration(state: u8) -> i32 {
match state {
ROLLING => 10,
SCARED => 50,
UNROLLING => 30,
_ => 0,
}
}
/// `Armadillo.ARMADILLO_STATE` and the brain's memory and behaviour.
#[derive(Clone, Debug, Default)]
pub struct State {
pub state: u8,
/// `inStateTicks`.
pub in_state_ticks: i32,
/// `DANGER_DETECTED_RECENTLY`: ticks to live, 0 when absent.
danger: i32,
/// Ticks to the sensor's next scan.
sensor: i32,
/// `ArmadilloBallUp`: running, its peek timer and whether it saw the
/// danger around last tick.
ball_up: bool,
next_peek: i32,
danger_was_around: bool,
/// Whether the PANIC activity was the active one after the last tick.
panic_activity: bool,
/// `peekReceivedClient`.
peek_received: bool,
}
pub fn register(m: &mut Mob) {
let mut panic = Panic::new(2.0);
panic.environmental = true;
let g = &mut m.ai.goals;
g.add(0, Float);
g.add(1, ArmadilloPanic(panic));
g.add(2, Rolled);
g.add(5, Stroll::avoiding_water(1.0));
g.add(6, LookAtPlayer::new(6.0));
g.add(7, RandomLookAround::default());
}
/// `Armadillo.isScared`.
pub fn scared(m: &Mob) -> bool {
m.extra.armadillo.state != IDLE
}
/// `ArmadilloState.shouldHideInShell`: the model shows its shell.
pub fn hiding_in_shell(m: &Mob) -> bool {
let a = &m.extra.armadillo;
match a.state {
ROLLING => a.in_state_ticks > 5,
SCARED => true,
UNROLLING => a.in_state_ticks < 26,
_ => false,
}
}
/// `Armadillo.getState`, as it is synched.
pub fn state(m: &Mob) -> u8 {
m.extra.armadillo.state
}
/// `Armadillo.switchToState` and `onSyncedDataUpdated(ARMADILLO_STATE)`.
pub fn set_state(m: &mut Mob, state: u8) {
let a = &mut m.extra.armadillo;
if a.state != state {
a.state = state;
a.in_state_ticks = 0;
}
}
/// `Armadillo.canStayRolledUp`.
fn can_stay_rolled_up(m: &Mob) -> bool {
!m.ai.panicking
&& !m.fluids.in_water
&& !m.fluids.in_lava
&& m.extra.leash.is_none()
&& m.extra.mount.is_none()
&& m.extra.rider.is_none()
}
/// `Entity.playSound(sound)`.
fn play(m: &Mob, events: &mut Vec<MobEvent>, sound: &'static str) {
if !m.silent {
let at = [m.position.x, m.position.y, m.position.z];
events.push(MobEvent::Sound(sound, at, 1.0, 1.0));
}
}
/// `Armadillo.rollUp`.
fn roll_up(m: &mut Mob, events: &mut Vec<MobEvent>) {
if !scared(m) {
super::creaking::stop_in_place(m);
m.extra.in_love = 0;
if !m.silent {
m.voice("entity.armadillo.roll", events);
}
set_state(m, ROLLING);
}
}
/// `Armadillo.rollOut`.
fn roll_out(m: &mut Mob, events: &mut Vec<MobEvent>) {
if scared(m) {
if !m.silent {
m.voice("entity.armadillo.unroll_finish", events);
}
set_state(m, IDLE);
}
}
/// `Armadillo.hurtServer`: a ball takes half of the damage past the first
/// point.
pub fn reduce_damage(m: &Mob, amount: f32) -> f32 {
if scared(m) {
((amount - 1.0) / 2.0).max(0.0)
} else {
amount
}
}
/// `Armadillo.actuallyHurt`: a hit by a living thing curls it up (and keeps
/// the danger in mind for 80 ticks), a hit by fire and the like unrolls it.
pub fn after_hurt(m: &mut Mob, source: &Damage) {
if m.no_ai || m.health <= 0.0 {
return;
}
let mut events = Vec::new();
if source.attacker.is_some() {
m.extra.armadillo.danger = 80;
if can_stay_rolled_up(m) {
roll_up(m, &mut events);
}
} else if environmental(source.kind) {
roll_out(m, &mut events);
}
m.outbox.append(&mut events);
}
/// `DamageTypeTags.PANIC_ENVIRONMENTAL_CAUSES`.
pub fn environmental(kind: DamageKind) -> bool {
matches!(
kind,
DamageKind::Cactus | DamageKind::OnFire | DamageKind::InFire | DamageKind::Lava
)
}
/// The hurt sound: a ball's is muffled, and it has no ambient sound
/// (`getAmbientSound`, `getHurtSound`); empty is none.
pub fn sounds(m: &Mob) -> [&'static str; 3] {
let [ambient, hurt, death] = m.kind.sounds;
if scared(m) {
["", "entity.armadillo.hurt_reduced", death]
} else {
[ambient, hurt, death]
}
}
/// `EntityTypeTags.UNDEAD`.
fn undead(s: Species) -> bool {
matches!(
s,
Species::Skeleton
| Species::Stray
| Species::WitherSkeleton
| Species::SkeletonHorse
| Species::Bogged
| Species::Parched
| Species::ZombieHorse
| Species::CamelHusk
| Species::Zombie
| Species::ZombieVillager
| Species::ZombifiedPiglin
| Species::Zoglin
| Species::Drowned
| Species::Husk
| Species::ZombieNautilus
| Species::Wither
| Species::Phantom
)
}
/// `Armadillo.isScaredBy` for the nearby living entities: within 7 blocks
/// sideways and 2 up and down, an undead mob, the last attacker, or a
/// player who sprints or rides.
fn scared_by_someone(m: &Mob, w: &MobWorld) -> bool {
let area = m.bounds().inflate(Vec3::new(7.0, 2.0, 7.0));
let attacker =
m.ai.last_hurt_by
.filter(|_| m.age as i32 - m.ai.last_hurt_by_time <= 100);
if let Some(p) = &w.player
&& p.alive
&& !p.spectator
&& p.bounds.intersects(area)
&& (attacker == Some(Who::Player) || p.sprinting || p.riding)
{
return true;
}
w.others_near(area).any(|s| {
s.id != m.id
&& s.alive
&& s.bounds.intersects(area)
&& (undead(s.species) || attacker == Some(Who::Mob(s.id)))
})
}
/// `Armadillo.customServerAiStep`'s brain: memories expire, the scare
/// sensor scans every fifth tick, the core's rolling out, then the
/// `ArmadilloBallUp` behaviour of the PANIC activity.
pub fn server_ai_step(m: &mut Mob, cx: &mut Cx) {
let events = &mut *cx.events;
let a = &mut m.extra.armadillo;
if a.danger > 0 {
a.danger -= 1;
}
a.sensor -= 1;
if a.sensor <= 0 {
a.sensor = 5;
if !can_stay_rolled_up(m) {
m.extra.armadillo.danger = 0;
} else if scared_by_someone(m, cx.w) {
m.extra.armadillo.danger = 80;
}
}
// ARMADILLO_ROLLING_OUT: no danger left in mind.
if m.extra.armadillo.danger == 0 && scared(m) {
roll_out(m, events);
}
if m.extra.armadillo.panic_activity && !m.extra.armadillo.ball_up && m.on_ground && !liquid(m) {
// ArmadilloBallUp.start.
m.extra.armadillo.ball_up = true;
roll_up(m, events);
}
if m.extra.armadillo.ball_up {
ball_up_tick(m, events);
}
let a = &mut m.extra.armadillo;
a.panic_activity = a.danger > 0 && !m.ai.panicking;
}
fn liquid(m: &Mob) -> bool {
m.fluids.in_water || m.fluids.in_lava
}
/// `ArmadilloBallUp.tickOrStop`.
fn ball_up_tick(m: &mut Mob, events: &mut Vec<MobEvent>) {
if !scared(m) {
// canStillUse is false: stop.
m.extra.armadillo.ball_up = false;
if !can_stay_rolled_up(m) {
roll_out(m, events);
}
return;
}
let a = &mut m.extra.armadillo;
if a.next_peek > 0 {
a.next_peek -= 1;
}
if a.state == ROLLING && a.in_state_ticks > animation_duration(ROLLING) {
set_state(m, SCARED);
if m.on_ground {
play(m, events, "entity.armadillo.land");
}
return;
}
let danger = m.extra.armadillo.danger;
let around = danger > 75;
if around != m.extra.armadillo.danger_was_around {
m.extra.armadillo.next_peek = next_peek_timer(m);
}
m.extra.armadillo.danger_was_around = around;
let state = m.extra.armadillo.state;
if state == SCARED {
if m.extra.armadillo.next_peek == 0 && m.on_ground && around {
peek(m, events);
m.extra.armadillo.next_peek = next_peek_timer(m);
}
if danger < animation_duration(UNROLLING) {
play(m, events, "entity.armadillo.unroll_start");
set_state(m, UNROLLING);
}
} else if state == UNROLLING && danger > animation_duration(UNROLLING) {
set_state(m, SCARED);
}
}
/// `ArmadilloBallUp.pickNextPeekTimer`.
fn next_peek_timer(m: &mut Mob) -> i32 {
animation_duration(SCARED) + m.random.next_int_between_inclusive(100, 400).unwrap_or(100)
}
/// `level.broadcastEntityEvent(body, 64)`; the sound is the client's
/// `playLocalSound`, which the server plays for everyone here.
fn peek(m: &mut Mob, events: &mut Vec<MobEvent>) {
entity_event(m, 64);
let at = [m.position.x, m.position.y, m.position.z];
events.push(MobEvent::EntityEvent {
mob: m.id,
event: 64,
at,
});
events.push(MobEvent::Sound("entity.armadillo.peek", at, 1.0, 1.0));
}
/// `Armadillo.handleEntityEvent`: 64 is a peek.
pub fn entity_event(m: &mut Mob, event: u8) {
if event == 64 {
m.extra.armadillo.peek_received = true;
}
}
/// `Armadillo.tick`: the animation states on a client, the ball's head held
/// to its body, and `inStateTicks`. Server and client both run it.
pub fn tick(m: &mut Mob) {
animate(m);
if scared(m) {
// clampHeadRotationToBody with getMaxHeadYRot 0.
m.head_yaw += super::living::wrap(m.body_yaw - m.head_yaw);
}
m.extra.armadillo.in_state_ticks += 1;
}
/// `Armadillo.setupAnimationStates`.
fn animate(m: &mut Mob) {
let age = m.age as f32;
let a = &mut m.extra.armadillo;
let s = &mut m.anim.states;
match a.state {
ROLLING => {
s[ROLL_OUT].stop();
s[ROLL_UP].start_if_stopped(age);
s[PEEK].stop();
}
SCARED => {
s[ROLL_OUT].stop();
s[ROLL_UP].stop();
if a.peek_received {
s[PEEK].stop();
a.peek_received = false;
}
if a.in_state_ticks == 0 {
s[PEEK].start(age);
s[PEEK].fast_forward(animation_duration(SCARED));
} else {
s[PEEK].start_if_stopped(age);
}
}
UNROLLING => {
s[ROLL_OUT].start_if_stopped(age);
s[ROLL_UP].stop();
s[PEEK].stop();
}
_ => {
s[ROLL_OUT].stop();
s[ROLL_UP].stop();
s[PEEK].stop();
}
}
}
/// `ArmadilloAi.ArmadilloPanic`: it unrolls, then runs.
#[derive(Clone, Debug)]
struct ArmadilloPanic(Panic);
impl Goal for ArmadilloPanic {
goal_clone!();
fn flags(&self) -> u8 {
self.0.flags()
}
fn name(&self) -> &'static str {
"Panic"
}
fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
self.0.can_use(m, cx)
}
fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
self.0.can_continue(m, cx)
}
fn start(&mut self, m: &mut Mob, cx: &mut Cx) {
roll_out(m, cx.events);
self.0.start(m, cx);
}
fn stop(&mut self, m: &mut Mob, cx: &mut Cx) {
self.0.stop(m, cx);
}
}
/// The behaviours that do not run while the armadillo is a ball: its
/// `MoveToTargetSink` and the idle activity's walking and looking.
#[derive(Clone, Debug)]
struct Rolled;
impl Goal for Rolled {
goal_clone!();
fn flags(&self) -> u8 {
MOVE | LOOK
}
fn can_use(&mut self, m: &mut Mob, _cx: &mut Cx) -> bool {
scared(m)
}
}
+1 -1
View File
@@ -1007,7 +1007,7 @@ pub fn anim(m: &Mob, s: &State, p: &mut Poses, a: &Animations) -> bool {
p,
s,
&Armadillo {
hiding_in_shell: false,
hiding_in_shell: super::armadillo::hiding_in_shell(m),
roll_out: st[0],
roll_up: st[1],
peek: st[2],
+11 -1
View File
@@ -654,12 +654,15 @@ impl Goal for Float {
#[derive(Clone, Debug)]
pub struct Panic {
pub speed: f64,
/// Only the damage of `#panic_environmental_causes` (an armadillo).
pub environmental: bool,
pos: [f64; 3],
}
impl Panic {
pub fn new(speed: f64) -> Self {
Self {
speed,
environmental: false,
pos: [0.0; 3],
}
}
@@ -675,7 +678,14 @@ impl Goal for Panic {
MOVE
}
fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
if !should_panic(m) {
let wanted = if self.environmental {
m.last_damage.is_some_and(|(d, t)| {
super::armadillo::environmental(d.kind) && i64::from(m.age) - t <= 40
}) || m.fire_ticks > 0 && !m.kind.flags.fire_immune
} else {
should_panic(m)
};
if !wanted {
return false;
}
if m.fire_ticks > 0 {
@@ -165,6 +165,12 @@ impl AnimationState {
pub fn stop(&mut self) {
self.started = None;
}
/// `AnimationState.fastForward`: the animation is `ticks` further on.
pub fn fast_forward(&mut self, ticks: i32) {
if let Some(s) = &mut self.started {
*s -= ticks as f32;
}
}
pub fn animate_when(&mut self, condition: bool, age: f32) {
if condition {
self.start_if_stopped(age)
+5 -1
View File
@@ -15,7 +15,7 @@
//! them in `entity_event`;
//! - run its client side animation in `client_tick`, sharing the function
//! with the server's `ai_step` where the server computes the same.
use super::{Mob, Species, breeze, camel, shulker, sniffer, warden};
use super::{Mob, Species, armadillo, breeze, camel, shulker, sniffer, warden};
// `Pose.id` of the poses mobs take.
pub const STANDING: u8 = 0;
@@ -60,6 +60,7 @@ pub fn client_tick(m: &mut Mob, game_time: i64) {
Species::Breeze => breeze::ai_step(m),
Species::Camel | Species::CamelHusk => camel::client_tick(m, game_time),
Species::Shulker => shulker::client_tick(m),
Species::Armadillo => armadillo::tick(m),
_ => {}
}
}
@@ -67,6 +68,9 @@ pub fn client_tick(m: &mut Mob, game_time: i64) {
/// `handleEntityEvent` of the mob types that start animations by an event
/// (`ClientboundEntityEventPacket` for a mob of a client).
pub fn entity_event(m: &mut Mob, event: u8) {
if m.species() == Species::Armadillo {
armadillo::entity_event(m, event);
}
let age = m.age as f32;
if m.species() == Species::Warden {
match event {