Saves: write the structure starts a chunk holds

A generated chunk keeps the starts it holds and its save writes them
into structures.starts with their current state, as vanilla saves the
StructureStart objects its chunks share. Proto chunks get their
references and starts too, so vanilla and Paper find the structures of
the chunks they finish at the edge of a generated area.

A start with a piece that cannot be saved is left out, with a warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
FusselTV
2026-10-25 22:33:35 +01:00
parent ad50996f7c
commit c67a963da7
4 changed files with 38 additions and 5 deletions
+4
View File
@@ -147,6 +147,9 @@ pub struct Chunk {
/// Structure starts reaching into this chunk (`structures.References`):
/// structure id and the start's chunk.
pub structure_refs: Vec<(Arc<str>, (i32, i32))>,
/// The starts generation made in this chunk (`structures.starts`), saved
/// with the state their placement has reached by then.
pub structure_starts: Vec<Arc<horizon_world::worldgen::structure::start::StructureStart>>,
}
impl Chunk {
pub fn empty(x: i32, z: i32) -> Self {
@@ -164,6 +167,7 @@ impl Chunk {
block_entities: Vec::new(),
block_names: Vec::new(),
structure_refs: Vec::new(),
structure_starts: Vec::new(),
postprocess: Vec::new(),
saved: None,
entities: Vec::new(),
+19
View File
@@ -1017,6 +1017,25 @@ pub fn encode_with(
.collect();
s.insert("References".into(), Tag::Compound(refs));
}
let starts: Compound = chunk
.structure_starts
.iter()
.filter_map(|start| match start.save() {
Some(tag) => Some((start.name.as_str().into(), tag)),
None => {
log::warn!(
"Horizon: structure start {} not saved: one of its pieces cannot be",
start.name
);
None
}
})
.collect();
if !starts.is_empty()
&& let Some(Tag::Compound(s)) = map.get_mut(&"structures".into())
{
s.insert("starts".into(), Tag::Compound(starts));
}
map.entry("PostProcessing".into())
.or_insert_with(|| Tag::List(vec![Tag::List(Vec::new()); SECTIONS]));
}
+6 -5
View File
@@ -158,6 +158,12 @@ impl VanillaTerrain {
chunk.terrain.min_y().max(MIN_Y),
(chunk.terrain.min_y() + chunk.terrain.height()).min(MIN_Y + HEIGHT),
);
out.structure_refs = chunk
.structures()
.into_iter()
.map(|(id, start, _, _)| (Arc::from(id), (start.x, start.z)))
.collect();
out.structure_starts = chunk.own_starts();
let mut values = Box::new([0u32; 4096]);
for si in 0..SECTIONS as i32 {
let base = MIN_Y + si * 16;
@@ -564,11 +570,6 @@ impl ChunkGenerator for VanillaTerrain {
extras.block_entities = unmodelled;
out.saved = Some(Arc::new(extras));
}
out.structure_refs = chunk
.structures()
.into_iter()
.map(|(id, start, _, _)| (Arc::from(id), (start.x, start.z)))
.collect();
let entities = std::mem::take(&mut chunk.entities);
out.entities = entities
.into_iter()
@@ -594,6 +594,15 @@ impl ProtoChunk {
pub fn is_decorated(&self) -> bool {
self.decorated
}
/// The structure starts this chunk holds itself (`structures.starts`).
pub fn own_starts(&self) -> Vec<Arc<super::structure::start::StructureStart>> {
let pos = self.position();
self.structure_starts
.iter()
.filter(|s| s.chunk == pos)
.cloned()
.collect()
}
/// Also restores a saved proto chunk that was decorated before it was saved.
pub fn set_decorated(&mut self) {
self.decorated = true;