Add a vanilla structure placement oracle
VanillaStructurePlacementOracle generates vanilla terrain with live structure starts and references for the 3x3 chunks around a target, runs the structure part of applyBiomeDecoration through a WorldGenRegion-like level and dumps the target chunk's blocks and block entities. The structure_blocks example does the same with Horizon's generator (GeneratorWorker::set_structures_only skips placed features) and tools/compare_structure_blocks.py reports the differences. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
//! Structure placement dump in the vanilla structure-placement oracle's
|
||||
//! format: full terrain generation, then decoration with structure pieces
|
||||
//! only (no placed features) for each target chunk.
|
||||
//!
|
||||
//! structure_blocks <client resource dir> <seed> <dimension> <cx,cz>...
|
||||
//! prints `chunk CX CZ`, one `y Y ID...` line per block row (256 state ids in
|
||||
//! z-major order) and `be X Y Z ID LOOTTABLE SEED` per block entity.
|
||||
use horizon_core::position::ChunkPos;
|
||||
use horizon_world::{
|
||||
DATA_VERSION,
|
||||
nbt::Tag,
|
||||
registry::BlockRegistry,
|
||||
worldgen::generator::{GeneratorData, OverworldGenerator, ProtoChunk},
|
||||
};
|
||||
use serde_json::Value;
|
||||
use std::{fs, path::Path};
|
||||
|
||||
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||
|
||||
fn tag(v: &Value) -> Tag {
|
||||
match v {
|
||||
Value::String(s) => Tag::String(s.as_str().into()),
|
||||
Value::Number(n) => Tag::Int(n.as_i64().unwrap() as i32),
|
||||
Value::Array(a) => Tag::List(a.iter().map(tag).collect()),
|
||||
Value::Object(o) => {
|
||||
Tag::Compound(o.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect())
|
||||
}
|
||||
_ => panic!("invalid registry metadata"),
|
||||
}
|
||||
}
|
||||
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 main() -> Result<(), Error> {
|
||||
let mut args = std::env::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();
|
||||
worker.set_structures_only(true);
|
||||
for target in args {
|
||||
let (cx, cz) = target.split_once(',').ok_or("target cx,cz")?;
|
||||
let (cx, cz): (i32, i32) = (cx.parse()?, cz.parse()?);
|
||||
let mut region = Vec::with_capacity(9);
|
||||
for dz in -1..=1 {
|
||||
for dx in -1..=1 {
|
||||
region.push(worker.generate_proto(ChunkPos::new(cx + dx, cz + dz))?);
|
||||
}
|
||||
}
|
||||
let mut region: [ProtoChunk; 9] = region.try_into().map_err(|_| "region size")?;
|
||||
worker.decorate(&mut region)?;
|
||||
let chunk = ®ion[4];
|
||||
println!("chunk {cx} {cz}");
|
||||
let (min, height) = (chunk.terrain.min_y(), chunk.terrain.height());
|
||||
for y in min..min + height {
|
||||
let mut line = format!("y {y}");
|
||||
for z in 0..16 {
|
||||
for x in 0..16 {
|
||||
line.push_str(&format!(" {}", chunk.block(cx * 16 + x, y, cz * 16 + z).0));
|
||||
}
|
||||
}
|
||||
println!("{line}");
|
||||
}
|
||||
let mut entities: Vec<_> = chunk.block_entities.iter().collect();
|
||||
entities.sort_by_key(|(p, _)| *p);
|
||||
for ([x, y, z], nbt) in entities {
|
||||
let text = |key: &str| match nbt.get(&key.into()) {
|
||||
Some(Tag::String(s)) => s.to_string_lossy(),
|
||||
_ => "-".into(),
|
||||
};
|
||||
let seed = match nbt.get(&"LootTableSeed".into()) {
|
||||
Some(Tag::Long(v)) => v.to_string(),
|
||||
_ => "-".into(),
|
||||
};
|
||||
println!("be {x} {y} {z} {} {} {seed}", text("id"), text("LootTable"));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -1293,6 +1293,8 @@ pub(super) struct WorkerState {
|
||||
carver_context: SamplerContext,
|
||||
timings: StageTimings,
|
||||
heights: HashMap<(i32, i32, u8), i32>,
|
||||
/// Decorate with structure pieces only (for structure parity checks).
|
||||
pub(super) structures_only: bool,
|
||||
}
|
||||
|
||||
trait SeededPipeline: Send + Sync {
|
||||
@@ -1501,6 +1503,7 @@ impl OverworldGenerator {
|
||||
biome_context: SamplerContext::cached(),
|
||||
carver_context: SamplerContext::default(),
|
||||
timings: StageTimings::default(),
|
||||
structures_only: false,
|
||||
heights: HashMap::new(),
|
||||
},
|
||||
}
|
||||
@@ -1540,6 +1543,11 @@ impl GeneratorWorker {
|
||||
) -> Result<DecorateStats, GeneratorError> {
|
||||
self.pipeline.decorate(&mut self.state, region)
|
||||
}
|
||||
/// Skip placed features when decorating and keep structure pieces only
|
||||
/// (for structure placement parity checks).
|
||||
pub fn set_structures_only(&mut self, only: bool) {
|
||||
self.state.structures_only = only;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -295,6 +295,7 @@ impl Decoration {
|
||||
Ok(terrain) => terrain,
|
||||
Err(e) => return Err(resource(e.to_string())),
|
||||
};
|
||||
let structures_only = worker.structures_only;
|
||||
let mut block_entities = Vec::new();
|
||||
for chunk in region.iter_mut() {
|
||||
block_entities.append(&mut chunk.block_entities);
|
||||
@@ -369,6 +370,9 @@ impl Decoration {
|
||||
Ok(())
|
||||
},
|
||||
|world, placer, random, id, origin| {
|
||||
if structures_only {
|
||||
return Ok(false);
|
||||
}
|
||||
let Ok(feature) = &self.features[id.0 as usize] else {
|
||||
stats.unsupported += 1;
|
||||
stats.skipped.insert(self.names[id.0 as usize].clone());
|
||||
|
||||
+233
@@ -0,0 +1,233 @@
|
||||
package dev.horizonlab.parity;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.lang.reflect.*;
|
||||
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.Identifier;
|
||||
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.RandomizableContainer;
|
||||
import net.minecraft.world.level.*;
|
||||
import net.minecraft.world.level.biome.*;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.EntityBlock;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.chunk.*;
|
||||
import net.minecraft.world.level.chunk.status.ChunkStatus;
|
||||
import net.minecraft.world.level.levelgen.*;
|
||||
import net.minecraft.world.level.levelgen.blending.Blender;
|
||||
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: vanilla terrain (biomes and buildTerrain with live
|
||||
* structure starts and references) for the 3x3 chunks around each target,
|
||||
* then the structure part of ChunkGenerator.applyBiomeDecoration (no placed
|
||||
* features) through a WorldGenRegion-like level, and a dump of the target
|
||||
* chunk's blocks and block entities.
|
||||
*
|
||||
* usage: OUT SEED DIMENSION CX,CZ...
|
||||
*/
|
||||
public final class VanillaStructurePlacementOracle {
|
||||
private static final class Ignored extends RuntimeException {}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SharedConstants.tryDetectVersion();Bootstrap.bootStrap();
|
||||
Path out=Path.of(args[0]);long seed=Long.parseLong(args[1]);String dimension=args[2];
|
||||
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();
|
||||
var loaded=net.minecraft.server.ReloadableServerRegistries.reload(layers.replaceFrom(RegistryLayer.WORLD,world),staticTags,resources,Runnable::run).join();
|
||||
var components=BuiltInRegistries.DATA_COMPONENT_INITIALIZERS.build(loaded.lookupWithUpdatedTags());
|
||||
staticTags.forEach(Registry.PendingTags::apply);
|
||||
components.forEach(net.minecraft.core.component.DataComponentInitializers.PendingComponents::apply);
|
||||
RegistryAccess access=loaded.layers().compositeAccess();
|
||||
var settings=world.lookupOrThrow(Registries.NOISE_SETTINGS).getOrThrow(switch(dimension){case "the_nether"->NoiseGeneratorSettings.NETHER;case "the_end"->NoiseGeneratorSettings.END;default->NoiseGeneratorSettings.OVERWORLD;});
|
||||
var presets=world.lookupOrThrow(Registries.MULTI_NOISE_BIOME_SOURCE_PARAMETER_LIST);
|
||||
BiomeSource biomeSource=switch(dimension){
|
||||
case "the_nether"->MultiNoiseBiomeSource.createFromPreset(presets.getOrThrow(MultiNoiseBiomeSourceParameterLists.NETHER));
|
||||
case "the_end"->TheEndBiomeSource.create(world.lookupOrThrow(Registries.BIOME));
|
||||
default->MultiNoiseBiomeSource.createFromPreset(presets.getOrThrow(MultiNoiseBiomeSourceParameterLists.OVERWORLD));};
|
||||
var levelKey=switch(dimension){case "the_nether"->Level.NETHER;case "the_end"->Level.END;default->Level.OVERWORLD;};
|
||||
var randomState=RandomState.create(world.lookupOrThrow(Registries.NOISE),seed,settings.value());
|
||||
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=dimension.equals("overworld")?LevelHeightAccessor.create(-64,384):LevelHeightAccessor.create(0,256);
|
||||
var climate=randomState.createClimateSampler(net.minecraft.world.level.levelgen.densityfunction.SamplerContext.builder().enableCaches().build());
|
||||
var structures=world.lookupOrThrow(Registries.STRUCTURE);
|
||||
var factory=PalettedContainerFactory.create(access);
|
||||
try(var w=new PrintWriter(Files.newBufferedWriter(out,StandardCharsets.UTF_8))) {
|
||||
for(int t=3;t<args.length;t++) {
|
||||
String[] xz=args[t].split(",");int cx=Integer.parseInt(xz[0]),cz=Integer.parseInt(xz[1]);
|
||||
Map<Long,ProtoChunk> chunks=new HashMap<>();
|
||||
java.util.function.BiFunction<Integer,Integer,ProtoChunk> chunkAt=(x,z)->chunks.computeIfAbsent(ChunkPos.pack(x,z),k->new ProtoChunk(new ChunkPos(x,z),UpgradeData.EMPTY,height,factory,null));
|
||||
// STRUCTURE_STARTS within reference range of the 3x3 region.
|
||||
for(int x=cx-9;x<=cx+9;x++)for(int z=cz-9;z<=cz+9;z++) {
|
||||
ChunkPos pos=new ChunkPos(x,z);ProtoChunk chunk=chunkAt.apply(x,z);
|
||||
for(var set:state.possibleStructureSets()) {
|
||||
var placement=set.value().placement();var entries=set.value().structures();
|
||||
if(!placement.isStructureChunk(state,x,z))continue;
|
||||
var options=new ArrayList<>(entries);
|
||||
WorldgenRandom random=new WorldgenRandom(new LegacyRandomSource(0L));random.setLargeFeatureSeed(seed,x,z);
|
||||
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,levelKey,access,generator,biomeSource,climate,randomState,templates,seed,pos,0,height,structure.value().biomes()::contains);
|
||||
if(start.isValid()){chunk.setStartForStructure(structure.value(),start);break;}
|
||||
if(entries.size()==1)break;
|
||||
options.remove(index);total-=selected.weight();
|
||||
}
|
||||
}
|
||||
}
|
||||
// STRUCTURE_REFERENCES for the 3x3 region.
|
||||
for(int x=cx-1;x<=cx+1;x++)for(int z=cz-1;z<=cz+1;z++) {
|
||||
ProtoChunk chunk=chunkAt.apply(x,z);int bx=x<<4,bz=z<<4;
|
||||
for(int sx=x-8;sx<=x+8;sx++)for(int sz=z-8;sz<=z+8;sz++)
|
||||
for(StructureStart start:chunkAt.apply(sx,sz).getAllStarts().values())
|
||||
if(start.isValid()&&start.getBoundingBox().intersects(bx,bz,bx+15,bz+15))chunk.addReferenceForStructure(start.getStructure(),ChunkPos.pack(sx,sz));
|
||||
}
|
||||
int[] center={cx,cz};
|
||||
var heightRef=new Object(){LevelHeightAccessor value=height;};
|
||||
BiomeManager[] zoom=new BiomeManager[1];
|
||||
var uncached=biomeSource;
|
||||
Object[] self=new Object[1];
|
||||
var level=(WorldGenLevel)Proxy.newProxyInstance(WorldGenLevel.class.getClassLoader(),new Class<?>[]{WorldGenLevel.class},(proxy,method,values)->{
|
||||
switch(method.getName()) {
|
||||
case "getMinY":return heightRef.value.getMinY();
|
||||
case "getHeight":{
|
||||
if(values==null||values.length==0)return heightRef.value.getHeight();
|
||||
if(values.length==3){var kind=(Heightmap.Types)values[0];int x=(Integer)values[1],z=(Integer)values[2];return chunkAt.apply(x>>4,z>>4).getHeight(kind,x&15,z&15)+1;}
|
||||
break;
|
||||
}
|
||||
case "getSeaLevel":return generator.getSeaLevel();
|
||||
case "getSeed":return seed;
|
||||
case "registryAccess":return access;
|
||||
case "getRandom":return self[0];
|
||||
case "getBlockState":{var p=(BlockPos)values[0];return chunkAt.apply(p.getX()>>4,p.getZ()>>4).getBlockState(p);}
|
||||
case "getFluidState":{var p=(BlockPos)values[0];return chunkAt.apply(p.getX()>>4,p.getZ()>>4).getFluidState(p);}
|
||||
case "isStateAtPosition":{var p=(BlockPos)values[0];@SuppressWarnings("unchecked")var test=(java.util.function.Predicate<BlockState>)values[1];return test.test(chunkAt.apply(p.getX()>>4,p.getZ()>>4).getBlockState(p));}
|
||||
case "isFluidAtPosition":{var p=(BlockPos)values[0];@SuppressWarnings("unchecked")var test=(java.util.function.Predicate<net.minecraft.world.level.material.FluidState>)values[1];return test.test(chunkAt.apply(p.getX()>>4,p.getZ()>>4).getFluidState(p));}
|
||||
case "ensureCanWrite":{var p=(BlockPos)values[0];return Math.abs((p.getX()>>4)-center[0])<=1&&Math.abs((p.getZ()>>4)-center[1])<=1;}
|
||||
case "setBlock":{
|
||||
if(values.length!=4)break;
|
||||
var p=(BlockPos)values[0];var blockState=(BlockState)values[1];int flags=(Integer)values[2];
|
||||
if(!(Math.abs((p.getX()>>4)-center[0])<=1&&Math.abs((p.getZ()>>4)-center[1])<=1))return false;
|
||||
ProtoChunk chunk=chunkAt.apply(p.getX()>>4,p.getZ()>>4);
|
||||
BlockState old=chunk.setBlockState(p,blockState,flags);
|
||||
if(blockState.hasBlockEntity()){var tag=new net.minecraft.nbt.CompoundTag();tag.putInt("x",p.getX());tag.putInt("y",p.getY());tag.putInt("z",p.getZ());tag.putString("id","DUMMY");chunk.setBlockEntityNbt(tag);}
|
||||
else if(old!=null&&old.hasBlockEntity())chunk.removeBlockEntity(p);
|
||||
if((flags&16)==0){BlockPos post=blockState.getPostProcessPos((BlockGetter)proxy,p);if(post!=null)chunkAt.apply(post.getX()>>4,post.getZ()>>4).markPosForPostProcessing(post);}
|
||||
return true;
|
||||
}
|
||||
case "getBlockEntity":{
|
||||
if(values.length!=1)break;
|
||||
var p=(BlockPos)values[0];ProtoChunk chunk=chunkAt.apply(p.getX()>>4,p.getZ()>>4);
|
||||
BlockEntity be=chunk.getBlockEntity(p);if(be!=null)return be;
|
||||
var tag=chunk.getBlockEntityNbt(p);BlockState st=chunk.getBlockState(p);
|
||||
if(tag!=null){
|
||||
if("DUMMY".equals(tag.getStringOr("id",""))){if(!st.hasBlockEntity())return null;be=((EntityBlock)st.getBlock()).newBlockEntity(p,st);}
|
||||
else be=BlockEntity.loadStatic(p,st,tag,access);
|
||||
if(be!=null){chunk.setBlockEntity(be);return be;}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
case "getChunk":{
|
||||
int x,z;
|
||||
if(values[0] instanceof BlockPos p){x=p.getX()>>4;z=p.getZ()>>4;}else{x=(Integer)values[0];z=(Integer)values[1];}
|
||||
if(values.length==4&&!(Boolean)values[3]&&!chunks.containsKey(ChunkPos.pack(x,z)))return null;
|
||||
return chunkAt.apply(x,z);
|
||||
}
|
||||
case "hasChunk":return true;
|
||||
case "getBiomeManager":return zoom[0];
|
||||
case "getBiome":return zoom[0].getBiome((BlockPos)values[0]);
|
||||
case "getNoiseBiome":{int qx=(Integer)values[0],qy=(Integer)values[1],qz=(Integer)values[2];return chunkAt.apply(qx>>2,qz>>2).getNoiseBiome(qx,qy,qz);}
|
||||
case "getUncachedNoiseBiome":return uncached.createResolver(climate).getNoiseBiome((Integer)values[0],(Integer)values[1],(Integer)values[2]);
|
||||
case "setCurrentlyGenerating","scheduleTick","blockUpdated","updateNeighborsAt","gameEvent","playSound","levelEvent","addParticle":return null;
|
||||
case "addFreshEntity":return true;
|
||||
case "addFreshEntityWithPassengers":return null;
|
||||
case "getLevel":throw new Ignored();
|
||||
case "isClientSide":return false;
|
||||
case "enabledFeatures":return net.minecraft.world.flag.FeatureFlags.DEFAULT_FLAGS;
|
||||
case "dimensionType":return world.lookupOrThrow(Registries.DIMENSION_TYPE).getOrThrow(switch(dimension){case "the_nether"->net.minecraft.world.level.dimension.BuiltinDimensionTypes.NETHER;case "the_end"->net.minecraft.world.level.dimension.BuiltinDimensionTypes.END;default->net.minecraft.world.level.dimension.BuiltinDimensionTypes.OVERWORLD;}).value();
|
||||
case "hashCode":return System.identityHashCode(proxy);
|
||||
case "equals":return proxy==values[0];
|
||||
case "toString":return "oracle region";
|
||||
}
|
||||
if(method.isDefault())return InvocationHandler.invokeDefault(proxy,method,values);
|
||||
throw new AssertionError(method);
|
||||
});
|
||||
self[0]=randomState.getOrCreateRandomFactory(Identifier.withDefaultNamespace("worldgen_region_random")).at(new ChunkPos(cx,cz).getWorldPosition());
|
||||
zoom[0]=new BiomeManager(level,BiomeManager.obfuscateSeed(seed));
|
||||
var manager=new StructureManager(level,new WorldOptions(seed,true,false),null);
|
||||
// BIOMES for the 5x5 area, then TERRAIN for the 3x3 region.
|
||||
for(int x=cx-2;x<=cx+2;x++)for(int z=cz-2;z<=cz+2;z++){generator.createBiomes(randomState,Blender.empty(),manager,chunkAt.apply(x,z)).join();chunkAt.apply(x,z).setPersistedStatus(ChunkStatus.BIOMES);}
|
||||
for(int x=cx-1;x<=cx+1;x++)for(int z=cz-1;z<=cz+1;z++) {
|
||||
ProtoChunk chunk=chunkAt.apply(x,z);
|
||||
Set<Holder<Biome>> possible=new HashSet<>();
|
||||
for(int dx=-1;dx<=1;dx++)for(int dz=-1;dz<=1;dz++)chunkAt.apply(x+dx,z+dz).collectBiomesInPalette(possible);
|
||||
var region=new BiomeManager(level,BiomeManager.obfuscateSeed(seed));
|
||||
generator.buildTerrain(chunk,Blender.empty(),randomState,manager,region,null,possible).join();
|
||||
Heightmap.primeHeightmaps(chunk,EnumSet.of(Heightmap.Types.MOTION_BLOCKING,Heightmap.Types.MOTION_BLOCKING_NO_LEAVES,Heightmap.Types.OCEAN_FLOOR,Heightmap.Types.WORLD_SURFACE));
|
||||
chunk.setPersistedStatus(ChunkStatus.TERRAIN);
|
||||
}
|
||||
// FEATURES: structure pieces only.
|
||||
ProtoChunk target=chunkAt.apply(cx,cz);
|
||||
SectionPos sectionPos=SectionPos.of(new ChunkPos(cx,cz),level.getMinSectionY());
|
||||
var structuresByStep=new TreeMap<Integer,List<Structure>>();
|
||||
structures.listElements().forEach(h->structuresByStep.computeIfAbsent(h.value().step().ordinal(),k->new ArrayList<>()).add(h.value()));
|
||||
WorldgenRandom random=new WorldgenRandom(new XoroshiroRandomSource(0L));
|
||||
long decorationSeed=random.setDecorationSeed(seed,sectionPos.origin().getX(),sectionPos.origin().getZ());
|
||||
var chunkBox=new BoundingBox(cx<<4,height.getMinY()+1,cz<<4,(cx<<4)+15,height.getMaxY(),(cz<<4)+15);
|
||||
for(int step=0;step<GenerationStep.Decoration.values().length;step++) {
|
||||
int index=0;
|
||||
for(Structure structure:structuresByStep.getOrDefault(step,List.of())) {
|
||||
random.setFeatureSeed(decorationSeed,index,step);
|
||||
for(StructureStart start:manager.startsForStructure(sectionPos.x(),sectionPos.z(),structure)) {
|
||||
try{start.placeInChunk(level,manager,generator,random,chunkBox,new ChunkPos(cx,cz));}
|
||||
catch(Ignored e){w.println("note entity skipped");}
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
w.println("chunk "+cx+" "+cz);
|
||||
for(int y=height.getMinY();y<=height.getMaxY();y++) {
|
||||
StringBuilder line=new StringBuilder("y "+y);
|
||||
for(int z=0;z<16;z++)for(int x=0;x<16;x++)line.append(' ').append(Block.getId(target.getBlockState(new BlockPos((cx<<4)+x,y,(cz<<4)+z))));
|
||||
w.println(line);
|
||||
}
|
||||
var entities=new TreeMap<BlockPos,BlockEntity>(Comparator.<BlockPos>comparingInt(BlockPos::getX).thenComparingInt(BlockPos::getY).thenComparingInt(BlockPos::getZ));
|
||||
entities.putAll(target.getBlockEntities());
|
||||
for(var e:entities.values()) {
|
||||
var p=e.getBlockPos();String table="-",lootSeed="-";
|
||||
if(e instanceof RandomizableContainer rc&&rc.getLootTable()!=null){table=rc.getLootTable().identifier().toString();lootSeed=Long.toString(rc.getLootTableSeed());}
|
||||
w.println("be "+p.getX()+" "+p.getY()+" "+p.getZ()+" "+BuiltInRegistries.BLOCK_ENTITY_TYPE.getKey(e.getType())+" "+table+" "+lootSeed);
|
||||
}
|
||||
}
|
||||
}
|
||||
storage.close();
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compare structure placement (terrain + structure pieces, no placed features)
|
||||
between vanilla and Rust for target chunks.
|
||||
|
||||
usage: compare_structure_blocks.py RESOURCES SEED DIMENSION CX,CZ...
|
||||
Runs VanillaStructurePlacementOracle and the structure_blocks example and
|
||||
reports, per chunk, the blocks and block entities that differ.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
try:
|
||||
from . import lab, test_rust_parity as base
|
||||
except ImportError:
|
||||
import lab
|
||||
import test_rust_parity as base
|
||||
|
||||
|
||||
def build_dir():
|
||||
return Path(os.environ['HORIZON_ORACLE_BUILD']) if os.environ.get('HORIZON_ORACLE_BUILD') else lab.LOCAL/'structure-placement-oracle'
|
||||
|
||||
|
||||
def run_oracle(out, args):
|
||||
lock, jars, libraries = base.verify_artifacts()
|
||||
source = lab.ROOT/'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaStructurePlacementOracle.java'
|
||||
classes = build_dir()/'classes'
|
||||
classes.mkdir(parents=True, exist_ok=True)
|
||||
dependencies = os.pathsep.join(map(str, [jars['server'], *libraries]))
|
||||
compiled = subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-cp', dependencies,
|
||||
'-d', str(classes), str(source)], capture_output=True, text=True)
|
||||
if compiled.returncode:
|
||||
raise SystemExit('javac failed:\n' + compiled.stderr[-3000:])
|
||||
result = subprocess.run(['java', '-Xmx4G', '-cp', str(classes)+os.pathsep+dependencies,
|
||||
'dev.horizonlab.parity.VanillaStructurePlacementOracle', str(out), *args],
|
||||
capture_output=True, text=True)
|
||||
if result.returncode:
|
||||
lines = [line for line in (result.stdout + result.stderr).splitlines() if 'Exception' in line or 'Error' in line or '\tat ' in line]
|
||||
raise SystemExit('oracle failed: ' + ' | '.join(lines[:8]))
|
||||
|
||||
|
||||
def parse(lines):
|
||||
chunks, current = {}, None
|
||||
for line in lines:
|
||||
t = line.split()
|
||||
if not t:
|
||||
continue
|
||||
if t[0] == 'chunk':
|
||||
current = (int(t[1]), int(t[2]))
|
||||
chunks[current] = {'rows': {}, 'be': []}
|
||||
elif t[0] == 'y' and current:
|
||||
chunks[current]['rows'][int(t[1])] = t[2:]
|
||||
elif t[0] == 'be' and current:
|
||||
chunks[current]['be'].append(' '.join(t[1:]))
|
||||
return chunks
|
||||
|
||||
|
||||
def state_names(resources):
|
||||
names = {}
|
||||
report = lab.LOCAL/'datagen/out/reports/blocks.json'
|
||||
if report.exists():
|
||||
for block, info in json.loads(report.read_text()).items():
|
||||
for state in info['states']:
|
||||
props = ','.join(f'{k}={v}' for k, v in sorted(state.get('properties', {}).items()))
|
||||
names[str(state['id'])] = block + (f'[{props}]' if props else '')
|
||||
return names
|
||||
|
||||
|
||||
def main():
|
||||
resources, seed, dimension, targets = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4:]
|
||||
out = build_dir()/'vanilla.txt'
|
||||
out.parent.mkdir(parents=True, exist_ok=True)
|
||||
run_oracle(out, [seed, dimension, *targets])
|
||||
vanilla = parse(out.read_text().splitlines())
|
||||
subprocess.run(['cargo', 'build', '--profile', 'fast', '-q', '-p', 'horizon-world', '--example', 'structure_blocks'],
|
||||
check=True, cwd=lab.ROOT)
|
||||
rust_out = subprocess.run([str(lab.ROOT/'target/fast/examples/structure_blocks'), resources, seed, dimension, *targets],
|
||||
check=True, capture_output=True, text=True).stdout
|
||||
rust = parse(rust_out.splitlines())
|
||||
names = state_names(resources)
|
||||
failed = 0
|
||||
for key in sorted(vanilla):
|
||||
v, r = vanilla[key], rust.get(key)
|
||||
if r is None:
|
||||
print(key, 'missing from Rust')
|
||||
failed += 1
|
||||
continue
|
||||
diffs = []
|
||||
for y, row in v['rows'].items():
|
||||
other = r['rows'].get(y, [])
|
||||
for i, (a, b) in enumerate(zip(row, other)):
|
||||
if a != b:
|
||||
diffs.append((key[0] * 16 + i % 16, y, key[1] * 16 + i // 16, a, b))
|
||||
total = sum(len(row) for row in v['rows'].values())
|
||||
be_v, be_r = set(v['be']), set(r['be'])
|
||||
status = 'match' if not diffs and be_v == be_r else 'DIFF'
|
||||
print(f'chunk {key}: {status}; {total - len(diffs)} of {total} blocks equal; block entities {len(be_v & be_r)} of {len(be_v)} equal')
|
||||
for x, y, z, a, b in diffs[:12]:
|
||||
print(f' {x} {y} {z} vanilla {names.get(a, a)} rust {names.get(b, b)}')
|
||||
for e in sorted(be_v - be_r)[:6]:
|
||||
print(' only vanilla be', e)
|
||||
for e in sorted(be_r - be_v)[:6]:
|
||||
print(' only rust be', e)
|
||||
failed += status != 'match'
|
||||
return 1 if failed else 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user