From a013e35a61f3d888f141364ab5e04f13fbb1a842 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 21:17:59 +0100 Subject: [PATCH] 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 --- .../horizon-world/src/block_collision_scan.rs | 35 ++++++++++++++++++- .../tests/block_collision_scan.rs | 31 ++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/crates/horizon-world/src/block_collision_scan.rs b/crates/horizon-world/src/block_collision_scan.rs index 391c3e96e..172e6a79c 100644 --- a/crates/horizon-world/src/block_collision_scan.rs +++ b/crates/horizon-world/src/block_collision_scan.rs @@ -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, CollisionScanError> { 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; } } diff --git a/crates/horizon-world/tests/block_collision_scan.rs b/crates/horizon-world/tests/block_collision_scan.rs index adf530069..e3f61e1ae 100644 --- a/crates/horizon-world/tests/block_collision_scan.rs +++ b/crates/horizon-world/tests/block_collision_scan.rs @@ -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| { + 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::>() + }; + assert_eq!(scan(vec![3, 4]), scan(Vec::new()), "budget {budget}"); + } +}