diff --git a/crates/horizon-client/src/flatgen.rs b/crates/horizon-client/src/flatgen.rs new file mode 100644 index 000000000..72245a4d3 --- /dev/null +++ b/crates/horizon-client/src/flatgen.rs @@ -0,0 +1,73 @@ +//! FlatLevelSource: superflat worlds (`minecraft:flat` generator). +use crate::{chunk::Chunk, streaming::ChunkGenerator}; +use horizon_world::{nbt::Tag, registry::BlockRegistry}; + +/// The layers of a flat generator, bottom up from the world's minimum Y. +pub struct FlatTerrain { + layers: Vec, + biome: u16, +} +impl FlatTerrain { + /// The overworld generator of `world_gen_settings` (`data.dimensions`), if it + /// is flat. Its `features` and `lakes` decorate the world; a world without + /// them (the default) gets only its layers, as FlatLevelSource places them. + pub fn from_settings( + settings: &Tag, + registry: &BlockRegistry, + biomes: &crate::biome::Biomes, + ) -> Option { + let generator = settings + .get("dimensions")? + .get("minecraft:overworld")? + .get("generator")?; + if crate::save::text(generator.get("type"))?.trim_start_matches("minecraft:") != "flat" { + return None; + } + let s = generator.get("settings")?; + let biome = crate::save::text(s.get("biome")).unwrap_or_else(|| "minecraft:plains".into()); + let mut layers = Vec::new(); + if let Some(Tag::List(list)) = s.get("layers") { + for layer in list { + let block = crate::save::text(layer.get("block"))?; + let height = crate::save::number(layer.get("height")).unwrap_or(0).max(0); + let state = registry.default_state(&block)?.0 as u16; + layers.extend(std::iter::repeat_n(state, height as usize)); + } + } + Some(Self { + layers, + biome: biomes.id(&biome), + }) + } +} +impl ChunkGenerator for FlatTerrain { + type Worker = (); + fn worker(&self) {} + fn proto(&self, _: &mut (), cx: i32, cz: i32) -> crate::streaming::Proto { + let mut chunk = Chunk::empty(cx, cz); + for s in &mut chunk.sections { + std::sync::Arc::make_mut(s).biomes = horizon_core::paletted::PalettedContainer::new( + crate::chunk::BIOME_STRATEGY, + u32::from(self.biome), + ); + } + for (i, &block) in self.layers.iter().enumerate().take(384) { + if block == 0 { + continue; + } + for z in 0..16 { + for x in 0..16 { + chunk.set(x, -64 + i as i32, z, block); + } + } + } + for s in &mut chunk.sections { + std::sync::Arc::make_mut(s).compact(); + } + Box::new(chunk) + } + /// FlatLevelSource.spawnOriginalMobs is empty: no generation-time spawns. + fn finish(&self, proto: crate::streaming::Proto) -> Chunk { + *proto.downcast::().expect("flat terrain proto") + } +} diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 972ad6fb8..d0509bf2a 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -5236,7 +5236,28 @@ impl Game<'_> { dim.id(), ))); let res = &*self.res; - let mut streaming = if self.options.test_terrain && dim == Dim::Overworld { + let flat = (dim == Dim::Overworld) + .then(|| horizon_client::save::world_gen_settings(world_root)) + .flatten() + .and_then(|s| { + horizon_client::flatgen::FlatTerrain::from_settings( + &s, + res.assets.registry, + &res.biomes, + ) + }); + let mut streaming = if let Some(inner) = flat { + Streaming::new( + Arc::new(Persistent { + inner: Arc::new(inner), + save: save.clone(), + registry: res.assets.clone(), + biomes: res.biomes.clone(), + }), + res.context.clone(), + res.threads, + ) + } else if self.options.test_terrain && dim == Dim::Overworld { let inner = Arc::new(TestTerrain::new(res.assets.registry, &res.biomes)?); Streaming::new( Arc::new(Persistent { diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 4e6d6bf05..c888a1b60 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -41,6 +41,7 @@ pub mod entities; pub mod entity_nbt; pub mod environment; pub mod fastutil_order; +pub mod flatgen; pub mod fog; pub mod font; pub mod gamerules; diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index a520532d7..70758f7a7 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -46,7 +46,7 @@ pub const VERSION_NAME: &str = "26.3"; fn string(s: &str) -> Tag { Tag::String(s.into()) } -pub(crate) fn text(tag: Option<&Tag>) -> Option { +pub fn text(tag: Option<&Tag>) -> Option { match tag { Some(Tag::String(s)) => Some(s.to_string_lossy()), _ => None, @@ -1339,6 +1339,16 @@ fn read_saved_data(world: &Path, name: &str) -> Option { .get("data") .cloned() } +/// The world's generator settings: world_gen_settings.dat (26.3) or +/// level.dat's Data.WorldGenSettings. +pub fn world_gen_settings(world: &Path) -> Option { + read_saved_data(world, "world_gen_settings").or_else(|| { + read_dat(&world.join("level.dat"))? + .get("Data")? + .get("WorldGenSettings") + .cloned() + }) +} fn write_saved_data(world: &Path, name: &str, data: Tag) -> Result<()> { write_dat( &saved_data_path(world, name), diff --git a/tools/test_save_roundtrip_parity.py b/tools/test_save_roundtrip_parity.py index 11dfdae56..317956a40 100644 --- a/tools/test_save_roundtrip_parity.py +++ b/tools/test_save_roundtrip_parity.py @@ -52,6 +52,12 @@ IGNORED = { r'^chunk\.LastUpdate$': 'game time of the save (Horizon saves at its own time)', r'^chunk\.InhabitedTime$': 'ticks the chunk was loaded; differs by the time Horizon ran', r'^chunk\.isLightOn$': 'light is recomputed by vanilla when absent', + # Vanilla's server (view distance 4) leaves a border of proto chunks; Horizon's + # render distance 6 may finish some, as vanilla does when it loads the world at + # that distance. Their blocks are still compared; only what a full chunk saves + # differently (no worldgen heightmaps, entities in entities/) is accepted. + r'^chunk\.promoted\.(Status|Heightmaps\.(OCEAN_FLOOR_WG|WORLD_SURFACE_WG)|entities)$': + 'a proto chunk vanilla left at its view distance, finished by Horizon', # Horizon keeps simulating between loading and saving (it needs a few seconds of # chunk generation first), so counters and positions of live things move on. r'^chunk\.block_entities\.\(2, -60, 5\)\.(cooking_time_spent|lit_time_remaining|Items\[\]\.count)$': @@ -445,6 +451,10 @@ def compare_chunks(vanilla, horizon, report): ha, hs = normalize_chunk(horizon[key]) out = [] diff(va, ha, 'chunk', out) + if va.get('Status') != 'minecraft:full' and ha.get('Status') == 'minecraft:full': + promoted = re.compile(r'^chunk\.(Status|Heightmaps\.\w+_WG|entities)$') + out = [(path.replace('chunk.', 'chunk.promoted.', 1) if promoted.match(path) else path, p, q) + for path, p, q in out] for y in sorted(set(vs) | set(hs)): empty = {'blocks': ['minecraft:air'] * 4096, 'biomes': None, 'BlockLight': None, 'SkyLight': None} a, b = vs.get(y, empty), hs.get(y, empty)