diff --git a/crates/horizon-world/src/worldgen/generator.rs b/crates/horizon-world/src/worldgen/generator.rs index 858c8abf1..2d80130d9 100644 --- a/crates/horizon-world/src/worldgen/generator.rs +++ b/crates/horizon-world/src/worldgen/generator.rs @@ -1322,15 +1322,23 @@ where self.pipeline.generation() } fn noise_biome(&mut self, quart: [i32; 3]) -> BiomeId { + self.noise_biomes(&[quart])[0] + } + fn noise_biomes(&mut self, quarts: &[[i32; 3]]) -> Vec { let mut resolver = self.pipeline.source.resolver( &self.pipeline.climate, self.pipeline.router.get(RouterField::Erosion).as_ref(), ); - resolver - .sample(&mut self.worker.carver_context, quart) - .ok() - .and_then(|name| self.pipeline.data.biome_id(name)) - .unwrap_or(BiomeId(0)) + quarts + .iter() + .map(|&quart| { + resolver + .sample(&mut self.worker.carver_context, quart) + .ok() + .and_then(|name| self.pipeline.data.biome_id(name)) + .unwrap_or(BiomeId(0)) + }) + .collect() } fn set_valid_biomes(&mut self, biomes: &BTreeSet) { self.valid = biomes.clone(); @@ -2056,10 +2064,8 @@ mod tests { assert_eq!(second.state.carver_biomes, expected); } - /// Generators of one seed and dimension share their ring positions; - /// another seed or generator gets its own, and old entries go. - #[test] - fn ring_positions_are_shared_by_seed_and_dimension() { + /// The prepared worldgen data, if this checkout has it. + fn prepared_data() -> Option> { let root = std::env::var_os("HORIZON_RESOURCES") .map(std::path::PathBuf::from) .unwrap_or_else(|| { @@ -2067,12 +2073,46 @@ mod tests { }); if !root.join("worldgen/world_presets.json").exists() { eprintln!("skipping: no worldgen bundle at {}", root.display()); - return; + return None; } let registry: &'static BlockRegistry = Box::leak(Box::new( crate::level::data::load_registry(&root).expect("registry"), )); - let data = GeneratorData::load(&root.join("worldgen"), registry).expect("worldgen data"); + Some(GeneratorData::load(&root.join("worldgen"), registry).expect("worldgen data")) + } + + /// Seed 1234's strongholds where vanilla 26.3 puts them + /// (VanillaStructureStartOracle with HORIZON_RINGS). Each search sampled + /// its quarts through fresh resolvers, which broke climate ties their own + /// way: three of the 128 were up to 6 chunks off. + #[test] + fn stronghold_rings_match_vanilla() { + let Some(data) = prepared_data() else { + return; + }; + let generator = OverworldGenerator::new(data, 1234).expect("generator"); + generator.worker().structure_starts(ChunkPos::new(0, 0)); + let rings = generator + .ring_positions("minecraft:strongholds") + .expect("ring positions"); + assert_eq!(rings.len(), 128); + for (i, x, z) in [ + (0, -100, -125), + (1, 131, -12), + (111, 1190, -560), + (113, 1305, -112), + ] { + assert_eq!(rings[i], ChunkPos::new(x, z), "stronghold {i}"); + } + } + + /// Generators of one seed and dimension share their ring positions; + /// another seed or generator gets its own, and old entries go. + #[test] + fn ring_positions_are_shared_by_seed_and_dimension() { + let Some(data) = prepared_data() else { + return; + }; let first = OverworldGenerator::new(data.clone(), 1234).expect("generator"); let again = OverworldGenerator::new(data.clone(), 1234).expect("generator"); { diff --git a/crates/horizon-world/src/worldgen/generator_structures.rs b/crates/horizon-world/src/worldgen/generator_structures.rs index 519686242..1e4913c95 100644 --- a/crates/horizon-world/src/worldgen/generator_structures.rs +++ b/crates/horizon-world/src/worldgen/generator_structures.rs @@ -74,14 +74,14 @@ impl RingWork { fn help<'a>( &self, preferred: impl Fn(usize) -> &'a BTreeSet, - biome: &mut dyn FnMut([i32; 3]) -> BiomeId, + biomes: &mut dyn FnMut(&[[i32; 3]]) -> Vec, ) { loop { let i = self.next.fetch_add(1, Ordering::Relaxed); let Some((set, search)) = self.searches.get(i) else { break; }; - let _ = self.found[i].set(search.find(preferred(*set), biome)); + let _ = self.found[i].set(search.find(preferred(*set), biomes)); let mut left = self.left.lock().unwrap(); *left -= 1; if *left == 0 { @@ -239,8 +239,8 @@ impl StructureState { ) }); let registry = &self.registry; - work.help(|set| registry.ring_biomes(set), &mut |q| { - context.noise_biome(q) + work.help(|set| registry.ring_biomes(set), &mut |quarts| { + context.noise_biomes(quarts) }); self.rings .get_or_init(|| work.rings(|set| registry.sets[set].name.clone())) @@ -475,8 +475,13 @@ mod tests { #[test] fn ring_work_shared_between_threads_matches_serial_search() { let preferred: BTreeSet = (0..20).map(|b| BiomeId(b * 3)).collect(); - let biome = |q: [i32; 3]| { - BiomeId((q[0].wrapping_mul(31) ^ q[2].wrapping_mul(17)).rem_euclid(64) as u32) + let biomes = |quarts: &[[i32; 3]]| -> Vec { + quarts + .iter() + .map(|q| { + BiomeId((q[0].wrapping_mul(31) ^ q[2].wrapping_mul(17)).rem_euclid(64) as u32) + }) + .collect() }; let none = BTreeSet::new(); for seed in [0, 1234, -77, i64::MAX] { @@ -488,12 +493,12 @@ mod tests { let sets = [&preferred, &none, &preferred]; std::thread::scope(|scope| { for _ in 0..4 { - scope.spawn(|| work.help(|set| sets[set], &mut { biome })); + scope.spawn(|| work.help(|set| sets[set], &mut { biomes })); } }); let rings = work.rings(|set| format!("set{set}")); let serial = |s: i64, distance, spread, count, preferred| { - ring_positions(s, distance, spread, count, preferred, &mut { biome }) + ring_positions(s, distance, spread, count, preferred, &mut { biomes }) }; assert_eq!(rings["set0"], serial(seed, 32, 3, 128, &preferred)); assert_eq!(rings["set1"], Vec::new()); diff --git a/crates/horizon-world/src/worldgen/structure/start.rs b/crates/horizon-world/src/worldgen/structure/start.rs index 3d61bf168..dfb8176e0 100644 --- a/crates/horizon-world/src/worldgen/structure/start.rs +++ b/crates/horizon-world/src/worldgen/structure/start.rs @@ -707,6 +707,10 @@ pub trait StartContext: JigsawContext { fn generation(&self) -> WorldGenerationContext; /// Noise biome at a quart position (caching resolver, no zoom). fn noise_biome(&mut self, quart: [i32; 3]) -> BiomeId; + /// Noise biomes of `quarts` in order through one resolver, as + /// BiomeSource.findBiomeHorizontal samples: each climate lookup starts + /// from the previous result, which a tie keeps. + fn noise_biomes(&mut self, quarts: &[[i32; 3]]) -> Vec; fn set_valid_biomes(&mut self, biomes: &BTreeSet); } diff --git a/crates/horizon-world/src/worldgen/structure/stronghold.rs b/crates/horizon-world/src/worldgen/structure/stronghold.rs index 270b412af..e1a10808f 100644 --- a/crates/horizon-world/src/worldgen/structure/stronghold.rs +++ b/crates/horizon-world/src/worldgen/structure/stronghold.rs @@ -14,17 +14,18 @@ use std::{collections::BTreeSet, f64::consts::PI, sync::Mutex}; /// ChunkGeneratorStructureState.generateRingPositions with /// BiomeSource.findBiomeHorizontal (radius 112, step 1, not closest). +/// `biomes` samples a search's quarts in order, as one caching resolver. pub fn ring_positions( seed: i64, distance: i32, spread: i32, count: i32, preferred: &BTreeSet, - biome: &mut dyn FnMut([i32; 3]) -> BiomeId, + biomes: &mut dyn FnMut(&[[i32; 3]]) -> Vec, ) -> Vec { ring_searches(seed, distance, spread, count) .iter() - .map(|search| search.find(preferred, biome)) + .map(|search| search.find(preferred, biomes)) .collect() } @@ -39,23 +40,30 @@ pub struct RingSearch { impl RingSearch { /// BiomeSource.findBiomeHorizontal around the ring chunk's centre: a /// random preferred biome within 112 blocks, else the ring chunk itself. + /// + /// Vanilla samples the square in order on one thread, and its climate + /// index (Climate.RTree) starts each lookup from the previous result and + /// keeps it on a tie; so `biomes` gets the whole square at once, in + /// vanilla's order. Fresh lookups moved seed 1234's first stronghold 6 + /// chunks: one quart on a lush caves / ocean tie went to the ocean. pub fn find( &self, preferred: &BTreeSet, - biome: &mut dyn FnMut([i32; 3]) -> BiomeId, + biomes: &mut dyn FnMut(&[[i32; 3]]) -> Vec, ) -> ChunkPos { let mut search = self.random.clone(); let (cx, cz) = ((self.x * 16 + 8) >> 2, (self.z * 16 + 8) >> 2); + let quarts: Vec<[i32; 3]> = (-28..=28) + .flat_map(|dz| (-28..=28).map(move |dx| [cx + dx, 0, cz + dz])) + .collect(); let mut found = 0; let mut result = None; - for dz in -28..=28 { - for dx in -28..=28 { - if preferred.contains(&biome([cx + dx, 0, cz + dz])) { - if result.is_none() || search.next_int_bound(found + 1).unwrap_or(1) == 0 { - result = Some(((cx + dx) << 2, (cz + dz) << 2)); - } - found += 1; + for (quart, biome) in quarts.iter().zip(biomes(&quarts)) { + if preferred.contains(&biome) { + if result.is_none() || search.next_int_bound(found + 1).unwrap_or(1) == 0 { + result = Some((quart[0] << 2, quart[2] << 2)); } + found += 1; } } match result {