From 5159883086c1b2a8a96dca7d09afdc78230a0b3a Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 8 Oct 2026 11:20:05 +0200 Subject: [PATCH] WIP snapshot: mineshaft and stronghold structures Uncommitted work from a finished agent worktree on ludothinkpad (/home/ludovic/.t3/scratch/2026-10-03-i-want-you-to-retro-e5fa4461/.claude/worktrees/agent-ac2ea1edb29c4454e), saved before the worktree was removed. Not built or verified. Co-Authored-By: Claude Opus 5.5 --- .../horizon-world/src/worldgen/generator.rs | 3 + .../src/worldgen/generator_decoration.rs | 11 + .../src/worldgen/generator_structures.rs | 14 +- .../src/worldgen/placement/context.rs | 8 + .../horizon-world/src/worldgen/structure.rs | 2 + .../src/worldgen/structure/legacy.rs | 37 +- .../src/worldgen/structure/mineshaft.rs | 743 ++++++++++++ .../src/worldgen/structure/start.rs | 83 +- .../src/worldgen/structure/stronghold.rs | 1060 +++++++++++++++++ .../parity/VanillaStructureStartOracle.java | 3 + tools/structure_start_oracle.py | 3 +- 11 files changed, 1957 insertions(+), 10 deletions(-) create mode 100644 crates/horizon-world/src/worldgen/structure/mineshaft.rs create mode 100644 crates/horizon-world/src/worldgen/structure/stronghold.rs diff --git a/crates/horizon-world/src/worldgen/generator.rs b/crates/horizon-world/src/worldgen/generator.rs index 3a0b76529..9124b942e 100644 --- a/crates/horizon-world/src/worldgen/generator.rs +++ b/crates/horizon-world/src/worldgen/generator.rs @@ -1137,6 +1137,9 @@ where fn set_valid_biomes(&mut self, biomes: &BTreeSet) { self.valid = biomes.clone(); } + fn sea_level(&self) -> i32 { + self.pipeline.settings.sea_level + } } enum Biomes<'a, 'b> { diff --git a/crates/horizon-world/src/worldgen/generator_decoration.rs b/crates/horizon-world/src/worldgen/generator_decoration.rs index b2da351c2..fd3136932 100644 --- a/crates/horizon-world/src/worldgen/generator_decoration.rs +++ b/crates/horizon-world/src/worldgen/generator_decoration.rs @@ -571,6 +571,17 @@ impl PlacementContext for World<'_> { .is_face_sturdy(self, state, position, direction, SupportType::Full) .map_err(query) } + fn center_sturdy( + &mut self, + position: [i32; 3], + direction: Direction, + ) -> Result { + let state = self.region.block_state(position)?; + let support = self.blocks()?.support.clone(); + support + .is_face_sturdy(self, state, position, direction, SupportType::Center) + .map_err(query) + } fn collision_full_block( &mut self, state: BlockStateId, diff --git a/crates/horizon-world/src/worldgen/generator_structures.rs b/crates/horizon-world/src/worldgen/generator_structures.rs index d772d7859..2f32e0120 100644 --- a/crates/horizon-world/src/worldgen/generator_structures.rs +++ b/crates/horizon-world/src/worldgen/generator_structures.rs @@ -5,14 +5,14 @@ use super::{ generator::{GeneratorData, GeneratorError}, structure::{ StructureResources, - start::{StructureRegistry, StructureStart, beardifier, create_starts}, + start::{Rings, StructureRegistry, StructureStart, beardifier, create_starts}, }, }; use horizon_core::{density::Beardifier, position::ChunkPos}; use serde_json::Value; use std::{ collections::{BTreeSet, HashMap}, - sync::{Arc, Mutex}, + sync::{Arc, Mutex, OnceLock}, }; pub(super) type Starts = Arc>>; @@ -24,6 +24,8 @@ pub(super) struct StructureState { /// Structure indices of each decoration step, in registry order. pub(super) by_step: Vec>, starts: Mutex>, + /// Concentric ring positions, computed on first use. + rings: OnceLock, } const START_CACHE_LIMIT: usize = 65536; @@ -85,6 +87,7 @@ impl StructureState { possible_sets, by_step, starts: Mutex::new(HashMap::new()), + rings: OnceLock::new(), })) } /// The (cached) structure starts owned by a chunk. @@ -96,6 +99,11 @@ impl StructureState { if let Some(found) = self.starts.lock().unwrap().get(&(chunk.x, chunk.z)) { return found.clone(); } + let rings = self.rings.get_or_init(|| { + let seed = context.seed(); + self.registry + .ring_positions(&self.possible_sets, seed, &mut |q| context.noise_biome(q)) + }); let starts: Starts = Arc::new( create_starts( &self.registry, @@ -103,7 +111,7 @@ impl StructureState { &self.bindings, context, &self.possible_sets, - &[], + rings, chunk, ) .into_iter() diff --git a/crates/horizon-world/src/worldgen/placement/context.rs b/crates/horizon-world/src/worldgen/placement/context.rs index f64dadf94..194a4d479 100644 --- a/crates/horizon-world/src/worldgen/placement/context.rs +++ b/crates/horizon-world/src/worldgen/placement/context.rs @@ -237,6 +237,14 @@ pub trait PlacementContext { ) -> Result { Err(PlacementQueryError::Unavailable) } + /// Block.canSupportCenter: CENTER support on the requested face. + fn center_sturdy( + &mut self, + _position: [i32; 3], + _direction: horizon_core::geometry::Direction, + ) -> Result { + Err(PlacementQueryError::Unavailable) + } /// Test entity obstruction of a unit-block shape with no excluded entity. fn unobstructed_block(&mut self, _position: [i32; 3]) -> Result { Err(PlacementQueryError::Unavailable) diff --git a/crates/horizon-world/src/worldgen/structure.rs b/crates/horizon-world/src/worldgen/structure.rs index c5146d62a..34e60d2e3 100644 --- a/crates/horizon-world/src/worldgen/structure.rs +++ b/crates/horizon-world/src/worldgen/structure.rs @@ -3,10 +3,12 @@ pub mod features; pub mod jigsaw; pub mod legacy; +pub mod mineshaft; pub mod ocean_ruin; pub mod processor; pub mod simple; pub mod start; +pub mod stronghold; pub mod template; use crate::{ diff --git a/crates/horizon-world/src/worldgen/structure/legacy.rs b/crates/horizon-world/src/worldgen/structure/legacy.rs index d78b89b68..d846d666a 100644 --- a/crates/horizon-world/src/worldgen/structure/legacy.rs +++ b/crates/horizon-world/src/worldgen/structure/legacy.rs @@ -11,6 +11,7 @@ use crate::{ nbt::{Compound, JavaString, Tag}, registry::BlockStateId, worldgen::{ + biome::BiomeId, feature::FeatureWorld, placement::{PlacementContext, PlacementQueryError}, }, @@ -48,6 +49,10 @@ pub trait PieceWorld { fn max_y(&mut self) -> Result; fn block_entity(&mut self, pos: [i32; 3], nbt: Compound) -> Result<()>; fn post_process(&mut self, pos: [i32; 3]) -> Result<()>; + fn face_sturdy(&mut self, pos: [i32; 3], face: Direction) -> Result; + fn center_sturdy(&mut self, pos: [i32; 3], face: Direction) -> Result; + /// WorldGenLevel.getBiome (zoomed). + fn biome(&mut self, pos: [i32; 3]) -> Result; fn place_template( &mut self, resources: &StructureResources, @@ -85,6 +90,15 @@ impl PieceWorld for W { fn post_process(&mut self, pos: [i32; 3]) -> Result<()> { self.mark_postprocessing(pos) } + fn face_sturdy(&mut self, pos: [i32; 3], face: Direction) -> Result { + PlacementContext::face_sturdy(self, pos, face) + } + fn center_sturdy(&mut self, pos: [i32; 3], face: Direction) -> Result { + PlacementContext::center_sturdy(self, pos, face) + } + fn biome(&mut self, pos: [i32; 3]) -> Result { + self.biome_identity(pos) + } fn place_template( &mut self, resources: &StructureResources, @@ -170,6 +184,18 @@ impl PieceContext<'_> { nbt.insert("LootTableSeed".into(), Tag::Long(seed)); self.world.block_entity(pos, nbt) } + /// Place a spawner and set its entity (SpawnerBlockEntity.setEntityId). + pub fn spawner(&mut self, pos: [i32; 3], entity: &str) -> Result<()> { + self.world.set(pos, self.state("spawner"), 2)?; + let mut id = Compound::new(); + id.insert("id".into(), Tag::String(entity.into())); + let mut data = Compound::new(); + data.insert("entity".into(), Tag::Compound(id)); + let mut nbt = Compound::new(); + nbt.insert("id".into(), Tag::String("minecraft:mob_spawner".into())); + nbt.insert("SpawnData".into(), Tag::Compound(data)); + self.world.block_entity(pos, nbt) + } } /// A state by block name and properties (missing properties keep defaults). pub fn state( @@ -213,6 +239,8 @@ pub struct PieceFrame { pub orientation: Option, pub mirror: Mirror, pub rotation: Rotation, + /// Blocks placeBlock must not replace (StructurePiece.canBeReplaced). + pub keep: &'static [&'static str], } pub const HORIZONTAL: [Direction; 4] = [ Direction::North, @@ -252,6 +280,7 @@ impl PieceFrame { orientation, mirror, rotation, + keep: &[], } } pub fn world_x(&self, x: i32, z: i32) -> i32 { @@ -293,6 +322,12 @@ impl PieceFrame { if !c.chunk_box.is_inside(pos) { return Ok(()); } + if !self.keep.is_empty() { + let current = c.world.block(pos)?; + if self.keep.contains(&c.name(current)) { + return Ok(()); + } + } let mut state = state; if self.mirror != Mirror::None { state = c.resources.transforms.mirror(state, self.mirror); @@ -551,7 +586,7 @@ pub fn offset(pos: [i32; 3], d: Direction, n: i32) -> [i32; 3] { let s = d.normal(); [pos[0] + s[0] * n, pos[1] + s[1] * n, pos[2] + s[2] * n] } -fn solid_render(c: &PieceContext<'_>, state: BlockStateId) -> bool { +pub fn solid_render(c: &PieceContext<'_>, state: BlockStateId) -> bool { c.resources.transforms.full_block(state) && !c.is_liquid(state) } /// StructurePiece.createChest with an absolute position. diff --git a/crates/horizon-world/src/worldgen/structure/mineshaft.rs b/crates/horizon-world/src/worldgen/structure/mineshaft.rs new file mode 100644 index 000000000..5b6a306c3 --- /dev/null +++ b/crates/horizon-world/src/worldgen/structure/mineshaft.rs @@ -0,0 +1,743 @@ +//! Mineshafts: MineshaftStructure and MineshaftPieces. +use super::{ + BoundingBox, + legacy::{LegacyPiece, PieceContext, PieceFrame, Result, solid_render}, +}; +use crate::worldgen::biome::BiomeId; +use horizon_core::{geometry::Direction, random::RandomSource}; +use std::{ + collections::BTreeSet, + sync::{Arc, Mutex}, +}; + +/// MineshaftStructure.Type: wood, planks, fence, and the blocks pieces keep. +#[derive(Clone, Copy, Debug)] +pub struct Wood { + log: &'static str, + planks: &'static str, + fence: &'static str, + keep: &'static [&'static str], +} +pub const NORMAL: Wood = Wood { + log: "minecraft:oak_log", + planks: "minecraft:oak_planks", + fence: "minecraft:oak_fence", + keep: &[ + "minecraft:oak_planks", + "minecraft:oak_log", + "minecraft:oak_fence", + "minecraft:iron_chain", + ], +}; +pub const MESA: Wood = Wood { + log: "minecraft:dark_oak_log", + planks: "minecraft:dark_oak_planks", + fence: "minecraft:dark_oak_fence", + keep: &[ + "minecraft:dark_oak_planks", + "minecraft:dark_oak_log", + "minecraft:dark_oak_fence", + "minecraft:iron_chain", + ], +}; + +#[derive(Debug)] +enum Kind { + Room(Vec), + Corridor { + rails: bool, + spider: bool, + sections: i32, + placed_spider: Mutex, + }, + Crossing { + direction: Direction, + two_floored: bool, + }, + Stairs, +} +#[derive(Debug)] +pub struct MineshaftPiece { + kind: Kind, + frame: PieceFrame, + depth: i32, + wood: Wood, + blocking: Arc>, +} + +fn int(r: &mut impl RandomSource, bound: i32) -> i32 { + r.next_int_bound(bound).unwrap_or(0) +} +fn bb(a: [i32; 3], b: [i32; 3], at: [i32; 3]) -> BoundingBox { + BoundingBox::new(a, b).moved(at) +} + +struct Builder { + pieces: Vec, +} +impl Builder { + fn collides(&self, b: &BoundingBox) -> bool { + self.pieces + .iter() + .any(|p| p.frame.bounding_box.intersects(b)) + } + fn piece( + &self, + kind: Kind, + b: BoundingBox, + o: Option, + depth: i32, + ) -> MineshaftPiece { + let start = &self.pieces[0]; + let mut frame = PieceFrame::new(b, o); + frame.keep = start.wood.keep; + MineshaftPiece { + kind, + frame, + depth, + wood: start.wood, + blocking: start.blocking.clone(), + } + } + /// MineshaftPieces.createRandomShaftPiece. + fn create( + &self, + r: &mut impl RandomSource, + at: [i32; 3], + d: Direction, + depth: i32, + ) -> Option { + let selection = int(r, 100); + if selection >= 80 { + let y1 = if int(r, 4) == 0 { 6 } else { 2 }; + let b = match d { + Direction::South => bb([-1, 0, 0], [3, y1, 4], at), + Direction::West => bb([-4, 0, -1], [0, y1, 3], at), + Direction::East => bb([0, 0, -1], [4, y1, 3], at), + _ => bb([-1, 0, -4], [3, y1, 0], at), + }; + if self.collides(&b) { + return None; + } + let kind = Kind::Crossing { + direction: d, + two_floored: b.span()[1] > 3, + }; + Some(self.piece(kind, b, None, depth)) + } else if selection >= 70 { + let b = match d { + Direction::South => bb([0, -5, 0], [2, 2, 8], at), + Direction::West => bb([-8, -5, 0], [0, 2, 2], at), + Direction::East => bb([0, -5, 0], [8, 2, 2], at), + _ => bb([0, -5, -8], [2, 2, 0], at), + }; + (!self.collides(&b)).then(|| self.piece(Kind::Stairs, b, Some(d), depth)) + } else { + let mut length = int(r, 3) + 2; + while length > 0 { + let l = length * 5 - 1; + let b = match d { + Direction::South => bb([0, 0, 0], [2, 2, l], at), + Direction::West => bb([-l, 0, 0], [0, 2, 2], at), + Direction::East => bb([0, 0, 0], [l, 2, 2], at), + _ => bb([0, 0, -l], [2, 2, 0], at), + }; + if !self.collides(&b) { + let rails = int(r, 3) == 0; + let spider = !rails && int(r, 23) == 0; + let axis = if matches!(d, Direction::North | Direction::South) { + 2 + } else { + 0 + }; + let kind = Kind::Corridor { + rails, + spider, + sections: b.span()[axis] / 5, + placed_spider: Mutex::new(false), + }; + return Some(self.piece(kind, b, Some(d), depth)); + } + length -= 1; + } + None + } + } + /// MineshaftPieces.generateAndAddPiece; returns the new piece's box. + fn add( + &mut self, + r: &mut impl RandomSource, + at: [i32; 3], + d: Direction, + depth: i32, + ) -> Option { + if depth > 8 { + return None; + } + let start = self.pieces[0].frame.bounding_box; + if (at[0] - start.min[0]).abs() > 80 || (at[2] - start.min[2]).abs() > 80 { + return None; + } + let piece = self.create(r, at, d, depth + 1)?; + let b = piece.frame.bounding_box; + self.pieces.push(piece); + self.add_children(r, self.pieces.len() - 1); + Some(b) + } + fn add_children(&mut self, r: &mut impl RandomSource, index: usize) { + let p = &self.pieces[index]; + let (b, depth, orientation) = (p.frame.bounding_box, p.depth, p.frame.orientation); + let ([x0, y0, z0], [x1, y1, z1]) = (b.min, b.max); + use Direction::*; + match &p.kind { + Kind::Room(_) => { + let height = (b.span()[1] - 4).max(1); + let [xs, _, zs] = b.span(); + for side in 0..4 { + let span = if side < 2 { xs } else { zs }; + let mut pos = 0; + while pos < span { + pos += int(r, span); + if pos + 3 > span { + break; + } + let y = y0 + int(r, height) + 1; + let (at, d) = match side { + 0 => ([x0 + pos, y, z0 - 1], North), + 1 => ([x0 + pos, y, z1 + 1], South), + 2 => ([x0 - 1, y, z0 + pos], West), + _ => ([x1 + 1, y, z0 + pos], East), + }; + if let Some(c) = self.add(r, at, d, depth) { + let entrance = match side { + 0 => BoundingBox::new( + [c.min[0], c.min[1], z0], + [c.max[0], c.max[1], z0 + 1], + ), + 1 => BoundingBox::new( + [c.min[0], c.min[1], z1 - 1], + [c.max[0], c.max[1], z1], + ), + 2 => BoundingBox::new( + [x0, c.min[1], c.min[2]], + [x0 + 1, c.max[1], c.max[2]], + ), + _ => BoundingBox::new( + [x1 - 1, c.min[1], c.min[2]], + [x1, c.max[1], c.max[2]], + ), + }; + if let Kind::Room(entrances) = &mut self.pieces[index].kind { + entrances.push(entrance); + } + } + pos += 4; + } + } + } + Kind::Corridor { .. } => { + let end = int(r, 4); + let o = orientation.unwrap_or(North); + let (forward, left, right) = match o { + South => ( + [x0, z1 + 1], + (West, [x0 - 1, z1 - 3]), + (East, [x1 + 1, z1 - 3]), + ), + West => ([x0 - 1, z0], (North, [x0, z0 - 1]), (South, [x0, z1 + 1])), + East => ( + [x1 + 1, z0], + (North, [x1 - 3, z0 - 1]), + (South, [x1 - 3, z1 + 1]), + ), + _ => ([x0, z0 - 1], (West, [x0 - 1, z0]), (East, [x1 + 1, z0])), + }; + let (d, [x, z]) = match end { + 0 | 1 => (o, forward), + 2 => left, + _ => right, + }; + let y = y0 - 1 + int(r, 3); + self.add(r, [x, y, z], d, depth); + if depth < 8 { + if o != North && o != South { + let mut x = x0 + 3; + while x + 3 <= x1 { + match int(r, 5) { + 0 => { + self.add(r, [x, y0, z0 - 1], North, depth + 1); + } + 1 => { + self.add(r, [x, y0, z1 + 1], South, depth + 1); + } + _ => {} + } + x += 5; + } + } else { + let mut z = z0 + 3; + while z + 3 <= z1 { + match int(r, 5) { + 0 => { + self.add(r, [x0 - 1, y0, z], West, depth + 1); + } + 1 => { + self.add(r, [x1 + 1, y0, z], East, depth + 1); + } + _ => {} + } + z += 5; + } + } + } + } + Kind::Crossing { + direction, + two_floored, + } => { + let two = *two_floored; + let n = ([x0 + 1, y0, z0 - 1], North); + let s = ([x0 + 1, y0, z1 + 1], South); + let w = ([x0 - 1, y0, z0 + 1], West); + let e = ([x1 + 1, y0, z0 + 1], East); + let order = match direction { + South => [s, w, e], + West => [n, s, w], + East => [n, s, e], + _ => [n, w, e], + }; + for (at, d) in order { + self.add(r, at, d, depth); + } + if two { + for (at, d) in [n, w, e, s] { + if r.next_boolean() { + self.add(r, [at[0], y0 + 4, at[2]], d, depth); + } + } + } + } + Kind::Stairs => { + let (at, d) = match orientation.unwrap_or(North) { + South => ([x0, y0, z1 + 1], South), + West => ([x0 - 1, y0, z0], West), + East => ([x1 + 1, y0, z0], East), + _ => ([x0, y0, z0 - 1], North), + }; + self.add(r, at, d, depth); + } + } + } +} + +/// MineshaftStructure.findGenerationPoint: the pieces and the start position. +pub fn generate( + r: &mut impl RandomSource, + chunk: [i32; 2], + mesa: bool, + blocking: Arc>, + sea_level: i32, + min_y: i32, + surface: &mut dyn FnMut(i32, i32) -> i32, +) -> ([i32; 3], Vec) { + r.next_double(); + let (west, north) = (chunk[0] * 16 + 2, chunk[1] * 16 + 2); + let room = BoundingBox::new( + [west, 50, north], + [west + 7 + int(r, 6), 54 + int(r, 6), north + 7 + int(r, 6)], + ); + let wood = if mesa { MESA } else { NORMAL }; + let mut frame = PieceFrame::new(room, None); + frame.keep = wood.keep; + let mut builder = Builder { + pieces: vec![MineshaftPiece { + kind: Kind::Room(Vec::new()), + frame, + depth: 0, + wood, + blocking, + }], + }; + builder.add_children(r, 0); + let mut all = builder.pieces[0].frame.bounding_box; + for p in &builder.pieces[1..] { + all.encapsulate(&p.frame.bounding_box); + } + let dy = if mesa { + let center = all.center(); + let height = surface(center[0], center[2]); + let target = if height <= sea_level { + sea_level + } else { + int(r, height - sea_level + 1) + sea_level + }; + target - center[1] + } else { + let max = sea_level - 10; + let mut y1 = all.span()[1] + min_y + 1; + if y1 < max { + y1 += int(r, max - y1); + } + y1 - all.max[1] + }; + for p in &mut builder.pieces { + p.frame.bounding_box = p.frame.bounding_box.moved([0, dy, 0]); + if let Kind::Room(entrances) = &mut p.kind { + for e in entrances { + *e = e.moved([0, dy, 0]); + } + } + } + ([chunk[0] * 16 + 8, 50 + dy, chunk[1] * 16], builder.pieces) +} + +fn replaceable(name: &str) -> bool { + matches!( + name, + "minecraft:air" + | "minecraft:cave_air" + | "minecraft:void_air" + | "minecraft:water" + | "minecraft:lava" + | "minecraft:glow_lichen" + | "minecraft:seagrass" + | "minecraft:tall_seagrass" + ) +} +fn falling(name: &str) -> bool { + matches!( + name, + "minecraft:sand" + | "minecraft:red_sand" + | "minecraft:gravel" + | "minecraft:anvil" + | "minecraft:chipped_anvil" + | "minecraft:damaged_anvil" + | "minecraft:dragon_egg" + ) || name.ends_with("_concrete_powder") +} + +impl MineshaftPiece { + fn invalid(&self, c: &mut PieceContext<'_>) -> Result { + let (b, k) = (self.frame.bounding_box, c.chunk_box); + let lo: [i32; 3] = std::array::from_fn(|i| (b.min[i] - 1).max(k.min[i])); + let hi: [i32; 3] = std::array::from_fn(|i| (b.max[i] + 1).min(k.max[i])); + let center = std::array::from_fn(|i| (lo[i] + hi[i]) / 2); + if self.blocking.contains(&c.world.biome(center)?) { + return Ok(true); + } + let liquid = |c: &mut PieceContext<'_>, p: [i32; 3]| -> Result { + let s = c.world.block(p)?; + Ok(c.is_liquid(s)) + }; + for x in lo[0]..=hi[0] { + for z in lo[2]..=hi[2] { + if liquid(c, [x, lo[1], z])? || liquid(c, [x, hi[1], z])? { + return Ok(true); + } + } + } + for x in lo[0]..=hi[0] { + for y in lo[1]..=hi[1] { + if liquid(c, [x, y, lo[2]])? || liquid(c, [x, y, hi[2]])? { + return Ok(true); + } + } + } + for z in lo[2]..=hi[2] { + for y in lo[1]..=hi[1] { + if liquid(c, [lo[0], y, z])? || liquid(c, [hi[0], y, z])? { + return Ok(true); + } + } + } + Ok(false) + } + fn set_planks(&self, c: &mut PieceContext<'_>, x: i32, y: i32, z: i32) -> Result<()> { + if self.frame.is_interior(c, x, y, z)? { + let pos = self.frame.world_pos(x, y, z); + if !c.world.face_sturdy(pos, Direction::Up)? { + c.world.set(pos, c.state(self.wood.planks), 2)?; + } + } + Ok(()) + } + fn air_box(&self, c: &mut PieceContext<'_>, a: [i32; 3], b: [i32; 3]) -> Result<()> { + let air = c.state("cave_air"); + self.frame.generate_box(c, a, b, air, air, false) + } + fn room(&self, c: &mut PieceContext<'_>, entrances: &[BoundingBox]) -> Result<()> { + let b = self.frame.bounding_box; + self.air_box( + c, + [b.min[0], b.min[1] + 1, b.min[2]], + [b.max[0], (b.min[1] + 3).min(b.max[1]), b.max[2]], + )?; + for e in entrances { + self.air_box(c, [e.min[0], e.max[1] - 2, e.min[2]], e.max)?; + } + let air = c.state("cave_air"); + self.frame + .upper_half_sphere(c, [b.min[0], b.min[1] + 4, b.min[2]], b.max, air, false) + } + fn crossing(&self, c: &mut PieceContext<'_>, two_floored: bool) -> Result<()> { + let ([x0, y0, z0], [x1, y1, z1]) = + (self.frame.bounding_box.min, self.frame.bounding_box.max); + if two_floored { + self.air_box(c, [x0 + 1, y0, z0], [x1 - 1, y0 + 2, z1])?; + self.air_box(c, [x0, y0, z0 + 1], [x1, y0 + 2, z1 - 1])?; + self.air_box(c, [x0 + 1, y1 - 2, z0], [x1 - 1, y1, z1])?; + self.air_box(c, [x0, y1 - 2, z0 + 1], [x1, y1, z1 - 1])?; + self.air_box(c, [x0 + 1, y0 + 3, z0 + 1], [x1 - 1, y0 + 3, z1 - 1])?; + } else { + self.air_box(c, [x0 + 1, y0, z0], [x1 - 1, y1, z1])?; + self.air_box(c, [x0, y0, z0 + 1], [x1, y1, z1 - 1])?; + } + let planks = c.state(self.wood.planks); + let air = c.state("cave_air"); + for (x, z) in [ + (x0 + 1, z0 + 1), + (x0 + 1, z1 - 1), + (x1 - 1, z0 + 1), + (x1 - 1, z1 - 1), + ] { + let above = self.frame.get(c, x, y1 + 1, z)?; + if !c.is_air(above) { + self.frame + .generate_box(c, [x, y0, z], [x, y1, z], planks, air, false)?; + } + } + for x in x0..=x1 { + for z in z0..=z1 { + self.set_planks(c, x, y0 - 1, z)?; + } + } + Ok(()) + } + fn stairs(&self, c: &mut PieceContext<'_>) -> Result<()> { + self.air_box(c, [0, 5, 0], [2, 7, 1])?; + self.air_box(c, [0, 0, 7], [2, 2, 8])?; + for i in 0..5 { + self.air_box(c, [0, 5 - i - i32::from(i < 4), 2 + i], [2, 7 - i, 2 + i])?; + } + Ok(()) + } + #[allow(clippy::too_many_arguments)] + fn corridor( + &self, + c: &mut PieceContext<'_>, + rails: bool, + spider: bool, + sections: i32, + placed_spider: &Mutex, + ) -> Result<()> { + let f = self.frame; + let length = sections * 5 - 1; + let air = c.state("cave_air"); + let web = c.state("cobweb"); + f.generate_box(c, [0, 0, 0], [2, 1, length], air, air, false)?; + f.generate_maybe_box(c, 0.8, [0, 2, 0], [2, 2, length], air, air, false, false)?; + if spider { + f.generate_maybe_box(c, 0.6, [0, 0, 0], [2, 1, length], web, air, false, true)?; + } + for section in 0..sections { + let z = 2 + section * 5; + self.support(c, z)?; + for (p, x, dz) in [ + (0.1, 0, -1), + (0.1, 2, -1), + (0.1, 0, 1), + (0.1, 2, 1), + (0.05, 0, -2), + (0.05, 2, -2), + (0.05, 0, 2), + (0.05, 2, 2), + ] { + if f.is_interior(c, x, 2, z + dz)? + && c.random.float() < p + && self.sturdy_neighbours(c, [x, 2, z + dz])? + { + f.place(c, web, x, 2, z + dz)?; + } + } + if c.random.int(100) == 0 { + self.minecart_chest(c, 2, 0, z - 1)?; + } + if c.random.int(100) == 0 { + self.minecart_chest(c, 0, 0, z + 1)?; + } + if spider && !*placed_spider.lock().unwrap() { + let nz = z - 1 + c.random.int(3); + let pos = f.world_pos(1, 0, nz); + if c.chunk_box.is_inside(pos) && f.is_interior(c, 1, 0, nz)? { + *placed_spider.lock().unwrap() = true; + c.spawner(pos, "minecraft:cave_spider")?; + } + } + } + for x in 0..=2 { + for z in 0..=length { + self.set_planks(c, x, -1, z)?; + } + } + self.double_support(c, 2)?; + if sections > 1 { + self.double_support(c, length - 2)?; + } + if rails { + let rail = c.state_with("rail", &[("shape", "north_south")]); + for z in 0..=length { + let floor = f.get(c, 1, -1, z)?; + if !c.is_air(floor) && solid_render(c, floor) { + let p = if f.is_interior(c, 1, 0, z)? { 0.7 } else { 0.9 }; + f.maybe_block(c, p, 1, 0, z, rail)?; + } + } + } + Ok(()) + } + /// MineShaftCorridor.createChest: a rail with a chest minecart. + fn minecart_chest(&self, c: &mut PieceContext<'_>, x: i32, y: i32, z: i32) -> Result<()> { + let pos = self.frame.world_pos(x, y, z); + if !c.chunk_box.is_inside(pos) { + return Ok(()); + } + let here = c.world.block(pos)?; + let below = c.world.block([pos[0], pos[1] - 1, pos[2]])?; + if c.is_air(here) && !c.is_air(below) { + let shape = if c.random.boolean() { + "north_south" + } else { + "east_west" + }; + let rail = c.state_with("rail", &[("shape", shape)]); + self.frame.place(c, rail, x, y, z)?; + // The chest minecart entity (loot table seed drawn here) is not + // stored: worldgen output carries no entities. + c.random.long(); + } + Ok(()) + } + /// MineShaftCorridor.placeSupport(x0 0, y0 0, z, y1 2, x1 2). + fn support(&self, c: &mut PieceContext<'_>, z: i32) -> Result<()> { + let f = self.frame; + for x in 0..=2 { + let above = f.get(c, x, 3, z)?; + if c.is_air(above) { + return Ok(()); + } + } + let air = c.state("cave_air"); + let planks = c.state(self.wood.planks); + let west = c.state_with(self.wood.fence, &[("west", "true")]); + let east = c.state_with(self.wood.fence, &[("east", "true")]); + f.generate_box(c, [0, 0, z], [0, 1, z], west, air, false)?; + f.generate_box(c, [2, 0, z], [2, 1, z], east, air, false)?; + if c.random.int(4) == 0 { + f.generate_box(c, [0, 2, z], [0, 2, z], planks, air, false)?; + f.generate_box(c, [2, 2, z], [2, 2, z], planks, air, false)?; + } else { + f.generate_box(c, [0, 2, z], [2, 2, z], planks, air, false)?; + let south = c.state_with("wall_torch", &[("facing", "south")]); + let north = c.state_with("wall_torch", &[("facing", "north")]); + f.maybe_block(c, 0.05, 1, 2, z - 1, south)?; + f.maybe_block(c, 0.05, 1, 2, z + 1, north)?; + } + Ok(()) + } + fn sturdy_neighbours(&self, c: &mut PieceContext<'_>, [x, y, z]: [i32; 3]) -> Result { + let pos = self.frame.world_pos(x, y, z); + let mut count = 0; + for d in Direction::ALL { + let n = super::legacy::offset(pos, d, 1); + if c.chunk_box.is_inside(n) && c.world.face_sturdy(n, d.opposite())? { + count += 1; + if count >= 2 { + return Ok(true); + } + } + } + Ok(false) + } + fn double_support(&self, c: &mut PieceContext<'_>, z: i32) -> Result<()> { + for x in [0, 2] { + let s = self.frame.get(c, x, -1, z)?; + if c.name(s) == self.wood.planks { + self.pillar_or_chain(c, x, -1, z)?; + } + } + Ok(()) + } + /// MineShaftCorridor.fillPillarDownOrChainUp. + fn pillar_or_chain(&self, c: &mut PieceContext<'_>, x: i32, y: i32, z: i32) -> Result<()> { + let [px, wy, pz] = self.frame.world_pos(x, y, z); + if !c.chunk_box.is_inside([px, wy, pz]) { + return Ok(()); + } + let (min, max) = (c.world.min_y()?, c.world.max_y()?); + let (mut below, mut above) = (true, true); + let mut distance = 1; + while below || above { + if below { + let p = [px, wy - distance, pz]; + let s = c.world.block(p)?; + let name = c.name(s).to_owned(); + let empty = replaceable(&name) && name != "minecraft:lava"; + if !empty && c.world.face_sturdy(p, Direction::Up)? { + let log = c.state(self.wood.log); + for py in wy - distance + 1..wy { + c.world.set([px, py, pz], log, 2)?; + } + return Ok(()); + } + below = distance <= 20 && empty && p[1] > min + 1; + } + if above { + let p = [px, wy + distance, pz]; + let s = c.world.block(p)?; + let name = c.name(s).to_owned(); + let empty = replaceable(&name); + if !empty && c.world.center_sturdy(p, Direction::Down)? && !falling(&name) { + c.world.set([px, wy + 1, pz], c.state(self.wood.fence), 2)?; + let chain = c.state("iron_chain"); + for py in wy + 2..wy + distance { + c.world.set([px, py, pz], chain, 2)?; + } + return Ok(()); + } + above = distance <= 50 && empty && p[1] < max; + } + distance += 1; + } + Ok(()) + } +} +impl LegacyPiece for MineshaftPiece { + fn bounding_box(&self) -> BoundingBox { + self.frame.bounding_box + } + fn type_name(&self) -> &'static str { + match self.kind { + Kind::Room(_) => "minecraft:msroom", + Kind::Corridor { .. } => "minecraft:mscorridor", + Kind::Crossing { .. } => "minecraft:mscrossing", + Kind::Stairs => "minecraft:msstairs", + } + } + fn post_process(&self, c: &mut PieceContext<'_>) -> Result<()> { + if self.invalid(c)? { + return Ok(()); + } + match &self.kind { + Kind::Room(entrances) => self.room(c, entrances), + Kind::Crossing { two_floored, .. } => self.crossing(c, *two_floored), + Kind::Stairs => self.stairs(c), + Kind::Corridor { + rails, + spider, + sections, + placed_spider, + } => self.corridor(c, *rails, *spider, *sections, placed_spider), + } + } +} diff --git a/crates/horizon-world/src/worldgen/structure/start.rs b/crates/horizon-world/src/worldgen/structure/start.rs index 32c838a54..797b2f1ac 100644 --- a/crates/horizon-world/src/worldgen/structure/start.rs +++ b/crates/horizon-world/src/worldgen/structure/start.rs @@ -70,6 +70,12 @@ pub enum StructureKind { large: f32, cluster: f32, }, + Mineshaft { + mesa: bool, + /// #minecraft:mineshaft_blocking + blocking: Arc>, + }, + Stronghold, /// A structure type whose generator is not implemented yet. Unsupported(String), } @@ -95,9 +101,16 @@ enum Placement { separation: i32, triangular: bool, }, - ConcentricRings, + ConcentricRings { + distance: i32, + spread: i32, + count: i32, + preferred: BTreeSet, + }, Unsupported, } +/// Concentric ring chunk positions by structure set name. +pub type Rings = BTreeMap>; #[derive(Debug)] pub struct StructureSet { pub name: String, @@ -183,6 +196,13 @@ impl StructureRegistry { large: v["large_probability"].as_f64().unwrap_or(0.) as f32, cluster: v["cluster_probability"].as_f64().unwrap_or(0.) as f32, }, + "minecraft:mineshaft" => StructureKind::Mineshaft { + mesa: v["mineshaft_type"].as_str() == Some("mesa"), + blocking: Arc::new(biome_set(&Value::String( + "#minecraft:mineshaft_blocking".into(), + ))?), + }, + "minecraft:stronghold" => StructureKind::Stronghold, other => StructureKind::Unsupported(other.to_owned()), }; defs.push(StructureDef { @@ -207,7 +227,12 @@ impl StructureRegistry { separation: p["separation"].as_i64().unwrap_or(0) as i32, triangular: p["spread_type"].as_str() == Some("triangular"), }, - "minecraft:concentric_rings" => Placement::ConcentricRings, + "minecraft:concentric_rings" => Placement::ConcentricRings { + distance: p["distance"].as_i64().unwrap_or(0) as i32, + spread: p["spread"].as_i64().unwrap_or(0) as i32, + count: p["count"].as_i64().unwrap_or(0) as i32, + preferred: biome_set(&p["preferred_biomes"])?, + }, _ => Placement::Unsupported, }; out.push(StructureSet { @@ -246,6 +271,32 @@ impl StructureRegistry { pools: Pools::new(pools), }) } + /// ChunkGeneratorStructureState ring positions of the concentric-ring + /// sets among `possible_sets`. + pub fn ring_positions( + &self, + possible_sets: &[usize], + seed: i64, + biome: &mut dyn FnMut([i32; 3]) -> BiomeId, + ) -> Rings { + let mut rings = Rings::new(); + for &i in possible_sets { + let set = &self.sets[i]; + if let Placement::ConcentricRings { + distance, + spread, + count, + preferred, + } = &set.placement + { + let positions = super::stronghold::ring_positions( + seed, *distance, *spread, *count, preferred, biome, + ); + rings.insert(set.name.clone(), positions); + } + } + rings + } /// RandomSpreadStructurePlacement.getPotentialStructureChunk et al. pub fn is_structure_chunk( &self, @@ -253,7 +304,7 @@ impl StructureRegistry { seed: i64, x: i32, z: i32, - rings: &[ChunkPos], + rings: &Rings, ) -> bool { let placed = match set.placement { Placement::RandomSpread { @@ -278,7 +329,9 @@ impl StructureRegistry { let sz = spread(&mut random); gx * spacing + sx == x && gz * spacing + sz == z } - Placement::ConcentricRings => rings.contains(&ChunkPos::new(x, z)), + Placement::ConcentricRings { .. } => rings + .get(&set.name) + .is_some_and(|r| r.contains(&ChunkPos::new(x, z))), Placement::Unsupported => false, }; if !placed { @@ -399,6 +452,7 @@ pub trait StartContext: JigsawContext { /// Noise biome at a quart position (caching resolver, no zoom). fn noise_biome(&mut self, quart: [i32; 3]) -> BiomeId; fn set_valid_biomes(&mut self, biomes: &BTreeSet); + fn sea_level(&self) -> i32; } /// ChunkGenerator.createStructures for one chunk: the starts it owns. @@ -408,7 +462,7 @@ pub fn create_starts( bindings: &FeatureBlockBindings<'_>, context: &mut impl StartContext, possible_sets: &[usize], - rings: &[ChunkPos], + rings: &Rings, chunk: ChunkPos, ) -> Vec { let seed = context.seed(); @@ -581,6 +635,25 @@ fn try_generate( )?; (stub, legacy(pieces)) } + StructureKind::Mineshaft { mesa, blocking } => { + let (sea, min) = (context.sea_level(), context.min_y()); + let (position, pieces) = super::mineshaft::generate( + &mut random, + [chunk.x, chunk.z], + *mesa, + blocking.clone(), + sea, + min, + &mut |x, z| context.base_height(x, z, HeightmapType::WorldSurfaceWg), + ); + (position, legacy(pieces)) + } + StructureKind::Stronghold => { + let (sea, min) = (context.sea_level(), context.min_y()); + let pieces = + super::stronghold::generate(&mut random, seed, [chunk.x, chunk.z], sea, min); + ([chunk.x * 16, 0, chunk.z * 16], legacy(pieces)) + } StructureKind::Unsupported(_) => return None, }; let quart = position.map(|v| v >> 2); diff --git a/crates/horizon-world/src/worldgen/structure/stronghold.rs b/crates/horizon-world/src/worldgen/structure/stronghold.rs new file mode 100644 index 000000000..3c612c087 --- /dev/null +++ b/crates/horizon-world/src/worldgen/structure/stronghold.rs @@ -0,0 +1,1060 @@ +//! Strongholds: StrongholdStructure, StrongholdPieces and the concentric +//! ring positions of ChunkGeneratorStructureState. +use super::{ + BoundingBox, + legacy::{LegacyPiece, PieceContext, PieceFrame, Result, make_bounding_box, random_horizontal}, +}; +use crate::{registry::BlockStateId, worldgen::biome::BiomeId}; +use horizon_core::{ + geometry::Direction, + position::ChunkPos, + random::{LegacyRandom, RandomSource, WorldgenRandom}, +}; +use std::{collections::BTreeSet, f64::consts::PI, sync::Mutex}; + +/// ChunkGeneratorStructureState.generateRingPositions with +/// BiomeSource.findBiomeHorizontal (radius 112, step 1, not closest). +pub fn ring_positions( + seed: i64, + distance: i32, + spread: i32, + count: i32, + preferred: &BTreeSet, + biome: &mut dyn FnMut([i32; 3]) -> BiomeId, +) -> Vec { + let mut random = LegacyRandom::new(seed); + let mut angle = random.next_double() * PI * 2.0; + let (mut in_circle, mut circle, mut spread) = (0, 0, spread); + let mut out = Vec::with_capacity(count.max(0) as usize); + for i in 0..count { + let dist = f64::from(4 * distance + distance * circle * 6) + + (random.next_double() - 0.5) * (f64::from(distance) * 2.5); + let round = |v: f64| (v + 0.5).floor() as i64 as i32; + let (x, z) = (round(angle.cos() * dist), round(angle.sin() * dist)); + let mut search = random.fork(); + let (cx, cz) = ((x * 16 + 8) >> 2, (z * 16 + 8) >> 2); + let mut found = 0; + let mut result = None; + for dz in -28..=28 { + for dx in -28..=28 { + if preferred.contains(&biome([cx + dx, 0, cz + dz])) { + if result.is_none() || search.next_int_bound(found + 1).unwrap_or(1) == 0 { + result = Some(((cx + dx) << 2, (cz + dz) << 2)); + } + found += 1; + } + } + } + out.push(match result { + Some((bx, bz)) => ChunkPos::new(bx >> 4, bz >> 4), + None => ChunkPos::new(x, z), + }); + angle += PI * 2.0 / f64::from(spread); + in_circle += 1; + if in_circle == spread { + circle += 1; + in_circle = 0; + spread += 2 * spread / (circle + 1); + spread = spread.min(count - i); + angle += random.next_double() * PI * 2.0; + } + } + out +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Door { + Opening, + Wood, + Grates, + Iron, +} +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Kind { + Straight, + PrisonHall, + LeftTurn, + RightTurn, + RoomCrossing, + StraightStairsDown, + StairsDown, + FiveCrossing, + ChestCorridor, + Library, + PortalRoom, + Filler, + Start, +} +/// STRONGHOLD_PIECE_WEIGHTS: kind, weight, max place count. +const WEIGHTS: [(Kind, i32, i32); 11] = [ + (Kind::Straight, 40, 0), + (Kind::PrisonHall, 5, 5), + (Kind::LeftTurn, 20, 0), + (Kind::RightTurn, 20, 0), + (Kind::RoomCrossing, 10, 6), + (Kind::StraightStairsDown, 5, 5), + (Kind::StairsDown, 5, 5), + (Kind::FiveCrossing, 5, 4), + (Kind::ChestCorridor, 5, 4), + (Kind::Library, 10, 2), + (Kind::PortalRoom, 20, 1), +]; + +#[derive(Debug)] +pub struct StrongholdPiece { + kind: Kind, + frame: PieceFrame, + depth: i32, + door: Door, + /// Straight: left, right; FiveCrossing: leftLow, leftHigh, rightLow, + /// rightHigh; Library: tall. + flags: [bool; 4], + /// RoomCrossing type or FillerCorridor steps. + value: i32, + /// ChestCorridor chest / PortalRoom spawner placed. + placed: Mutex, +} + +fn int(r: &mut impl RandomSource, bound: i32) -> i32 { + r.next_int_bound(bound).unwrap_or(0) +} +/// BoundingBox.orientBox. +#[allow(clippy::too_many_arguments)] +fn orient_box( + [x, y, z]: [i32; 3], + [ox, oy, oz]: [i32; 3], + [w, h, d]: [i32; 3], + direction: Direction, +) -> BoundingBox { + match direction { + Direction::North => BoundingBox::new( + [x + ox, y + oy, z - d + 1 + oz], + [x + w - 1 + ox, y + h - 1 + oy, z + oz], + ), + Direction::West => BoundingBox::new( + [x - d + 1 + oz, y + oy, z + ox], + [x + oz, y + h - 1 + oy, z + w - 1 + ox], + ), + Direction::East => BoundingBox::new( + [x + oz, y + oy, z + ox], + [x + d - 1 + oz, y + h - 1 + oy, z + w - 1 + ox], + ), + _ => BoundingBox::new( + [x + ox, y + oy, z + oz], + [x + w - 1 + ox, y + h - 1 + oy, z + d - 1 + oz], + ), + } +} +fn random_door(r: &mut impl RandomSource) -> Door { + match int(r, 5) { + 2 => Door::Wood, + 3 => Door::Grates, + 4 => Door::Iron, + _ => Door::Opening, + } +} + +struct Builder { + pieces: Vec, + weights: Vec<(Kind, i32, i32, i32)>, + imposed: Option, + previous: Option, + pending: Vec, + portal: bool, +} +impl Builder { + fn collides(&self, b: &BoundingBox) -> bool { + self.pieces + .iter() + .any(|p| p.frame.bounding_box.intersects(b)) + } + fn piece(kind: Kind, b: BoundingBox, d: Direction, depth: i32) -> StrongholdPiece { + StrongholdPiece { + kind, + frame: PieceFrame::new(b, Some(d)), + depth, + door: Door::Opening, + flags: [false; 4], + value: 0, + placed: Mutex::new(false), + } + } + /// StrongholdPieces.findAndCreatePieceFactory. + fn create( + &self, + kind: Kind, + r: &mut impl RandomSource, + foot: [i32; 3], + d: Direction, + depth: i32, + ) -> Option { + let ok = |b: &BoundingBox| b.min[1] > 10 && !self.collides(b); + let (offset, size) = match kind { + Kind::Straight | Kind::ChestCorridor => ([-1, -1, 0], [5, 5, 7]), + Kind::PrisonHall => ([-1, -1, 0], [9, 5, 11]), + Kind::LeftTurn | Kind::RightTurn => ([-1, -1, 0], [5, 5, 5]), + Kind::RoomCrossing => ([-4, -1, 0], [11, 7, 11]), + Kind::StraightStairsDown => ([-1, -7, 0], [5, 11, 8]), + Kind::StairsDown => ([-1, -7, 0], [5, 11, 5]), + Kind::FiveCrossing => ([-4, -3, 0], [10, 9, 11]), + Kind::Library => ([-4, -1, 0], [14, 11, 15]), + Kind::PortalRoom => ([-4, -1, 0], [11, 8, 16]), + Kind::Filler | Kind::Start => return None, + }; + let mut b = orient_box(foot, offset, size, d); + if !ok(&b) { + if kind != Kind::Library { + return None; + } + b = orient_box(foot, offset, [14, 6, 15], d); + if !ok(&b) { + return None; + } + } + let mut piece = Self::piece(kind, b, d, depth); + if kind != Kind::PortalRoom { + piece.door = random_door(r); + } + match kind { + Kind::Straight => { + piece.flags[0] = int(r, 2) == 0; + piece.flags[1] = int(r, 2) == 0; + } + Kind::RoomCrossing => piece.value = int(r, 5), + Kind::FiveCrossing => { + piece.flags[0] = r.next_boolean(); + piece.flags[1] = r.next_boolean(); + piece.flags[2] = r.next_boolean(); + piece.flags[3] = int(r, 3) > 0; + } + Kind::Library => piece.flags[0] = b.span()[1] > 6, + _ => {} + } + Some(piece) + } + /// StrongholdPieces.generatePieceFromSmallDoor. + fn from_small_door( + &mut self, + r: &mut impl RandomSource, + foot: [i32; 3], + d: Direction, + depth: i32, + ) -> Option { + let any = self.weights.iter().any(|w| w.2 > 0 && w.3 < w.2); + let total: i32 = self.weights.iter().map(|w| w.1).sum(); + if !any { + return None; + } + if let Some(kind) = self.imposed.take() { + if let Some(piece) = self.create(kind, r, foot, d, depth) { + return Some(piece); + } + } + for _ in 0..5 { + let mut selection = int(r, total); + for i in 0..self.weights.len() { + let (kind, weight, max, count) = self.weights[i]; + selection -= weight; + if selection < 0 { + let allowed = (max == 0 || count < max) + && match kind { + Kind::Library => depth > 4, + Kind::PortalRoom => depth > 5, + _ => true, + }; + if !allowed || self.previous == Some(kind) { + break; + } + if let Some(piece) = self.create(kind, r, foot, d, depth) { + self.weights[i].3 += 1; + self.previous = Some(kind); + if max != 0 && count + 1 >= max { + self.weights.remove(i); + } + return Some(piece); + } + } + } + } + let b = orient_box(foot, [-1, -1, 0], [5, 5, 4], d); + let collision = self + .pieces + .iter() + .find(|p| p.frame.bounding_box.intersects(&b))? + .frame + .bounding_box; + if collision.min[1] != b.min[1] { + return None; + } + for length in [2, 1] { + if !collision.intersects(&orient_box(foot, [-1, -1, 0], [5, 5, length], d)) { + let b = orient_box(foot, [-1, -1, 0], [5, 5, length + 1], d); + if b.min[1] <= 1 { + return None; + } + let mut piece = Self::piece(Kind::Filler, b, d, depth); + piece.value = b.span()[if matches!(d, Direction::North | Direction::South) { + 2 + } else { + 0 + }]; + return Some(piece); + } + } + None + } + /// StrongholdPieces.generateAndAddPiece. + fn add(&mut self, r: &mut impl RandomSource, foot: [i32; 3], d: Direction, depth: i32) { + if depth > 50 { + return; + } + let start = self.pieces[0].frame.bounding_box; + if (foot[0] - start.min[0]).abs() > 112 || (foot[2] - start.min[2]).abs() > 112 { + return; + } + if let Some(piece) = self.from_small_door(r, foot, d, depth + 1) { + self.pieces.push(piece); + self.pending.push(self.pieces.len() - 1); + } + } + fn forward(&mut self, r: &mut impl RandomSource, i: usize, x_off: i32, y_off: i32) { + let p = &self.pieces[i]; + let (b, depth) = (p.frame.bounding_box, p.depth); + let Some(o) = p.frame.orientation else { return }; + let y = b.min[1] + y_off; + let foot = match o { + Direction::North => [b.min[0] + x_off, y, b.min[2] - 1], + Direction::South => [b.min[0] + x_off, y, b.max[2] + 1], + Direction::West => [b.min[0] - 1, y, b.min[2] + x_off], + Direction::East => [b.max[0] + 1, y, b.min[2] + x_off], + _ => return, + }; + self.add(r, foot, o, depth); + } + fn side(&mut self, r: &mut impl RandomSource, i: usize, right: bool, y_off: i32, z_off: i32) { + let p = &self.pieces[i]; + let (b, depth) = (p.frame.bounding_box, p.depth); + let Some(o) = p.frame.orientation else { return }; + let y = b.min[1] + y_off; + let (foot, d) = match (o, right) { + (Direction::North | Direction::South, false) => { + ([b.min[0] - 1, y, b.min[2] + z_off], Direction::West) + } + (Direction::North | Direction::South, true) => { + ([b.max[0] + 1, y, b.min[2] + z_off], Direction::East) + } + (Direction::West | Direction::East, false) => { + ([b.min[0] + z_off, y, b.min[2] - 1], Direction::North) + } + (Direction::West | Direction::East, true) => { + ([b.min[0] + z_off, y, b.max[2] + 1], Direction::South) + } + _ => return, + }; + self.add(r, foot, d, depth); + } + fn add_children(&mut self, r: &mut impl RandomSource, i: usize) { + let p = &self.pieces[i]; + let (kind, flags, o) = (p.kind, p.flags, p.frame.orientation); + let north_or_east = matches!(o, Some(Direction::North | Direction::East)); + match kind { + Kind::Start | Kind::StairsDown => { + if kind == Kind::Start { + self.imposed = Some(Kind::FiveCrossing); + } + self.forward(r, i, 1, 1); + } + Kind::ChestCorridor | Kind::PrisonHall | Kind::StraightStairsDown => { + self.forward(r, i, 1, 1) + } + Kind::Straight => { + self.forward(r, i, 1, 1); + if flags[0] { + self.side(r, i, false, 1, 2); + } + if flags[1] { + self.side(r, i, true, 1, 2); + } + } + Kind::FiveCrossing => { + let (a, b) = if matches!(o, Some(Direction::West | Direction::North)) { + (5, 3) + } else { + (3, 5) + }; + self.forward(r, i, 5, 1); + if flags[0] { + self.side(r, i, false, a, 1); + } + if flags[1] { + self.side(r, i, false, b, 7); + } + if flags[2] { + self.side(r, i, true, a, 1); + } + if flags[3] { + self.side(r, i, true, b, 7); + } + } + Kind::LeftTurn => self.side(r, i, !north_or_east, 1, 1), + Kind::RightTurn => self.side(r, i, north_or_east, 1, 1), + Kind::RoomCrossing => { + self.forward(r, i, 4, 1); + self.side(r, i, false, 1, 4); + self.side(r, i, true, 1, 4); + } + Kind::PortalRoom => self.portal = true, + Kind::Library | Kind::Filler => {} + } + } +} + +/// StrongholdStructure.generatePieces: retried with seed + tries until a +/// portal room is placed. +pub fn generate( + random: &mut WorldgenRandom, + seed: i64, + chunk: [i32; 2], + sea_level: i32, + min_y: i32, +) -> Vec { + let mut tries = 0i64; + loop { + random.set_large_feature_seed(seed.wrapping_add(tries), chunk[0], chunk[1]); + tries += 1; + let d = random_horizontal(random); + let b = make_bounding_box(chunk[0] * 16 + 2, 64, chunk[1] * 16 + 2, d, 5, 11, 5); + let mut g = Builder { + pieces: vec![Builder::piece(Kind::Start, b, d, 0)], + weights: WEIGHTS.iter().map(|&(k, w, m)| (k, w, m, 0)).collect(), + imposed: None, + previous: None, + pending: Vec::new(), + portal: false, + }; + g.add_children(random, 0); + while !g.pending.is_empty() { + let index = int(random, g.pending.len() as i32) as usize; + let next = g.pending.remove(index); + g.add_children(random, next); + } + let mut all = g.pieces[0].frame.bounding_box; + for p in &g.pieces[1..] { + all.encapsulate(&p.frame.bounding_box); + } + let max = sea_level - 10; + let mut y1 = all.span()[1] + min_y + 1; + if y1 < max { + y1 += int(random, max - y1); + } + let dy = y1 - all.max[1]; + for p in &mut g.pieces { + p.frame.bounding_box = p.frame.bounding_box.moved([0, dy, 0]); + } + if g.portal { + return g.pieces; + } + } +} + +fn smooth(c: &mut PieceContext<'_>, edge: bool) -> BlockStateId { + if !edge { + return c.state("cave_air"); + } + let s = c.random.float(); + c.state(if s < 0.2 { + "cracked_stone_bricks" + } else if s < 0.5 { + "mossy_stone_bricks" + } else if s < 0.55 { + "infested_stone_bricks" + } else { + "stone_bricks" + }) +} +impl StrongholdPiece { + fn smooth_box( + &self, + c: &mut PieceContext<'_>, + a: [i32; 3], + b: [i32; 3], + skip_air: bool, + ) -> Result<()> { + self.frame + .generate_box_selector(c, a, b, skip_air, &mut |c, _, _, _, edge| smooth(c, edge)) + } + fn fill( + &self, + c: &mut PieceContext<'_>, + a: [i32; 3], + b: [i32; 3], + state: BlockStateId, + ) -> Result<()> { + self.frame.generate_box(c, a, b, state, state, false) + } + fn air(&self, c: &mut PieceContext<'_>, a: [i32; 3], b: [i32; 3]) -> Result<()> { + let air = c.state("cave_air"); + self.fill(c, a, b, air) + } + fn put( + &self, + c: &mut PieceContext<'_>, + state: BlockStateId, + x: i32, + y: i32, + z: i32, + ) -> Result<()> { + self.frame.place(c, state, x, y, z) + } + fn small_door( + &self, + c: &mut PieceContext<'_>, + door: Door, + x: i32, + y: i32, + z: i32, + ) -> Result<()> { + let bricks = c.state("stone_bricks"); + let frame = [(0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (2, 1), (2, 0)]; + match door { + Door::Opening => self.air(c, [x, y, z], [x + 2, y + 2, z])?, + Door::Wood | Door::Iron => { + for (dx, dy) in frame { + self.put(c, bricks, x + dx, y + dy, z)?; + } + let block = if door == Door::Wood { + "oak_door" + } else { + "iron_door" + }; + self.put(c, c.state(block), x + 1, y, z)?; + self.put( + c, + c.state_with(block, &[("half", "upper")]), + x + 1, + y + 1, + z, + )?; + if door == Door::Iron { + self.put( + c, + c.state_with("stone_button", &[("facing", "north")]), + x + 2, + y + 1, + z + 1, + )?; + self.put( + c, + c.state_with("stone_button", &[("facing", "south")]), + x + 2, + y + 1, + z - 1, + )?; + } + } + Door::Grates => { + let air = c.state("cave_air"); + let west = c.state_with("iron_bars", &[("west", "true")]); + let east = c.state_with("iron_bars", &[("east", "true")]); + let both = c.state_with("iron_bars", &[("east", "true"), ("west", "true")]); + self.put(c, air, x + 1, y, z)?; + self.put(c, air, x + 1, y + 1, z)?; + self.put(c, west, x, y, z)?; + self.put(c, west, x, y + 1, z)?; + self.put(c, both, x, y + 2, z)?; + self.put(c, both, x + 1, y + 2, z)?; + self.put(c, both, x + 2, y + 2, z)?; + self.put(c, east, x + 2, y + 1, z)?; + self.put(c, east, x + 2, y, z)?; + } + } + Ok(()) + } + fn torch(c: &PieceContext<'_>, facing: &str) -> BlockStateId { + c.state_with("wall_torch", &[("facing", facing)]) + } + fn chest_corridor(&self, c: &mut PieceContext<'_>) -> Result<()> { + self.smooth_box(c, [0, 0, 0], [4, 4, 6], true)?; + self.small_door(c, self.door, 1, 1, 0)?; + self.small_door(c, Door::Opening, 1, 1, 6)?; + let bricks = c.state("stone_bricks"); + self.fill(c, [3, 1, 2], [3, 1, 4], bricks)?; + let slab = c.state("stone_brick_slab"); + for (x, y, z) in [ + (3, 1, 1), + (3, 1, 5), + (3, 2, 2), + (3, 2, 4), + (2, 1, 2), + (2, 1, 3), + (2, 1, 4), + ] { + self.put(c, slab, x, y, z)?; + } + if !*self.placed.lock().unwrap() && c.chunk_box.is_inside(self.frame.world_pos(3, 2, 3)) { + *self.placed.lock().unwrap() = true; + self.frame + .create_chest(c, 3, 2, 3, "minecraft:chests/stronghold_corridor")?; + } + Ok(()) + } + fn filler(&self, c: &mut PieceContext<'_>) -> Result<()> { + let bricks = c.state("stone_bricks"); + let air = c.state("cave_air"); + for i in 0..self.value { + for x in 0..=4 { + self.put(c, bricks, x, 0, i)?; + } + for y in 1..=3 { + self.put(c, bricks, 0, y, i)?; + for x in 1..=3 { + self.put(c, air, x, y, i)?; + } + self.put(c, bricks, 4, y, i)?; + } + for x in 0..=4 { + self.put(c, bricks, x, 4, i)?; + } + } + Ok(()) + } + fn five_crossing(&self, c: &mut PieceContext<'_>) -> Result<()> { + let [left_low, left_high, right_low, right_high] = self.flags; + self.smooth_box(c, [0, 0, 0], [9, 8, 10], true)?; + self.small_door(c, self.door, 4, 3, 0)?; + if left_low { + self.air(c, [0, 3, 1], [0, 5, 3])?; + } + if right_low { + self.air(c, [9, 3, 1], [9, 5, 3])?; + } + if left_high { + self.air(c, [0, 5, 7], [0, 7, 9])?; + } + if right_high { + self.air(c, [9, 5, 7], [9, 7, 9])?; + } + self.air(c, [5, 1, 10], [7, 3, 10])?; + self.smooth_box(c, [1, 2, 1], [8, 2, 6], false)?; + self.smooth_box(c, [4, 1, 5], [4, 4, 9], false)?; + self.smooth_box(c, [8, 1, 5], [8, 4, 9], false)?; + self.smooth_box(c, [1, 4, 7], [3, 4, 9], false)?; + self.smooth_box(c, [1, 3, 5], [3, 3, 6], false)?; + let slab = c.state("smooth_stone_slab"); + self.fill(c, [1, 3, 4], [3, 3, 4], slab)?; + self.fill(c, [1, 4, 6], [3, 4, 6], slab)?; + self.smooth_box(c, [5, 1, 7], [7, 1, 8], false)?; + self.fill(c, [5, 1, 9], [7, 1, 9], slab)?; + self.fill(c, [5, 2, 7], [7, 2, 7], slab)?; + self.fill(c, [4, 5, 7], [4, 5, 9], slab)?; + self.fill(c, [8, 5, 7], [8, 5, 9], slab)?; + let double = c.state_with("smooth_stone_slab", &[("type", "double")]); + self.fill(c, [5, 5, 7], [7, 5, 9], double)?; + self.put(c, Self::torch(c, "south"), 6, 5, 6) + } + fn turn(&self, c: &mut PieceContext<'_>, left: bool) -> Result<()> { + self.smooth_box(c, [0, 0, 0], [4, 4, 4], true)?; + self.small_door(c, self.door, 1, 1, 0)?; + let north_or_east = matches!( + self.frame.orientation, + Some(Direction::North | Direction::East) + ); + if north_or_east == left { + self.air(c, [0, 1, 1], [0, 3, 3]) + } else { + self.air(c, [4, 1, 1], [4, 3, 3]) + } + } + fn library(&self, c: &mut PieceContext<'_>) -> Result<()> { + let tall = self.flags[0]; + let height = if tall { 11 } else { 6 }; + self.smooth_box(c, [0, 0, 0], [13, height - 1, 14], true)?; + self.small_door(c, self.door, 4, 1, 0)?; + let web = c.state("cobweb"); + self.frame + .generate_maybe_box(c, 0.07, [2, 1, 1], [11, 4, 13], web, web, false, false)?; + let planks = c.state("oak_planks"); + let shelf = c.state("bookshelf"); + for d in 1..=13 { + let block = if (d - 1) % 4 == 0 { planks } else { shelf }; + self.fill(c, [1, 1, d], [1, 4, d], block)?; + self.fill(c, [12, 1, d], [12, 4, d], block)?; + if block == planks { + self.put(c, Self::torch(c, "east"), 2, 3, d)?; + self.put(c, Self::torch(c, "west"), 11, 3, d)?; + } + if tall { + self.fill(c, [1, 6, d], [1, 9, d], block)?; + self.fill(c, [12, 6, d], [12, 9, d], block)?; + } + } + for d in (3..12).step_by(2) { + self.fill(c, [3, 1, d], [4, 3, d], shelf)?; + self.fill(c, [6, 1, d], [7, 3, d], shelf)?; + self.fill(c, [9, 1, d], [10, 3, d], shelf)?; + } + if tall { + self.fill(c, [1, 5, 1], [3, 5, 13], planks)?; + self.fill(c, [10, 5, 1], [12, 5, 13], planks)?; + self.fill(c, [4, 5, 1], [9, 5, 2], planks)?; + self.fill(c, [4, 5, 12], [9, 5, 13], planks)?; + self.put(c, planks, 9, 5, 11)?; + self.put(c, planks, 8, 5, 11)?; + self.put(c, planks, 9, 5, 10)?; + let fence = |c: &PieceContext<'_>, sides: &[&str]| { + let props: Vec<(&str, &str)> = sides.iter().map(|s| (*s, "true")).collect(); + c.state_with("oak_fence", &props) + }; + let we = fence(c, &["west", "east"]); + let ns = fence(c, &["north", "south"]); + self.fill(c, [3, 6, 3], [3, 6, 11], ns)?; + self.fill(c, [10, 6, 3], [10, 6, 9], ns)?; + self.fill(c, [4, 6, 2], [9, 6, 2], we)?; + self.fill(c, [4, 6, 12], [7, 6, 12], we)?; + self.put(c, fence(c, &["north", "east"]), 3, 6, 2)?; + self.put(c, fence(c, &["south", "east"]), 3, 6, 12)?; + self.put(c, fence(c, &["north", "west"]), 10, 6, 2)?; + for i in 0..=2 { + self.put(c, fence(c, &["south", "west"]), 8 + i, 6, 12 - i)?; + if i != 2 { + self.put(c, fence(c, &["north", "east"]), 8 + i, 6, 11 - i)?; + } + } + let ladder = c.state_with("ladder", &[("facing", "south")]); + for y in 1..=7 { + self.put(c, ladder, 10, y, 13)?; + } + let e = fence(c, &["east"]); + let w = fence(c, &["west"]); + self.put(c, e, 6, 9, 7)?; + self.put(c, w, 7, 9, 7)?; + self.put(c, e, 6, 8, 7)?; + self.put(c, w, 7, 8, 7)?; + let all = fence(c, &["north", "south", "west", "east"]); + self.put(c, all, 6, 7, 7)?; + self.put(c, all, 7, 7, 7)?; + self.put(c, e, 5, 7, 7)?; + self.put(c, w, 8, 7, 7)?; + self.put(c, fence(c, &["east", "north"]), 6, 7, 6)?; + self.put(c, fence(c, &["east", "south"]), 6, 7, 8)?; + self.put(c, fence(c, &["west", "north"]), 7, 7, 6)?; + self.put(c, fence(c, &["west", "south"]), 7, 7, 8)?; + let torch = c.state("torch"); + for (x, z) in [(5, 7), (8, 7), (6, 6), (6, 8), (7, 6), (7, 8)] { + self.put(c, torch, x, 8, z)?; + } + } + self.frame + .create_chest(c, 3, 3, 5, "minecraft:chests/stronghold_library")?; + if tall { + self.put(c, c.state("cave_air"), 12, 9, 1)?; + self.frame + .create_chest(c, 12, 8, 1, "minecraft:chests/stronghold_library")?; + } + Ok(()) + } + fn portal_room(&self, c: &mut PieceContext<'_>) -> Result<()> { + self.smooth_box(c, [0, 0, 0], [10, 7, 15], false)?; + self.small_door(c, Door::Grates, 4, 1, 0)?; + for (a, b) in [ + ([1, 6, 1], [1, 6, 14]), + ([9, 6, 1], [9, 6, 14]), + ([2, 6, 1], [8, 6, 2]), + ([2, 6, 14], [8, 6, 14]), + ([1, 1, 1], [2, 1, 4]), + ([8, 1, 1], [9, 1, 4]), + ] { + self.smooth_box(c, a, b, false)?; + } + let lava = c.state("lava"); + self.fill(c, [1, 1, 1], [1, 1, 3], lava)?; + self.fill(c, [9, 1, 1], [9, 1, 3], lava)?; + self.smooth_box(c, [3, 1, 8], [7, 1, 12], false)?; + self.fill(c, [4, 1, 9], [6, 1, 11], lava)?; + let ns = c.state_with("iron_bars", &[("north", "true"), ("south", "true")]); + let we = c.state_with("iron_bars", &[("west", "true"), ("east", "true")]); + for z in (3..14).step_by(2) { + self.fill(c, [0, 3, z], [0, 4, z], ns)?; + self.fill(c, [10, 3, z], [10, 4, z], ns)?; + } + for x in (2..9).step_by(2) { + self.fill(c, [x, 3, 15], [x, 4, 15], we)?; + } + let stairs = c.state_with("stone_brick_stairs", &[("facing", "north")]); + self.smooth_box(c, [4, 1, 5], [6, 1, 7], false)?; + self.smooth_box(c, [4, 2, 6], [6, 2, 7], false)?; + self.smooth_box(c, [4, 3, 7], [6, 3, 7], false)?; + for x in 4..=6 { + self.put(c, stairs, x, 1, 4)?; + self.put(c, stairs, x, 2, 5)?; + self.put(c, stairs, x, 3, 6)?; + } + let mut eyes = [false; 12]; + for e in &mut eyes { + *e = c.random.float() > 0.9; + } + let frames = [ + ("north", 4, 8), + ("north", 5, 8), + ("north", 6, 8), + ("south", 4, 12), + ("south", 5, 12), + ("south", 6, 12), + ("east", 3, 9), + ("east", 3, 10), + ("east", 3, 11), + ("west", 7, 9), + ("west", 7, 10), + ("west", 7, 11), + ]; + for (i, (facing, x, z)) in frames.into_iter().enumerate() { + let eye = if eyes[i] { "true" } else { "false" }; + let state = c.state_with("end_portal_frame", &[("facing", facing), ("eye", eye)]); + self.put(c, state, x, 3, z)?; + } + if eyes.iter().all(|e| *e) { + let portal = c.state("end_portal"); + for z in 9..=11 { + for x in 4..=6 { + self.put(c, portal, x, 3, z)?; + } + } + } + if !*self.placed.lock().unwrap() { + let pos = self.frame.world_pos(5, 3, 6); + if c.chunk_box.is_inside(pos) { + *self.placed.lock().unwrap() = true; + c.spawner(pos, "minecraft:silverfish")?; + } + } + Ok(()) + } + fn prison_hall(&self, c: &mut PieceContext<'_>) -> Result<()> { + self.smooth_box(c, [0, 0, 0], [8, 4, 10], true)?; + self.small_door(c, self.door, 1, 1, 0)?; + self.air(c, [1, 1, 10], [3, 3, 10])?; + for z in [1, 3, 7, 9] { + self.smooth_box(c, [4, 1, z], [4, 3, z], false)?; + } + let ns = c.state_with("iron_bars", &[("north", "true"), ("south", "true")]); + let nse = c.state_with( + "iron_bars", + &[("north", "true"), ("south", "true"), ("east", "true")], + ); + let we = c.state_with("iron_bars", &[("west", "true"), ("east", "true")]); + for y in 1..=3 { + self.put(c, ns, 4, y, 4)?; + self.put(c, nse, 4, y, 5)?; + self.put(c, ns, 4, y, 6)?; + self.put(c, we, 5, y, 5)?; + self.put(c, we, 6, y, 5)?; + self.put(c, we, 7, y, 5)?; + } + self.put(c, ns, 4, 3, 2)?; + self.put(c, ns, 4, 3, 8)?; + let bottom = c.state_with("iron_door", &[("facing", "west")]); + let top = c.state_with("iron_door", &[("facing", "west"), ("half", "upper")]); + for z in [2, 8] { + self.put(c, bottom, 4, 1, z)?; + self.put(c, top, 4, 2, z)?; + } + Ok(()) + } + fn room_crossing(&self, c: &mut PieceContext<'_>) -> Result<()> { + self.smooth_box(c, [0, 0, 0], [10, 6, 10], true)?; + self.small_door(c, self.door, 4, 1, 0)?; + self.air(c, [4, 1, 10], [6, 3, 10])?; + self.air(c, [0, 1, 4], [0, 3, 6])?; + self.air(c, [10, 1, 4], [10, 3, 6])?; + let bricks = c.state("stone_bricks"); + match self.value { + 0 => { + for y in 1..=3 { + self.put(c, bricks, 5, y, 5)?; + } + self.put(c, Self::torch(c, "west"), 4, 3, 5)?; + self.put(c, Self::torch(c, "east"), 6, 3, 5)?; + self.put(c, Self::torch(c, "south"), 5, 3, 4)?; + self.put(c, Self::torch(c, "north"), 5, 3, 6)?; + let slab = c.state("smooth_stone_slab"); + for (x, z) in [ + (4, 4), + (4, 5), + (4, 6), + (6, 4), + (6, 5), + (6, 6), + (5, 4), + (5, 6), + ] { + self.put(c, slab, x, 1, z)?; + } + } + 1 => { + for i in 0..5 { + self.put(c, bricks, 3, 1, 3 + i)?; + self.put(c, bricks, 7, 1, 3 + i)?; + self.put(c, bricks, 3 + i, 1, 3)?; + self.put(c, bricks, 3 + i, 1, 7)?; + } + for y in 1..=3 { + self.put(c, bricks, 5, y, 5)?; + } + self.put(c, c.state("water"), 5, 4, 5)?; + } + 2 => { + let cobble = c.state("cobblestone"); + for z in 1..=9 { + self.put(c, cobble, 1, 3, z)?; + self.put(c, cobble, 9, 3, z)?; + } + for x in 1..=9 { + self.put(c, cobble, x, 3, 1)?; + self.put(c, cobble, x, 3, 9)?; + } + for (x, y, z) in [ + (5, 1, 4), + (5, 1, 6), + (5, 3, 4), + (5, 3, 6), + (4, 1, 5), + (6, 1, 5), + (4, 3, 5), + (6, 3, 5), + ] { + self.put(c, cobble, x, y, z)?; + } + for y in 1..=3 { + self.put(c, cobble, 4, y, 4)?; + self.put(c, cobble, 6, y, 4)?; + self.put(c, cobble, 4, y, 6)?; + self.put(c, cobble, 6, y, 6)?; + } + self.put(c, c.state("wall_torch"), 5, 3, 5)?; + let planks = c.state("oak_planks"); + for z in 2..=8 { + self.put(c, planks, 2, 3, z)?; + self.put(c, planks, 3, 3, z)?; + if z <= 3 || z >= 7 { + self.put(c, planks, 4, 3, z)?; + self.put(c, planks, 5, 3, z)?; + self.put(c, planks, 6, 3, z)?; + } + self.put(c, planks, 7, 3, z)?; + self.put(c, planks, 8, 3, z)?; + } + let ladder = c.state_with("ladder", &[("facing", "west")]); + for y in 1..=3 { + self.put(c, ladder, 9, y, 3)?; + } + self.frame + .create_chest(c, 3, 4, 8, "minecraft:chests/stronghold_crossing")?; + } + _ => {} + } + Ok(()) + } + fn stairs_down(&self, c: &mut PieceContext<'_>) -> Result<()> { + self.smooth_box(c, [0, 0, 0], [4, 10, 4], true)?; + self.small_door(c, self.door, 1, 7, 0)?; + self.small_door(c, Door::Opening, 1, 1, 4)?; + let bricks = c.state("stone_bricks"); + let slab = c.state("smooth_stone_slab"); + for (state, x, y, z) in [ + (bricks, 2, 6, 1), + (bricks, 1, 5, 1), + (slab, 1, 6, 1), + (bricks, 1, 5, 2), + (bricks, 1, 4, 3), + (slab, 1, 5, 3), + (bricks, 2, 4, 3), + (bricks, 3, 3, 3), + (slab, 3, 4, 3), + (bricks, 3, 3, 2), + (bricks, 3, 2, 1), + (slab, 3, 3, 1), + (bricks, 2, 2, 1), + (bricks, 1, 1, 1), + (slab, 1, 2, 1), + (bricks, 1, 1, 2), + (slab, 1, 1, 3), + ] { + self.put(c, state, x, y, z)?; + } + Ok(()) + } + fn straight(&self, c: &mut PieceContext<'_>) -> Result<()> { + self.smooth_box(c, [0, 0, 0], [4, 4, 6], true)?; + self.small_door(c, self.door, 1, 1, 0)?; + self.small_door(c, Door::Opening, 1, 1, 6)?; + let east = Self::torch(c, "east"); + let west = Self::torch(c, "west"); + self.frame.maybe_block(c, 0.1, 1, 2, 1, east)?; + self.frame.maybe_block(c, 0.1, 3, 2, 1, west)?; + self.frame.maybe_block(c, 0.1, 1, 2, 5, east)?; + self.frame.maybe_block(c, 0.1, 3, 2, 5, west)?; + if self.flags[0] { + self.air(c, [0, 1, 2], [0, 3, 4])?; + } + if self.flags[1] { + self.air(c, [4, 1, 2], [4, 3, 4])?; + } + Ok(()) + } + fn straight_stairs_down(&self, c: &mut PieceContext<'_>) -> Result<()> { + self.smooth_box(c, [0, 0, 0], [4, 10, 7], true)?; + self.small_door(c, self.door, 1, 7, 0)?; + self.small_door(c, Door::Opening, 1, 1, 7)?; + let stairs = c.state_with("cobblestone_stairs", &[("facing", "south")]); + let bricks = c.state("stone_bricks"); + for i in 0..6 { + for x in 1..=3 { + self.put(c, stairs, x, 6 - i, 1 + i)?; + } + if i < 5 { + for x in 1..=3 { + self.put(c, bricks, x, 5 - i, 1 + i)?; + } + } + } + Ok(()) + } +} +impl LegacyPiece for StrongholdPiece { + fn bounding_box(&self) -> BoundingBox { + self.frame.bounding_box + } + fn type_name(&self) -> &'static str { + match self.kind { + Kind::Straight => "minecraft:shs", + Kind::PrisonHall => "minecraft:shph", + Kind::LeftTurn => "minecraft:shlt", + Kind::RightTurn => "minecraft:shrt", + Kind::RoomCrossing => "minecraft:shrc", + Kind::StraightStairsDown => "minecraft:shssd", + Kind::StairsDown => "minecraft:shsd", + Kind::FiveCrossing => "minecraft:sh5c", + Kind::ChestCorridor => "minecraft:shcc", + Kind::Library => "minecraft:shli", + Kind::PortalRoom => "minecraft:shpr", + Kind::Filler => "minecraft:shfc", + Kind::Start => "minecraft:shstart", + } + } + fn post_process(&self, c: &mut PieceContext<'_>) -> Result<()> { + match self.kind { + Kind::Straight => self.straight(c), + Kind::PrisonHall => self.prison_hall(c), + Kind::LeftTurn => self.turn(c, true), + Kind::RightTurn => self.turn(c, false), + Kind::RoomCrossing => self.room_crossing(c), + Kind::StraightStairsDown => self.straight_stairs_down(c), + Kind::StairsDown | Kind::Start => self.stairs_down(c), + Kind::FiveCrossing => self.five_crossing(c), + Kind::ChestCorridor => self.chest_corridor(c), + Kind::Library => self.library(c), + Kind::PortalRoom => self.portal_room(c), + Kind::Filler => self.filler(c), + } + } +} diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaStructureStartOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaStructureStartOracle.java index c2a226a3c..87e2a4466 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaStructureStartOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaStructureStartOracle.java @@ -58,6 +58,9 @@ public final class VanillaStructureStartOracle { var structures=world.lookupOrThrow(Registries.STRUCTURE); try(var w=new PrintWriter(Files.newBufferedWriter(out,StandardCharsets.UTF_8))) { structures.listElements().forEach(h->w.println("registry "+h.key().identifier()+" "+h.value().step().ordinal())); + if(System.getenv("HORIZON_RINGS")!=null)for(var set:state.possibleStructureSets()) + if(set.value().placement() instanceof net.minecraft.world.level.levelgen.structure.placement.ConcentricRingsStructurePlacement rings) + for(var p:state.getRingPositionsFor(rings))w.println("ring "+set.unwrapKey().get().identifier()+" "+p.x()+" "+p.z()); if(System.getenv("HORIZON_HEIGHTS")!=null)for(String p:System.getenv("HORIZON_HEIGHTS").split(";")){ String[] xz=p.split(",");int x=Integer.parseInt(xz[0]),z=Integer.parseInt(xz[1]); int floor=generator.getBaseHeight(x,z,Heightmap.Types.OCEAN_FLOOR_WG,height,randomState),surface=generator.getBaseHeight(x,z,Heightmap.Types.WORLD_SURFACE_WG,height,randomState); diff --git a/tools/structure_start_oracle.py b/tools/structure_start_oracle.py index 61ab19c61..dc622ae19 100644 --- a/tools/structure_start_oracle.py +++ b/tools/structure_start_oracle.py @@ -5,6 +5,7 @@ usage: structure_start_oracle.py OUT SEED X0 Z0 X1 Z1 """ import os import subprocess +from pathlib import Path import sys try: from . import lab, test_rust_parity as base @@ -17,7 +18,7 @@ def main(): out, seed, x0, z0, x1, z1 = sys.argv[1:7] lock, jars, libraries = base.verify_artifacts() source = lab.ROOT/'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaStructureStartOracle.java' - build = lab.LOCAL/'structure-start-oracle' + build = Path(os.environ['HORIZON_ORACLE_BUILD']) if os.environ.get('HORIZON_ORACLE_BUILD') else lab.LOCAL/'structure-start-oracle' classes = build/'classes' classes.mkdir(parents=True, exist_ok=True) dependencies = os.pathsep.join(map(str, [jars['server'], *libraries]))