Files
horizon-airdef/crates/horizon-world/src/block_collision_scan.rs
T
Ludovic Marie 55945ee975 Collision scans: skip blocks with no shape, build the entity shape lazily
BlockCollisionScan asked every cursor cell for its collision shape,
moved it into place and intersected it with Shapes.create(box), even
for air. Mob movement and every other Entity.collide spent most of
their time there. Now:

- CollisionBlock carries empty_shape, set by the registry for states
  with no contextual rule and an empty cached shape (air, plants): the
  scan skips them without asking. Providers that build CollisionBlock
  themselves (the query-order fixture) leave it false, so their query
  logs are unchanged.
- A shape whose bounds miss the box is skipped before moving it:
  joinIsNotEmpty(AND) of the two is false anyway.
- Shapes.create(box) is built the first time a shape is not the unit
  cube (finite bounds always make one), not for every scan.
- SceneCollisions hands the scan the resident chunk itself, so a cell
  costs one section read instead of a chunk-map lookup.

Results are unchanged: running both paths side by side over 900,000
mob moves in a 600-mob village gave bit-identical movement.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 16:40:22 +02:00

261 lines
9.2 KiB
Rust

//! Lazy block collision selection in vanilla cursor/query order. Providers own
//! chunk availability, state metadata, suffocation predicates and context dispatch.
use crate::{collision_context::CollisionContext, registry::BlockStateId};
use horizon_core::{
geometry::{Aabb, ShapeError, ShapeOp, Vec3, VoxelShape},
position::{BlockPos, ChunkPos},
};
#[derive(Clone, Copy, Debug)]
pub struct CollisionBlock {
pub state: BlockStateId,
pub large_collision_shape: bool,
pub moving_piston: bool,
/// The shape is empty under every context (air, plants): a scan skips
/// the block without asking for its shape. Providers whose shapes come
/// from elsewhere leave it false.
pub empty_shape: bool,
}
pub trait BlockCollisionWorld {
type Error;
type Chunk: Copy;
/// None is a missing collision chunk; native iteration retries missing chunks.
fn collision_chunk(&mut self, position: ChunkPos) -> Result<Option<Self::Chunk>, Self::Error>;
fn collision_block(
&mut self,
chunk: Self::Chunk,
position: BlockPos,
) -> Result<CollisionBlock, Self::Error>;
fn is_suffocating(
&mut self,
chunk: Self::Chunk,
block: CollisionBlock,
position: BlockPos,
) -> Result<bool, Self::Error>;
/// Query against the global collision world, not the cached chunk provider.
/// Shapes must preserve canonical block identity (including imported shapes).
fn context_shape(
&mut self,
block: CollisionBlock,
position: BlockPos,
context: &CollisionContext<'_>,
) -> Result<VoxelShape, Self::Error>;
}
#[derive(Debug)]
pub enum CollisionScanError<E> {
InvalidBounds,
TooLarge,
VisitBudget,
World(E),
Shape(ShapeError),
}
impl<E: std::fmt::Display> std::fmt::Display for CollisionScanError<E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidBounds => f.write_str("collision scan bounds must be finite"),
Self::TooLarge => {
f.write_str("collision scan exceeds the positive Java-int cursor domain")
}
Self::VisitBudget => f.write_str("collision scan candidate budget exhausted"),
Self::World(error) => error.fmt(f),
Self::Shape(error) => error.fmt(f),
}
}
}
impl<E: std::error::Error + 'static> std::error::Error for CollisionScanError<E> {}
#[derive(Debug)]
pub struct BlockCollisionHit {
pub position: BlockPos,
pub shape: VoxelShape,
}
pub struct BlockCollisionScan<'w, 'c, W: BlockCollisionWorld> {
world: &'w mut W,
context: CollisionContext<'c>,
bounds: Aabb,
/// `Shapes.create(box)`, built the first time a block's shape is not
/// the unit cube (finite bounds always make one).
entity_shape: Option<VoxelShape>,
only_suffocating: bool,
start: BlockPos,
sizes: [usize; 3],
index: usize,
count: usize,
budget: usize,
chunk: Option<(ChunkPos, W::Chunk)>,
failed: bool,
}
impl<'w, 'c, W: BlockCollisionWorld> BlockCollisionScan<'w, 'c, W> {
/// Finite bounds and a positive-Java-int candidate volume are supported.
/// Budget counts every cursor cell, including skipped corners. After an error
/// iteration terminates; already performed queries are not rolled back.
pub fn new(
world: &'w mut W,
context: CollisionContext<'c>,
bounds: Aabb,
only_suffocating: bool,
candidate_budget: usize,
) -> Result<Self, CollisionScanError<W::Error>> {
let min = bounds.min_position();
let max = bounds.max_position();
if !min.is_finite() || !max.is_finite() {
return Err(CollisionScanError::InvalidBounds);
}
let start = BlockPos::containing(Vec3::new(min.x - 1.0e-7, min.y - 1.0e-7, min.z - 1.0e-7))
.offset(-1, -1, -1);
let end = BlockPos::containing(Vec3::new(max.x + 1.0e-7, max.y + 1.0e-7, max.z + 1.0e-7))
.offset(1, 1, 1);
let sizes = [
end.x.wrapping_sub(start.x).wrapping_add(1),
end.y.wrapping_sub(start.y).wrapping_add(1),
end.z.wrapping_sub(start.z).wrapping_add(1),
];
if sizes.iter().any(|size| *size <= 0) {
return Err(CollisionScanError::TooLarge);
}
let count = sizes
.into_iter()
.try_fold(1u64, |n, size| {
n.checked_mul(size as u64).filter(|n| *n <= i32::MAX as u64)
})
.ok_or(CollisionScanError::TooLarge)? as usize;
Ok(Self {
world,
context,
bounds,
entity_shape: None,
only_suffocating,
start,
sizes: sizes.map(|s| s as usize),
index: 0,
count,
budget: candidate_budget,
chunk: None,
failed: false,
})
}
fn find_next(&mut self) -> Result<Option<BlockCollisionHit>, CollisionScanError<W::Error>> {
while self.index < self.count {
if self.index >= self.budget {
return Err(CollisionScanError::VisitBudget);
}
let [width, height, _] = self.sizes;
let relative = [
self.index % width,
self.index / width % height,
self.index / width / height,
];
self.index += 1;
let kind = relative
.into_iter()
.zip(self.sizes)
.filter(|(p, size)| *p == 0 || *p == size - 1)
.count();
if kind == 3 {
continue;
}
let p = self
.start
.offset(relative[0] as i32, relative[1] as i32, relative[2] as i32);
let chunk_position = p.chunk();
let chunk = match self.chunk {
Some((at, chunk)) if at == chunk_position => Some(chunk),
_ => {
let chunk = self
.world
.collision_chunk(chunk_position)
.map_err(CollisionScanError::World)?;
self.chunk = chunk.map(|chunk| (chunk_position, chunk));
chunk
}
};
let Some(chunk) = chunk else {
continue;
};
let block = self
.world
.collision_block(chunk, p)
.map_err(CollisionScanError::World)?;
if self.only_suffocating
&& !self
.world
.is_suffocating(chunk, block, p)
.map_err(CollisionScanError::World)?
{
continue;
}
if kind == 1 && !block.large_collision_shape || kind == 2 && !block.moving_piston {
continue;
}
if block.empty_shape {
continue;
}
let shape = self
.world
.context_shape(block, p, &self.context)
.map_err(CollisionScanError::World)?;
let offset = Vec3::new(f64::from(p.x), f64::from(p.y), f64::from(p.z));
let moved = if shape.same_identity(&VoxelShape::block()) {
// Native adds one in floating point, including at i32::MAX.
let unit = Aabb::new(
offset,
Vec3::new(offset.x + 1.0, offset.y + 1.0, offset.z + 1.0),
);
if !self.bounds.intersects(unit) {
continue;
}
shape.moved(offset)
} else {
// Shapes whose bounds miss the box share no volume with it:
// joinIsNotEmpty(AND) is false without moving them.
if !shape
.bounds()
.is_some_and(|b| b.move_by(offset).intersects(self.bounds))
{
continue;
}
let moved = shape.moved(offset);
let entity_shape = match &self.entity_shape {
Some(shape) => shape,
None => self.entity_shape.insert(
VoxelShape::from_box(self.bounds).map_err(CollisionScanError::Shape)?,
),
};
if moved.is_empty()
|| !moved
.join_is_not_empty(entity_shape, ShapeOp::AND)
.map_err(CollisionScanError::Shape)?
{
continue;
}
moved
};
return Ok(Some(BlockCollisionHit {
position: p,
shape: moved,
}));
}
Ok(None)
}
}
impl<W: BlockCollisionWorld> Iterator for BlockCollisionScan<'_, '_, W> {
type Item = Result<BlockCollisionHit, CollisionScanError<W::Error>>;
fn next(&mut self) -> Option<Self::Item> {
if self.failed {
return None;
}
match self.find_next() {
Ok(hit) => hit.map(Ok),
Err(error) => {
self.failed = true;
Some(Err(error))
}
}
}
}
impl<W: BlockCollisionWorld> std::iter::FusedIterator for BlockCollisionScan<'_, '_, W> {}