Skip to content

Minecraft: Java Edition 26.3 rewritten from scratch in Rust, checked against vanilla with 281 differential harnesses. Get it with the Horizon launcher.

Worldgen writes deep under a heightmap's surface skip its update

Ludovic Marie340dfd1d3e97
diff --git a/crates/horizon-world/src/heightmap.rs b/crates/horizon-world/src/heightmap.rs
index 7b2a5588f..8cd8dd38d 100644
--- a/crates/horizon-world/src/heightmap.rs
+++ b/crates/horizon-world/src/heightmap.rs
@@ -133,6 +133,18 @@ impl Heightmap {
x.wrapping_add(z.wrapping_mul(16)) as usize
}
+ /// Whether `update` at (`x`, `y`, `z`) returns at once, whatever the
+ /// state: the block is two or more below the first available one. `x`
+ /// and `z` are within 0..16. Most generation writes are (the surface
+ /// rule turns every stone under y 8 to deepslate), and this skips the
+ /// checks `update` does first.
+ #[inline]
+ pub fn below_update(&self, chunk: &impl HeightmapChunk, x: i32, y: i32, z: i32) -> bool {
+ debug_assert!((0..16).contains(&x) && (0..16).contains(&z));
+ let first = (self.data.read(Self::index(x, z)) as i32).wrapping_add(self.min_y);
+ y <= first.wrapping_sub(2) && self.check_chunk(chunk).is_ok()
+ }
+
pub fn first_available(&self, x: i32, z: i32) -> Result<i32, HeightmapError> {
Ok((self.data.get(Self::index(x, z))? as i32).wrapping_add(self.min_y))
}
diff --git a/crates/horizon-world/src/worldgen/terrain.rs b/crates/horizon-world/src/worldgen/terrain.rs
index bae816d48..e57b5f132 100644
--- a/crates/horizon-world/src/worldgen/terrain.rs
+++ b/crates/horizon-world/src/worldgen/terrain.rs
@@ -344,10 +344,17 @@ impl<'r, B: TerrainBlockStates> TerrainChunk<'r, B> {
let previous = self.set_block_raw(x, y, z, state)?;
let mut missing = Vec::new();
for &kind in self.stage.heightmaps() {
- if self.heightmap(kind).is_some() {
- self.update_heightmap(kind, x & 15, y, z & 15, state)?;
- } else {
- missing.push(kind);
+ match self.heightmap(kind) {
+ // `Heightmap::update` would return at once: no state is read.
+ Some(map) if map.below_update(&self.blocks, x & 15, y, z & 15) => {
+ if self.blocks.registry.state(state).is_none() {
+ return Err(TerrainError::InvalidState);
+ }
+ }
+ Some(_) => {
+ self.update_heightmap(kind, x & 15, y, z & 15, state)?;
+ }
+ None => missing.push(kind),
}
}
if !missing.is_empty() {
diff --git a/crates/horizon-world/tests/feature_region.rs b/crates/horizon-world/tests/feature_region.rs
index ce24cdfd0..6b6f3e52d 100644
--- a/crates/horizon-world/tests/feature_region.rs
+++ b/crates/horizon-world/tests/feature_region.rs
@@ -473,3 +473,56 @@ fn spread_out_regions_route_like_dense_ones() {
);
}
}
+/// `set_block_worldgen` skips a heightmap's update for writes deep under its
+/// surface; the maps end as when every write updates them.
+#[test]
+fn worldgen_writes_keep_heightmaps_as_updating_each_write() {
+ let registry = registry();
+ for stage in [PreLightChunkStage::Biomes, PreLightChunkStage::Terrain] {
+ let new = || {
+ let mut chunk =
+ TerrainChunk::new(ChunkPos::new(2, -3), -32, 64, &registry, Properties).unwrap();
+ chunk.set_generation_stage(stage);
+ chunk.prime_heightmaps(stage.heightmaps()).unwrap();
+ chunk
+ };
+ let (mut fast, mut each) = (new(), new());
+ let mut seed = 0x9E37_79B9_7F4A_7C15u64;
+ let mut next = |n: u64| {
+ seed ^= seed << 13;
+ seed ^= seed >> 7;
+ seed ^= seed << 17;
+ seed % n
+ };
+ for _ in 0..20_000 {
+ let (x, z) = (next(4) as i32, next(4) as i32);
+ // Mostly solid columns written top down, as the surface rule does,
+ // with air and water mixed in.
+ let y = 31 - next(64) as i32;
+ let state = BlockStateId([2, 2, 2, 4, 6, 0, 3, 7][next(8) as usize]);
+ fast.set_block_worldgen(x, y, z, state).unwrap();
+ each.set_block_raw(x, y, z, state).unwrap();
+ for &kind in stage.heightmaps() {
+ each.update_heightmap(kind, x, y, z, state).unwrap();
+ }
+ }
+ for &kind in stage.heightmaps() {
+ for z in 0..16 {
+ for x in 0..16 {
+ assert_eq!(
+ fast.heightmap(kind).unwrap().first_available(x, z).unwrap(),
+ each.heightmap(kind).unwrap().first_available(x, z).unwrap(),
+ "{kind:?} at {x} {z}"
+ );
+ }
+ }
+ }
+ for y in -32..32 {
+ for z in 0..4 {
+ for x in 0..4 {
+ assert_eq!(fast.block(x, y, z), each.block(x, y, z));
+ }
+ }
+ }
+ }
+}