From 94539db425e436d1f799e59d0912cc688bad9dda Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 8 Oct 2026 15:25:41 +0200 Subject: [PATCH 1/3] Light with vanilla shape occlusion and incremental relighting Port LightEngine.shapeOccludes / getFaceOcclusionShape (occlusion.nbt) so slabs, stairs, snow layers, paths etc. block light directionally, and use ChunkSkyLightSources.isEdgeOccluded for sky sources. Block light now rises into the section above the highest occupied one. Block edits on lit chunks are relit in place with vanilla's decrease and increase passes instead of recomputing the 3x3 chunks (and remeshing every section of them); edits that race a chunk light job are replayed onto it. examples/light_parity compares Horizon light with the SkyLight/BlockLight arrays of worlds saved by the vanilla 26.3 server (0 differences on villages, outposts and a slab/stair/snow scene; 181 of 113M sky values differ where vanilla's worldgen light is stale) and checks incremental relighting against full relights. tools/vanilla_world_check.py runs the vanilla server headless; tools/anvil_dump.py prints NBT and region chunks. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/examples/light_parity.rs | 326 ++++++++++++ crates/horizon-client/src/game.rs | 2 +- crates/horizon-client/src/light.rs | 492 ++++++++++++++++-- crates/horizon-client/src/streaming.rs | 78 +++ crates/horizon-client/src/world.rs | 14 +- tools/anvil_dump.py | 146 ++++++ tools/vanilla_world_check.py | 98 ++++ 7 files changed, 1111 insertions(+), 45 deletions(-) create mode 100644 crates/horizon-client/examples/light_parity.rs create mode 100644 tools/anvil_dump.py create mode 100644 tools/vanilla_world_check.py diff --git a/crates/horizon-client/examples/light_parity.rs b/crates/horizon-client/examples/light_parity.rs new file mode 100644 index 000000000..b7f6a0eae --- /dev/null +++ b/crates/horizon-client/examples/light_parity.rs @@ -0,0 +1,326 @@ +//! Differential light test: relight chunks of a world saved by the vanilla +//! server with Horizon's light engine and compare every SkyLight/BlockLight +//! value with the arrays vanilla stored. +//! +//! cargo run --profile fast -p horizon-client --example light_parity -- \ +//! RESOURCES DIMENSION_DIR [--nether] [--chunks X0 Z0 X1 Z1] +use horizon_client::{ + assets::Assets, + biome::Biomes, + blocks::BlockFacts, + chunk::{Chunk, MIN_Y, SECTIONS, index}, + light::{LightTables, light_chunk}, + save, +}; +use horizon_core::position::ChunkPos; +use horizon_world::{ + nbt::{Limits, Tag}, + region::RegionFile, +}; +use std::{collections::HashMap, path::PathBuf}; + +type Arrays = HashMap>, Option>)>; + +fn main() -> horizon_client::Result<()> { + let args: Vec = std::env::args().skip(1).collect(); + let root = PathBuf::from( + args.first() + .ok_or("usage: light_parity RESOURCES DIMENSION_DIR")?, + ); + let dim = PathBuf::from(args.get(1).ok_or("missing dimension directory")?); + let sky_light = !args.iter().any(|a| a == "--nether"); + let assets = Assets::load(&root)?; + let facts = BlockFacts::load(&root, assets.registry)?; + let biomes = Biomes::load(&root)?; + let tables = if args.iter().any(|a| a == "--no-shapes") { + LightTables::new(&facts) + } else { + LightTables::load(&root, assets.registry, &facts)? + }; + let mut chunks: HashMap<(i32, i32), Chunk> = HashMap::new(); + let mut arrays: HashMap<(i32, i32), Arrays> = HashMap::new(); + for entry in std::fs::read_dir(dim.join("region"))? { + let path = entry?.path(); + let mut region = RegionFile::open(&path)?; + let positions: Vec = region.chunks().map(|(p, _)| p).collect(); + for p in positions { + let Some(record) = region.read(p)? else { + continue; + }; + let root_tag = record.nbt(Limits::default())?; + let tag = &root_tag.tag; + if !matches!(tag.get("Status"), Some(Tag::String(s)) if s.to_string_lossy() == "minecraft:full") + { + continue; + } + if tag.get("isLightOn") != Some(&Tag::Byte(1)) { + continue; + } + let Some(chunk) = save::decode(&root_tag, assets.registry, &biomes) else { + continue; + }; + let mut light = Arrays::new(); + if let Some(Tag::List(sections)) = tag.get("sections") { + for s in sections { + let Some(Tag::Byte(y)) = s.get("Y") else { + continue; + }; + let get = |k: &str| match s.get(k) { + Some(Tag::ByteArray(a)) => Some(a.clone()), + _ => None, + }; + light.insert(i32::from(*y), (get("SkyLight"), get("BlockLight"))); + } + } + arrays.insert((chunk.x, chunk.z), light); + chunks.insert((chunk.x, chunk.z), chunk); + } + } + let bounds: Option<[i32; 4]> = args.iter().position(|a| a == "--chunks").map(|i| { + std::array::from_fn(|k| { + args.get(i + 1 + k) + .and_then(|v| v.parse().ok()) + .unwrap_or(0) + }) + }); + if let Some(i) = args.iter().position(|a| a == "--column") { + let wx: i32 = args[i + 1].parse()?; + let wz: i32 = args[i + 2].parse()?; + let (y0, y1): (i32, i32) = (args[i + 3].parse()?, args[i + 4].parse()?); + let (x, z) = (wx >> 4, wz >> 4); + let region: [[Option<&Chunk>; 3]; 3] = std::array::from_fn(|dz| { + std::array::from_fn(|dx| chunks.get(&(x + dx as i32 - 1, z + dz as i32 - 1))) + }); + let lit = light_chunk(®ion, &tables, sky_light); + for wy in (y0..=y1).rev() { + let si = ((wy - MIN_Y) >> 4) as usize; + let i = index(wx, wy, wz); + let (sky, block) = arrays[&(x, z)] + .get(&((wy) >> 4)) + .cloned() + .unwrap_or((None, None)); + let nib = |a: &Option>| a.as_ref().map(|a| (a[i >> 1] >> ((i & 1) * 4)) & 15); + let id = chunks[&(x, z)].get(wx & 15, wy, wz & 15); + let packed = lit[si].light(i); + println!( + "{wy}: {} horizon sky {} block {} vanilla sky {:?} block {:?}", + facts.states[id as usize].name, + packed >> 4, + packed & 15, + nib(&sky), + nib(&block) + ); + } + return Ok(()); + } + if let Some(i) = args.iter().position(|a| a == "--incremental") { + let edits: usize = args.get(i + 1).and_then(|v| v.parse().ok()).unwrap_or(200); + incremental(&chunks, &tables, &facts, assets.registry, sky_light, edits); + return Ok(()); + } + let mut keys: Vec<(i32, i32)> = chunks + .keys() + .copied() + .filter(|&(x, z)| { + (-1..=1).all(|dz| (-1..=1).all(|dx| chunks.contains_key(&(x + dx, z + dz)))) + }) + .filter(|&(x, z)| bounds.is_none_or(|b| x >= b[0] && z >= b[1] && x <= b[2] && z <= b[3])) + .collect(); + keys.sort_unstable(); + let (mut total, mut sky_wrong, mut block_wrong) = (0u64, 0u64, 0u64); + let mut shown = 0; + for (x, z) in keys { + let region: [[Option<&Chunk>; 3]; 3] = std::array::from_fn(|dz| { + std::array::from_fn(|dx| chunks.get(&(x + dx as i32 - 1, z + dz as i32 - 1))) + }); + let lit = light_chunk(®ion, &tables, sky_light); + let vanilla = &arrays[&(x, z)]; + let center = &chunks[&(x, z)]; + let (mut s_bad, mut b_bad) = (0, 0); + for (si, section) in lit.iter().enumerate().take(SECTIONS) { + let sy = (MIN_Y >> 4) + si as i32; + let (sky, block) = vanilla.get(&sy).cloned().unwrap_or((None, None)); + for i in 0..4096 { + let nibble = |a: &Option>, default: u8| { + a.as_ref() + .map_or(default, |a| (a[i >> 1] >> ((i & 1) * 4)) & 15) + }; + // Vanilla omits never-written (all-zero) layers. Air sections + // without a layer read the bottom row of the next stored layer + // above (SkyLightSectionStorage.getLightValue), or full + // skylight above every stored layer. + let vs = match &sky { + Some(_) => nibble(&sky, 0), + None if !sky_light || !center.sections[si].is_uniform(0) => 0, + None => ((sy + 1)..=(MIN_Y >> 4) + SECTIONS as i32) + .find_map(|y| vanilla.get(&y).and_then(|(s, _)| s.as_ref())) + .map_or(15, |a| (a[(i & 255) >> 1] >> ((i & 1) * 4)) & 15), + }; + let vb = nibble(&block, 0); + let packed = section.light(i); + let (hs, hb) = (packed >> 4, packed & 15); + total += 1; + if hs != vs { + s_bad += 1; + } + if hb != vb { + b_bad += 1; + } + if (hs != vs || hb != vb) && shown < 30 { + shown += 1; + let (lx, lz, ly) = (i & 15, (i >> 4) & 15, i >> 8); + let wy = sy * 16 + ly as i32; + let wx = x * 16 + lx as i32; + let wz = z * 16 + lz as i32; + let id = center.get(lx as i32, wy, lz as i32); + eprintln!( + " {wx} {wy} {wz} {}: sky {hs} vs {vs}, block {hb} vs {vb}", + facts.states[id as usize].name + ); + } + let _ = index; + } + } + if s_bad + b_bad > 0 { + println!("chunk {x} {z}: {s_bad} sky and {b_bad} block values differ"); + } + sky_wrong += s_bad; + block_wrong += b_bad; + } + println!( + "{total} values compared: {sky_wrong} sky and {block_wrong} block light values differ" + ); + if sky_wrong + block_wrong > 0 { + std::process::exit(1); + } + Ok(()) +} + +/// Edit lit chunks block by block with `light::relight` and check the result +/// against a full relight of the edited neighbourhood after every edit. +fn incremental( + chunks: &HashMap<(i32, i32), Chunk>, + tables: &LightTables, + facts: &BlockFacts, + registry: &horizon_world::registry::BlockRegistry, + sky_light: bool, + edits: usize, +) { + use horizon_client::light::relight; + use std::sync::Arc; + let mut world: HashMap<(i32, i32), Arc> = HashMap::new(); + let inner: Vec<(i32, i32)> = { + let mut k: Vec<_> = chunks + .keys() + .copied() + .filter(|&(x, z)| { + (-3..=3).all(|dz| (-3..=3).all(|dx| chunks.contains_key(&(x + dx, z + dz)))) + }) + .collect(); + k.sort_unstable(); + k + }; + for &(x, z) in chunks.keys() { + if !(-1..=1).all(|dz| (-1..=1).all(|dx| chunks.contains_key(&(x + dx, z + dz)))) { + continue; + } + let region: [[Option<&Chunk>; 3]; 3] = std::array::from_fn(|dz| { + std::array::from_fn(|dx| chunks.get(&(x + dx as i32 - 1, z + dz as i32 - 1))) + }); + let mut c = chunks[&(x, z)].clone(); + c.sections = light_chunk(®ion, tables, sky_light); + c.lit = true; + world.insert((x, z), Arc::new(c)); + } + let palette: Vec = [ + "minecraft:air", + "minecraft:stone", + "minecraft:torch", + "minecraft:glowstone", + "minecraft:oak_leaves", + "minecraft:water", + "minecraft:glass", + ] + .iter() + .filter_map(|n| registry.default_state(n).map(|s| s.0 as u16)) + .chain( + facts + .states + .iter() + .enumerate() + .filter(|(_, f)| { + [ + "minecraft:stone_slab", + "minecraft:oak_stairs", + "minecraft:snow", + "minecraft:dirt_path", + ] + .contains(&&*f.name) + }) + .map(|(i, _)| i as u16) + .step_by(3), + ) + .collect(); + let mut seed = 0x2545_F491_4F6C_DD1Du64; + let mut next = |n: u64| { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed % n + }; + let mut bad = 0; + for e in 0..edits { + let (cx, cz) = inner[next(inner.len() as u64) as usize]; + let (x, z) = (cx * 16 + next(16) as i32, cz * 16 + next(16) as i32); + // Near the surface, where light varies most. + let top = (0..384) + .rev() + .find(|y| world[&(cx, cz)].get(x & 15, MIN_Y + y, z & 15) != 0) + .unwrap_or(100) as i32 + + MIN_Y; + let y = top - 3 + next(6) as i32; + let state = palette[next(palette.len() as u64) as usize]; + let chunk = world.get_mut(&(cx, cz)).unwrap(); + let old = Arc::make_mut(chunk).set(x & 15, y, z & 15, state); + if old == state || !tables.differs(old, state) { + continue; + } + relight(&mut world, tables, sky_light, &[(x, y, z)], false); + for dz in -1..=1 { + for dx in -1..=1 { + let (kx, kz) = (cx + dx, cz + dz); + let region: [[Option<&Chunk>; 3]; 3] = std::array::from_fn(|rz| { + std::array::from_fn(|rx| { + world + .get(&(kx + rx as i32 - 1, kz + rz as i32 - 1)) + .map(|c| &**c) + }) + }); + let full = light_chunk(®ion, tables, sky_light); + for (si, s) in full.iter().enumerate() { + for i in 0..4096 { + let (a, b) = (s.light(i), world[&(kx, kz)].sections[si].light(i)); + if a != b { + bad += 1; + if bad < 20 { + let (lx, lz, ly) = (i & 15, (i >> 4) & 15, i >> 8); + eprintln!( + "edit {e} at {x} {y} {z} ({} -> {}): {} {} {} full {a:02x} incremental {b:02x}", + facts.states[old as usize].name, + facts.states[state as usize].name, + kx * 16 + lx as i32, + MIN_Y + si as i32 * 16 + ly as i32, + kz * 16 + lz as i32 + ); + } + } + } + } + } + } + } + println!("{edits} edits: {bad} light values differ from a full relight"); + if bad > 0 { + std::process::exit(1); + } +} diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 6f6b063aa..b4cf33c03 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -4640,7 +4640,7 @@ pub fn run() -> Result<()> { let visibility = models::load_visibility(&root, &assets.registry)?; eprintln!("load visibility: {:.2}s", started.elapsed().as_secs_f32()); let tables = Arc::new(MeshTables::new(&assets, &facts, visibility)?); - let light = Arc::new(LightTables::new(&facts)); + let light = Arc::new(LightTables::load(&root, &assets.registry, &facts)?); eprintln!("load tables: {:.2}s", started.elapsed().as_secs_f32()); let atlas = HudAtlas::build(&root)?; eprintln!("load atlas: {:.2}s", started.elapsed().as_secs_f32()); diff --git a/crates/horizon-client/src/light.rs b/crates/horizon-client/src/light.rs index 5f479e3f1..0dc8071b3 100644 --- a/crates/horizon-client/src/light.rs +++ b/crates/horizon-client/src/light.rs @@ -1,26 +1,52 @@ //! Sky and block light for one chunk, flood-filled over its 3x3 neighbourhood -//! with vanilla's rules: sky sources run down each column until an edge is -//! occluded (`ChunkSkyLightSources`), and light then spreads losing -//! `max(1, dampening)` per step (`LightEngine.getOpacity`). Light can travel at -//! most 15 blocks, so the 48-wide region determines the centre exactly, apart -//! from face-shape occlusion of partial blocks (slabs, stairs), which is not -//! modelled yet. +//! with vanilla's `LightEngine` rules: +//! - light entering a block loses `max(1, getLightDampening)` (`getOpacity`); +//! - it cannot cross a face where the two blocks' occlusion faces together +//! cover the whole square (`shapeOccludes`, using `getFaceOcclusionShape` +//! only for states with `canOcclude && useShapeForLightOcclusion`), which is +//! what makes slabs, stairs, snow layers etc. block light directionally; +//! - sky sources run down each column to the first occluded edge +//! (`ChunkSkyLightSources.isEdgeOccluded`: dampening != 0 or the faces +//! between the two blocks occlude). +//! +//! The final values of vanilla's queue-based engine are the maximum over all +//! paths, so a flood fill to the fixed point reproduces them. Light travels at +//! most 15 blocks, so the 48-wide region determines the centre exactly. +//! `relight_block` applies vanilla's incremental decrease/increase passes +//! directly to resident chunks after a block change. use crate::{ blocks::BlockFacts, - chunk::{Chunk, HEIGHT, Light, SECTIONS, Section, index}, + chunk::{Chunk, HEIGHT, Light, MIN_Y, SECTIONS, Section, index}, +}; +use horizon_core::geometry::{Direction, ShapeOp, VoxelShape}; +use horizon_world::registry::{BlockRegistry, BlockStateId}; +use std::{ + collections::{HashMap, VecDeque}, + path::Path, + sync::Arc, }; -use std::{collections::VecDeque, sync::Arc}; const W: usize = 48; +/// Face ID 0: empty occlusion face; 1: the full square. +const EMPTY: u16 = 0; +const FULL: u16 = 1; + #[derive(Clone, Copy)] struct Cell { dampening: u8, emission: u8, + /// Any face is non-empty (`!isEmptyShape`), so edges need the face table. + shaped: bool, + faces: [u16; 6], } pub struct LightTables { cells: Vec, + /// `faceShapeOccludes(a, b)` for every pair of distinct face shapes. + occludes: Vec, + faces: usize, } impl LightTables { + /// Dampening and emission only: every occlusion face is empty. pub fn new(facts: &BlockFacts) -> Self { Self { cells: facts @@ -29,11 +55,120 @@ impl LightTables { .map(|s| Cell { dampening: s.dampening.min(15), emission: s.emission.min(15), + shaped: false, + faces: [EMPTY; 6], }) .collect(), + occludes: vec![false, true, true, true], + faces: 2, } } + /// Full tables with the vanilla occlusion shapes from `occlusion.nbt`. + pub fn load(root: &Path, registry: &BlockRegistry, facts: &BlockFacts) -> crate::Result { + use horizon_world::{ + collision::CachedCollisionRegistry, + nbt::{self, Limits}, + }; + let data = nbt::read_named(&std::fs::read(root.join("occlusion.nbt"))?, Limits::default())?.0.tag; + let occlusion = CachedCollisionRegistry::from_nbt(&data, registry)?; + let mut tables = Self::new(facts); + let block = VoxelShape::block(); + let mut shapes: Vec = vec![VoxelShape::empty(), block.clone()]; + let mut by_geometry: HashMap, u16> = HashMap::new(); + let mut by_shape: HashMap<*const VoxelShape, [u16; 6]> = HashMap::new(); + for (id, cell) in tables.cells.iter_mut().enumerate() { + let state = &facts.states[id]; + // LightEngine.isEmptyShape: !canOcclude || !useShapeForLightOcclusion. + if !state.can_occlude || !state.shape_occlusion { + continue; + } + let Some(shape) = occlusion.cached_shape(BlockStateId(id as u32))? else { continue }; + if shape.is_empty() { + continue; + } + let faces = if let Some(faces) = by_shape.get(&std::ptr::from_ref(shape)) { + *faces + } else { + let mut faces = [FULL; 6]; + // BlockState.initCache: solidRender shapes use the full faces. + if shape.join_is_not_empty(&block, ShapeOp::XOR)? { + for direction in Direction::ALL { + let face = shape.face_shape(direction)?; + faces[direction as usize] = if face.is_empty() { + EMPTY + } else if face.same_identity(&block) { + FULL + } else { + let key: Vec = face + .boxes() + .iter() + .flat_map(|b| b.min_position().components().into_iter().chain(b.max_position().components())) + .map(f64::to_bits) + .collect(); + *by_geometry.entry(key).or_insert_with(|| { + shapes.push(face.clone()); + (shapes.len() - 1) as u16 + }) + }; + } + } + by_shape.insert(std::ptr::from_ref(shape), faces); + faces + }; + cell.faces = faces; + cell.shaped = faces.iter().any(|f| *f != EMPTY); + } + let n = shapes.len(); + let mut occludes = vec![false; n * n]; + for a in 0..n { + for b in 0..n { + occludes[a * n + b] = if a == FULL as usize || b == FULL as usize { + true + } else if a == EMPTY as usize && b == EMPTY as usize { + false + } else { + // Shapes.faceShapeOccludes: the union covers the whole face. + let union = shapes[a].join_unoptimized(&shapes[b], ShapeOp::OR)?; + !block.join_is_not_empty(&union, ShapeOp::ONLY_FIRST)? + }; + } + } + tables.occludes = occludes; + tables.faces = n; + Ok(tables) + } + /// `LightEngine.shapeOccludes(from, to, direction)`. + #[inline] + fn blocked(&self, from: usize, to: usize, direction: usize) -> bool { + let (a, b) = (&self.cells[from], &self.cells[to]); + if !a.shaped && !b.shaped { + return false; + } + let fa = a.faces[direction] as usize; + let fb = b.faces[direction ^ 1] as usize; + self.occludes[fa * self.faces + fb] + } + /// `ChunkSkyLightSources.isEdgeOccluded(top, bottom)`. + #[inline] + fn edge_occluded(&self, top: usize, bottom: usize) -> bool { + self.cells[bottom].dampening != 0 || self.blocked(top, bottom, Direction::Down as usize) + } + pub fn emission(&self, state: u16) -> u8 { + self.cells[state as usize].emission + } + pub fn dampening(&self, state: u16) -> u8 { + self.cells[state as usize].dampening + } + /// Whether a state change can change light (`hasDifferentLightProperties`). + pub fn differs(&self, old: u16, new: u16) -> bool { + let (a, b) = (&self.cells[old as usize], &self.cells[new as usize]); + old != new && (a.dampening != b.dampening || a.emission != b.emission || a.shaped || b.shaped) + } } +// Direction index order matches `Direction` (down, up, north, south, west, east); +// `d ^ 1` is the opposite direction. +const OFFSETS: [(i32, i32, i32); 6] = [(0, -1, 0), (0, 1, 0), (0, 0, -1), (0, 0, 1), (-1, 0, 0), (1, 0, 0)]; + /// `region[dz][dx]` holds the chunks around the centre (index 1,1). /// `sky` is false in dimensions without skylight (the Nether). pub fn light_chunk(region: &[[Option<&Chunk>; 3]; 3], tables: &LightTables, sky_light: bool) -> Vec> { @@ -48,9 +183,11 @@ pub fn light_chunk(region: &[[Option<&Chunk>; 3]; 3], tables: &LightTables, sky_ } } } - let h = (top_section * 16 + 1).min(HEIGHT as usize); + // One extra section above the highest occupied one: block light rises + // up to 14 blocks into it (sky light there is always 15). + let h = ((top_section + 1) * 16).min(HEIGHT as usize); let at = |x: usize, y: usize, z: usize| (y * W + z) * W + x; - let mut damp = vec![0u8; W * W * h]; + let mut states = vec![0u16; W * W * h]; let mut sky = vec![0u8; W * W * h]; let mut block = vec![0u8; W * W * h]; let mut queue = VecDeque::new(); @@ -63,20 +200,20 @@ pub fn light_chunk(region: &[[Option<&Chunk>; 3]; 3], tables: &LightTables, sky_ continue; } for i in 0..4096 { - let id = section.get(i) as usize; - let cell = tables.cells[id]; + let id = section.get(i); let (lx, lz, ly) = (i & 15, (i >> 4) & 15, i >> 8); let p = at(dx * 16 + lx, si * 16 + ly, dz * 16 + lz); - damp[p] = cell.dampening; - if cell.emission > 0 { - block[p] = cell.emission; + states[p] = id; + let emission = tables.cells[id as usize].emission; + if emission > 0 { + block[p] = emission; queue.push_back(p as u32); } } } } } - // Columns of missing neighbour chunks contribute no light. + // Columns of missing neighbour chunks neither hold nor pass light. let present = |x: usize, z: usize| region[z / 16][x / 16].is_some(); let mut lowest = vec![h as i32; W * W]; for z in 0..W { @@ -84,13 +221,14 @@ pub fn light_chunk(region: &[[Option<&Chunk>; 3]; 3], tables: &LightTables, sky_ if !present(x, z) || !sky_light { continue; } - let mut y = h as i32 - 1; // The top layer (above the occupied range) is always open sky. - while y >= 0 && damp[at(x, y as usize, z)] == 0 { - sky[at(x, y as usize, z)] = 15; + let mut y = h as i32 - 1; + sky[at(x, y as usize, z)] = 15; + while y > 0 && !tables.edge_occluded(states[at(x, y as usize, z)] as usize, states[at(x, y as usize - 1, z)] as usize) { y -= 1; + sky[at(x, y as usize, z)] = 15; } - lowest[z * W + x] = y + 1; + lowest[z * W + x] = y; } } for z in 0..W { @@ -125,30 +263,23 @@ pub fn light_chunk(region: &[[Option<&Chunk>; 3]; 3], tables: &LightTables, sky_ let x = p % W; let z = (p / W) % W; let y = p / (W * W); - let mut visit = |q: usize| { - let next = level.saturating_sub(damp[q].max(1)); - if next > light[q] { + let from = states[p] as usize; + for (d, (ox, oy, oz)) in OFFSETS.iter().enumerate() { + let (nx, ny, nz) = (x as i32 + ox, y as i32 + oy, z as i32 + oz); + if nx < 0 || nz < 0 || ny < 0 || nx >= W as i32 || nz >= W as i32 || ny >= h as i32 { + continue; + } + let (nx, ny, nz) = (nx as usize, ny as usize, nz as usize); + if !present(nx, nz) { + continue; + } + let q = at(nx, ny, nz); + let to = states[q] as usize; + let next = level.saturating_sub(tables.cells[to].dampening.max(1)); + if next > light[q] && !tables.blocked(from, to, d) { light[q] = next; queue.push_back(q as u32); } - }; - if x > 0 { - visit(p - 1); - } - if x + 1 < W { - visit(p + 1); - } - if z > 0 { - visit(p - W); - } - if z + 1 < W { - visit(p + W); - } - if y > 0 { - visit(p - W * W); - } - if y + 1 < h { - visit(p + W * W); } } }; @@ -184,3 +315,282 @@ pub fn light_chunk(region: &[[Option<&Chunk>; 3]; 3], tables: &LightTables, sky_ } sections } + + +/// Resident chunks the incremental relighter reads and writes. +pub trait LightWorld { + fn state(&self, x: i32, y: i32, z: i32) -> Option; + /// Packed light (sky << 4 | block), None outside loaded chunks. + fn light(&self, x: i32, y: i32, z: i32) -> Option; + fn set_light(&mut self, x: i32, y: i32, z: i32, packed: u8); +} +impl LightWorld for HashMap<(i32, i32), Arc> { + fn state(&self, x: i32, y: i32, z: i32) -> Option { + if !(MIN_Y..MIN_Y + HEIGHT).contains(&y) { + return None; + } + self.get(&(x >> 4, z >> 4)).filter(|c| c.lit).map(|c| c.get(x & 15, y, z & 15)) + } + fn light(&self, x: i32, y: i32, z: i32) -> Option { + if !(MIN_Y..MIN_Y + HEIGHT).contains(&y) { + return None; + } + self.get(&(x >> 4, z >> 4)).filter(|c| c.lit).map(|c| c.light(x & 15, y, z & 15)) + } + fn set_light(&mut self, x: i32, y: i32, z: i32, packed: u8) { + let Some(chunk) = self.get_mut(&(x >> 4, z >> 4)) else { return }; + let s = ((y - MIN_Y) >> 4) as usize; + let i = index(x, y, z); + if chunk.sections[s].light(i) == packed { + return; + } + let section = Arc::make_mut(&mut Arc::make_mut(chunk).sections[s]); + if let Light::Uniform(v) = section.light { + section.light = Light::Full(Box::new([v; 4096])); + } + if let Light::Full(a) = &mut section.light { + a[i] = packed; + } + } +} +type Pos = (i32, i32, i32); +fn level(world: &impl LightWorld, sky: bool, p: Pos) -> Option { + world.light(p.0, p.1, p.2).map(|v| if sky { v >> 4 } else { v & 15 }) +} +fn set_level(world: &mut impl LightWorld, sky: bool, p: Pos, value: u8) { + let Some(old) = world.light(p.0, p.1, p.2) else { return }; + let packed = if sky { (old & 0x0F) | value << 4 } else { (old & 0xF0) | value }; + world.set_light(p.0, p.1, p.2, packed); +} +/// `ChunkSkyLightSources`: the Y of the upper block of the highest occluded +/// edge in the column (every block at or above it is a sky source). +pub fn lowest_sky_source(world: &impl LightWorld, tables: &LightTables, x: i32, z: i32) -> i32 { + let mut y = MIN_Y + HEIGHT - 1; + let Some(mut top) = world.state(x, y, z) else { return MIN_Y }; + while y > MIN_Y { + let Some(bottom) = world.state(x, y - 1, z) else { break }; + if tables.edge_occluded(top as usize, bottom as usize) { + return y; + } + top = bottom; + y -= 1; + } + MIN_Y +} +/// Vanilla's `checkBlock` + `runLightUpdates` for changed positions on +/// resident, lit chunks: clear the light that may have come through or from +/// the old states (decrease pass), then re-seed sources and propagate back in +/// from the brighter boundary (increase pass). Returns every position whose +/// stored light changed, for remeshing. `force` clears around each position +/// as if it had been fully lit, for light computed from outdated blocks. +pub fn relight(world: &mut impl LightWorld, tables: &LightTables, sky_light: bool, positions: &[Pos], force: bool) -> Vec { + let mut changed = Vec::new(); + relight_layer(world, tables, false, positions.to_vec(), force, &mut changed); + if sky_light { + let mut seeds = positions.to_vec(); + let mut columns: HashMap<(i32, i32), i32> = HashMap::new(); + for &(x, _, z) in positions { + if columns.contains_key(&(x, z)) { + continue; + } + let source = lowest_sky_source(world, tables, x, z); + columns.insert((x, z), source); + // Cells whose source status changed: non-15 at or above the new + // lowest source, and old 15s (sources) below it. + for y in source..MIN_Y + HEIGHT { + if level(world, true, (x, y, z)).is_some_and(|l| l != 15) { + seeds.push((x, y, z)); + } + } + let mut y = source - 1; + while y >= MIN_Y && level(world, true, (x, y, z)) == Some(15) { + seeds.push((x, y, z)); + y -= 1; + } + } + relight_layer(world, tables, true, seeds, force, &mut changed); + } + changed.sort_unstable(); + changed.dedup(); + changed +} +fn relight_layer(world: &mut impl LightWorld, tables: &LightTables, sky: bool, seeds: Vec, force: bool, changed: &mut Vec) { + let mut decrease: VecDeque<(Pos, u8)> = VecDeque::new(); + let mut cleared: Vec = Vec::new(); + let mut increase: VecDeque = VecDeque::new(); + for p in seeds { + let Some(old) = level(world, sky, p) else { continue }; + if old > 0 { + set_level(world, sky, p, 0); + changed.push(p); + } + cleared.push(p); + decrease.push_back((p, if force { 15 } else { old })); + } + while let Some((p, from)) = decrease.pop_front() { + for (ox, oy, oz) in OFFSETS { + let q = (p.0 + ox, p.1 + oy, p.2 + oz); + let Some(l) = level(world, sky, q) else { continue }; + if l == 0 { + continue; + } + // Sky light runs straight down without loss, so a 15 below a + // cleared 15 may have come from it too. + if l < from || (sky && oy == -1 && l == 15 && from == 15) { + set_level(world, sky, q, 0); + changed.push(q); + cleared.push(q); + decrease.push_back((q, l)); + } else { + increase.push_back(q); + } + } + } + // Sources inside the cleared area light up again. + let mut sources: HashMap<(i32, i32), i32> = HashMap::new(); + for p in cleared { + let seed = if sky { + let lowest = *sources.entry((p.0, p.2)).or_insert_with(|| lowest_sky_source(world, tables, p.0, p.2)); + if p.1 >= lowest { 15 } else { 0 } + } else { + world.state(p.0, p.1, p.2).map_or(0, |s| tables.cells[s as usize].emission) + }; + if seed > level(world, sky, p).unwrap_or(15) { + set_level(world, sky, p, seed); + changed.push(p); + increase.push_back(p); + } + } + while let Some(p) = increase.pop_front() { + let Some(l) = level(world, sky, p) else { continue }; + if l <= 1 { + continue; + } + let Some(from) = world.state(p.0, p.1, p.2) else { continue }; + for (d, (ox, oy, oz)) in OFFSETS.iter().enumerate() { + let q = (p.0 + ox, p.1 + oy, p.2 + oz); + let Some(to) = world.state(q.0, q.1, q.2) else { continue }; + let Some(ql) = level(world, sky, q) else { continue }; + let next = l.saturating_sub(tables.cells[to as usize].dampening.max(1)); + if next > ql && !tables.blocked(from as usize, to as usize, d) { + set_level(world, sky, q, next); + changed.push(q); + increase.push_back(q); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const AIR: u16 = 0; + const STONE: u16 = 1; + const TORCH: u16 = 2; + const SLAB: u16 = 3; + /// Air, stone, torch and a bottom slab (full bottom face, half sides, + /// empty top), with a third face shape for the half sides. + fn tables() -> LightTables { + let cell = |dampening, emission, faces: [u16; 6]| Cell { + dampening, + emission, + shaped: faces.iter().any(|f| *f != EMPTY), + faces, + }; + LightTables { + cells: vec![ + cell(0, 0, [EMPTY; 6]), + cell(15, 0, [FULL; 6]), + cell(0, 14, [EMPTY; 6]), + cell(0, 0, [FULL, EMPTY, 2, 2, 2, 2]), + ], + // empty, full, half: only pairs with a full face cover the square. + occludes: vec![false, true, false, true, true, true, false, true, false], + faces: 3, + } + } + fn world(edit: impl Fn(&mut Chunk)) -> HashMap<(i32, i32), Arc> { + let mut chunks = HashMap::new(); + for z in -1..=1 { + for x in -1..=1 { + let mut c = Chunk::empty(x, z); + for y in MIN_Y..0 { + for i in 0..256 { + c.set(i & 15, y, i >> 4, STONE); + } + } + if (x, z) == (0, 0) { + edit(&mut c); + } + chunks.insert((x, z), c); + } + } + let lit: Vec<_> = chunks + .keys() + .map(|&(x, z)| { + let region: [[Option<&Chunk>; 3]; 3] = + std::array::from_fn(|dz| std::array::from_fn(|dx| chunks.get(&(x + dx as i32 - 1, z + dz as i32 - 1)))); + let mut c = chunks[&(x, z)].clone(); + c.sections = light_chunk(®ion, &tables(), true); + c.lit = true; + ((x, z), Arc::new(c)) + }) + .collect(); + lit.into_iter().collect() + } + fn sky(world: &HashMap<(i32, i32), Arc>, x: i32, y: i32, z: i32) -> u8 { + world.light(x, y, z).unwrap() >> 4 + } + fn block(world: &HashMap<(i32, i32), Arc>, x: i32, y: i32, z: i32) -> u8 { + world.light(x, y, z).unwrap() & 15 + } + + #[test] + fn torch_light_falls_off_by_one_per_block() { + let w = world(|c| { + c.set(8, 0, 8, TORCH); + }); + assert_eq!(block(&w, 8, 0, 8), 14); + assert_eq!(block(&w, 9, 0, 8), 13); + assert_eq!(block(&w, 8, 3, 8), 11); + assert_eq!(block(&w, 8, -1, 8), 0); + } + #[test] + fn bottom_slab_takes_sky_from_above_but_not_through_its_bottom() { + // A slab roof over a closed stone room at y 0..2. + let w = world(|c| { + for x in 4..13 { + for z in 4..13 { + c.set(x, 3, z, SLAB); + for y in 0..3 { + let wall = x == 4 || x == 12 || z == 4 || z == 12; + c.set(x, y, z, if wall { STONE } else { AIR }); + } + } + } + }); + assert_eq!(sky(&w, 8, 3, 8), 15, "slab cell is lit through its open top"); + // Without the shape test the room would be lit (slabs have no dampening). + assert_eq!(sky(&w, 8, 2, 8), 0, "the full bottom face stops sky light"); + } + #[test] + fn incremental_relight_matches_full_relight() { + let mut w = world(|c| { + c.set(8, 0, 8, TORCH); + }); + let edits = [((8, 0, 8), AIR), ((9, 1, 8), STONE), ((3, 2, 3), TORCH), ((8, 5, 8), SLAB), ((8, -1, 8), AIR)]; + for ((x, y, z), state) in edits { + Arc::make_mut(w.get_mut(&(0, 0)).unwrap()).set(x, y, z, state); + relight(&mut w, &tables(), true, &[(x, y, z)], false); + } + let region: [[Option<&Chunk>; 3]; 3] = + std::array::from_fn(|dz| std::array::from_fn(|dx| w.get(&(dx as i32 - 1, dz as i32 - 1)).map(|c| &**c))); + let full = light_chunk(®ion, &tables(), true); + for (si, s) in full.iter().enumerate() { + for i in 0..4096 { + assert_eq!(s.light(i), w[&(0, 0)].sections[si].light(i), "section {si} index {i}"); + } + } + } +} diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 60e404f76..fd3298594 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -109,6 +109,13 @@ pub struct Streaming { /// Dimension flags: sky light, Nether face shading. pub sky_light: bool, pub nether_shading: bool, + /// Incremental relighting of block edits on lit chunks (see `light::relight`). + light_tables: Arc, + /// Applied edits by sequence, replayed onto light jobs snapshotted before them. + light_log: Vec<(u64, (i32, i32, i32))>, + light_edit_sequence: u64, + /// Edit sequence at each in-flight light job's snapshot. + light_snapshots: HashMap<(i32, i32), u64>, pub generated_total: u64, pub meshed_total: u64, } @@ -126,6 +133,7 @@ impl Streaming { stop: AtomicBool::new(false), }); let (send, results) = mpsc::channel(); + let light_tables = context.light.clone(); let threads = (0..threads.max(1)) .map(|i| { let shared = shared.clone(); @@ -203,6 +211,10 @@ impl Streaming { paused: false, sky_light: true, nether_shading: false, + light_tables, + light_log: Vec::new(), + light_edit_sequence: 0, + light_snapshots: HashMap::new(), generated_total: 0, meshed_total: 0, } @@ -242,6 +254,7 @@ impl Streaming { if self.paused { return (uploads, Vec::new()); } + self.relight_edits(world); while let Ok(done) = self.results.try_recv() { match done { Done::Proto(x, z, proto) => { @@ -282,6 +295,7 @@ impl Streaming { continue; } self.lighting.remove(&(x, z)); + let snapshot = self.light_snapshots.remove(&(x, z)).unwrap_or(u64::MAX); if let Some(chunk) = world.chunks.get_mut(&(x, z)) { let chunk = Arc::make_mut(chunk); // Keep blocks edited since the snapshot; take only light. @@ -292,6 +306,7 @@ impl Streaming { } chunk.lit = true; self.lit.insert((x, z)); + self.replay_light_edits(world, x, z, snapshot); for dz in -1..=1 { for dx in -1..=1 { for sy in 0..SECTIONS as i32 { @@ -426,6 +441,7 @@ impl Streaming { world.dirty_light.remove(&(x, z)); self.sequence += 1; self.lighting.insert((x, z), self.sequence); + self.light_snapshots.insert((x, z), self.light_edit_sequence); self.submit(self.distance(x, z) * 4 + 1, Job::Light(x, z, region, self.sequence, self.sky_light)); } // Meshing once the column and its neighbours are lit. @@ -455,6 +471,68 @@ impl Streaming { } (uploads, removed) } + /// Relight block edits on lit chunks in place (vanilla's incremental light + /// engine) instead of recomputing the 3x3 chunks around each edit. + fn relight_edits(&mut self, world: &mut World) { + if world.light_edits.is_empty() { + return; + } + let edits = std::mem::take(&mut world.light_edits); + let positions: Vec<(i32, i32, i32)> = edits + .iter() + .filter(|(_, old, new)| self.light_tables.differs(*old, *new)) + .map(|(p, _, _)| *p) + .collect(); + if positions.is_empty() { + return; + } + let changed = crate::light::relight(&mut world.chunks, &self.light_tables, self.sky_light, &positions, false); + Self::mark_light_changes(world, &changed); + if !self.light_snapshots.is_empty() { + for p in positions { + self.light_edit_sequence += 1; + self.light_log.push((self.light_edit_sequence, p)); + } + } + } + /// A light job computed from blocks older than some edits: redo those + /// edits on top of its result. + fn replay_light_edits(&mut self, world: &mut World, x: i32, z: i32, snapshot: u64) { + let positions: Vec<(i32, i32, i32)> = self + .light_log + .iter() + .filter(|(seq, p)| *seq > snapshot && ((p.0 >> 4) - x).abs() <= 1 && ((p.2 >> 4) - z).abs() <= 1) + .map(|(_, p)| *p) + .collect(); + if !positions.is_empty() { + let changed = crate::light::relight(&mut world.chunks, &self.light_tables, self.sky_light, &positions, true); + Self::mark_light_changes(world, &changed); + } + let oldest = self.light_snapshots.values().copied().min(); + match oldest { + Some(oldest) => self.light_log.retain(|(seq, _)| *seq > oldest), + None => self.light_log.clear(), + } + } + /// Sections whose meshes sample a changed light value (faces and corners + /// read light from neighbouring blocks). + fn mark_light_changes(world: &mut World, changed: &[(i32, i32, i32)]) { + let mut last = None; + for &(x, y, z) in changed { + for dz in -1..=1 { + for dy in -1..=1 { + for dx in -1..=1 { + let s = ((y + dy - crate::chunk::MIN_Y) >> 4, (x + dx) >> 4, (z + dz) >> 4); + if last == Some(s) || !(0..SECTIONS as i32).contains(&s.0) { + continue; + } + last = Some(s); + world.dirty_sections.insert((s.1, s.0, s.2)); + } + } + } + } + } fn in_range(&self, x: i32, z: i32, margin: i32) -> bool { let r = i64::from(self.radius + margin); self.distance(x, z) <= r * r diff --git a/crates/horizon-client/src/world.rs b/crates/horizon-client/src/world.rs index d377c5103..1941d87d4 100644 --- a/crates/horizon-client/src/world.rs +++ b/crates/horizon-client/src/world.rs @@ -18,6 +18,9 @@ pub struct World { pub dirty_sections: HashSet<(i32, i32, i32)>, /// Chunks whose light must be recomputed. pub dirty_light: HashSet<(i32, i32)>, + /// Edits on lit chunks (position, old state, new state), relit + /// incrementally by streaming. + pub light_edits: Vec<((i32, i32, i32), u16, u16)>, /// Chunks changed since the last save. pub modified: HashSet<(i32, i32)>, pub block_entities: HashMap<(i32, i32, i32), crate::containers::BlockEntity>, @@ -90,6 +93,7 @@ impl World { air, dirty_sections: HashSet::new(), dirty_light: HashSet::new(), + light_edits: Vec::new(), modified: HashSet::new(), block_entities: HashMap::new(), game_time: 0, @@ -125,9 +129,13 @@ impl World { } } let _ = sy; - for dz in -1..=1 { - for dx in -1..=1 { - self.dirty_light.insert(((x >> 4) + dx, (z >> 4) + dz)); + if self.chunks.get(&(x >> 4, z >> 4)).is_some_and(|c| c.lit) { + self.light_edits.push(((x, y, z), previous, state.0 as u16)); + } else { + for dz in -1..=1 { + for dx in -1..=1 { + self.dirty_light.insert(((x >> 4) + dx, (z >> 4) + dz)); + } } } } diff --git a/tools/anvil_dump.py b/tools/anvil_dump.py new file mode 100644 index 000000000..7d9e49afa --- /dev/null +++ b/tools/anvil_dump.py @@ -0,0 +1,146 @@ +#!/usr/bin/env python3 +"""Dump Minecraft NBT files (level.dat, data/*.dat, players/*.dat) and Anvil +region chunks (region/, entities/, poi/) as readable SNBT-like text. + + anvil_dump.py FILE.dat [PATH...] + anvil_dump.py r.0.0.mca [CX CZ] [PATH...] + +PATH selects nested keys (for example `sections 3 block_states`). Long +arrays are summarised unless --full is given; --typed suffixes numbers +with their NBT type (b, s, l, f, d; plain ints are TAG_Int). Used to compare Horizon saves +with worlds written by the vanilla server. +""" +import gzip +import io +import json +import struct +import sys +import zlib + + +def read_tag(f, tag_id): + if tag_id == 1: + return ('b', struct.unpack('>b', f.read(1))[0]) + if tag_id == 2: + return ('s', struct.unpack('>h', f.read(2))[0]) + if tag_id == 3: + return struct.unpack('>i', f.read(4))[0] + if tag_id == 4: + return ('l', struct.unpack('>q', f.read(8))[0]) + if tag_id == 5: + return ('f', struct.unpack('>f', f.read(4))[0]) + if tag_id == 6: + return ('d', struct.unpack('>d', f.read(8))[0]) + if tag_id == 7: + n = struct.unpack('>i', f.read(4))[0] + return ('B', list(struct.unpack(f'>{n}b', f.read(n)))) + if tag_id == 8: + n = struct.unpack('>H', f.read(2))[0] + return f.read(n).decode('utf-8', 'replace') + if tag_id == 9: + inner, n = struct.unpack('>bi', f.read(5)) + return [read_tag(f, inner) for _ in range(max(n, 0))] + if tag_id == 10: + out = {} + while True: + t = f.read(1)[0] + if t == 0: + return out + n = struct.unpack('>H', f.read(2))[0] + name = f.read(n).decode('utf-8', 'replace') + out[name] = read_tag(f, t) + if tag_id == 11: + n = struct.unpack('>i', f.read(4))[0] + return ('I', list(struct.unpack(f'>{n}i', f.read(4 * n)))) + if tag_id == 12: + n = struct.unpack('>i', f.read(4))[0] + return ('L', list(struct.unpack(f'>{n}q', f.read(8 * n)))) + raise ValueError(f'bad tag id {tag_id}') + + +def read_named(data): + f = io.BytesIO(data) + t = f.read(1)[0] + n = struct.unpack('>H', f.read(2))[0] + f.read(n) + return read_tag(f, t) + + +def read_dat(path): + raw = open(path, 'rb').read() + if raw[:2] == b'\x1f\x8b': + raw = gzip.decompress(raw) + return read_named(raw) + + +def region_chunks(path): + data = open(path, 'rb').read() + out = {} + for i in range(1024): + off = int.from_bytes(data[i * 4:i * 4 + 3], 'big') + if off == 0: + continue + start = off * 4096 + length = struct.unpack('>i', data[start:start + 4])[0] + codec = data[start + 4] & 0x7F + payload = data[start + 5:start + 4 + length] + if codec == 1: + payload = gzip.decompress(payload) + elif codec == 2: + payload = zlib.decompress(payload) + out[(i & 31, i >> 5)] = read_named(payload) + return out + + +def plain(v, full, typed=False): + if isinstance(v, tuple): + kind, val = v + if kind in 'bsldf': + return f'{val}{kind}' if typed else val + if not full and len(val) > 8: + return f'{kind}[{len(val)}: {val[:4]}...]' + return val + if isinstance(v, dict): + return {k: plain(x, full, typed) for k, x in v.items()} + if isinstance(v, list): + return [plain(x, full, typed) for x in v] + return v + + +def select(v, path): + for p in path: + if isinstance(v, list): + v = v[int(p)] + elif isinstance(v, tuple): + v = v[1][int(p)] + else: + v = v[p] + return v + + +def main(argv): + full = '--full' in argv + typed = '--typed' in argv + argv = [a for a in argv if a not in ('--full', '--typed')] + if not argv: + print(__doc__) + return 1 + path = argv[0] + rest = argv[1:] + if path.endswith('.mca'): + chunks = region_chunks(path) + if len(rest) >= 2 and rest[0].lstrip('-').isdigit(): + cx, cz = int(rest[0]) & 31, int(rest[1]) & 31 + value = chunks[(cx, cz)] + rest = rest[2:] + else: + print(json.dumps(sorted(chunks))) + return 0 + else: + value = read_dat(path) + print(json.dumps(plain(select(value, rest), full, typed), indent=1)) + return 0 + + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:])) diff --git a/tools/vanilla_world_check.py b/tools/vanilla_world_check.py new file mode 100644 index 000000000..81ad61b72 --- /dev/null +++ b/tools/vanilla_world_check.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python3 +"""Run the vanilla 26.3 dedicated server headless on a world folder, optionally +send console commands (`sleep SECONDS` pauses between them), then stop it cleanly and report problems from its log. + + vanilla_world_check.py --jar server.jar --dir RUN_DIR [--world NAME] + [--seed N] [--port P] [--command CMD ...] + [--wait-after SECONDS] + +RUN_DIR receives eula.txt, server.properties and logs/. An existing +RUN_DIR/NAME world is opened as is (this is how Horizon saves are checked); a +missing one is generated by vanilla (this is how reference worlds are made). +Exit status 1 when the log contains errors, upgrade/datafix work or chunk +load failures. +""" +import argparse +import os +import re +import subprocess +import sys +import threading +import time + +SUSPICIOUS = re.compile( + r'(ERROR|Exception|Couldn\'t load|Failed to|Skipping|Invalid|Unknown|corrupt|' + r'Recreating|upgrad|DataFix|out of bounds|missing|Mismatch)', re.IGNORECASE) +# Benign lines the server prints on every start. +BENIGN = re.compile(r'(OFFLINE/INSECURE|online-mode|authenticate usernames|hackers|' + r'Failed to load properties|Failed to load eula)') + + +def main(): + p = argparse.ArgumentParser() + p.add_argument('--jar', required=True) + p.add_argument('--dir', required=True) + p.add_argument('--world', default='world') + p.add_argument('--seed', default='12345') + p.add_argument('--port', default='25671') + p.add_argument('--command', action='append', default=[]) + p.add_argument('--wait-after', type=float, default=1.0) + p.add_argument('--timeout', type=float, default=240) + args = p.parse_args() + os.makedirs(args.dir, exist_ok=True) + with open(os.path.join(args.dir, 'eula.txt'), 'w') as f: + f.write('eula=true\n') + props = { + 'server-port': args.port, 'online-mode': 'false', 'level-seed': args.seed, + 'level-name': args.world, 'spawn-protection': '0', 'view-distance': '4', + 'simulation-distance': '4', 'sync-chunk-writes': 'true', 'enable-query': 'false', + 'enable-rcon': 'false', 'management-server-enabled': 'false', + 'pause-when-empty-seconds': '-1', + } + with open(os.path.join(args.dir, 'server.properties'), 'w') as f: + f.writelines(f'{k}={v}\n' for k, v in props.items()) + proc = subprocess.Popen( + ['java', '-Xmx2G', '-jar', os.path.abspath(args.jar), '--nogui'], cwd=args.dir, + stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) + lines = [] + done = threading.Event() + + def pump(): + for line in proc.stdout: + lines.append(line.rstrip('\n')) + if 'Done (' in line: + done.set() + done.set() + + threading.Thread(target=pump, daemon=True).start() + if not done.wait(args.timeout) or proc.poll() is not None: + proc.kill() + print('\n'.join(lines)) + print('server did not start', file=sys.stderr) + return 1 + for command in args.command: + if command.startswith('sleep '): + time.sleep(float(command.split()[1])) + continue + proc.stdin.write(command + '\n') + proc.stdin.flush() + time.sleep(0.5) + time.sleep(args.wait_after) + proc.stdin.write('stop\n') + proc.stdin.flush() + try: + proc.wait(args.timeout) + except subprocess.TimeoutExpired: + proc.kill() + time.sleep(0.2) + problems = [l for l in lines if SUSPICIOUS.search(l) and not BENIGN.search(l)] + with open(os.path.join(args.dir, 'check.log'), 'w') as f: + f.write('\n'.join(lines) + '\n') + for l in problems: + print(l) + print(f'{len(lines)} log lines, {len(problems)} suspicious') + return 1 if problems else 0 + + +if __name__ == '__main__': + sys.exit(main()) From 5c69e0521a6a5df3e71f8bee5a99486f06712fd1 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 8 Oct 2026 15:40:19 +0200 Subject: [PATCH 2/3] Save worlds in the vanilla 26.3 layout with entities, ticks, light and heightmaps Worlds now use 26.3's layout and files: dimensions/minecraft//{region, entities}, level.dat with singleplayer_uuid, players/data/.dat and the saved-data files for weather, world clocks (day time), world generation settings and game rules. Older Horizon saves are migrated on open. Chunks are written like SerializableChunkData: vanilla's short palette form for default states, SkyLight/BlockLight layers, the four FULL heightmaps, block_ticks/fluid_ticks from the tick scheduler, chest/trapped chest/barrel and furnace block entity IDs, and everything Horizon does not simulate (unknown block entities, structure references, post-processing) kept from the loaded chunk. Partially generated vanilla chunks are regenerated instead of loading as holes. Mobs, item entities, experience orbs, arrows and falling blocks are saved per chunk in entities/*.mca with vanilla NBT (entity_nbt.rs), keeping each loaded entity's UUID and unknown fields; entity types Horizon does not simulate are written back unchanged. Entities leave the world with their chunk and come back when it loads. examples/save_parity re-encodes every chunk of a vanilla-saved world and compares blocks, biomes, heightmaps, light, block entities and ticks (identical on three reference worlds apart from vanilla's stale worldgen light). A Horizon save of a vanilla world opens in the vanilla server with its mobs, items, arrows and unknown entities. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/examples/save_parity.rs | 265 ++++ crates/horizon-client/src/chunk.rs | 6 + crates/horizon-client/src/entity_nbt.rs | 358 +++++ crates/horizon-client/src/game.rs | 26 +- crates/horizon-client/src/game/persist.rs | 261 ++++ crates/horizon-client/src/lib.rs | 1 + crates/horizon-client/src/save.rs | 1343 ++++++++++++++--- crates/horizon-client/src/streaming.rs | 4 + crates/horizon-client/src/world.rs | 3 + 9 files changed, 2029 insertions(+), 238 deletions(-) create mode 100644 crates/horizon-client/examples/save_parity.rs create mode 100644 crates/horizon-client/src/entity_nbt.rs create mode 100644 crates/horizon-client/src/game/persist.rs diff --git a/crates/horizon-client/examples/save_parity.rs b/crates/horizon-client/examples/save_parity.rs new file mode 100644 index 000000000..017f60294 --- /dev/null +++ b/crates/horizon-client/examples/save_parity.rs @@ -0,0 +1,265 @@ +//! Differential save test: load every full chunk of a world written by the +//! vanilla 26.3 server, light it, encode it again with Horizon's chunk writer +//! and compare the result with the original NBT: block states, biomes, +//! heightmaps, light layers (by value), block entities and scheduled ticks. +//! +//! cargo run --profile fast -p horizon-client --example save_parity -- \ +//! RESOURCES DIMENSION_DIR [--nether] +use horizon_client::{ + assets::Assets, + biome::Biomes, + blocks::BlockFacts, + chunk::{Chunk, MIN_Y, SECTIONS}, + light::{LightTables, light_chunk}, + save::{self, SaveTables}, +}; +use horizon_core::position::ChunkPos; +use horizon_world::{ + chunk::BlockState, + nbt::{Limits, NamedTag, Tag}, + region::RegionFile, +}; +use std::{ + collections::{BTreeMap, HashMap}, + path::PathBuf, +}; + +/// Block state names per section index, decoded independently of Horizon. +fn block_names(tag: &Tag) -> BTreeMap> { + let mut out = BTreeMap::new(); + let Some(Tag::List(sections)) = tag.get("sections") else { + return out; + }; + for s in sections { + let Some(Tag::Byte(y)) = s.get("Y") else { + continue; + }; + let Some(states) = s.get("block_states") else { + continue; + }; + let Some(Tag::List(palette)) = states.get("palette") else { + continue; + }; + let names: Vec = palette + .iter() + .map(|p| { + let st = BlockState::from_nbt(p).expect("palette entry"); + format!("{}{:?}", st.name, st.properties) + }) + .collect(); + let bits = (usize::BITS - (names.len().max(1) - 1).leading_zeros()).max(4); + let per = 64 / bits as usize; + let data = match states.get("data") { + Some(Tag::LongArray(d)) => d.clone(), + _ => Vec::new(), + }; + let blocks = (0..4096) + .map(|i| { + if names.len() == 1 { + return names[0].clone(); + } + let v = (data[i / per] as u64 >> ((i % per) as u32 * bits)) & ((1 << bits) - 1); + names[v as usize].clone() + }) + .collect(); + out.insert(i64::from(*y), blocks); + } + out +} +fn light(tag: &Tag, key: &str) -> HashMap> { + let mut out = HashMap::new(); + if let Some(Tag::List(sections)) = tag.get("sections") { + for s in sections { + if let (Some(Tag::Byte(y)), Some(Tag::ByteArray(a))) = (s.get("Y"), s.get(key)) { + out.insert(i64::from(*y), a.clone()); + } + } + } + out +} +/// Light value with vanilla's reading of missing layers. +fn value( + layers: &HashMap>, + sy: i64, + i: usize, + sky: bool, + empty: bool, + top: i64, +) -> u8 { + let nibble = |a: &Vec, i: usize| (a[i >> 1] >> ((i & 1) * 4)) & 15; + if let Some(a) = layers.get(&sy) { + return nibble(a, i); + } + if !sky || !empty { + return 0; + } + ((sy + 1)..=top) + .find_map(|y| layers.get(&y)) + .map_or(15, |a| nibble(a, i & 255)) +} +fn count(tag: &Tag, key: &str) -> usize { + match tag.get(key) { + Some(Tag::List(l)) => l.len(), + _ => 0, + } +} + +fn main() -> horizon_client::Result<()> { + let args: Vec = std::env::args().skip(1).collect(); + let root = PathBuf::from( + args.first() + .ok_or("usage: save_parity RESOURCES DIMENSION_DIR")?, + ); + let dim = PathBuf::from(args.get(1).ok_or("missing dimension directory")?); + let sky_light = !args.iter().any(|a| a == "--nether"); + let assets = Assets::load(&root)?; + let facts = BlockFacts::load(&root, assets.registry)?; + let biomes = Biomes::load(&root)?; + let lights = LightTables::load(&root, assets.registry, &facts)?; + let tables = SaveTables::new(&assets).with_block_entities(&facts); + let mut originals: HashMap<(i32, i32), NamedTag> = HashMap::new(); + let mut chunks: HashMap<(i32, i32), Chunk> = HashMap::new(); + for entry in std::fs::read_dir(dim.join("region"))? { + let mut region = RegionFile::open(entry?.path())?; + let positions: Vec = region.chunks().map(|(p, _)| p).collect(); + for p in positions { + let Some(record) = region.read(p)? else { + continue; + }; + let tag = record.nbt(Limits::default())?; + if tag.tag.get("isLightOn") != Some(&Tag::Byte(1)) { + continue; + } + if let Some(c) = save::decode_with(&tag, assets.registry, &biomes, Some(&tables)) { + chunks.insert((c.x, c.z), c); + originals.insert((p.x, p.z), tag); + } + } + } + let mut keys: Vec<_> = chunks + .keys() + .copied() + .filter(|&(x, z)| { + (-1..=1).all(|dz| (-1..=1).all(|dx| chunks.contains_key(&(x + dx, z + dz)))) + }) + .collect(); + keys.sort_unstable(); + let ( + mut blocks_bad, + mut biomes_bad, + mut heightmaps_bad, + mut light_bad, + mut light_total, + mut other_bad, + ) = (0, 0, 0, 0u64, 0u64, 0); + for &(x, z) in &keys { + let region: [[Option<&Chunk>; 3]; 3] = std::array::from_fn(|dz| { + std::array::from_fn(|dx| chunks.get(&(x + dx as i32 - 1, z + dz as i32 - 1))) + }); + let mut chunk = chunks[&(x, z)].clone(); + chunk.sections = light_chunk(®ion, &lights, sky_light); + chunk.lit = true; + let ours = save::encode_with(&chunk, assets.registry, &biomes, Some(&tables), sky_light); + let theirs = &originals[&(x, z)]; + let (a, b) = (&ours.tag, &theirs.tag); + // Block states: compare every non-empty vanilla section. + let (na, nb) = (block_names(a), block_names(b)); + for (y, vb) in &nb { + match na.get(y) { + Some(va) if va == vb => {} + Some(va) => { + let n = va.iter().zip(vb).filter(|(p, q)| p != q).count(); + if blocks_bad < 5 { + let i = va.iter().zip(vb).position(|(p, q)| p != q).unwrap_or(0); + eprintln!( + "chunk {x} {z} section {y}: {n} blocks differ, e.g. {} vs {}", + va[i], vb[i] + ); + } + blocks_bad += 1; + } + None if vb.iter().all(|s| s.starts_with("minecraft:air")) => {} + None => blocks_bad += 1, + } + } + let biome_of = |t: &Tag| -> Vec { + match t.get("sections") { + Some(Tag::List(l)) => l.iter().filter_map(|s| s.get("biomes").cloned()).collect(), + _ => Vec::new(), + } + }; + if biome_of(a) != biome_of(b) { + biomes_bad += 1; + } + for kind in [ + "WORLD_SURFACE", + "OCEAN_FLOOR", + "MOTION_BLOCKING", + "MOTION_BLOCKING_NO_LEAVES", + ] { + let get = |t: &Tag| t.get("Heightmaps").and_then(|h| h.get(kind)).cloned(); + if get(a) != get(b) { + if heightmaps_bad < 5 { + eprintln!("chunk {x} {z}: heightmap {kind} differs"); + } + heightmaps_bad += 1; + } + } + for (key, sky) in [("SkyLight", true), ("BlockLight", false)] { + if sky && !sky_light { + continue; + } + let (la, lb) = (light(a, key), light(b, key)); + let top = i64::from((MIN_Y >> 4) + SECTIONS as i32); + for si in 0..SECTIONS { + let sy = i64::from((MIN_Y >> 4) + si as i32); + let empty = chunk.sections[si].is_uniform(tables_air(&assets)); + for i in 0..4096 { + light_total += 1; + if value(&la, sy, i, sky, empty, top) != value(&lb, sy, i, sky, empty, top) { + light_bad += 1; + } + } + } + } + for key in ["block_entities", "block_ticks", "fluid_ticks"] { + if count(a, key) != count(b, key) { + if other_bad < 5 { + eprintln!( + "chunk {x} {z}: {key} {} vs {}", + count(a, key), + count(b, key) + ); + } + other_bad += 1; + } + } + for key in [ + "structures", + "PostProcessing", + "InhabitedTime", + "Status", + "DataVersion", + "yPos", + ] { + if a.get(key) != b.get(key) { + if other_bad < 5 { + eprintln!("chunk {x} {z}: {key} differs"); + } + other_bad += 1; + } + } + } + println!( + "{} chunks: {blocks_bad} sections with different blocks, {biomes_bad} chunks with different biomes, \ + {heightmaps_bad} different heightmaps, {light_bad} of {light_total} light values differ, {other_bad} other differences", + keys.len() + ); + Ok(()) +} +fn tables_air(assets: &Assets) -> u16 { + assets + .registry + .default_state("minecraft:air") + .map_or(0, |s| s.0 as u16) +} diff --git a/crates/horizon-client/src/chunk.rs b/crates/horizon-client/src/chunk.rs index fd20abf21..c4fa24c0b 100644 --- a/crates/horizon-client/src/chunk.rs +++ b/crates/horizon-client/src/chunk.rs @@ -98,6 +98,10 @@ pub struct Chunk { pub fresh: bool, /// Block entities read from a save, moved into the world on arrival. pub block_entities: Vec<((i32, i32, i32), crate::containers::BlockEntity)>, + /// Saved data Horizon keeps but does not simulate (save.rs). + pub saved: Option>, + /// Entities read from a save (vanilla entity NBT), spawned on arrival. + pub entities: Vec, } impl Chunk { pub fn empty(x: i32, z: i32) -> Self { @@ -110,6 +114,8 @@ impl Chunk { lit: false, fresh: false, block_entities: Vec::new(), + saved: None, + entities: Vec::new(), } } #[inline] diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs new file mode 100644 index 000000000..fca810e6d --- /dev/null +++ b/crates/horizon-client/src/entity_nbt.rs @@ -0,0 +1,358 @@ +//! Vanilla 26.3 entity NBT (`Entity.saveWithoutId` and the subclasses' +//! `addAdditionalSaveData`) for the entities Horizon simulates: mobs, item +//! entities, experience orbs, arrows and falling blocks. Encoding starts from +//! the compound the entity was loaded with, when there is one, so fields +//! Horizon does not model (variants, attributes, equipment, custom names) +//! survive a Horizon save. +use crate::{ + entities::{Arrow, ExperienceOrb, ItemEntity}, + mobs::{Mob, MobType}, + save::{doubles, float, floats, item_from_nbt, item_to_nbt, number, put, read_doubles, text, uuid_from_tag}, +}; +use horizon_core::geometry::Vec3; +use horizon_world::nbt::{Compound, Tag}; +use std::{collections::HashMap, sync::Arc}; + +fn string(s: &str) -> Tag { + Tag::String(s.into()) +} +/// Entity type ID (`id`). +pub fn kind(tag: &Tag) -> Option { + text(tag.get("id")) +} +pub fn position(tag: &Tag) -> Option { + read_doubles::<3>(tag.get("Pos")).map(|p| Vec3::new(p[0], p[1], p[2])) +} +pub fn uuid(tag: &Tag) -> Option<[i32; 4]> { + uuid_from_tag(tag.get("UUID")) +} +fn motion(tag: &Tag) -> Vec3 { + read_doubles::<3>(tag.get("Motion")).map_or(Vec3::ZERO, |m| Vec3::new(m[0], m[1], m[2])) +} +fn rotation(tag: &Tag) -> [f32; 2] { + read_doubles::<2>(tag.get("Rotation")).map_or([0.0; 2], |r| [r[0] as f32, r[1] as f32]) +} +fn flag(tag: &Tag, key: &str) -> bool { + number(tag.get(key)).unwrap_or(0) != 0 +} +/// `Entity.saveWithoutId` fields every entity has, over the loaded compound. +struct Base<'a> { + id: &'a str, + uuid: [i32; 4], + position: Vec3, + motion: Vec3, + rotation: [f32; 2], + on_ground: bool, + fall_distance: f64, +} +impl Base<'_> { + fn tag(&self, raw: Option<&Tag>) -> Tag { + let mut tag = match raw { + Some(t @ Tag::Compound(_)) => t.clone(), + _ => Tag::Compound(Compound::new()), + }; + for (key, value) in [ + ("id", string(self.id)), + ("UUID", Tag::IntArray(self.uuid.to_vec())), + ("Pos", doubles(&[self.position.x, self.position.y, self.position.z])), + ("Motion", doubles(&[self.motion.x, self.motion.y, self.motion.z])), + ("Rotation", floats(&self.rotation)), + ("OnGround", Tag::Byte(i8::from(self.on_ground))), + ("fall_distance", Tag::Double(self.fall_distance.to_bits())), + ] { + put(&mut tag, key, value); + } + if let Tag::Compound(map) = &mut tag { + for (key, value) in [("Fire", Tag::Short(-1)), ("Air", Tag::Short(300)), ("Invulnerable", Tag::Byte(0)), ("PortalCooldown", Tag::Int(0))] { + map.entry(key.into()).or_insert(value); + } + } + tag + } +} + +/// `LivingEntity` + `Mob` (+ `AgeableMob`, creeper and skeleton specifics). +pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag { + let base = Base { + id: &m.kind.id, + uuid, + position: m.position, + motion: m.velocity, + rotation: [m.yaw, m.head_pitch], + on_ground: m.on_ground, + fall_distance: m.fall_distance, + }; + let mut tag = base.tag(raw); + for (key, value) in [ + ("Health", Tag::Float(m.health.max(0.0).to_bits())), + ("HurtTime", Tag::Short(m.hurt_time.clamp(0, i32::from(i16::MAX)) as i16)), + ("DeathTime", Tag::Short(m.death_time.clamp(0, i32::from(i16::MAX)) as i16)), + ("PersistenceRequired", Tag::Byte(i8::from(m.persistent))), + ] { + put(&mut tag, key, value); + } + let Tag::Compound(map) = &mut tag else { return tag }; + for (key, value) in [ + ("AbsorptionAmount", Tag::Float(0)), + ("FallFlying", Tag::Byte(0)), + ("CanPickUpLoot", Tag::Byte(0)), + ("LeftHanded", Tag::Byte(0)), + ("Brain", Tag::compound([("memories", Tag::Compound(Compound::new()))])), + ] { + map.entry(key.into()).or_insert(value); + } + let id = m.kind.id.as_str(); + if m.kind.category == "creature" { + for (key, value) in [("Age", Tag::Int(0)), ("ForcedAge", Tag::Int(0)), ("InLove", Tag::Int(0)), ("AgeLocked", Tag::Byte(0))] { + map.entry(key.into()).or_insert(value); + } + } + match id { + "minecraft:creeper" => { + for (key, value) in [("Fuse", Tag::Short(30)), ("ExplosionRadius", Tag::Byte(3)), ("powered", Tag::Byte(0)), ("ignited", Tag::Byte(0))] { + map.entry(key.into()).or_insert(value); + } + } + // Skeletons are spawned holding a bow (finalizeSpawn); a mob loaded + // from NBT keeps only its saved equipment. + "minecraft:skeleton" | "minecraft:stray" | "minecraft:bogged" => { + map.entry("equipment".into()).or_insert_with(|| Tag::compound([("mainhand", Tag::compound([("id", string("minecraft:bow")), ("count", Tag::Int(1))]))])); + } + "minecraft:zombie" => { + for (key, value) in [("IsBaby", Tag::Byte(0)), ("CanBreakDoors", Tag::Byte(0)), ("DrownedConversionTime", Tag::Int(-1)), ("InWaterTime", Tag::Int(-1))] { + map.entry(key.into()).or_insert(value); + } + } + "minecraft:sheep" => { + for (key, value) in [("Color", Tag::Byte(0)), ("Sheared", Tag::Byte(0))] { + map.entry(key.into()).or_insert(value); + } + } + _ => {} + } + tag +} +pub fn mob_from_nbt(tag: &Tag, types: &HashMap>, id: u32) -> Option { + let kind = types.get(&self::kind(tag)?)?.clone(); + let position = position(tag)?; + let [yaw, pitch] = rotation(tag); + let mut m = Mob::new(id, kind, position, yaw); + m.velocity = motion(tag); + m.head_pitch = pitch; + m.on_ground = flag(tag, "OnGround"); + m.fall_distance = float(tag.get("fall_distance")).unwrap_or(0.0); + if let Some(h) = float(tag.get("Health")) { + m.health = h as f32; + } + m.hurt_time = number(tag.get("HurtTime")).unwrap_or(0) as i32; + m.death_time = number(tag.get("DeathTime")).unwrap_or(0) as i32; + m.persistent = flag(tag, "PersistenceRequired"); + // A mob saved mid-death finishes dying; one saved dead is dropped. + if m.health <= 0.0 && m.death_time == 0 { + return None; + } + Some(m) +} + +/// `ItemEntity`: Health, Age, PickupDelay, Item. +pub fn item_entity_to_nbt(e: &ItemEntity, uuid: [i32; 4], raw: Option<&Tag>) -> Tag { + let base = Base { + id: "minecraft:item", + uuid, + position: e.position, + motion: e.velocity, + rotation: [0.0, 0.0], + on_ground: e.on_ground, + fall_distance: 0.0, + }; + let mut tag = base.tag(raw); + for (key, value) in [ + ("Item", item_to_nbt(&e.stack)), + ("Age", Tag::Short(e.age.min(i16::MAX as u32) as i16)), + ("PickupDelay", Tag::Short(e.pickup_delay.min(i16::MAX as u32) as i16)), + ] { + put(&mut tag, key, value); + } + if let Tag::Compound(map) = &mut tag { + map.entry("Health".into()).or_insert(Tag::Short(5)); + } + tag +} +pub fn item_entity_from_nbt(tag: &Tag, id: u32, bob_offset: f32) -> Option { + let stack = item_from_nbt(tag.get("Item")?)?; + let position = position(tag)?; + Some(ItemEntity { + id, + stack, + position, + previous: position, + velocity: motion(tag), + on_ground: flag(tag, "OnGround"), + // Age -32768 marks items that never despawn; Horizon counts upwards. + age: number(tag.get("Age")).unwrap_or(0).max(0) as u32, + pickup_delay: number(tag.get("PickupDelay")).unwrap_or(0).clamp(0, 32767) as u32, + bob_offset, + }) +} + +/// `ExperienceOrb`: Health, Age, Value, Count. +pub fn orb_to_nbt(o: &ExperienceOrb, uuid: [i32; 4]) -> Tag { + let base = Base { + id: "minecraft:experience_orb", + uuid, + position: o.position, + motion: o.velocity, + rotation: [0.0, 0.0], + on_ground: o.on_ground, + fall_distance: 0.0, + }; + let mut tag = base.tag(None); + for (key, value) in [ + ("Health", Tag::Short(5)), + ("Age", Tag::Short(o.age.min(i16::MAX as u32) as i16)), + ("Value", Tag::Short(o.value.min(i16::MAX as u32) as i16)), + ("Count", Tag::Int(1)), + ] { + put(&mut tag, key, value); + } + tag +} +/// One Horizon orb per counted orb (vanilla merges orbs of equal value). +pub fn orbs_from_nbt(tag: &Tag) -> Vec { + let Some(position) = position(tag) else { return Vec::new() }; + let count = number(tag.get("Count")).unwrap_or(1).clamp(1, 64) as usize; + let orb = ExperienceOrb { + position, + previous: position, + velocity: motion(tag), + value: number(tag.get("Value")).unwrap_or(1).max(1) as u32, + age: number(tag.get("Age")).unwrap_or(0).max(0) as u32, + on_ground: flag(tag, "OnGround"), + }; + vec![orb; count] +} + +/// `AbstractArrow`: damage, crit, inGround, life, pickup, item. +pub fn arrow_to_nbt(a: &Arrow, uuid: [i32; 4], owner: Option<[i32; 4]>) -> Tag { + let v = a.velocity; + let horizontal = (v.x * v.x + v.z * v.z).sqrt(); + let rotation = [(v.x.atan2(v.z).to_degrees()) as f32, (v.y.atan2(horizontal).to_degrees()) as f32]; + let base = Base { + id: "minecraft:arrow", + uuid, + position: a.position, + motion: a.velocity, + rotation, + on_ground: false, + fall_distance: 0.0, + }; + let mut tag = base.tag(None); + for (key, value) in [ + ("damage", Tag::Double(a.base_damage.to_bits())), + ("crit", Tag::Byte(i8::from(a.critical))), + ("inGround", Tag::Byte(i8::from(a.stuck))), + ("life", Tag::Short(if a.stuck { a.age.min(1200) as i16 } else { 0 })), + // Pickup: allowed for the player's arrows, disallowed for mobs'. + ("pickup", Tag::Byte(i8::from(a.from_player))), + ("shake", Tag::Byte(0)), + ("PierceLevel", Tag::Byte(0)), + ("HasBeenShot", Tag::Byte(1)), + ("SoundEvent", string("minecraft:entity.arrow.hit")), + ("item", Tag::compound([("id", string("minecraft:arrow")), ("count", Tag::Int(1))])), + ] { + put(&mut tag, key, value); + } + if let Some(owner) = owner { + put(&mut tag, "Owner", Tag::IntArray(owner.to_vec())); + } + tag +} +pub fn arrow_from_nbt(tag: &Tag, player: Option<[i32; 4]>) -> Option { + let position = position(tag)?; + let stuck = flag(tag, "inGround"); + Some(Arrow { + from_player: player.is_some() && uuid_from_tag(tag.get("Owner")) == player || number(tag.get("pickup")) == Some(1), + critical: flag(tag, "crit"), + position, + previous: position, + velocity: motion(tag), + stuck, + age: if stuck { number(tag.get("life")).unwrap_or(0).max(0) as u32 } else { 0 }, + base_damage: float(tag.get("damage")).unwrap_or(2.0), + }) +} + +/// `FallingBlockEntity`: BlockState, Time, DropItem, HurtEntities. +pub fn falling_block_to_nbt(state: Tag, position: Vec3, motion: Vec3, time: u32, uuid: [i32; 4]) -> Tag { + let base = Base { + id: "minecraft:falling_block", + uuid, + position, + motion, + rotation: [0.0, 0.0], + on_ground: false, + fall_distance: 0.0, + }; + let mut tag = base.tag(None); + for (key, value) in [ + ("BlockState", state), + ("Time", Tag::Int(time as i32)), + ("DropItem", Tag::Byte(1)), + ("HurtEntities", Tag::Byte(0)), + ("CancelDrop", Tag::Byte(0)), + ("FallHurtAmount", Tag::Float(0)), + ("FallHurtMax", Tag::Int(40)), + ] { + put(&mut tag, key, value); + } + tag +} +/// (block state tag, position, motion, time) +pub fn falling_block_from_nbt(tag: &Tag) -> Option<(Tag, Vec3, Vec3, u32)> { + Some((tag.get("BlockState")?.clone(), position(tag)?, motion(tag), number(tag.get("Time")).unwrap_or(0).max(0) as u32)) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::items::ItemStack; + + #[test] + fn item_entities_round_trip() { + let e = ItemEntity { + id: 7, + stack: ItemStack { item: Arc::from("minecraft:diamond_pickaxe"), count: 1, damage: 5 }, + position: Vec3::new(100.5, 64.0, -31.5), + previous: Vec3::ZERO, + velocity: Vec3::new(0.0, -0.04, 0.0), + on_ground: true, + age: 120, + pickup_delay: 10, + bob_offset: 0.0, + }; + let tag = item_entity_to_nbt(&e, [1, 2, 3, 4], None); + assert_eq!(kind(&tag).as_deref(), Some("minecraft:item")); + assert_eq!(uuid(&tag), Some([1, 2, 3, 4])); + let back = item_entity_from_nbt(&tag, 9, 0.0).unwrap(); + assert_eq!(back.stack.damage, 5); + assert_eq!((back.age, back.pickup_delay, back.on_ground), (120, 10, true)); + assert_eq!(back.position.x, 100.5); + } + #[test] + fn unknown_fields_survive() { + let raw = Tag::compound([("id", string("minecraft:item")), ("CustomName", string("x")), ("Pos", doubles(&[0.5, 64.0, 0.5])), ("Item", Tag::compound([("id", string("minecraft:stone")), ("count", Tag::Int(3))]))]); + let e = item_entity_from_nbt(&raw, 1, 0.0).unwrap(); + let tag = item_entity_to_nbt(&e, [0; 4], Some(&raw)); + assert_eq!(text(tag.get("CustomName")).as_deref(), Some("x")); + assert_eq!(number(tag.get("Health")), Some(5)); + } + #[test] + fn orbs_expand_their_count() { + let o = ExperienceOrb { position: Vec3::new(1.0, 2.0, 3.0), previous: Vec3::ZERO, velocity: Vec3::ZERO, value: 7, age: 3, on_ground: false }; + let mut tag = orb_to_nbt(&o, [0; 4]); + put(&mut tag, "Count", Tag::Int(3)); + let back = orbs_from_nbt(&tag); + assert_eq!(back.len(), 3); + assert_eq!(back[0].value, 7); + } +} diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index b4cf33c03..6fc6dc311 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1,6 +1,7 @@ //! The playable game loop: streaming world, player simulation at 20 ticks/s, //! block interaction, inventory and HUD. mod control; +mod persist; use horizon_client::{ Result, assets::Assets, @@ -387,6 +388,7 @@ struct Game<'a> { /// Control replies waiting for a rendered frame or a benchmark deadline. pending_screenshot: Option>, pending_bench: Option<(Instant, std::sync::mpsc::Sender)>, + persist: persist::EntityMemory, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum Dim { @@ -403,13 +405,6 @@ impl Dim { fn from_id(id: &str) -> Self { if id == "minecraft:the_nether" { Self::Nether } else { Self::Overworld } } - /// Region folder inside the world (vanilla's DIM-1 for the Nether). - fn folder(self) -> Option<&'static str> { - match self { - Self::Overworld => None, - Self::Nether => Some("DIM-1"), - } - } } fn facing_offset(facing: &str) -> [i32; 3] { match facing { @@ -700,7 +695,7 @@ impl Game<'_> { fn sync_tick_chunks(&mut self) { let loaded: HashSet<(i32, i32)> = self.world.chunks.keys().copied().collect(); for k in loaded.difference(&self.tick_chunks.clone()) { - let _ = self.block_ticks.add_empty_chunk(horizon_core::position::ChunkPos::new(k.0, k.1)); + self.register_chunk_ticks(*k); } for k in self.tick_chunks.difference(&loaded) { self.block_ticks.remove_chunk(horizon_core::position::ChunkPos::new(k.0, k.1)); @@ -1374,6 +1369,7 @@ impl Game<'_> { self.tick_using(); self.tick_mining()?; self.tick_items(); + self.spawn_loaded_entities(); self.tick_mobs()?; self.tick_experience(); self.tick_hand(); @@ -2586,6 +2582,7 @@ impl Game<'_> { .filter(|(p, _)| p.0 >> 4 == c.x && p.2 >> 4 == c.z) .map(|(p, e)| (*p, e.clone())) .collect(); + self.attach_save_data(&mut c); Arc::new(c) } fn tick_mobs(&mut self) -> Result<()> { @@ -2852,6 +2849,8 @@ impl Game<'_> { /// Load (or create) a world folder under saves/ and start streaming it. fn open_world(&mut self, folder: &str, name: Option<&str>, seed: Option, creative: bool) -> Result<()> { let save_root = std::env::current_dir()?.join("saves").join(folder); + horizon_client::save::migrate_legacy(&save_root)?; + self.persist = Default::default(); let level = Level::read(&save_root).unwrap_or_else(|| Level { name: name.unwrap_or(folder).to_owned(), // WorldOptions.randomSeed when no seed was given. @@ -2950,11 +2949,7 @@ impl Game<'_> { } /// Streaming and region storage for one dimension of a world. fn make_streaming(&self, world_root: &std::path::Path, dim: Dim, seed: i64) -> Result<(Streaming, Arc)> { - let region_root = match dim.folder() { - Some(f) => world_root.join(f), - None => world_root.to_owned(), - }; - let save = Arc::new(SaveDir::new(region_root)); + let save = Arc::new(SaveDir::new(horizon_client::save::dimension_dir(world_root, dim.id()))); let res = &*self.res; let mut streaming = if self.options.test_terrain && dim == Dim::Overworld { let inner = Arc::new(TestTerrain::new(res.assets.registry, &res.biomes)?); @@ -2978,6 +2973,7 @@ impl Game<'_> { let keys: Vec<_> = self.world.modified.drain().collect(); let chunks: Vec<_> = keys.iter().filter_map(|k| self.world.chunks.get(k)).map(|c| self.with_entities(c)).collect(); self.save.save(&chunks, &self.res.assets.registry, &self.res.biomes)?; + self.save_dimension_entities()?; // Coordinates scale by 8 between the Overworld and the Nether. let scale = match (self.dim, target) { (Dim::Overworld, Dim::Nether) => 1.0 / 8.0, @@ -4080,6 +4076,8 @@ impl Game<'_> { .map(|c| self.with_entities(c)) .collect(); self.save.save(&chunks, &self.res.assets.registry, &self.res.biomes)?; + self.save_entities()?; + self.level.selected_slot = self.selected as i32; let level = &mut self.level; level.creative = self.creative; level.time = self.world.game_time; @@ -4318,6 +4316,7 @@ impl Game<'_> { .cloned() .collect(); let modified: Vec<_> = dirty.iter().map(|c| self.with_entities(c)).collect(); + self.unload_entities(&unloaded); for c in &unloaded { self.world.block_entities.retain(|p, _| !(p.0 >> 4 == c.x && p.2 >> 4 == c.z)); } @@ -4803,6 +4802,7 @@ pub fn run() -> Result<()> { frame_stats: Default::default(), pending_screenshot: None, pending_bench: None, + persist: Default::default(), }; if let Some(path) = &game.options.control { let server = horizon_client::control::Server::start(path) diff --git a/crates/horizon-client/src/game/persist.rs b/crates/horizon-client/src/game/persist.rs new file mode 100644 index 000000000..592bbe34e --- /dev/null +++ b/crates/horizon-client/src/game/persist.rs @@ -0,0 +1,261 @@ +//! Saving and loading the game state that lives outside chunk blocks: +//! entities (`entities/*.mca`, per chunk like vanilla's `EntityStorage`) and +//! scheduled block/fluid ticks (chunk `block_ticks`/`fluid_ticks`). +use super::{FallingBlock, Game}; +use horizon_client::{ + Result, + chunk::Chunk, + entity_nbt, + save::{ChunkExtras, random_uuid}, +}; +use horizon_core::{ + geometry::Vec3, + position::ChunkPos, + random::RandomSource, + ticks::ChunkTicks, +}; +use horizon_world::{chunk::BlockState, nbt::Tag, registry::BlockStateId}; +use std::{ + collections::{HashMap, HashSet}, + sync::Arc, +}; + +/// What Horizon remembers about loaded entities to save them faithfully. +#[derive(Default)] +pub struct EntityMemory { + /// UUIDs of mobs and item entities by Horizon entity ID. + uuids: HashMap, + /// The compound each mob or item entity was loaded with. + raw: HashMap, + /// Entities of types Horizon does not simulate, kept per chunk and + /// written back unchanged. + unknown: HashMap<(i32, i32), Vec>, +} + +fn chunk_of(p: Vec3) -> (i32, i32) { + ((p.x.floor() as i32) >> 4, (p.z.floor() as i32) >> 4) +} + +impl Game<'_> { + /// Saved ticks and the save time for a chunk about to be written. + pub(super) fn attach_save_data(&self, chunk: &mut Chunk) { + let pos = ChunkPos::new(chunk.x, chunk.z); + let time = self.world.game_time; + let mut extras: ChunkExtras = chunk.saved.as_deref().cloned().unwrap_or_default(); + if let Some(t) = self.block_ticks.blocks.chunk(pos) { + extras.block_ticks = t.pack(time); + } else { + extras.block_ticks.clear(); + } + if let Some(t) = self.block_ticks.fluids.chunk(pos) { + extras.fluid_ticks = t.pack(time); + } else { + extras.fluid_ticks.clear(); + } + extras.last_update = time; + chunk.saved = Some(Arc::new(extras)); + } + /// Register a newly resident chunk with the tick scheduler, with the + /// block and fluid ticks it was saved with. + pub(super) fn register_chunk_ticks(&mut self, key: (i32, i32)) { + let pos = ChunkPos::new(key.0, key.1); + let saved = self.world.chunks.get(&key).and_then(|c| c.saved.clone()); + let (blocks, fluids) = saved.as_deref().map_or((Vec::new(), Vec::new()), |s| (s.block_ticks.clone(), s.fluid_ticks.clone())); + let loaded = !blocks.is_empty() || !fluids.is_empty(); + if self.block_ticks.add_chunk(pos, ChunkTicks::from_saved(blocks), ChunkTicks::from_saved(fluids)).is_ok() && loaded { + let time = self.world.game_time; + self.block_ticks.blocks.unpack_chunk(pos, time); + self.block_ticks.fluids.unpack_chunk(pos, time); + } + // The saved list is consumed: later saves take the scheduler's ticks. + if loaded && let Some(c) = self.world.chunks.get_mut(&key) { + let c = Arc::make_mut(c); + if let Some(s) = c.saved.as_mut() { + let s = Arc::make_mut(s); + s.block_ticks.clear(); + s.fluid_ticks.clear(); + } + } + } + /// Spawn the entities of chunks that finished loading. + pub(super) fn spawn_loaded_entities(&mut self) { + let loaded = std::mem::take(&mut self.world.loaded_entities); + let player_uuid = self.level.player_uuid; + for (key, tags) in loaded { + for tag in tags { + let kind = entity_nbt::kind(&tag).unwrap_or_default(); + let uuid = entity_nbt::uuid(&tag); + match kind.as_str() { + "minecraft:item" => { + self.next_entity += 1; + let bob = self.world.random.next_float() * std::f32::consts::PI * 2.0; + if let Some(e) = entity_nbt::item_entity_from_nbt(&tag, self.next_entity, bob) { + self.remember(e.id, uuid, tag); + self.dropped.push(e); + } + } + "minecraft:experience_orb" => self.orbs.extend(entity_nbt::orbs_from_nbt(&tag)), + "minecraft:arrow" => { + if let Some(a) = entity_nbt::arrow_from_nbt(&tag, player_uuid) { + self.arrows.push(a); + } + } + "minecraft:falling_block" => { + if let Some((state, position, velocity, age)) = entity_nbt::falling_block_from_nbt(&tag) + && let Some(state) = self.state_from_nbt(&state) + { + self.falling.push(FallingBlock { state, position, previous: position, velocity, age }); + } + } + _ if self.res.mob_types.contains_key(&kind) => { + self.next_entity += 1; + if let Some(m) = entity_nbt::mob_from_nbt(&tag, &self.res.mob_types, self.next_entity) { + self.remember(m.id, uuid, tag); + self.mobs.push(m); + } + } + _ => self.persist.unknown.entry(key).or_default().push(tag), + } + } + } + } + fn remember(&mut self, id: u32, uuid: Option<[i32; 4]>, tag: Tag) { + if let Some(u) = uuid { + self.persist.uuids.insert(id, u); + } + self.persist.raw.insert(id, tag); + } + fn uuid_for(&mut self, id: u32) -> [i32; 4] { + *self.persist.uuids.entry(id).or_insert_with(random_uuid) + } + /// `BlockState.CODEC`: a block name or `{id, properties}` (or legacy `{Name, Properties}`). + fn state_from_nbt(&self, tag: &Tag) -> Option { + let registry = self.res.assets.registry; + let state = match tag.get("Name") { + Some(Tag::String(name)) => BlockState { + name: name.to_string_lossy(), + properties: match tag.get("Properties") { + Some(Tag::Compound(p)) => p + .iter() + .filter_map(|(k, v)| match v { + Tag::String(v) => Some((k.to_string_lossy(), v.to_string_lossy())), + _ => None, + }) + .collect(), + _ => Default::default(), + }, + }, + _ => BlockState::from_nbt(tag).ok()?, + }; + registry.id(&state).or_else(|| registry.default_state(&state.name)) + } + fn state_to_nbt(&self, state: BlockStateId) -> Tag { + let registry = self.res.assets.registry; + match registry.state(state) { + Some(s) if registry.default_state(&s.name) == Some(state) => Tag::String(s.name.as_str().into()), + Some(s) => s.to_nbt(), + None => Tag::String("minecraft:air".into()), + } + } + /// The entity NBT of every simulated entity in `keys`, plus the unknown + /// entities kept for them. With `remove`, the entities leave the world + /// (their chunks unload). + fn entity_chunks(&mut self, keys: &HashSet<(i32, i32)>, remove: bool) -> Vec<((i32, i32), Vec)> { + let mut out: HashMap<(i32, i32), Vec> = keys.iter().map(|k| (*k, Vec::new())).collect(); + let player_uuid = self.level.player_uuid; + let mobs: Vec = (0..self.mobs.len()).filter(|&i| keys.contains(&chunk_of(self.mobs[i].position)) && self.mobs[i].alive()).collect(); + for i in mobs { + let id = self.mobs[i].id; + let uuid = self.uuid_for(id); + let tag = entity_nbt::mob_to_nbt(&self.mobs[i], uuid, self.persist.raw.get(&id)); + out.entry(chunk_of(self.mobs[i].position)).or_default().push(tag); + } + let items: Vec = (0..self.dropped.len()).filter(|&i| keys.contains(&chunk_of(self.dropped[i].position))).collect(); + for i in items { + let id = self.dropped[i].id; + let uuid = self.uuid_for(id); + let tag = entity_nbt::item_entity_to_nbt(&self.dropped[i], uuid, self.persist.raw.get(&id)); + out.entry(chunk_of(self.dropped[i].position)).or_default().push(tag); + } + for o in &self.orbs { + if keys.contains(&chunk_of(o.position)) { + out.entry(chunk_of(o.position)).or_default().push(entity_nbt::orb_to_nbt(o, random_uuid())); + } + } + for a in &self.arrows { + if keys.contains(&chunk_of(a.position)) { + let owner = if a.from_player { player_uuid } else { None }; + out.entry(chunk_of(a.position)).or_default().push(entity_nbt::arrow_to_nbt(a, random_uuid(), owner)); + } + } + for f in &self.falling { + if keys.contains(&chunk_of(f.position)) { + let tag = entity_nbt::falling_block_to_nbt(self.state_to_nbt(f.state), f.position, f.velocity, f.age, random_uuid()); + out.entry(chunk_of(f.position)).or_default().push(tag); + } + } + for key in keys { + let unknown = if remove { self.persist.unknown.remove(key) } else { self.persist.unknown.get(key).cloned() }; + if let Some(list) = unknown { + out.entry(*key).or_default().extend(list); + } + } + if remove { + let gone = |p: Vec3| keys.contains(&chunk_of(p)); + let mut removed: Vec = Vec::new(); + self.mobs.retain(|m| { + let keep = !gone(m.position); + if !keep { + removed.push(m.id); + } + keep + }); + self.dropped.retain(|e| { + let keep = !gone(e.position); + if !keep { + removed.push(e.id); + } + keep + }); + self.orbs.retain(|o| !gone(o.position)); + self.arrows.retain(|a| !gone(a.position)); + self.falling.retain(|f| !gone(f.position)); + for id in removed { + self.persist.uuids.remove(&id); + self.persist.raw.remove(&id); + } + } + out.into_iter().collect() + } + /// Write the entities of every resident chunk (an autosave or quit). + pub(super) fn save_entities(&mut self) -> Result<()> { + self.spawn_loaded_entities(); + let keys: HashSet<(i32, i32)> = self.world.chunks.keys().copied().collect(); + let chunks = self.entity_chunks(&keys, false); + // Forget entities that no longer exist. + let alive: HashSet = self.mobs.iter().map(|m| m.id).chain(self.dropped.iter().map(|e| e.id)).collect(); + self.persist.uuids.retain(|id, _| alive.contains(id)); + self.persist.raw.retain(|id, _| alive.contains(id)); + self.save.save_entities(chunks) + } + /// Save and remove the entities of chunks that unloaded. + pub(super) fn unload_entities(&mut self, unloaded: &[Arc]) { + if unloaded.is_empty() { + return; + } + self.spawn_loaded_entities(); + let keys: HashSet<(i32, i32)> = unloaded.iter().map(|c| (c.x, c.z)).collect(); + let chunks = self.entity_chunks(&keys, true); + if let Err(e) = self.save.save_entities(chunks) { + eprintln!("Horizon: failed to save entities: {e}"); + } + } + /// Leaving a dimension: its entities are saved with it. + pub(super) fn save_dimension_entities(&mut self) -> Result<()> { + self.spawn_loaded_entities(); + let keys: HashSet<(i32, i32)> = self.world.chunks.keys().copied().collect(); + let chunks = self.entity_chunks(&keys, true); + self.persist = EntityMemory::default(); + self.save.save_entities(chunks) + } +} diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 00265cade..ae1b1c5c6 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -14,6 +14,7 @@ pub mod crafting; pub mod creative; pub mod collisions; pub mod entities; +pub mod entity_nbt; pub mod environment; pub mod gfx; pub mod health; diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index 6997be4f8..b29300882 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -1,29 +1,47 @@ -//! World persistence in vanilla's Anvil layout: `region/r.X.Z.mca` files with -//! zlib-compressed chunk NBT (paletted `block_states`/`biomes` per section) and -//! a gzip `level.dat`. Light is not stored (`isLightOn: 0`), so both Horizon and -//! vanilla relight saved chunks on load. +//! World persistence in the vanilla 26.3 layout, readable by both Horizon and +//! the vanilla game: +//! - `level.dat` (`Data`: name, game type, time, spawn, difficulty, version) +//! plus the saved-data files `data/minecraft/{weather,world_clocks, +//! world_gen_settings,game_rules}.dat`; +//! - the player in `players/data/.dat` (`singleplayer_uuid`); +//! - per dimension `dimensions/minecraft//region/r.X.Z.mca` (chunks: +//! paletted sections with light, heightmaps, block entities, scheduled +//! block/fluid ticks) and `entities/r.X.Z.mca` (entities, `entity_nbt`). +//! +//! Data Horizon does not simulate (structure references, unknown block +//! entities and entities, player fields such as the ender chest or effects, +//! game rules) is kept from the loaded files and written back unchanged. +//! Worlds saved by older Horizon builds (`region/`, `DIM-1/`, `Player` in +//! `level.dat`) are migrated on open. use crate::{ Result, biome::Biomes, - chunk::{Blocks, Chunk, MIN_Y, SECTIONS}, + chunk::{Blocks, Chunk, Light, MIN_Y, SECTIONS}, streaming::ChunkGenerator, }; -use horizon_core::position::ChunkPos; +use horizon_core::{ + position::{BlockPos, ChunkPos}, + ticks::{SavedTick, TickPriority, TickTypeId}, +}; use horizon_world::{ DATA_VERSION, chunk::BlockState, compression::Compression, - nbt::{self, Limits, NamedTag, Tag}, + heightmap::{Heightmap, HeightmapChunk, HeightmapType}, + nbt::{self, Compound, JavaString, Limits, NamedTag, Tag}, region::{ChunkRecord, RegionFile, RegionWriter}, registry::{BlockRegistry, BlockStateId}, + worldgen::terrain::TerrainBlockStates, }; use std::{ collections::{BTreeMap, HashMap}, fs, path::{Path, PathBuf}, - sync::{Arc, Mutex}, + sync::{Arc, Mutex, OnceLock}, }; +pub const VERSION_NAME: &str = "26.3"; + fn pack(indices: &[u16], bits: u32) -> Vec { let per_long = (64 / bits) as usize; let mut longs = vec![0i64; indices.len().div_ceil(per_long)]; @@ -49,79 +67,269 @@ fn bits_for(len: usize, min: u32) -> u32 { fn string(s: &str) -> Tag { Tag::String(s.into()) } +pub(crate) fn text(tag: Option<&Tag>) -> Option { + match tag { + Some(Tag::String(s)) => Some(s.to_string_lossy()), + _ => None, + } +} +/// Any numeric tag as i64 (vanilla's `getIntOr` etc. accept every number type). +pub(crate) fn number(tag: Option<&Tag>) -> Option { + Some(match tag? { + Tag::Byte(v) => i64::from(*v), + Tag::Short(v) => i64::from(*v), + Tag::Int(v) => i64::from(*v), + Tag::Long(v) => *v, + Tag::Float(b) => f32::from_bits(*b) as i64, + Tag::Double(b) => f64::from_bits(*b) as i64, + _ => return None, + }) +} +pub(crate) fn float(tag: Option<&Tag>) -> Option { + Some(match tag? { + Tag::Float(b) => f64::from(f32::from_bits(*b)), + Tag::Double(b) => f64::from_bits(*b), + other => number(Some(other))? as f64, + }) +} +/// Insert or replace a key of a compound tag. +pub(crate) fn put(tag: &mut Tag, key: &str, value: Tag) { + if let Tag::Compound(map) = tag { + map.insert(key.into(), value); + } +} +fn compound_or_empty(tag: Option) -> Tag { + match tag { + Some(t @ Tag::Compound(_)) => t, + _ => Tag::Compound(Compound::new()), + } +} -fn encode_items(slots: &[Option]) -> Tag { +/// `dimensions//` inside a world folder. +pub fn dimension_dir(world: &Path, dimension: &str) -> PathBuf { + let (namespace, path) = dimension.split_once(':').unwrap_or(("minecraft", dimension)); + world.join("dimensions").join(namespace).join(path) +} +/// Move a world saved by an older Horizon build (pre-26.3 layout with a +/// DataVersion that keeps vanilla from upgrading it) into the 26.3 layout. +pub fn migrate_legacy(world: &Path) -> Result<()> { + for (old, dimension) in [("", "minecraft:overworld"), ("DIM-1", "minecraft:the_nether"), ("DIM1", "minecraft:the_end")] { + let from = if old.is_empty() { world.to_owned() } else { world.join(old) }; + let to = dimension_dir(world, dimension); + for folder in ["region", "entities", "poi"] { + let (src, dst) = (from.join(folder), to.join(folder)); + if src.is_dir() && !dst.exists() { + fs::create_dir_all(&to)?; + fs::rename(&src, &dst)?; + } + } + if !old.is_empty() && from.is_dir() && fs::read_dir(&from)?.next().is_none() { + let _ = fs::remove_dir(&from); + } + } + Ok(()) +} + +/// Registry lookups shared by chunk encoding and decoding. +pub struct SaveTables { + /// Block registry IDs (`BlockId`, scheduled tick types) in registry order. + block_names: Vec, + block_ids: HashMap, + /// Default state of each state's block, to write vanilla's short palette form. + default_state: Vec, + block_entity: Vec, + heightmaps: crate::assets::Properties, + air: u16, +} +impl SaveTables { + pub fn new(assets: &crate::assets::Assets) -> Self { + let registry = assets.registry; + let mut block_names: Vec = Vec::new(); + let mut default_state = Vec::with_capacity(registry.len()); + for id in 0..registry.len() { + let state = registry.state(BlockStateId(id as u32)).expect("registry state"); + if block_names.last() != Some(&state.name) { + block_names.push(state.name.clone()); + } + default_state.push(registry.default_state(&state.name) == Some(BlockStateId(id as u32))); + } + let block_ids = block_names.iter().enumerate().map(|(i, n)| (n.clone(), i as u16)).collect(); + Self { + block_names, + block_ids, + default_state, + block_entity: Vec::new(), + heightmaps: assets.properties.clone(), + air: registry.default_state("minecraft:air").map_or(0, |s| s.0 as u16), + } + } + /// Which states carry a block entity (`BlockFacts`), to drop entries for + /// unknown block entities whose block is gone. + pub fn with_block_entities(mut self, facts: &crate::blocks::BlockFacts) -> Self { + self.block_entity = facts.states.iter().map(|s| s.block_entity).collect(); + self + } +} + +/// Save data loaded with a chunk that Horizon keeps but does not simulate. +#[derive(Clone, Debug, Default)] +pub struct ChunkExtras { + /// Chunk root keys written back as loaded (structures, PostProcessing, + /// InhabitedTime, blending data, ...). + pub root: Compound, + /// Block entities Horizon does not model. + pub block_entities: Vec, + /// Scheduled ticks (`block_ticks`/`fluid_ticks`): delays relative to the + /// save time. Registered with the level's tick scheduler on arrival and + /// replaced with the scheduler's pending ticks when saving. + pub block_ticks: Vec, + pub fluid_ticks: Vec, + /// Game time written as `LastUpdate`. + pub last_update: i64, +} + +fn tick_list(ticks: &[SavedTick], name: impl Fn(u32) -> Option) -> Tag { + Tag::List( + ticks + .iter() + .filter_map(|t| { + Some(Tag::compound([ + ("i", string(&name(t.kind.0)?)), + ("x", Tag::Int(t.pos.x)), + ("y", Tag::Int(t.pos.y)), + ("z", Tag::Int(t.pos.z)), + ("t", Tag::Int(t.delay)), + ("p", Tag::Int(t.priority as i32)), + ])) + }) + .collect(), + ) +} +fn read_ticks(tag: Option<&Tag>, chunk: (i32, i32), id: impl Fn(&str) -> Option) -> Vec { + let Some(Tag::List(list)) = tag else { return Vec::new() }; + list.iter() + .filter_map(|t| { + let kind = id(&text(t.get("i"))?)?; + let pos = BlockPos::new(number(t.get("x"))? as i32, number(t.get("y"))? as i32, number(t.get("z"))? as i32); + // SavedTick.filterTickListForChunk + if (pos.x >> 4, pos.z >> 4) != chunk { + return None; + } + Some(SavedTick { + kind: TickTypeId(kind), + pos, + delay: number(t.get("t")).unwrap_or(0) as i32, + priority: TickPriority::by_value(number(t.get("p")).unwrap_or(0) as i32), + }) + }) + .collect() +} +fn fluid_name(kind: u32) -> Option { + use horizon_world::fluid::FluidKind; + let fluid = *FluidKind::ALL.get(kind as usize)?; + Some( + match fluid { + FluidKind::Empty => return None, + FluidKind::FlowingWater => "minecraft:flowing_water", + FluidKind::Water => "minecraft:water", + FluidKind::FlowingLava => "minecraft:flowing_lava", + FluidKind::Lava => "minecraft:lava", + } + .to_owned(), + ) +} +fn fluid_id(name: &str) -> Option { + use horizon_world::fluid::FluidKind; + Some(match name { + "minecraft:flowing_water" => FluidKind::FlowingWater, + "minecraft:water" => FluidKind::Water, + "minecraft:flowing_lava" => FluidKind::FlowingLava, + "minecraft:lava" => FluidKind::Lava, + _ => return None, + } as u32) +} + +fn encode_item(stack: &crate::items::ItemStack) -> Vec<(&'static str, Tag)> { + let mut entries = vec![("id", string(&stack.item)), ("count", Tag::Int(stack.count as i32))]; + if stack.damage > 0 { + entries.push(("components", Tag::compound([("minecraft:damage", Tag::Int(stack.damage as i32))]))); + } + entries +} +/// ItemStack.CODEC (id, count, components). +pub fn item_to_nbt(stack: &crate::items::ItemStack) -> Tag { + Tag::compound(encode_item(stack)) +} +pub fn item_from_nbt(tag: &Tag) -> Option { + let id = text(tag.get("id"))?; + if id == "minecraft:air" { + return None; + } + let count = number(tag.get("count")).or_else(|| number(tag.get("Count"))).unwrap_or(1).max(1) as u32; + let damage = number(tag.get("components").and_then(|c| c.get("minecraft:damage"))).unwrap_or(0).max(0) as u32; + Some(crate::items::ItemStack { item: Arc::from(id.as_str()), count, damage }) +} +pub fn encode_items(slots: &[Option]) -> Tag { Tag::List( slots .iter() .enumerate() .filter_map(|(i, s)| { - let s = s.as_ref()?; - let mut entries = vec![ - ("Slot", Tag::Byte(i as i8)), - ("id", string(&s.item)), - ("count", Tag::Int(s.count as i32)), - ]; - if s.damage > 0 { - entries.push(("components", Tag::compound([("minecraft:damage", Tag::Int(s.damage as i32))]))); - } + let mut entries = vec![("Slot", Tag::Byte(i as i8))]; + entries.extend(encode_item(s.as_ref()?)); Some(Tag::compound(entries)) }) .collect(), ) } -fn decode_items(tag: Option<&Tag>, size: usize) -> Vec> { +pub fn decode_items(tag: Option<&Tag>, size: usize) -> Vec> { let mut slots = vec![None; size]; if let Some(Tag::List(items)) = tag { for it in items { - let Some(Tag::Byte(slot)) = it.get("Slot") else { continue }; - let Some(Tag::String(id)) = it.get("id") else { continue }; - let count = match it.get("count") { - Some(Tag::Int(c)) => *c as u32, - Some(Tag::Byte(c)) => *c as u32, - _ => 1, - }; - let damage = match it.get("components").and_then(|c| c.get("minecraft:damage")) { - Some(Tag::Int(d)) => *d as u32, - _ => 0, - }; - if let Some(cell) = slots.get_mut(*slot as u8 as usize) { - *cell = Some(crate::items::ItemStack { item: Arc::from(id.to_string_lossy().as_str()), count, damage }); + let slot = number(it.get("Slot")).unwrap_or(0) as u8 as usize; + if let Some(cell) = slots.get_mut(slot) { + *cell = item_from_nbt(it); } } } slots } -fn encode_block_entity(p: (i32, i32, i32), e: &crate::containers::BlockEntity) -> Tag { +/// The block entity type ID for the block it sits in (a chest-like container +/// is a chest, trapped chest or barrel). +fn block_entity_id(e: &crate::containers::BlockEntity, block: &str) -> String { + match (e, block) { + (crate::containers::BlockEntity::Chest(_), "minecraft:trapped_chest" | "minecraft:barrel") => block.to_owned(), + _ => e.id().to_owned(), + } +} +fn encode_block_entity(p: (i32, i32, i32), e: &crate::containers::BlockEntity, block: &str) -> Tag { use crate::containers::BlockEntity; let mut entries = vec![ - ("id", string(e.id())), + ("id", string(&block_entity_id(e, block))), ("x", Tag::Int(p.0)), ("y", Tag::Int(p.1)), ("z", Tag::Int(p.2)), ("keepPacked", Tag::Byte(0)), + ("components", Tag::Compound(Compound::new())), ]; match e { BlockEntity::Chest(items) => entries.push(("Items", encode_items(items))), BlockEntity::Furnace(f) => { entries.push(("Items", encode_items(&f.items))); - entries.push(("lit_time_remaining", Tag::Short(f.lit_remaining.clamp(0, i32::from(i16::MAX)) as i16))); - entries.push(("lit_total_time", Tag::Short(f.lit_total.clamp(0, i32::from(i16::MAX)) as i16))); - entries.push(("cooking_time_spent", Tag::Short(f.cooking_timer as i16))); - entries.push(("cooking_total_time", Tag::Short(f.cooking_total as i16))); + entries.push(("lit_time_remaining", Tag::Int(f.lit_remaining))); + entries.push(("lit_total_time", Tag::Int(f.lit_total))); + entries.push(("cooking_time_spent", Tag::Int(f.cooking_timer))); + entries.push(("cooking_total_time", Tag::Int(f.cooking_total))); + entries.push(("speed_multiplier", Tag::Float(f.speed.to_bits()))); + entries.push(("RecipesUsed", Tag::Compound(Compound::new()))); } } Tag::compound(entries) } fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::containers::BlockEntity)> { use crate::containers::BlockEntity; - let int = |k: &str| match tag.get(k) { - Some(Tag::Int(v)) => Some(*v), - Some(Tag::Short(v)) => Some(i32::from(*v)), - _ => None, - }; - let Some(Tag::String(id)) = tag.get("id") else { return None }; - let id = id.to_string_lossy(); + let int = |k: &str| number(tag.get(k)).map(|v| v as i32); + let id = text(tag.get("id"))?; let pos = (int("x")?, int("y")?, int("z")?); let mut entity = BlockEntity::for_block(&id)?; match &mut entity { @@ -133,12 +341,76 @@ fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::containers: f.lit_total = int("lit_total_time").unwrap_or(0); f.cooking_timer = int("cooking_time_spent").unwrap_or(0); f.cooking_total = int("cooking_total_time").unwrap_or(200); + if let Some(speed) = float(tag.get("speed_multiplier")) { + f.speed = speed as f32; + } } } Some((pos, entity)) } + +struct HeightmapView<'a> { + chunk: &'a Chunk, + tables: &'a SaveTables, +} +impl HeightmapChunk for HeightmapView<'_> { + type State = crate::assets::StateFacts; + fn min_y(&self) -> i32 { + MIN_Y + } + fn height(&self) -> i32 { + SECTIONS as i32 * 16 + } + fn highest_section_y(&self) -> i32 { + let top = self.chunk.sections.iter().rposition(|s| !s.is_uniform(self.tables.air)).unwrap_or(0); + MIN_Y + top as i32 * 16 + } + fn block_state(&self, x: i32, y: i32, z: i32) -> Self::State { + self.tables.heightmaps.heightmap_state(self.chunk.block(x, y, z)) + } +} +/// Heightmaps kept after world generation (`ChunkStatus.FULL.heightmapsAfter`). +fn heightmaps(chunk: &Chunk, tables: &SaveTables) -> Tag { + let kinds = [HeightmapType::WorldSurface, HeightmapType::OceanFloor, HeightmapType::MotionBlocking, HeightmapType::MotionBlockingNoLeaves]; + let mut maps: Vec = kinds.iter().filter_map(|k| Heightmap::new(*k, MIN_Y, SECTIONS as i32 * 16).ok()).collect(); + let view = HeightmapView { chunk, tables }; + let mut refs: Vec<&mut Heightmap> = maps.iter_mut().collect(); + if Heightmap::prime(&view, &mut refs).is_err() { + return Tag::Compound(Compound::new()); + } + Tag::compound(maps.iter().map(|m| (m.kind().name(), Tag::LongArray(m.raw_data().iter().map(|&w| w as i64).collect())))) +} +/// Vanilla light layer (nibbles, index y<<8|z<<4|x, low nibble first). +fn nibbles(light: &Light, sky: bool) -> Vec { + let mut out = vec![0u8; 2048]; + for (i, byte) in out.iter_mut().enumerate() { + let value = |j: usize| { + let v = match light { + Light::Uniform(v) => *v, + Light::Full(a) => a[j], + }; + if sky { v >> 4 } else { v & 15 } + }; + *byte = value(2 * i) | value(2 * i + 1) << 4; + } + out +} + pub fn encode(chunk: &Chunk, registry: &BlockRegistry, biomes: &Biomes) -> NamedTag { + encode_with(chunk, registry, biomes, None, true) +} +/// The chunk NBT vanilla's `SerializableChunkData.write` produces for a full +/// chunk. `sky_light` is false in dimensions without skylight. +pub fn encode_with(chunk: &Chunk, registry: &BlockRegistry, biomes: &Biomes, tables: Option<&SaveTables>, sky_light: bool) -> NamedTag { let mut sections = Vec::new(); + let air = tables.map_or(0, |t| t.air); + // Sky layers below the highest section with light other than 15 are + // written (even all-zero ones): vanilla derives missing layers from the + // layer above them. + let top_sky = chunk.sections.iter().rposition(|s| match &s.light { + Light::Uniform(v) => v >> 4 != 15, + Light::Full(a) => a.iter().any(|v| v >> 4 != 15), + }); for (si, section) in chunk.sections.iter().enumerate() { let y = (MIN_Y >> 4) + si as i32; let ids: Vec = match §ion.blocks { @@ -162,9 +434,13 @@ pub fn encode(chunk: &Chunk, registry: &BlockRegistry, biomes: &Biomes) -> Named palette .iter() .map(|id| { - registry - .state(BlockStateId(u32::from(*id))) - .map_or_else(|| Tag::compound([("Name", string("minecraft:air"))]), BlockState::to_nbt) + let state = registry.state(BlockStateId(u32::from(*id))); + match state { + // BlockState.CODEC: the block name for default states. + Some(s) if tables.is_some_and(|t| t.default_state.get(*id as usize) == Some(&true)) => string(&s.name), + Some(s) => s.to_nbt(), + None => string("minecraft:air"), + } }) .collect(), ), @@ -191,40 +467,94 @@ pub fn encode(chunk: &Chunk, registry: &BlockRegistry, biomes: &Biomes) -> Named if biome_palette.len() > 1 { biome_tag.push(("data", Tag::LongArray(pack(&biome_indices, bits_for(biome_palette.len(), 0))))); } - sections.push(Tag::compound([ + let mut entries = vec![ ("Y", Tag::Byte(y as i8)), ("block_states", Tag::compound(states)), ("biomes", Tag::compound(biome_tag)), - ])); + ]; + if chunk.lit { + let block_lit = match §ion.light { + Light::Uniform(v) => v & 15 != 0, + Light::Full(a) => a.iter().any(|v| v & 15 != 0), + }; + if block_lit { + entries.push(("BlockLight", Tag::ByteArray(nibbles(§ion.light, false)))); + } + if sky_light && top_sky.is_some_and(|top| si <= top + 1) { + entries.push(("SkyLight", Tag::ByteArray(nibbles(§ion.light, true)))); + } + } + let _ = air; + sections.push(Tag::compound(entries)); } - NamedTag { - name: "".into(), - tag: Tag::compound([ - ("DataVersion", Tag::Int(DATA_VERSION)), - ("xPos", Tag::Int(chunk.x)), - ("zPos", Tag::Int(chunk.z)), - ("yPos", Tag::Int(MIN_Y >> 4)), - ("Status", string("minecraft:full")), - ("LastUpdate", Tag::Long(0)), - ("InhabitedTime", Tag::Long(0)), - ("isLightOn", Tag::Byte(0)), - ("sections", Tag::List(sections)), - ("block_entities", Tag::List(chunk.block_entities.iter().map(|(p, e)| encode_block_entity(*p, e)).collect())), - ]), + let extras = chunk.saved.as_deref(); + let block_name = |p: &(i32, i32, i32)| -> String { + registry.state(chunk.block(p.0 & 15, p.1, p.2 & 15)).map_or_else(String::new, |s| s.name.clone()) + }; + let mut block_entities: Vec = chunk.block_entities.iter().map(|(p, e)| encode_block_entity(*p, e, &block_name(p))).collect(); + if let Some(extras) = extras { + for raw in &extras.block_entities { + let pos = (number(raw.get("x")).unwrap_or(0) as i32, number(raw.get("y")).unwrap_or(0) as i32, number(raw.get("z")).unwrap_or(0) as i32); + let state = chunk.get(pos.0 & 15, pos.1, pos.2 & 15) as usize; + let still_there = tables.is_none_or(|t| t.block_entity.get(state).copied().unwrap_or(true)); + if still_there && !chunk.block_entities.iter().any(|(p, _)| *p == pos) { + block_entities.push(raw.clone()); + } + } } + let mut root = extras.map(|e| Tag::Compound(e.root.clone())).unwrap_or_else(|| Tag::Compound(Compound::new())); + for (key, value) in [ + ("DataVersion", Tag::Int(DATA_VERSION)), + ("xPos", Tag::Int(chunk.x)), + ("zPos", Tag::Int(chunk.z)), + ("yPos", Tag::Int(MIN_Y >> 4)), + ("Status", string("minecraft:full")), + ("LastUpdate", Tag::Long(extras.map_or(0, |e| e.last_update))), + ("isLightOn", Tag::Byte(i8::from(chunk.lit))), + ("sections", Tag::List(sections)), + ("block_entities", Tag::List(block_entities)), + ] { + put(&mut root, key, value); + } + if let Some(Tag::Compound(map)) = Some(&mut root) { + map.entry("InhabitedTime".into()).or_insert(Tag::Long(0)); + map.entry("structures".into()).or_insert_with(|| Tag::compound([("References", Tag::Compound(Compound::new())), ("starts", Tag::Compound(Compound::new()))])); + map.entry("PostProcessing".into()).or_insert_with(|| Tag::List(vec![Tag::List(Vec::new()); SECTIONS])); + } + if let Some(t) = tables { + put(&mut root, "Heightmaps", heightmaps(chunk, t)); + let names = |k: u32| t.block_names.get(k as usize).cloned(); + let (blocks, fluids) = extras.map_or((&[][..], &[][..]), |e| (&e.block_ticks[..], &e.fluid_ticks[..])); + put(&mut root, "block_ticks", tick_list(blocks, names)); + put(&mut root, "fluid_ticks", tick_list(fluids, fluid_name)); + } + NamedTag { name: "".into(), tag: root } } +/// Root keys `encode_with` regenerates; everything else is kept as loaded. +const GENERATED_KEYS: [&str; 14] = [ + "DataVersion", "xPos", "zPos", "yPos", "Status", "LastUpdate", "isLightOn", "sections", "block_entities", "Heightmaps", + "block_ticks", "fluid_ticks", "entities", "CarvingMask", +]; pub fn decode(root: &NamedTag, registry: &BlockRegistry, biomes: &Biomes) -> Option { + decode_with(root, registry, biomes, None) +} +/// Only full chunks load; partially generated ones (vanilla's world border +/// of proto chunks) are generated again. +pub fn decode_with(root: &NamedTag, registry: &BlockRegistry, biomes: &Biomes, tables: Option<&SaveTables>) -> Option { let tag = &root.tag; let Tag::Int(x) = tag.get("xPos")? else { return None }; let Tag::Int(z) = tag.get("zPos")? else { return None }; + if text(tag.get("Status")).is_some_and(|s| s != "minecraft:full" && s != "full") { + return None; + } let mut chunk = Chunk::empty(*x, *z); let air = registry.default_state("minecraft:air")?.0 as u16; let mut cache: HashMap = HashMap::new(); let mut biome_data = (*chunk.biomes).clone(); let Some(Tag::List(sections)) = tag.get("sections") else { return Some(chunk) }; for section in sections { - let Some(Tag::Byte(y)) = section.get("Y") else { continue }; - let si = i32::from(*y) - (MIN_Y >> 4); + let Some(y) = number(section.get("Y")) else { continue }; + let si = y as i32 - (MIN_Y >> 4); if !(0..SECTIONS as i32).contains(&si) { continue; } @@ -238,7 +568,19 @@ pub fn decode(root: &NamedTag, registry: &BlockRegistry, biomes: &Biomes) -> Opt let key = state.as_ref().map(|s| format!("{}{:?}", s.name, s.properties)).unwrap_or_default(); *cache.entry(key).or_insert_with(|| { state - .and_then(|s| registry.id(&s).or_else(|| registry.default_state(&s.name))) + .and_then(|s| { + registry.id(&s).or_else(|| { + // Missing properties take the block's defaults. + let default = registry.default_state(&s.name)?; + let mut full = registry.state(default)?.clone(); + for (k, v) in &s.properties { + if full.properties.contains_key(k) { + full.properties.insert(k.clone(), v.clone()); + } + } + registry.id(&full).or(Some(default)) + }) + }) .map_or(air, |id| id.0 as u16) }) }) @@ -275,52 +617,102 @@ pub fn decode(root: &NamedTag, registry: &BlockRegistry, biomes: &Biomes) -> Opt } } chunk.biomes = Arc::new(biome_data); + let mut extras = ChunkExtras { + last_update: number(tag.get("LastUpdate")).unwrap_or(0), + ..ChunkExtras::default() + }; if let Some(Tag::List(entities)) = tag.get("block_entities") { - chunk.block_entities = entities.iter().filter_map(decode_block_entity).collect(); + for e in entities { + match decode_block_entity(e) { + Some(be) => chunk.block_entities.push(be), + None => extras.block_entities.push(e.clone()), + } + } } + if let Tag::Compound(map) = tag { + extras.root = map.iter().filter(|(k, _)| !GENERATED_KEYS.contains(&k.to_string_lossy().as_str())).map(|(k, v)| (k.clone(), v.clone())).collect(); + } + if let Some(t) = tables { + extras.block_ticks = read_ticks(tag.get("block_ticks"), (*x, *z), |n| t.block_ids.get(n).map(|&i| u32::from(i))); + extras.fluid_ticks = read_ticks(tag.get("fluid_ticks"), (*x, *z), fluid_id); + } + chunk.saved = Some(Arc::new(extras)); Some(chunk) } -/// The world folder. Region files are read lazily by worker threads. + +type RegionCache = Mutex>>>>; +/// One dimension folder (`dimensions/minecraft/`). Region files are +/// read lazily by worker threads. pub struct SaveDir { pub root: PathBuf, - regions: Mutex>>>>, + regions: RegionCache, + entity_regions: RegionCache, + /// Set by `Persistent` before the first load. + pub tables: OnceLock>, + /// False in dimensions without skylight (no SkyLight layers are written). + pub sky_light: bool, } impl SaveDir { pub fn new(root: PathBuf) -> Self { + let sky_light = !root.ends_with("the_nether") && !root.ends_with("DIM-1"); Self { root, regions: Mutex::new(HashMap::new()), + entity_regions: Mutex::new(HashMap::new()), + tables: OnceLock::new(), + sky_light, } } - fn region_path(&self, rx: i32, rz: i32) -> PathBuf { - self.root.join(format!("region/r.{rx}.{rz}.mca")) + fn region_path(&self, folder: &str, rx: i32, rz: i32) -> PathBuf { + self.root.join(format!("{folder}/r.{rx}.{rz}.mca")) } - pub fn load(&self, x: i32, z: i32, registry: &BlockRegistry, biomes: &Biomes) -> Option { + fn read_record(&self, folder: &str, x: i32, z: i32) -> Option { let key = (x >> 5, z >> 5); + let cache = if folder == "region" { &self.regions } else { &self.entity_regions }; let region = { - let mut regions = self.regions.lock().ok()?; + let mut regions = cache.lock().ok()?; regions .entry(key) - .or_insert_with(|| RegionFile::open(self.region_path(key.0, key.1)).ok().map(|r| Arc::new(Mutex::new(r)))) + .or_insert_with(|| RegionFile::open(self.region_path(folder, key.0, key.1)).ok().map(|r| Arc::new(Mutex::new(r)))) .clone()? }; let record = region.lock().ok()?.read(ChunkPos::new(x, z)).ok()??; - let root = record.nbt(Limits::default()).ok()?; - decode(&root, registry, biomes) + record.nbt(Limits::default()).ok() } - /// Rewrite every region touched by `chunks`, keeping their other chunks. - pub fn save(&self, chunks: &[Arc], registry: &BlockRegistry, biomes: &Biomes) -> Result { - let mut by_region: BTreeMap<(i32, i32), Vec<&Arc>> = BTreeMap::new(); - for c in chunks { - by_region.entry((c.x >> 5, c.z >> 5)).or_default().push(c); + pub fn load(&self, x: i32, z: i32, registry: &BlockRegistry, biomes: &Biomes) -> Option { + let root = self.read_record("region", x, z)?; + let tables = self.tables.get().map(|t| &**t); + decode_with(&root, registry, biomes, tables) + } + /// Entities saved for a chunk (`entities/`; older chunks may still keep + /// them in the chunk's own `entities` list). + pub fn load_entities(&self, x: i32, z: i32) -> Vec { + let mut out = Vec::new(); + if let Some(root) = self.read_record("entities", x, z) + && let Some(Tag::List(list)) = root.tag.get("Entities") + { + out.extend(list.iter().cloned()); } - let staging = self.root.join("region/.staging"); + out + } + /// Rewrite every region file in `folder` touched by `records`, keeping + /// its other chunks. `None` deletes a chunk. + fn write_records(&self, folder: &str, records: Vec<((i32, i32), Option)>) -> Result<()> { + let mut by_region: BTreeMap<(i32, i32), Vec<((i32, i32), Option)>> = BTreeMap::new(); + for (key, tag) in records { + by_region.entry((key.0 >> 5, key.1 >> 5)).or_default().push((key, tag)); + } + if by_region.is_empty() { + return Ok(()); + } + let staging = self.root.join(folder).join(".staging"); fs::create_dir_all(&staging)?; let timestamp = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map_or(0, |d| d.as_secs() as u32); + let cache = if folder == "region" { &self.regions } else { &self.entity_regions }; for ((rx, rz), list) in by_region { - let path = self.region_path(rx, rz); + let path = self.region_path(folder, rx, rz); let mut records: BTreeMap<(i32, i32), ChunkRecord> = BTreeMap::new(); if let Ok(mut existing) = RegionFile::open(&path) { let positions: Vec = existing.chunks().map(|(p, _)| p).collect(); @@ -330,24 +722,189 @@ impl SaveDir { } } } - for c in list { - let record = ChunkRecord::from_nbt(&encode(c, registry, biomes), Compression::Zlib, timestamp, Limits::default())?; - records.insert((c.x, c.z), record); + for (key, tag) in list { + match tag { + Some(tag) => { + records.insert(key, ChunkRecord::from_nbt(&tag, Compression::Zlib, timestamp, Limits::default())?); + } + None => { + records.remove(&key); + } + } } - let temporary = staging.join(format!("r.{rx}.{rz}.mca")); - let _ = fs::remove_file(&temporary); - let mut writer = RegionWriter::create(&temporary)?; - for ((x, z), r) in &records { - writer.write(ChunkPos::new(*x, *z), r)?; + if records.is_empty() { + let _ = fs::remove_file(&path); + } else { + let temporary = staging.join(format!("r.{rx}.{rz}.mca")); + let _ = fs::remove_file(&temporary); + let mut writer = RegionWriter::create(&temporary)?; + for ((x, z), r) in &records { + writer.write(ChunkPos::new(*x, *z), r)?; + } + writer.finish()?; + fs::rename(&temporary, &path)?; } - writer.finish()?; - fs::rename(&temporary, &path)?; - self.regions.lock().map_err(|_| "region cache poisoned")?.remove(&(rx, rz)); + cache.lock().map_err(|_| "region cache poisoned")?.remove(&(rx, rz)); } + let _ = fs::remove_dir(&staging); + Ok(()) + } + /// Rewrite every region touched by `chunks`, keeping their other chunks. + pub fn save(&self, chunks: &[Arc], registry: &BlockRegistry, biomes: &Biomes) -> Result { + let tables = self.tables.get().map(|t| &**t); + let records = chunks + .iter() + .map(|c| ((c.x, c.z), Some(encode_with(c, registry, biomes, tables, self.sky_light)))) + .collect(); + self.write_records("region", records)?; Ok(chunks.len()) } + /// Write the entity chunks (`EntityStorage`): an empty list deletes it. + pub fn save_entities(&self, chunks: Vec<((i32, i32), Vec)>) -> Result<()> { + let records = chunks + .into_iter() + .map(|((x, z), entities)| { + let tag = (!entities.is_empty()).then(|| NamedTag { + name: "".into(), + tag: Tag::compound([ + ("DataVersion", Tag::Int(DATA_VERSION)), + ("Position", Tag::IntArray(vec![x, z])), + ("Entities", Tag::List(entities)), + ]), + }); + ((x, z), tag) + }) + .collect(); + self.write_records("entities", records) + } } -/// Player and world state stored in `level.dat`. + +fn read_dat(path: &Path) -> Option { + let bytes = fs::read(path).ok()?; + let raw = match Compression::Gzip.decode(&bytes, 64 << 20) { + Ok(raw) => raw, + Err(_) => bytes, + }; + Some(nbt::read_named(&raw, Limits::default()).ok()?.0.tag) +} +/// Write a gzip NBT file the way vanilla does (`_new`, keep `_old`). +fn write_dat(path: &Path, tag: Tag) -> Result<()> { + if let Some(dir) = path.parent() { + fs::create_dir_all(dir)?; + } + let bytes = nbt::write_named(&NamedTag { name: "".into(), tag }, Limits::default()).map_err(|e| e.to_string())?; + let compressed = Compression::Gzip.encode(&bytes)?; + let file = path.file_name().and_then(|n| n.to_str()).unwrap_or("data.dat"); + let temporary = path.with_file_name(format!("{file}_new")); + fs::write(&temporary, compressed)?; + if path.exists() { + let old = match file.strip_suffix(".dat") { + Some(stem) => path.with_file_name(format!("{stem}.dat_old")), + None => path.with_file_name(format!("{file}_old")), + }; + let _ = fs::rename(path, old); + } + fs::rename(temporary, path)?; + Ok(()) +} +/// `SavedDataStorage`: `{data: ..., DataVersion}` under `data/minecraft/`. +fn saved_data_path(world: &Path, name: &str) -> PathBuf { + world.join("data").join("minecraft").join(format!("{name}.dat")) +} +fn read_saved_data(world: &Path, name: &str) -> Option { + read_dat(&saved_data_path(world, name))?.get("data").cloned() +} +fn write_saved_data(world: &Path, name: &str, data: Tag) -> Result<()> { + write_dat(&saved_data_path(world, name), Tag::compound([("data", data), ("DataVersion", Tag::Int(DATA_VERSION))])) +} +fn uuid_text(u: [i32; 4]) -> String { + let hex: String = u.iter().map(|v| format!("{:08x}", *v as u32)).collect(); + format!("{}-{}-{}-{}-{}", &hex[0..8], &hex[8..12], &hex[12..16], &hex[16..20], &hex[20..32]) +} +fn uuid_from_text(s: &str) -> Option<[i32; 4]> { + let hex: String = s.chars().filter(|c| *c != '-').collect(); + if hex.len() != 32 { + return None; + } + let mut out = [0i32; 4]; + for (i, v) in out.iter_mut().enumerate() { + *v = u32::from_str_radix(&hex[i * 8..i * 8 + 8], 16).ok()? as i32; + } + Some(out) +} +/// A random (version 4) UUID as vanilla's four-int form. +pub fn random_uuid() -> [i32; 4] { + let t = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_nanos() as u64); + let mut s = t ^ (std::process::id() as u64).rotate_left(32) ^ 0x9E37_79B9_7F4A_7C15; + let mut next = || { + s ^= s << 13; + s ^= s >> 7; + s ^= s << 17; + s + }; + let (a, b) = (next(), next()); + let a = (a & !0xF000) | 0x4000; + let b = (b & !(0xC000_0000_0000_0000)) | 0x8000_0000_0000_0000; + [(a >> 32) as i32, a as i32, (b >> 32) as i32, b as i32] +} +fn uuid_tag(u: [i32; 4]) -> Tag { + Tag::IntArray(u.to_vec()) +} +pub(crate) fn uuid_from_tag(tag: Option<&Tag>) -> Option<[i32; 4]> { + match tag? { + Tag::IntArray(v) if v.len() == 4 => Some([v[0], v[1], v[2], v[3]]), + Tag::String(s) => uuid_from_text(&s.to_string_lossy()), + _ => None, + } +} +pub(crate) fn doubles(values: &[f64]) -> Tag { + Tag::List(values.iter().map(|v| Tag::Double(v.to_bits())).collect()) +} +pub(crate) fn floats(values: &[f32]) -> Tag { + Tag::List(values.iter().map(|v| Tag::Float(v.to_bits())).collect()) +} +pub(crate) fn read_doubles(tag: Option<&Tag>) -> Option<[f64; N]> { + match tag? { + Tag::List(l) if l.len() == N => { + let mut out = [0.0; N]; + for (o, v) in out.iter_mut().zip(l) { + *o = float(Some(v))?; + } + Some(out) + } + _ => None, + } +} + +/// Default game rules of a new 26.3 world (`data/minecraft/game_rules.dat`). +const GAME_RULES: [(&str, i32); 58] = [ + ("spawn_wandering_traders", 1), ("block_drops", 1), ("reduced_debug_info", 0), ("show_death_messages", 1), + ("spawner_blocks_work", 1), ("spawn_monsters", 1), ("tnt_explodes", 1), ("player_movement_check", 1), + ("immediate_respawn", 0), ("block_explosion_drop_decay", 1), ("spread_vines", 1), ("max_entity_cramming", 24), + ("forgive_dead_players", 1), ("fall_damage", 1), ("send_command_feedback", 1), ("global_sound_events", 1), + ("elytra_movement_check", 1), ("fire_spread_radius_around_player", 128), ("freeze_damage", 1), + ("mob_explosion_drop_decay", 1), ("natural_health_regeneration", 1), ("players_nether_portal_default_delay", 80), + ("mob_drops", 1), ("log_admin_commands", 1), ("mob_griefing", 1), ("spawn_mobs", 1), ("pvp", 1), + ("spectators_generate_chunks", 1), ("max_command_sequence_length", 65536), ("players_nether_portal_creative_delay", 0), + ("players_sleeping_percentage", 100), ("advance_weather", 1), ("max_block_modifications", 32768), + ("max_command_forks", 65536), ("drowning_damage", 1), ("command_block_output", 1), ("locator_bar", 1), + ("show_advancement_messages", 1), ("respawn_radius", 10), ("raids", 1), ("spawn_phantoms", 1), + ("max_snow_accumulation_height", 1), ("limited_crafting", 0), ("allow_entering_nether_using_portals", 1), + ("lava_source_conversion", 0), ("tnt_explosion_drop_decay", 0), ("universal_anger", 0), ("keep_inventory", 0), + ("spawn_patrols", 1), ("random_tick_speed", 3), ("fire_damage", 1), ("entity_drops", 1), ("advance_time", 1), + ("command_blocks_work", 1), ("spawn_wardens", 1), ("water_source_conversion", 1), ("projectiles_can_break_blocks", 1), + ("ender_pearls_vanish_on_death", 1), +]; +/// Integer-valued rules; the others are booleans stored as bytes. +const INT_RULES: [&str; 11] = [ + "max_snow_accumulation_height", "random_tick_speed", + "max_entity_cramming", "fire_spread_radius_around_player", "players_nether_portal_default_delay", + "max_command_sequence_length", "players_nether_portal_creative_delay", "players_sleeping_percentage", + "max_block_modifications", "max_command_forks", "respawn_radius", +]; + +/// World and player state stored in `level.dat`, the saved-data files and +/// the player file. #[derive(Clone, Debug, Default)] pub struct Level { pub name: String, @@ -355,12 +912,14 @@ pub struct Level { pub creative: bool, pub time: i64, pub day_time: i64, + /// The player's respawn point (bed, or the world spawn). pub spawn: Option<[f64; 3]>, pub position: Option<[f64; 3]>, pub rotation: [f32; 2], pub health: f32, pub food: i32, pub saturation: f32, + /// (slot, item, count, damage): 0..36 inventory, 100..103 armor (feet..head), 150 offhand. pub inventory: Vec<(u8, String, u32, u32)>, pub xp_level: i32, pub xp_progress: f32, @@ -368,155 +927,407 @@ pub struct Level { pub weather: crate::world::Weather, /// Player.Dimension (minecraft:overworld / minecraft:the_nether). pub dimension: String, + /// World spawn (`level.dat` `spawn`), distinct from a bed respawn point. + pub world_spawn: Option<[i32; 3]>, + pub difficulty: String, + pub hardcore: bool, + /// Game rule values by name without namespace (`keep_inventory`, ...). + pub game_rules: BTreeMap, + pub player_uuid: Option<[i32; 4]>, + pub selected_slot: i32, + pub motion: [f64; 3], + pub fall_distance: f64, + pub on_ground: bool, + pub food_exhaustion: f32, + pub food_tick_timer: i32, + /// The player compound as loaded, for fields Horizon does not model + /// (ender chest, effects, abilities, recipe book, attributes, ...). + pub player_extra: Option, } impl Level { + pub fn game_rule(&self, name: &str) -> i32 { + self.game_rules + .get(name) + .copied() + .or_else(|| GAME_RULES.iter().find(|(n, _)| *n == name).map(|(_, v)| *v)) + .unwrap_or(0) + } pub fn read(dir: &Path) -> Option { - let bytes = fs::read(dir.join("level.dat")).ok()?; - let raw = Compression::Gzip.decode(&bytes, 64 << 20).ok()?; - let (root, _) = nbt::read_named(&raw, Limits::default()).ok()?; - let data = root.tag.get("Data")?; - let long = |t: Option<&Tag>| match t { - Some(Tag::Long(v)) => *v, - Some(Tag::Int(v)) => i64::from(*v), - Some(Tag::Short(v)) => i64::from(*v), - Some(Tag::Byte(v)) => i64::from(*v), - _ => 0, + let root = read_dat(&dir.join("level.dat"))?; + let data = root.get("Data")?; + let n = |t: Option<&Tag>| number(t).unwrap_or(0); + let mut level = Self { + name: text(data.get("LevelName")).unwrap_or_else(|| "World".into()), + creative: n(data.get("GameType")) == 1, + time: n(data.get("Time")), + health: 20.0, + food: 20, + saturation: 5.0, + dimension: "minecraft:overworld".into(), + difficulty: "normal".into(), + ..Self::default() }; - let doubles = |t: Option<&Tag>| match t { - Some(Tag::List(l)) if l.len() == 3 => { - let v: Vec = l.iter().map(|d| if let Tag::Double(b) = d { f64::from_bits(*b) } else { 0.0 }).collect(); - Some([v[0], v[1], v[2]]) - } - _ => None, + // 26.3: spawn {pos: [I; x, y, z], dimension, yaw, pitch}; older: SpawnX/Y/Z. + level.world_spawn = match data.get("spawn").and_then(|s| s.get("pos")) { + Some(Tag::IntArray(p)) if p.len() == 3 => Some([p[0], p[1], p[2]]), + _ => data.get("SpawnX").map(|_| [n(data.get("SpawnX")) as i32, n(data.get("SpawnY")) as i32, n(data.get("SpawnZ")) as i32]), }; - let player = data.get("Player"); - let float = |t: Option<&Tag>| if let Some(Tag::Float(b)) = t { f32::from_bits(*b) } else { 0.0 }; - let mut inventory = Vec::new(); - if let Some(Tag::List(items)) = player.and_then(|p| p.get("Inventory")) { - for it in items { - let slot = if let Some(Tag::Byte(s)) = it.get("Slot") { *s as u8 } else { continue }; - let id = if let Some(Tag::String(s)) = it.get("id") { s.to_string_lossy() } else { continue }; - let count = long(it.get("count")).max(1) as u32; - let damage = it - .get("components") - .and_then(|c| c.get("minecraft:damage")) - .map_or(0, |d| long(Some(d)) as u32); - inventory.push((slot, id, count, damage)); + if let Some(d) = data.get("difficulty_settings") { + level.difficulty = text(d.get("difficulty")).unwrap_or_else(|| "normal".into()); + level.hardcore = n(d.get("hardcore")) != 0; + } + let mut rules: BTreeMap = GAME_RULES.iter().map(|(k, v)| ((*k).to_owned(), *v)).collect(); + let saved_rules = read_saved_data(dir, "game_rules"); + if let Some(Tag::Compound(map)) = &saved_rules { + for (k, v) in map { + let name = k.to_string_lossy(); + let name = name.strip_prefix("minecraft:").unwrap_or(&name).to_owned(); + if let Some(v) = number(Some(v)) { + rules.insert(name, v as i32); + } } } - let rotation = match player.and_then(|p| p.get("Rotation")) { - Some(Tag::List(r)) if r.len() == 2 => [float(r.first()), float(r.get(1))], - _ => [0.0; 2], + level.game_rules = rules; + // Seed: world_gen_settings.dat (26.3) or Data.WorldGenSettings. + level.seed = read_saved_data(dir, "world_gen_settings") + .and_then(|w| number(w.get("seed"))) + .or_else(|| number(data.get("WorldGenSettings").and_then(|w| w.get("seed")))) + .unwrap_or(0); + // Day time: the overworld clock (26.3) or Data.DayTime. + level.day_time = read_saved_data(dir, "world_clocks") + .and_then(|c| number(c.get("minecraft:overworld").and_then(|o| o.get("total_ticks")))) + .or_else(|| number(data.get("DayTime"))) + .unwrap_or(level.time); + let weather = read_saved_data(dir, "weather"); + let w = |new: &str, old: &str| number(weather.as_ref().and_then(|w| w.get(new))).or_else(|| number(data.get(old))).unwrap_or(0); + let raining = w("raining", "raining") != 0; + let thundering = w("thundering", "thundering") != 0; + level.weather = crate::world::Weather { + clear_time: w("clear_weather_time", "clearWeatherTime") as i32, + rain_time: w("rain_time", "rainTime") as i32, + thunder_time: w("thunder_time", "thunderTime") as i32, + raining, + thundering, + rain_level: if raining { (1.0, 1.0) } else { (0.0, 0.0) }, + thunder_level: if thundering { (1.0, 1.0) } else { (0.0, 0.0) }, }; - Some(Self { - name: match data.get("LevelName") { - Some(Tag::String(s)) => s.to_string_lossy(), - _ => "World".into(), - }, - seed: long(data.get("WorldGenSettings").and_then(|w| w.get("seed"))), - creative: long(data.get("GameType")) == 1, - time: long(data.get("Time")), - day_time: long(data.get("DayTime")), - spawn: Some([long(data.get("SpawnX")) as f64, long(data.get("SpawnY")) as f64, long(data.get("SpawnZ")) as f64]), - position: doubles(player.and_then(|p| p.get("Pos"))), - rotation, - health: player.map_or(20.0, |p| float(p.get("Health"))), - food: player.map_or(20, |p| long(p.get("foodLevel")) as i32), - saturation: player.map_or(5.0, |p| float(p.get("foodSaturationLevel"))), - dimension: match player.and_then(|p| p.get("Dimension")) { - Some(Tag::String(s)) => s.to_string_lossy(), - _ => "minecraft:overworld".into(), - }, - weather: crate::world::Weather { - clear_time: long(data.get("clearWeatherTime")) as i32, - rain_time: long(data.get("rainTime")) as i32, - thunder_time: long(data.get("thunderTime")) as i32, - raining: long(data.get("raining")) != 0, - thundering: long(data.get("thundering")) != 0, - rain_level: if long(data.get("raining")) != 0 { (1.0, 1.0) } else { (0.0, 0.0) }, - thunder_level: if long(data.get("thundering")) != 0 { (1.0, 1.0) } else { (0.0, 0.0) }, - }, - xp_level: player.map_or(0, |p| long(p.get("XpLevel")) as i32), - xp_progress: player.map_or(0.0, |p| float(p.get("XpP"))), - xp_total: player.map_or(0, |p| long(p.get("XpTotal")) as i32), - inventory, - }) + // The player: players/data/.dat, the newest player + // file of a server world, or the legacy Data.Player. + let uuid = uuid_from_tag(data.get("singleplayer_uuid")); + let player_dir = dir.join("players").join("data"); + let player = match uuid { + Some(u) => read_dat(&player_dir.join(format!("{}.dat", uuid_text(u)))), + None => fs::read_dir(&player_dir) + .ok() + .and_then(|d| { + d.flatten() + .filter(|e| e.path().extension().is_some_and(|x| x == "dat")) + .max_by_key(|e| e.metadata().and_then(|m| m.modified()).ok()) + }) + .and_then(|e| read_dat(&e.path())), + } + .or_else(|| data.get("Player").cloned()); + level.player_uuid = uuid.or_else(|| uuid_from_tag(player.as_ref().and_then(|p| p.get("UUID")))); + let spawn = level.world_spawn.map(|s| [f64::from(s[0]), f64::from(s[1]), f64::from(s[2])]); + level.spawn = spawn; + if let Some(p) = &player { + level.read_player(p); + } + level.player_extra = player; + Some(level) } - pub fn write(&self, dir: &Path) -> Result<()> { - fs::create_dir_all(dir)?; - let spawn = self.spawn.unwrap_or([0.0, 64.0, 0.0]); - let mut player = vec![ + fn read_player(&mut self, p: &Tag) { + let n = |k: &str| number(p.get(k)); + let f = |k: &str| float(p.get(k)); + self.position = read_doubles::<3>(p.get("Pos")); + if let Some(r) = read_doubles::<2>(p.get("Rotation")) { + self.rotation = [r[0] as f32, r[1] as f32]; + } + self.motion = read_doubles::<3>(p.get("Motion")).unwrap_or_default(); + self.health = f("Health").map_or(20.0, |v| v as f32); + self.food = n("foodLevel").map_or(20, |v| v as i32); + self.saturation = f("foodSaturationLevel").map_or(5.0, |v| v as f32); + self.food_exhaustion = f("foodExhaustionLevel").unwrap_or(0.0) as f32; + self.food_tick_timer = n("foodTickTimer").unwrap_or(0) as i32; + self.xp_level = n("XpLevel").unwrap_or(0) as i32; + self.xp_progress = f("XpP").unwrap_or(0.0) as f32; + self.xp_total = n("XpTotal").unwrap_or(0) as i32; + self.selected_slot = n("SelectedItemSlot").unwrap_or(0) as i32; + self.fall_distance = f("fall_distance").unwrap_or(0.0); + self.on_ground = n("OnGround").unwrap_or(0) != 0; + if let Some(mode) = n("playerGameType") { + self.creative = mode == 1; + } + if let Some(d) = text(p.get("Dimension")) { + self.dimension = d; + } + if let Some(respawn) = p.get("respawn") + && let Some(Tag::IntArray(pos)) = respawn.get("pos") + && pos.len() == 3 + { + self.spawn = Some([f64::from(pos[0]), f64::from(pos[1]), f64::from(pos[2])]); + } + let mut inventory = Vec::new(); + if let Some(Tag::List(items)) = p.get("Inventory") { + for it in items { + let slot = number(it.get("Slot")).unwrap_or(0) as i8 as u8; + if let Some(stack) = item_from_nbt(it) { + // Legacy armor slots 100..103 and offhand -106 (150). + let slot = if slot as i8 == -106 { 150 } else { slot }; + inventory.push((slot, stack.item.to_string(), stack.count, stack.damage)); + } + } + } + if let Some(eq) = p.get("equipment") { + for (name, slot) in [("feet", 100u8), ("legs", 101), ("chest", 102), ("head", 103), ("offhand", 150)] { + if let Some(stack) = eq.get(name).and_then(item_from_nbt) { + inventory.retain(|(s, ..)| *s != slot); + inventory.push((slot, stack.item.to_string(), stack.count, stack.damage)); + } + } + } + self.inventory = inventory; + } + /// The player compound: the loaded one with Horizon's state written over it. + fn player_tag(&self) -> Tag { + let mut p = compound_or_empty(self.player_extra.clone()); + // Keys rewritten from scratch. + if let Tag::Compound(map) = &mut p { + for key in ["Inventory", "equipment", "Pos", "respawn"] { + map.remove(&JavaString::from(key)); + } + } + let mut inventory = Vec::new(); + let mut equipment = Vec::new(); + for (slot, id, count, damage) in &self.inventory { + let stack = crate::items::ItemStack { item: Arc::from(id.as_str()), count: *count, damage: *damage }; + match slot { + 0..=35 => { + let mut entries = vec![("Slot", Tag::Byte(*slot as i8))]; + entries.extend(encode_item(&stack)); + inventory.push(Tag::compound(entries)); + } + 100 => equipment.push(("feet", item_to_nbt(&stack))), + 101 => equipment.push(("legs", item_to_nbt(&stack))), + 102 => equipment.push(("chest", item_to_nbt(&stack))), + 103 => equipment.push(("head", item_to_nbt(&stack))), + 150 => equipment.push(("offhand", item_to_nbt(&stack))), + _ => {} + } + } + let creative = self.creative; + let mut abilities = compound_or_empty(p.get("abilities").cloned()); + for (k, v) in [("invulnerable", creative), ("mayfly", creative), ("instabuild", creative)] { + put(&mut abilities, k, Tag::Byte(i8::from(v))); + } + if !creative { + put(&mut abilities, "flying", Tag::Byte(0)); + } + if let Tag::Compound(map) = &mut abilities { + map.entry("mayBuild".into()).or_insert(Tag::Byte(1)); + map.entry("flySpeed".into()).or_insert(Tag::Float(0.05f32.to_bits())); + map.entry("walkSpeed".into()).or_insert(Tag::Float(0.1f32.to_bits())); + map.entry("flying".into()).or_insert(Tag::Byte(0)); + } + let uuid = self.player_uuid.unwrap_or([0, 0, 0, 0]); + let mut entries: Vec<(&str, Tag)> = vec![ + ("DataVersion", Tag::Int(DATA_VERSION)), + ("UUID", uuid_tag(uuid)), + ("Rotation", floats(&self.rotation)), + ("Motion", doubles(&self.motion)), + ("OnGround", Tag::Byte(i8::from(self.on_ground))), + ("fall_distance", Tag::Double(self.fall_distance.to_bits())), ("Health", Tag::Float(self.health.to_bits())), ("foodLevel", Tag::Int(self.food)), ("foodSaturationLevel", Tag::Float(self.saturation.to_bits())), + ("foodExhaustionLevel", Tag::Float(self.food_exhaustion.to_bits())), + ("foodTickTimer", Tag::Int(self.food_tick_timer)), ("XpLevel", Tag::Int(self.xp_level)), ("XpP", Tag::Float(self.xp_progress.to_bits())), ("XpTotal", Tag::Int(self.xp_total)), + ("SelectedItemSlot", Tag::Int(self.selected_slot)), ("playerGameType", Tag::Int(i32::from(self.creative))), - ( - "Rotation", - Tag::List(vec![Tag::Float(self.rotation[0].to_bits()), Tag::Float(self.rotation[1].to_bits())]), - ), - ( - "Inventory", - Tag::List( - self.inventory - .iter() - .map(|(slot, id, count, damage)| { - let mut entries = vec![ - ("Slot", Tag::Byte(*slot as i8)), - ("id", string(id)), - ("count", Tag::Int(*count as i32)), - ]; - if *damage > 0 { - entries.push(("components", Tag::compound([("minecraft:damage", Tag::Int(*damage as i32))]))); - } - Tag::compound(entries) - }) - .collect(), - ), - ), + ("Inventory", Tag::List(inventory)), + ("abilities", abilities), ("Dimension", string(if self.dimension.is_empty() { "minecraft:overworld" } else { &self.dimension })), ]; - if let Some(p) = self.position { - player.push(("Pos", Tag::List(p.iter().map(|v| Tag::Double(v.to_bits())).collect()))); + if !equipment.is_empty() { + entries.push(("equipment", Tag::compound(equipment))); } - let root = NamedTag { - name: "".into(), - tag: Tag::compound([( - "Data", - Tag::compound([ - ("DataVersion", Tag::Int(DATA_VERSION)), - ("version", Tag::Int(19133)), - ("LevelName", string(&self.name)), - ("GameType", Tag::Int(i32::from(self.creative))), - ("Time", Tag::Long(self.time)), - ("DayTime", Tag::Long(self.day_time)), - ("SpawnX", Tag::Int(spawn[0].floor() as i32)), - ("SpawnY", Tag::Int(spawn[1].floor() as i32)), - ("SpawnZ", Tag::Int(spawn[2].floor() as i32)), - ("initialized", Tag::Byte(1)), - ("clearWeatherTime", Tag::Int(self.weather.clear_time)), - ("rainTime", Tag::Int(self.weather.rain_time)), - ("thunderTime", Tag::Int(self.weather.thunder_time)), - ("raining", Tag::Byte(i8::from(self.weather.raining))), - ("thundering", Tag::Byte(i8::from(self.weather.thundering))), - ("allowCommands", Tag::Byte(0)), - ("hardcore", Tag::Byte(0)), - ("Difficulty", Tag::Byte(2)), - ("WorldGenSettings", Tag::compound([("seed", Tag::Long(self.seed)), ("generate_features", Tag::Byte(1)), ("bonus_chest", Tag::Byte(0))])), - ("Player", Tag::compound(player)), - ]), - )]), + if let Some(pos) = self.position { + entries.push(("Pos", doubles(&pos))); + } + // A respawn point other than the world spawn (a bed). + if let Some(s) = self.spawn { + let block = [s[0].floor() as i32, s[1].floor() as i32, s[2].floor() as i32]; + if Some(block) != self.world_spawn { + entries.push(( + "respawn", + Tag::compound([ + ("dimension", string("minecraft:overworld")), + ("pos", Tag::IntArray(block.to_vec())), + ("yaw", Tag::Float(0f32.to_bits())), + ("pitch", Tag::Float(0f32.to_bits())), + ]), + )); + } + } + for (key, value) in entries { + put(&mut p, key, value); + } + if let Tag::Compound(map) = &mut p { + for (key, value) in [("Air", Tag::Short(300)), ("Fire", Tag::Short(-20)), ("Invulnerable", Tag::Byte(0)), ("PortalCooldown", Tag::Int(0)), ("Score", Tag::Int(0)), ("SleepTimer", Tag::Short(0)), ("XpSeed", Tag::Int(0)), ("seenCredits", Tag::Byte(0)), ("HurtTime", Tag::Short(0)), ("DeathTime", Tag::Short(0)), ("AbsorptionAmount", Tag::Float(0)), ("FallFlying", Tag::Byte(0))] { + map.entry(key.into()).or_insert(value); + } + } + p + } + pub fn write(&self, dir: &Path) -> Result<()> { + fs::create_dir_all(dir)?; + let existing = read_dat(&dir.join("level.dat")).and_then(|r| r.get("Data").cloned()); + let legacy = existing.as_ref().is_some_and(|d| d.get("Player").is_some() || d.get("WorldGenSettings").is_some()); + let mut data = compound_or_empty(existing); + if let Tag::Compound(map) = &mut data { + // Pre-26.3 keys now stored elsewhere (a legacy Horizon level.dat). + for key in [ + "Player", "WorldGenSettings", "DayTime", "SpawnX", "SpawnY", "SpawnZ", "SpawnAngle", "clearWeatherTime", "rainTime", + "thunderTime", "raining", "thundering", "Difficulty", "hardcore", "GameRules", "DimensionData", "WanderingTraderSpawnChance", + "WanderingTraderSpawnDelay", "WanderingTraderId", "BorderCenterX", "BorderCenterZ", "BorderSize", "BorderSafeZone", + "BorderWarningBlocks", "BorderWarningTime", "BorderSizeLerpTime", "BorderSizeLerpTarget", "BorderDamagePerBlock", + "DifficultyLocked", "ScheduledEvents", "CustomBossEvents", "DragonFight", + ] { + map.remove(&JavaString::from(key)); + } + } + let uuid = self.player_uuid.unwrap_or_else(random_uuid); + let spawn = self.world_spawn.unwrap_or_else(|| { + let s = self.spawn.unwrap_or([0.0, 64.0, 0.0]); + [s[0].floor() as i32, s[1].floor() as i32, s[2].floor() as i32] + }); + let mut history: Vec = match data.get("version_history") { + Some(Tag::List(l)) => l.clone(), + _ => Vec::new(), }; - let bytes = nbt::write_named(&root, Limits::default()).map_err(|e| e.to_string())?; - let compressed = Compression::Gzip.encode(&bytes)?; - let temporary = dir.join("level.dat_new"); - fs::write(&temporary, compressed)?; - if dir.join("level.dat").exists() { - let _ = fs::rename(dir.join("level.dat"), dir.join("level.dat_old")); + if history.last() != Some(&Tag::Int(DATA_VERSION)) { + history.push(Tag::Int(DATA_VERSION)); } - fs::rename(temporary, dir.join("level.dat"))?; + let mut brands: Vec = match data.get("ServerBrands") { + Some(Tag::List(l)) => l.clone(), + _ => Vec::new(), + }; + if !brands.contains(&string("horizon")) { + brands.push(string("horizon")); + } + let mut difficulty = compound_or_empty(data.get("difficulty_settings").cloned()); + put(&mut difficulty, "difficulty", string(if self.difficulty.is_empty() { "normal" } else { &self.difficulty })); + put(&mut difficulty, "hardcore", Tag::Byte(i8::from(self.hardcore))); + if let Tag::Compound(map) = &mut difficulty { + map.entry("locked".into()).or_insert(Tag::Byte(0)); + } + let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_millis() as i64); + for (key, value) in [ + ("DataVersion", Tag::Int(DATA_VERSION)), + ( + "Version", + Tag::compound([ + ("Name", string(VERSION_NAME)), + ("Id", Tag::Int(DATA_VERSION)), + ("Snapshot", Tag::Byte(0)), + ("Series", string("main")), + ]), + ), + ("version_history", Tag::List(history)), + ("version", Tag::Int(19133)), + ("LevelName", string(&self.name)), + ("GameType", Tag::Int(i32::from(self.creative))), + ("Time", Tag::Long(self.time)), + ("LastPlayed", Tag::Long(now)), + ( + "spawn", + Tag::compound([ + ("pos", Tag::IntArray(spawn.to_vec())), + ("dimension", string("minecraft:overworld")), + ("yaw", Tag::Float(0f32.to_bits())), + ("pitch", Tag::Float(0f32.to_bits())), + ]), + ), + ("difficulty_settings", difficulty), + ("initialized", Tag::Byte(1)), + ("singleplayer_uuid", uuid_tag(uuid)), + ("ServerBrands", Tag::List(brands)), + ] { + put(&mut data, key, value); + } + if let Tag::Compound(map) = &mut data { + map.entry("allowCommands".into()).or_insert(Tag::Byte(0)); + map.entry("WasModded".into()).or_insert(Tag::Byte(0)); + map.entry("DataPacks".into()).or_insert_with(|| { + Tag::compound([ + ("Enabled", Tag::List(vec![string("vanilla")])), + ("Disabled", Tag::List(["minecart_improvements", "redstone_experiments", "trade_rebalance"].iter().map(|s| string(s)).collect())), + ]) + }); + } + // Saved data: weather, clocks, and (when missing or legacy) world + // generation settings and game rules. + write_saved_data( + dir, + "weather", + Tag::compound([ + ("clear_weather_time", Tag::Int(self.weather.clear_time)), + ("rain_time", Tag::Int(self.weather.rain_time)), + ("thunder_time", Tag::Int(self.weather.thunder_time)), + ("raining", Tag::Byte(i8::from(self.weather.raining))), + ("thundering", Tag::Byte(i8::from(self.weather.thundering))), + ]), + )?; + let mut clocks = compound_or_empty(read_saved_data(dir, "world_clocks")); + let mut overworld = compound_or_empty(clocks.get("minecraft:overworld").cloned()); + put(&mut overworld, "total_ticks", Tag::Long(self.day_time)); + put(&mut clocks, "minecraft:overworld", overworld); + if let Tag::Compound(map) = &mut clocks { + map.entry("minecraft:the_end".into()).or_insert_with(|| Tag::compound([("total_ticks", Tag::Long(self.time))])); + } + write_saved_data(dir, "world_clocks", clocks)?; + if legacy || !saved_data_path(dir, "world_gen_settings").exists() { + let dimension = |kind: &str, settings: &str, source: Tag| { + Tag::compound([ + ("type", string(kind)), + ("generator", Tag::compound([("type", string("minecraft:noise")), ("settings", string(settings)), ("biome_source", source)])), + ]) + }; + let preset = |p: &str| Tag::compound([("type", string("minecraft:multi_noise")), ("preset", string(p))]); + write_saved_data( + dir, + "world_gen_settings", + Tag::compound([ + ("seed", Tag::Long(self.seed)), + ("generate_structures", Tag::Byte(1)), + ("bonus_chest", Tag::Byte(0)), + ( + "dimensions", + Tag::compound([ + ("minecraft:overworld", dimension("minecraft:overworld", "minecraft:overworld", preset("minecraft:overworld"))), + ("minecraft:the_nether", dimension("minecraft:the_nether", "minecraft:nether", preset("minecraft:nether"))), + ("minecraft:the_end", dimension("minecraft:the_end", "minecraft:end", Tag::compound([("type", string("minecraft:the_end"))]))), + ]), + ), + ]), + )?; + } + if !saved_data_path(dir, "game_rules").exists() || !self.game_rules.is_empty() { + // Only rules vanilla knows: an unknown key makes it reject the file. + let mut rules = Tag::Compound(Compound::new()); + for (name, default) in GAME_RULES { + let value = self.game_rules.get(name).copied().unwrap_or(default); + let tag = if INT_RULES.contains(&name) { Tag::Int(value) } else { Tag::Byte(i8::from(value != 0)) }; + put(&mut rules, &format!("minecraft:{name}"), tag); + } + write_saved_data(dir, "game_rules", rules)?; + } + let player_level = Self { player_uuid: Some(uuid), ..self.clone() }; + write_dat(&dir.join("players").join("data").join(format!("{}.dat", uuid_text(uuid))), player_level.player_tag())?; + write_dat(&dir.join("level.dat"), Tag::compound([("Data", data)]))?; Ok(()) } } @@ -533,10 +1344,12 @@ impl ChunkGenerator for Persistent { self.inner.worker() } fn load(&self, x: i32, z: i32) -> Option { - let mut chunk = self.save.load(x, z, &self.registry.registry, &self.biomes)?; + self.save.tables.get_or_init(|| Arc::new(SaveTables::new(&self.registry))); + let mut chunk = self.save.load(x, z, self.registry.registry, &self.biomes)?; for s in &mut chunk.sections { Arc::make_mut(s).compact(); } + chunk.entities = self.save.load_entities(x, z); Some(chunk) } fn proto(&self, worker: &mut G::Worker, x: i32, z: i32) -> crate::streaming::Proto { @@ -552,3 +1365,83 @@ impl ChunkGenerator for Persistent { self.inner.finish(proto) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn uuid_text_round_trips() { + let u = [-158327916, 1572358581, -2125825222, -487948715]; + assert_eq!(uuid_from_text(&uuid_text(u)), Some(u)); + assert_eq!(uuid_text([0, 1, 2, 3]), "00000000-0000-0001-0000-000200000003"); + let r = random_uuid(); + assert_eq!((r[1] >> 12) & 0xF, 4, "version 4"); + } + #[test] + fn light_nibbles_use_vanilla_order() { + let mut a = Box::new([0u8; 4096]); + a[0] = 0xF3; // sky 15, block 3 at x=0 + a[1] = 0x52; // sky 5, block 2 at x=1 + let light = Light::Full(a); + assert_eq!(nibbles(&light, false)[0], 0x23); + assert_eq!(nibbles(&light, true)[0], 0x5F); + } + #[test] + fn level_round_trips_through_the_vanilla_files() { + let dir = std::env::temp_dir().join(format!("horizon-level-{}", std::process::id())); + let _ = fs::remove_dir_all(&dir); + let mut level = Level { + name: "Test".into(), + seed: -42, + time: 1234, + day_time: 25000, + creative: true, + position: Some([1.5, 70.0, -3.25]), + rotation: [90.0, 10.0], + health: 17.0, + food: 15, + saturation: 2.5, + inventory: vec![(0, "minecraft:stone".into(), 12, 0), (5, "minecraft:iron_sword".into(), 1, 3), (103, "minecraft:iron_helmet".into(), 1, 0)], + xp_level: 3, + xp_progress: 0.5, + xp_total: 30, + dimension: "minecraft:the_nether".into(), + world_spawn: Some([8, 64, 8]), + spawn: Some([20.0, 70.0, 20.0]), + ..Level::default() + }; + level.weather.raining = true; + level.weather.rain_time = 500; + level.game_rules.insert("keep_inventory".into(), 1); + level.write(&dir).unwrap(); + let back = Level::read(&dir).unwrap(); + assert_eq!(back.name, "Test"); + assert_eq!(back.seed, -42); + assert_eq!((back.time, back.day_time), (1234, 25000)); + assert!(back.creative); + assert_eq!(back.position, Some([1.5, 70.0, -3.25])); + assert_eq!(back.rotation, [90.0, 10.0]); + assert_eq!((back.health, back.food, back.saturation), (17.0, 15, 2.5)); + let mut inv = back.inventory.clone(); + inv.sort(); + assert_eq!(inv, vec![(0, "minecraft:stone".into(), 12, 0), (5, "minecraft:iron_sword".into(), 1, 3), (103, "minecraft:iron_helmet".into(), 1, 0)]); + assert_eq!((back.xp_level, back.xp_progress, back.xp_total), (3, 0.5, 30)); + assert_eq!(back.dimension, "minecraft:the_nether"); + assert_eq!(back.world_spawn, Some([8, 64, 8])); + assert_eq!(back.spawn, Some([20.0, 70.0, 20.0])); + assert!(back.weather.raining); + assert_eq!(back.weather.rain_time, 500); + assert_eq!(back.game_rule("keep_inventory"), 1); + assert_eq!(back.game_rule("random_tick_speed"), 3); + // Unknown player data survives another save. + let mut extra = back.player_extra.clone().unwrap(); + put(&mut extra, "EnderItems", Tag::List(vec![Tag::compound([("Slot", Tag::Byte(0)), ("id", string("minecraft:diamond")), ("count", Tag::Int(2))])])); + let again = Level { player_extra: Some(extra), ..back }; + again.write(&dir).unwrap(); + let third = Level::read(&dir).unwrap(); + assert!(matches!(third.player_extra.as_ref().and_then(|p| p.get("EnderItems")), Some(Tag::List(l)) if l.len() == 1)); + assert!(dir.join("data/minecraft/world_gen_settings.dat").exists()); + let _ = fs::remove_dir_all(&dir); + } +} diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index fd3298594..c69ae2046 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -277,6 +277,10 @@ impl Streaming { Done::Generated(mut chunk) => { let key = (chunk.x, chunk.z); world.block_entities.extend(std::mem::take(&mut chunk.block_entities)); + let entities = std::mem::take(&mut chunk.entities); + if !entities.is_empty() { + world.loaded_entities.push(((chunk.x, chunk.z), entities)); + } self.generating.remove(&key); self.finishing.remove(&key); self.decorated.insert(key); diff --git a/crates/horizon-client/src/world.rs b/crates/horizon-client/src/world.rs index 1941d87d4..32840e003 100644 --- a/crates/horizon-client/src/world.rs +++ b/crates/horizon-client/src/world.rs @@ -21,6 +21,8 @@ pub struct World { /// Edits on lit chunks (position, old state, new state), relit /// incrementally by streaming. pub light_edits: Vec<((i32, i32, i32), u16, u16)>, + /// Saved entities of chunks that just loaded, spawned by the game. + pub loaded_entities: Vec<((i32, i32), Vec)>, /// Chunks changed since the last save. pub modified: HashSet<(i32, i32)>, pub block_entities: HashMap<(i32, i32, i32), crate::containers::BlockEntity>, @@ -94,6 +96,7 @@ impl World { dirty_sections: HashSet::new(), dirty_light: HashSet::new(), light_edits: Vec::new(), + loaded_entities: Vec::new(), modified: HashSet::new(), block_entities: HashMap::new(), game_time: 0, From caefa4348366b00038a0e9e21fb8c95e9ed4dbd1 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 8 Oct 2026 15:49:59 +0200 Subject: [PATCH 3/3] Check Horizon player files in vanilla with a bare protocol client tools/vanilla_login.py joins an offline 26.3 server (login and configuration phases) so the server loads players/data/.dat; vanilla_world_check.py gains 'login NAME SECONDS' and 'sleep S' steps and disables the whitelist. With it, vanilla loads a Horizon player with its position, rotation, inventory, selected slot and abilities, and Horizon reads the file vanilla writes back. A new world's first spawn becomes its world spawn (no bogus respawn point), the player UUID is kept across saves, the selected hotbar slot is restored, and game_rules.dat only holds rules vanilla knows. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 1 + crates/horizon-client/src/save.rs | 46 +++++---- tools/vanilla_login.py | 160 ++++++++++++++++++++++++++++++ tools/vanilla_world_check.py | 23 ++++- 4 files changed, 204 insertions(+), 26 deletions(-) create mode 100644 tools/vanilla_login.py diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 6fc6dc311..b9ea4ae82 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2909,6 +2909,7 @@ impl Game<'_> { let (yaw, pitch) = self.options.look.unwrap_or((level.rotation[0], level.rotation[1])); self.yaw = yaw; self.pitch = pitch; + self.selected = level.selected_slot.clamp(0, 8) as usize; self.streaming = streaming; self.world = world; self.inventory = inventory; diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index b29300882..37430ea8e 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -1015,19 +1015,20 @@ impl Level { // file of a server world, or the legacy Data.Player. let uuid = uuid_from_tag(data.get("singleplayer_uuid")); let player_dir = dir.join("players").join("data"); - let player = match uuid { - Some(u) => read_dat(&player_dir.join(format!("{}.dat", uuid_text(u)))), - None => fs::read_dir(&player_dir) - .ok() - .and_then(|d| { - d.flatten() - .filter(|e| e.path().extension().is_some_and(|x| x == "dat")) - .max_by_key(|e| e.metadata().and_then(|m| m.modified()).ok()) - }) - .and_then(|e| read_dat(&e.path())), - } - .or_else(|| data.get("Player").cloned()); - level.player_uuid = uuid.or_else(|| uuid_from_tag(player.as_ref().and_then(|p| p.get("UUID")))); + let owner = uuid.and_then(|u| read_dat(&player_dir.join(format!("{}.dat", uuid_text(u))))); + let player = owner + .or_else(|| { + fs::read_dir(&player_dir) + .ok() + .and_then(|d| { + d.flatten() + .filter(|e| e.path().extension().is_some_and(|x| x == "dat")) + .max_by_key(|e| e.metadata().and_then(|m| m.modified()).ok()) + }) + .and_then(|e| read_dat(&e.path())) + }) + .or_else(|| data.get("Player").cloned()); + level.player_uuid = uuid_from_tag(player.as_ref().and_then(|p| p.get("UUID"))).or(uuid); let spawn = level.world_spawn.map(|s| [f64::from(s[0]), f64::from(s[1]), f64::from(s[2])]); level.spawn = spawn; if let Some(p) = &player { @@ -1182,7 +1183,8 @@ impl Level { } p } - pub fn write(&self, dir: &Path) -> Result<()> { + /// Write every file; a world without a player UUID gets a random one. + pub fn write(&mut self, dir: &Path) -> Result<()> { fs::create_dir_all(dir)?; let existing = read_dat(&dir.join("level.dat")).and_then(|r| r.get("Data").cloned()); let legacy = existing.as_ref().is_some_and(|d| d.get("Player").is_some() || d.get("WorldGenSettings").is_some()); @@ -1199,11 +1201,12 @@ impl Level { map.remove(&JavaString::from(key)); } } - let uuid = self.player_uuid.unwrap_or_else(random_uuid); - let spawn = self.world_spawn.unwrap_or_else(|| { - let s = self.spawn.unwrap_or([0.0, 64.0, 0.0]); - [s[0].floor() as i32, s[1].floor() as i32, s[2].floor() as i32] - }); + let uuid = *self.player_uuid.get_or_insert_with(random_uuid); + // A new world's first spawn point is its world spawn. + if self.world_spawn.is_none() { + self.world_spawn = self.spawn.map(|s| [s[0].floor() as i32, s[1].floor() as i32, s[2].floor() as i32]); + } + let spawn = self.world_spawn.unwrap_or([0, 64, 0]); let mut history: Vec = match data.get("version_history") { Some(Tag::List(l)) => l.clone(), _ => Vec::new(), @@ -1325,8 +1328,7 @@ impl Level { } write_saved_data(dir, "game_rules", rules)?; } - let player_level = Self { player_uuid: Some(uuid), ..self.clone() }; - write_dat(&dir.join("players").join("data").join(format!("{}.dat", uuid_text(uuid))), player_level.player_tag())?; + write_dat(&dir.join("players").join("data").join(format!("{}.dat", uuid_text(uuid))), self.player_tag())?; write_dat(&dir.join("level.dat"), Tag::compound([("Data", data)]))?; Ok(()) } @@ -1437,7 +1439,7 @@ mod tests { // Unknown player data survives another save. let mut extra = back.player_extra.clone().unwrap(); put(&mut extra, "EnderItems", Tag::List(vec![Tag::compound([("Slot", Tag::Byte(0)), ("id", string("minecraft:diamond")), ("count", Tag::Int(2))])])); - let again = Level { player_extra: Some(extra), ..back }; + let mut again = Level { player_extra: Some(extra), ..back }; again.write(&dir).unwrap(); let third = Level::read(&dir).unwrap(); assert!(matches!(third.player_extra.as_ref().and_then(|p| p.get("EnderItems")), Some(Tag::List(l)) if l.len() == 1)); diff --git a/tools/vanilla_login.py b/tools/vanilla_login.py new file mode 100644 index 000000000..c077a0546 --- /dev/null +++ b/tools/vanilla_login.py @@ -0,0 +1,160 @@ +#!/usr/bin/env python3 +"""Join an offline-mode vanilla 26.3 server as a bare protocol client, stay +connected for a while (answering keep-alives) and leave. + + vanilla_login.py [--host H] [--port P] [--seconds S] NAME + vanilla_login.py --uuid NAME print the offline UUID the server uses + +The server loads and saves the player's file (players/data/.dat), so +this checks that player data written by Horizon is accepted by vanilla: +its log reports the position it read, and console commands such as +`data get entity NAME` can inspect the loaded player meanwhile. +""" +import argparse +import hashlib +import socket +import struct +import sys +import time +import uuid +import zlib + +PROTOCOL = 777 # 26.3 + + +def offline_uuid(name): + digest = bytearray(hashlib.md5(('OfflinePlayer:' + name).encode()).digest()) + digest[6] = (digest[6] & 0x0F) | 0x30 + digest[8] = (digest[8] & 0x3F) | 0x80 + return uuid.UUID(bytes=bytes(digest)) + + +def varint(v): + out = b'' + v &= 0xFFFFFFFF + while True: + b = v & 0x7F + v >>= 7 + out += bytes([b | (0x80 if v else 0)]) + if not v: + return out + + +def string(s): + data = s.encode() + return varint(len(data)) + data + + +class Connection: + def __init__(self, host, port): + self.sock = socket.create_connection((host, port), timeout=30) + self.threshold = -1 + self.buffer = b'' + + def read_exact(self, n): + while len(self.buffer) < n: + chunk = self.sock.recv(65536) + if not chunk: + raise EOFError('server closed the connection') + self.buffer += chunk + out, self.buffer = self.buffer[:n], self.buffer[n:] + return out + + def read_varint(self): + value = shift = 0 + while True: + b = self.read_exact(1)[0] + value |= (b & 0x7F) << shift + shift += 7 + if not b & 0x80: + return value + + def send(self, packet_id, payload=b''): + body = varint(packet_id) + payload + if self.threshold >= 0: + if len(body) >= self.threshold: + body = varint(len(body)) + zlib.compress(body) + else: + body = varint(0) + body + self.sock.sendall(varint(len(body)) + body) + + def receive(self): + length = self.read_varint() + body = self.read_exact(length) + if self.threshold >= 0: + pos, size = parse_varint(body, 0) + body = zlib.decompress(body[pos:]) if size else body[pos:] + pos, packet_id = parse_varint(body, 0) + return packet_id, body[pos:] + + +def parse_varint(data, pos): + value = shift = 0 + while True: + b = data[pos] + pos += 1 + value |= (b & 0x7F) << shift + shift += 7 + if not b & 0x80: + return pos, value + + +def join(host, port, name, seconds): + c = Connection(host, port) + c.send(0, varint(PROTOCOL) + string(host) + struct.pack('>H', port) + varint(2)) + c.send(0, string(name) + offline_uuid(name).bytes) + # Login: compression, then login finished. + while True: + pid, data = c.receive() + if pid == 3: + c.threshold = parse_varint(data, 0)[1] + elif pid == 2: + break + elif pid == 0: + raise RuntimeError(f'login refused: {data!r}') + c.send(3) # login acknowledged -> configuration + while True: + pid, data = c.receive() + if pid == 15: # select known packs: claim none, receive full registries + c.send(7, varint(0)) + elif pid == 4: # keep alive + c.send(4, data) + elif pid == 5: # ping + c.send(5, data) + elif pid == 3: # finish configuration + c.send(3) + break + elif pid == 2: + raise RuntimeError(f'disconnected during configuration: {data!r}') + deadline = time.time() + seconds + c.sock.settimeout(1.0) + while time.time() < deadline: + try: + c.receive() + except socket.timeout: + continue + except EOFError: + break # the server stopped + # Play packets are not decoded: the server's first keep-alive comes + # after 15 seconds and it waits 15 more for the answer, so a session + # under 30 seconds needs none. + c.sock.close() + + +def main(): + p = argparse.ArgumentParser() + p.add_argument('name') + p.add_argument('--host', default='127.0.0.1') + p.add_argument('--port', type=int, default=25565) + p.add_argument('--seconds', type=float, default=5, help='at most 25') + p.add_argument('--uuid', action='store_true') + args = p.parse_args() + if args.uuid: + print(offline_uuid(args.name)) + return 0 + join(args.host, args.port, args.name, args.seconds) + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tools/vanilla_world_check.py b/tools/vanilla_world_check.py index 81ad61b72..2cf1c06fa 100644 --- a/tools/vanilla_world_check.py +++ b/tools/vanilla_world_check.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 """Run the vanilla 26.3 dedicated server headless on a world folder, optionally -send console commands (`sleep SECONDS` pauses between them), then stop it cleanly and report problems from its log. +send console commands (`sleep SECONDS` pauses between them; `login NAME +SECONDS` joins with tools/vanilla_login.py in the background), then stop it +cleanly and report problems from its log. vanilla_world_check.py --jar server.jar --dir RUN_DIR [--world NAME] [--seed N] [--port P] [--command CMD ...] @@ -21,10 +23,10 @@ import threading import time SUSPICIOUS = re.compile( - r'(ERROR|Exception|Couldn\'t load|Failed to|Skipping|Invalid|Unknown|corrupt|' + r'(ERROR|login failed|Exception|Couldn\'t load|Failed to|Skipping|Invalid|Unknown|corrupt|' r'Recreating|upgrad|DataFix|out of bounds|missing|Mismatch)', re.IGNORECASE) # Benign lines the server prints on every start. -BENIGN = re.compile(r'(OFFLINE/INSECURE|online-mode|authenticate usernames|hackers|' +BENIGN = re.compile(r'(Unpacking |OFFLINE/INSECURE|online-mode|authenticate usernames|hackers|' r'Failed to load properties|Failed to load eula)') @@ -47,7 +49,7 @@ def main(): 'level-name': args.world, 'spawn-protection': '0', 'view-distance': '4', 'simulation-distance': '4', 'sync-chunk-writes': 'true', 'enable-query': 'false', 'enable-rcon': 'false', 'management-server-enabled': 'false', - 'pause-when-empty-seconds': '-1', + 'pause-when-empty-seconds': '-1', 'white-list': 'false', 'enforce-whitelist': 'false', } with open(os.path.join(args.dir, 'server.properties'), 'w') as f: f.writelines(f'{k}={v}\n' for k, v in props.items()) @@ -74,6 +76,19 @@ def main(): if command.startswith('sleep '): time.sleep(float(command.split()[1])) continue + if command.startswith('login '): + sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) + import vanilla_login + _, name, seconds = command.split() + + def run(name=name, seconds=float(seconds)): + try: + vanilla_login.join('127.0.0.1', int(args.port), name, seconds) + except Exception as e: # reported in the log check below + lines.append(f'login failed: {e}') + + threading.Thread(target=run, daemon=True).start() + continue proc.stdin.write(command + '\n') proc.stdin.flush() time.sleep(0.5)