Natural spawning skips sections of air when it finds the surface

GameLevel::heightmap found getHeight by testing every block from the top
of the world down (with a block tag lookup each), for every spawnable chunk
every tick: a quarter of the main thread in a village. Sections holding
only air are now passed over whole; air counts for no heightmap, so the
height is the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-15 15:43:40 +02:00
parent 337ee470b5
commit 7f631bb36f
2 changed files with 61 additions and 4 deletions
+50
View File
@@ -158,6 +158,19 @@ impl Chunk {
pub fn block(&self, x: i32, y: i32, z: i32) -> BlockStateId {
BlockStateId(u32::from(self.get(x, y, z)))
}
/// The highest y where `matches`, scanning down. Sections holding only
/// `skip` are passed over whole: `matches` must be false for it.
pub fn highest(&self, skip: u16, mut matches: impl FnMut(i32) -> bool) -> Option<i32> {
self.sections
.iter()
.enumerate()
.rev()
.filter(|(_, section)| !section.is_uniform(skip))
.find_map(|(s, _)| {
let base = MIN_Y + s as i32 * 16;
(base..base + 16).rev().find(|&y| matches(y))
})
}
pub fn set(&mut self, x: i32, y: i32, z: i32, v: u16) -> u16 {
let s = (y - MIN_Y) >> 4;
if !(0..SECTIONS as i32).contains(&s) {
@@ -198,3 +211,40 @@ impl Chunk {
);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn highest_skips_only_sections_that_cannot_match() {
let (air, cave_air, stone) = (0, 7, 1);
let mut chunk = Chunk::empty(0, 0);
// Stone low down, cave air (uniform in its section) above it, and a
// section of air with one stone block higher up.
for y in MIN_Y..MIN_Y + 40 {
chunk.set(3, y, 4, stone);
}
for i in 0..4096 {
Arc::make_mut(&mut chunk.sections[5]).set(i, cave_air);
}
chunk.set(3, 150, 4, stone);
let full = |c: &Chunk, matches: &dyn Fn(u16) -> bool| {
(MIN_Y..MIN_Y + HEIGHT)
.rev()
.find(|&y| matches(c.get(3, y, 4)))
};
for matches in [
&(|v: u16| v != air) as &dyn Fn(u16) -> bool,
&|v: u16| v == stone,
&|v: u16| v == cave_air,
] {
let fast = chunk.highest(air, |y| matches(chunk.get(3, y, 4)));
assert_eq!(fast, full(&chunk, matches));
}
assert_eq!(
chunk.highest(air, |y| chunk.get(3, y, 4) == stone),
Some(150)
);
assert_eq!(Chunk::empty(0, 0).highest(air, |_| unreachable!()), None);
}
}
@@ -198,11 +198,18 @@ impl NaturalLevel for GameLevel<'_, '_> {
fn spawn_mobs(&self) -> bool {
self.game.ui.rules.bool("spawn_mobs")
}
/// Scanned down the column: sections of air alone are skipped (air counts
/// for no heightmap), and an unloaded column reads as air.
fn heightmap(&self, kind: Heightmap, x: i32, z: i32) -> i32 {
let top = horizon_client::chunk::MIN_Y + horizon_client::chunk::HEIGHT;
(horizon_client::chunk::MIN_Y..top)
.rev()
.find(|&y| spawn_selection::heightmap_blocks(self, kind, x, y, z))
let world = self.w.world;
world
.chunks
.get(&(x >> 4, z >> 4))
.and_then(|chunk| {
chunk.highest(world.air.0 as u16, |y| {
spawn_selection::heightmap_blocks(self, kind, x, y, z)
})
})
.map_or(horizon_client::chunk::MIN_Y, |y| y + 1)
}
fn pathfindable_land(&self, x: i32, y: i32, z: i32) -> bool {