Check the cat's owner and sync lying and relaxing to clients
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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<u32>,
|
||||
) -> Option<u32> {
|
||||
// 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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<MobEvent>) {
|
||||
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<MobEvent>) {
|
||||
} 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 {
|
||||
|
||||
Reference in New Issue
Block a user