Fill generated structure containers in the client
Chests, trapped chests and barrels written by world generation become client containers; their loot tables are unpacked with the loot module using the generated LootTableSeed and the container's biome. chunkgen_bench can list generated block entities (HORIZON_BLOCK_ENTITIES). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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<Arc<LootData>> {
|
||||
static LOOT: OnceLock<Option<Arc<LootData>>> = 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<String> {
|
||||
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
|
||||
}
|
||||
@@ -16,6 +16,8 @@ pub struct VanillaTerrain {
|
||||
generator: OverworldGenerator,
|
||||
/// Generator biome index -> client biome table index.
|
||||
biome_map: Vec<u16>,
|
||||
/// Loot tables for generated containers.
|
||||
loot: Option<Arc<horizon_world::loot::LootData>>,
|
||||
}
|
||||
/// 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();
|
||||
}
|
||||
|
||||
@@ -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::<char, BTreeMap<String, usize>>::new();
|
||||
let step = (size * 16 / 96).max(1);
|
||||
for bz in (0..size * 16).step_by(step as usize) {
|
||||
|
||||
Reference in New Issue
Block a user