Files
horizon-airdef/crates/horizon-client/src/flatgen.rs
T
Ludovic Marie 7ace13cc82 Find a new world's spawn as vanilla does
Horizon searched its own spiral of 48-block steps from 0 0 for a dry,
non-leaf top block, so seed 1891623539983467248 spawned at -48 -48 where
vanilla puts the world spawn at 144 71 144 (the player then lands within
the respawn radius, 152 145 in the report).

Vanilla (MinecraftServer.setInitialSpawn): NoiseBasedChunkGenerator.
getOrigin runs NoiseSpawnFinder over the noise settings' spawn_target
points (SpawnTargetPoint.sampleFitness, a 2048/512 then 512/32 radial
search biased towards 0 0); an 11 by 11 chunk spiral around that chunk
takes the first column PlayerSpawnFinder.getSpawnPosInChunk accepts
(getLevelRespawnPos: MOTION_BLOCKING, WORLD_SURFACE and OCEAN_FLOOR
heights, no liquid, a full-topped collision shape); without one, the
origin chunk's centre at the spawn height. The first player is then
placed by PlayerSpawnFinder.findSpawn: the respawn-radius square in a
random coprime order, a free player box with liquids colliding, else the
spawn with fixupSpawnHeight; adventure mode uses the spawn as is.

horizon-world gains worldgen::spawn_finder and OverworldGenerator::origin;
the client's world_spawn module and game/initial_spawn.rs follow
setInitialSpawn and PlayerSpawnFinder, for single player and the
dedicated server alike. Flat worlds use FlatLevelSource's origin and
spawn height.

tools/test_world_spawn_parity.py compares, for the report's seed, the
chunk harness's seeds and 50 random ones, the origins
(VanillaWorldSpawnOracle against the spawn_origin example) and the
level.dat spawn of worlds created by the vanilla dedicated server and by
Horizon's. Vanilla is not deterministic on chunk corners where trees of
neighbouring chunks overlap (decoration order follows its worker
threads), so a mismatching seed reruns vanilla up to three times.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 17:49:15 +02:00

78 lines
2.9 KiB
Rust

//! FlatLevelSource: superflat worlds (`minecraft:flat` generator).
use crate::{chunk::Chunk, streaming::ChunkGenerator};
use horizon_world::{nbt::Tag, registry::BlockRegistry};
/// The layers of a flat generator, bottom up from the world's minimum Y.
pub struct FlatTerrain {
layers: Vec<u16>,
biome: u16,
}
impl FlatTerrain {
/// The overworld generator of `world_gen_settings` (`data.dimensions`), if it
/// is flat. Its `features` and `lakes` decorate the world; a world without
/// them (the default) gets only its layers, as FlatLevelSource places them.
pub fn from_settings(
settings: &Tag,
registry: &BlockRegistry,
biomes: &crate::biome::Biomes,
) -> Option<Self> {
let generator = settings
.get("dimensions")?
.get("minecraft:overworld")?
.get("generator")?;
if crate::save::text(generator.get("type"))?.trim_start_matches("minecraft:") != "flat" {
return None;
}
let s = generator.get("settings")?;
let biome = crate::save::text(s.get("biome")).unwrap_or_else(|| "minecraft:plains".into());
let mut layers = Vec::new();
if let Some(Tag::List(list)) = s.get("layers") {
for layer in list {
let block = crate::save::text(layer.get("block"))?;
let height = crate::save::number(layer.get("height")).unwrap_or(0).max(0);
let state = registry.default_state(&block)?.0 as u16;
layers.extend(std::iter::repeat_n(state, height as usize));
}
}
Some(Self {
layers,
biome: biomes.id(&biome),
})
}
/// FlatLevelSource.getSpawnHeight: the top of the layers.
pub fn spawn_height(&self) -> i32 {
crate::chunk::MIN_Y + self.layers.len().min(crate::chunk::HEIGHT as usize) as i32
}
}
impl ChunkGenerator for FlatTerrain {
type Worker = ();
fn worker(&self) {}
fn proto(&self, _: &mut (), cx: i32, cz: i32) -> crate::streaming::Proto {
let mut chunk = Chunk::empty(cx, cz);
for s in &mut chunk.sections {
std::sync::Arc::make_mut(s).biomes = horizon_core::paletted::PalettedContainer::new(
crate::chunk::BIOME_STRATEGY,
u32::from(self.biome),
);
}
for (i, &block) in self.layers.iter().enumerate().take(384) {
if block == 0 {
continue;
}
for z in 0..16 {
for x in 0..16 {
chunk.set(x, -64 + i as i32, z, block);
}
}
}
for s in &mut chunk.sections {
std::sync::Arc::make_mut(s).compact();
}
Box::new(chunk)
}
/// FlatLevelSource.spawnOriginalMobs is empty: no generation-time spawns.
fn finish(&self, proto: crate::streaming::Proto) -> Chunk {
*proto.downcast::<Chunk>().expect("flat terrain proto")
}
}