One isWithinBlockInteractionRange for guests, the in-frame player, stations, lecterns and signs

The reach test was written out four times (stations, lecterns, the
in-frame container check and the guest's); they now share
lan::guest::within_block_range.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-25 17:25:02 +01:00
parent ed7d88b213
commit 6d3cd330d1
4 changed files with 21 additions and 41 deletions
+2 -18
View File
@@ -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::<f64>()
< 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<ItemStack> {
.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)
}
+1 -1
View File
@@ -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;
};
+2 -12
View File
@@ -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::<f64>();
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();
+16 -10
View File
@@ -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::<f64>()
< 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::<f64>()
< range * range
}
#[cfg(test)]
mod tests {
use super::*;