Add tree, fallen-tree and ore differential parity harness
Decodes every vanilla 26.3 tree, fallen_tree, ore and scattered_ore configured feature from the authenticated JAR, places each with both random kinds over layered synthetic worlds backed by native proto-chunk sections, and compares ordered writes, beehive occupants, post-processing marks, results, random tails and the final block diff with Rust. First run: 2783 of 3018 fixtures identical; every ore fixture matches. The remaining differences are StructureTemplate.updateShapeAtEdge neighbor shape updates (faceless vines and unsupported plants beside the tree), which the Rust world hook does not yet implement. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,337 @@
|
||||
//! Vanilla tree, fallen-tree and ore features over a layered synthetic world.
|
||||
use horizon_core::{
|
||||
geometry::{Direction, VoxelShape},
|
||||
random::{LegacyRandom, RandomSource, XoroshiroRandom},
|
||||
};
|
||||
use horizon_world::{
|
||||
block_placement::BlockPlacementBindings,
|
||||
block_properties::BlockPropertiesRegistry,
|
||||
block_support::{BlockSupportRegistry, SupportType},
|
||||
collision::CachedCollisionRegistry,
|
||||
dynamic_support::DynamicSupportWorld,
|
||||
fluid::{FluidKind, FluidState, FluidStateTable},
|
||||
heightmap::HeightmapType,
|
||||
nbt::{self, Limits, Tag},
|
||||
registry::{BlockRegistry, BlockStateId},
|
||||
worldgen::{
|
||||
anchor::WorldGenerationContext,
|
||||
block_feature::FeatureBlockBindings,
|
||||
feature::{FeatureBlocks, FeatureWorld},
|
||||
feature_catalog::FeatureCatalog,
|
||||
placement::{PlacementContext, PlacementQueryError},
|
||||
state_provider::{StateProviderBindings, StateProviderCatalog},
|
||||
tags::TagDefinitions,
|
||||
},
|
||||
};
|
||||
use serde_json::Value;
|
||||
use std::{
|
||||
collections::{BTreeMap, BTreeSet, HashMap},
|
||||
fs,
|
||||
io::{self, BufRead, Write},
|
||||
path::Path,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
const MIN_Y: i32 = -64;
|
||||
const HEIGHT: i32 = 384;
|
||||
|
||||
struct Terrain {
|
||||
layers: Vec<[i32; 2]>,
|
||||
hole: [i32; 2],
|
||||
obstacle: [i32; 3],
|
||||
overrides: HashMap<[i32; 3], u32>,
|
||||
air: u32,
|
||||
}
|
||||
impl Terrain {
|
||||
fn new(f: &Value, air: u32) -> Self {
|
||||
let n = |key: &str| f[key].as_i64().unwrap() as i32;
|
||||
Self {
|
||||
layers: f["layers"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|l| [l[0].as_i64().unwrap() as i32, l[1].as_i64().unwrap() as i32])
|
||||
.collect(),
|
||||
hole: [n("hole_mod"), n("hole_state")],
|
||||
obstacle: [n("obstacle_mod"), n("obstacle_state"), n("obstacle_min")],
|
||||
overrides: f["states"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|v| {
|
||||
(
|
||||
std::array::from_fn(|i| v[i].as_i64().unwrap() as i32),
|
||||
v[3].as_u64().unwrap() as u32,
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
air,
|
||||
}
|
||||
}
|
||||
fn hash(x: i32, y: i32, z: i32) -> i32 {
|
||||
let mut h = x.wrapping_mul(73856093) ^ y.wrapping_mul(19349663) ^ z.wrapping_mul(83492791);
|
||||
h ^= ((h as u32) >> 13) as i32;
|
||||
h = h.wrapping_mul(0x5bd1e995);
|
||||
h ^= ((h as u32) >> 15) as i32;
|
||||
h & 0x7fff_ffff
|
||||
}
|
||||
fn at(&self, [x, y, z]: [i32; 3]) -> BlockStateId {
|
||||
if let Some(state) = self.overrides.get(&[x, y, z]) {
|
||||
return BlockStateId(*state);
|
||||
}
|
||||
let h = Self::hash(x, y, z);
|
||||
if let Some(layer) = self.layers.iter().find(|l| y <= l[0]) {
|
||||
if self.hole[0] > 0 && h % self.hole[0] == 0 {
|
||||
return BlockStateId(self.hole[1] as u32);
|
||||
}
|
||||
return BlockStateId(layer[1] as u32);
|
||||
}
|
||||
if self.obstacle[0] > 0 && y >= self.obstacle[2] && h % self.obstacle[0] == 0 {
|
||||
return BlockStateId(self.obstacle[1] as u32);
|
||||
}
|
||||
BlockStateId(self.air)
|
||||
}
|
||||
}
|
||||
|
||||
struct World<'a, 'r, W> {
|
||||
out: &'a mut W,
|
||||
terrain: Terrain,
|
||||
overlay: BTreeMap<[i32; 3], BlockStateId>,
|
||||
void_air: BlockStateId,
|
||||
height: i32,
|
||||
fluids: &'a FluidStateTable,
|
||||
tags: &'a BTreeMap<String, BTreeSet<BlockStateId>>,
|
||||
support: &'a BlockSupportRegistry<'r>,
|
||||
}
|
||||
impl<W: Write> World<'_, '_, W> {
|
||||
fn get(&self, p: [i32; 3]) -> BlockStateId {
|
||||
if p[1] < MIN_Y || p[1] >= MIN_Y + HEIGHT {
|
||||
return self.void_air;
|
||||
}
|
||||
self.overlay
|
||||
.get(&p)
|
||||
.copied()
|
||||
.unwrap_or_else(|| self.terrain.at(p))
|
||||
}
|
||||
}
|
||||
fn query(error: impl std::fmt::Display) -> PlacementQueryError {
|
||||
PlacementQueryError::Failed(error.to_string())
|
||||
}
|
||||
impl<W: Write> FeatureWorld for World<'_, '_, W> {
|
||||
type Error = io::Error;
|
||||
fn block(&mut self, p: [i32; 3]) -> io::Result<BlockStateId> {
|
||||
Ok(self.get(p))
|
||||
}
|
||||
fn set_block(&mut self, p: [i32; 3], state: BlockStateId, flags: i32) -> io::Result<bool> {
|
||||
writeln!(self.out, "set {} {} {} {} {flags}", p[0], p[1], p[2], state.0)?;
|
||||
if p[1] < MIN_Y || p[1] >= MIN_Y + HEIGHT {
|
||||
return Ok(false);
|
||||
}
|
||||
self.overlay.insert(p, state);
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
impl<W: Write> DynamicSupportWorld for World<'_, '_, W> {
|
||||
type Error = io::Error;
|
||||
fn shulker_is_closed(&mut self, _: [i32; 3]) -> io::Result<Option<bool>> {
|
||||
Ok(None)
|
||||
}
|
||||
fn moving_piston_shape(&mut self, _: [i32; 3]) -> io::Result<Option<VoxelShape>> {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
impl<W: Write> PlacementContext for World<'_, '_, W> {
|
||||
fn generation(&self) -> WorldGenerationContext {
|
||||
WorldGenerationContext::new(MIN_Y, HEIGHT, 63)
|
||||
}
|
||||
fn min_y(&mut self) -> Result<i32, PlacementQueryError> {
|
||||
Ok(MIN_Y)
|
||||
}
|
||||
fn max_y(&mut self) -> Result<i32, PlacementQueryError> {
|
||||
Ok(MIN_Y + HEIGHT - 1)
|
||||
}
|
||||
fn height(&mut self, _: HeightmapType, _: i32, _: i32) -> Result<i32, PlacementQueryError> {
|
||||
Ok(self.height)
|
||||
}
|
||||
fn block_state(&mut self, p: [i32; 3]) -> Result<BlockStateId, PlacementQueryError> {
|
||||
Ok(self.get(p))
|
||||
}
|
||||
fn fluid_state(&mut self, p: [i32; 3]) -> Result<FluidState, PlacementQueryError> {
|
||||
self.fluids
|
||||
.get(self.get(p))
|
||||
.ok_or_else(|| query("unknown fluid"))
|
||||
}
|
||||
fn block_matches_tag(&mut self, p: [i32; 3], tag: &str) -> Result<bool, PlacementQueryError> {
|
||||
let state = self.get(p);
|
||||
Ok(self.tags.get(tag).is_some_and(|s| s.contains(&state)))
|
||||
}
|
||||
fn state_face_sturdy(
|
||||
&mut self,
|
||||
state: BlockStateId,
|
||||
p: [i32; 3],
|
||||
direction: Direction,
|
||||
) -> Result<bool, PlacementQueryError> {
|
||||
let support = self.support;
|
||||
support
|
||||
.is_face_sturdy(self, state, p, direction, SupportType::Full)
|
||||
.map_err(query)
|
||||
}
|
||||
fn face_sturdy(&mut self, p: [i32; 3], direction: Direction) -> Result<bool, PlacementQueryError> {
|
||||
let state = self.get(p);
|
||||
self.state_face_sturdy(state, p, direction)
|
||||
}
|
||||
fn mark_postprocessing(&mut self, p: [i32; 3]) -> Result<(), PlacementQueryError> {
|
||||
writeln!(self.out, "post {} {} {}", p[0], p[1], p[2]).map_err(query)
|
||||
}
|
||||
fn set_section_block(&mut self, p: [i32; 3], state: BlockStateId) -> Result<(), PlacementQueryError> {
|
||||
self.overlay.insert(p, state);
|
||||
Ok(())
|
||||
}
|
||||
fn store_beehive_occupants(&mut self, p: [i32; 3], ticks: &[i32]) -> Result<(), PlacementQueryError> {
|
||||
for t in ticks {
|
||||
writeln!(self.out, "bee {} {} {} {t}", p[0], p[1], p[2]).map_err(query)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn fixture<R: RandomSource>(
|
||||
f: &Value,
|
||||
mut random: R,
|
||||
catalog: &FeatureCatalog,
|
||||
features: &BTreeMap<String, String>,
|
||||
bindings: &FeatureBlockBindings<'_>,
|
||||
blocks: FeatureBlocks,
|
||||
world: &mut World<'_, '_, impl Write>,
|
||||
) -> io::Result<()> {
|
||||
let name = f["feature"].as_str().unwrap();
|
||||
writeln!(world.out, "fixture {name}")?;
|
||||
let feature = match catalog.feature_with_blocks(&features[name], bindings) {
|
||||
Ok(feature) => feature,
|
||||
Err(error) => {
|
||||
eprintln!("decode {name}: {error}");
|
||||
return writeln!(world.out, "decode error");
|
||||
}
|
||||
};
|
||||
let origin = std::array::from_fn(|i| f["origin"][i].as_i64().unwrap() as i32);
|
||||
match feature.place(world, &mut random, origin, blocks) {
|
||||
Ok(result) => writeln!(world.out, "place {result}")?,
|
||||
Err(error) => writeln!(world.out, "place error {error}")?,
|
||||
}
|
||||
writeln!(world.out, "next {}", random.next_long())?;
|
||||
let changes: Vec<_> = world
|
||||
.overlay
|
||||
.iter()
|
||||
.filter(|(p, state)| world.terrain.at(**p) != **state)
|
||||
.map(|(p, s)| (*p, *s))
|
||||
.collect();
|
||||
for (p, state) in changes {
|
||||
writeln!(world.out, "final {} {} {} {}", p[0], p[1], p[2], state.0)?;
|
||||
}
|
||||
writeln!(world.out, "end")
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let arg = std::env::args().nth(1).expect("private fixture directory");
|
||||
let dir = Path::new(&arg);
|
||||
let read = |name: &str| -> Result<Tag, Box<dyn std::error::Error>> {
|
||||
Ok(nbt::read_named(&fs::read(dir.join(name))?, Limits::default())?.0.tag)
|
||||
};
|
||||
let registry = BlockRegistry::from_nbt(&read("block-registry.nbt")?)?;
|
||||
let collisions = CachedCollisionRegistry::from_nbt(&read("cached-collision.nbt")?, ®istry)?;
|
||||
let properties = BlockPropertiesRegistry::from_nbt(&read("block-properties.nbt")?, &collisions)?;
|
||||
let support = BlockSupportRegistry::from_nbt(&read("cached-support.nbt")?, ®istry)?;
|
||||
let resources: BTreeMap<String, String> =
|
||||
serde_json::from_str(&fs::read_to_string(dir.join("tags.json"))?)?;
|
||||
let mut tags = TagDefinitions::default();
|
||||
for (name, text) in resources {
|
||||
tags.apply_json(&name, "vanilla", &text)?;
|
||||
}
|
||||
let tags = tags.build(|name, _| registry.default_state(name).map(|_| name.to_owned()))?;
|
||||
let fluid_resources: BTreeMap<String, String> =
|
||||
serde_json::from_str(&fs::read_to_string(dir.join("fluid-tags.json"))?)?;
|
||||
let mut fluid_tags = TagDefinitions::default();
|
||||
for (name, text) in fluid_resources {
|
||||
fluid_tags.apply_json(&name, "vanilla", &text)?;
|
||||
}
|
||||
let fluid_tags =
|
||||
fluid_tags.build(|name, _| FluidKind::from_name(name).map(|_| name.to_owned()))?;
|
||||
let facts: Value = serde_json::from_str(&fs::read_to_string(dir.join("facts.json"))?)?;
|
||||
let integers: BTreeSet<(String, String)> = facts["integers"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|v| (v[0].as_str().unwrap().to_owned(), v[1].as_str().unwrap().to_owned()))
|
||||
.collect();
|
||||
let bundle: Value =
|
||||
serde_json::from_str(&fs::read_to_string(dir.join("vanilla-features.json"))?)?;
|
||||
let providers: BTreeMap<String, String> = serde_json::from_value(bundle["providers"].clone())?;
|
||||
let features: BTreeMap<String, String> = serde_json::from_value(bundle["features"].clone())?;
|
||||
let definitions: Value =
|
||||
serde_json::from_str(&fs::read_to_string(dir.join("placement-definitions.json"))?)?;
|
||||
let bindings = FeatureBlockBindings::with_tags(®istry, integers.clone(), &tags.tags)?
|
||||
.with_block_properties(&properties)?
|
||||
.with_fluid_tags(&fluid_tags.tags)?
|
||||
.with_state_providers(
|
||||
StateProviderBindings::new(®istry, integers, &tags.tags)?
|
||||
.with_block_properties(&properties)?
|
||||
.with_fluid_tags(&fluid_tags.tags)?,
|
||||
StateProviderCatalog::from_resources(&providers)?,
|
||||
BlockPlacementBindings::new(
|
||||
®istry,
|
||||
&properties,
|
||||
&serde_json::from_value(definitions["double_plants"].clone())?,
|
||||
&serde_json::from_value(definitions["mossy_carpets"].clone())?,
|
||||
)?,
|
||||
)?;
|
||||
let catalog = FeatureCatalog::from_resources(&features, &BTreeMap::new(), &TagDefinitions::default())?;
|
||||
let blocks = FeatureBlocks::from_registry(®istry)?;
|
||||
let fluids = FluidStateTable::new(®istry)?;
|
||||
let mut by_name = BTreeMap::<String, Vec<BlockStateId>>::new();
|
||||
for id in 0..registry.len() {
|
||||
let id = BlockStateId(id as u32);
|
||||
by_name
|
||||
.entry(registry.state(id).unwrap().name.clone())
|
||||
.or_default()
|
||||
.push(id);
|
||||
}
|
||||
let tag_states: BTreeMap<String, BTreeSet<BlockStateId>> = tags
|
||||
.tags
|
||||
.iter()
|
||||
.map(|(name, entries)| {
|
||||
(
|
||||
name.clone(),
|
||||
entries
|
||||
.iter()
|
||||
.flat_map(|e| by_name.get(e).into_iter().flatten().copied())
|
||||
.collect(),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let support = Arc::new(support);
|
||||
let air = registry.default_state("minecraft:air").unwrap().0;
|
||||
let void_air = registry.default_state("minecraft:void_air").unwrap();
|
||||
let mut out = io::BufWriter::new(fs::File::create(dir.join("rust.txt"))?);
|
||||
for line in io::BufReader::new(fs::File::open(dir.join("inputs.txt"))?).lines() {
|
||||
let f: Value = serde_json::from_str(&line?)?;
|
||||
let mut world = World {
|
||||
out: &mut out,
|
||||
terrain: Terrain::new(&f, air),
|
||||
overlay: BTreeMap::new(),
|
||||
void_air,
|
||||
height: f["height"].as_i64().unwrap() as i32,
|
||||
fluids: &fluids,
|
||||
tags: &tag_states,
|
||||
support: &support,
|
||||
};
|
||||
let seed = f["seed"].as_i64().unwrap();
|
||||
if f["legacy"].as_bool().unwrap() {
|
||||
fixture(&f, LegacyRandom::new(seed), &catalog, &features, &bindings, blocks, &mut world)?;
|
||||
} else {
|
||||
fixture(&f, XoroshiroRandom::new(seed), &catalog, &features, &bindings, blocks, &mut world)?;
|
||||
}
|
||||
}
|
||||
out.flush()?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package dev.horizonlab.parity;
|
||||
import com.google.gson.*;
|
||||
import com.mojang.serialization.JsonOps;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.*;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import net.minecraft.core.*;
|
||||
import net.minecraft.core.registries.*;
|
||||
import net.minecraft.resources.*;
|
||||
import net.minecraft.util.RandomSource;
|
||||
import net.minecraft.world.level.*;
|
||||
import net.minecraft.world.level.biome.*;
|
||||
import net.minecraft.world.level.block.*;
|
||||
import net.minecraft.world.level.block.entity.*;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.chunk.*;
|
||||
import net.minecraft.world.level.levelgen.*;
|
||||
import net.minecraft.world.level.levelgen.feature.Feature;
|
||||
import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider;
|
||||
|
||||
/**
|
||||
* Actual native tree, fallen-tree and ore features over a layered synthetic
|
||||
* world backed by proto-chunk sections. Logs ordered level writes, beehive
|
||||
* occupants, post-processing marks, results, random tails and the final diff.
|
||||
*/
|
||||
public final class VanillaTreeFeatureOracle {
|
||||
private static final int MIN_Y=-64,HEIGHT=384;
|
||||
private static BlockPos pos(JsonArray a){return new BlockPos(a.get(0).getAsInt(),a.get(1).getAsInt(),a.get(2).getAsInt());}
|
||||
private static String xyz(BlockPos p){return p.getX()+" "+p.getY()+" "+p.getZ();}
|
||||
private static final class Chunks {
|
||||
static final PalettedContainerFactory FACTORY;
|
||||
static {
|
||||
Holder<Biome> biome=Holder.Reference.createStandAlone(new HolderOwner<Biome>(){},Biomes.PLAINS);
|
||||
var ids=new IdMapper<Holder<Biome>>();ids.add(biome);
|
||||
FACTORY=new PalettedContainerFactory(Strategy.createForBlockStates(Block.BLOCK_STATE_REGISTRY),Blocks.AIR.defaultBlockState(),null,Strategy.createForBiomes(ids),biome,null);
|
||||
}
|
||||
}
|
||||
/** Shared with the Rust fixture: layered column, pseudo-random holes and obstacles, overrides. */
|
||||
static final class Terrain {
|
||||
final int[][] layers;final int holeMod,holeState,obstacleMod,obstacleState,obstacleMin;final Map<BlockPos,Integer> overrides=new HashMap<>();
|
||||
Terrain(JsonObject f){
|
||||
var raw=f.getAsJsonArray("layers");layers=new int[raw.size()][];for(int i=0;i<raw.size();i++){var l=raw.get(i).getAsJsonArray();layers[i]=new int[]{l.get(0).getAsInt(),l.get(1).getAsInt()};}
|
||||
holeMod=f.get("hole_mod").getAsInt();holeState=f.get("hole_state").getAsInt();obstacleMod=f.get("obstacle_mod").getAsInt();obstacleState=f.get("obstacle_state").getAsInt();obstacleMin=f.get("obstacle_min").getAsInt();
|
||||
for(var e:f.getAsJsonArray("states")){var v=e.getAsJsonArray();overrides.put(pos(v),v.get(3).getAsInt());}
|
||||
}
|
||||
static int hash(int x,int y,int z){int h=x*73856093^y*19349663^z*83492791;h^=h>>>13;h*=0x5bd1e995;h^=h>>>15;return h&0x7fffffff;}
|
||||
int at(int x,int y,int z){
|
||||
var o=overrides.get(new BlockPos(x,y,z));if(o!=null)return o;
|
||||
int base=-1;for(var l:layers)if(y<=l[0]){base=l[1];break;}
|
||||
int h=hash(x,y,z);
|
||||
if(base>=0){if(holeMod>0&&h%holeMod==0)return holeState;return base;}
|
||||
if(obstacleMod>0&&y>=obstacleMin&&h%obstacleMod==0)return obstacleState;
|
||||
return Block.getId(Blocks.AIR.defaultBlockState());
|
||||
}
|
||||
}
|
||||
private static final class World implements InvocationHandler {
|
||||
final JsonObject f;final PrintWriter out;final Terrain terrain;final Map<ChunkPos,ProtoChunk> chunks=new HashMap<>();final Map<BlockPos,BeehiveBlockEntity> hives=new HashMap<>();
|
||||
final RandomSource shapeRandom=RandomSource.create(0);
|
||||
World(JsonObject f,PrintWriter out){this.f=f;this.out=out;this.terrain=new Terrain(f);}
|
||||
ProtoChunk chunk(int cx,int cz){
|
||||
return chunks.computeIfAbsent(new ChunkPos(cx,cz),cp->{
|
||||
var chunk=new ProtoChunk(cp,UpgradeData.EMPTY,LevelHeightAccessor.create(MIN_Y,HEIGHT),Chunks.FACTORY,null){
|
||||
@Override public void markPosForPostProcessing(BlockPos position){out.println("post "+xyz(position));super.markPosForPostProcessing(position);}
|
||||
};
|
||||
for(int s=0;s<HEIGHT/16;s++){var section=chunk.getSection(s);for(int ly=0;ly<16;ly++)for(int lz=0;lz<16;lz++)for(int lx=0;lx<16;lx++){
|
||||
var state=Block.stateById(terrain.at(cx*16+lx,MIN_Y+s*16+ly,cz*16+lz));if(!state.isAir()||!state.is(Blocks.AIR))section.setBlockState(lx,ly,lz,state,false);}}
|
||||
return chunk;
|
||||
});
|
||||
}
|
||||
BlockState get(BlockPos p){
|
||||
if(p.getY()<MIN_Y||p.getY()>=MIN_Y+HEIGHT)return Blocks.VOID_AIR.defaultBlockState();
|
||||
return chunk(p.getX()>>4,p.getZ()>>4).getBlockState(p);
|
||||
}
|
||||
public Object invoke(Object proxy,Method method,Object[] args)throws Throwable {
|
||||
String name=method.getName();
|
||||
switch(name){
|
||||
case "getBlockState":return get((BlockPos)args[0]);
|
||||
case "getFluidState":return get((BlockPos)args[0]).getFluidState();
|
||||
case "isStateAtPosition":return ((java.util.function.Predicate<BlockState>)args[1]).test(get((BlockPos)args[0]));
|
||||
case "isFluidAtPosition":return ((java.util.function.Predicate<net.minecraft.world.level.material.FluidState>)args[1]).test(get((BlockPos)args[0]).getFluidState());
|
||||
case "getMinY":return MIN_Y;
|
||||
case "getSeaLevel":return 63;
|
||||
case "ensureCanWrite":return true;
|
||||
case "getRandom":return shapeRandom;
|
||||
case "scheduleTick":return null;
|
||||
case "setCurrentlyGenerating":return null;
|
||||
case "getBlockEntity":{
|
||||
if(args.length!=1)break;var p=((BlockPos)args[0]).immutable();var state=get(p);
|
||||
if(!state.is(Blocks.BEE_NEST))return null;
|
||||
return hives.computeIfAbsent(p,key->new BeehiveBlockEntity(key,state){@Override public void storeBee(BeehiveBlockEntity.Occupant occupant){out.println("bee "+xyz(key)+" "+occupant.ticksInHive());}});
|
||||
}
|
||||
case "setBlock":{
|
||||
var p=((BlockPos)args[0]).immutable();var state=(BlockState)args[1];out.println("set "+xyz(p)+" "+Block.getId(state)+" "+args[2]);
|
||||
if(p.getY()<MIN_Y||p.getY()>=MIN_Y+HEIGHT)return false;
|
||||
chunk(p.getX()>>4,p.getZ()>>4).getSection((p.getY()-MIN_Y)>>4).setBlockState(p.getX()&15,p.getY()&15,p.getZ()&15,state,false);return true;
|
||||
}
|
||||
case "getHeight":if(args==null||args.length==0)return HEIGHT;return f.get("height").getAsInt();
|
||||
case "getChunk":
|
||||
if(args.length==1&&args[0] instanceof BlockPos p)return chunk(p.getX()>>4,p.getZ()>>4);
|
||||
if(args.length>=2&&args[0] instanceof Integer x&&args[1] instanceof Integer z)return chunk(x,z);
|
||||
break;
|
||||
default:break;
|
||||
}
|
||||
if(method.isDefault())return InvocationHandler.invokeDefault(proxy,method,args);
|
||||
throw new AssertionError(method);
|
||||
}
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void main(String[] args)throws Exception {
|
||||
net.minecraft.SharedConstants.tryDetectVersion();net.minecraft.server.Bootstrap.bootStrap();Path dir=Path.of(args[1]);
|
||||
VanillaHeightmapOracle.bindTags(JsonParser.parseString(Files.readString(dir.resolve("tags.json"))).getAsJsonObject());
|
||||
var bind=VanillaBlockSurvivalOracle.class.getDeclaredMethod("bindFluidTags",JsonObject.class);bind.setAccessible(true);bind.invoke(null,JsonParser.parseString(Files.readString(dir.resolve("fluid-tags.json"))).getAsJsonObject());
|
||||
var providers=new MappedRegistry<BlockStateProvider>(Registries.BLOCK_STATE_PROVIDER,com.mojang.serialization.Lifecycle.stable());
|
||||
var ops=RegistryOps.create(JsonOps.INSTANCE,new RegistryOps.RegistryInfoLookup(){public <T> Optional<HolderGetter<T>> lookup(ResourceKey<? extends Registry<? extends T>> key){
|
||||
if(key.equals(Registries.BLOCK_STATE_PROVIDER))return (Optional<HolderGetter<T>>)(Optional<?>)Optional.of(providers);
|
||||
if(key.equals(Registries.FLUID))return (Optional<HolderGetter<T>>)(Optional<?>)Optional.of(BuiltInRegistries.FLUID);
|
||||
return key.equals(Registries.BLOCK)?(Optional<HolderGetter<T>>)(Optional<?>)Optional.of(BuiltInRegistries.BLOCK):Optional.empty();
|
||||
}});
|
||||
var bundle=JsonParser.parseString(Files.readString(dir.resolve("vanilla-features.json"))).getAsJsonObject();
|
||||
for(var entry:bundle.getAsJsonObject("providers").entrySet())
|
||||
providers.register(ResourceKey.create(Registries.BLOCK_STATE_PROVIDER,Identifier.parse(entry.getKey())),BlockStateProvider.DIRECT_CODEC.parse(ops,JsonParser.parseString(entry.getValue().getAsString())).getOrThrow(),RegistrationInfo.BUILT_IN);
|
||||
providers.freeze();
|
||||
var features=bundle.getAsJsonObject("features");
|
||||
try(var out=new PrintWriter(Files.newBufferedWriter(dir.resolve(args[0]+".txt")));var inputs=Files.lines(dir.resolve("inputs.txt"))) {
|
||||
for(var line:(Iterable<String>)inputs::iterator){
|
||||
var f=JsonParser.parseString(line).getAsJsonObject();long seed=f.get("seed").getAsLong();
|
||||
RandomSource random=f.get("legacy").getAsBoolean()?new LegacyRandomSource(seed):new XoroshiroRandomSource(seed);
|
||||
out.println("fixture "+f.get("feature").getAsString());
|
||||
Optional<Feature> decoded;
|
||||
try{decoded=Feature.DIRECT_CODEC.parse(ops,JsonParser.parseString(features.get(f.get("feature").getAsString()).getAsString())).result();}catch(RuntimeException error){decoded=Optional.empty();}
|
||||
if(decoded.isEmpty()){out.println("decode error");continue;}
|
||||
var handler=new World(f,out);var world=(WorldGenLevel)Proxy.newProxyInstance(WorldGenLevel.class.getClassLoader(),new Class<?>[]{WorldGenLevel.class},handler);
|
||||
try{out.println("place "+decoded.get().place(world,null,random,pos(f.getAsJsonArray("origin"))));}catch(RuntimeException error){out.println("place error "+error);}
|
||||
out.println("next "+random.nextLong());
|
||||
var keys=new ArrayList<>(handler.chunks.keySet());keys.sort(Comparator.<ChunkPos>comparingInt(ChunkPos::x).thenComparingInt(ChunkPos::z));
|
||||
var changes=new TreeMap<BlockPos,Integer>(Comparator.<BlockPos>comparingInt(BlockPos::getX).thenComparingInt(BlockPos::getY).thenComparingInt(BlockPos::getZ));
|
||||
for(var key:keys){var chunk=handler.chunks.get(key);
|
||||
for(int y=MIN_Y;y<MIN_Y+HEIGHT;y++)for(int lz=0;lz<16;lz++)for(int lx=0;lx<16;lx++){
|
||||
int x=key.x()*16+lx,z=key.z()*16+lz;int id=Block.getId(chunk.getSection((y-MIN_Y)>>4).getBlockState(lx,y&15,lz));
|
||||
if(id!=handler.terrain.at(x,y,z))changes.put(new BlockPos(x,y,z),id);
|
||||
}}
|
||||
for(var e:changes.entrySet())out.println("final "+xyz(e.getKey())+" "+e.getValue());
|
||||
out.println("end");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compare vanilla tree, fallen-tree and ore features block-for-block with vanilla."""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import subprocess
|
||||
import zipfile
|
||||
try:
|
||||
from . import lab, test_rust_parity as base, test_worldgen_parity as worldgen, test_block_survival_parity as survival
|
||||
except ImportError:
|
||||
import lab
|
||||
import test_rust_parity as base
|
||||
import test_worldgen_parity as worldgen
|
||||
import test_block_survival_parity as survival
|
||||
|
||||
|
||||
def feature_resources(jar):
|
||||
"""Vanilla configured features and named state providers from the authenticated JAR."""
|
||||
result={'features':{},'providers':{}}
|
||||
with zipfile.ZipFile(jar) as archive:
|
||||
for kind,key in [('feature','features'),('block_state_provider','providers')]:
|
||||
prefix=f'data/minecraft/worldgen/{kind}/'
|
||||
for name in sorted(archive.namelist()):
|
||||
if name.startswith(prefix) and name.endswith('.json'):
|
||||
result[key]['minecraft:'+name[len(prefix):-5]]=archive.read(name).decode('utf-8')
|
||||
return result
|
||||
|
||||
|
||||
def fixture_hashes(paths):
|
||||
# Build outputs may live behind a linked private directory outside ROOT.
|
||||
return {path.name:lab.digest(path,'sha256') for path in paths}
|
||||
|
||||
|
||||
def corpus(facts,resources):
|
||||
get=lambda name:facts['defaults']['minecraft:'+name]
|
||||
def fixture(feature,**extra):
|
||||
f={'feature':feature,'seed':0,'legacy':False,'origin':[8,65,8],'height':65,'layers':[[59,get('stone')],[63,get('dirt')],[64,get('grass_block')]],
|
||||
'hole_mod':0,'hole_state':get('air'),'obstacle_mod':0,'obstacle_state':get('air'),'obstacle_min':0,'states':[]}
|
||||
f.update(extra);return f
|
||||
seeds=[0,1,-1,0x5DEECE66D,0x123456789abcdef,-8070450532247928832]
|
||||
origins=[[8,65,8],[15,65,0],[0,65,15],[-1,65,-17],[31,65,-16],[100003,65,-4097]]
|
||||
trees,ores=[],[]
|
||||
for name,text in resources['features'].items():
|
||||
value=json.loads(text);kind=value['type'].removeprefix('minecraft:')
|
||||
if kind in ('tree','fallen_tree'):
|
||||
if 'pale_moss' in text:continue # needs the level's feature registry and server level
|
||||
trees.append(name)
|
||||
elif kind in ('ore','scattered_ore'):ores.append(name)
|
||||
worlds=[
|
||||
{},
|
||||
{'obstacle_mod':37,'obstacle_state':get('oak_leaves'),'obstacle_min':66},
|
||||
{'obstacle_mod':23,'obstacle_state':get('stone'),'obstacle_min':65},
|
||||
{'obstacle_mod':19,'obstacle_state':get('vine'),'obstacle_min':65},
|
||||
{'layers':[[59,get('stone')],[64,get('mud')],[67,get('water')]],'height':68},
|
||||
{'layers':[[300,get('stone')],[310,get('dirt')],[311,get('grass_block')]],'origin_y':312,'height':312},
|
||||
{'layers':[[59,get('stone')],[63,get('dirt')],[64,get('podzol')]],'hole_mod':11,'hole_state':get('short_grass')},
|
||||
]
|
||||
for name in trees:
|
||||
for w,world in enumerate(worlds):
|
||||
for s,seed in enumerate(seeds):
|
||||
extra=dict(world);origin=list(origins[(s+w)%len(origins)]);origin[1]=extra.pop('origin_y',extra.get('height',65))
|
||||
yield fixture(name,seed=seed,legacy=s%3==2,origin=origin,**extra)
|
||||
ore_worlds=[
|
||||
{'layers':[[-1,get('deepslate')],[250,get('stone')]],'height':251},
|
||||
{'layers':[[-1,get('deepslate')],[250,get('stone')]],'height':251,'hole_mod':5,'hole_state':get('air')},
|
||||
{'layers':[[-1,get('deepslate')],[250,get('stone')]],'height':-100},
|
||||
{'layers':[[250,get('netherrack')]],'height':251,'hole_mod':9,'hole_state':get('cave_air')},
|
||||
{'layers':[[30,get('tuff')],[250,get('granite')]],'height':251,'hole_mod':13,'hole_state':get('water')},
|
||||
]
|
||||
for name in ores:
|
||||
for w,world in enumerate(ore_worlds):
|
||||
for s,seed in enumerate(seeds):
|
||||
origin=list(origins[(s+w)%len(origins)]);origin[1]=[-60,-3,0,40,316,-64][(s*5+w)%6]
|
||||
yield fixture(name,seed=seed,legacy=s%3==2,origin=origin,**world)
|
||||
|
||||
|
||||
def main():
|
||||
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('--prepare',action='store_true');args=parser.parse_args()
|
||||
build=lab.LOCAL/'rust-tree-feature-parity';build.mkdir(parents=True,exist_ok=True);report_path=build/'report.json';report_path.unlink(missing_ok=True)
|
||||
if args.prepare:lab.prepare()
|
||||
lock,jars,libraries=base.verify_artifacts();parity=lab.ROOT/'mods/horizon/src/test/java/dev/horizonlab/parity'
|
||||
oracle=parity/'VanillaTreeFeatureOracle.java';exporter=parity/'VanillaSimpleBlockOracle.java'
|
||||
helpers=[parity/(name+'.java') for name in ['VanillaPlacementOracle','VanillaBlockSurvivalOracle','VanillaBlockPropertiesOracle','VanillaBlockSupportOracle','VanillaBlockStateCodecOracle','VanillaCollisionOracle','VanillaHeightmapOracle']]
|
||||
sources=[oracle,exporter,*helpers,Path(__file__),*[lab.ROOT/'tools'/name for name in ['test_block_survival_parity.py','test_worldgen_parity.py','test_rust_parity.py','lab.py']],lab.ROOT/'Cargo.toml',lab.ROOT/'Cargo.lock',
|
||||
*sorted((lab.ROOT/'crates/horizon-core/src').rglob('*.rs')),*sorted((lab.ROOT/'crates/horizon-world/src').rglob('*.rs')),lab.ROOT/'crates/horizon-world/examples/tree_feature_fixture.rs']
|
||||
tested_sources=base.source_hashes(sources);classes=build/'classes';classes.mkdir(exist_ok=True)
|
||||
def run(command,name):
|
||||
with (build/(name+'.log')).open('w',encoding='utf-8') as log:subprocess.run(list(map(str,command)),cwd=lab.ROOT,check=True,timeout=7200,stdout=log,stderr=subprocess.STDOUT)
|
||||
dependency_path=os.pathsep.join(map(str,libraries))
|
||||
def java(main_class,side,mode,path):run(['java','-Xmx2G','-cp',os.pathsep.join([str(classes),str(jars[side]),dependency_path]),'dev.horizonlab.parity.'+main_class,mode,path],side+'-'+mode)
|
||||
tags=worldgen.tag_resources(jars['server']);fluid_tags=survival.fluid_tag_resources(jars['server']);resources=feature_resources(jars['server'])
|
||||
if tags!=worldgen.tag_resources(jars['client']) or fluid_tags!=survival.fluid_tag_resources(jars['client']) or resources!=feature_resources(jars['client']):raise ValueError('reference resources differ')
|
||||
for name,value in [('tags.json',tags),('fluid-tags.json',fluid_tags),('vanilla-features.json',resources)]:(build/name).write_text(json.dumps(value,separators=(',',':')))
|
||||
run(['javac','--release',lock['minecraft']['java_major'],'-proc:none','-cp',str(jars['server'])+os.pathsep+dependency_path,'-d',classes,oracle,exporter,*helpers],'compile-java')
|
||||
for side in jars:
|
||||
export=build/(side+'-definitions');export.mkdir(exist_ok=True);java('VanillaSimpleBlockOracle',side,'export',export)
|
||||
for path in (build/'server-definitions').iterdir():
|
||||
if path.read_bytes()!=(build/'client-definitions'/path.name).read_bytes():raise ValueError('reference definitions differ: '+path.name)
|
||||
(build/path.name).write_bytes(path.read_bytes())
|
||||
facts=json.loads((build/'facts.json').read_text())
|
||||
inputs=build/'inputs.txt';count=0
|
||||
with inputs.open('w',encoding='utf-8') as output:
|
||||
for fixture in corpus(facts,resources):output.write(json.dumps(fixture,separators=(',',':'))+'\n');count+=1
|
||||
fixtures=[inputs,*sorted(build.glob('*.nbt')),*[p for p in sorted(build.glob('*.json')) if p.name!='report.json']];tested_fixtures=fixture_hashes(fixtures)
|
||||
print(f'Prepared {count:,} feature fixtures',flush=True)
|
||||
run(['cargo','build','--release','-p','horizon-world','--example','tree_feature_fixture'],'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'/('tree_feature_fixture.exe' if os.name=='nt' else 'tree_feature_fixture')
|
||||
run([binary,build],'rust');references=[]
|
||||
for side,jar in jars.items():
|
||||
java('VanillaTreeFeatureOracle',side,side,build);matched=base.compare(build/(side+'.txt'),build/'rust.txt');print(f'{side}: {matched:,} tree/ore output lines match Rust',flush=True)
|
||||
references.append({'side':side,'jar_sha256':lab.digest(jar,'sha256'),'output_sha256':lab.digest(build/(side+'.txt'),'sha256'),'matched_output_lines':matched})
|
||||
base.ensure_sources_unchanged(sources,tested_sources);
|
||||
if fixture_hashes(fixtures)!=tested_fixtures:raise ValueError('fixtures changed during the run')
|
||||
report={'schema':1,'minecraft':lock['minecraft']['version'],'manifest_sha1':lock['minecraft']['manifest_sha1'],'platform':platform.platform(),'java':subprocess.check_output(['java','--version'],text=True).splitlines()[0],
|
||||
'rustc':subprocess.check_output(['rustc','--version'],text=True).strip(),'fixtures':count,'sources_sha256':tested_sources,'fixtures_sha256':tested_fixtures,'rust_output_sha256':lab.digest(build/'rust.txt','sha256'),'references':references,
|
||||
'scope':'Every vanilla 26.3 minecraft:tree (except pale-moss decorated), minecraft:fallen_tree, minecraft:ore and minecraft:scattered_ore configured feature, decoded from the authenticated JAR data, placed with both random kinds over layered synthetic worlds (open ground, leaf/stone/vine obstacles, flooded mud, near the build limit, podzol with grass) backed by native proto-chunk sections. Ordered level writes and flags, beehive occupants, post-processing marks, results, random tails and the final block diff of every touched chunk are compared, including native leaf distances and edge shape updates. Pale-moss ground patches, real WorldGenRegion write policy, heightmaps and decoration scheduling remain covered elsewhere or pending.'}
|
||||
lab.write_json(report_path,report);print(f'Scoped tree/ore feature parity passed: {report_path}',flush=True)
|
||||
|
||||
|
||||
if __name__=='__main__':main()
|
||||
Reference in New Issue
Block a user