Merge pull request #2076 from RoyalPixelsDE/fix/structure-starts-2-templates

🤖 Agent: Gate check by Ludovic's AI agent (Claude). Template pieces now save as vanilla's TemplateStructurePiece does. Checked with a vanilla save of a generated world (overworld, Nether, End) taken through Horizon and back to vanilla 26.3 on the server thread and in-frame. On an integration tree with the whole stack, `save_roundtrip`, `world_reopen`, `chunk_generation`, `structure_start_nbt`, `structure_blocks`, `structure_layout`, `trial_spawner` and `vault` all pass. Thanks @FusselTV!
This commit is contained in:
Ludovic Marie
2026-10-26 16:48:38 +01:00
committed by GitHub
18 changed files with 585 additions and 17 deletions
@@ -169,6 +169,7 @@ mod stored_effect_attributes_fixture;
mod stored_effect_notifications_fixture;
mod structure_density_fixture;
mod structure_pieces;
mod structure_start_nbt;
mod suffocation_fixture;
mod swimming_fixture;
mod synced_bytes_fixture;
@@ -340,6 +341,7 @@ probes! {
stored_effect_notifications_fixture,
structure_density_fixture,
structure_pieces,
structure_start_nbt,
suffocation_fixture,
swimming_fixture,
synced_bytes_fixture,
@@ -21,7 +21,7 @@ use std::{fs, path::Path};
type Error = Box<dyn std::error::Error + Send + Sync>;
fn registry(resources: &Path) -> Result<&'static BlockRegistry, Error> {
pub 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([
@@ -0,0 +1,74 @@
//! The structure starts a chunk owns, as `StructureStart.createTag` saves them.
//!
//! structure_start_nbt <client resource dir> <seed> <dimension> <cx,cz>...
//! prints `nbt CX CZ TAG` for every start whose chunk is a target, the tag
//! with sorted keys and typed numbers (VanillaStructureStartOracle's
//! `canonical`), or `unsaved CX CZ ID` when Horizon cannot save it yet.
use horizon_core::position::ChunkPos;
use horizon_world::{
nbt::Tag,
worldgen::generator::{GeneratorData, OverworldGenerator},
};
type Error = Box<dyn std::error::Error + Send + Sync>;
/// `VanillaStructureStartOracle.canonical`.
fn canonical(tag: &Tag) -> String {
let join = |parts: Vec<String>| parts.join(",");
match tag {
Tag::Compound(c) => {
let mut keys: Vec<_> = c.keys().map(|k| (k.to_string_lossy(), k)).collect();
keys.sort_by(|a, b| a.0.encode_utf16().cmp(b.0.encode_utf16()));
let parts = keys
.into_iter()
.map(|(name, key)| format!("{name}:{}", canonical(&c[key])))
.collect();
format!("{{{}}}", join(parts))
}
Tag::List(l) => format!("[{}]", join(l.iter().map(canonical).collect())),
Tag::Byte(v) => format!("{v}b"),
Tag::Short(v) => format!("{v}s"),
Tag::Int(v) => v.to_string(),
Tag::Long(v) => format!("{v}L"),
Tag::Float(bits) => format!("{}f", *bits as i32),
Tag::Double(bits) => format!("{}d", *bits as i64),
Tag::String(s) => format!(
"\"{}\"",
s.to_string_lossy()
.replace('\\', "\\\\")
.replace('"', "\\\"")
),
Tag::IntArray(a) => format!("[I;{}]", join(a.iter().map(i32::to_string).collect())),
Tag::LongArray(a) => format!("[L;{}]", join(a.iter().map(i64::to_string).collect())),
Tag::ByteArray(a) => format!(
"[B;{}]",
join(a.iter().map(|b| (*b as i8).to_string()).collect())
),
Tag::End => "END".into(),
}
}
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 registry = crate::structure_pieces::registry(&resources)?;
let data = GeneratorData::load(&resources.join("worldgen"), registry)?;
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;
}
match start.save() {
Some(tag) => println!("nbt {} {} {}", chunk.x, chunk.z, canonical(&tag)),
None => println!("unsaved {} {} {}", chunk.x, chunk.z, start.name),
}
}
}
Ok(())
}
+1
View File
@@ -114,6 +114,7 @@ mod place;
mod portal;
pub mod portal_travel;
mod query;
pub(crate) use query::data_2d;
mod rails;
mod redstone;
pub mod removal;
@@ -79,6 +79,15 @@ impl Rotation {
pub fn get_rotated(self, other: Self) -> Self {
Self::ALL[(self as usize + other as usize) % 4]
}
/// `Rotation.name()`, as structure pieces save it.
pub fn saved_name(self) -> &'static str {
match self {
Self::None => "NONE",
Self::Clockwise90 => "CLOCKWISE_90",
Self::Clockwise180 => "CLOCKWISE_180",
Self::Counterclockwise90 => "COUNTERCLOCKWISE_90",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Mirror {
@@ -86,6 +95,16 @@ pub enum Mirror {
LeftRight,
FrontBack,
}
impl Mirror {
/// `Mirror.name()`, as structure pieces save it.
pub fn saved_name(self) -> &'static str {
match self {
Self::None => "NONE",
Self::LeftRight => "LEFT_RIGHT",
Self::FrontBack => "FRONT_BACK",
}
}
}
/// Inclusive integer box (vanilla BoundingBox).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -381,19 +381,22 @@ pub fn pieces(
} else {
vec!["minecraft:structure_block".into(), "minecraft:air".into()]
};
EndCityPiece(TemplatePiece::new(
"minecraft:ecp",
&format!("minecraft:end_city/{}", d.name),
d.template,
d.position,
d.rotation,
Mirror::None,
[0; 3],
Arc::new(ProcessorList::default()),
true,
ignore,
TemplateHandler::Plain,
))
EndCityPiece(
TemplatePiece::new(
"minecraft:ecp",
&format!("minecraft:end_city/{}", d.name),
d.template,
d.position,
d.rotation,
Mirror::None,
[0; 3],
Arc::new(ProcessorList::default()),
true,
ignore,
TemplateHandler::Plain,
),
d.depth,
)
})
.collect(),
)
@@ -401,11 +404,21 @@ pub fn pieces(
/// EndCityPieces.EndCityPiece.
#[derive(Debug)]
pub struct EndCityPiece(TemplatePiece);
/// The template piece and its genDepth (the collision tag of its batch).
pub struct EndCityPiece(TemplatePiece, i32);
impl LegacyPiece for EndCityPiece {
fn bounding_box(&self) -> BoundingBox {
self.0.bounding_box
}
/// `EndCityPiece`: the template piece's tag, `OW` for the pieces that
/// overwrite air (only structure blocks ignored).
fn save(&self) -> Option<crate::nbt::Compound> {
let mut tag = self.0.saved();
tag.insert("GD".into(), crate::nbt::Tag::Int(self.1));
let overwrite = self.0.ignore.len() == 1;
tag.insert("OW".into(), crate::nbt::Tag::Byte(i8::from(overwrite)));
Some(tag)
}
fn type_name(&self) -> &'static str {
"minecraft:ecp"
}
@@ -294,6 +294,8 @@ mod tests {
source_x: junction[0],
source_ground_y: junction[1],
source_z: junction[2],
delta_y: 0,
dest_projection: Projection::Rigid,
}],
waterlogging: false,
})
@@ -303,6 +305,7 @@ mod tests {
Arc::new(PoolElement::Single {
template: Arc::new(StructureTemplate::empty()),
processors: Default::default(),
processors_source: serde_json::Value::Null,
legacy: false,
projection,
waterlogging: None,
@@ -5,6 +5,7 @@ use super::{
};
use crate::{
heightmap::HeightmapType,
nbt::{Compound, Tag},
worldgen::{
DecodeError,
block_feature::FeatureBlockBindings,
@@ -30,12 +31,24 @@ pub enum Projection {
Rigid,
TerrainMatching,
}
impl Projection {
/// `StructureTemplatePool.Projection.getSerializedName`.
pub fn saved_name(self) -> &'static str {
match self {
Self::Rigid => "rigid",
Self::TerrainMatching => "terrain_matching",
}
}
}
#[derive(Clone, Debug)]
pub enum PoolElement {
Empty,
Single {
template: Arc<StructureTemplate>,
processors: ProcessorList,
/// The element's `processors` as written: a list id or an inline list,
/// saved back as it was.
processors_source: Value,
legacy: bool,
projection: Projection,
waterlogging: Option<bool>,
@@ -136,6 +149,7 @@ impl PoolElement {
resources,
bindings,
)?,
processors_source: value["processors"].clone(),
legacy: kind == "minecraft:legacy_single_pool_element",
projection: projection()?,
waterlogging: match value
@@ -178,6 +192,64 @@ impl PoolElement {
_ => return Err(DecodeError::Field("pool element type")),
})
}
/// `StructurePoolElement.CODEC`'s encoding, as a jigsaw piece saves it.
pub fn save(&self) -> Tag {
let text = |s: &str| Tag::String(s.into());
match self {
Self::Empty => Tag::compound([("element_type", text("minecraft:empty_pool_element"))]),
Self::Single {
template,
processors_source,
legacy,
projection,
waterlogging,
..
} => {
let kind = if *legacy {
"minecraft:legacy_single_pool_element"
} else {
"minecraft:single_pool_element"
};
let mut entries = vec![
("element_type", text(kind)),
("location", text(&template.location)),
(
"processors",
super::processor::json_to_nbt(processors_source),
),
("projection", text(projection.saved_name())),
];
if let Some(apply) = waterlogging {
let settings = if *apply {
"apply_waterlogging"
} else {
"ignore_waterlogging"
};
entries.push(("override_liquid_settings", text(settings)));
}
Tag::compound(entries)
}
Self::Feature {
feature,
projection,
} => Tag::compound([
("element_type", text("minecraft:feature_pool_element")),
("feature", text(feature)),
("projection", text(projection.saved_name())),
]),
Self::List {
elements,
projection,
} => Tag::compound([
("element_type", text("minecraft:list_pool_element")),
(
"elements",
Tag::List(elements.iter().map(Self::save).collect()),
),
("projection", text(projection.saved_name())),
]),
}
}
pub fn projection(&self) -> Projection {
match self {
Self::Empty => Projection::TerrainMatching,
@@ -466,6 +538,9 @@ pub struct Junction {
pub source_x: i32,
pub source_ground_y: i32,
pub source_z: i32,
/// Saved only (`JigsawJunction.deltaY`, `destProjection`).
pub delta_y: i32,
pub dest_projection: Projection,
}
#[derive(Debug)]
pub struct PoolPiece {
@@ -517,6 +592,12 @@ impl PoolPiece {
source_x: int(j, "source_x")?,
source_ground_y: int(j, "source_ground_y")?,
source_z: int(j, "source_z")?,
delta_y: int(j, "delta_y")?,
dest_projection: match j["dest_proj"].as_str() {
Some("rigid") => Projection::Rigid,
Some("terrain_matching") => Projection::TerrainMatching,
_ => return Err(DecodeError::Field("dest_proj")),
},
})
})
.collect::<Result<_, DecodeError>>()?;
@@ -535,6 +616,45 @@ impl PoolPiece {
waterlogging: v["liquid_settings"].as_str() != Some("ignore_waterlogging"),
})
}
/// `PoolElementStructurePiece`'s tag (always genDepth 0, no orientation).
pub fn save(&self) -> Compound {
let mut tag = super::legacy::piece_tag("minecraft:jigsaw", self.bounding_box, None, 0);
let junctions = self
.junctions
.iter()
.map(|j| {
Tag::compound([
("source_x", Tag::Int(j.source_x)),
("source_ground_y", Tag::Int(j.source_ground_y)),
("source_z", Tag::Int(j.source_z)),
("delta_y", Tag::Int(j.delta_y)),
(
"dest_proj",
Tag::String(j.dest_projection.saved_name().into()),
),
])
})
.collect();
for (key, value) in [
("PosX", Tag::Int(self.position[0])),
("PosY", Tag::Int(self.position[1])),
("PosZ", Tag::Int(self.position[2])),
("ground_level_delta", Tag::Int(self.ground_level_delta)),
("pool_element", self.element.save()),
("rotation", Tag::String(self.rotation.saved_name().into())),
("junctions", Tag::List(junctions)),
] {
tag.insert(key.into(), value);
}
// LiquidSettings.APPLY_WATERLOGGING, the default, is not written.
if !self.waterlogging {
tag.insert(
"liquid_settings".into(),
Tag::String("ignore_waterlogging".into()),
);
}
tag
}
pub(super) fn moved(&mut self, dy: i32) {
self.position[1] += dy;
self.bounding_box = self.bounding_box.moved([0, dy, 0]);
@@ -1204,6 +1324,8 @@ impl Placer<'_, '_> {
source_x: target_pos[0],
source_ground_y: junction_y - local_y + source_ground,
source_z: target_pos[2],
delta_y,
dest_projection: target.projection(),
});
self.pieces.push(PoolPiece {
element: target.clone(),
@@ -1215,6 +1337,12 @@ impl Placer<'_, '_> {
source_x: jigsaw.pos[0],
source_ground_y: junction_y - target_local_y + target_ground,
source_z: jigsaw.pos[2],
delta_y: -delta_y,
dest_projection: if source_rigid {
Projection::Rigid
} else {
Projection::TerrainMatching
},
}],
waterlogging: self.config.waterlogging,
});
@@ -282,6 +282,11 @@ pub trait LegacyPiece: std::fmt::Debug + Send + Sync {
fn bounding_box(&self) -> BoundingBox;
/// The vanilla StructurePieceType id, for diagnostics and comparisons.
fn type_name(&self) -> &'static str;
/// `StructurePiece.createTag`; None while Horizon cannot save this type,
/// and then its start is not saved.
fn save(&self) -> Option<Compound> {
None
}
fn post_process(&self, context: &mut PieceContext<'_>) -> Result<()>;
/// Structure.afterPlace, run on the first piece once per chunk after all
/// pieces were placed, with every piece's current box.
@@ -290,6 +295,31 @@ pub trait LegacyPiece: std::fmt::Debug + Send + Sync {
}
}
/// `StructurePiece.createTag`'s own fields: the type, `BB`, `O` (the
/// orientation's 2D data value, -1 for none) and `GD` (genDepth).
pub fn piece_tag(
id: &str,
b: BoundingBox,
orientation: Option<Direction>,
gen_depth: i32,
) -> Compound {
let o = orientation.map_or(-1, crate::level::data_2d);
[
("id", Tag::String(id.into())),
(
"BB",
Tag::IntArray(vec![
b.min[0], b.min[1], b.min[2], b.max[0], b.max[1], b.max[2],
]),
),
("O", Tag::Int(o)),
("GD", Tag::Int(gen_depth)),
]
.into_iter()
.map(|(k, v)| (JavaString::from(k), v))
.collect()
}
/// StructurePiece's orientation transform and block helpers.
#[derive(Clone, Copy, Debug)]
pub struct PieceFrame {
@@ -73,6 +73,9 @@ impl LegacyPiece for NetherFossilPiece {
fn bounding_box(&self) -> BoundingBox {
self.0.bounding_box
}
fn save(&self) -> Option<crate::nbt::Compound> {
Some(self.0.saved())
}
fn type_name(&self) -> &'static str {
"minecraft:nefos"
}
@@ -325,6 +325,8 @@ pub fn post_process(piece: &TemplatePiece, c: &mut PieceContext<'_>) -> Result<(
new_y = min_y + 1;
}
position[1] = new_y;
// OceanRuinPiece.postProcess keeps the height in templatePosition.
*piece.state.lock().unwrap() = Some(new_y);
if let Some(markers) = piece.place_at(c, position, &[])? {
for (marker, pos) in markers {
if marker == "chest" {
@@ -910,6 +910,8 @@ impl Assembler<'_> {
source_x: slot.target_pos[0],
source_ground_y: junction_y - local_y + source_ground,
source_z: slot.target_pos[2],
delta_y,
dest_projection: target.projection(),
});
slot.free.borrow_mut().removed.push(target_box);
self.pieces.push(PoolPiece {
@@ -922,6 +924,12 @@ impl Assembler<'_> {
source_x: jigsaw.pos[0],
source_ground_y: junction_y - target_local[1] + target_ground,
source_z: jigsaw.pos[2],
delta_y: -delta_y,
dest_projection: if source_rigid {
Projection::Rigid
} else {
Projection::TerrainMatching
},
}],
waterlogging: self.config.waterlogging,
});
@@ -1022,6 +1030,7 @@ mod tests {
let element = Arc::new(PoolElement::Single {
template,
processors: Default::default(),
processors_source: serde_json::Value::Null,
legacy: false,
projection: Projection::Rigid,
waterlogging: None,
@@ -1038,6 +1047,8 @@ mod tests {
source_x: 1,
source_ground_y: 2,
source_z: 3,
delta_y: 0,
dest_projection: Projection::Rigid,
}],
waterlogging: true,
};
@@ -706,7 +706,7 @@ fn holder_set(
}
Ok(Arc::new(out))
}
fn json_to_nbt(value: &Value) -> Tag {
pub(super) fn json_to_nbt(value: &Value) -> Tag {
match value {
Value::Bool(b) => Tag::Byte(i8::from(*b)),
Value::Number(n) => match n.as_i64() {
@@ -25,6 +25,17 @@ pub enum Vertical {
InNether,
}
impl Vertical {
/// `RuinedPortalPiece.VerticalPlacement.getSerializedName`.
fn saved_name(self) -> &'static str {
match self {
Self::OnLandSurface => "on_land_surface",
Self::PartlyBuried => "partly_buried",
Self::OnOceanFloor => "on_ocean_floor",
Self::InMountain => "in_mountain",
Self::Underground => "underground",
Self::InNether => "in_nether",
}
}
fn heightmap(self) -> HeightmapType {
if self == Self::OnOceanFloor {
HeightmapType::OceanFloorWg
@@ -70,6 +81,9 @@ impl Setup {
#[derive(Clone, Copy, Debug)]
pub struct PortalProps {
placement: Vertical,
mossiness: f32,
air_pocket: bool,
blackstone: bool,
cold: bool,
overgrown: bool,
vines: bool,
@@ -182,6 +196,9 @@ pub fn start(
};
let props = PortalProps {
placement: setup.placement,
mossiness: setup.mossiness,
air_pocket,
blackstone: setup.blackstone,
cold,
overgrown: setup.overgrown,
vines: setup.vines,
@@ -379,3 +396,28 @@ pub fn post_process(piece: &TemplatePiece, c: &mut PieceContext<'_>) -> Result<(
}
Ok(())
}
impl PortalProps {
/// `RuinedPortalPiece`'s `VerticalPlacement` and `Properties` tags.
pub fn save(&self) -> [(&'static str, crate::nbt::Tag); 2] {
use crate::nbt::Tag;
let flag = |b: bool| Tag::Byte(i8::from(b));
[
(
"VerticalPlacement",
Tag::String(self.placement.saved_name().into()),
),
(
"Properties",
Tag::compound([
("cold", flag(self.cold)),
("mossiness", Tag::Float(self.mossiness.to_bits())),
("air_pocket", flag(self.air_pocket)),
("overgrown", flag(self.overgrown)),
("vines", flag(self.vines)),
("replace_with_blackstone", flag(self.blackstone)),
]),
),
]
}
}
@@ -193,10 +193,87 @@ pub fn shipwreck_too_big(template: &StructureTemplate) -> bool {
template.size[0] > 32 || template.size[1] > 32
}
impl TemplatePiece {
/// `TemplateStructurePiece.addAdditionalSaveData` and its subclasses',
/// with the height a shipwreck settled on once placed.
pub fn saved(&self) -> crate::nbt::Compound {
use crate::nbt::Tag;
let flag = |b: bool| Tag::Byte(i8::from(b));
let adjusted = match self.handler {
TemplateHandler::Shipwreck { .. }
| TemplateHandler::OceanRuin { .. }
| TemplateHandler::Igloo => *self.state.lock().unwrap(),
_ => None,
};
let dy = adjusted.map_or(0, |y| y - self.position[1]);
let template_dy = if matches!(self.handler, TemplateHandler::Igloo) {
0
} else {
dy
};
let mut tag = super::legacy::piece_tag(
self.type_name,
self.bounding_box.moved([0, dy, 0]),
Some(Direction::North),
0,
);
// Mansion and End City pieces keep the name inside their folder.
let name = self
.template_name
.strip_prefix("minecraft:woodland_mansion/")
.or_else(|| self.template_name.strip_prefix("minecraft:end_city/"))
.unwrap_or(&self.template_name);
let rotation = || Tag::String(self.rotation.saved_name().into());
let mut entries = vec![
("TPX", Tag::Int(self.position[0])),
("TPY", Tag::Int(self.position[1] + template_dy)),
("TPZ", Tag::Int(self.position[2])),
("Template", Tag::String(name.into())),
];
match self.handler {
TemplateHandler::Igloo => entries.push(("Rot", rotation())),
TemplateHandler::Shipwreck { beached } => entries.extend([
("isBeached", flag(beached)),
("Rot", rotation()),
("height_adjusted", flag(adjusted.is_some())),
]),
TemplateHandler::OceanRuin {
warm,
large,
integrity,
} => entries.extend([
("Rot", rotation()),
("Integrity", Tag::Float(integrity.to_bits())),
(
"BiomeType",
Tag::String(if warm { "WARM" } else { "COLD" }.into()),
),
("IsLarge", flag(large)),
]),
TemplateHandler::RuinedPortal(props) => {
entries.push(("Rotation", rotation()));
entries.push(("Mirror", Tag::String(self.mirror.saved_name().into())));
entries.extend(props.save());
}
TemplateHandler::Mansion => entries.extend([
("Rot", rotation()),
("Mi", Tag::String(self.mirror.saved_name().into())),
]),
TemplateHandler::Plain => entries.push(("Rot", rotation())),
}
for (key, value) in entries {
tag.insert(key.into(), value);
}
tag
}
}
impl LegacyPiece for TemplatePiece {
fn bounding_box(&self) -> BoundingBox {
self.bounding_box
}
fn save(&self) -> Option<crate::nbt::Compound> {
Some(self.saved())
}
fn after_place(&self, c: &mut PieceContext<'_>, pieces: &[BoundingBox]) -> Result<()> {
match self.handler {
TemplateHandler::Mansion => super::mansion::after_place(c, pieces),
@@ -232,6 +309,8 @@ impl LegacyPiece for TemplatePiece {
self.position[1] + height - 90 - 1,
self.position[2],
];
// The box stays moved; templatePosition goes back after placing.
*self.state.lock().unwrap() = Some(position[1]);
if let Some(markers) = self.place_at(c, position, &[])? {
for (marker, pos) in markers {
if marker == "chest" {
@@ -694,6 +694,13 @@ impl Piece {
Self::Legacy(p) => p.type_name(),
}
}
/// `StructurePiece.createTag`; None for a type Horizon cannot save yet.
pub fn save(&self) -> Option<crate::nbt::Compound> {
match self {
Self::Pool(p) => Some(p.save()),
Self::Legacy(p) => p.save(),
}
}
}
fn legacy(pieces: Vec<impl LegacyPiece + 'static>) -> Vec<Piece> {
pieces
@@ -735,6 +742,26 @@ pub struct StructureStart {
/// Piece union, inflated by 12 for terrain-adapting structures.
pub bounding_box: BoundingBox,
}
impl StructureStart {
/// `StructureStart.createTag` in its own chunk; None while one of its
/// pieces cannot be saved. `references` counts `addReference` calls,
/// which only locating structures makes: 0 in a freshly made world.
pub fn save(&self) -> Option<crate::nbt::Tag> {
use crate::nbt::Tag;
let children: Option<Vec<Tag>> = self
.pieces
.iter()
.map(|p| p.save().map(Tag::Compound))
.collect();
Some(Tag::compound([
("id", Tag::String(self.name.as_str().into())),
("ChunkX", Tag::Int(self.chunk.x)),
("ChunkZ", Tag::Int(self.chunk.z)),
("references", Tag::Int(0)),
("Children", Tag::List(children?)),
]))
}
}
/// A block of a noise column (ChunkGenerator.getBaseColumn), by what
/// structures ask of it.
@@ -76,7 +76,13 @@ public final class VanillaStructureStartOracle {
for(int qy=(floor-12)>>2;qy<=(floor+8)>>2;qy++)biomes.append(" "+qy+":"+resolver.getNoiseBiome(x>>2,qy,z>>2).unwrapKey().get().identifier().getPath());
w.println("height "+x+" "+z+" "+floor+" "+surface+biomes);
}
for(int cx=x0;cx<=x1;cx++)for(int cz=z0;cz<=z1;cz++) {
// HORIZON_CHUNKS="cx,cz;cx,cz": only these chunks, instead of the rectangle.
List<int[]> chunks=new ArrayList<>();
String only=System.getenv("HORIZON_CHUNKS");
if(only!=null)for(String c:only.split(";")){String[] xz=c.split(",");chunks.add(new int[]{Integer.parseInt(xz[0]),Integer.parseInt(xz[1])});}
else for(int cx=x0;cx<=x1;cx++)for(int cz=z0;cz<=z1;cz++)chunks.add(new int[]{cx,cz});
for(int[] chunk:chunks) {
int cx=chunk[0],cz=chunk[1];
ChunkPos pos=new ChunkPos(cx,cz);
for(var set:state.possibleStructureSets()) {
var placement=set.value().placement();var entries=set.value().structures();
@@ -107,6 +113,8 @@ public final class VanillaStructureStartOracle {
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);
}
if(System.getenv("HORIZON_START_NBT")!=null)
w.println("nbt "+cx+" "+cz+" "+canonical(start.createTag(new net.minecraft.world.level.levelgen.structure.pieces.StructurePieceSerializationContext(resources,access,templates),pos)));
break;
}
if(entries.size()==1)break;
@@ -118,4 +126,33 @@ public final class VanillaStructureStartOracle {
storage.close();
System.exit(0);
}
/** A tag as text with sorted keys and every number's type, so equal tags print equally. */
static String canonical(net.minecraft.nbt.Tag tag) {
return switch (tag) {
case net.minecraft.nbt.CompoundTag c -> {
var keys = new ArrayList<>(c.keySet());
Collections.sort(keys);
var b = new StringBuilder("{");
for (int i = 0; i < keys.size(); i++) b.append(i == 0 ? "" : ",").append(keys.get(i)).append(':').append(canonical(c.get(keys.get(i))));
yield b.append('}').toString();
}
case net.minecraft.nbt.ListTag l -> {
var b = new StringBuilder("[");
for (int i = 0; i < l.size(); i++) b.append(i == 0 ? "" : ",").append(canonical(l.get(i)));
yield b.append(']').toString();
}
case net.minecraft.nbt.ByteTag v -> v.value() + "b";
case net.minecraft.nbt.ShortTag v -> v.value() + "s";
case net.minecraft.nbt.IntTag v -> String.valueOf(v.value());
case net.minecraft.nbt.LongTag v -> v.value() + "L";
case net.minecraft.nbt.FloatTag v -> Float.floatToRawIntBits(v.value()) + "f";
case net.minecraft.nbt.DoubleTag v -> Double.doubleToRawLongBits(v.value()) + "d";
case net.minecraft.nbt.StringTag v -> "\"" + v.value().replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
case net.minecraft.nbt.IntArrayTag v -> "[I;" + String.join(",", Arrays.stream(v.getAsIntArray()).mapToObj(String::valueOf).toList()) + "]";
case net.minecraft.nbt.LongArrayTag v -> "[L;" + String.join(",", Arrays.stream(v.getAsLongArray()).mapToObj(String::valueOf).toList()) + "]";
case net.minecraft.nbt.ByteArrayTag v -> { var b = new StringBuilder("[B;"); byte[] a = v.getAsByteArray(); for (int i = 0; i < a.length; i++) b.append(i == 0 ? "" : ",").append(a[i]); yield b.append(']').toString(); }
default -> tag.toString();
};
}
}
+97
View File
@@ -0,0 +1,97 @@
#!/usr/bin/env python3
"""Compare the structure starts Horizon saves with the tags vanilla's StructureStart.createTag writes.
The oracle (VanillaStructureStartOracle with HORIZON_START_NBT) generates the start of each case's chunk with the
pinned server's own classes and prints its tag; the probe (world_probe structure_start_nbt) prints the tag
Horizon saves for the same chunk. Both print sorted keys and typed numbers, and the lines must be identical.
The cases are the start chunks of one site of every structure kind built from jigsaw pools or templates (seed
12345).
"""
import argparse
import collections
import json
import os
from pathlib import Path
import platform
import subprocess
try:
from . import lab, test_rust_parity as base
except ImportError:
import lab
import test_rust_parity as base
# (seed, dimension, chunk x, chunk z): the start chunk of one site of each structure kind.
CASES = [
(12345, 'overworld', -9, -52), (12345, 'overworld', -146, 37), (12345, 'overworld', -66, -98),
(12345, 'overworld', 91, 75), (12345, 'overworld', 75, -23), (12345, 'overworld', 17, -60),
(12345, 'overworld', 11, -11), (12345, 'overworld', 91, -90), (12345, 'overworld', 8, -33),
(12345, 'overworld', 2, -71), (12345, 'overworld', 39, -28), (12345, 'overworld', -90, -60),
(12345, 'the_nether', 11, -16),
(12345, 'overworld', 291, 441), (12345, 'overworld', 36, 41), (12345, 'overworld', -8, 2),
(12345, 'overworld', 114, -133), (12345, 'overworld', -16, 5), (12345, 'overworld', -109, 91),
(12345, 'overworld', 0, -18), (12345, 'overworld', -150, 51), (12345, 'overworld', 61, 49),
(12345, 'the_nether', 0, -18), (12345, 'the_nether', -22, 6), (12345, 'the_end', -15, -76),
]
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.parse_args()
build = lab.LOCAL / 'rust-structure-start-nbt-parity'
build.mkdir(parents=True, exist_ok=True)
report_path = build / 'report.json'
report_path.unlink(missing_ok=True)
lock, jars, libraries = base.verify_artifacts()
oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaStructureStartOracle.java'
probe = lab.ROOT / 'crates/horizon-world/examples/probe/structure_start_nbt.rs'
sources = [oracle, probe, Path(__file__), lab.ROOT / 'tools/test_rust_parity.py', lab.ROOT / 'tools/lab.py',
lab.ROOT / 'Cargo.toml', lab.ROOT / 'Cargo.lock', lab.ROOT / 'crates/horizon-world/Cargo.toml',
*sorted((lab.ROOT / 'crates/horizon-core/src').rglob('*.rs')),
*sorted((lab.ROOT / 'crates/horizon-world/src').rglob('*.rs'))]
tested_sources = base.source_hashes(sources)
def run(command, name, env=None):
with (build / (name + '.log')).open('w', encoding='utf-8') as log:
base.run(list(map(str, command)), stdout=log, stderr=subprocess.STDOUT, env=env)
classes = build / 'classes'
classes.mkdir(exist_ok=True)
dependencies = os.pathsep.join(map(str, [jars['client'], *libraries]))
run(['javac', '--release', lock['minecraft']['java_major'], '-proc:none', '-sourcepath',
lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle], 'compile-java')
run(['cargo', 'build', '--release', '-p', 'horizon-world', '--example', 'world_probe'], 'compile-rust')
metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'],
cwd=lab.ROOT, text=True))
binary = Path(metadata['target_directory']) / 'release/examples' / (
'world_probe.exe' if os.name == 'nt' else 'world_probe')
resources = lab.LOCAL / 'horizon-client' / lock['minecraft']['version']
groups = collections.defaultdict(list)
for seed, dimension, cx, cz in CASES:
groups[(seed, dimension)].append((cx, cz))
vanilla, horizon = [], []
for (seed, dimension), chunks in sorted(groups.items()):
name = f'{seed}-{dimension}'
out = build / f'client-{name}.txt'
env = dict(os.environ, HORIZON_START_NBT='1', HORIZON_DIMENSION=dimension,
HORIZON_CHUNKS=';'.join(f'{x},{z}' for x, z in chunks))
run(['java', '-Xmx2g', '-cp', str(classes) + os.pathsep + dependencies,
'dev.horizonlab.parity.VanillaStructureStartOracle', out, seed, 0, 0, 0, 0], 'client-' + name, env)
vanilla += sorted(l for l in out.read_text(encoding='utf-8').splitlines() if l.startswith('nbt '))
probe_out = subprocess.check_output([str(binary), 'structure_start_nbt', str(resources), str(seed), dimension,
*(f'{x},{z}' for x, z in chunks)], text=True)
horizon += sorted(probe_out.splitlines())
(build / 'client.txt').write_text(''.join(l + '\n' for l in vanilla), encoding='utf-8')
(build / 'rust.txt').write_text(''.join(l + '\n' for l in horizon), encoding='utf-8')
if len(vanilla) != len(CASES):
raise AssertionError(f'the oracle found {len(vanilla)} starts for {len(CASES)} cases')
matched = base.compare(build / 'client.txt', build / 'rust.txt')
base.ensure_sources_unchanged(sources, tested_sources)
lab.write_json(report_path, dict(
schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), units=len(CASES),
sources_sha256=tested_sources, matched_output_lines=matched,
output_sha256=lab.digest(build / 'rust.txt', 'sha256'), client_jar_sha256=lab.digest(jars['client'], 'sha256')))
print(f'{len(CASES)} structure starts save as vanilla ({matched} lines): {report_path}', flush=True)
if __name__ == '__main__':
main()