diff --git a/crates/horizon-client/examples/lan_flat.rs b/crates/horizon-client/examples/lan_flat.rs index 53a4d62f7..f3dfea6b1 100644 --- a/crates/horizon-client/examples/lan_flat.rs +++ b/crates/horizon-client/examples/lan_flat.rs @@ -102,6 +102,7 @@ fn main() -> Result<(), Box> { equipment: Default::default(), health: 20.0, attributes: Vec::new(), + bed: None, living_flags: 0, }; loop { diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 18325828d..00279246a 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -395,6 +395,8 @@ impl Game<'_> { ], "chunks": g.sent_chunks.len(), "tracked": g.tracker.len(), + // Whether the host draws this guest lying in a bed. + "model_lying": h.models.get(&g.conn).map(|m| m.extra.ints[1] != 0), // The armour the host draws on this guest, head to feet. "model_armor": h.models.get(&g.conn).map(|m| { [&m.extra.head, &m.extra.chest, &m.extra.legs, &m.extra.feet] @@ -735,6 +737,7 @@ impl Game<'_> { health: p.health.health(), attributes: horizon_client::lan::attributes::syncable(&p.attributes), living_flags: living_flags(self.using), + bed: self.sleeping, }) } @@ -1207,6 +1210,8 @@ impl Game<'_> { .each_ref() .map(|s| s.as_ref().map(|s| s.item.clone())); m.extra.crouching = g.sneaking; + // 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. m.anim.attack_anim_o = m.anim.attack_anim; horizon_client::mobs::ai::tick_swing(m); @@ -1218,6 +1223,20 @@ impl Game<'_> { } } + /// The bed at `head` as a sleeping model lies in it (`ints[1]`, + /// `villager::sleep_pose`): its `facing`. + pub(super) fn bed_facing(&self, head: [i32; 3]) -> i32 { + let state = self.world.block(head[0], head[1], head[2]); + let facing = self + .pack + .assets + .registry + .state(state) + .and_then(|s| s.properties.get("facing")) + .map(String::as_str); + horizon_client::mobs::villager::bed_facing_code(facing) + } + /// The guests' models for the entity pass. pub(super) fn guest_model_geometry( &self, diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 5bb3e172c..ec6b35c1f 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -1544,6 +1544,18 @@ impl Game<'_> { } } } + // LivingEntity.onSyncedDataUpdated(SLEEPING_POS_ID): another + // player lies down in their bed (drawn lying in it), or gets up. + if other { + for (index, _, value) in &p.values { + if let (14, Value::OptPos(bed)) = (index, value) { + let code = bed.map_or(0, |b| self.bed_facing(b)); + if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) { + m.extra.ints[1] = code; + } + } + } + } // LivingEntity.onSyncedDataUpdated(SLEEPING_POS_ID): this // player lies down in the bed, or gets up. if self.remote.as_ref().is_some_and(|r| r.entity_id == p.id) { @@ -4102,6 +4114,13 @@ impl Game<'_> { .filter(|m| r.level_players.contains_key(&(m.id as i32)) && !m.alive()) .map(|m| m.id) .collect::>(), + // The other players this client draws lying in a bed (SLEEPING_POS). + "lying_players": self + .mobs + .iter() + .filter(|m| r.level_players.contains_key(&(m.id as i32)) && m.extra.ints[1] != 0) + .map(|m| m.id) + .collect::>(), // The player's own model exists (third person and the inventory draw it). "player_model": self.player_model.is_some(), "health": r.health.0, diff --git a/crates/horizon-client/src/lan/entity.rs b/crates/horizon-client/src/lan/entity.rs index 9082e01fa..c1156001c 100644 --- a/crates/horizon-client/src/lan/entity.rs +++ b/crates/horizon-client/src/lan/entity.rs @@ -393,11 +393,13 @@ impl ServerEntity { .collect(); // A value back at its default is left out of the state, but the // clients still hold the old one: Entity's shared flags and pose go - // back to 0 (a player standing up again, a mob no longer on fire). + // back to 0 (a player standing up again, a mob no longer on fire), + // a sleeper's bed to none. for (index, kind, _) in &self.meta { let default = match (*index, *kind) { (0, serializer::BYTE) => Value::Byte(0), (6, serializer::POSE) => Value::Id(0), + (14, serializer::OPTIONAL_BLOCK_POS) => Value::OptPos(None), _ => continue, }; if !e.meta.iter().any(|m| m.0 == *index) { diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index a961d57a4..9c65e9292 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -85,6 +85,8 @@ pub struct HostState { pub attributes: Vec, /// `LivingEntity.DATA_LIVING_ENTITY_FLAGS` (the item in use). pub living_flags: i8, + /// The bed the host sleeps in (`LivingEntity.SLEEPING_POS`). + pub bed: Option<[i32; 3]>, } /// Everything the hub needs from the game for one tick. @@ -1467,6 +1469,7 @@ impl Hub { on_ground: bool, flags: (bool, bool, bool, bool), living_flags: i8, + bed: Option<[i32; 3]>, skin: u8, health: f32, equipment: Vec<(u8, Option)>, @@ -1502,6 +1505,11 @@ impl Hub { // Always there: the trackers send the entries that changed, and an // item no longer in use is a change back to 0. meta.push((8, serializer::BYTE, Value::Byte(living_flags))); + // LivingEntity.SLEEPING_POS: the others draw the player lying in it, + // a player paired later too. + if let Some(bed) = bed.filter(|_| sleeping) { + meta.push((14, serializer::OPTIONAL_BLOCK_POS, Value::OptPos(Some(bed)))); + } if (health - 1.0).abs() > f32::EPSILON && health != 20.0 { meta.push((9, serializer::FLOAT, Value::Float(health))); } @@ -1565,8 +1573,9 @@ impl Hub { h.position, (h.yaw, h.pitch, h.head_yaw), h.on_ground, - (h.sneaking, h.sprinting, false, false), + (h.sneaking, h.sprinting, false, h.bed.is_some()), h.living_flags, + h.bed, 0x7F, h.health, equipment, @@ -1598,6 +1607,7 @@ impl Hub { g.on_ground, (g.sneaking, g.sprinting, g.fall_flying, g.sleeping), g.living_flags, + g.bed, g.info.model_customisation, g.health, equipment, diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs index a0dce0e08..99548ec3f 100644 --- a/crates/horizon-client/src/mobs/render.rs +++ b/crates/horizon-client/src/mobs/render.rs @@ -749,7 +749,8 @@ pub fn base_transform_at(m: &Mob, alpha: f32, origin: glam::Mat4) -> glam::Mat4 let ticks = (m.age as f32 + alpha).floor(); body += (f64::from(ticks * 3.25).cos() * std::f64::consts::PI * 0.4) as f32; } - let sleep = (m.species() == Species::Villager) + // A sleeping villager, or another player lying in their bed. + let sleep = (m.species() == Species::Villager || m.kind.id == "minecraft:player") .then(|| super::villager::sleep_pose(m)) .flatten(); let rotation = match sleep { diff --git a/crates/horizon-client/src/mobs/villager.rs b/crates/horizon-client/src/mobs/villager.rs index f568cb47b..84c607933 100644 --- a/crates/horizon-client/src/mobs/villager.rs +++ b/crates/horizon-client/src/mobs/villager.rs @@ -98,12 +98,7 @@ pub fn start_sleeping(m: &mut Mob, p: [i32; 3], w: &MobWorld) -> bool { if w.prop(p[0], p[1], p[2], "occupied") == Some("true") { return false; } - m.extra.ints[1] = match w.prop(p[0], p[1], p[2], "facing") { - Some("north") => 1, - Some("south") => 2, - Some("west") => 3, - _ => 4, - }; + m.extra.ints[1] = bed_facing_code(w.prop(p[0], p[1], p[2], "facing")); m.nav_stop(); ext_mut(m).last_slept = Some(w.game_time); m.velocity = horizon_core::geometry::Vec3::ZERO; @@ -144,6 +139,15 @@ pub fn stop_sleeping(m: &mut Mob, w: &MobWorld) { } /// `LivingEntityRenderer.sleepDirectionToRotation` and the bed's direction /// (the body is shifted back from the pillow by the standing eye height). +/// A bed's `facing` as `sleep_pose` keeps it in `ints[1]`. +pub fn bed_facing_code(facing: Option<&str>) -> i32 { + match facing { + Some("north") => 1, + Some("south") => 2, + Some("west") => 3, + _ => 4, + } +} pub fn sleep_pose(m: &Mob) -> Option<(f32, [f32; 2])> { Some(match m.extra.ints[1] { 1 => (270.0, [0.0, -1.0]), diff --git a/crates/horizon-client/tests/lan_bot.rs b/crates/horizon-client/tests/lan_bot.rs index ae70cccb8..bd22d6e0a 100644 --- a/crates/horizon-client/tests/lan_bot.rs +++ b/crates/horizon-client/tests/lan_bot.rs @@ -184,6 +184,7 @@ impl Server { equipment: Default::default(), health: 20.0, attributes: Vec::new(), + bed: None, living_flags: 0, }; let mut ctx = TickCtx { diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 606321e99..8a20b3227 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -1219,6 +1219,11 @@ def check_sleep(host, guest, out): asleep = {'guest': guest('state').get('sleeping'), 'host': host('state').get('sleeping'), 'server': lan_guest(host).get('sleeping')} guest.shot(out, 'sleep-guest') + # The host draws the guest lying in its bed (#1045): in frame its model, on the server thread its + # client's (SLEEPING_POS), and a client pairing with the sleeper later too. + host.wait(5) + lying = {'host': lan_guest(host).get('model_lying') if not SERVER_THREAD else bool(remote(host)['lying_players'])} + host.shot(out, 'sleep-host-sees-guest') # Alone in bed: still night 120 ticks later (everyone must sleep). guest.wait(120) night = host('state')['time']['time_of_day'] @@ -1228,19 +1233,25 @@ def check_sleep(host, guest, out): up = guest('state').get('sleeping') guest.raw('interact', *guest_bed) host.raw('interact', *host_bed) - host.wait(130) + host.wait(5) + # Both in bed: the guest draws the host lying too. + lying['guest'] = bool(remote(guest)['lying_players']) + host.wait(125) after = {'guest': guest('state').get('sleeping'), 'host': host('state').get('sleeping'), 'time': host('state')['time']['time_of_day']} + lying['after'] = bool(remote(guest)['lying_players']) guest.shot(out, 'sleep-morning') for at in (guest_bed, host_bed): guest('command', command=f'fill {at[0] - 1} {at[1]} {at[2]} {at[0]} {at[1]} {at[2]} minecraft:air') host('command', command='time set day') - detail = {'asleep': asleep, 'night': night, 'up': up, 'after': after} + detail = {'asleep': asleep, 'night': night, 'up': up, 'after': after, 'lying': lying} assert asleep['guest'] == guest_bed and asleep['server'] == guest_bed, f'the guest is not in its bed: {detail}' assert asleep['host'] is None, f'the guest\'s bed put the host to sleep: {detail}' assert 13000 < night < 23000, f'one of two players asleep ended the night: {detail}' assert up is None, f'Escape did not get the guest up: {detail}' assert after['guest'] is None and after['host'] is None and after['time'] < 1000, f'no morning: {detail}' + assert lying['host'] and lying['guest'], f'a sleeping player is drawn standing: {detail}' + assert not lying['after'], f'a player up again is still drawn lying: {detail}' return detail def check_push(host, guest, out): guest('command', command='gamemode survival')