From c5fc4d7eb59258d5281168eb07e4572852fa0f80 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 8 Oct 2026 03:19:08 +0200 Subject: [PATCH] Read 26.3 template block states and verify jigsaw starts against vanilla Templates store palette states as id/properties; reading only Name made every template block air. Add a vanilla oracle (structure placement, start selection and Structure.generate over a chunk rectangle), a bench mode dumping Rust starts in the same format, and a comparison script. For seed 0 over 97x97 chunks all 15 jigsaw starts (villages, trial chambers, trail ruins, abandoned camps) match vanilla piece for piece. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../horizon-world/examples/chunkgen_bench.rs | 77 +++++++++++++++ .../horizon-world/src/worldgen/generator.rs | 31 ++++++ .../src/worldgen/structure/jigsaw.rs | 18 +++- .../src/worldgen/structure/template.rs | 7 +- .../parity/VanillaStructureStartOracle.java | 95 +++++++++++++++++++ tools/compare_structure_starts.py | 55 +++++++++++ tools/structure_start_oracle.py | 35 +++++++ 7 files changed, 311 insertions(+), 7 deletions(-) create mode 100644 mods/horizon/src/test/java/dev/horizonlab/parity/VanillaStructureStartOracle.java create mode 100644 tools/compare_structure_starts.py create mode 100644 tools/structure_start_oracle.py diff --git a/crates/horizon-world/examples/chunkgen_bench.rs b/crates/horizon-world/examples/chunkgen_bench.rs index e38b01392..f38de35df 100644 --- a/crates/horizon-world/examples/chunkgen_bench.rs +++ b/crates/horizon-world/examples/chunkgen_bench.rs @@ -146,6 +146,69 @@ fn verify(data: &std::sync::Arc, parity: &Path) -> Result<(), Err } } +/// Structure starts in the oracle's line format (VanillaStructureStartOracle). +fn dump_starts( + generator: &OverworldGenerator, + seed: i64, + [x0, z0, x1, z1]: [i32; 4], + out: &Path, +) -> Result<(), Error> { + use horizon_world::worldgen::structure::{Rotation, start::Piece}; + let _ = seed; + let mut worker = generator.worker(); + let mut lines = Vec::new(); + let start = Instant::now(); + for cx in x0..=x1 { + for cz in z0..=z1 { + for s in worker.structure_starts(ChunkPos::new(cx, cz)) { + let b = s.bounding_box; + lines.push(format!( + "start {cx} {cz} {} {} {} {} {} {} {} {}", + s.name, + b.min[0], + b.min[1], + b.min[2], + b.max[0], + b.max[1], + b.max[2], + s.pieces.len() + )); + for piece in &s.pieces { + let Piece::Pool(p) = piece; + let b = p.bounding_box; + let rotation = match p.rotation { + Rotation::None => "NONE", + Rotation::Clockwise90 => "CLOCKWISE_90", + Rotation::Clockwise180 => "CLOCKWISE_180", + Rotation::Counterclockwise90 => "COUNTERCLOCKWISE_90", + }; + lines.push(format!( + "piece minecraft:jigsaw {} {} {} {} {} {} {} {} {} {rotation} {} {}", + b.min[0], + b.min[1], + b.min[2], + b.max[0], + b.max[1], + b.max[2], + p.position[0], + p.position[1], + p.position[2], + p.ground_level_delta, + p.junctions.len() + )); + } + } + } + } + println!( + "structure starts for {} chunks in {:.2?}", + (x1 - x0 + 1) * (z1 - z0 + 1), + start.elapsed() + ); + fs::write(out, lines.join("\n") + "\n")?; + Ok(()) +} + /// Process CPU seconds (Linux), robust against other load on shared machines. fn cpu_seconds() -> f64 { fs::read_to_string("/proc/self/stat") @@ -198,6 +261,7 @@ fn main() -> Result<(), Error> { let (mut seed, mut size, mut threads, mut center) = (0i64, 16i32, 2usize, (0, 0)); let mut parity = None; let mut decorate = true; + let mut starts: Option<(i32, i32, i32, i32, PathBuf)> = None; while let Some(arg) = args.next() { let mut next = || args.next().expect("argument value"); match arg.as_str() { @@ -207,6 +271,15 @@ fn main() -> Result<(), Error> { "--center" => center = (next().parse()?, next().parse()?), "--verify" => parity = Some(PathBuf::from(next())), "--no-decorate" => decorate = false, + "--starts" => { + starts = Some(( + next().parse()?, + next().parse()?, + next().parse()?, + next().parse()?, + PathBuf::from(next()), + )) + } _ => return Err(format!("unknown argument {arg}").into()), } } @@ -220,6 +293,10 @@ fn main() -> Result<(), Error> { let start = Instant::now(); let generator = OverworldGenerator::new(data.clone(), seed)?; println!("seeded generator in {:.2?}", start.elapsed()); + if let Some((x0, z0, x1, z1, out)) = starts { + dump_starts(&generator, seed, [x0, z0, x1, z1], &out)?; + return Ok(()); + } let (x0, z0) = (center.0 - size / 2, center.1 - size / 2); let count = (size * size) as usize; let next = AtomicUsize::new(0); diff --git a/crates/horizon-world/src/worldgen/generator.rs b/crates/horizon-world/src/worldgen/generator.rs index 7a1708599..3a0b76529 100644 --- a/crates/horizon-world/src/worldgen/generator.rs +++ b/crates/horizon-world/src/worldgen/generator.rs @@ -1264,6 +1264,11 @@ trait SeededPipeline: Send + Sync { ) -> Result; fn data(&self) -> &Arc; fn sea_level(&self) -> i32; + fn structure_starts( + &self, + worker: &mut WorkerState, + pos: ChunkPos, + ) -> Vec>; fn unsupported_features(&self) -> Vec<(String, String)>; } impl SeededPipeline for Pipeline @@ -1314,6 +1319,25 @@ where fn sea_level(&self) -> i32 { self.settings.sea_level } + fn structure_starts( + &self, + worker: &mut WorkerState, + pos: ChunkPos, + ) -> Vec> { + match &self.structures { + Some(structures) => structures + .starts_at( + &mut StructureQueries { + pipeline: self, + worker, + valid: BTreeSet::new(), + }, + pos, + ) + .to_vec(), + None => Vec::new(), + } + } fn unsupported_features(&self) -> Vec<(String, String)> { self.decoration .as_ref() @@ -1452,6 +1476,13 @@ impl GeneratorWorker { self.state.timings.chunks += 1; result } + /// STRUCTURE_STARTS: the structure starts owned by a chunk (cached). + pub fn structure_starts( + &mut self, + pos: ChunkPos, + ) -> Vec> { + self.pipeline.structure_starts(&mut self.state, pos) + } pub fn stage_timings(&self) -> StageTimings { self.state.timings } diff --git a/crates/horizon-world/src/worldgen/structure/jigsaw.rs b/crates/horizon-world/src/worldgen/structure/jigsaw.rs index 76035582e..005fab6d0 100644 --- a/crates/horizon-world/src/worldgen/structure/jigsaw.rs +++ b/crates/horizon-world/src/worldgen/structure/jigsaw.rs @@ -345,9 +345,16 @@ impl Pools { let value: Value = serde_json::from_str(json).ok()?; let mut templates = Vec::new(); for entry in value["elements"].as_array()? { - let element = Arc::new( - PoolElement::decode(&entry["element"], resources, bindings, None).ok()?, - ); + let element = + match PoolElement::decode(&entry["element"], resources, bindings, None) { + Ok(element) => Arc::new(element), + Err(error) => { + if std::env::var_os("HORIZON_JIGSAW_DEBUG").is_some() { + eprintln!("pool {name}: {error}"); + } + return None; + } + }; for _ in 0..entry["weight"].as_i64().unwrap_or(1) { templates.push(element.clone()); } @@ -687,12 +694,13 @@ impl Placer<'_, '_> { let source_rigid = source_element.projection() == Projection::Rigid; let mut source_free: Option>> = None; let source_y = source_box.min[1]; - 'jigsaws: for jigsaw in source_element.shuffled_jigsaws( + let jigsaws = source_element.shuffled_jigsaws( self.resources, source_position, source_rotation, random, - ) { + ); + 'jigsaws: for jigsaw in jigsaws { let direction = jigsaw.front; let s = step(direction); let target_pos: [i32; 3] = std::array::from_fn(|i| jigsaw.pos[i] + s[i]); diff --git a/crates/horizon-world/src/worldgen/structure/template.rs b/crates/horizon-world/src/worldgen/structure/template.rs index 8726d2b20..a95a68f46 100644 --- a/crates/horizon-world/src/worldgen/structure/template.rs +++ b/crates/horizon-world/src/worldgen/structure/template.rs @@ -66,14 +66,17 @@ pub(super) fn read_state(registry: &BlockRegistry, tag: &Tag) -> BlockStateId { let Tag::Compound(compound) = tag else { return air; }; - let Some(name) = string(get(compound, "Name")) else { + // 26.3 writes `id`/`properties`; older templates use `Name`/`Properties`. + let Some(name) = string(get(compound, "id")).or_else(|| string(get(compound, "Name"))) else { return air; }; let Some(default) = registry.default_state(&name) else { return air; }; let mut state = registry.state(default).unwrap().clone(); - if let Some(Tag::Compound(properties)) = get(compound, "Properties") { + if let Some(Tag::Compound(properties)) = + get(compound, "properties").or_else(|| get(compound, "Properties")) + { for (key, value) in properties { let (Tag::String(value), key) = (value, key.to_string_lossy()) else { continue; diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaStructureStartOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaStructureStartOracle.java new file mode 100644 index 000000000..3b0818cc3 --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaStructureStartOracle.java @@ -0,0 +1,95 @@ +package dev.horizonlab.parity; + +import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; +import java.nio.file.*; +import java.util.*; +import net.minecraft.SharedConstants; +import net.minecraft.core.*; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.RegistryDataLoader; +import net.minecraft.server.Bootstrap; +import net.minecraft.server.RegistryLayer; +import net.minecraft.server.packs.PackType; +import net.minecraft.server.packs.repository.ServerPacksSource; +import net.minecraft.server.packs.resources.MultiPackResourceManager; +import net.minecraft.tags.TagLoader; +import net.minecraft.util.datafix.DataFixers; +import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.LevelHeightAccessor; +import net.minecraft.world.level.biome.MultiNoiseBiomeSource; +import net.minecraft.world.level.biome.MultiNoiseBiomeSourceParameterLists; +import net.minecraft.world.level.chunk.ChunkGeneratorStructureState; +import net.minecraft.world.level.levelgen.*; +import net.minecraft.world.level.levelgen.structure.*; +import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager; +import net.minecraft.world.level.storage.LevelStorageSource; + +/** + * Original oracle: runs vanilla structure placement checks, start selection + * and Structure.generate (as ChunkGenerator.createStructures does for a fresh + * chunk) for a rectangle of overworld chunks and prints every valid start. + */ +public final class VanillaStructureStartOracle { + public static void main(String[] args) throws Exception { + SharedConstants.tryDetectVersion();Bootstrap.bootStrap(); + Path out=Path.of(args[0]);long seed=Long.parseLong(args[1]); + int x0=Integer.parseInt(args[2]),z0=Integer.parseInt(args[3]),x1=Integer.parseInt(args[4]),z1=Integer.parseInt(args[5]); + var repo=ServerPacksSource.createVanillaTrustedRepository();repo.reload();repo.setSelected(List.of("vanilla")); + var resources=new MultiPackResourceManager(PackType.SERVER_DATA,repo.openAllSelected()); + var layers=RegistryLayer.createRegistryAccess(); + var staticTags=TagLoader.loadTagsForExistingRegistries(resources,layers.getLayer(RegistryLayer.STATIC)); + var context=TagLoader.buildUpdatedLookups(layers.getAccessForLoading(RegistryLayer.WORLD),staticTags); + RegistryAccess.Frozen world=RegistryDataLoader.load(resources,context,RegistryDataLoader.WORLD_REGISTRIES,Runnable::run).join(); + staticTags.forEach(Registry.PendingTags::apply); + RegistryAccess access=layers.replaceFrom(RegistryLayer.WORLD,world).compositeAccess(); + var settings=world.lookupOrThrow(Registries.NOISE_SETTINGS).getOrThrow(NoiseGeneratorSettings.OVERWORLD); + var randomState=RandomState.create(world.lookupOrThrow(Registries.NOISE),seed,settings.value()); + var biomeSource=MultiNoiseBiomeSource.createFromPreset(world.lookupOrThrow(Registries.MULTI_NOISE_BIOME_SOURCE_PARAMETER_LIST).getOrThrow(MultiNoiseBiomeSourceParameterLists.OVERWORLD)); + var generator=new NoiseBasedChunkGenerator(biomeSource,settings); + var state=ChunkGeneratorStructureState.createForNormal(randomState,seed,new ChunkPos(0,0),biomeSource,world.lookupOrThrow(Registries.STRUCTURE_SET)); + Path tmp=Files.createTempDirectory("horizon-structures"); + var storage=LevelStorageSource.createDefault(tmp).createAccess("oracle"); + var templates=new StructureTemplateManager(resources,storage,DataFixers.getDataFixer(),BuiltInRegistries.BLOCK); + LevelHeightAccessor height=LevelHeightAccessor.create(-64,384); + var climate=randomState.createClimateSampler(net.minecraft.world.level.levelgen.densityfunction.SamplerContext.builder().enableCaches().build()); + var structures=world.lookupOrThrow(Registries.STRUCTURE); + try(var w=new PrintWriter(Files.newBufferedWriter(out,StandardCharsets.UTF_8))) { + structures.listElements().forEach(h->w.println("registry "+h.key().identifier()+" "+h.value().step().ordinal())); + for(int cx=x0;cx<=x1;cx++)for(int cz=z0;cz<=z1;cz++) { + ChunkPos pos=new ChunkPos(cx,cz); + for(var set:state.possibleStructureSets()) { + var placement=set.value().placement();var entries=set.value().structures(); + if(!placement.isStructureChunk(state,cx,cz))continue; + var options=new ArrayList<>(entries); + WorldgenRandom random=new WorldgenRandom(new LegacyRandomSource(0L));random.setLargeFeatureSeed(seed,cx,cz); + int total=0;for(var o:options)total+=o.weight(); + while(!options.isEmpty()) { + StructureSet.StructureSelectionEntry selected;int index=0; + if(entries.size()==1){selected=options.get(0);} + else{int choice=random.nextInt(total);for(var o:options){choice-=o.weight();if(choice<0)break;index++;}selected=options.get(index);} + var structure=selected.structure(); + StructureStart start=structure.value().generate(structure,Level.OVERWORLD,access,generator,biomeSource,climate,randomState,templates,seed,pos,0,height,structure.value().biomes()::contains); + if(start.isValid()) { + var b=start.getBoundingBox(); + w.println("start "+cx+" "+cz+" "+structure.unwrapKey().get().identifier()+" "+b.minX()+" "+b.minY()+" "+b.minZ()+" "+b.maxX()+" "+b.maxY()+" "+b.maxZ()+" "+start.getPieces().size()); + for(var piece:start.getPieces()) { + var pb=piece.getBoundingBox(); + String extra=""; + if(piece instanceof PoolElementStructurePiece pool)extra=" "+pool.getPosition().getX()+" "+pool.getPosition().getY()+" "+pool.getPosition().getZ()+" "+pool.getRotation()+" "+pool.getGroundLevelDelta()+" "+pool.getJunctions().size()+" "+pool.getElement(); + w.println("piece "+BuiltInRegistries.STRUCTURE_PIECE.getKey(piece.getType())+" "+pb.minX()+" "+pb.minY()+" "+pb.minZ()+" "+pb.maxX()+" "+pb.maxY()+" "+pb.maxZ()+extra); + } + break; + } + if(entries.size()==1)break; + options.remove(index);total-=selected.weight(); + } + } + } + } + storage.close(); + System.exit(0); + } +} diff --git a/tools/compare_structure_starts.py b/tools/compare_structure_starts.py new file mode 100644 index 000000000..363e7b76f --- /dev/null +++ b/tools/compare_structure_starts.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +"""Compare Rust structure starts (chunkgen_bench --starts) with the vanilla oracle. + +usage: compare_structure_starts.py VANILLA RUST [--all] +Pieces are compared by type, bounding box and (for jigsaw pieces) position, +rotation, ground level delta and junction count. Without --all, vanilla +starts of structure types Rust does not generate yet are only counted. +""" +import collections +import sys + + +def load(path): + starts = {} + current = None + for line in open(path): + t = line.split() + if not t: + continue + if t[0] == 'start': + current = (int(t[1]), int(t[2]), t[3]) + starts[current] = [' '.join(t[4:])] + elif t[0] == 'piece' and current: + starts[current].append(' '.join(t[1:14])) + return starts + + +def main(): + vanilla, rust = load(sys.argv[1]), load(sys.argv[2]) + generated = {k[2] for k in rust} + matched = 0 + missing = collections.Counter() + for key in sorted(vanilla): + if key not in rust: + missing[key[2]] += 1 + if key[2] in generated or '--all' in sys.argv: + print('missing', key) + continue + if vanilla[key] == rust[key]: + matched += 1 + continue + v, r = vanilla[key], rust[key] + print('DIFF', key, 'pieces', len(v) - 1, len(r) - 1) + for i, (a, b) in enumerate(zip(v, r)): + if a != b: + print(' first difference at line', i, '\n vanilla', a, '\n rust ', b) + break + for key in sorted(set(rust) - set(vanilla)): + print('extra', key) + print(f'{matched} of {len(vanilla)} vanilla starts match exactly; not generated by Rust:', dict(missing)) + return 0 if matched == len(rust) == sum(1 for k in vanilla if k[2] in generated) else 1 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tools/structure_start_oracle.py b/tools/structure_start_oracle.py new file mode 100644 index 000000000..3382905a9 --- /dev/null +++ b/tools/structure_start_oracle.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +"""Run the vanilla structure-start oracle for a seed and chunk rectangle. + +usage: structure_start_oracle.py OUT SEED X0 Z0 X1 Z1 +""" +import os +import subprocess +import sys +try: + from . import lab, test_rust_parity as base +except ImportError: + import lab + import test_rust_parity as base + + +def main(): + out, seed, x0, z0, x1, z1 = sys.argv[1:7] + lock, jars, libraries = base.verify_artifacts() + source = lab.ROOT/'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaStructureStartOracle.java' + build = lab.LOCAL/'structure-start-oracle' + classes = build/'classes' + classes.mkdir(parents=True, exist_ok=True) + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-cp', dependencies, + '-d', str(classes), str(source)], check=True) + result = subprocess.run(['java', '-Xmx2G', '-cp', str(classes)+os.pathsep+dependencies, + 'dev.horizonlab.parity.VanillaStructureStartOracle', out, seed, x0, z0, x1, z1], + capture_output=True, text=True) + if result.returncode: + lines = [line for line in (result.stdout + result.stderr).splitlines() if 'Exception' in line or 'Caused' in line] + raise SystemExit('oracle failed: ' + ' | '.join(lines[:5])) + + +if __name__ == '__main__': + main()