diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index 535efe62b..c3b9b1af6 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -157,6 +157,19 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag { } map.insert("powered".into(), Tag::Byte(i8::from(m.extra.charged))); } + "minecraft:cat" => { + if let Some(owner) = m.extra.owner { + map.insert("Owner".into(), Tag::IntArray(owner.to_vec())); + } else { + map.remove(&"Owner".into()); + } + map.insert("Sitting".into(), Tag::Byte(i8::from(m.extra.sitting))); + map.insert("CollarColor".into(), Tag::Byte(m.extra.color as i8)); + let variant = crate::mobs::tame::CAT_VARIANTS[(m.extra.variant as usize).min(10)]; + map.insert("variant".into(), string(&format!("minecraft:{variant}"))); + map.entry("sound_variant".into()) + .or_insert_with(|| string("minecraft:classic")); + } "minecraft:wolf" => { if let Some(owner) = m.extra.owner { map.insert("Owner".into(), Tag::IntArray(owner.to_vec())); @@ -238,6 +251,21 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap>, id: u32) - if m.species() == crate::mobs::Species::Creeper { m.extra.charged = flag(tag, "powered"); } + if m.species() == crate::mobs::Species::Cat { + m.extra.owner = crate::save::uuid_from_tag(tag.get("Owner")); + m.extra.tame = m.extra.owner.is_some(); + m.extra.sitting = flag(tag, "Sitting"); + m.extra.color = + number(tag.get("CollarColor")).map_or(crate::mobs::tame::RED, |c| (c as u8) & 15); + if let Some(Tag::String(v)) = tag.get("variant") { + let v = v.to_string().unwrap_or_default(); + let v = v.trim_start_matches("minecraft:"); + m.extra.variant = crate::mobs::tame::CAT_VARIANTS + .iter() + .position(|n| *n == v) + .unwrap_or(0) as u8; + } + } if m.species() == crate::mobs::Species::Wolf { let health = m.health; m.extra.owner = crate::save::uuid_from_tag(tag.get("Owner")); diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 95d2bc9c3..431e3afe7 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -160,6 +160,7 @@ impl Game<'_> { desert: b.name == "minecraft:desert", jungle: has("minecraft:is_jungle"), wolf: horizon_client::mobs::tame::wolf_variant_for(&b.name, &has), + full_moon: (self.world.day_time / 24000).rem_euclid(8) == 0, frog: if has("minecraft:spawns_warm_variant_frogs") { 1 } else if has("minecraft:spawns_cold_variant_frogs") { @@ -843,6 +844,27 @@ impl Game<'_> { && let Some(child) = self.mobs.iter_mut().find(|m| m.id == id) { child.extra.age = -24000; + if p.species() == Species::Cat { + // Cat.getBreedOffspring. + let other = q.as_ref().unwrap_or(&p); + child.extra.variant = if coin { + p.extra.variant + } else { + other.extra.variant + }; + if p.extra.tame { + child.extra.tame = true; + child.extra.owner = p.extra.owner; + child.extra.color = if coin2 { + p.extra.color + } else { + other.extra.color + }; + } + let xp = 1 + self.world.random.next_int_bound(7).unwrap_or(0) as u32; + self.spawn_orbs([p.position.x, p.position.y, p.position.z], xp); + return; + } if p.species() == Species::Wolf { // Wolf.getBreedOffspring: a parent's variant; tame parents // give a tame baby with one of their collar colours. @@ -1464,6 +1486,74 @@ impl Game<'_> { m.position.z.floor() as i32 >> 4, )); } + // Cat.mobInteract: dye, heal or sit when tame; fish may tame a wild one. + Species::Cat + if m.extra.tame + && item.ends_with("_dye") + && horizon_client::mobs::render::DYE_NAMES + .iter() + .position(|d| { + item.trim_start_matches("minecraft:") + .trim_end_matches("_dye") + == *d + }) + .is_some_and(|c| c as u8 != m.extra.color) => + { + let name = item + .trim_start_matches("minecraft:") + .trim_end_matches("_dye"); + if let Some(c) = horizon_client::mobs::render::DYE_NAMES + .iter() + .position(|d| *d == name) + { + m.extra.color = c as u8; + } + m.persistent = true; + consume = true; + } + Species::Cat if m.extra.tame && is_food && m.health < m.attrs.max_health => { + let heal = self + .res + .items + .get(&item) + .and_then(|i| i.food.as_ref()) + .map_or(1.0, |f| f.0 as f32); + m.health = (m.health + heal).min(m.attrs.max_health); + consume = true; + sound = Some("entity.cat.eat"); + } + Species::Cat + if m.extra.tame && !(is_food && m.extra.age == 0 && m.extra.in_love <= 0) => + { + m.extra.sitting = !m.extra.sitting; + self.world.modified.insert(( + m.position.x.floor() as i32 >> 4, + m.position.z.floor() as i32 >> 4, + )); + } + Species::Cat if !m.extra.tame && is_food => { + consume = true; + sound = Some("entity.cat.eat"); + m.persistent = true; + let at = [ + m.position.x, + m.position.y + f64::from(m.height()) * 0.5 + 0.5, + m.position.z, + ]; + let tamed = m.random.next_int_bound(3).unwrap_or(1) == 0; + if tamed { + m.extra.tame = true; + m.extra.owner = self.level.player_uuid.or(Some([0, 0, 0, 1])); + m.extra.sitting = true; + } + let kind = if tamed { "heart" } else { "smoke" }; + self.handle_mob_events(vec![MobEvent::Particles { kind, at, count: 7 }])?; + let m = &self.mobs[index]; + self.world.modified.insert(( + m.position.x.floor() as i32 >> 4, + m.position.z.floor() as i32 >> 4, + )); + } Species::Creeper if item == "minecraft:flint_and_steel" => { m.extra.flag = true; sound = Some("item.flintandsteel.use"); diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 73e110231..30bfa8665 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -96,7 +96,7 @@ pub fn family(s: Species) -> Family { | S::Dolphin => Family::Aquatic, S::Giant => Family::Inert, S::Witch => Family::Witch, - S::Wolf => Family::Tame, + S::Wolf | S::Cat => Family::Tame, S::Enderman => Family::Enderman, S::Slime | S::MagmaCube => Family::Cube, _ => Family::Generic, @@ -111,6 +111,7 @@ pub fn register(m: &mut Mob) { Family::Generic => generic(m), Family::Inert => {} Family::Witch => super::witch::register(m), + Family::Tame if m.species() == Species::Cat => super::tame::register_cat(m), Family::Tame => super::tame::register(m), Family::Enderman => super::enderman::register(m), Family::Cube => super::cube::register(m), @@ -460,6 +461,7 @@ fn species_ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { } Species::Enderman => super::enderman::ai_step(m, w, events), Species::Wolf => super::tame::ai_step(m, w, events), + Species::Cat => super::tame::cat_ai_step(m, events), Species::Slime | Species::MagmaCube => super::cube::ai_step(m, w, events), Species::Chicken => farm::chicken_ai_step(m, w, events), Species::Sheep if m.extra.anim_ticks > 0 => { diff --git a/crates/horizon-client/src/mobs/anim.rs b/crates/horizon-client/src/mobs/anim.rs index c9318a880..e146206a6 100644 --- a/crates/horizon-client/src/mobs/anim.rs +++ b/crates/horizon-client/src/mobs/anim.rs @@ -419,6 +419,7 @@ pub fn setup( } Anim::Witch => super::witch::anim(m, s, p), Anim::Wolf => super::tame::anim(m, s, p), + Anim::Feline if m.species() == super::Species::Cat => super::tame::cat_anim(m, s, p), Anim::Enderman => super::enderman::anim(m, s, p), Anim::Slime | Anim::MagmaCube => super::cube::anim(m, s, p), Anim::Creeper => { diff --git a/crates/horizon-client/src/mobs/goals.rs b/crates/horizon-client/src/mobs/goals.rs index dbb01ee8e..ecdb6807f 100644 --- a/crates/horizon-client/src/mobs/goals.rs +++ b/crates/horizon-client/src/mobs/goals.rs @@ -1412,7 +1412,7 @@ impl Goal for Breed { } let species = m.species(); // Wolf.canMate: both tame and standing. - let tamable = species == Species::Wolf; + let tamable = matches!(species, Species::Wolf | Species::Cat); if tamable && (!m.extra.tame || m.extra.sitting) { return false; } @@ -1765,3 +1765,114 @@ impl Goal for TryFindWater { m.nav_move_to(cx.w, self.wanted, 1.0); } } +/// `MoveToBlockGoal`'s shared state: search, walk to and stay at a block. +#[derive(Clone, Debug, Default)] +pub struct MoveToBlock { + pub speed: f64, + pub range: i32, + pub vertical_range: i32, + pub vertical_start: i32, + pub next_start: i32, + pub try_ticks: i32, + pub max_stay: i32, + pub reached: bool, + pub block: [i32; 3], +} +impl MoveToBlock { + pub fn new(speed: f64, range: i32, vertical_range: i32) -> Self { + Self { + speed, + range, + vertical_range, + ..Default::default() + } + } + /// `canUse` with a custom `nextStartTick` delay. + pub fn can_use( + &mut self, + m: &mut Mob, + w: &MobWorld, + next: i32, + valid: impl Fn(&MobWorld, [i32; 3]) -> bool, + ) -> bool { + if self.next_start > 0 { + self.next_start -= 1; + return false; + } + self.next_start = next; + self.find_nearest(m, w, valid) + } + /// `MoveToBlockGoal.findNearestBlock`: rings outward, alternating heights. + fn find_nearest( + &mut self, + m: &Mob, + w: &MobWorld, + valid: impl Fn(&MobWorld, [i32; 3]) -> bool, + ) -> bool { + let base = [ + m.position.x.floor() as i32, + m.position.y.floor() as i32, + m.position.z.floor() as i32, + ]; + let next = |v: i32| if v > 0 { -v } else { 1 - v }; + let mut y = self.vertical_start; + while y <= self.vertical_range { + for r in 0..self.range { + let mut x = 0; + while x <= r { + let mut z = if x < r && x > -r { r } else { 0 }; + while z <= r { + let p = [base[0] + x, base[1] + y - 1, base[2] + z]; + if valid(w, p) { + self.block = p; + return true; + } + z = next(z); + } + x = next(x); + } + } + y = next(y); + } + false + } + pub fn can_continue(&self, w: &MobWorld, valid: impl Fn(&MobWorld, [i32; 3]) -> bool) -> bool { + self.try_ticks >= -self.max_stay && self.try_ticks <= 1200 && valid(w, self.block) + } + pub fn start(&mut self, m: &mut Mob, w: &MobWorld) { + let b = self.block; + m.nav_move_to( + w, + [ + f64::from(b[0]) + 0.5, + f64::from(b[1] + 1), + f64::from(b[2]) + 0.5, + ], + self.speed, + ); + self.try_ticks = 0; + let r = m.random.next_int_bound(1200).unwrap_or(0) + 1200; + self.max_stay = m.random.next_int_bound(r).unwrap_or(0) + 1200; + } + /// `MoveToBlockGoal.tick` (accepted distance 1 from the block above). + pub fn tick(&mut self, m: &mut Mob, w: &MobWorld) { + let t = [ + f64::from(self.block[0]) + 0.5, + f64::from(self.block[1] + 1) + 0.5, + f64::from(self.block[2]) + 0.5, + ]; + let d2 = (m.position.x - t[0]).powi(2) + + (m.position.y - t[1]).powi(2) + + (m.position.z - t[2]).powi(2); + if d2 >= 1.0 { + self.reached = false; + self.try_ticks += 1; + if self.try_ticks % 40 == 0 { + m.nav_move_to(w, [t[0], t[1] - 0.5, t[2]], self.speed); + } + } else { + self.reached = true; + self.try_ticks -= 1; + } + } +} diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs index 527ccc83e..7d13c6400 100644 --- a/crates/horizon-client/src/mobs/render.rs +++ b/crates/horizon-client/src/mobs/render.rs @@ -107,6 +107,10 @@ pub fn body(m: &Mob) -> (String, String) { baby_or(m, "wolf#main", "wolf_baby#main"), super::tame::texture(m), ), + S::Cat => ( + baby_or(m, "cat#main", "cat_baby#main"), + super::tame::cat_texture(m), + ), S::Frog => ( "frog#main".into(), format!("entity/frog/frog_{}", climate(m)), @@ -274,6 +278,21 @@ pub fn layers(m: &Mob) -> Vec { false, ); } + // CatCollarLayer. + S::Cat if m.extra.tame => { + let (model, texture) = if m.is_baby() { + ("cat_baby#collar", "entity/cat/cat_collar_baby") + } else { + ("cat#collar", "entity/cat/cat_collar") + }; + push( + &mut v, + model, + texture, + abgr(DYE[m.extra.color as usize & 15]), + false, + ); + } // SlimeOuterLayer: the translucent outer cube. S::Slime => push(&mut v, "slime#outer", "entity/slime/slime", white, false), S::Sheep if !m.extra.sheared => { @@ -476,9 +495,11 @@ fn flip_degrees(m: &Mob) -> f32 { _ => 90.0, } } -fn species_rotations(_m: &Mob, _alpha: f32) -> glam::Mat4 { - let _ = PI; - glam::Mat4::IDENTITY +fn species_rotations(m: &Mob, alpha: f32) -> glam::Mat4 { + match m.species() { + Species::Cat => super::tame::cat_rotations(m, alpha), + _ => glam::Mat4::IDENTITY, + } } fn species_scale(m: &Mob, alpha: f32) -> glam::Mat4 { match m.species() { diff --git a/crates/horizon-client/src/mobs/spawning.rs b/crates/horizon-client/src/mobs/spawning.rs index 4ea244492..83840cede 100644 --- a/crates/horizon-client/src/mobs/spawning.rs +++ b/crates/horizon-client/src/mobs/spawning.rs @@ -22,6 +22,8 @@ pub struct BiomeFacts { pub frog: u8, /// Wolf variant index (`tame::WOLF_VARIANTS`). pub wolf: u8, + /// `MoonBrightnessCheck(>= 0.9)`: the full moon. + pub full_moon: bool, } /// SpawnPlacementTypes for the species. pub fn placement_ok(species: Species, w: &MobWorld, x: i32, y: i32, z: i32) -> bool { @@ -172,6 +174,7 @@ pub fn finalize(m: &mut Mob, biome: &BiomeFacts) { S::Mooshroom => m.extra.variant = 0, S::Frog => m.extra.variant = biome.frog, S::Wolf => m.extra.variant = biome.wolf, + S::Cat => m.extra.variant = super::tame::cat_variant(m, biome.full_moon), S::Zombie | S::Husk | S::Drowned | S::ZombieVillager => { // Zombie.finalizeSpawn: 5% babies (normal difficulty). if m.random.next_float() < 0.05 { diff --git a/crates/horizon-client/src/mobs/tame.rs b/crates/horizon-client/src/mobs/tame.rs index 674b3f2c7..1b6ef9d66 100644 --- a/crates/horizon-client/src/mobs/tame.rs +++ b/crates/horizon-client/src/mobs/tame.rs @@ -672,3 +672,389 @@ fn max_head_x_rot(m: &Mob) -> f32 { m.kind.max_head_x_rot } } + +/// Cat variants (`CatVariants`): registry name, texture file. +pub const CAT_VARIANTS: [&str; 11] = [ + "tabby", + "black", + "red", + "siamese", + "british_shorthair", + "calico", + "persian", + "ragdoll", + "white", + "jellie", + "all_black", +]; +// Cat AnimState slots. +const LIE: usize = 0; +const LIE_TAIL: usize = 1; +const RELAX: usize = 2; + +/// `Cat.registerGoals` (with `reassessTameGoals`' player avoidance). +pub fn register_cat(m: &mut Mob) { + m.extra.color = RED; + let food = m.kind.food.clone(); + let g = &mut m.ai.goals; + g.add(1, Float); + g.add(1, TamePanic(Panic::new(1.5))); + g.add(2, SitWhenOrdered); + g.add(4, CatTempt(super::goals::Tempt::new(0.6, food, true))); + g.add( + 4, + super::goals::AvoidEntity::new(super::goals::Avoid::Player, 16.0, 0.8, 1.33) + .when(|m| !m.extra.tame), + ); + g.add( + 5, + CatLieOnBlock(super::goals::MoveToBlock { + vertical_start: -2, + ..super::goals::MoveToBlock::new(1.1, 8, 6) + }), + ); + g.add(6, FollowOwner::new(1.0, 10.0, 5.0)); + g.add(7, CatSitOnBlock(super::goals::MoveToBlock::new(0.8, 8, 1))); + g.add(8, LeapAtTarget::new(0.3)); + g.add(9, OcelotAttack { attack_time: 0 }); + g.add(10, Breed::new(0.8)); + g.add(11, Stroll::avoiding_water(0.8)); + g.add(12, LookAtPlayer::new(10.0)); + m.ai.targets.add( + 1, + NearestTarget::species(&[Species::Rabbit], false).when(|m, _| !m.extra.tame), + ); + m.ai.targets.add( + 1, + NearestTarget::new(Prey::Filter(baby_turtle_on_land), false).when(|m, _| !m.extra.tame), + ); +} +/// `Cat.isInSittingPose`: ordered to sit or sitting on a block. +pub fn cat_sitting(m: &Mob) -> bool { + m.extra.sitting || m.extra.flag2 +} +/// `Cat.CatTemptGoal`: only wild cats come for fish. +#[derive(Clone, Debug)] +struct CatTempt(super::goals::Tempt); +impl Goal for CatTempt { + goal_clone!(); + fn flags(&self) -> u8 { + self.0.flags() + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + !m.extra.tame && 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) { + self.0.start(m, cx) + } + fn stop(&mut self, m: &mut Mob, cx: &mut Cx) { + self.0.stop(m, cx) + } + fn every_tick(&self) -> bool { + self.0.every_tick() + } + fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { + // Cat.tick: a wild cat being tempted begs every 100 ticks. + if m.age.is_multiple_of(100) { + cx.events.push(MobEvent::Sound( + "entity.cat.beg_for_food", + m.center(), + 1.0, + 1.0, + )); + } + self.0.tick(m, cx) + } +} +fn has_tag(w: &MobWorld, p: [i32; 3], tag: &str) -> bool { + let name = w.facts.get(w.world.block(p[0], p[1], p[2])).name.clone(); + w.block_tags.get(tag).is_some_and(|t| t.contains(&*name)) +} +fn empty_above(w: &MobWorld, p: [i32; 3]) -> bool { + w.facts + .get(w.world.block(p[0], p[1] + 1, p[2])) + .name + .as_ref() + == "minecraft:air" +} +/// `CatLieOnBlockGoal`: tame cats lie on beds and other warm blocks. +#[derive(Clone, Debug)] +struct CatLieOnBlock(super::goals::MoveToBlock); +fn can_lie_on(w: &MobWorld, p: [i32; 3]) -> bool { + empty_above(w, p) && has_tag(w, p, "minecraft:cats_can_lie_on") +} +impl Goal for CatLieOnBlock { + goal_clone!(); + fn flags(&self) -> u8 { + JUMP | MOVE + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + m.extra.tame + && !m.extra.sitting + && !m.extra.flag + && self.0.can_use(m, cx.w, reduced(40), can_lie_on) + } + fn can_continue(&mut self, _: &mut Mob, cx: &mut Cx) -> bool { + self.0.can_continue(cx.w, can_lie_on) + } + fn start(&mut self, m: &mut Mob, cx: &mut Cx) { + self.0.start(m, cx.w); + m.extra.flag2 = false; + } + fn stop(&mut self, m: &mut Mob, _: &mut Cx) { + m.extra.flag = false; + } + fn every_tick(&self) -> bool { + true + } + fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { + self.0.tick(m, cx.w); + m.extra.flag2 = false; + m.extra.flag = self.0.reached; + } +} +/// `CatSitOnBlockGoal`: chests, lit furnaces and beds. +#[derive(Clone, Debug)] +struct CatSitOnBlock(super::goals::MoveToBlock); +fn can_sit_on(w: &MobWorld, p: [i32; 3]) -> bool { + if !empty_above(w, p) || !has_tag(w, p, "minecraft:cats_can_sit_on") { + return false; + } + let f = w.facts.get(w.world.block(p[0], p[1], p[2])); + match f.name.as_ref() { + // The lit state is the only light source a furnace has. + "minecraft:furnace" => f.emission > 0, + _ => true, + } +} +impl Goal for CatSitOnBlock { + goal_clone!(); + fn flags(&self) -> u8 { + JUMP | MOVE + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + let next = reduced(200 + m.random.next_int_bound(200).unwrap_or(0)); + m.extra.tame && !m.extra.sitting && self.0.can_use(m, cx.w, next, can_sit_on) + } + fn can_continue(&mut self, _: &mut Mob, cx: &mut Cx) -> bool { + self.0.can_continue(cx.w, can_sit_on) + } + fn start(&mut self, m: &mut Mob, cx: &mut Cx) { + self.0.start(m, cx.w); + m.extra.flag2 = false; + } + fn stop(&mut self, m: &mut Mob, _: &mut Cx) { + m.extra.flag2 = false; + } + fn every_tick(&self) -> bool { + true + } + fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { + self.0.tick(m, cx.w); + m.extra.flag2 = self.0.reached; + } +} +/// `OcelotAttackGoal`: stalk slowly, dash in, swipe every second. +#[derive(Clone, Debug)] +struct OcelotAttack { + attack_time: i32, +} +impl Goal for OcelotAttack { + goal_clone!(); + fn flags(&self) -> u8 { + MOVE | LOOK + } + fn can_use(&mut self, m: &mut Mob, _: &mut Cx) -> bool { + m.ai.target.is_some() + } + fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + let Some(t) = m.ai.target.and_then(|t| cx.w.target_info(t)) else { + return false; + }; + t.alive + && m.distance_sqr(t.position) <= 225.0 + && (!m.ai.nav.is_done() || m.ai.target.is_some()) + } + fn stop(&mut self, m: &mut Mob, _: &mut Cx) { + m.nav_stop(); + } + fn every_tick(&self) -> bool { + true + } + fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { + let Some(who) = m.ai.target else { + return; + }; + let Some(t) = cx.w.target_info(who) else { + return; + }; + m.look_at_speed([t.position.x, t.eye_y, t.position.z], 30.0, 30.0); + let reach = f64::from(m.width() * 2.0); + let melee = reach * reach; + let d2 = m.distance_sqr(t.position); + let speed = if d2 > melee && d2 < 16.0 { + 1.33 + } else if d2 < 225.0 { + 0.6 + } else { + 0.8 + }; + m.nav_move_to_entity(cx.w, [t.position.x, t.position.y, t.position.z], 0, speed); + self.attack_time = (self.attack_time - 1).max(0); + if d2 <= melee && self.attack_time <= 0 { + self.attack_time = 20; + super::ai::do_hurt_target(m, who, cx); + } + } +} +/// `Cat.tick`: lying and relaxing amounts and the purr. +pub fn cat_ai_step(m: &mut Mob, events: &mut Vec) { + let lying = m.extra.flag; + let v = &mut m.anim.v; + if lying { + v[LIE] = (v[LIE] + 0.15).min(1.0); + v[LIE_TAIL] = (v[LIE_TAIL] + 0.08).min(1.0); + } else { + v[LIE] = (v[LIE] - 0.22).max(0.0); + v[LIE_TAIL] = (v[LIE_TAIL] - 0.13).max(0.0); + } + v[RELAX] = (v[RELAX] - 0.13).max(0.0); + if lying && m.age.is_multiple_of(5) { + let volume = 0.6 + 0.4 * (m.random.next_float() - m.random.next_float()); + events.push(MobEvent::Sound("entity.cat.purr", m.center(), volume, 1.0)); + } +} +/// `Cat.finalizeSpawn` variant: any but all-black, which a full moon allows. +pub fn cat_variant(m: &mut Mob, w_moon_full: bool) -> u8 { + let n = if w_moon_full { 11 } else { 10 }; + m.random.next_int_bound(n).unwrap_or(0) as u8 +} +pub fn cat_texture(m: &Mob) -> String { + let v = CAT_VARIANTS[(m.extra.variant as usize).min(10)]; + let baby = if m.is_baby() { "_baby" } else { "" }; + format!("entity/cat/cat_{v}{baby}") +} +/// `CatRenderer.setupRotations`: lying cats roll onto their side. +pub fn cat_rotations(m: &Mob, partial: f32) -> glam::Mat4 { + let lie = m.anim.lerp(LIE, partial); + if lie <= 0.0 { + return glam::Mat4::IDENTITY; + } + glam::Mat4::from_translation(glam::Vec3::new(0.4 * lie, 0.15 * lie, 0.1 * lie)) + * glam::Mat4::from_rotation_z((90.0 * lie).to_radians()) +} +fn rot_lerp(t: f32, from: f32, to: f32) -> f32 { + let mut d = (to - from) % (PI * 2.0); + if d < -PI { + d += PI * 2.0; + } + if d >= PI { + d -= PI * 2.0; + } + from + t * d +} +/// `AdultFelineModel` / `BabyFelineModel.setupAnim` for cats. +pub fn cat_anim(m: &Mob, s: &State, p: &mut Poses) { + let baby = m.is_baby(); + let age = if baby { 0.5 } else { 1.0 }; + let wanted = m.ai.mov.has_wanted(); + let speed_mod = m.ai.mov.speed_modifier; + // Cat.customServerAiStep: crouch while stalking, sprint while dashing. + let crouching = wanted && (speed_mod - 0.6).abs() < 1e-6; + let sprinting = wanted && (speed_mod - 1.33).abs() < 1e-6; + let sitting = cat_sitting(m); + let add = |p: &mut Poses, part: &str, x: f32, y: f32, z: f32| { + let v = p.get(part); + v[0] += x; + v[1] += y; + v[2] += z; + }; + if crouching { + add(p, "body", 0.0, age, 0.0); + add(p, "head", 0.0, 2.0 * age, 0.0); + add(p, "tail1", 0.0, age, 0.0); + add(p, "tail2", 0.0, -4.0 * age, 2.0 * age); + p.xrot("tail1", PI / 2.0); + p.xrot("tail2", PI / 2.0); + } else if sprinting { + let y1 = p.get("tail1")[1]; + p.get("tail2")[1] = y1; + add(p, "tail2", 0.0, 0.0, 2.0 * age); + p.xrot("tail1", PI / 2.0); + p.xrot("tail2", PI / 2.0); + } + p.xrot("head", s.x_rot.to_radians()); + p.yrot("head", s.y_rot.to_radians()); + let (pos, sp) = (s.pos, s.speed); + if !sitting { + if !baby { + p.xrot("body", PI / 2.0); + } + if sprinting { + p.xrot("left_hind_leg", (pos * 0.6662).cos() * sp); + p.xrot("right_hind_leg", (pos * 0.6662 + 0.3).cos() * sp); + p.xrot("left_front_leg", (pos * 0.6662 + PI + 0.3).cos() * sp); + p.xrot("right_front_leg", (pos * 0.6662 + PI).cos() * sp); + p.xrot("tail2", 1.727_876_1 + PI / 10.0 * pos.cos() * sp); + } else { + p.xrot("left_hind_leg", (pos * 0.6662).cos() * sp); + p.xrot("right_hind_leg", (pos * 0.6662 + PI).cos() * sp); + p.xrot("left_front_leg", (pos * 0.6662 + PI).cos() * sp); + p.xrot("right_front_leg", (pos * 0.6662).cos() * sp); + let swing = if crouching { 0.471_238_94 } else { PI / 4.0 }; + p.xrot("tail2", 1.727_876_1 + swing * pos.cos() * sp); + } + } else if baby { + let x = p.get("body")[3] - 0.436_332_32; + p.xrot("body", x); + add(p, "body", 0.0, 1.25, 0.0); + add(p, "head", 0.0, 0.0, 0.75); + let t = p.get("tail1")[3] + 0.545_415_4; + p.xrot("tail1", t); + add(p, "tail1", 0.0, 4.0, -0.9); + add(p, "left_hind_leg", 0.0, 0.0, -0.9); + add(p, "right_hind_leg", 0.0, 0.0, -0.9); + } else { + p.xrot("body", PI / 4.0); + add(p, "body", 0.0, -4.0 * age, 5.0 * age); + add(p, "head", 0.0, -3.3 * age, age); + add(p, "tail1", 0.0, 8.0 * age, -2.0 * age); + add(p, "tail2", 0.0, 2.0 * age, -0.8 * age); + p.xrot("tail1", 1.727_876_1); + p.xrot("tail2", 2.670_354); + p.xrot("left_front_leg", -PI / 20.0); + add(p, "left_front_leg", 0.0, 2.0 * age, -2.0 * age); + p.xrot("right_front_leg", -PI / 20.0); + add(p, "right_front_leg", 0.0, 2.0 * age, -2.0 * age); + p.xrot("left_hind_leg", -PI / 2.0); + add(p, "left_hind_leg", 0.0, 3.0 * age, -4.0 * age); + p.xrot("right_hind_leg", -PI / 2.0); + add(p, "right_hind_leg", 0.0, 3.0 * age, -4.0 * age); + } + let lie = m.anim.lerp(LIE, s.partial); + let lie_tail = m.anim.lerp(LIE_TAIL, s.partial); + if lie > 0.0 && !baby { + let (hz, hy) = (p.get("head")[5], p.get("head")[4]); + p.zrot("head", rot_lerp(lie, hz, -1.270_796_3)); + p.yrot("head", rot_lerp(lie, hy, 1.270_796_3)); + p.xrot("left_front_leg", -1.270_796_3); + p.xrot("right_front_leg", -0.470_796_35); + p.zrot("right_front_leg", -0.2); + add(p, "right_front_leg", age, 0.0, 0.0); + p.xrot("left_hind_leg", -0.4); + p.xrot("right_hind_leg", 0.5); + p.zrot("right_hind_leg", -0.5); + add(p, "right_hind_leg", 0.8 * age, 2.0 * age, 0.0); + let (t1, t2) = (p.get("tail1")[3], p.get("tail2")[3]); + p.xrot("tail1", rot_lerp(lie_tail, t1, 0.8)); + p.xrot("tail2", rot_lerp(lie_tail, t2, -0.4)); + } + let relax = m.anim.lerp(RELAX, s.partial); + if relax > 0.0 { + let hx = p.get("head")[3]; + p.xrot("head", rot_lerp(relax, hx, -0.581_776_44)); + } +}