From 8948bf90c4304dd142e090bc3006ff7f2cde2679 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 03:57:01 +0100 Subject: [PATCH] Structure layout harness: Horizon's starts piece by piece against the structure oracle The clean-room structure oracle saved, for 31 starts of vanilla structures (villages, outposts, trial chambers, shipwrecks, ruined portals, desert pyramids over seeds 1234, 2 and 42), the no-mod world's pieces. A new structure_pieces probe lists the starts a chunk owns, piece by piece, and tools/test_structure_layout_parity.py compares them with those saves. 22 of the 31 match piece for piece (every village and outpost that 26.3 still places there, and the horizontal boxes of the code-built pieces). The other 9 differ between 1.21.1 and 26.3 themselves: 26.3 vanilla's own pieces there (VanillaChunkGenerationOracle scan) equal Horizon's for all 18 starts that differ from 1.21.1. No vanilla jigsaw fix is needed. Templates now keep the id they were loaded under, so a piece names its element. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-world/examples/probe/main.rs | 2 + .../examples/probe/structure_pieces.rs | 103 ++++++++++++ .../horizon-world/src/worldgen/structure.rs | 8 +- .../src/worldgen/structure/template.rs | 7 +- tools/test_structure_layout_parity.py | 155 ++++++++++++++++++ 5 files changed, 271 insertions(+), 4 deletions(-) create mode 100644 crates/horizon-world/examples/probe/structure_pieces.rs create mode 100644 tools/test_structure_layout_parity.py diff --git a/crates/horizon-world/examples/probe/main.rs b/crates/horizon-world/examples/probe/main.rs index f3b9b6ef5..479a3ed23 100644 --- a/crates/horizon-world/examples/probe/main.rs +++ b/crates/horizon-world/examples/probe/main.rs @@ -168,6 +168,7 @@ mod stored_consume_fixture; mod stored_effect_attributes_fixture; mod stored_effect_notifications_fixture; mod structure_density_fixture; +mod structure_pieces; mod suffocation_fixture; mod swimming_fixture; mod synced_bytes_fixture; @@ -338,6 +339,7 @@ probes! { stored_effect_attributes_fixture, stored_effect_notifications_fixture, structure_density_fixture, + structure_pieces, suffocation_fixture, swimming_fixture, synced_bytes_fixture, diff --git a/crates/horizon-world/examples/probe/structure_pieces.rs b/crates/horizon-world/examples/probe/structure_pieces.rs new file mode 100644 index 000000000..ebbad9c7a --- /dev/null +++ b/crates/horizon-world/examples/probe/structure_pieces.rs @@ -0,0 +1,103 @@ +//! The structure starts a chunk owns, piece by piece, as a saved start lists them. +//! +//! structure_pieces ... +//! prints, for every start whose chunk is a target, `start ID CX CZ` and then +//! one `piece TYPE ELEMENT ROTATION X Y Z GROUND_LEVEL_DELTA X0 Y0 Z0 X1 Y1 Z1` +//! line per piece in placement order (jigsaw pieces: the element's template +//! location; other pieces: `-` for element, rotation and position). +use crate::support::common::tag; +use horizon_core::position::ChunkPos; +use horizon_world::{ + DATA_VERSION, + nbt::Tag, + registry::BlockRegistry, + worldgen::{ + generator::{GeneratorData, OverworldGenerator}, + structure::{Rotation, jigsaw::PoolElement, start::Piece}, + }, +}; +use serde_json::Value; +use std::{fs, path::Path}; + +type Error = Box; + +fn registry(resources: &Path) -> Result<&'static BlockRegistry, Error> { + let facts: Value = serde_json::from_str(&fs::read_to_string(resources.join("facts.json"))?)?; + let states = facts["states"].as_array().ok_or("facts states")?; + let registry = BlockRegistry::from_nbt(&Tag::compound([ + ("DataVersion", Tag::Int(DATA_VERSION)), + ( + "states", + Tag::List(states.iter().map(|v| tag(&v[4])).collect()), + ), + ("defaults", tag(&facts["defaults"])), + ]))?; + Ok(Box::leak(Box::new(registry))) +} + +fn element(element: &PoolElement) -> String { + match element { + PoolElement::Empty => "empty".into(), + PoolElement::Single { template, .. } => template.location.to_string(), + PoolElement::Feature { feature, .. } => format!("feature:{feature}"), + PoolElement::List { elements, .. } => { + let parts: Vec<_> = elements.iter().map(self::element).collect(); + format!("list:{}", parts.join("+")) + } + } +} + +fn rotation(rotation: Rotation) -> &'static str { + match rotation { + Rotation::None => "NONE", + Rotation::Clockwise90 => "CLOCKWISE_90", + Rotation::Clockwise180 => "CLOCKWISE_180", + Rotation::Counterclockwise90 => "COUNTERCLOCKWISE_90", + } +} + +pub fn main() -> Result<(), Error> { + let mut args = crate::args().skip(1); + let resources = std::path::PathBuf::from(args.next().ok_or("client resource directory")?); + let seed: i64 = args.next().ok_or("seed")?.parse()?; + let dimension = format!("minecraft:{}", args.next().ok_or("dimension")?); + let data = GeneratorData::load(&resources.join("worldgen"), registry(&resources)?)?; + let generator = OverworldGenerator::for_dimension(data, seed, "minecraft:normal", &dimension)?; + let mut worker = generator.worker(); + for target in args { + let (x, z) = target.split_once(',').ok_or("target cx,cz")?; + let chunk = ChunkPos::new(x.parse()?, z.parse()?); + for start in worker.structure_starts(chunk) { + if start.chunk != chunk { + continue; + } + println!("start {} {} {}", start.name, chunk.x, chunk.z); + for piece in &start.pieces { + let b = piece.bounding_box(); + let placed = match piece { + Piece::Pool(p) => format!( + "{} {} {} {} {} {}", + element(&p.element), + rotation(p.rotation), + p.position[0], + p.position[1], + p.position[2], + p.ground_level_delta + ), + Piece::Legacy(_) => "- - - - - -".into(), + }; + println!( + "piece {} {placed} {} {} {} {} {} {}", + piece.type_name(), + b.min[0], + b.min[1], + b.min[2], + b.max[0], + b.max[1], + b.max[2] + ); + } + } + } + Ok(()) +} diff --git a/crates/horizon-world/src/worldgen/structure.rs b/crates/horizon-world/src/worldgen/structure.rs index 405073450..d9b037808 100644 --- a/crates/horizon-world/src/worldgen/structure.rs +++ b/crates/horizon-world/src/worldgen/structure.rs @@ -347,14 +347,16 @@ impl StructureResources { .read_to_end(&mut raw) .ok()?; let tag = nbt::read_named(&raw, Limits::templates()).ok()?.0.tag; - Some(Arc::new(StructureTemplate::from_nbt( + Some(StructureTemplate::from_nbt( &tag, self.registry, &self.transforms, - ))) + )) }); // StructureTemplateManager.getOrCreate: missing templates are empty. - let loaded = loaded.or_else(|| Some(Arc::new(StructureTemplate::empty()))); + let mut loaded = loaded.unwrap_or_else(StructureTemplate::empty); + loaded.location = id.as_str().into(); + let loaded = Some(Arc::new(loaded)); self.templates.write().unwrap().insert(id, loaded.clone()); loaded } diff --git a/crates/horizon-world/src/worldgen/structure/template.rs b/crates/horizon-world/src/worldgen/structure/template.rs index 149033b8c..92c8c5676 100644 --- a/crates/horizon-world/src/worldgen/structure/template.rs +++ b/crates/horizon-world/src/worldgen/structure/template.rs @@ -5,7 +5,7 @@ use crate::{ nbt::{Compound, Tag}, registry::{BlockRegistry, BlockStateId}, }; -use std::sync::OnceLock; +use std::sync::{Arc, OnceLock}; #[derive(Clone, Debug, PartialEq)] pub struct BlockInfo { @@ -42,6 +42,9 @@ pub struct EntityInfo { } #[derive(Debug)] pub struct StructureTemplate { + /// The id it was loaded under (`StructureResources::template`); empty + /// for a template read from elsewhere. + pub location: Arc, pub size: [i32; 3], palettes: Vec, pub entities: Vec, @@ -129,6 +132,7 @@ impl StructureTemplate { /// A new, never-loaded template: zero size and no palettes. pub fn empty() -> Self { Self { + location: "".into(), size: [0; 3], palettes: Vec::new(), entities: Vec::new(), @@ -231,6 +235,7 @@ impl StructureTemplate { } } Self { + location: "".into(), size, palettes, entities, diff --git a/tools/test_structure_layout_parity.py b/tools/test_structure_layout_parity.py new file mode 100644 index 000000000..44544f06e --- /dev/null +++ b/tools/test_structure_layout_parity.py @@ -0,0 +1,155 @@ +#!/usr/bin/env python3 +"""Compare Horizon's structure starts, piece by piece, with the starts a real server saved. + +Expected side: the clean-room structure oracle (horizon-parity `cleanroom/structures/oracle/`), which ran +vanilla 1.21.1 servers and kept every saved start. Its `base_pieces` are the no-mod world's start of each +located structure (villages, outposts, trial chambers, shipwrecks, ruined portals, desert pyramids): the +vanilla layout. Its results live outside the repository; put them (or link them) under +`.local/structure-oracle//seed-.json.gz`, or point HORIZON_STRUCTURE_ORACLE at that folder. + +Horizon side: the structure_pieces probe generates the same seed and lists the starts each chunk owns. +Jigsaw pieces must match exactly: element (template location), rotation, position, ground level delta and +bounding box, in saved order. Other pieces (shipwrecks, ruined portals, desert pyramids) match by type and +horizontal box: their height is settled when they are placed, after the start is created, and the oracle +saved them placed. + +The oracle's game is 1.21.1 and Horizon's 26.3. KNOWN lists the starts where 26.3 itself differs from +1.21.1, each checked against 26.3 vanilla (VanillaChunkGenerationOracle's `scan`, HORIZON_PIECES=1), whose +pieces equal Horizon's there; they are reported, not failed. + +usage: test_structure_layout_parity.py [--profile lithostitched] [--filter REGEX] [--verbose] +""" +import argparse +import gzip +import json +import os +import re +import subprocess +import sys +from pathlib import Path + +try: + from . import lab +except ImportError: + import lab + +ORACLE = Path(os.environ.get('HORIZON_STRUCTURE_ORACLE', lab.LOCAL / 'structure-oracle')) +# (profile, seed, structure id, start chunk x, z) -> how 26.3 vanilla differs from 1.21.1 there. +TRIAL_CHAMBERS = '26.3 vanilla lays it out as Horizon does, unlike 1.21.1' +RUINED_PORTAL = "26.3 vanilla's portal is Horizon's; 1.21.1 picked another" +KNOWN = { + ('lithostitched', 1234, 'minecraft:village_plains', -24, 47): 'no start in 26.3 vanilla (nor in Horizon)', + ('lithostitched', 1234, 'minecraft:ruined_portal', -16, 51): RUINED_PORTAL, + ('lithostitched', 42, 'minecraft:ruined_portal', -19, -38): RUINED_PORTAL, + **{('lithostitched', seed, 'minecraft:trial_chambers', x, z): TRIAL_CHAMBERS + for seed, x, z in [(1234, 13, 21), (2, -28, 13), (2, 11, 16), (2, 146, 44), (42, 17, 7), (42, 38, -25)]}, +} + + +def element(saved): + kind = saved.get('element_type') + if kind in ('minecraft:single_pool_element', 'minecraft:legacy_single_pool_element'): + location = saved['location'] + return location if ':' in location else 'minecraft:' + location + if kind == 'minecraft:feature_pool_element': + return 'feature:' + saved['feature'] + if kind == 'minecraft:list_pool_element': + return 'list:' + '+'.join(element(e) for e in saved['elements']) + if kind == 'minecraft:empty_pool_element': + return 'empty' + return kind + + +def expected_piece(saved): + box = saved['BB'] + piece = {'type': saved['id'], 'box': box if saved['id'] == 'minecraft:jigsaw' else [box[0], box[2], box[3], box[5]]} + if saved['id'] == 'minecraft:jigsaw': + piece.update(element=element(saved['pool_element']), rotation=saved['rotation'], + position=[saved['PosX'], saved['PosY'], saved['PosZ']], ground_level_delta=saved['ground_level_delta']) + return piece + + +def probe_binary(): + subprocess.run(['cargo', 'build', '--release', '-q', '-p', 'horizon-world', '--example', 'world_probe'], check=True, cwd=lab.ROOT) + name = 'world_probe.exe' if os.name == 'nt' else 'world_probe' + return lab.cargo_target() / 'release/examples' / name + + +def horizon_starts(binary, seed, dimension, chunks): + result = subprocess.run([str(binary), 'structure_pieces', str(lab.RESOURCES), str(seed), dimension, + *(f'{x},{z}' for x, z in chunks)], capture_output=True, text=True, cwd=lab.ROOT) + if result.returncode: + raise SystemExit('structure_pieces failed: ' + result.stderr[-2000:]) + starts, current = {}, None + for line in result.stdout.splitlines(): + fields = line.split() + if fields[0] == 'start': + current = starts.setdefault((fields[1], int(fields[2]), int(fields[3])), []) + elif fields[0] == 'piece': + box = list(map(int, fields[8:14])) + piece = {'type': fields[1], 'box': box if fields[1] == 'minecraft:jigsaw' else [box[0], box[2], box[3], box[5]]} + if fields[1] == 'minecraft:jigsaw': + piece.update(element=fields[2], rotation=fields[3], position=list(map(int, fields[4:7])), + ground_level_delta=int(fields[7])) + current.append(piece) + return starts + + +def compare(expected, actual): + """Index of the first differing piece and what differs, or None.""" + for i, (want, got) in enumerate(zip(expected, actual)): + if want != got: + return i, {k: (want.get(k), got.get(k)) for k in want.keys() | got.keys() if want.get(k) != got.get(k)} + if len(expected) != len(actual): + return min(len(expected), len(actual)), {'pieces': (len(expected), len(actual))} + return None + + +def main(): + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument('--profile', default='lithostitched') + parser.add_argument('--filter', default='', help='only structure ids matching this regex') + parser.add_argument('--verbose', action='store_true') + args = parser.parse_args() + results = sorted((ORACLE / args.profile).glob('seed-*.json.gz')) + if not results: + print(f'SKIP: no oracle results in {ORACLE / args.profile} (see this file\'s docstring)') + return 0 + binary = probe_binary() + matched, known, failed = 0, 0, [] + for path in results: + oracle = json.load(gzip.open(path)) + seed = oracle['seed'] + by_dimension = {} + for start in oracle['structures']: + if start.get('base_pieces') is None or not re.search(args.filter, start['id']): + continue + by_dimension.setdefault(start['dimension'], []).append(start) + for dimension, starts in by_dimension.items(): + chunks = sorted({tuple(s['start_chunk']) for s in starts}) + actual = horizon_starts(binary, seed, dimension.removeprefix('minecraft:'), chunks) + for start in starts: + key = (start['id'], *start['start_chunk']) + expected = [expected_piece(p) for p in start['base_pieces']] + got = actual.get(key) + label = f'seed {seed} {start["id"]} chunk {start["start_chunk"]}' + difference = (0, {'start': 'missing in Horizon'}) if got is None else compare(expected, got) + reason = KNOWN.get((args.profile, seed, *key)) + if difference is None: + matched += 1 + if args.verbose: + print(f'ok {label}: {len(expected)} pieces') + elif reason: + known += 1 + print(f'known {label}: {reason}') + else: + failed.append(label) + index, fields = difference + print(f'FAIL {label}: {len(expected)} pieces expected, {len(got or [])} placed; piece {index}: {fields}') + total = matched + known + len(failed) + print(f'{matched} of {total} starts match the oracle piece for piece; {known} known version differences') + return 1 if failed else 0 + + +if __name__ == '__main__': + sys.exit(main())