Merge pull request #284 from ludovic111/fix/bastion-chunks

Save every generated chunk and decorated proto chunks, as vanilla does (#249)
This commit is contained in:
Ludovic Marie
2026-10-15 06:48:44 +02:00
committed by GitHub
9 changed files with 898 additions and 157 deletions
+13 -2
View File
@@ -2853,7 +2853,7 @@ impl Game<'_> {
self.tick_weather_effects();
}
if self.ticks.is_multiple_of(6000) {
self.save_game()?;
self.save_game_with(false)?;
}
if running {
self.advance_clock();
@@ -6048,6 +6048,7 @@ impl Game<'_> {
return Ok(());
};
// Save the departing dimension's changed chunks.
self.streaming.save(&mut self.world, true);
let keys: Vec<_> = self.world.modified.drain().collect();
let chunks = self.with_entities(keys.iter().filter_map(|k| self.world.chunks.get(k)));
self.save
@@ -7891,13 +7892,23 @@ impl Game<'_> {
.collect();
}
fn save_game(&mut self) -> Result<()> {
self.save_game_with(true)
}
/// The autosave doesn't `wait` for the chunks to be on disk: every
/// generated chunk is saved, hundreds at a time, encoded and written on
/// the region writer meanwhile.
fn save_game_with(&mut self, wait: bool) -> Result<()> {
if self.remote.is_some() {
return Ok(());
}
self.streaming.save(&mut self.world, wait);
let keys: Vec<_> = self.world.modified.drain().collect();
let chunks = self.with_entities(keys.iter().filter_map(|k| self.world.chunks.get(k)));
self.save
.save(&chunks, self.res.assets.registry, &self.res.biomes)?;
.save_later(&chunks, self.res.assets.registry, &self.res.biomes)?;
if wait {
self.save.flush();
}
self.save_entities()?;
self.save_maps();
self.save_random_sequences();
+179 -6
View File
@@ -27,6 +27,7 @@ use horizon_core::{
use horizon_world::{
DATA_VERSION,
chunk::BlockState,
chunk_status::ChunkStatus,
compression::Compression,
heightmap::{Heightmap, HeightmapChunk, HeightmapType},
nbt::{self, Compound, JavaString, Limits, NamedTag, Tag},
@@ -972,13 +973,25 @@ const GENERATED_KEYS: [&str; 14] = [
pub fn decode(root: &NamedTag, registry: &BlockRegistry, biomes: &Biomes) -> Option<Chunk> {
decode_with(root, registry, biomes, None)
}
/// Only full chunks load; partially generated ones (vanilla's world border
/// of proto chunks) are generated again.
/// Only full chunks load here; partially generated ones (vanilla's world
/// border of proto chunks) resume generation through `decode_proto`.
pub fn decode_with(
root: &NamedTag,
registry: &BlockRegistry,
biomes: &Biomes,
tables: Option<&SaveTables>,
) -> Option<Chunk> {
if text(root.tag.get("Status")).is_some_and(|s| s != "minecraft:full" && s != "full") {
return None;
}
decode_column(root, registry, biomes, tables)
}
/// `decode_with` whatever the chunk's status.
fn decode_column(
root: &NamedTag,
registry: &BlockRegistry,
biomes: &Biomes,
tables: Option<&SaveTables>,
) -> Option<Chunk> {
let tag = &root.tag;
let Tag::Int(x) = tag.get("xPos")? else {
@@ -987,9 +1000,6 @@ pub fn decode_with(
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);
// structures.References: structure id -> packed start chunk positions.
if let Some(Tag::Compound(refs)) = tag.get("structures").and_then(|s| s.get("References")) {
@@ -1127,6 +1137,102 @@ pub fn decode_with(
Some(chunk)
}
/// A proto chunk saved before it became final. Decoration writes into a
/// chunk's neighbours, and decorated chunks wait for theirs, so the chunks
/// past the finished ones hold writes that generating them again would lose
/// (whole structure pieces, trees, ores). Vanilla saves its proto chunks too.
pub struct SavedProto {
/// Blocks, scheduled ticks (in `saved`) and post-processing marks.
pub column: Chunk,
/// Decorated itself (`features`), not only written to by its neighbours
/// (`terrain`).
pub decorated: bool,
/// Block entity data in placement order, with `x`, `y` and `z`.
pub block_entities: Vec<Tag>,
/// Entities placed by structures, with absolute `Pos`.
pub entities: Vec<Tag>,
}
pub fn encode_proto(
proto: &SavedProto,
registry: &BlockRegistry,
biomes: &Biomes,
tables: Option<&SaveTables>,
sky_light: bool,
) -> NamedTag {
let mut root = encode_with(&proto.column, registry, biomes, tables, sky_light);
let status = if proto.decorated {
ChunkStatus::Features
} else {
ChunkStatus::Terrain
};
// ProtoChunk.postProcessing: one list per section, packed section offsets.
let mut marks = vec![Vec::new(); SECTIONS];
for &[x, y, z] in &proto.column.postprocess {
if let Some(list) = marks.get_mut(((y - MIN_Y) >> 4) as usize) {
list.push(Tag::Short(
((x & 15) | (y & 15) << 4 | (z & 15) << 8) as i16,
));
}
}
for (key, value) in [
("Status", string(status.name())),
("isLightOn", Tag::Byte(0)),
("block_entities", Tag::List(proto.block_entities.clone())),
("entities", Tag::List(proto.entities.clone())),
(
"PostProcessing",
Tag::List(marks.into_iter().map(Tag::List).collect()),
),
] {
put(&mut root.tag, key, value);
}
root
}
/// A proto chunk generation can resume: `terrain` (written to by a
/// neighbour's decoration) up to `spawn`. Earlier statuses hold nothing that
/// generating the chunk again doesn't reproduce.
pub fn decode_proto(
root: &NamedTag,
registry: &BlockRegistry,
biomes: &Biomes,
tables: Option<&SaveTables>,
) -> Option<SavedProto> {
let status = ChunkStatus::parse(&text(root.tag.get("Status"))?).ok()?;
if !(ChunkStatus::Terrain..ChunkStatus::Full).contains(&status) {
return None;
}
let mut column = decode_column(root, registry, biomes, tables)?;
column.block_entities.clear();
let base = number(root.tag.get("yPos")).map_or(MIN_Y, |y| y as i32 * 16);
if let Some(Tag::List(sections)) = root.tag.get("PostProcessing") {
for (si, marks) in sections.iter().enumerate() {
let Tag::List(marks) = marks else {
continue;
};
for mark in marks {
if let Tag::Short(p) = mark {
let p = i32::from(*p);
column.postprocess.push([
column.x * 16 + (p & 15),
base + si as i32 * 16 + ((p >> 4) & 15),
column.z * 16 + ((p >> 8) & 15),
]);
}
}
}
}
let list = |key: &str| match root.tag.get(key) {
Some(Tag::List(list)) => list.clone(),
_ => Vec::new(),
};
Some(SavedProto {
column,
decorated: status >= ChunkStatus::Features,
block_entities: list("block_entities"),
entities: list("entities"),
})
}
/// Read the stored `SkyLight`/`BlockLight` layers of a chunk saved with
/// `isLightOn` (`SerializableChunkData.read` + `ThreadedLevelLightEngine`),
/// so it needs no relighting. A missing sky layer repeats the bottom layer of
@@ -1395,6 +1501,34 @@ impl SaveDir {
load_light(&root.tag, &mut chunk, self.sky_light);
Some(chunk)
}
/// A proto chunk saved before it became final (`decode_proto`).
pub fn load_proto(
&self,
x: i32,
z: i32,
registry: &BlockRegistry,
biomes: &Biomes,
) -> Option<SavedProto> {
let root = self.read_record("region", x, z)?;
decode_proto(&root, registry, biomes, self.tables.get().map(|t| &**t))
}
/// Queue proto chunks for the writer thread; loads see them meanwhile.
pub fn save_protos(
&self,
protos: &[SavedProto],
registry: &BlockRegistry,
biomes: &Biomes,
) -> Result<()> {
let tables = self.tables.get().map(|t| &**t);
let records = protos
.iter()
.map(|p| {
let tag = encode_proto(p, registry, biomes, tables, self.sky_light);
((p.column.x, p.column.z), Some(tag))
})
.collect();
self.write_records("region", records, false)
}
/// 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<Tag> {
@@ -2539,7 +2673,46 @@ impl<G: ChunkGenerator> ChunkGenerator for Persistent<G> {
Some(chunk)
}
fn proto(&self, worker: &mut G::Worker, x: i32, z: i32) -> crate::streaming::Proto {
self.inner.proto(worker, x, z)
let mut proto = self.inner.proto(worker, x, z);
if let Some(saved) = self
.save
.load_proto(x, z, self.registry.registry, &self.biomes)
{
self.inner.restore_proto(&mut proto, saved);
}
proto
}
fn is_decorated(&self, proto: &crate::streaming::Proto) -> bool {
self.inner.is_decorated(proto)
}
fn save_protos(&self, protos: &[&crate::streaming::Proto]) {
self.save
.tables
.get_or_init(|| Arc::new(SaveTables::new(&self.registry)));
let saved: Vec<SavedProto> = protos
.iter()
.filter_map(|p| self.inner.saved_proto(p))
.collect();
if let Err(e) = self
.save
.save_protos(&saved, self.registry.registry, &self.biomes)
{
log::warn!("Horizon: failed to save proto chunks: {e}");
}
}
fn save_finished(&self, mut chunk: Chunk) {
self.save
.tables
.get_or_init(|| Arc::new(SaveTables::new(&self.registry)));
let key = (chunk.x, chunk.z);
let entities = std::mem::take(&mut chunk.entities);
let saved = self
.save
.save_later(&[Arc::new(chunk)], self.registry.registry, &self.biomes)
.and_then(|_| self.save.save_entities_later(vec![(key, entities)]));
if let Err(e) = saved {
log::warn!("Horizon: failed to save chunk {} {}: {e}", key.0, key.1);
}
}
fn decorates(&self) -> bool {
self.inner.decorates()
+171 -65
View File
@@ -42,6 +42,35 @@ pub trait ChunkGenerator: Send + Sync + 'static {
/// Decorate `region[4]`; the region is row-major (dz, dx) around it.
fn decorate(&self, _worker: &mut Self::Worker, _region: &mut [Proto; 9]) {}
fn finish(&self, proto: Proto) -> Chunk;
/// Whether `proto` has been decorated (a saved one can come back decorated).
fn is_decorated(&self, _proto: &Proto) -> bool {
false
}
/// What to save of a proto chunk dropped before it became final.
fn saved_proto(&self, _proto: &Proto) -> Option<crate::save::SavedProto> {
None
}
/// Put a saved proto chunk's state back onto `proto`, generated again.
fn restore_proto(&self, _proto: &mut Proto, _saved: crate::save::SavedProto) {}
/// Save proto chunks decoration wrote to, so that they come back as they
/// were: generating them again would lose those writes.
fn save_protos(&self, _protos: &[&Proto]) {}
/// Save a column that finished after the player moved away: its
/// neighbours' decoration is done, so it can't be generated again.
fn save_finished(&self, _chunk: Chunk) {}
}
/// The generator's saving, for the streamer (which doesn't know its type).
trait Saves: Send + Sync {
fn save_protos(&self, protos: &[&Proto]);
fn save_finished(&self, chunk: Chunk);
}
impl<G: ChunkGenerator> Saves for G {
fn save_protos(&self, protos: &[&Proto]) {
ChunkGenerator::save_protos(self, protos);
}
fn save_finished(&self, chunk: Chunk) {
ChunkGenerator::save_finished(self, chunk);
}
}
/// Per-job mesher inputs that change with the dimension or the options.
#[derive(Clone, Copy)]
@@ -61,7 +90,8 @@ enum Job {
}
pub enum Done {
Generated(Chunk),
Proto(i32, i32, Proto),
/// A proto chunk, and whether it was decorated (loaded so from the save).
Proto(i32, i32, Proto, bool),
Decorated(i32, i32, Box<[Proto; 9]>),
Lit(i32, i32, Vec<Arc<crate::chunk::Section>>, u64),
Meshed(i32, i32, i32, SectionMesh, u64),
@@ -124,6 +154,11 @@ pub struct Streaming {
/// the player comes closer, and the area they were set aside for.
waiting: FxHashSet<(i32, i32, i32)>,
waiting_area: ((i32, i32), i32, usize),
/// Proto chunks decoration wrote to since they were generated or saved.
written: FxHashSet<(i32, i32)>,
saves: Arc<dyn Saves>,
/// Results received while `save` waited, for the next `update`.
deferred: Vec<Done>,
sequence: u64,
pub center: (i32, i32),
pub radius: i32,
@@ -176,6 +211,7 @@ impl Streaming {
});
let (send, results) = mpsc::channel();
let light_tables = context.light.clone();
let saves: Arc<dyn Saves> = generator.clone();
let threads = (0..threads.max(1))
.map(|i| {
let shared = shared.clone();
@@ -202,7 +238,11 @@ impl Streaming {
let done = match job {
Job::Generate(x, z) => match generator.load(x, z) {
Some(chunk) => Done::Generated(chunk),
None => Done::Proto(x, z, generator.proto(&mut worker, x, z)),
None => {
let proto = generator.proto(&mut worker, x, z);
let decorated = generator.is_decorated(&proto);
Done::Proto(x, z, proto, decorated)
}
},
Job::Decorate(x, z, mut region) => {
generator.decorate(&mut worker, &mut region);
@@ -255,6 +295,9 @@ impl Streaming {
meshed: Default::default(),
waiting: Default::default(),
waiting_area: ((0, 0), 0, 0),
written: Default::default(),
saves,
deferred: Vec::new(),
sequence: 0,
center: (0, 0),
radius: 8,
@@ -322,6 +365,67 @@ impl Streaming {
});
self.shared.ready.notify_one();
}
fn receive_decorated(&mut self, x: i32, z: i32, region: Box<[Proto; 9]>) {
self.decorating -= 1;
self.decorated.insert((x, z));
for (i, proto) in region.into_iter().enumerate() {
let key = (x + (i % 3) as i32 - 1, z + (i / 3) as i32 - 1);
self.protos.insert(key, Some(proto));
self.written.insert(key);
}
}
fn receive_generated(&mut self, mut chunk: Chunk, world: &mut World) {
let key = (chunk.x, chunk.z);
self.generating.remove(&key);
self.finishing.remove(&key);
self.decorated.insert(key);
// A column that finished after the player moved away is dropped
// whole: its block entities and saved entities too, or they would
// stay in the world (and the entities spawn again, duplicated, when
// the column loads later). A generated one is saved first: its
// neighbours were decorated around it, so generating it again would
// not give it back.
if !(self.in_range(key.0, key.1, 2) || self.near_finished(key.0, key.1, 1)) {
if chunk.fresh {
self.saves.save_finished(chunk);
}
return;
}
// Generated columns are saved like edited ones, as vanilla saves
// every chunk it generates.
if chunk.fresh {
world.modified.insert(key);
}
world
.block_entities
.extend(std::mem::take(&mut chunk.block_entities));
world
.block_names
.extend(std::mem::take(&mut chunk.block_names));
let entities = std::mem::take(&mut chunk.entities);
if !entities.is_empty() {
world.loaded_entities.push(((chunk.x, chunk.z), entities));
}
self.generated_total += 1;
// A chunk saved with its light needs no light job; its unlit
// neighbours still light themselves from it.
let loaded = chunk.light_loaded;
world.chunks.insert(key, Arc::new(chunk));
if loaded {
self.lit.insert(key);
for sy in 0..SECTIONS as i32 {
world.dirty_sections.insert((key.0, sy, key.1));
}
}
for dz in -1..=1 {
for dx in -1..=1 {
let n = (key.0 + dx, key.1 + dz);
if !loaded || world.chunks.get(&n).is_some_and(|c| !c.light_loaded) {
world.dirty_light.insert(n);
}
}
}
}
fn distance(&self, x: i32, z: i32) -> i64 {
let d = |c: (i32, i32)| {
let (dx, dz) = (i64::from(x - c.0), i64::from(z - c.1));
@@ -363,67 +467,21 @@ impl Streaming {
return (uploads, Vec::new());
}
self.relight_edits(world);
while let Ok(done) = self.results.try_recv() {
let deferred = std::mem::take(&mut self.deferred);
let received = std::iter::from_fn(|| self.results.try_recv().ok());
for done in deferred.into_iter().chain(received.collect::<Vec<_>>()) {
match done {
Done::Proto(x, z, proto) => {
Done::Proto(x, z, proto, decorated) => {
self.generating.remove(&(x, z));
if self.in_range(x, z, 4) || self.near_finished(x, z, 2) {
self.protos.insert((x, z), Some(proto));
if !self.decorates {
if decorated || !self.decorates {
self.decorated.insert((x, z));
}
}
}
Done::Decorated(x, z, region) => {
self.decorating -= 1;
self.decorated.insert((x, z));
for (i, proto) in region.into_iter().enumerate() {
let key = (x + (i % 3) as i32 - 1, z + (i / 3) as i32 - 1);
self.protos.insert(key, Some(proto));
}
}
Done::Generated(mut chunk) => {
let key = (chunk.x, chunk.z);
self.generating.remove(&key);
self.finishing.remove(&key);
self.decorated.insert(key);
// A column that finished after the player moved away is
// dropped whole: its block entities and saved entities
// too, or they would stay in the world (and the entities
// spawn again, duplicated, when the column loads later).
if self.in_range(key.0, key.1, 2) || self.near_finished(key.0, key.1, 1) {
world
.block_entities
.extend(std::mem::take(&mut chunk.block_entities));
world
.block_names
.extend(std::mem::take(&mut chunk.block_names));
let entities = std::mem::take(&mut chunk.entities);
if !entities.is_empty() {
world.loaded_entities.push(((chunk.x, chunk.z), entities));
}
self.generated_total += 1;
// A chunk saved with its light needs no light job; its
// unlit neighbours still light themselves from it.
let loaded = chunk.light_loaded;
world.chunks.insert(key, Arc::new(chunk));
if loaded {
self.lit.insert(key);
for sy in 0..SECTIONS as i32 {
world.dirty_sections.insert((key.0, sy, key.1));
}
}
for dz in -1..=1 {
for dx in -1..=1 {
let n = (key.0 + dx, key.1 + dz);
if !loaded || world.chunks.get(&n).is_some_and(|c| !c.light_loaded)
{
world.dirty_light.insert(n);
}
}
}
}
}
Done::Decorated(x, z, region) => self.receive_decorated(x, z, region),
Done::Generated(chunk) => self.receive_generated(chunk, world),
Done::Lit(x, z, sections, seq) => {
if self.lighting.get(&(x, z)) != Some(&seq) {
continue;
@@ -500,15 +558,31 @@ impl Streaming {
.chain(self.extra.iter().map(|&(c, r)| (c, i64::from(r + 4))))
.collect();
let forced = &self.forced;
self.protos.retain(|&(x, z), p| {
p.is_none()
|| areas.iter().any(|&((cx, cz), r4)| {
i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2) <= r4 * r4
})
|| forced
.iter()
.any(|f| (x - f.0).abs() <= 3 && (z - f.1).abs() <= 3)
});
let far: Vec<(i32, i32)> = self
.protos
.iter()
.filter(|&(&(x, z), p)| {
p.is_some()
&& !areas.iter().any(|&((cx, cz), r4)| {
i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2) <= r4 * r4
})
&& !forced
.iter()
.any(|f| (x - f.0).abs() <= 3 && (z - f.1).abs() <= 3)
})
.map(|(k, _)| *k)
.collect();
// Those decoration wrote to are saved (see `ChunkGenerator::save_protos`).
let written: Vec<Proto> = far
.iter()
.filter_map(|k| {
let proto = self.protos.remove(k).flatten();
if self.written.remove(k) { proto } else { None }
})
.collect();
if !written.is_empty() {
self.saves.save_protos(&written.iter().collect::<Vec<_>>());
}
let (protos, finishing) = (&self.protos, &self.finishing);
self.decorated.retain(|k| {
protos.contains_key(k) || finishing.contains(k) || world.chunks.contains_key(k)
@@ -535,6 +609,7 @@ impl Streaming {
.flatten()
.expect("present proto");
self.finishing.insert((x, z));
self.written.remove(&(x, z));
self.submit(self.distance(x, z) * 4, Job::Finish(proto));
}
}
@@ -616,7 +691,9 @@ impl Streaming {
}
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.
// A final neighbour cannot receive writes: skip decoration. Only
// worlds saved before proto chunks were saved get here (their
// final chunks' neighbours are generated again).
if keys.iter().any(|k| world.chunks.contains_key(k)) {
self.decorated.insert((x, z));
continue;
@@ -878,6 +955,35 @@ impl Streaming {
fn near_finished(&self, x: i32, z: i32, reach: i32) -> bool {
self.ranges().near_finished(x, z, reach)
}
/// Save what generation holds outside the world, before the world's
/// chunks: the proto chunks decoration wrote to. With `wait`, the
/// decorations and columns in flight are waited for first (their proto
/// chunks are out in the jobs); otherwise they are saved later, as they
/// unload or at the next save.
pub fn save(&mut self, world: &mut World, wait: bool) {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
while wait && (self.decorating > 0 || !self.finishing.is_empty()) {
let left = deadline.saturating_duration_since(std::time::Instant::now());
match self.results.recv_timeout(left) {
Ok(Done::Decorated(x, z, region)) => self.receive_decorated(x, z, region),
Ok(Done::Generated(chunk)) => self.receive_generated(chunk, world),
Ok(other) => self.deferred.push(other),
Err(_) => {
log::warn!("Horizon: chunk generation did not finish before saving");
break;
}
}
}
let protos: Vec<&Proto> = self
.written
.iter()
.filter_map(|k| self.protos.get(k)?.as_ref())
.collect();
if !protos.is_empty() {
self.saves.save_protos(&protos);
}
self.written.clear();
}
fn in_range(&self, x: i32, z: i32, margin: i32) -> bool {
self.ranges().in_range(x, z, margin)
}
+204 -79
View File
@@ -3,12 +3,22 @@
use crate::{
biome::Biomes,
chunk::{BIOME_STRATEGY, Chunk, HEIGHT, MIN_Y, SECTIONS, Section},
save::SavedProto,
streaming::{ChunkGenerator, Proto},
};
use horizon_core::{paletted::PalettedContainer, position::ChunkPos};
use horizon_core::{
paletted::PalettedContainer,
position::ChunkPos,
ticks::{ProtoChunkTicks, SavedTick, TickTypeId},
};
use horizon_world::{
heightmap::HeightmapType,
nbt::Tag,
registry::BlockRegistry,
worldgen::generator::{GeneratorData, GeneratorWorker, OverworldGenerator, ProtoChunk},
worldgen::{
generator::{GeneratorData, GeneratorWorker, OverworldGenerator, ProtoChunk},
terrain::PreLightChunkStage,
},
};
use std::{path::Path, sync::Arc};
@@ -23,6 +33,8 @@ pub struct VanillaTerrain {
/// Block state id -> block index, the game's block tick type: worldgen
/// schedules block ticks by default state id.
tick_blocks: Vec<u32>,
/// Block index -> default state id: `tick_blocks` the other way.
tick_states: Vec<u32>,
}
/// A proto chunk, or the position of one whose generation failed.
enum VanillaProto {
@@ -68,6 +80,7 @@ impl VanillaTerrain {
}
let loot = dir.parent().and_then(crate::structure_blocks::loot_data);
let mut tick_blocks = Vec::with_capacity(registry.len());
let mut tick_states = Vec::new();
let mut last: Option<&str> = None;
let mut index = 0u32;
for id in 0..registry.len() {
@@ -77,6 +90,9 @@ impl VanillaTerrain {
if last.is_some_and(|l| l != name) {
index += 1;
}
if last != Some(name) {
tick_states.push(registry.default_state(name).map_or(id as u32, |s| s.0));
}
last = Some(name);
tick_blocks.push(index);
}
@@ -86,8 +102,91 @@ impl VanillaTerrain {
loot,
query: std::sync::Mutex::new(None),
tick_blocks,
tick_states,
})
}
/// A proto chunk's blocks, biomes, scheduled ticks and post-processing
/// marks as a column.
fn column(&self, chunk: &ProtoChunk) -> Chunk {
let pos = chunk.position();
let mut out = Chunk::empty(pos.x, pos.z);
let (bx, bz) = (pos.x * 16, pos.z * 16);
// Only the generated range (the Nether is 0..256) is copied.
let (lo, hi) = (
chunk.terrain.min_y().max(MIN_Y),
(chunk.terrain.min_y() + chunk.terrain.height()).min(MIN_Y + HEIGHT),
);
let mut values = Box::new([0u16; 4096]);
for si in 0..SECTIONS as i32 {
let base = MIN_Y + si * 16;
if base + 16 <= lo || base >= hi {
continue;
}
values.fill(0);
let mut any = false;
for y in base.max(lo)..(base + 16).min(hi) {
for z in 0..16 {
for x in 0..16 {
let id = chunk.block(bx + x, y, bz + z).0 as u16;
values[((y & 15) << 8 | z << 4 | x) as usize] = id;
any |= id != 0;
}
}
}
if any {
Arc::make_mut(&mut out.sections[si as usize]).blocks =
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;
for &packed in marks {
let p = i32::from(packed);
let y = sy + ((p >> 4) & 15);
if (lo..hi).contains(&y) {
out.postprocess
.push([bx + (p & 15), y, bz + ((p >> 8) & 15)]);
}
}
}
for si in 0..SECTIONS as i32 {
let mut cells = [0u32; 64];
for (j, cell) in cells.iter_mut().enumerate() {
let (qx, qz, qy) = (j as i32 & 3, (j as i32 >> 2) & 3, si * 4 + (j as i32 >> 4));
let y = (MIN_Y + qy * 4).clamp(lo, hi - 1);
let id = chunk.biome_id_at(bx + qx * 4, y, bz + qz * 4);
*cell = u32::from(self.biome_map.get(id.0 as usize).copied().unwrap_or(0));
}
Arc::make_mut(&mut out.sections[si as usize]).biomes =
PalettedContainer::from_values(BIOME_STRATEGY, &cells);
}
out
}
/// The structure starts owned by a chunk (cached by the generator).
pub fn starts_at(
&self,
@@ -209,89 +308,115 @@ impl ChunkGenerator for VanillaTerrain {
}
}
}
fn is_decorated(&self, proto: &Proto) -> bool {
matches!(proto.downcast_ref::<VanillaProto>(), Some(VanillaProto::Ready(c)) if c.is_decorated())
}
fn saved_proto(&self, proto: &Proto) -> Option<SavedProto> {
let Some(VanillaProto::Ready(chunk)) = proto.downcast_ref::<VanillaProto>() else {
return None;
};
let block_entities = chunk
.block_entities
.iter()
.map(|(pos, nbt)| {
let mut nbt = nbt.clone();
for (key, v) in ["x", "y", "z"].into_iter().zip(pos) {
nbt.insert(key.into(), Tag::Int(*v));
}
Tag::Compound(nbt)
})
.collect();
Some(SavedProto {
column: self.column(chunk),
decorated: chunk.is_decorated(),
block_entities,
entities: chunk.entities.iter().cloned().map(Tag::Compound).collect(),
})
}
/// The saved state replaces what decoration can change; biomes, structure
/// starts and references are generated again as they were.
fn restore_proto(&self, proto: &mut Proto, saved: SavedProto) {
let Some(VanillaProto::Ready(chunk)) = proto.downcast_mut::<VanillaProto>() else {
return;
};
let column = &saved.column;
let (bx, bz) = (column.x * 16, column.z * 16);
let (lo, hi) = (
chunk.terrain.min_y().max(MIN_Y),
(chunk.terrain.min_y() + chunk.terrain.height()).min(MIN_Y + HEIGHT),
);
for y in lo..hi {
for z in 0..16 {
for x in 0..16 {
let state = column.block(x, y, z);
if chunk.block(bx + x, y, bz + z) != state {
let _ = chunk.terrain.set_block_raw(bx + x, y, bz + z, state);
}
}
}
}
if saved.decorated {
chunk
.terrain
.set_generation_stage(PreLightChunkStage::Features);
chunk.set_decorated();
}
// Heightmaps follow the blocks (ProtoChunk primes missing ones on read).
let stage = chunk.terrain.generation_stage().heightmaps();
let kinds: Vec<HeightmapType> = HeightmapType::ALL
.into_iter()
.filter(|&k| chunk.terrain.heightmap(k).is_some() || stage.contains(&k))
.collect();
let _ = chunk.terrain.prime_heightmaps(&kinds);
let ticks = |ticks: &[SavedTick], kind: &dyn Fn(u32) -> Option<u32>| {
ProtoChunkTicks::from_saved(ticks.iter().filter_map(|t| {
Some(SavedTick {
kind: TickTypeId(kind(t.kind.0)?),
..*t
})
}))
};
if let Some(extras) = column.saved.as_deref() {
*chunk.terrain.block_ticks_mut() = ticks(&extras.block_ticks, &|k| {
self.tick_states.get(k as usize).copied()
});
*chunk.terrain.fluid_ticks_mut() = ticks(&extras.fluid_ticks, &Some);
}
chunk.terrain.clear_postprocessing();
for &[x, y, z] in &column.postprocess {
chunk.terrain.mark_postprocessing(x, y, z);
}
chunk.block_entities = saved
.block_entities
.into_iter()
.filter_map(|tag| {
let Tag::Compound(mut nbt) = tag else {
return None;
};
let mut coordinate = |key: &str| match nbt.remove(&key.into()) {
Some(Tag::Int(v)) => Some(v),
_ => None,
};
let pos = [coordinate("x")?, coordinate("y")?, coordinate("z")?];
Some((pos, nbt))
})
.collect();
chunk.entities = saved
.entities
.into_iter()
.filter_map(|tag| match tag {
Tag::Compound(nbt) => Some(nbt),
_ => None,
})
.collect();
}
fn finish(&self, proto: Proto) -> Chunk {
let proto = proto.downcast::<VanillaProto>().expect("vanilla proto");
let mut chunk = match *proto {
VanillaProto::Ready(c) => c,
VanillaProto::Failed(x, z) => return Chunk::empty(x, z),
};
let pos = chunk.position();
let mut out = Chunk::empty(pos.x, pos.z);
let (bx, bz) = (pos.x * 16, pos.z * 16);
// Only the generated range (the Nether is 0..256) is copied.
let (lo, hi) = (
chunk.terrain.min_y().max(MIN_Y),
(chunk.terrain.min_y() + chunk.terrain.height()).min(MIN_Y + HEIGHT),
);
let mut values = Box::new([0u16; 4096]);
for si in 0..SECTIONS as i32 {
let base = MIN_Y + si * 16;
if base + 16 <= lo || base >= hi {
continue;
}
values.fill(0);
let mut any = false;
for y in base.max(lo)..(base + 16).min(hi) {
for z in 0..16 {
for x in 0..16 {
let id = chunk.block(bx + x, y, bz + z).0 as u16;
values[((y & 15) << 8 | z << 4 | x) as usize] = id;
any |= id != 0;
}
}
}
if any {
Arc::make_mut(&mut out.sections[si as usize]).blocks =
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;
for &packed in marks {
let p = i32::from(packed);
let y = sy + ((p >> 4) & 15);
if (lo..hi).contains(&y) {
out.postprocess
.push([bx + (p & 15), y, bz + ((p >> 8) & 15)]);
}
}
}
for si in 0..SECTIONS as i32 {
let mut cells = [0u32; 64];
for (j, cell) in cells.iter_mut().enumerate() {
let (qx, qz, qy) = (j as i32 & 3, (j as i32 >> 2) & 3, si * 4 + (j as i32 >> 4));
let y = (MIN_Y + qy * 4).clamp(lo, hi - 1);
let id = chunk.biome_id_at(bx + qx * 4, y, bz + qz * 4);
*cell = u32::from(self.biome_map.get(id.0 as usize).copied().unwrap_or(0));
}
Arc::make_mut(&mut out.sections[si as usize]).biomes =
PalettedContainer::from_values(BIOME_STRATEGY, &cells);
}
let mut out = self.column(&chunk);
out.block_entities = crate::structure_blocks::block_entities(&chunk, self.loot.as_deref());
out.structure_refs = chunk
.structures()
@@ -0,0 +1,303 @@
//! Streaming keeps generation consistent when the player leaves an area and
//! comes back, and across a restart (#249): every column is finished once,
//! after its own decoration and its eight neighbours', and chunks that
//! decoration wrote to are saved rather than generated again without those
//! writes. Bastions lost whole chunks of pieces this way.
use horizon_client::{
assets::Assets,
biome::Biomes,
blocks::BlockFacts,
chunk::Chunk,
light::LightTables,
mesher::MeshTables,
models,
streaming::{ChunkGenerator, Proto, Streaming, WorkerContext},
world::World,
};
use horizon_world::registry::BlockStateId;
use std::{
collections::HashMap,
path::Path,
sync::{Arc, Mutex},
thread,
time::{Duration, Instant},
};
fn resources() -> Option<std::path::PathBuf> {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3");
root.join("items.json").exists().then_some(root)
}
/// A proto chunk that records the decorations that wrote to it.
#[derive(Clone)]
struct Recorded {
x: i32,
z: i32,
decorated: bool,
writes: Vec<(i32, i32)>,
}
/// Generation with an in-memory save, like `Persistent`.
#[derive(Default)]
struct Recorder {
protos: Mutex<HashMap<(i32, i32), Recorded>>,
chunks: Mutex<HashMap<(i32, i32), Chunk>>,
/// Each column as it was when it was finished, once per finish.
finished: Mutex<HashMap<(i32, i32), Vec<Recorded>>>,
}
impl Recorder {
fn save_chunk(&self, chunk: &Chunk) {
let mut saved = chunk.clone();
saved.fresh = false;
self.chunks
.lock()
.unwrap()
.insert((chunk.x, chunk.z), saved);
}
}
impl ChunkGenerator for Recorder {
type Worker = ();
fn worker(&self) {}
fn load(&self, x: i32, z: i32) -> Option<Chunk> {
self.chunks.lock().unwrap().get(&(x, z)).cloned()
}
fn proto(&self, _: &mut (), x: i32, z: i32) -> Proto {
let saved = self.protos.lock().unwrap().get(&(x, z)).cloned();
Box::new(saved.unwrap_or(Recorded {
x,
z,
decorated: false,
writes: Vec::new(),
}))
}
fn decorates(&self) -> bool {
true
}
fn decorate(&self, _: &mut (), region: &mut [Proto; 9]) {
let center = region[4].downcast_ref::<Recorded>().unwrap();
let center = (center.x, center.z);
for proto in region.iter_mut() {
proto
.downcast_mut::<Recorded>()
.unwrap()
.writes
.push(center);
}
region[4].downcast_mut::<Recorded>().unwrap().decorated = true;
}
fn finish(&self, proto: Proto) -> Chunk {
let proto = proto.downcast::<Recorded>().unwrap();
let mut chunk = Chunk::empty(proto.x, proto.z);
chunk.fresh = true;
self.finished
.lock()
.unwrap()
.entry((proto.x, proto.z))
.or_default()
.push(*proto);
chunk
}
fn is_decorated(&self, proto: &Proto) -> bool {
proto.downcast_ref::<Recorded>().unwrap().decorated
}
fn save_protos(&self, protos: &[&Proto]) {
let mut saved = self.protos.lock().unwrap();
for proto in protos {
let proto = proto.downcast_ref::<Recorded>().unwrap();
saved.insert((proto.x, proto.z), proto.clone());
}
}
fn save_finished(&self, chunk: Chunk) {
self.save_chunk(&chunk);
}
}
/// The game's part: columns saved when they unload and when the world saves.
struct Session {
streaming: Streaming,
world: World,
generator: Arc<Recorder>,
}
impl Session {
fn new(generator: Arc<Recorder>, context: Arc<WorkerContext>) -> Self {
let mut streaming = Streaming::new(generator.clone(), context, 3);
streaming.radius = 4;
Self {
streaming,
world: World::new(BlockStateId(0), 0),
generator,
}
}
fn update(&mut self) {
let (_, unloaded) = self.streaming.update(&mut self.world);
for chunk in unloaded {
if self.world.modified.remove(&(chunk.x, chunk.z)) {
self.generator.save_chunk(&chunk);
}
}
}
/// Run until generation around `center` is done and nothing changes.
fn settle_at(&mut self, center: (i32, i32)) {
self.streaming.center = center;
let deadline = Instant::now() + Duration::from_secs(120);
let mut quiet = 0;
while quiet < 40 {
assert!(Instant::now() < deadline, "generation never settled");
let before = (self.streaming.generated_total, self.world.chunks.len());
self.update();
thread::sleep(Duration::from_millis(5));
let idle = self.streaming.pending() == 0
&& before == (self.streaming.generated_total, self.world.chunks.len());
quiet = if idle { quiet + 1 } else { 0 };
}
}
fn save(&mut self) {
self.streaming.save(&mut self.world, true);
for key in std::mem::take(&mut self.world.modified) {
if let Some(chunk) = self.world.chunks.get(&key) {
self.generator.save_chunk(chunk);
}
}
}
}
#[test]
fn chunks_left_behind_keep_their_decoration() {
let Some(root) = resources() else {
return;
};
let assets = Arc::new(Assets::load(&root).unwrap());
let facts = BlockFacts::load(&root, assets.registry).unwrap();
let visibility = models::load_visibility(&root, assets.registry).unwrap();
let context = Arc::new(WorkerContext {
tables: Arc::new(MeshTables::new(&assets, &facts, visibility).unwrap()),
light: Arc::new(LightTables::load(&root, assets.registry, &facts).unwrap()),
biomes: Arc::new(Biomes::load(&root).unwrap()),
assets,
});
let generator = Arc::new(Recorder::default());
let mut session = Session::new(generator.clone(), context.clone());
// Generate an area, walk away from it in steps (its edge unloads a ring
// at a time), teleport far, come back past where the edge was; then
// save, restart and look again.
session.settle_at((0, 0));
for step in 1..=4 {
session.settle_at((step * 3, 0));
}
session.settle_at((300, 300));
session.settle_at((0, 0));
session.settle_at((-4, 0));
session.save();
drop(session);
let mut session = Session::new(generator.clone(), context);
session.settle_at((6, 0));
session.settle_at((0, 0));
let finished = generator.finished.lock().unwrap();
let mut checked = 0;
for (&(x, z), finishes) in finished.iter() {
assert_eq!(
finishes.len(),
1,
"({x}, {z}) was generated again after it was finished"
);
let column = &finishes[0];
assert!(column.decorated, "({x}, {z}) was finished undecorated");
let mut writes = column.writes.clone();
writes.sort_unstable();
let neighbours: Vec<(i32, i32)> = (-1..=1)
.flat_map(|dx| (-1..=1).map(move |dz| (x + dx, z + dz)))
.collect();
assert_eq!(
writes, neighbours,
"({x}, {z}) must hold each neighbour's decoration exactly once"
);
checked += 1;
}
for x in -4..=4 {
for z in (-4..=4).filter(|z| x * x + z * z <= 16) {
assert!(
session.world.chunks.contains_key(&(x, z)),
"({x}, {z}) did not load back"
);
}
}
assert!(checked > 200, "only {checked} columns finished");
}
/// A saved proto chunk comes back exactly: the bastion region of #249, its
/// centre decorated (writing into all nine chunks), saved and restored onto
/// chunks generated again finishes into the same columns.
#[test]
fn saved_proto_chunks_restore_exactly() {
use horizon_client::{
save::{Persistent, SaveDir},
vanillagen::VanillaTerrain,
};
let Some(root) = resources() else {
return;
};
let assets = Arc::new(Assets::load(&root).unwrap());
let biomes = Arc::new(Biomes::load(&root).unwrap());
let terrain = VanillaTerrain::for_dimension(
&root.join("worldgen"),
assets.registry,
&biomes,
-1860902098141295193,
"minecraft:the_nether",
)
.unwrap();
let dir = std::env::temp_dir().join(format!("horizon-proto-saves-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let save = Arc::new(SaveDir::new(dir.clone()));
let generator = Persistent {
inner: Arc::new(terrain),
save: save.clone(),
registry: assets.clone(),
biomes,
};
let mut worker = generator.worker();
let keys: Vec<(i32, i32)> = (0..9).map(|i| (3 + i % 3 - 1, 7 + i / 3 - 1)).collect();
let mut region: [Proto; 9] =
std::array::from_fn(|i| generator.proto(&mut worker, keys[i].0, keys[i].1));
generator.decorate(&mut worker, &mut region);
assert!(generator.is_decorated(&region[4]));
generator.save_protos(&region.iter().collect::<Vec<_>>());
save.flush();
let restored: Vec<Proto> = keys
.iter()
.map(|&(x, z)| generator.proto(&mut worker, x, z))
.collect();
for (i, (original, restored)) in region.into_iter().zip(restored).enumerate() {
assert_eq!(generator.is_decorated(&restored), i == 4);
let (a, b) = (generator.finish(original), generator.finish(restored));
let (x, z) = keys[i];
for y in horizon_client::chunk::MIN_Y..horizon_client::chunk::MIN_Y + 384 {
for lz in 0..16 {
for lx in 0..16 {
assert_eq!(
a.get(lx, y, lz),
b.get(lx, y, lz),
"block {} {y} {} differs",
x * 16 + lx,
z * 16 + lz
);
}
}
}
assert_eq!(
format!("{:?}", a.block_entities),
format!("{:?}", b.block_entities)
);
assert_eq!(a.postprocess, b.postprocess, "chunk {x} {z}");
assert_eq!(a.entities, b.entities, "chunk {x} {z}");
assert_eq!(a.structure_refs, b.structure_refs, "chunk {x} {z}");
let ticks = |c: &Chunk| {
c.saved
.as_ref()
.map(|s| (s.block_ticks.clone(), s.fluid_ticks.clone()))
};
assert_eq!(ticks(&a), ticks(&b), "chunk {x} {z}");
}
drop(generator);
drop(save);
let _ = std::fs::remove_dir_all(&dir);
}
@@ -552,7 +552,8 @@ impl ProtoChunk {
pub fn is_decorated(&self) -> bool {
self.decorated
}
pub(super) fn set_decorated(&mut self) {
/// Also restores a saved proto chunk that was decorated before it was saved.
pub fn set_decorated(&mut self) {
self.decorated = true;
}
/// Highest non-air block + 1 in the column (WORLD_SURFACE).
@@ -444,6 +444,10 @@ impl<'r, B: TerrainBlockStates> TerrainChunk<'r, B> {
.set_raw_data(&self.blocks, words)?;
Ok(())
}
/// Before replaying a saved chunk's `PostProcessing` lists.
pub fn clear_postprocessing(&mut self) {
self.postprocessing.iter_mut().for_each(Vec::clear);
}
/// Preserves duplicates and insertion order, as ProtoChunk does.
pub fn mark_postprocessing(&mut self, x: i32, y: i32, z: i32) {
if let Some(section) = self.blocks.section_index(y) {
@@ -135,6 +135,8 @@ public final class VanillaStructurePlacementOracle {
int[] center={cx,cz};
var heightRef=new Object(){LevelHeightAccessor value=height;};
BiomeManager[] zoom=new BiomeManager[1];
// WorldGenRegion's static attribute layers (lava's tick delay reads FAST_LAVA).
net.minecraft.world.attribute.EnvironmentAttributeSystem[] attributes=new net.minecraft.world.attribute.EnvironmentAttributeSystem[1];
var uncached=biomeSource;
Object[] self=new Object[1];
var level=(WorldGenLevel)Proxy.newProxyInstance(WorldGenLevel.class.getClassLoader(),new Class<?>[]{WorldGenLevel.class},(proxy,method,values)->{
@@ -185,6 +187,7 @@ public final class VanillaStructurePlacementOracle {
}
case "hasChunk":return true;
case "getBiomeManager":return zoom[0];
case "environmentAttributes":return attributes[0];
case "getBiome":return zoom[0].getBiome((BlockPos)values[0]);
case "getNoiseBiome":{int qx=(Integer)values[0],qy=(Integer)values[1],qz=(Integer)values[2];return chunkAt.apply(qx>>2,qz>>2).getNoiseBiome(qx,qy,qz);}
case "getUncachedNoiseBiome":return uncached.createResolver(climate).getNoiseBiome((Integer)values[0],(Integer)values[1],(Integer)values[2]);
@@ -204,6 +207,7 @@ public final class VanillaStructurePlacementOracle {
});
self[0]=randomState.getOrCreateRandomFactory(Identifier.withDefaultNamespace("worldgen_region_random")).at(new ChunkPos(cx,cz).getWorldPosition());
zoom[0]=new BiomeManager(level,BiomeManager.obfuscateSeed(seed));
attributes[0]=net.minecraft.world.attribute.EnvironmentAttributeSystem.builder().addStaticLayers(level).build();
var manager=new StructureManager(level,new WorldOptions(seed,true,false),null);
// BIOMES for the 5x5 area, then TERRAIN for the 3x3 region.
for(int x=cx-2;x<=cx+2;x++)for(int z=cz-2;z<=cz+2;z++){generator.createBiomes(randomState,Blender.empty(),manager,chunkAt.apply(x,z)).join();chunkAt.apply(x,z).setPersistedStatus(ChunkStatus.BIOMES);}
+18 -4
View File
@@ -11,7 +11,7 @@ block entity (canonical NBT) of each target chunk is compared.
Cases: terrain sites for several seeds across the overworld, the Nether and the
End, plus rectangles around vanilla structure starts found by scanning each
seed (villages, mineshafts, strongholds, ancient cities, trial chambers,
monuments, mansions, ...).
monuments, mansions, ...), and the fixed sites of REGRESSIONS.
usage: test_chunk_generation_parity.py [--seeds N] [--per-structure K]
[--filter REGEX] [--dimension overworld|the_nether|the_end] [--keep]
@@ -35,6 +35,11 @@ except ImportError:
import test_rust_parity as base
SEEDS = [12345, -7046029254386353131, 987654321987, 4242, -1, 0x5DEECE66D, 20260607, 777]
# Sites from bug reports, compared on every run: seed, dimension, chunk
# rectangle x0,z0,x1,z1 and label.
REGRESSIONS = [
(-1860902098141295193, 'the_nether', '-1,6,3,9', 'bastion_remnant across 20 chunks (#249)'),
]
RESOURCES = lab.LOCAL / 'horizon-client/26.3'
ORACLE = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaChunkGenerationOracle.java'
@@ -304,20 +309,29 @@ def main():
biome_seen = set()
structure_seen = Counter()
started = time.time()
for seed in args.seed or SEEDS[:args.seeds]:
seeds = list(args.seed or SEEDS[:args.seeds])
sampled = set(seeds)
if not args.seed and not args.rect:
seeds += [s for s, *_ in REGRESSIONS if s not in sampled]
for seed in seeds:
rng = random.Random(seed)
for dimension in dimensions:
rects, labels = [], []
if args.rect:
rects, labels = list(args.rect), [f'rect {r}' for r in args.rect]
else:
for s, d, rect, label in REGRESSIONS:
if (s, d) == (seed, dimension) and not (args.filter and not re.search(args.filter, label)):
rects.append(rect)
labels.append(label)
r = args.radius
if not args.structures_only and not args.rect:
if seed in sampled and not args.structures_only and not args.rect:
spread = {'overworld': 5000, 'the_nether': 1500, 'the_end': 1800}[dimension]
sites = [(0, 0)] + [(rng.randint(-spread, spread) // 16, rng.randint(-spread, spread) // 16) for _ in range(args.sites if dimension == 'overworld' else 2)]
for cx, cz in sites:
rects.append(f'{cx - r},{cz - r},{cx + r},{cz + r}')
labels.append(f'terrain {cx},{cz}')
if not args.terrain_only and not args.rect:
if seed in sampled and not args.terrain_only and not args.rect:
starts = scan(oracle, binary, seed, dimension, args.scan_radius if dimension == 'overworld' else 80)
for kind, cx, cz, box in pick_structures(starts, args.per_structure, rng):
if args.filter and not re.search(args.filter, kind):