diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 7596ee871..8763a4077 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1983,9 +1983,11 @@ impl Game<'_> { /// item in use (LocalPlayer.modifyInput), the helmet's Respiration /// (decreaseAirSupply), the wings, leather boots on powder snow /// (PowderSnowBlock.canEntityWalkOnPowderSnow), freeze-immune wear - /// (LivingEntity.canFreeze) and the dimension's FAST_LAVA. + /// (LivingEntity.canFreeze), the dimension's FAST_LAVA and the sleeping + /// pose. pub(crate) fn sync_player_body(&mut self) { let glider = self.glider_worn(); + let sleeping = self.sleeping.is_some(); let item_use_speed = self .using .and_then(|(slot, _)| { @@ -2013,6 +2015,7 @@ impl Game<'_> { player.item_use_speed = item_use_speed; player.respiration = respiration; player.glider = glider; + let _ = player.set_sleeping(sleeping); player.leather_boots = leather_boots; player.freeze_immune = freeze_immune; player.fluids.fast_lava = self.dim == Dim::Nether; diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index e39fa3a68..f1a06e98b 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -1428,6 +1428,7 @@ impl Game<'_> { .each_ref() .map(|s| s.as_ref().map(|s| s.item.clone())); m.extra.crouching = g.sneaking; + m.extra.avatar_pose = g.pose(); // Lying in its bed (LivingEntity.getBedOrientation). m.extra.ints[1] = g.bed.map_or(0, |b| self.bed_facing(b)); // LivingEntity.updateSwingTime: the arm swing lan_swings started. diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 98162ae46..d4cbe536c 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -2182,7 +2182,7 @@ impl Game<'_> { } let in_wall = !g.sleeping && self.in_wall(g.position, g.eye_height()); let at = g.position; - let sneaking = g.sneaking; + let (sneaking, sleeping) = (g.sneaking, g.sleeping); let events = self.as_guest(host, conn, |game| { let (rules, motion) = (game.world.health_rules, game.motion); let health = &motion.health; @@ -2250,6 +2250,7 @@ impl Game<'_> { // Entity.applyEffectsFromBlocks along those moves: magma, cactus, // lit campfires and grown sweet berry bushes hurt (#1305). w.player.shift_key_down = sneaking; + let _ = w.player.set_sleeping(sleeping); w.player.freeze_immune = freeze_immune; let _ = w.server_block_damage(start); let _ = w.tick_environment_damage(); @@ -2626,8 +2627,7 @@ impl Game<'_> { /// with Mending and gives the rest as points. Everyone near hears it. fn guest_orbs(&mut self, host: &mut LanHost, gi: usize) { let g = &host.hub.guests[gi]; - // Player's standing and crouching boxes: 1.8 and 1.5 high. - let height = if g.sneaking { 1.5 } else { 1.8 }; + let height = f64::from(g.dimensions().height); let (conn, id, at) = (g.conn, g.entity_id, g.position); let unable = g.spectator() || g.dead || g.health <= 0.0; let play = host.play.entry(conn).or_default(); diff --git a/crates/horizon-client/src/game/lan_mobs.rs b/crates/horizon-client/src/game/lan_mobs.rs index 6527f1015..db042be43 100644 --- a/crates/horizon-client/src/game/lan_mobs.rs +++ b/crates/horizon-client/src/game/lan_mobs.rs @@ -175,9 +175,9 @@ impl Game<'_> { .iter() .filter(|g| host.hub.here(g) && g.loaded && !g.dead && !g.spectator()) .map(|g| { - let [x, y, z] = g.position; - let height = if g.sneaking { 1.5 } else { 1.8 }; - ([x - 0.3, y, z - 0.3], [x + 0.3, y + height, z + 0.3]) + let b = g.bounds(); + let (lo, hi) = (b.min_position(), b.max_position()); + ([lo.x, lo.y, lo.z], [hi.x, hi.y, hi.z]) }) .collect() } @@ -319,7 +319,7 @@ impl Game<'_> { uuid: horizon_client::lan::playerdata::uuid_ints(g.uuid), position: g.position, eye: [x, y + g.eye_height(), z], - height: if g.sneaking { 1.5 } else { 1.8 }, + height: g.dimensions().height, creative: g.creative(), spectator: g.spectator(), alive: !g.dead, @@ -336,14 +336,9 @@ impl Game<'_> { } } -/// A guest's bounding box (0.6 wide, 1.8 tall, 1.5 crouching). +/// A guest's bounding box, by its pose (`Avatar.POSES`). pub(super) fn guest_bounds(g: &horizon_client::lan::guest::Guest) -> Aabb { - let [x, y, z] = g.position; - let height = if g.sneaking { 1.5 } else { 1.8 }; - Aabb::new( - Vec3::new(x - 0.3, y, z - 0.3), - Vec3::new(x + 0.3, y + height, z + 0.3), - ) + g.bounds() } #[cfg(test)] diff --git a/crates/horizon-client/src/game/minecarts.rs b/crates/horizon-client/src/game/minecarts.rs index 072059e49..8a9241a58 100644 --- a/crates/horizon-client/src/game/minecarts.rs +++ b/crates/horizon-client/src/game/minecarts.rs @@ -713,12 +713,7 @@ impl Game<'_> { host.hub.here(g) && g.loaded && !g.dead && !g.spectator() && g.vehicle.is_none() }) { let [x, y, z] = g.position; - let height = if g.sneaking { 1.5 } else { 1.8 }; - let bb = Aabb::new( - Vec3::new(x - 0.3, y, z - 0.3), - Vec3::new(x + 0.3, y + height, z + 0.3), - ); - pushers.push((Vec3::new(x, y, z), bb)); + pushers.push((Vec3::new(x, y, z), g.bounds())); } } for (feet, bb) in pushers { diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 945af4585..509eb972c 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -3555,7 +3555,7 @@ impl Game<'_> { m.extra.main_hand.clone() }; if let Some(item) = item { - let eye = if m.extra.crouching { 1.27 } else { 1.62 }; + let eye = m.eye_height(); let at = [m.position.x, m.position.y, m.position.z]; using.push(( item, diff --git a/crates/horizon-client/src/game/remote_mob_data.rs b/crates/horizon-client/src/game/remote_mob_data.rs index c6702dfb6..a974dd9e4 100644 --- a/crates/horizon-client/src/game/remote_mob_data.rs +++ b/crates/horizon-client/src/game/remote_mob_data.rs @@ -16,6 +16,12 @@ fn apply(m: &mut Mob, values: &[(u8, i32, Value)], builtin: &Builtin, local_enti let baby = super::lan::baby_index(&m.kind.id); for (index, _, value) in values { match (*index, value) { + // Entity.DATA_POSE: a player's or mannequin's box follows it. + (6, Value::Id(pose)) + if matches!(&*m.kind.id, "minecraft:player" | "minecraft:mannequin") => + { + m.extra.avatar_pose = horizon_client::pose::Pose::from_id(*pose); + } // Entity.DATA_SHARED_FLAGS_ID's FLAG_ONFIRE: Entity.isOnFire on a // client, which draws the flames. A client doesn't count the // fire down: the flag goes off when the server's fire is out. diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index 7e43cf04e..2deb5905e 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -2,7 +2,12 @@ //! player state (inventory, health, game mode) that the host's gameplay code //! operates on when it acts for them. use super::{Joined, PlayerSender, entity::Tracker}; -use crate::{items::ItemStack, motion::PlayerMotion}; +use crate::{ + items::ItemStack, + motion::PlayerMotion, + pose::{Dimensions, Pose}, +}; +use horizon_core::geometry::{Aabb, Vec3}; use horizon_protocol::{ item::Stack, packets::{configuration::ClientInformation, play}, @@ -407,8 +412,41 @@ impl Guest { } f } + /// `Player.getDesiredPose`: sleeping, gliding, then the body's own pose + /// (swimming, crawling, crouching) once its server tick has run. + pub fn pose(&self) -> Pose { + if self.sleeping { + Pose::Sleeping + } else if self.fall_flying { + Pose::FallFlying + } else if let Some(m) = &self.motion { + m.pose + } else if self.sneaking && !self.flying { + Pose::Crouching + } else { + Pose::Standing + } + } + /// `Avatar.POSES` for the guest's pose, with its scale attribute. + pub fn dimensions(&self) -> Dimensions { + let pose = self.pose(); + match &self.motion { + Some(m) if m.pose == pose => m.dimensions, + _ => pose.dimensions(1.0), + } + } + /// `Entity.getBoundingBox`. + pub fn bounds(&self) -> Aabb { + let [x, y, z] = self.position; + let d = self.dimensions(); + let half = f64::from(d.width) / 2.0; + Aabb::new( + Vec3::new(x - half, y, z - half), + Vec3::new(x + half, y + f64::from(d.height), z + half), + ) + } pub fn eye_height(&self) -> f64 { - if self.sneaking { 1.27 } else { 1.62 } + f64::from(self.dimensions().eye_height) } /// `Player.isWithinBlockInteractionRange(pos, buffer)` for the guest: /// its block interaction range is 4.5, 5 in creative. diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index b1a8cbc62..c4ce83946 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -16,6 +16,7 @@ use crate::{ collisions::SceneCollisions, effects::{EffectInstance, PlayerEffects}, items::ItemStack, + pose::Pose, world::World, }; use horizon_core::geometry::{Aabb, ShapeOp, Vec3, VoxelShape}; @@ -1507,7 +1508,7 @@ impl Hub { position: [f64; 3], rot: (f32, f32, f32), on_ground: bool, - flags: (bool, bool, bool, bool), + flags: (bool, bool, bool, bool, bool), living_flags: i8, bed: Option<[i32; 3]>, skin: u8, @@ -1515,12 +1516,14 @@ impl Hub { equipment: Vec<(u8, Option)>, attributes: Vec, ) -> EntityState { - let (sneaking, sprinting, gliding, sleeping) = flags; + let (sneaking, sprinting, gliding, sleeping, swimming) = flags; let shared = shared_flags(sneaking, sprinting, gliding); let pose = if sleeping { 2 } else if gliding { 1 + } else if swimming { + 3 } else if sneaking { 5 } else { @@ -1607,7 +1610,7 @@ impl Hub { h.position, (h.yaw, h.pitch, h.head_yaw), h.on_ground, - (h.sneaking, h.sprinting, false, h.bed.is_some()), + (h.sneaking, h.sprinting, false, h.bed.is_some(), false), h.living_flags, h.bed, 0x7F, @@ -1639,7 +1642,13 @@ impl Hub { g.position, (g.yaw, g.pitch, g.head_yaw), g.on_ground, - (g.sneaking, g.sprinting, g.fall_flying, g.sleeping), + ( + g.sneaking, + g.sprinting, + g.fall_flying, + g.sleeping, + g.pose() == Pose::Swimming, + ), g.living_flags, g.bed, g.info.model_customisation, @@ -2194,18 +2203,8 @@ impl Hub { } // getBoundingBox: the box of the body's pose (swimming and crawling // are 0.6 tall), so a swimmer under a ceiling isn't sent back. - let (half, height) = if g.fall_flying { - (0.3, 0.6) - } else if let Some(m) = &g.motion { - ( - f64::from(m.dimensions.width) / 2.0, - f64::from(m.dimensions.height), - ) - } else if g.sneaking { - (0.3, 1.5) - } else { - (0.3, 1.8) - }; + let d = g.dimensions(); + let (half, height) = (f64::from(d.width) / 2.0, f64::from(d.height)); let at = |p: [f64; 3]| { Aabb::new( Vec3::new(p[0] - half, p[1], p[2] - half), diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index f4086216d..34ecab1a7 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -638,6 +638,9 @@ pub struct Attrs { pub struct Extra { /// Players: crouching and riding (HumanoidModel's poses). pub crouching: bool, + /// A player's or mannequin's synced pose (`Entity.DATA_POSE`), which + /// sets its box (`Avatar.POSES`). + pub avatar_pose: crate::pose::Pose, pub passenger: bool, /// AgeableMob.age: negative while a baby (a tadpole counts up to 24000). pub age: i32, diff --git a/crates/horizon-client/src/mobs/living.rs b/crates/horizon-client/src/mobs/living.rs index 394821ace..829603f64 100644 --- a/crates/horizon-client/src/mobs/living.rs +++ b/crates/horizon-client/src/mobs/living.rs @@ -281,6 +281,15 @@ impl Mob { return (f64::from(0.2_f32), f64::from(0.2_f32), f64::from(0.2_f32)); } let k = &self.kind; + // Avatar.POSES: a player's or mannequin's box follows its pose. + if matches!(&*k.id, "minecraft:player" | "minecraft:mannequin") { + let d = self.extra.avatar_pose.dimensions(1.0); + return ( + f64::from(d.width), + f64::from(d.height), + f64::from(d.eye_height), + ); + } let (mut w, mut h, mut e) = (k.width as f32, k.height as f32, k.eye_height as f32); // Sniffer.DIGGING_DIMENSIONS: 0.4 lower, its eyes at 0.81. if k.species == super::Species::Sniffer && super::sniffer::digging(self) { diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs index 077fd38a6..e161e6af1 100644 --- a/crates/horizon-client/src/mobs/render.rs +++ b/crates/horizon-client/src/mobs/render.rs @@ -765,7 +765,14 @@ pub fn base_transform_at(m: &Mob, alpha: f32, origin: glam::Mat4) -> glam::Mat4 let rotation = match sleep { // setupRotations for Pose.SLEEPING, with the head moved toward the pillow. Some((angle, step)) => { - let head = (m.eye_y() - m.position.y) as f32 - 0.1; + // The eye height of the standing pose (LivingEntityRenderer + // extracts `getEyeHeight(Pose.STANDING)` for a bed). + let eye = if avatar(m) { + m.kind.eye_height as f32 + } else { + (m.eye_y() - m.position.y) as f32 + }; + let head = eye - 0.1; glam::Mat4::from_translation(glam::Vec3::new(-step[0] * head, 0.0, -step[1] * head)) * glam::Mat4::from_rotation_y(angle.to_radians()) * glam::Mat4::from_rotation_z(90.0_f32.to_radians()) diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index 234390e23..a71e29abc 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -252,6 +252,8 @@ pub struct PlayerMotion { pub pitch: f32, /// Gliding (shared flag 7) and LivingEntity.fallFlyTicks. pub fall_flying: bool, + /// Asleep in a bed (`LivingEntity.isSleeping`): the sleeping pose. + pub sleeping: bool, pub fall_fly_ticks: i32, /// The server saw the glide end last tick (`updateFallFlying`'s `canGlide` /// failed): the cleared flag reaches the client at its next tick. @@ -446,6 +448,7 @@ impl PlayerMotion { glider: false, pitch: 0.0, fall_flying: false, + sleeping: false, fall_fly_ticks: 0, glide_ending: false, glider_wear: 0, diff --git a/crates/horizon-client/src/pose.rs b/crates/horizon-client/src/pose.rs index e34b1f1fc..2a01579f3 100644 --- a/crates/horizon-client/src/pose.rs +++ b/crates/horizon-client/src/pose.rs @@ -15,8 +15,21 @@ pub enum Pose { Swimming, FallFlying, SpinAttack, + Sleeping, } impl Pose { + /// `Pose` by its network id (`Pose.id`); the poses a player never takes + /// are standing. + pub fn from_id(id: i32) -> Self { + match id { + 1 => Self::FallFlying, + 2 => Self::Sleeping, + 3 => Self::Swimming, + 4 => Self::SpinAttack, + 5 => Self::Crouching, + _ => Self::Standing, + } + } pub fn name(self) -> &'static str { match self { Self::Standing => "STANDING", @@ -24,18 +37,24 @@ impl Pose { Self::Swimming => "SWIMMING", Self::FallFlying => "FALL_FLYING", Self::SpinAttack => "SPIN_ATTACK", + Self::Sleeping => "SLEEPING", } } + /// `Avatar.POSES` (and `LivingEntity.SLEEPING_DIMENSIONS`): the box of a + /// player or mannequin in this pose; sleeping ignores the scale. pub fn dimensions(self, scale: f32) -> Dimensions { - let (height, eye_height) = match self { - Self::Standing => (1.8, 1.62), - Self::Crouching => (1.5, 1.27), - Self::Swimming | Self::FallFlying | Self::SpinAttack => (0.6, 0.4), + let (width, height, eye_height) = match self { + Self::Standing => (0.6 * scale, 1.8 * scale, 1.62 * scale), + Self::Crouching => (0.6 * scale, 1.5 * scale, 1.27 * scale), + Self::Swimming | Self::FallFlying | Self::SpinAttack => { + (0.6 * scale, 0.6 * scale, 0.4 * scale) + } + Self::Sleeping => (0.2, 0.2, 0.2), }; Dimensions { - width: 0.6 * scale, - height: height * scale, - eye_height: eye_height * scale, + width, + height, + eye_height, } } } @@ -73,6 +92,19 @@ impl PlayerMotion { } Ok(()) } + /// `LivingEntity.startSleeping` and `stopSleeping` set the pose at once + /// (the 0.2 box in the bed, standing again after). + pub fn set_sleeping(&mut self, sleeping: bool) -> Result<()> { + if self.sleeping != sleeping { + self.sleeping = sleeping; + self.set_pose(if sleeping { + Pose::Sleeping + } else { + Pose::Standing + })?; + } + Ok(()) + } pub fn moving_slowly(&self) -> bool { self.local_crouching || (self.pose == Pose::Swimming && !self.fluids.was_touching_water) } @@ -100,8 +132,10 @@ impl MotionWorld<'_, '_, '_> { if !self.can_fit_pose(Pose::Swimming)? { return Ok(()); } - // Player.getDesiredPose: swimming, gliding, Riptide's spin. - let desired = if self.player.swimming { + // Player.getDesiredPose: sleeping, swimming, gliding, Riptide's spin. + let desired = if self.player.sleeping { + Pose::Sleeping + } else if self.player.swimming { Pose::Swimming } else if self.player.fall_flying { Pose::FallFlying