diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 00265cade..4ea6a6f45 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -34,6 +34,7 @@ pub mod settings; pub mod sprint; pub mod sprites; pub mod streaming; +pub mod structure_blocks; pub mod testgen; pub mod title; pub mod vanillagen; diff --git a/crates/horizon-client/src/structure_blocks.rs b/crates/horizon-client/src/structure_blocks.rs new file mode 100644 index 000000000..1c8450f20 --- /dev/null +++ b/crates/horizon-client/src/structure_blocks.rs @@ -0,0 +1,72 @@ +//! Block entities placed by world generation (structure chests, barrels and +//! template containers): turned into client containers, with loot tables +//! unpacked as `RandomizableContainer.unpackLootTable` would on first open. +use crate::{containers::BlockEntity, items::ItemStack}; +use horizon_world::{ + loot::LootData, + nbt::{Compound, JavaString, Tag}, + worldgen::generator::ProtoChunk, +}; +use std::{ + path::Path, + sync::{Arc, OnceLock}, +}; + +/// Loot data of the resource root, loaded once per process. +pub fn loot_data(root: &Path) -> Option> { + static LOOT: OnceLock>> = OnceLock::new(); + LOOT.get_or_init(|| match LootData::load(root) { + Ok(data) => Some(Arc::new(data)), + Err(e) => { + eprintln!("Horizon: loot tables unavailable, structure containers stay empty: {e}"); + None + } + }) + .clone() +} + +fn text(nbt: &Compound, key: &str) -> Option { + match nbt.get(&JavaString::from(key)) { + Some(Tag::String(s)) => Some(s.to_string_lossy()), + _ => None, + } +} + +/// Client block entities for the generated block entities of a chunk. +pub fn block_entities( + chunk: &ProtoChunk, + loot: Option<&LootData>, +) -> Vec<((i32, i32, i32), BlockEntity)> { + let mut out = Vec::new(); + for (pos, nbt) in &chunk.block_entities { + let [x, y, z] = *pos; + let block = chunk.block_name(x, y, z); + let Some(mut entity) = BlockEntity::for_block(block) else { + continue; + }; + if let (BlockEntity::Chest(slots), Some(table), Some(loot)) = + (&mut entity, text(nbt, "LootTable"), loot) + { + let seed = match nbt.get(&JavaString::from("LootTableSeed")) { + Some(Tag::Long(seed)) => *seed, + // Seed 0 means "the level's random": derive one from the position. + _ => horizon_core::random::position_seed(horizon_core::position::BlockPos::new( + x, y, z, + )), + }; + let biome = chunk.biome_at(x, y, z); + for (slot, stack) in slots + .iter_mut() + .zip(loot.fill(&table, seed, 27, Some(biome))) + { + *slot = stack.map(|s| ItemStack { + item: Arc::from(s.item.as_str()), + count: s.count.max(0) as u32, + damage: s.damage.max(0) as u32, + }); + } + } + out.push(((x, y, z), entity)); + } + out +} diff --git a/crates/horizon-client/src/vanillagen.rs b/crates/horizon-client/src/vanillagen.rs index 6c49822ea..a261d088a 100644 --- a/crates/horizon-client/src/vanillagen.rs +++ b/crates/horizon-client/src/vanillagen.rs @@ -16,6 +16,8 @@ pub struct VanillaTerrain { generator: OverworldGenerator, /// Generator biome index -> client biome table index. biome_map: Vec, + /// Loot tables for generated containers. + loot: Option>, } /// A proto chunk, or the position of one whose generation failed. enum VanillaProto { @@ -37,7 +39,8 @@ impl VanillaTerrain { unsupported.len() ); } - Ok(Self { generator, biome_map }) + let loot = dir.parent().and_then(crate::structure_blocks::loot_data); + Ok(Self { generator, biome_map, loot }) } } impl ChunkGenerator for VanillaTerrain { @@ -124,6 +127,7 @@ impl ChunkGenerator for VanillaTerrain { } } out.biomes = Arc::new(biomes); + out.block_entities = crate::structure_blocks::block_entities(&chunk, self.loot.as_deref()); for s in &mut out.sections { Arc::make_mut(s).compact(); } diff --git a/crates/horizon-world/examples/chunkgen_bench.rs b/crates/horizon-world/examples/chunkgen_bench.rs index d8e129cc0..96ebd9665 100644 --- a/crates/horizon-world/examples/chunkgen_bench.rs +++ b/crates/horizon-world/examples/chunkgen_bench.rs @@ -464,6 +464,22 @@ fn main() -> Result<(), Error> { .map(|c| c.block_entities.len()) .sum(); println!("block entities: {block_entities}"); + if std::env::var_os("HORIZON_BLOCK_ENTITIES").is_some() { + for chunk in chunks.iter().flatten() { + for ([x, y, z], nbt) in &chunk.block_entities { + let text = |key: &str| match nbt.get(&key.into()) { + Some(Tag::String(s)) => s.to_string_lossy(), + _ => "-".into(), + }; + println!( + "block_entity {x} {y} {z} {} {} {}", + chunk.block_name(*x, *y, *z), + text("id"), + text("LootTable") + ); + } + } + } let mut legend = BTreeMap::>::new(); let step = (size * 16 / 96).max(1); for bz in (0..size * 16).step_by(step as usize) {