From b0ce58adeab076fe9bd0eb93ff3c985032be6267 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Mon, 26 Oct 2026 21:48:18 +0100 Subject: [PATCH] Check the cat's owner and sync lying and relaxing to clients --- crates/horizon-client/src/game/lan.rs | 9 ++++++ crates/horizon-client/src/game/lan_mobs.rs | 16 +++++++++- crates/horizon-client/src/game/remote.rs | 5 ++++ .../src/game/remote_mob_data.rs | 3 ++ crates/horizon-client/src/mobs/tame.rs | 29 ++++++++++++++----- 5 files changed, 54 insertions(+), 8 deletions(-) diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 5292cd31b..44c669bd6 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -252,6 +252,15 @@ fn mob_meta( if let Some(v) = id(registry, name) { meta.push((index, serializer, Value::Id(v))); } + // Cat.IS_LYING and RELAX_STATE_ONE. + if kind == "minecraft:cat" { + for (index, on) in [(21, e.flag), (22, e.relax_one)] { + if on { + meta.push((index, serializer::BOOLEAN, Value::Bool(true))); + } + defaults.push((index, serializer::BOOLEAN, Value::Bool(false))); + } + } // Wolf.DATA_COLLAR_COLOR and Cat's, red by default. let collar = if kind == "minecraft:cat" { 23 } else { 21 }; let red = i32::from(horizon_client::mobs::tame::RED); diff --git a/crates/horizon-client/src/game/lan_mobs.rs b/crates/horizon-client/src/game/lan_mobs.rs index 1d3b3d086..0d37de086 100644 --- a/crates/horizon-client/src/game/lan_mobs.rs +++ b/crates/horizon-client/src/game/lan_mobs.rs @@ -5,7 +5,7 @@ //! (hits, pushes, fire, its projectiles) runs with that guest swapped in. use super::{Game, lan::LanHost}; use horizon_client::{ - mobs::{Mob, MobEvent, PlayerView, Who}, + mobs::{Mob, MobEvent, PlayerView, Species, Who}, projectiles::{Hit, Projectile}, }; use horizon_core::geometry::{Aabb, Vec3}; @@ -26,6 +26,20 @@ pub(super) fn player_for( guests: &GuestViews, bound: Option, ) -> Option { + // A tame cat reads its owner wherever they are while they sleep, or just + // woke up (`CatRelaxOnOwnerGoal` uses `getOwner`, not the nearest player). + if m.species() == Species::Cat + && m.extra.tame + && let Some(owner) = m.extra.owner + { + let asleep = |v: &PlayerView| v.uuid == owner && (v.sleeping || v.sleep_timer > 0); + if host.is_some_and(asleep) { + return None; + } + if let Some((conn, _)) = guests.iter().find(|(_, v)| asleep(v)) { + return Some(*conn); + } + } let targeting = m.ai.target == Some(Who::Player) || m.ai.last_hurt_by == Some(Who::Player) && m.age as i32 - m.ai.last_hurt_by_time <= 100; choose(m.position, targeting, host, guests, bound) diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 3cfbd15e3..3eb6b56db 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -3535,6 +3535,11 @@ impl Game<'_> { m.head_yaw += wrap_degrees(*yaw - m.head_yaw) / *steps as f32; *steps -= 1; } + // Cat.tick: the lying and relaxing amounts follow the synched flags. + if m.species() == horizon_client::mobs::Species::Cat { + m.anim.vo = m.anim.v; + horizon_client::mobs::tame::cat_amounts(m); + } // Chicken.aiStep: the wings follow the server's on-ground flag. if m.species() == horizon_client::mobs::Species::Chicken { horizon_client::mobs::farm::chicken_flap(m); diff --git a/crates/horizon-client/src/game/remote_mob_data.rs b/crates/horizon-client/src/game/remote_mob_data.rs index c6702dfb6..3d775d6dd 100644 --- a/crates/horizon-client/src/game/remote_mob_data.rs +++ b/crates/horizon-client/src/game/remote_mob_data.rs @@ -71,6 +71,9 @@ fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin, local_enti m.extra.sitting = flags & 1 != 0; m.extra.tame = flags & 4 != 0; } + // Cat.IS_LYING and RELAX_STATE_ONE. + (Species::Cat, 21, Value::Bool(lying)) => m.extra.flag = *lying, + (Species::Cat, 22, Value::Bool(relaxing)) => m.extra.relax_one = *relaxing, // Wolf.DATA_COLLAR_COLOR and Cat's. (Species::Wolf, 21, Value::Int(c)) | (Species::Cat, 23, Value::Int(c)) => { m.extra.color = (*c & 15) as u8; diff --git a/crates/horizon-client/src/mobs/tame.rs b/crates/horizon-client/src/mobs/tame.rs index 7998b4763..4396c4487 100644 --- a/crates/horizon-client/src/mobs/tame.rs +++ b/crates/horizon-client/src/mobs/tame.rs @@ -800,6 +800,11 @@ fn empty_above(w: &MobWorld, p: [i32; 3]) -> bool { .as_ref() == "minecraft:air" } +/// Whether `p` is the player that owns `m` (a cat without a recorded owner +/// belongs to whoever it sees). +pub fn is_owner(m: &Mob, p: &super::PlayerView) -> bool { + m.extra.owner.is_none_or(|o| o == p.uuid) +} /// `Cat.CatRelaxOnOwnerGoal`: while the owner sleeps, the cat lies down beside /// them, and may leave a morning gift when they wake up. #[derive(Clone, Debug, Default)] @@ -827,11 +832,12 @@ impl CatRelaxOnOwner { o.species == Species::Cat && o.id != m.id && o.lying && o.bounds.intersects(area) }) } - /// The owner's sleeping state, when this cat may relax on them. + /// The owner's sleeping state, when this cat may relax on them: the + /// player it sees has to be its owner (`Cat.getOwner`). fn sleeping_owner<'w>(m: &Mob, w: &'w MobWorld) -> Option<&'w super::PlayerView> { w.player .as_ref() - .filter(|p| m.extra.tame && !m.extra.sitting && p.sleeping) + .filter(|p| m.extra.tame && !m.extra.sitting && p.sleeping && is_owner(m, p)) } } impl Goal for CatRelaxOnOwner { @@ -891,7 +897,11 @@ impl Goal for CatRelaxOnOwner { } else { 0.0 }; - if cx.w.player.as_ref().is_some_and(|p| p.sleep_timer >= 100) + if cx + .w + .player + .as_ref() + .is_some_and(|p| is_owner(m, p) && p.sleep_timer >= 100) && m.random.next_float() < chance { give_morning_gift(m, cx); @@ -1127,6 +1137,15 @@ impl Goal for OcelotAttack { } /// `Cat.tick`: lying and relaxing amounts and the purr. pub fn cat_ai_step(m: &mut Mob, events: &mut Vec) { + cat_amounts(m); + if (m.extra.flag || m.extra.relax_one) && 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)); + } +} +/// The lying and relaxing amounts of `Cat.tick` (also a client's, from the +/// synched flags). +pub fn cat_amounts(m: &mut Mob) { let lying = m.extra.flag; let relaxing = m.extra.relax_one; let v = &mut m.anim.v; @@ -1142,10 +1161,6 @@ pub fn cat_ai_step(m: &mut Mob, events: &mut Vec) { } else { (v[RELAX] - 0.13).max(0.0) }; - if (lying || relaxing) && 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 {