diff --git a/crates/horizon-client/src/game/lan_stations.rs b/crates/horizon-client/src/game/lan_stations.rs index 49a90b7b9..418ed9c2a 100644 --- a/crates/horizon-client/src/game/lan_stations.rs +++ b/crates/horizon-client/src/game/lan_stations.rs @@ -588,7 +588,7 @@ impl Game<'_> { let [x, y, z] = gs.pos; let name = &self.res.facts().get(self.world.block(x, y, z)).name; let block = StationKind::from_block(name) == Some(gs.kind); - if !(block && within_reach(g, gs.pos)) { + if !(block && g.within_block_range(gs.pos, 4.0)) { let window = gs.window; self.lan_station_closed(host, conn, Some(window)); host.hub.send_to( @@ -631,16 +631,7 @@ impl Game<'_> { Some(BlockEntity::Lectern(l)) if l.book.is_some() ); // Container.stillValidBlockEntity: the interaction range and its buffer. - let eye = [g.position[0], g.position[1] + g.eye_height(), g.position[2]]; - let reach = if g.creative() { 5.0 } else { 4.5 } + 4.0; - let pos = [key.0, key.1, key.2]; - let near = (0..3) - .map(|k| { - let lo = f64::from(pos[k]); - (lo - eye[k]).max(eye[k] - lo - 1.0).max(0.0).powi(2) - }) - .sum::() - < reach * reach; + let near = g.within_block_range([key.0, key.1, key.2], 4.0); if has_book && near { self.lan_send_lectern(host, conn, false); } else { @@ -660,10 +651,3 @@ pub(super) fn station_inputs(gs: &GuestStation) -> Vec { .map(|(_, _, inputs)| inputs.filter_map(|s| gs.menu.slot(s).cloned()).collect()) .unwrap_or_default() } - -/// `Player.isWithinBlockInteractionRange(pos, 4)`: the guest's eyes within -/// its block interaction range (4.5, creative 5) and a 4-block buffer of the -/// block's box. -pub(super) fn within_reach(g: &horizon_client::lan::guest::Guest, pos: [i32; 3]) -> bool { - g.within_block_range(pos, 4.0) -} diff --git a/crates/horizon-client/src/game/sign_edit.rs b/crates/horizon-client/src/game/sign_edit.rs index 5a26e3e69..5efc3dd0b 100644 --- a/crates/horizon-client/src/game/sign_edit.rs +++ b/crates/horizon-client/src/game/sign_edit.rs @@ -78,7 +78,7 @@ impl Game<'_> { let Some(g) = host.hub.guest(conn) else { return; }; - let near = super::lan_stations::within_reach(g, p.pos); + let near = g.within_block_range(p.pos, 4.0); let Some(play) = host.play.get_mut(&conn) else { return; }; diff --git a/crates/horizon-client/src/game/storage.rs b/crates/horizon-client/src/game/storage.rs index 9132c02a8..de7dfd6e7 100644 --- a/crates/horizon-client/src/game/storage.rs +++ b/crates/horizon-client/src/game/storage.rs @@ -144,18 +144,8 @@ impl Game<'_> { return; } let range = self.reach() + 4.0; - let near = self.container_parts().iter().all(|((x, y, z), _)| { - let d = [*x, *y, *z] - .iter() - .zip(eye) - .map(|(&lo, e)| { - (f64::from(lo) - e) - .max(e - f64::from(lo) - 1.0) - .max(0.0) - .powi(2) - }) - .sum::(); - d < range * range + let near = self.container_parts().iter().all(|&((x, y, z), _)| { + horizon_client::lan::guest::within_block_range(eye, range, [x, y, z]) }); if !near { self.close_container(); diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index dc10096e3..57fa7c636 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -404,22 +404,16 @@ impl Guest { pub fn eye_height(&self) -> f64 { if self.sneaking { 1.27 } else { 1.62 } } - /// `Player.isWithinBlockInteractionRange(pos, buffer)`: the eyes within the - /// block interaction range (4.5, creative 5) plus `buffer` of the block's box. + /// `Player.isWithinBlockInteractionRange(pos, buffer)` for the guest: + /// its block interaction range is 4.5, 5 in creative. pub fn within_block_range(&self, pos: [i32; 3], buffer: f64) -> bool { let eye = [ self.position[0], self.position[1] + self.eye_height(), self.position[2], ]; - let reach = if self.creative() { 5.0 } else { 4.5 } + buffer; - (0..3) - .map(|k| { - let lo = f64::from(pos[k]); - (lo - eye[k]).max(eye[k] - lo - 1.0).max(0.0).powi(2) - }) - .sum::() - < reach * reach + let range = if self.creative() { 5.0 } else { 4.5 }; + within_block_range(eye, range + buffer, pos) } /// The inventory menu's 46 slots (ServerPlayer.inventoryMenu): 0 crafting /// result, 1..=4 crafting grid, 5..=8 armor, 9..=35 main, 36..=44 hotbar, @@ -585,6 +579,18 @@ fn network_stack( Some(stack) } +/// `Player.isWithinBlockInteractionRange`: `eye` closer than `range` to the +/// box of the block at `pos` (`AABB.distanceToSqr`). +pub fn within_block_range(eye: [f64; 3], range: f64, pos: [i32; 3]) -> bool { + (0..3) + .map(|k| { + let lo = f64::from(pos[k]); + (lo - eye[k]).max(eye[k] - lo - 1.0).max(0.0).powi(2) + }) + .sum::() + < range * range +} + #[cfg(test)] mod tests { use super::*;