Collision scans: pass the rest of an outer row in a contained section at once

On the rows along a box's bottom, top and sides every cell is outer, so in a
section no block reaches out of, the rest of the row up to the chunk's edge
is passed in one step instead of cell by cell. The candidate budget still
ends the scan where it did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-25 21:17:59 +01:00
parent 4459f132fe
commit a013e35a61
2 changed files with 65 additions and 1 deletions
@@ -151,12 +151,31 @@ impl<'w, 'c, W: BlockCollisionWorld> BlockCollisionScan<'w, 'c, W> {
})
}
/// Move the cursor `n` cells further along its row; `n` must not pass
/// the row's end.
fn skip(&mut self, n: usize) {
if n == 0 {
return;
}
let [width, height, _] = self.sizes;
self.index += n;
self.cursor[0] += n;
if self.cursor[0] == width {
self.cursor[0] = 0;
self.cursor[1] += 1;
if self.cursor[1] == height {
self.cursor[1] = 0;
self.cursor[2] += 1;
}
}
}
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 [width, height, depth] = self.sizes;
let relative = self.cursor;
self.index += 1;
self.cursor[0] += 1;
@@ -212,6 +231,20 @@ impl<'w, 'c, W: BlockCollisionWorld> BlockCollisionScan<'w, 'c, W> {
}
};
if !reaches {
// On a row along the box's bottom, top or sides every
// cell is outer: the rest of it in this chunk is passed
// at once (never beyond the budget, which still ends the
// scan where it would have).
if relative[1] == 0
|| relative[1] == height - 1
|| relative[2] == 0
|| relative[2] == depth - 1
{
let rest = (width - 1 - relative[0])
.min(15 - (p.x & 15) as usize)
.min(self.budget.saturating_sub(self.index));
self.skip(rest);
}
continue;
}
}
@@ -222,6 +222,11 @@ fn outer_cells_of_contained_sections_are_not_read() {
Aabb::new(Vec3::new(4.4, 2.0, -7.6), Vec3::new(4.65, 2.25, -7.35)),
vec![0],
),
// Across a chunk's edge: the rest of a row is passed only up to it.
(
Aabb::new(Vec3::new(14.2, 70.5, -0.4), Vec3::new(17.8, 71.5, 0.4)),
vec![4],
),
];
for (bounds, contained) in boxes {
let mut world = Recording {
@@ -242,3 +247,29 @@ fn outer_cells_of_contained_sections_are_not_read() {
assert!(world.cells.len() < expected_cells(bounds, &[]).len());
}
}
/// Passing outer cells at once still stops at the candidate budget, as
/// visiting them one by one does.
#[test]
fn passed_cells_count_toward_the_budget() {
let bounds = Aabb::new(Vec3::new(0.2, 63.5, 0.3), Vec3::new(0.8, 65.3, 0.9));
for budget in 0..80 {
let scan = |contained: Vec<i32>| {
let mut world = Recording {
cells: Vec::new(),
contained,
};
BlockCollisionScan::new(
&mut world,
CollisionContext::empty(false),
bounds,
false,
budget,
)
.unwrap()
.map(|hit| hit.map(|_| ()).map_err(|e| e.to_string()))
.collect::<Vec<_>>()
};
assert_eq!(scan(vec![3, 4]), scan(Vec::new()), "budget {budget}");
}
}