diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index d407ec32f..eec4b9eeb 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -10,6 +10,7 @@ use crate::{ world::World, }; use horizon_core::biome::BiomeZoom; +use horizon_world::worldgen::decoration_order::DecorationOrder; use std::{ collections::{BinaryHeap, HashSet}, sync::{ @@ -104,6 +105,10 @@ pub struct Streaming { protos: FxHashMap<(i32, i32), Option>, decorated: FxHashSet<(i32, i32)>, decorating: usize, + /// Vanilla's decoration order around the player, and the centre and + /// radius it was last ranked for. + decoration_order: DecorationOrder, + order_area: Option<((i32, i32), i32)>, finishing: FxHashSet<(i32, i32)>, decorates: bool, lighting: FxHashMap<(i32, i32), u64>, @@ -239,6 +244,8 @@ impl Streaming { protos: Default::default(), decorated: Default::default(), decorating: 0, + decoration_order: DecorationOrder::default(), + order_area: None, finishing: Default::default(), decorates: generator.decorates(), lighting: Default::default(), @@ -512,24 +519,82 @@ impl Streaming { self.submit(self.distance(x, z) * 4, Job::Finish(proto)); } } - // Decorate chunks whose 3x3 proto neighbourhood is available. + // Decorate chunks whose 3x3 proto neighbourhood is available, in + // vanilla's order: decoration is order dependent where write regions + // overlap (see `DecorationOrder`). let workers = self.threads.len(); + if self.decorates && self.order_area != Some((self.center, self.radius)) { + let (decorated, chunks) = (&self.decorated, &world.chunks); + // The render distance is the server view distance in singleplayer. + self.decoration_order + .reassign(self.center, self.radius, |x, z| { + decorated.contains(&(x, z)) || chunks.contains_key(&(x, z)) + }); + self.order_area = Some((self.center, self.radius)); + let ranges = Ranges { + center: self.center, + radius: self.radius, + extra: &self.extra, + forced: &self.forced, + }; + let (protos, generating) = (&self.protos, &self.generating); + self.decoration_order.retain(|x, z| { + ranges.in_range(x, z, 4) + || ranges.near_finished(x, z, 2) + || protos.contains_key(&(x, z)) + || generating.contains(&(x, z)) + }); + } if self.decorates && self.decorating < workers * 2 { - let mut wanted: Vec<(i64, i32, i32)> = self + // The generation margins below. + let (margin, reach) = (3, 2); + let wants = |s: &Self, x: i32, z: i32| { + !s.decorated.contains(&(x, z)) + && !world.chunks.contains_key(&(x, z)) + && (s.in_range(x, z, 2) || s.near_finished(x, z, 1)) + }; + // A wanted chunk holds back higher-ranked ones only if it can be + // decorated: its 3x3 neighbourhood is (or will be) generated as + // protos, none of them already final. + let waits_for = |s: &Self, x: i32, z: i32| { + wants(s, x, z) + && (-1..=1).all(|dz| { + (-1..=1).all(|dx| { + let n = (x + dx, z + dz); + !world.chunks.contains_key(&n) + && (s.protos.contains_key(&n) + || s.generating.contains(&n) + || s.in_range(n.0, n.1, margin) + || s.near_finished(n.0, n.1, reach)) + }) + }) + }; + let keys: Vec<(i32, i32)> = self .protos .iter() - .filter(|(k, p)| { - p.is_some() - && !self.decorated.contains(k) - && (self.in_range(k.0, k.1, 2) || self.near_finished(k.0, k.1, 1)) - }) - .map(|(k, _)| (self.distance(k.0, k.1), k.0, k.1)) + .filter(|(k, p)| p.is_some() && wants(self, k.0, k.1)) + .map(|(k, _)| *k) .collect(); + let mut wanted: Vec<(u64, i64, i32, i32)> = keys + .into_iter() + .map(|(x, z)| (0, self.distance(x, z), x, z)) + .collect(); + // Chunks no ticket reached (forced and extra areas) go last, nearest first. + wanted.sort_unstable_by_key(|w| w.1); + for w in &mut wanted { + w.0 = self.decoration_order.rank_or_append(w.2, w.3); + } wanted.sort_unstable(); - for (d, x, z) in wanted { + for (_, d, x, z) in wanted { if self.decorating >= workers * 2 { break; } + if self + .decoration_order + .blocked(x, z, |nx, nz| waits_for(self, nx, nz)) + { + continue; + } let keys: Vec<(i32, i32)> = (0..9).map(|i| (x + (i % 3) - 1, z + (i / 3) - 1)).collect(); // A final (saved) neighbour cannot receive writes: skip decoration. diff --git a/crates/horizon-client/src/vanillagen.rs b/crates/horizon-client/src/vanillagen.rs index 14f432fa3..9b48edb18 100644 --- a/crates/horizon-client/src/vanillagen.rs +++ b/crates/horizon-client/src/vanillagen.rs @@ -20,6 +20,9 @@ pub struct VanillaTerrain { loot: Option>, /// A worker for structure queries from the game thread. query: std::sync::Mutex>, + /// Block state id -> block index, the game's block tick type: worldgen + /// schedules block ticks by default state id. + tick_blocks: Vec, } /// A proto chunk, or the position of one whose generation failed. enum VanillaProto { @@ -64,11 +67,25 @@ impl VanillaTerrain { } } let loot = dir.parent().and_then(crate::structure_blocks::loot_data); + let mut tick_blocks = Vec::with_capacity(registry.len()); + let mut last: Option<&str> = None; + let mut index = 0u32; + for id in 0..registry.len() { + let name = registry + .state(horizon_world::registry::BlockStateId(id as u32)) + .map_or("", |s| s.name.as_str()); + if last.is_some_and(|l| l != name) { + index += 1; + } + last = Some(name); + tick_blocks.push(index); + } Ok(Self { generator, biome_map, loot, query: std::sync::Mutex::new(None), + tick_blocks, }) } /// The structure starts owned by a chunk (cached by the generator). @@ -217,6 +234,30 @@ impl ChunkGenerator for VanillaTerrain { Section::from_blocks(&values); } } + // Ticks the features scheduled (leaves reshaped at a tree's edge, + // fluids of lakes and springs) run once the chunk is live, as + // LevelChunk takes over ProtoChunk's ticks. + let block_ticks: Vec<_> = chunk + .terrain + .block_ticks() + .pack(0) + .iter() + .filter_map(|t| { + let kind = *self.tick_blocks.get(t.kind.0 as usize)?; + Some(horizon_core::ticks::SavedTick { + kind: horizon_core::ticks::TickTypeId(kind), + ..*t + }) + }) + .collect(); + let fluid_ticks = chunk.terrain.fluid_ticks().pack(0).to_vec(); + if !block_ticks.is_empty() || !fluid_ticks.is_empty() { + out.saved = Some(Arc::new(crate::save::ChunkExtras { + block_ticks, + fluid_ticks, + ..Default::default() + })); + } let base_section = chunk.terrain.min_y() >> 4; for (i, marks) in chunk.terrain.postprocessing().iter().enumerate() { let sy = (base_section + i as i32) * 16; diff --git a/crates/horizon-world/examples/chunk_dump.rs b/crates/horizon-world/examples/chunk_dump.rs index b72a5e05e..26f8f6adf 100644 --- a/crates/horizon-world/examples/chunk_dump.rs +++ b/crates/horizon-world/examples/chunk_dump.rs @@ -4,11 +4,13 @@ //! With HORIZON_SCAN set, one rectangle lists the structure starts in it. //! //! Chunks are generated through biomes, terrain and carvers, then decorated in -//! row-major order over the target rectangle grown by one chunk, each with its -//! eight neighbours so feature spill persists, exactly as the oracle does. +//! row-major order over the target rectangle grown by one chunk (or in the +//! order HORIZON_ORDER lists), each with its eight neighbours so feature spill +//! persists, exactly as the oracle does. //! For every target chunk this prints `chunk X Z`, one `q QY NAME...` line per //! quart layer (16 columns, z-major), one `y Y ID...` line per block row and -//! one `be X Y Z CANONICAL_NBT` line per block entity. +//! one `be X Y Z CANONICAL_NBT` line per block entity and one `t block|fluid +//! NAME X Y Z PRIORITY` line per scheduled tick. use horizon_core::position::ChunkPos; use horizon_world::{ DATA_VERSION, @@ -116,7 +118,22 @@ fn main() -> Result<(), Error> { Ok(v) }) .collect::, Error>>()?; - let data = GeneratorData::load(&resources.join("worldgen"), registry(&resources)?)?; + let blocks = registry(&resources)?; + // HORIZON_LIST_ORDER=x,z,view: print the chunks vanilla decorates around a + // player standing in chunk x,z (`decoration_order::vanilla_order`) and exit. + if let Some(at) = std::env::var_os("HORIZON_LIST_ORDER") { + let v: Vec = at + .to_string_lossy() + .split(',') + .map(str::parse) + .collect::>()?; + let [x, z, view]: [i32; 3] = v.try_into().map_err(|_| "HORIZON_LIST_ORDER=x,z,view")?; + for (x, z) in horizon_world::worldgen::decoration_order::vanilla_order((x, z), view) { + println!("{x} {z}"); + } + return Ok(()); + } + let data = GeneratorData::load(&resources.join("worldgen"), blocks)?; let generator = OverworldGenerator::for_dimension(data, seed, "minecraft:normal", &dimension)?; let mut worker = generator.worker(); // HORIZON_PHASE: terrain (no decoration), structures (pieces only) or full. @@ -171,31 +188,43 @@ fn main() -> Result<(), Error> { } let mut failed = 0; let margin = i32::from(std::env::var_os("HORIZON_ISOLATED").is_none()); - for z in z0 - margin..=z1 + margin { - for x in x0 - margin..=x1 + margin { - let mut region = Vec::with_capacity(9); - for dz in -1..=1 { - for dx in -1..=1 { - region.push(chunks.remove(&(x + dx, z + dz)).ok_or("missing chunk")?); - } - } - let mut region: [ProtoChunk; 9] = region.try_into().map_err(|_| "region size")?; - let stats = if phase == "terrain" { - Default::default() - } else { - worker.decorate(&mut region)? - }; - if stats.failed > 0 || stats.unsupported > 0 { - failed += 1; - eprintln!( - "chunk {x} {z}: {} failed, {} unsupported {:?}", - stats.failed, stats.unsupported, stats.skipped - ); - } - for (i, chunk) in region.into_iter().enumerate() { - chunks.insert((x + i as i32 % 3 - 1, z + i as i32 / 3 - 1), chunk); + // HORIZON_ORDER: a file of `x z` lines, the chunks to decorate in that + // order (the oracle reads the same file); row-major by default. + let order: Vec<(i32, i32)> = match std::env::var_os("HORIZON_ORDER") { + Some(path) => fs::read_to_string(path)? + .lines() + .filter_map(|l| { + let mut t = l.split_whitespace().map(str::parse::); + Some((t.next()?.ok()?, t.next()?.ok()?)) + }) + .collect(), + None => (z0 - margin..=z1 + margin) + .flat_map(|z| (x0 - margin..=x1 + margin).map(move |x| (x, z))) + .collect(), + }; + for (x, z) in order { + let mut region = Vec::with_capacity(9); + for dz in -1..=1 { + for dx in -1..=1 { + region.push(chunks.remove(&(x + dx, z + dz)).ok_or("missing chunk")?); } } + let mut region: [ProtoChunk; 9] = region.try_into().map_err(|_| "region size")?; + let stats = if phase == "terrain" { + Default::default() + } else { + worker.decorate(&mut region)? + }; + if stats.failed > 0 || stats.unsupported > 0 { + failed += 1; + eprintln!( + "chunk {x} {z}: {} failed, {} unsupported {:?}", + stats.failed, stats.unsupported, stats.skipped + ); + } + for (i, chunk) in region.into_iter().enumerate() { + chunks.insert((x + i as i32 % 3 - 1, z + i as i32 / 3 - 1), chunk); + } } if failed > 0 { eprintln!("{failed} chunks had failed or unsupported features"); @@ -233,6 +262,36 @@ fn main() -> Result<(), Error> { canonical(&Tag::Compound(nbt.clone()), &mut text); writeln!(w, "be {bx} {by} {bz}\t{text}")?; } + // Scheduled ticks, in scheduling order (the order they run in). + for t in chunk.terrain.block_ticks().pack(0) { + let name = blocks + .state(horizon_world::registry::BlockStateId(t.kind.0)) + .map_or("?", |s| s.name.as_str()); + let p = t.pos; + writeln!( + w, + "t block {name} {} {} {} {}", + p.x, p.y, p.z, t.priority as i32 + )?; + } + for t in chunk.terrain.fluid_ticks().pack(0) { + let name = [ + "minecraft:empty", + "minecraft:flowing_water", + "minecraft:water", + "minecraft:flowing_lava", + "minecraft:lava", + ] + .get(t.kind.0 as usize) + .copied() + .unwrap_or("?"); + let p = t.pos; + writeln!( + w, + "t fluid {name} {} {} {} {}", + p.x, p.y, p.z, t.priority as i32 + )?; + } } } } diff --git a/crates/horizon-world/src/level/ticks.rs b/crates/horizon-world/src/level/ticks.rs index c6be4282f..fbad6c28d 100644 --- a/crates/horizon-world/src/level/ticks.rs +++ b/crates/horizon-world/src/level/ticks.rs @@ -34,6 +34,18 @@ pub struct Ticks { pub block_events: Vec, /// `ServerLevel.isHandlingTick` (true while scheduled ticks run). pub(crate) handling_tick: bool, + /// Worldgen shape updates collect the ticks they schedule here, for the + /// proto chunks of their region, instead of scheduling them. + pub(crate) capture: Option>, +} +/// A tick scheduled while `Ticks::capture` is set. +#[derive(Clone, Copy, Debug)] +pub(crate) struct CapturedTick { + pub fluid: bool, + /// `BlockId` for block ticks, `FluidKind` for fluid ticks. + pub kind: u32, + pub pos: BlockPos, + pub delay: i32, } impl Default for Ticks { fn default() -> Self { @@ -56,6 +68,7 @@ impl Ticks { next_block_entity_id: 0, block_events: Vec::new(), handling_tick: false, + capture: None, } } /// Register a loaded chunk's saved block and fluid ticks. @@ -89,6 +102,15 @@ impl Ticks { delay: i32, priority: TickPriority, ) -> ScheduleResult { + if let Some(captured) = &mut self.capture { + captured.push(CapturedTick { + fluid: false, + kind: u32::from(block.0), + pos, + delay, + }); + return ScheduleResult::Added; + } let tick = ScheduledTick { kind: TickTypeId(u32::from(block.0)), pos, @@ -105,6 +127,15 @@ impl Ticks { fluid: FluidKind, delay: i32, ) -> ScheduleResult { + if let Some(captured) = &mut self.capture { + captured.push(CapturedTick { + fluid: true, + kind: fluid as u32, + pos, + delay, + }); + return ScheduleResult::Added; + } let tick = ScheduledTick { kind: TickTypeId(fluid as u32), pos, diff --git a/crates/horizon-world/src/worldgen.rs b/crates/horizon-world/src/worldgen.rs index 4c9295af7..a3f8487c4 100644 --- a/crates/horizon-world/src/worldgen.rs +++ b/crates/horizon-world/src/worldgen.rs @@ -23,6 +23,7 @@ pub mod cave; pub mod chorus_plant; pub mod climate; pub mod decoration; +pub mod decoration_order; pub mod delta; pub mod density; pub mod disk; diff --git a/crates/horizon-world/src/worldgen/decoration_order.rs b/crates/horizon-world/src/worldgen/decoration_order.rs new file mode 100644 index 000000000..52649ceed --- /dev/null +++ b/crates/horizon-world/src/worldgen/decoration_order.rs @@ -0,0 +1,266 @@ +//! The order in which chunks around a player are decorated. +//! +//! Biome decoration is order dependent: a chunk's features write into its +//! eight neighbours, and a tree, ore blob or sculk patch placed first changes +//! where (and whether) the next chunk's features fit, and so how many random +//! draws they take. Vanilla decorates on a single `worldgen` executor, in an +//! order its chunk system makes nearly deterministic: player loading tickets +//! are added nearest first, four at a time (`ThrottlingChunkTaskDispatcher`), +//! generation tasks run by ticket level, and each `ChunkGenerationTask` +//! decorates its square layer x-outer, z-inner. Two vanilla runs on the same +//! seed decorate the same chunks around a teleported player with about 6% of +//! the overlapping chunk pairs swapped; a world decorated in any other order +//! grows visibly different forests, ore veins and sculk. +//! +//! [`DecorationOrder`] replays that scheduler for a player standing still, +//! with every asynchronous step finishing at once: player tickets in rings of +//! Chebyshev distance, each ring in the iteration order of the fastutil hash +//! set that queues them (`PlayerTicketTracker.toUpdate`, at the 8192-slot +//! capacity a single player's area gives it), at most four in flight; each +//! ticket raises the levels around it, a chunk at level 33 or less gets a +//! FULL task that decorates its 3x3 square, one at level 34 an +//! INITIALIZE_LIGHT task that decorates itself; tasks run by level, then in +//! the order they were queued, and a ticket is released once the 5x5 square +//! around it is FULL. Against vanilla's own order (JFR +//! `minecraft.ChunkGeneration` events) it swaps as many overlapping pairs as +//! a second vanilla run does. +//! +//! Only chunks within two of each other interact (their write regions +//! overlap), so a chunk may be decorated as soon as no lower-ranked chunk +//! within two of it is still waiting: [`DecorationOrder::blocked`]. Chunks +//! further apart still decorate in parallel. +use std::collections::HashMap; + +#[derive(Default)] +pub struct DecorationOrder { + rank: HashMap<(i32, i32), u64>, + next: u64, +} + +impl DecorationOrder { + /// The rank of a chunk; lower ranks decorate first. + pub fn rank(&self, x: i32, z: i32) -> Option { + self.rank.get(&(x, z)).copied() + } + + /// Rank the chunks a player at `center` with view distance `radius` + /// gets decorated, in the order vanilla decorates them, after every chunk + /// ranked before; `decorated` ones are skipped. Ranked chunks that are + /// still undecorated are ranked again, as vanilla re-sorts queued tickets + /// when the player moves. + pub fn reassign( + &mut self, + center: (i32, i32), + radius: i32, + decorated: impl Fn(i32, i32) -> bool, + ) { + for (x, z) in vanilla_order(center, radius) { + if !decorated(x, z) { + self.rank.insert((x, z), self.next); + self.next += 1; + } + } + } + + /// Rank a chunk that no ticket reached (forced or extra areas), after + /// every ranked one. + pub fn rank_or_append(&mut self, x: i32, z: i32) -> u64 { + let next = &mut self.next; + *self.rank.entry((x, z)).or_insert_with(|| { + *next += 1; + *next - 1 + }) + } + + /// Whether a lower-ranked chunk within two of `(x, z)` still has to be + /// decorated (`pending`) first. + pub fn blocked(&self, x: i32, z: i32, pending: impl Fn(i32, i32) -> bool) -> bool { + let Some(own) = self.rank(x, z) else { + return false; + }; + (-2..=2).any(|dz| { + (-2..=2).any(|dx| { + (dx, dz) != (0, 0) + && self.rank(x + dx, z + dz).is_some_and(|r| r < own) + && pending(x + dx, z + dz) + }) + }) + } + + /// Forget the ranks `keep` rejects. + pub fn retain(&mut self, mut keep: impl FnMut(i32, i32) -> bool) { + self.rank.retain(|&(x, z), _| keep(x, z)); + } + + /// Every ranked chunk, lowest rank first. + pub fn ordered(&self) -> Vec<(i32, i32)> { + let mut all: Vec<_> = self.rank.iter().map(|(&k, &r)| (r, k)).collect(); + all.sort_unstable(); + all.into_iter().map(|(_, k)| k).collect() + } +} + +/// `ThrottlingChunkTaskDispatcher`'s limit for player loading tickets. +const TICKETS_IN_FLIGHT: usize = 4; +/// Capacity of `PlayerTicketTracker.toUpdate` around one player. +const TICKET_SET_CAPACITY: u64 = 8192; +/// `ChunkLevel.byStatus(ENTITY_TICKING)`, the player loading ticket's level. +const TICKET_LEVEL: i32 = 31; +/// The highest level that gets a FULL task, and the INITIALIZE_LIGHT level. +const FULL_LEVEL: i32 = 33; +const LIGHT_LEVEL: i32 = 34; + +/// `HashCommon.mix(ChunkPos.pack(x, z))`. +fn ticket_hash(x: i32, z: i32) -> u64 { + let packed = u64::from(x as u32) | (u64::from(z as u32) << 32); + let h = packed.wrapping_mul(0x9E37_79B9_7F4A_7C15); + let h = h ^ (h >> 32); + h ^ (h >> 16) +} + +/// The chunks decorated around a player at `center`, in vanilla's order. +pub fn vanilla_order(center: (i32, i32), radius: i32) -> Vec<(i32, i32)> { + use std::cmp::Reverse; + use std::collections::{BinaryHeap, HashSet, VecDeque}; + let mut tickets: Vec<(i32, i32)> = (-radius..=radius) + .flat_map(|dx| (-radius..=radius).map(move |dz| (center.0 + dx, center.1 + dz))) + .collect(); + // A fastutil open hash set iterates its slots from the last to the first. + tickets.sort_by_key(|&(x, z)| { + ( + (x - center.0).abs().max((z - center.1).abs()), + Reverse(ticket_hash(x, z) & (TICKET_SET_CAPACITY - 1)), + ) + }); + let mut tickets: VecDeque<_> = tickets.into(); + let mut active: Vec<(i32, i32)> = Vec::new(); + // Queued task per chunk (level, sequence); stale heap entries are skipped. + let mut queued: HashMap<(i32, i32), (i32, u64)> = HashMap::new(); + let mut heap = BinaryHeap::new(); + let mut sequence = 0u64; + let mut full = HashSet::new(); + let mut seen = HashSet::new(); + let mut order = Vec::new(); + // Each ticket lowers the levels around it to 31 + its Chebyshev distance. + let mut levels: HashMap<(i32, i32), i32> = HashMap::new(); + let mut add = |ticket: (i32, i32), + active: &mut Vec<(i32, i32)>, + queued: &mut HashMap<(i32, i32), (i32, u64)>, + heap: &mut BinaryHeap>, + full: &HashSet<(i32, i32)>| { + active.push(ticket); + let reach = LIGHT_LEVEL - TICKET_LEVEL; + for x in ticket.0 - reach..=ticket.0 + reach { + for z in ticket.1 - reach..=ticket.1 + reach { + let own = TICKET_LEVEL + (x - ticket.0).abs().max((z - ticket.1).abs()); + let l = levels.entry((x, z)).or_insert(i32::MAX); + *l = (*l).min(own); + let l = *l; + if full.contains(&(x, z)) { + continue; + } + if queued.get(&(x, z)).is_none_or(|&(old, _)| l < old) { + sequence += 1; + queued.insert((x, z), (l, sequence)); + heap.push(Reverse((l, sequence, (x, z)))); + } + } + } + }; + for _ in 0..TICKETS_IN_FLIGHT { + if let Some(t) = tickets.pop_front() { + add(t, &mut active, &mut queued, &mut heap, &full); + } + } + while let Some(Reverse((l, s, chunk))) = heap.pop() { + if queued.get(&chunk) != Some(&(l, s)) { + continue; + } + queued.remove(&chunk); + let r = i32::from(l <= FULL_LEVEL); + for x in chunk.0 - r..=chunk.0 + r { + for z in chunk.1 - r..=chunk.1 + r { + if seen.insert((x, z)) { + order.push((x, z)); + } + } + } + if l <= FULL_LEVEL { + full.insert(chunk); + } + active.retain(|t| { + !(-2..=2).all(|dx| (-2..=2).all(|dz| full.contains(&(t.0 + dx, t.1 + dz)))) + }); + while active.len() < TICKETS_IN_FLIGHT { + let Some(t) = tickets.pop_front() else { break }; + add(t, &mut active, &mut queued, &mut heap, &full); + } + } + order +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn first_wave_matches_vanilla() { + // Seed 1891623539983467248, player teleported into chunk 12,-15 with + // view distance 8: vanilla (JFR) decorates the player's 3x3 square, + // then the rest of the squares of the tickets at 12,-14, 11,-15 and + // 13,-14. + let order = vanilla_order((12, -15), 8); + let rel: Vec<_> = order + .iter() + .take(18) + .map(|&(x, z)| (x - 12, z + 15)) + .collect(); + assert_eq!( + rel, + [ + (-1, -1), + (-1, 0), + (-1, 1), + (0, -1), + (0, 0), + (0, 1), + (1, -1), + (1, 0), + (1, 1), + (-1, 2), + (0, 2), + (1, 2), + (-2, -1), + (-2, 0), + (-2, 1), + (2, 0), + (2, 1), + (2, 2), + ] + ); + let unique: std::collections::HashSet<_> = order.iter().collect(); + assert_eq!(unique.len(), order.len()); + assert_eq!(order.len(), 23 * 23); + } + + #[test] + fn blocked_by_lower_ranks_within_two() { + let mut order = DecorationOrder::default(); + order.reassign((0, 0), 2, |_, _| false); + let first = order.ordered()[0]; + assert!(!order.blocked(first.0, first.1, |_, _| true)); + let last = *order.ordered().last().unwrap(); + assert!(order.blocked(last.0, last.1, |_, _| true)); + assert!(!order.blocked(last.0, last.1, |_, _| false)); + } + + #[test] + fn decorated_chunks_keep_their_place() { + let mut order = DecorationOrder::default(); + order.reassign((0, 0), 1, |_, _| false); + let before = order.rank(0, 0); + order.reassign((10, 0), 1, |x, _| x < 5); + assert_eq!(order.rank(0, 0), before); + assert!(order.rank(10, 0) > order.rank(2, 2)); + } +} diff --git a/crates/horizon-world/src/worldgen/generator_decoration.rs b/crates/horizon-world/src/worldgen/generator_decoration.rs index 655de6a33..e8028dd5a 100644 --- a/crates/horizon-world/src/worldgen/generator_decoration.rs +++ b/crates/horizon-world/src/worldgen/generator_decoration.rs @@ -511,13 +511,16 @@ impl World<'_> { direction: Direction, neighbor_pos: [i32; 3], neighbor: BlockStateId, - ) -> BlockStateId { + ) -> (BlockStateId, Vec) { let mut level = ShapeLevel { world: self, random: horizon_core::random::LegacyRandom::new(0), }; + // The ticks updateShape schedules (LeavesBlock re-measuring its + // distance, waterlogged blocks' fluid) go to the proto chunks. let mut ticks = crate::level::Ticks::new(0); - behaviours.update_shape( + ticks.capture = Some(Vec::new()); + let updated = behaviours.update_shape( &mut level, &mut ticks, state, @@ -525,7 +528,28 @@ impl World<'_> { direction, BlockPos::new(neighbor_pos[0], neighbor_pos[1], neighbor_pos[2]), neighbor, - ) + ); + (updated, ticks.capture.take().unwrap_or_default()) + } + /// Schedule ticks a shape update requested in the region's proto chunks. + fn schedule_captured( + &mut self, + behaviours: &crate::level::Behaviours, + ticks: Vec, + ) -> Result<(), PlacementQueryError> { + for t in ticks { + let at = [t.pos.x, t.pos.y, t.pos.z]; + if t.fluid { + let fluid = crate::fluid::FluidKind::ALL[t.kind as usize]; + self.schedule_fluid_tick(at, fluid, t.delay)?; + } else { + let state = behaviours + .table + .default_state(crate::level::BlockId(t.kind as u16)); + self.schedule_block_tick(at, state, t.delay)?; + } + } + Ok(()) } } impl DecorationWorld for World<'_> { @@ -879,7 +903,7 @@ impl PlacementContext for World<'_> { let neighbor_pos = [position[0] + n[0], position[1] + n[1], position[2] + n[2]]; let state = self.region.block(position).map_err(query)?; let neighbor = self.region.block(neighbor_pos).map_err(query)?; - let new_state = self.shape_update( + let (new_state, ticks) = self.shape_update( behaviours, state, position, @@ -887,11 +911,12 @@ impl PlacementContext for World<'_> { neighbor_pos, neighbor, ); + self.schedule_captured(behaviours, ticks)?; if new_state != state { self.set_block(position, new_state, flags & !1) .map_err(query)?; } - let new_neighbor = self.shape_update( + let (new_neighbor, ticks) = self.shape_update( behaviours, neighbor, neighbor_pos, @@ -899,6 +924,7 @@ impl PlacementContext for World<'_> { position, new_state, ); + self.schedule_captured(behaviours, ticks)?; if new_neighbor != neighbor { self.set_block(neighbor_pos, new_neighbor, flags & !1) .map_err(query)?; @@ -927,7 +953,7 @@ impl PlacementContext for World<'_> { let n = direction.normal(); let neighbor_pos = [position[0] + n[0], position[1] + n[1], position[2] + n[2]]; let neighbor = self.region.block(neighbor_pos).map_err(query)?; - new_state = self.shape_update( + let (updated, ticks) = self.shape_update( behaviours, new_state, position, @@ -935,6 +961,8 @@ impl PlacementContext for World<'_> { neighbor_pos, neighbor, ); + self.schedule_captured(behaviours, ticks)?; + new_state = updated; } if new_state != state { self.set_block(position, new_state, flags).map_err(query)?; diff --git a/crates/horizon-world/src/worldgen/structure/legacy.rs b/crates/horizon-world/src/worldgen/structure/legacy.rs index e55209baf..f558081ee 100644 --- a/crates/horizon-world/src/worldgen/structure/legacy.rs +++ b/crates/horizon-world/src/worldgen/structure/legacy.rs @@ -53,6 +53,13 @@ pub trait PieceWorld { /// `addFreshEntity`: an entity from its NBT (absolute `Pos`). fn entity(&mut self, nbt: Compound) -> Result<()>; fn post_process(&mut self, pos: [i32; 3]) -> Result<()>; + /// `scheduleTick(pos, fluid, delay)`: kept in the proto chunk until it is live. + fn fluid_tick( + &mut self, + pos: [i32; 3], + fluid: crate::fluid::FluidKind, + delay: i32, + ) -> Result<()>; /// WorldGenLevel.getSeed. fn seed(&mut self) -> Result; /// Draws from WorldGenLevel.getRandom (the region random). @@ -107,6 +114,14 @@ impl PieceWorld for W { fn post_process(&mut self, pos: [i32; 3]) -> Result<()> { self.mark_postprocessing(pos) } + fn fluid_tick( + &mut self, + pos: [i32; 3], + fluid: crate::fluid::FluidKind, + delay: i32, + ) -> Result<()> { + self.schedule_fluid_tick(pos, fluid, delay) + } fn seed(&mut self) -> Result { self.world_seed() } @@ -381,7 +396,7 @@ impl PieceFrame { c.world.set(pos, state, 2)?; let fluid = c.world.fluid(pos)?; if fluid.kind() != crate::fluid::FluidKind::Empty { - // scheduleTick(pos, fluid, 0) is a no-op for generation output. + c.world.fluid_tick(pos, fluid.kind(), 0)?; } if matches!( c.name(state), diff --git a/docs/world-generation.md b/docs/world-generation.md index 2a1ff9cf3..42cc07cff 100644 --- a/docs/world-generation.md +++ b/docs/world-generation.md @@ -196,8 +196,28 @@ above remains a record of its narrower tested source snapshot. `tools/test_chunk_generation_parity.py` generates chunks in actual vanilla classes (`VanillaChunkGenerationOracle`: structure starts and references, biomes, noise, surface, carvers, then biome decoration and structure pieces through real `WorldGenRegion`s) and with `crates/horizon-world/examples/chunk_dump.rs`, and compares every block state, quart biome and block entity (canonical NBT) of each target -chunk. Cases are terrain sites for several seeds across the overworld, Nether and End, plus rectangles around every structure kind found -by scanning the seed. `--phase structures` isolates terrain plus structure pieces, `--phase terrain` stops before features. +chunk, and the scheduled block and fluid ticks each proto chunk carries into the live world. Cases are terrain sites for several seeds +across the overworld, Nether and End, plus rectangles around every structure kind found by scanning the seed. `--phase structures` +isolates terrain plus structure pieces, `--phase terrain` stops before features. `HORIZON_ORDER` (a file of `x z` lines) makes both sides +decorate in that order instead of row-major. + +### Decoration order + +Biome decoration is order dependent: a chunk's features write into its eight neighbours, so a tree, ore blob, sculk patch or geode placed +first changes where the next chunk's features fit (and how many random draws they take), and a neighbour's geode can cut through a +stronghold room or leave it whole. Vanilla decorates on one `worldgen` executor in the order its chunk scheduler produces around a player: +loading tickets ring by ring in fastutil hash order, four in flight, FULL tasks decorating their 3x3 squares x-outer, z-inner by ticket +level. Two vanilla servers decorating around a player teleported to the same spot agree on all but about 6% of the overlapping chunk pairs +(measured from JFR `minecraft.ChunkGeneration` events) and a few hundred blocks. + +`worldgen::decoration_order` replays that scheduler for a standing player and the game's streamer decorates in its order (a chunk waits +only for lower-ranked chunks within two of it, so distant chunks still decorate in parallel). Fed vanilla's recorded order, the oracle +reproduces a vanilla server's world exactly; the model itself swaps as many pairs as a second vanilla run does. + +`tools/test_decorated_region_parity.py` decorates a 13x13-chunk area around each site in that order on both sides and compares everything +the whole-chunk harness compares: the two reported sites on seed 1891623539983467248 (an old-growth taiga at 204 -239, a stronghold with +sculk and a geode at 4870 401), eleven tree-heavy biomes (taigas, dark forest, birch, jungles, savanna, cherry grove, mangrove swamp, pale +garden) and the nearest mineshaft, village, ancient city and trial chambers of two seeds: 21 sites, 3,549 chunks, 0 differences. Result on seed 12345 (overworld 42 sites / 372 chunks, Nether, End): structure pieces 0 differences on all 35 overworld structure sites; full generation 4 differing chunks (3 blocks, 1 block entity) out of 372; Nether and End terrain sites 0 differences. diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaChunkGenerationOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaChunkGenerationOracle.java index ea1d4aa23..35b9e6bce 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaChunkGenerationOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaChunkGenerationOracle.java @@ -382,7 +382,19 @@ public final class VanillaChunkGenerationOracle { Heightmap.primeHeightmaps(chunk, EnumSet.of(Heightmap.Types.MOTION_BLOCKING, Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, Heightmap.Types.OCEAN_FLOOR, Heightmap.Types.WORLD_SURFACE)); chunk.setPersistedStatus(ChunkStatus.TERRAIN); } - for (int z = fz0; z <= fz1 && !phase.equals("terrain"); z++) for (int x = fx0; x <= fx1; x++) { + // HORIZON_ORDER: a file of "x z" lines, the chunks to decorate in that order (default: row-major). + List order = new ArrayList<>(); + if (System.getenv("HORIZON_ORDER") != null) { + for (String line : java.nio.file.Files.readAllLines(java.nio.file.Path.of(System.getenv("HORIZON_ORDER")))) { + String[] t = line.trim().split("\\s+"); + if (t.length == 2) order.add(new int[]{Integer.parseInt(t[0]), Integer.parseInt(t[1])}); + } + } else { + for (int z = fz0; z <= fz1; z++) for (int x = fx0; x <= fx1; x++) order.add(new int[]{x, z}); + } + for (int[] at : order) { + if (phase.equals("terrain")) break; + int x = at[0], z = at[1]; var chunk = chunkAt.apply(x, z); var region = regionFor.apply(ChunkStatus.FEATURES, chunk); generator.applyBiomeDecoration(region, chunk, OracleLevel.structures.forWorldGenRegion(region)); @@ -423,6 +435,13 @@ public final class VanillaChunkGenerationOracle { if ("DUMMY".equals(tag.getStringOr("id", ""))) tag = absent = fresh; w.println("be " + pos.getX() + " " + pos.getY() + " " + pos.getZ() + "\t" + canonicalOf(tag) + "\t" + canonicalOf(absent)); } + // Scheduled ticks, in scheduling order (the order they run in). + if (target instanceof net.minecraft.world.level.chunk.ProtoChunk proto) { + for (var t : ((net.minecraft.world.ticks.ProtoChunkTicks) proto.getBlockTicks()).scheduledTicks()) + w.println("t block " + net.minecraft.core.registries.BuiltInRegistries.BLOCK.getKey(t.type()) + " " + t.pos().getX() + " " + t.pos().getY() + " " + t.pos().getZ() + " " + t.priority().getValue()); + for (var t : ((net.minecraft.world.ticks.ProtoChunkTicks) proto.getFluidTicks()).scheduledTicks()) + w.println("t fluid " + net.minecraft.core.registries.BuiltInRegistries.FLUID.getKey(t.type()) + " " + t.pos().getX() + " " + t.pos().getY() + " " + t.pos().getZ() + " " + t.priority().getValue()); + } } } } diff --git a/tools/test_chunk_generation_parity.py b/tools/test_chunk_generation_parity.py index 38e50f9b2..3d12904a8 100644 --- a/tools/test_chunk_generation_parity.py +++ b/tools/test_chunk_generation_parity.py @@ -88,13 +88,15 @@ def parse(path): if line.startswith('chunk '): _, x, z = line.split() current = (int(x), int(z)) - chunks[current] = {'q': {}, 'y': {}, 'be': {}} + chunks[current] = {'q': {}, 'y': {}, 'be': {}, 't': []} elif line.startswith('q '): _, q, row = line.split(' ', 2) chunks[current]['q'][int(q)] = row elif line.startswith('y '): _, y, row = line.split(' ', 2) chunks[current]['y'][int(y)] = row + elif line.startswith('t '): + chunks[current]['t'].append(line[2:]) elif line.startswith('be '): head, *forms = line.split('\t') parts = head.split(' ') @@ -228,6 +230,12 @@ def compare(vanilla, rust, names, label, limit=6, quiet=False): for key in sorted(set(a['be']) | set(b['be'])): if not entity_matches(a['be'].get(key), b['be'].get(key)): entity_diffs.append((key, a['be'].get(key, [None])[0], (b['be'].get(key) or [None])[0])) + # Scheduled ticks run once the chunk is live (leaves at a tree's edge, lake fluids). + if a.get('t', []) != b.get('t', []): + ta, tb = a.get('t', []), b.get('t', []) + first = next((i for i, (u, v) in enumerate(zip(ta, tb)) if u != v), min(len(ta), len(tb))) + block_diffs.append((pos[0] * 16, 0, pos[1] * 16, f'{len(ta)} ticks, #{first}: {ta[first] if first < len(ta) else "-"}', + f'{len(tb)} ticks, #{first}: {tb[first] if first < len(tb) else "-"}')) if block_diffs or biome_diffs or entity_diffs: chunks += 1 blocks += len(block_diffs) diff --git a/tools/test_decorated_region_parity.py b/tools/test_decorated_region_parity.py new file mode 100644 index 000000000..2716a99e0 --- /dev/null +++ b/tools/test_decorated_region_parity.py @@ -0,0 +1,139 @@ +#!/usr/bin/env python3 +"""Compare fully decorated regions around a player with vanilla. + +Biome decoration is order dependent: a chunk's features write into its +neighbours, so where trees, ore blobs, sculk patches and geodes end up (and +whether a geode cuts through a stronghold room) depends on which chunk is +decorated first. test_chunk_generation_parity.py decorates row-major; this +harness decorates every site in the order vanilla's chunk scheduler uses +around a player standing in the site's chunk (horizon_world's +decoration_order::vanilla_order, which the game's streamer follows), on both +sides, and compares every block state, quart biome, block entity and +scheduled tick of the whole decorated area: the square of chunks whose 3x3 +neighbourhood is decorated. + +Sites: the spots of two reports on seed 1891623539983467248 (an old-growth +taiga forest at 204 108 -239; a stronghold with sculk and a geode at 4870 -22 +401), a set of tree-heavy biomes on that seed, and the nearest mineshaft, +village, ancient city, trial chambers and stronghold-free structures found by +scanning it and a second seed. + +usage: test_decorated_region_parity.py [--view N] [--filter REGEX] [--limit N] +""" + +import argparse +import os +import re +import subprocess +import sys +import time +from collections import Counter +from concurrent.futures import ThreadPoolExecutor + +try: + from . import lab, test_chunk_generation_parity as cg +except ImportError: + import lab + import test_chunk_generation_parity as cg + +REPORTS = 1891623539983467248 +# (seed, label, chunk x, chunk z). Biome sites are /locate biome results. +SITES = [ + (REPORTS, 'report: old-growth taiga trees at 204 -239', 12, -15), + (REPORTS, 'report: stronghold, sculk and geode at 4870 401', 304, 25), + (REPORTS, 'taiga', 6, -10), + (REPORTS, 'old_growth_spruce_taiga', -16, -50), + (REPORTS, 'snowy_taiga', 54, -52), + (REPORTS, 'dark_forest', -60, -96), + (REPORTS, 'birch_forest', 8, 50), + (REPORTS, 'jungle', -58, 48), + (REPORTS, 'bamboo_jungle', -20, 70), + (REPORTS, 'savanna', -28, 28), + (REPORTS, 'cherry_grove', -10, -138), + (REPORTS, 'mangrove_swamp', -4, 238), + (REPORTS, 'pale_garden', -2, -202), +] +# Structures whose nearest start is added for each seed. +STRUCTURES = ['mineshaft', 'village', 'ancient_city', 'trial_chambers'] +STRUCTURE_SEEDS = [REPORTS, 12345] +SCAN_RADIUS = 120 + + +def build_dir(): + path = lab.LOCAL / 'decorated-region-parity' + path.mkdir(parents=True, exist_ok=True) + return path + + +def vanilla_order(binary, cx, cz, view): + out = subprocess.run([str(binary), str(cg.RESOURCES), '0', 'overworld'], capture_output=True, text=True, check=True, + env=dict(os.environ, HORIZON_LIST_ORDER=f'{cx},{cz},{view}'), cwd=lab.ROOT).stdout + return [tuple(map(int, line.split())) for line in out.splitlines()] + + +def structure_sites(oracle, binary): + sites = [] + for seed in STRUCTURE_SEEDS: + starts = cg.scan(oracle, binary, seed, 'overworld', SCAN_RADIUS) + for kind in STRUCTURES: + found = sorted((abs(cx) + abs(cz), name, cx, cz) for name, cx, cz, _ in starts if kind in name) + if found: + _, name, cx, cz = found[0] + sites.append((seed, name, cx, cz)) + return sites + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--view', type=int, default=4, help='view distance of the player at each site') + parser.add_argument('--filter', help='regex over site labels') + parser.add_argument('--limit', type=int, default=4, help='block differences shown per chunk') + parser.add_argument('--no-structures', action='store_true', help='skip the scanned structure sites') + args = parser.parse_args() + os.environ['HORIZON_PHASE'] = 'full' + oracle = cg.Oracle() + binary = cg.rust_binary() + names = cg.state_names() + build = build_dir() + sites = list(SITES) + ([] if args.no_structures else structure_sites(oracle, binary)) + if args.filter: + sites = [s for s in sites if re.search(args.filter, s[1])] + totals, started = Counter(), time.time() + for seed, label, cx, cz in sites: + # The player's tickets reach `view`; the chunks they get decorated reach + # view + 3, so every chunk within view + 2 has its whole neighbourhood decorated. + r = args.view + 2 + rect = f'{cx - r},{cz - r},{cx + r},{cz + r}' + tag = f'{seed}-{cx}-{cz}-v{args.view}' + order = build / f'order-{tag}.txt' + order.write_text(''.join(f'{x} {z}\n' for x, z in vanilla_order(binary, cx, cz, args.view))) + env = dict(os.environ, HORIZON_ORDER=str(order)) + vanilla_path, rust_path = build / f'vanilla-{tag}.txt', build / f'rust-{tag}.txt' + t0 = time.time() + saved = dict(os.environ) + os.environ.update(env) + try: + with ThreadPoolExecutor(2) as pool: + rust_run = pool.submit(cg.run_rust, binary, rust_path, seed, 'overworld', [rect]) + oracle.run(vanilla_path, ['dump', seed, 'overworld', rect]) + stderr = rust_run.result() + finally: + os.environ.clear() + os.environ.update(saved) + for line in stderr.splitlines()[:3]: + print(' horizon:', line) + vanilla, rust = cg.parse(vanilla_path), cg.parse(rust_path) + counts = cg.compare(vanilla, rust, names, f'{label} ({seed})', limit=args.limit) + totals.update(dict(zip(['chunks', 'blocks', 'biomes', 'entities'], counts))) + totals['compared'] += len(vanilla) + ticks = sum(len(c['t']) for c in vanilla.values()) + print(f'{label} (seed {seed}, chunk {cx},{cz}): {len(vanilla)} chunks, {ticks} scheduled ticks, differing chunks {counts[0]} ' + f'(blocks {counts[1]}, biome cells {counts[2]}, block entities {counts[3]}); {time.time() - t0:.0f}s', flush=True) + print(f'Compared {totals["compared"]} chunks at {len(sites)} sites in {time.time() - started:.0f}s') + print(f'Differences: {totals["chunks"]} chunks, {totals["blocks"]} blocks, {totals["biomes"]} biome cells, {totals["entities"]} block entities') + if totals['chunks']: + sys.exit(1) + + +if __name__ == '__main__': + main()