diff --git a/crates/horizon-world/examples/chunk_dump.rs b/crates/horizon-world/examples/chunk_dump.rs index 199f7e26f..b72a5e05e 100644 --- a/crates/horizon-world/examples/chunk_dump.rs +++ b/crates/horizon-world/examples/chunk_dump.rs @@ -160,6 +160,9 @@ fn main() -> Result<(), Error> { return Ok(()); } for [x0, z0, x1, z1] in rects { + // Each rectangle starts from scratch, like the oracle's fresh chunks: + // structure pieces keep placement state (a shipwreck's adjusted height). + worker.forget_structure_starts(); let mut chunks: HashMap<(i32, i32), ProtoChunk> = HashMap::new(); for z in z0 - 2..=z1 + 2 { for x in x0 - 2..=x1 + 2 { diff --git a/crates/horizon-world/src/worldgen/generator.rs b/crates/horizon-world/src/worldgen/generator.rs index 91a5e0f1d..7a264020f 100644 --- a/crates/horizon-world/src/worldgen/generator.rs +++ b/crates/horizon-world/src/worldgen/generator.rs @@ -563,10 +563,24 @@ impl ProtoChunk { } } +/// Build range (minimum Y, height) of the vanilla dimension types. +fn dimension_type_height(name: &str) -> Option<(i32, i32)> { + match name { + "minecraft:overworld" | "minecraft:overworld_caves" => Some((-64, 384)), + "minecraft:the_nether" | "minecraft:the_end" => Some((0, 256)), + _ => None, + } +} + /// Per-noise-settings values resolved from the dimension's generator JSON. struct Settings { - min_y: i32, - height: i32, + noise_min_y: i32, + noise_height: i32, + /// The dimension type's build range (LevelHeightAccessor): proto chunks, + /// their heightmaps and the decoration region span it, while the noise + /// only fills `noise_min_y..noise_min_y + noise_height`. + level_min_y: i32, + level_height: i32, sea_level: i32, default_block: BlockStateId, default_fluid: BlockStateId, @@ -635,16 +649,25 @@ where generator: &Value, settings_json: &Value, with_structures: bool, + level: Option<(i32, i32)>, ) -> Result { let registry = data.registry; let noise = &settings_json["noise"]; + let min_y = noise["min_y"] + .as_i64() + .ok_or_else(|| resource("noise min_y"))? as i32; + let height = noise["height"] + .as_i64() + .ok_or_else(|| resource("noise height"))? as i32; + let (level_min_y, level_height) = level.unwrap_or((min_y, height)); + if min_y < level_min_y || min_y + height > level_min_y + level_height { + return Err(resource("noise range outside the dimension's build height")); + } let settings = Settings { - min_y: noise["min_y"] - .as_i64() - .ok_or_else(|| resource("noise min_y"))? as i32, - height: noise["height"] - .as_i64() - .ok_or_else(|| resource("noise height"))? as i32, + noise_min_y: min_y, + noise_height: height, + level_min_y, + level_height, sea_level: settings_json["sea_level"] .as_i64() .ok_or_else(|| resource("sea_level"))? as i32, @@ -777,8 +800,8 @@ where } fn generation(&self) -> WorldGenerationContext { WorldGenerationContext::new( - self.settings.min_y, - self.settings.height, + self.settings.noise_min_y, + self.settings.noise_height, self.settings.sea_level, ) } @@ -790,7 +813,7 @@ where if let Some(chunk) = self.biome_cache.lock().unwrap().get(&(pos.x, pos.z)) { return Ok(chunk.clone()); } - let (min, height) = (self.settings.min_y, self.settings.height); + let (min, height) = (self.settings.level_min_y, self.settings.level_height); let mut chunk = BiomeChunk::new(pos, min, height, BiomeId(0)).map_err(stage("biomes"))?; let mut resolver = self.source.resolver( &self.climate, @@ -856,8 +879,8 @@ where ) -> Result { let data = &self.data; let (min, height, sea) = ( - self.settings.min_y, - self.settings.height, + self.settings.noise_min_y, + self.settings.noise_height, self.settings.sea_level, ); let mut clock = std::time::Instant::now(); @@ -869,8 +892,10 @@ where worker, ChunkPos::new(pos.x.wrapping_add(dx), pos.z.wrapping_add(dz)), )?; - for section in 0..(height / 16) as usize { - possible.extend(chunk.section(section).unwrap().iter().copied()); + let mut section = 0; + while let Some(biomes) = chunk.section(section) { + possible.extend(biomes.iter().copied()); + section += 1; } neighbors.push((*chunk).clone()); } @@ -883,8 +908,14 @@ where .map(|id| data.biome_name(id).to_owned()) .collect(); worker.timings.biomes += lap(&mut clock); - let mut chunk = TerrainChunk::new(pos, min, height, data.registry, data.states.clone()) - .map_err(stage("noise"))?; + let mut chunk = TerrainChunk::new( + pos, + self.settings.level_min_y, + self.settings.level_height, + data.registry, + data.states.clone(), + ) + .map_err(stage("noise"))?; chunk.set_generation_stage(PreLightChunkStage::Biomes); let references = match &self.structures { Some(structures) => structures.references( @@ -1036,7 +1067,7 @@ where /// ChunkGenerator.getBaseHeight: the first free Y above the noise column /// (with aquifers, without terrain adaptation) for a heightmap type. fn base_height(&self, x: i32, z: i32, kind: HeightmapType) -> i32 { - let min = self.settings.min_y; + let min = self.settings.noise_min_y; let mut found = min; self.noise_column(x, z, &mut |y, state| { if kind.is_opaque(self.data.states.get(state)) { @@ -1054,7 +1085,7 @@ where } /// One noise-column pass, as opacities for each heightmap kind. fn base_columns(&self, x: i32, z: i32, kinds: &[HeightmapType]) -> Vec> { - let (min, height) = (self.settings.min_y, self.settings.height); + let (min, height) = (self.settings.noise_min_y, self.settings.noise_height); let mut columns = vec![vec![false; height as usize]; kinds.len()]; self.noise_column(x, z, &mut |y, state| { let state = self.data.states.get(state); @@ -1067,7 +1098,7 @@ where } /// iterateNoiseColumn from the top down until `visit` returns true. fn noise_column(&self, x: i32, z: i32, visit: &mut dyn FnMut(i32, BlockStateId) -> bool) { - let (min, height) = (self.settings.min_y, self.settings.height); + let (min, height) = (self.settings.noise_min_y, self.settings.noise_height); let Ok(volume) = DensityVolume::new([1, height, 1], [x, min, z], [1; 3]) else { return; }; @@ -1131,11 +1162,12 @@ impl super::structure::jigsaw::JigsawContext for StructureQueri where R::Positional: Clone + Send + Sync, { + // Structure.GenerationContext.heightAccessor: the chunk, so the level's range. fn min_y(&self) -> i32 { - self.pipeline.settings.min_y + self.pipeline.settings.level_min_y } fn max_y(&self) -> i32 { - self.pipeline.settings.min_y + self.pipeline.settings.height - 1 + self.pipeline.settings.level_min_y + self.pipeline.settings.level_height - 1 } fn base_height(&mut self, x: i32, z: i32, kind: HeightmapType) -> i32 { if let Some(h) = self.worker.heights.get(&(x, z, kind as u8)) { @@ -1329,6 +1361,7 @@ trait SeededPipeline: Send + Sync { pos: ChunkPos, ) -> Vec>; fn unsupported_features(&self) -> Vec<(String, String)>; + fn forget_structure_starts(&self); fn ring_positions(&self, set: &str) -> Option>; fn locate( &self, @@ -1411,6 +1444,11 @@ where .map(super::generator_decoration::Decoration::unsupported) .unwrap_or_default() } + fn forget_structure_starts(&self) { + if let Some(structures) = &self.structures { + structures.forget_starts(); + } + } fn ring_positions(&self, set: &str) -> Option> { self.structures.as_ref()?.cached_rings(set) } @@ -1485,6 +1523,9 @@ impl OverworldGenerator { .ok_or_else(|| resource(format!("missing world preset {preset}")))?, )?; let generator = preset_json["dimensions"][dimension]["generator"].clone(); + let level = preset_json["dimensions"][dimension]["type"] + .as_str() + .and_then(dimension_type_height); if generator["type"].as_str() != Some("minecraft:noise") { return Err(resource(format!("{dimension} is not a noise generator"))); } @@ -1507,6 +1548,7 @@ impl OverworldGenerator { &generator, &settings_json, with_structures, + level, )?) } else { Arc::new(Pipeline::new( @@ -1516,6 +1558,7 @@ impl OverworldGenerator { &generator, &settings_json, with_structures, + level, )?) }; Ok(Self { pipeline }) @@ -1586,6 +1629,11 @@ impl GeneratorWorker { ) -> Vec> { self.pipeline.structure_starts(&mut self.state, pos) } + /// Forget the cached structure starts: chunks generated afterwards get + /// fresh starts, as in a new level that has not loaded the old chunks. + pub fn forget_structure_starts(&mut self) { + self.pipeline.forget_structure_starts(); + } pub fn stage_timings(&self) -> StageTimings { self.state.timings } diff --git a/crates/horizon-world/src/worldgen/generator_decoration.rs b/crates/horizon-world/src/worldgen/generator_decoration.rs index 993872f98..655de6a33 100644 --- a/crates/horizon-world/src/worldgen/generator_decoration.rs +++ b/crates/horizon-world/src/worldgen/generator_decoration.rs @@ -329,18 +329,16 @@ impl Decoration { decoration: self, temperatures: &mut worker.temperatures, behaviours: data.behaviours.as_deref(), - build: (generation.min_y, generation.min_y + generation.height), + // WorldGenRegion.isOutsideBuildHeight: the level's range, not the noise's. + build: (min_y, min_y + height), }; let mut stats = DecorateStats::default(); let blocks = self.blocks; let references = region[4].structure_starts.clone(); + // ChunkGenerator.getWritableArea: the chunk's minimum Y + 1 to its maximum Y. let chunk_box = BoundingBox::new( - [center.x * 16, generation.min_y + 1, center.z * 16], - [ - center.x * 16 + 15, - generation.min_y + generation.height - 1, - center.z * 16 + 15, - ], + [center.x * 16, min_y + 1, center.z * 16], + [center.x * 16 + 15, min_y + height - 1, center.z * 16 + 15], ); let mut structure_errors = 0; let result = self.plan.decorate( @@ -497,6 +495,11 @@ impl crate::level::LevelAccess for ShapeLevel<'_, '_> { fn game_time(&self) -> i64 { 0 } + /// Lighting has not run during FEATURES: the light engine has no data for + /// these chunks, so sky light reads zero too (MushroomBlock.canSurvive). + fn sky_light(&self, _: BlockPos) -> i32 { + 0 + } } impl World<'_> { /// `BlockState.updateShape` of the state at `pos` against `neighbor`. @@ -670,6 +673,40 @@ impl PlacementContext for World<'_> { nbt.insert("LootTableSeed".into(), Tag::Long(random.next_long())); self.set_block_entity(position, nbt) } + /// BeehiveDecorator: `getBlockEntity(pos, BEEHIVE)` then + /// `storeBee(Occupant.create(ticks))` for each bee (min ticks in hive 600). + fn store_beehive_occupants( + &mut self, + position: [i32; 3], + ticks_in_hive: &[i32], + ) -> Result<(), PlacementQueryError> { + use crate::nbt::Tag; + let chunk = self.region.chunk(position[0] >> 4, position[2] >> 4)?; + let state = chunk.block(position[0], position[1], position[2]); + let registry = self.decoration.bindings.registry(); + if !matches!( + registry.state(state).map(|s| s.name.as_str()), + Some("minecraft:bee_nest" | "minecraft:beehive") + ) { + return Ok(()); + } + let bees = ticks_in_hive + .iter() + .map(|&ticks| { + let mut entity = crate::nbt::Compound::new(); + entity.insert("id".into(), Tag::String("minecraft:bee".into())); + let mut bee = crate::nbt::Compound::new(); + bee.insert("entity_data".into(), Tag::Compound(entity)); + bee.insert("ticks_in_hive".into(), Tag::Int(ticks)); + bee.insert("min_ticks_in_hive".into(), Tag::Int(600)); + Tag::Compound(bee) + }) + .collect(); + let mut nbt = crate::nbt::Compound::new(); + nbt.insert("id".into(), Tag::String("minecraft:beehive".into())); + nbt.insert("bees".into(), Tag::List(bees)); + self.set_block_entity(position, nbt) + } /// SpawnerBlockEntity.setEntityId: the next spawn data and no potentials. fn configure_spawner( &mut self, diff --git a/crates/horizon-world/src/worldgen/generator_structures.rs b/crates/horizon-world/src/worldgen/generator_structures.rs index ce584eaa4..8a5f15d65 100644 --- a/crates/horizon-world/src/worldgen/generator_structures.rs +++ b/crates/horizon-world/src/worldgen/generator_structures.rs @@ -96,6 +96,11 @@ impl StructureState { pub(super) fn cached_rings(&self, set: &str) -> Option> { self.rings.get().and_then(|rings| rings.get(set).cloned()) } + /// Drop the cached starts, so that later chunks build fresh pieces (the + /// pieces keep per-start placement state such as a shipwreck's height). + pub(super) fn forget_starts(&self) { + self.starts.lock().unwrap().clear(); + } /// The (cached) structure starts owned by a chunk. pub(super) fn starts_at( &self, diff --git a/crates/horizon-world/src/worldgen/iceberg.rs b/crates/horizon-world/src/worldgen/iceberg.rs index e4370e71d..2bfeff29a 100644 --- a/crates/horizon-world/src/worldgen/iceberg.rs +++ b/crates/horizon-world/src/worldgen/iceberg.rs @@ -277,7 +277,8 @@ impl IcebergFeature { let off = 10.0f32 * random.next_float().clamp(0.2, 0.8) / radius as f32; f64::from(off) + pow2(xo) + pow2(zo) - pow2(radius) }; - if signed >= 0.0 { + // `signedDist < 0.0` fails for NaN too (a zero-width ellipse layer at its centre). + if signed.is_nan() || signed >= 0.0 { return Ok(()); } let p = tb::offset(origin, xo, y, zo); diff --git a/crates/horizon-world/src/worldgen/structure.rs b/crates/horizon-world/src/worldgen/structure.rs index 4b4c1cdbb..f15c37dc2 100644 --- a/crates/horizon-world/src/worldgen/structure.rs +++ b/crates/horizon-world/src/worldgen/structure.rs @@ -296,7 +296,12 @@ impl StructureResources { { sources.insert(id); } - if state.properties.get("waterlogged").map(String::as_str) == Some("false") { + // SlabBlock.placeLiquid refuses double slabs. + let double_slab = state.name.ends_with("_slab") + && state.properties.get("type").map(String::as_str) == Some("double"); + if !double_slab + && state.properties.get("waterlogged").map(String::as_str) == Some("false") + { let mut wet = state.clone(); wet.properties.insert("waterlogged".into(), "true".into()); if let Some(wet) = registry.id(&wet) { diff --git a/crates/horizon-world/src/worldgen/structure/legacy.rs b/crates/horizon-world/src/worldgen/structure/legacy.rs index 0d0ccb103..e55209baf 100644 --- a/crates/horizon-world/src/worldgen/structure/legacy.rs +++ b/crates/horizon-world/src/worldgen/structure/legacy.rs @@ -230,6 +230,8 @@ impl PieceContext<'_> { let mut nbt = Compound::new(); nbt.insert("id".into(), Tag::String("minecraft:mob_spawner".into())); nbt.insert("SpawnData".into(), Tag::Compound(data)); + // SpawnerBlockEntity.setEntityId on a fresh spawner: no potentials. + nbt.insert("SpawnPotentials".into(), Tag::List(Vec::new())); self.world.block_entity(pos, nbt) } } diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaChunkGenerationOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaChunkGenerationOracle.java index a5e6c1402..ea1d4aa23 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaChunkGenerationOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaChunkGenerationOracle.java @@ -411,9 +411,17 @@ public final class VanillaChunkGenerationOracle { var tag = target.getBlockEntityNbtForSaving(pos, registry); if (tag == null) continue; var st = target.getBlockState(pos); - var fresh = ((net.minecraft.world.level.block.EntityBlock) st.getBlock()).newBlockEntity(pos, st).saveWithFullMetadata(registry); - if ("DUMMY".equals(tag.getStringOr("id", ""))) tag = fresh; - w.println("be " + pos.getX() + " " + pos.getY() + " " + pos.getZ() + "\t" + canonicalOf(tag) + "\t" + canonicalOf(fresh)); + var block = (net.minecraft.world.level.block.EntityBlock) st.getBlock(); + var fresh = block.newBlockEntity(pos, st).saveWithFullMetadata(registry); + // The value of each key Horizon leaves out: what loading a tag without + // it gives (BrewingStandBlockEntity reads a missing total_brew_time as 400). + var empty = block.newBlockEntity(pos, st); + empty.loadWithComponents(net.minecraft.world.level.storage.TagValueInput.create( + net.minecraft.util.ProblemReporter.DISCARDING, registry, new net.minecraft.nbt.CompoundTag())); + var absent = empty.saveWithFullMetadata(registry); + // A DUMMY pending entry becomes a fresh block entity, not a loaded one. + if ("DUMMY".equals(tag.getStringOr("id", ""))) tag = absent = fresh; + w.println("be " + pos.getX() + " " + pos.getY() + " " + pos.getZ() + "\t" + canonicalOf(tag) + "\t" + canonicalOf(absent)); } } } diff --git a/tools/test_chunk_generation_parity.py b/tools/test_chunk_generation_parity.py index 962780756..8b831d3cd 100644 --- a/tools/test_chunk_generation_parity.py +++ b/tools/test_chunk_generation_parity.py @@ -141,7 +141,8 @@ def parse_nbt(text, i=0): def nbt_matches(vanilla, rust, default): """Horizon stores the placed data only; vanilla saves defaults too. Every key - Horizon sets must match, and every other vanilla key must be the fresh default.""" + Horizon sets must match, and every other vanilla key must be what vanilla + loads for an absent key (the oracle's second form).""" if isinstance(vanilla, dict) and isinstance(rust, dict): default = default if isinstance(default, dict) else {} if 'LootTableSeed' in rust and 'LootTable' not in rust: @@ -154,7 +155,11 @@ def nbt_matches(vanilla, rust, default): return False elif value != default.get(key): return False - return all(key in vanilla or key in ('x', 'y', 'z') or nbt_matches(default.get(key), rust[key], None) or (key, rust[key]) == VAULT_REWARD for key in rust) + return all(key in vanilla or key in ('x', 'y', 'z') or nbt_matches(default.get(key), rust[key], None) or (key, rust[key]) == VAULT_REWARD + or omitted_on_save(key, rust) for key in rust) + if isinstance(vanilla, tuple) and vanilla[0] == 'str' and isinstance(rust, dict) and set(rust) == {'text'}: + # A literal text component saves as its plain string: {text:"a"} is "a". + return vanilla == rust['text'] if isinstance(vanilla, list) and isinstance(rust, list): return len(vanilla) == len(rust) and all(nbt_matches(a, b, None) for a, b in zip(vanilla, rust)) if isinstance(vanilla, tuple) and isinstance(rust, tuple) and vanilla[0] in 'bsil' and rust[0] in 'bsil': @@ -163,6 +168,14 @@ def nbt_matches(vanilla, rust, default): return vanilla == rust +def omitted_on_save(key, rust): + """Template keys that vanilla's codecs leave out when re-saving the loaded block entity.""" + if key == 'patterns' and rust[key] == []: + return True # BannerBlockEntity saves its patterns only when there are some + # SignText saves filtered_messages only when some line differs from messages. + return key == 'filtered_messages' and rust[key] == rust.get('messages') + + def entity_matches(vanilla, rust): if vanilla is None: return False @@ -258,6 +271,7 @@ def pick_structures(starts, per_kind, rng): def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--seeds', type=int, default=3) + parser.add_argument('--seed', type=int, action='append', help='run only this seed (repeatable)') parser.add_argument('--per-structure', type=int, default=2) parser.add_argument('--sites', type=int, default=6, help='terrain sites per overworld seed') parser.add_argument('--radius', type=int, default=1, help='chunk radius around each site') @@ -281,7 +295,7 @@ def main(): biome_seen = set() structure_seen = Counter() started = time.time() - for seed in SEEDS[:args.seeds]: + for seed in args.seed or SEEDS[:args.seeds]: rng = random.Random(seed) for dimension in dimensions: rects, labels = [], []