Files
horizon-airdef/crates/horizon-world/src/entity_obstruction.rs
T
Ludovic Marie cb19660fbe Build Horizon Rust parity foundations and differential validation
Add version-bound core, world data, geometry, generation and feature implementations with native client/server comparison tools and scoped evidence reports. Apply Horizon project branding.

Workspace tests, Clippy and 147 Python tests passed. Full 51-family survival and expanded provider comparisons are still running; complete gameplay parity and a playable Rust client remain unfinished. Private game artifacts and the isolated simple-block placement work are excluded.
2026-10-06 11:24:51 +02:00

90 lines
3.0 KiB
Rust

//! Entity obstruction against a voxel shape. Entity storage, lifecycle and
//! vehicle-tree maintenance supply the immutable candidate snapshots here.
use horizon_core::geometry::{Aabb, ShapeError, ShapeOp, VoxelShape};
/// Object identity within the caller's entity snapshot, including its generation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct EntityIdentity(pub u64);
#[derive(Clone, Copy, Debug)]
pub struct ObstructionEntity {
pub identity: EntityIdentity,
pub root_vehicle: EntityIdentity,
pub bounds: Aabb,
pub removed: bool,
pub spectator: bool,
pub blocks_building: bool,
}
pub trait EntityObstructionWorld {
type Error;
/// Preserve native query order. Implementations may return a broad-phase
/// superset: exclusion, spectator and exact AABB filters are enforced below.
fn obstruction_candidates(
&mut self,
excluded: Option<EntityIdentity>,
bounds: Aabb,
) -> Result<Vec<ObstructionEntity>, Self::Error>;
}
#[derive(Debug)]
pub enum ObstructionError<E> {
World(E),
Shape(ShapeError),
}
impl<E: std::fmt::Display> std::fmt::Display for ObstructionError<E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::World(e) => e.fmt(f),
Self::Shape(e) => e.fmt(f),
}
}
}
impl<E: std::error::Error + 'static> std::error::Error for ObstructionError<E> {}
pub fn is_unobstructed<W: EntityObstructionWorld>(
world: &mut W,
excluded: Option<&ObstructionEntity>,
shape: &VoxelShape,
) -> Result<bool, ObstructionError<W::Error>> {
let Some(bounds) = shape.bounds() else {
return Ok(true);
};
let candidates = world
.obstruction_candidates(excluded.map(|e| e.identity), bounds)
.map_err(ObstructionError::World)?;
for entity in candidates {
if excluded.is_some_and(|e| e.identity == entity.identity)
|| entity.spectator
|| !entity.bounds.intersects(bounds)
{
continue;
}
if entity.removed
|| !entity.blocks_building
|| excluded.is_some_and(|e| e.root_vehicle == entity.root_vehicle)
{
continue;
}
let other = VoxelShape::from_box(entity.bounds).map_err(ObstructionError::Shape)?;
if shape
.join_is_not_empty(&other, ShapeOp::AND)
.map_err(ObstructionError::Shape)?
{
return Ok(false);
}
}
Ok(true)
}
/// Entity obstruction for the unit cube used by the vanilla block predicate.
/// Translate the voxel shape in floating point: integer MAX + 1 must not wrap.
pub fn is_block_unobstructed<W: EntityObstructionWorld>(
world: &mut W,
position: [i32; 3],
) -> Result<bool, ObstructionError<W::Error>> {
let shape = VoxelShape::block().moved(horizon_core::geometry::Vec3::new(
f64::from(position[0]),
f64::from(position[1]),
f64::from(position[2]),
));
is_unobstructed(world, None, &shape)
}