diff --git a/crates/horizon-world/src/worldgen/generator.rs b/crates/horizon-world/src/worldgen/generator.rs index 3a0b76529..bd3f482fd 100644 --- a/crates/horizon-world/src/worldgen/generator.rs +++ b/crates/horizon-world/src/worldgen/generator.rs @@ -1023,9 +1023,33 @@ where /// ChunkGenerator.getBaseHeight: the first free Y above the noise column /// (with aquifers, without terrain adaptation) for a heightmap type. fn base_height(&self, x: i32, z: i32, kind: HeightmapType) -> i32 { + let min = self.settings.min_y; + let mut found = min; + self.noise_column(x, z, &mut |y, state| { + if kind.is_opaque(self.data.states.get(state)) { + found = y + 1; + return true; + } + false + }); + found + } + /// ChunkGenerator.getBaseColumn: opacity of each noise-column state for + /// a heightmap type, indexed from the minimum Y. + fn base_column(&self, x: i32, z: i32, kind: HeightmapType) -> Vec { + let (min, height) = (self.settings.min_y, self.settings.height); + let mut column = vec![false; height as usize]; + self.noise_column(x, z, &mut |y, state| { + column[(y - min) as usize] = kind.is_opaque(self.data.states.get(state)); + false + }); + column + } + /// iterateNoiseColumn from the top down until `visit` returns true. + fn noise_column(&self, x: i32, z: i32, visit: &mut dyn FnMut(i32, BlockStateId) -> bool) { let (min, height) = (self.settings.min_y, self.settings.height); let Ok(volume) = DensityVolume::new([1, height, 1], [x, min, z], [1; 3]) else { - return min; + return; }; let mut context = SamplerContext::cached(); let mut aquifer: Box = match &self.aquifer { @@ -1045,7 +1069,7 @@ where .ok() }) { Some(aquifer) => Box::new(aquifer), - None => return min, + None => return, }, None => Box::new(DisabledAquifer::new( self.picker, @@ -1059,7 +1083,7 @@ where .sample_volume(&mut context, &mut density, volume) .is_err() { - return min; + return; } for y in (0..height).rev() { let state = aquifer @@ -1071,11 +1095,10 @@ where .ok() .flatten() .unwrap_or(self.settings.default_block); - if kind.is_opaque(self.data.states.get(state)) { - return min + y + 1; + if visit(min + y, state) { + return; } } - min } } /// Biome and height queries for structure generation on one worker. @@ -1120,6 +1143,17 @@ where fn seed(&self) -> i64 { self.pipeline.seed } + fn sea_level(&self) -> i32 { + self.pipeline.settings.sea_level + } + fn base_column(&mut self, x: i32, z: i32, kind: HeightmapType) -> Vec { + self.pipeline.base_column(x, z, kind) + } + fn cold_enough_to_snow(&mut self, biome: BiomeId, pos: [i32; 3]) -> bool { + let sea = self.pipeline.settings.sea_level; + self.worker.temperatures[biome.0 as usize] + .cold_enough_to_snow(BlockPos::new(pos[0], pos[1], pos[2]), sea) + } fn generation(&self) -> WorldGenerationContext { self.pipeline.generation() } @@ -1306,7 +1340,7 @@ where self.generation(), worker, region, - || random.next_boolean(), + &mut random, self.structures.as_ref(), ) } diff --git a/crates/horizon-world/src/worldgen/generator_decoration.rs b/crates/horizon-world/src/worldgen/generator_decoration.rs index b2da351c2..f0596cf74 100644 --- a/crates/horizon-world/src/worldgen/generator_decoration.rs +++ b/crates/horizon-world/src/worldgen/generator_decoration.rs @@ -253,7 +253,7 @@ impl Decoration { generation: WorldGenerationContext, worker: &mut WorkerState, region: &mut [ProtoChunk; 9], - next_boolean: impl FnMut() -> bool, + level_random: &mut dyn super::structure::legacy::PieceRandom, structures: Option<&super::generator_structures::StructureState>, ) -> Result { let center = region[4].position(); @@ -294,14 +294,13 @@ impl Decoration { Ok(terrain) => terrain, Err(e) => return Err(resource(e.to_string())), }; - let mut next_boolean = next_boolean; let mut block_entities = Vec::new(); for chunk in region.iter_mut() { block_entities.append(&mut chunk.block_entities); } let mut world = World { block_entities, - next_boolean: &mut next_boolean, + level_random, blocks: data .feature_resources .as_ref() @@ -417,7 +416,7 @@ impl Decoration { /// the FEATURES step's write radius, biome lookups through the biome zoom. struct World<'a> { block_entities: Vec<([i32; 3], crate::nbt::Compound)>, - next_boolean: &'a mut dyn FnMut() -> bool, + level_random: &'a mut dyn super::structure::legacy::PieceRandom, blocks: Option<&'a BlockBehavior>, region: TerrainRegion<'static, GenBlockStates>, biomes: BiomeRegion, @@ -539,7 +538,13 @@ impl PlacementContext for World<'_> { .map_err(query) } fn next_world_boolean(&mut self) -> Result { - Ok((self.next_boolean)()) + Ok(self.level_random.boolean()) + } + fn next_world_float(&mut self) -> Result { + Ok(self.level_random.float()) + } + fn next_world_int(&mut self, bound: i32) -> Result { + Ok(self.level_random.int(bound)) } fn block_survives( &mut self, diff --git a/crates/horizon-world/src/worldgen/placement/context.rs b/crates/horizon-world/src/worldgen/placement/context.rs index f64dadf94..5bd83815c 100644 --- a/crates/horizon-world/src/worldgen/placement/context.rs +++ b/crates/horizon-world/src/worldgen/placement/context.rs @@ -202,6 +202,12 @@ pub trait PlacementContext { fn next_world_boolean(&mut self) -> Result { Err(PlacementQueryError::Unavailable) } + fn next_world_float(&mut self) -> Result { + Err(PlacementQueryError::Unavailable) + } + fn next_world_int(&mut self, _bound: i32) -> Result { + Err(PlacementQueryError::Unavailable) + } /// Schedule the block type identified by its registry default state. fn schedule_block_tick( &mut self, diff --git a/crates/horizon-world/src/worldgen/structure.rs b/crates/horizon-world/src/worldgen/structure.rs index c5146d62a..1f6a9f097 100644 --- a/crates/horizon-world/src/worldgen/structure.rs +++ b/crates/horizon-world/src/worldgen/structure.rs @@ -2,12 +2,16 @@ //! and structure pieces. pub mod features; pub mod jigsaw; +pub mod mansion; +pub mod monument; pub mod legacy; pub mod ocean_ruin; pub mod processor; +pub mod ruined_portal; pub mod simple; pub mod start; pub mod template; +pub mod temple; use crate::{ nbt::{self, Limits}, diff --git a/crates/horizon-world/src/worldgen/structure/legacy.rs b/crates/horizon-world/src/worldgen/structure/legacy.rs index d78b89b68..c2e049840 100644 --- a/crates/horizon-world/src/worldgen/structure/legacy.rs +++ b/crates/horizon-world/src/worldgen/structure/legacy.rs @@ -48,6 +48,15 @@ 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<()>; + /// WorldGenLevel.getSeed. + fn seed(&mut self) -> Result; + /// Draws from WorldGenLevel.getRandom (the region random). + fn level_boolean(&mut self) -> Result; + fn level_float(&mut self) -> Result; + fn level_int(&mut self, bound: i32) -> Result; + fn sea_level(&mut self) -> Result; + /// Block.isFaceFull of the block's shape at `pos` on face `direction`. + fn face_full(&mut self, pos: [i32; 3], direction: Direction) -> Result; fn place_template( &mut self, resources: &StructureResources, @@ -85,6 +94,24 @@ impl PieceWorld for W { fn post_process(&mut self, pos: [i32; 3]) -> Result<()> { self.mark_postprocessing(pos) } + fn seed(&mut self) -> Result { + self.world_seed() + } + fn level_boolean(&mut self) -> Result { + self.next_world_boolean() + } + fn level_float(&mut self) -> Result { + self.next_world_float() + } + fn level_int(&mut self, bound: i32) -> Result { + self.next_world_int(bound) + } + fn sea_level(&mut self) -> Result { + PlacementContext::sea_level(self) + } + fn face_full(&mut self, pos: [i32; 3], direction: Direction) -> Result { + self.attachment_at(offset(pos, direction, 1), opposite(direction)) + } fn place_template( &mut self, resources: &StructureResources, @@ -204,6 +231,11 @@ pub trait LegacyPiece: std::fmt::Debug + Send + Sync { /// The vanilla StructurePieceType id, for diagnostics and comparisons. fn type_name(&self) -> &'static str; fn post_process(&self, context: &mut PieceContext<'_>) -> Result<()>; + /// Structure.afterPlace, run on the first piece once per chunk after all + /// pieces were placed, with every piece's current box. + fn after_place(&self, _context: &mut PieceContext<'_>, _pieces: &[BoundingBox]) -> Result<()> { + Ok(()) + } } /// StructurePiece's orientation transform and block helpers. @@ -674,6 +706,8 @@ pub enum TemplateHandler { large: bool, integrity: f32, }, + RuinedPortal(super::ruined_portal::PortalProps), + Mansion, Plain, } impl TemplatePiece { diff --git a/crates/horizon-world/src/worldgen/structure/mansion.rs b/crates/horizon-world/src/worldgen/structure/mansion.rs new file mode 100644 index 000000000..3d8f8a498 --- /dev/null +++ b/crates/horizon-world/src/worldgen/structure/mansion.rs @@ -0,0 +1,879 @@ +//! Woodland mansions (WoodlandMansionStructure, WoodlandMansionPieces). +use super::{ + BoundingBox, Mirror, Rotation, StructureResources, + legacy::{ + HORIZONTAL, PieceContext, Result, TemplateHandler, TemplatePiece, clockwise, + counter_clockwise, create_chest, direction_name, offset, opposite, + }, + processor::ProcessorList, +}; +use horizon_core::{geometry::Direction, random::RandomSource}; +use std::sync::Arc; + +const CORRIDOR_FLAG: i32 = 8_388_608; +const STAIRS_FLAG: i32 = 4_194_304; +const DOOR_FLAG: i32 = 2_097_152; +const ORIGIN_FLAG: i32 = 1_048_576; +const TYPE_MASK: i32 = 983_040; +const ID_MASK: i32 = 65_535; +const ROOM_1X1: i32 = 65_536; +const ROOM_1X2: i32 = 131_072; +const ROOM_2X2: i32 = 262_144; + +#[derive(Clone)] +struct Grid { + cells: Vec, + width: i32, + height: i32, + outside: i32, +} +impl Grid { + fn new(width: i32, height: i32, outside: i32) -> Self { + Self { + cells: vec![0; (width * height) as usize], + width, + height, + outside, + } + } + fn set(&mut self, x: i32, y: i32, value: i32) { + if x >= 0 && x < self.width && y >= 0 && y < self.height { + self.cells[(x * self.height + y) as usize] = value; + } + } + fn fill(&mut self, x0: i32, y0: i32, x1: i32, y1: i32, value: i32) { + for y in y0..=y1 { + for x in x0..=x1 { + self.set(x, y, value); + } + } + } + fn get(&self, x: i32, y: i32) -> i32 { + if x >= 0 && x < self.width && y >= 0 && y < self.height { + self.cells[(x * self.height + y) as usize] + } else { + self.outside + } + } + fn set_if(&mut self, x: i32, y: i32, if_value: i32, value: i32) { + if self.get(x, y) == if_value { + self.set(x, y, value); + } + } + fn edges_to(&self, x: i32, y: i32, value: i32) -> bool { + self.get(x - 1, y) == value + || self.get(x + 1, y) == value + || self.get(x, y + 1) == value + || self.get(x, y - 1) == value + } +} +fn is_house(grid: &Grid, x: i32, y: i32) -> bool { + matches!(grid.get(x, y), 1..=4) +} +fn step(d: Direction) -> (i32, i32) { + let n = d.normal(); + (n[0], n[2]) +} +fn from_2d(value: i32) -> Direction { + [ + Direction::South, + Direction::West, + Direction::North, + Direction::East, + ][value as usize & 3] +} +fn shuffle(list: &mut [T], random: &mut impl RandomSource) { + for i in (2..=list.len()).rev() { + let j = random.next_int_bound(i as i32).unwrap_or(0) as usize; + list.swap(i - 1, j); + } +} + +struct MansionGrid { + base: Grid, + third: Grid, + rooms: [Grid; 3], + entrance_x: i32, + entrance_y: i32, +} +impl MansionGrid { + fn new(r: &mut impl RandomSource) -> Self { + let (ex, ey) = (7, 4); + let mut base = Grid::new(11, 11, 5); + base.fill(ex, ey, ex + 1, ey + 1, 3); + base.fill(ex - 1, ey, ex - 1, ey + 1, 2); + base.fill(ex + 2, ey - 2, ex + 3, ey + 3, 5); + base.fill(ex + 1, ey - 2, ex + 1, ey - 1, 1); + base.fill(ex + 1, ey + 2, ex + 1, ey + 3, 1); + base.set(ex - 1, ey - 1, 1); + base.set(ex - 1, ey + 2, 1); + base.fill(0, 0, 11, 1, 5); + base.fill(0, 9, 11, 11, 5); + corridor(&mut base, r, ex, ey - 2, Direction::West, 6); + corridor(&mut base, r, ex, ey + 3, Direction::West, 6); + corridor(&mut base, r, ex - 2, ey - 1, Direction::West, 3); + corridor(&mut base, r, ex - 2, ey + 2, Direction::West, 3); + while clean_edges(&mut base) {} + let mut rooms = [Grid::new(11, 11, 5), Grid::new(11, 11, 5), Grid::new(11, 11, 5)]; + identify_rooms(&base, &mut rooms[0], r); + identify_rooms(&base, &mut rooms[1], r); + rooms[0].fill(ex + 1, ey, ex + 1, ey + 1, CORRIDOR_FLAG); + rooms[1].fill(ex + 1, ey, ex + 1, ey + 1, CORRIDOR_FLAG); + let mut grid = Self { + third: Grid::new(base.width, base.height, 5), + base, + rooms, + entrance_x: ex, + entrance_y: ey, + }; + grid.setup_third_floor(r); + let mut third_rooms = std::mem::replace(&mut grid.rooms[2], Grid::new(0, 0, 0)); + identify_rooms(&grid.third, &mut third_rooms, r); + grid.rooms[2] = third_rooms; + grid + } + fn is_room_id(&self, x: i32, y: i32, floor: usize, id: i32) -> bool { + (self.rooms[floor].get(x, y) & ID_MASK) == id + } + fn room_direction_1x2(&self, x: i32, y: i32, floor: usize, id: i32) -> Option { + HORIZONTAL.into_iter().find(|&d| { + let (dx, dz) = step(d); + self.is_room_id(x + dx, y + dz, floor, id) + }) + } + fn setup_third_floor(&mut self, r: &mut impl RandomSource) { + let mut potential = Vec::new(); + for y in 0..self.third.height { + for x in 0..self.third.width { + let data = self.rooms[1].get(x, y); + if data & TYPE_MASK == ROOM_1X2 && data & DOOR_FLAG == DOOR_FLAG { + potential.push((x, y)); + } + } + } + let (w, h) = (self.third.width, self.third.height); + if potential.is_empty() { + self.third.fill(0, 0, w, h, 5); + return; + } + let (rx, ry) = potential[r.next_int_bound(potential.len() as i32).unwrap_or(0) as usize]; + let data = self.rooms[1].get(rx, ry); + self.rooms[1].set(rx, ry, data | STAIRS_FLAG); + let Some(dir) = self.room_direction_1x2(rx, ry, 1, data & ID_MASK) else { + return; + }; + let (dx, dz) = step(dir); + let (ex, ey) = (rx + dx, ry + dz); + for y in 0..h { + for x in 0..w { + if !is_house(&self.base, x, y) { + self.third.set(x, y, 5); + } else if x == rx && y == ry { + self.third.set(x, y, 3); + } else if x == ex && y == ey { + self.third.set(x, y, 3); + self.rooms[2].set(x, y, CORRIDOR_FLAG); + } + } + } + let corridors: Vec = HORIZONTAL + .into_iter() + .filter(|&d| { + let (dx, dz) = step(d); + self.third.get(ex + dx, ey + dz) == 0 + }) + .collect(); + if corridors.is_empty() { + self.third.fill(0, 0, w, h, 5); + self.rooms[1].set(rx, ry, data); + } else { + let d = corridors[r.next_int_bound(corridors.len() as i32).unwrap_or(0) as usize]; + let (dx, dz) = step(d); + corridor(&mut self.third, r, ex + dx, ey + dz, d, 4); + while clean_edges(&mut self.third) {} + } + } +} +fn corridor(g: &mut Grid, r: &mut impl RandomSource, x: i32, y: i32, heading: Direction, depth: i32) { + if depth <= 0 { + return; + } + let (hx, hz) = step(heading); + g.set(x, y, 1); + g.set_if(x + hx, y + hz, 0, 1); + for _ in 0..8 { + let next = from_2d(r.next_int_bound(4).unwrap_or(0)); + if next != opposite(heading) && (next != Direction::East || !r.next_boolean()) { + let (nx, ny) = (x + hx, y + hz); + let (dx, dz) = step(next); + if g.get(nx + dx, ny + dz) == 0 && g.get(nx + dx * 2, ny + dz * 2) == 0 { + corridor(g, r, x + hx + dx, y + hz + dz, next, depth - 1); + break; + } + } + } + let (cx, cz) = step(clockwise(heading)); + let (ax, az) = step(counter_clockwise(heading)); + g.set_if(x + cx, y + cz, 0, 2); + g.set_if(x + ax, y + az, 0, 2); + g.set_if(x + hx + cx, y + hz + cz, 0, 2); + g.set_if(x + hx + ax, y + hz + az, 0, 2); + g.set_if(x + hx * 2, y + hz * 2, 0, 2); + g.set_if(x + cx * 2, y + cz * 2, 0, 2); + g.set_if(x + ax * 2, y + az * 2, 0, 2); +} +fn clean_edges(g: &mut Grid) -> bool { + let mut touched = false; + for y in 0..g.height { + for x in 0..g.width { + if g.get(x, y) != 0 { + continue; + } + let house = |g: &Grid, x, y| i32::from(is_house(g, x, y)); + let direct = house(g, x + 1, y) + house(g, x - 1, y) + house(g, x, y + 1) + house(g, x, y - 1); + if direct >= 3 { + g.set(x, y, 2); + touched = true; + } else if direct == 2 { + let diagonal = house(g, x + 1, y + 1) + + house(g, x - 1, y + 1) + + house(g, x + 1, y - 1) + + house(g, x - 1, y - 1); + if diagonal <= 1 { + g.set(x, y, 2); + touched = true; + } + } + } + } + touched +} +fn identify_rooms(from: &Grid, rooms: &mut Grid, r: &mut impl RandomSource) { + let mut positions = Vec::new(); + for y in 0..from.height { + for x in 0..from.width { + if from.get(x, y) == 2 { + positions.push((x, y)); + } + } + } + shuffle(&mut positions, r); + let mut id = 10; + for (x, y) in positions { + if rooms.get(x, y) != 0 { + continue; + } + let (mut x0, mut x1, mut y0, mut y1) = (x, x, y, y); + let mut kind = ROOM_1X1; + let free = |dx: i32, dy: i32| rooms.get(x + dx, y + dy) == 0 && from.get(x + dx, y + dy) == 2; + if free(1, 0) && free(0, 1) && free(1, 1) { + x1 += 1; + y1 += 1; + kind = ROOM_2X2; + } else if free(-1, 0) && free(0, 1) && free(-1, 1) { + x0 -= 1; + y1 += 1; + kind = ROOM_2X2; + } else if free(-1, 0) && free(0, -1) && free(-1, -1) { + x0 -= 1; + y0 -= 1; + kind = ROOM_2X2; + } else if free(1, 0) { + x1 += 1; + kind = ROOM_1X2; + } else if free(0, 1) { + y1 += 1; + kind = ROOM_1X2; + } else if free(-1, 0) { + x0 -= 1; + kind = ROOM_1X2; + } else if free(0, -1) { + y0 -= 1; + kind = ROOM_1X2; + } + let mut door_x = if r.next_boolean() { x0 } else { x1 }; + let mut door_y = if r.next_boolean() { y0 } else { y1 }; + let mut door = DOOR_FLAG; + let swap = |v: i32, a: i32, b: i32| if v == a { b } else { a }; + if !from.edges_to(door_x, door_y, 1) { + door_x = swap(door_x, x0, x1); + door_y = swap(door_y, y0, y1); + if !from.edges_to(door_x, door_y, 1) { + door_y = swap(door_y, y0, y1); + if !from.edges_to(door_x, door_y, 1) { + door_x = swap(door_x, x0, x1); + door_y = swap(door_y, y0, y1); + if !from.edges_to(door_x, door_y, 1) { + door = 0; + door_x = x0; + door_y = y0; + } + } + } + } + for ry in y0..=y1 { + for rx in x0..=x1 { + if rx == door_x && ry == door_y { + rooms.set(rx, ry, ORIGIN_FLAG | door | kind | id); + } else { + rooms.set(rx, ry, kind | id); + } + } + } + id += 1; + } +} + +struct Piece { + name: String, + position: [i32; 3], + rotation: Rotation, + mirror: Mirror, +} +struct Placement { + position: [i32; 3], + rotation: Rotation, + wall: &'static str, +} +fn up(p: [i32; 3], n: i32) -> [i32; 3] { + [p[0], p[1] + n, p[2]] +} +fn rel(p: [i32; 3], rotation: Rotation, d: Direction, n: i32) -> [i32; 3] { + offset(p, rotation.rotate_direction(d), n) +} +/// One floor's room-template choices (First/Second/ThirdFloorRoomCollection). +fn room_name(floor: usize, kind: &str, r: &mut impl RandomSource, stairs: bool) -> String { + let mut n = |b: i32| r.next_int_bound(b).unwrap_or(0) + 1; + if floor == 0 { + match kind { + "1x1" => format!("1x1_a{}", n(5)), + "1x1s" => format!("1x1_as{}", n(4)), + "side" => format!("1x2_a{}", n(9)), + "front" => format!("1x2_b{}", n(5)), + "1x2s" => format!("1x2_s{}", n(2)), + "2x2" => format!("2x2_a{}", n(4)), + _ => "2x2_s1".into(), + } + } else { + match kind { + "1x1" => format!("1x1_b{}", n(5)), + "1x1s" => format!("1x1_as{}", n(4)), + "side" if stairs => "1x2_c_stairs".into(), + "side" => format!("1x2_c{}", n(4)), + "front" if stairs => "1x2_d_stairs".into(), + "front" => format!("1x2_d{}", n(5)), + "1x2s" => format!("1x2_se{}", n(1)), + "2x2" => format!("2x2_b{}", n(5)), + _ => "2x2_s1".into(), + } + } +} +struct Placer<'a, R: RandomSource> { + random: &'a mut R, + pieces: Vec, + start_x: i32, + start_y: i32, +} +impl Placer<'_, R> { + fn add(&mut self, name: impl Into, position: [i32; 3], rotation: Rotation, mirror: Mirror) { + self.pieces.push(Piece { + name: name.into(), + position, + rotation, + mirror, + }); + } + fn grid_pos(&self, origin: [i32; 3], rotation: Rotation, x: i32, y: i32, east: i32) -> [i32; 3] { + let p = rel(origin, rotation, Direction::South, 8 + (y - self.start_y) * 8); + rel(p, rotation, Direction::East, east + (x - self.start_x) * 8) + } + fn create(&mut self, origin: [i32; 3], rotation: Rotation, mansion: &MansionGrid) { + let mut data = Placement { + position: origin, + rotation, + wall: "wall_flat", + }; + let west = rotation.rotate_direction(Direction::West); + self.add("entrance", offset(data.position, west, 9), rotation, Mirror::None); + data.position = rel(data.position, rotation, Direction::South, 16); + let mut second = Placement { + position: up(data.position, 8), + rotation: data.rotation, + wall: "wall_window", + }; + let (base, third) = (&mansion.base, &mansion.third); + self.start_x = mansion.entrance_x + 1; + self.start_y = mansion.entrance_y + 1; + let (end_x, end_y) = (mansion.entrance_x + 1, mansion.entrance_y); + let (sx, sy) = (self.start_x, self.start_y); + self.outer_walls(&mut data, base, Direction::South, sx, sy, end_x, end_y); + self.outer_walls(&mut second, base, Direction::South, sx, sy, end_x, end_y); + let mut third_data = Placement { + position: up(data.position, 19), + rotation: data.rotation, + wall: "wall_window", + }; + 'done: for y in 0..third.height { + for x in (0..third.width).rev() { + if is_house(third, x, y) { + third_data.position = self.grid_pos(third_data.position, rotation, x, y, 0); + self.wall_piece(&mut third_data); + self.outer_walls(&mut third_data, third, Direction::South, x, y, x, y); + break 'done; + } + } + } + self.roof(up(origin, 16), rotation, base, Some(third)); + self.roof(up(origin, 27), rotation, third, None); + for floor in 0..3 { + let floor_origin = up(origin, 8 * floor as i32 + if floor == 2 { 3 } else { 0 }); + let rooms = &mansion.rooms[floor]; + let grid = if floor == 2 { third } else { base }; + let south_piece = if floor == 0 { "carpet_south_1" } else { "carpet_south_2" }; + let west_piece = if floor == 0 { "carpet_west_1" } else { "carpet_west_2" }; + let corridor = |x, y| grid.get(x, y) == 1 || rooms.get(x, y) & CORRIDOR_FLAG == CORRIDOR_FLAG; + for y in 0..grid.height { + for x in 0..grid.width { + if grid.get(x, y) != 1 { + continue; + } + let pos = self.grid_pos(floor_origin, rotation, x, y, 0); + self.add("corridor_floor", pos, rotation, Mirror::None); + if corridor(x, y - 1) { + let p = up(rel(pos, rotation, Direction::East, 1), 1); + self.add("carpet_north", p, rotation, Mirror::None); + } + if corridor(x + 1, y) { + let p = rel(pos, rotation, Direction::South, 1); + let p = up(rel(p, rotation, Direction::East, 5), 1); + self.add("carpet_east", p, rotation, Mirror::None); + } + if corridor(x, y + 1) { + let p = rel(pos, rotation, Direction::South, 5); + let p = rel(p, rotation, Direction::West, 1); + self.add(south_piece, p, rotation, Mirror::None); + } + if corridor(x - 1, y) { + let p = rel(pos, rotation, Direction::West, 1); + let p = rel(p, rotation, Direction::North, 1); + self.add(west_piece, p, rotation, Mirror::None); + } + } + } + let wall = if floor == 0 { "indoors_wall_1" } else { "indoors_wall_2" }; + let door = if floor == 0 { "indoors_door_1" } else { "indoors_door_2" }; + for y in 0..grid.height { + for x in 0..grid.width { + let mut third_start = floor == 2 && grid.get(x, y) == 3; + if grid.get(x, y) != 2 && !third_start { + continue; + } + let data = rooms.get(x, y); + let kind = data & TYPE_MASK; + let id = data & ID_MASK; + third_start = third_start && data & CORRIDOR_FLAG == CORRIDOR_FLAG; + let mut doors = Vec::new(); + if data & DOOR_FLAG == DOOR_FLAG { + for d in HORIZONTAL { + let (dx, dz) = step(d); + if grid.get(x + dx, y + dz) == 1 { + doors.push(d); + } + } + } + let door_dir = if !doors.is_empty() { + Some(doors[self.random.next_int_bound(doors.len() as i32).unwrap_or(0) as usize]) + } else if data & ORIGIN_FLAG == ORIGIN_FLAG { + Some(Direction::Up) + } else { + None + }; + let room_pos = self.grid_pos(floor_origin, rotation, x, y, -1); + let pick = |d: Direction| if door_dir == Some(d) { door } else { wall }; + if is_house(grid, x - 1, y) && !mansion.is_room_id(x - 1, y, floor, id) { + self.add(pick(Direction::West), room_pos, rotation, Mirror::None); + } + if grid.get(x + 1, y) == 1 && !third_start { + let p = rel(room_pos, rotation, Direction::East, 8); + self.add(pick(Direction::East), p, rotation, Mirror::None); + } + let cw90 = rotation.get_rotated(Rotation::Clockwise90); + if is_house(grid, x, y + 1) && !mansion.is_room_id(x, y + 1, floor, id) { + let p = rel(room_pos, rotation, Direction::South, 7); + let p = rel(p, rotation, Direction::East, 7); + self.add(pick(Direction::South), p, cw90, Mirror::None); + } + if grid.get(x, y - 1) == 1 && !third_start { + let p = rel(room_pos, rotation, Direction::North, 1); + let p = rel(p, rotation, Direction::East, 7); + self.add(pick(Direction::North), p, cw90, Mirror::None); + } + if kind == ROOM_1X1 { + self.room_1x1(room_pos, rotation, door_dir, floor); + } else if kind == ROOM_1X2 && door_dir.is_some() { + let room_dir = mansion.room_direction_1x2(x, y, floor, id); + let stairs = data & STAIRS_FLAG == STAIRS_FLAG; + if let (Some(room_dir), Some(door_dir)) = (room_dir, door_dir) { + self.room_1x2(room_pos, rotation, room_dir, door_dir, floor, stairs); + } + } else if kind == ROOM_2X2 && door_dir.is_some_and(|d| d != Direction::Up) { + let door_dir = door_dir.unwrap(); + let mut room_dir = clockwise(door_dir); + let (dx, dz) = step(room_dir); + if !mansion.is_room_id(x + dx, y + dz, floor, id) { + room_dir = opposite(room_dir); + } + self.room_2x2(room_pos, rotation, room_dir, door_dir, floor); + } else if kind == ROOM_2X2 && door_dir == Some(Direction::Up) { + let name = room_name(floor, "2x2s", self.random, false); + let p = rel(room_pos, rotation, Direction::East, 1); + self.add(name, p, rotation, Mirror::None); + } + } + } + } + } + #[allow(clippy::too_many_arguments)] + fn outer_walls( + &mut self, + data: &mut Placement, + grid: &Grid, + direction: Direction, + start_x: i32, + start_y: i32, + end_x: i32, + end_y: i32, + ) { + let (mut gx, mut gy, mut dir) = (start_x, start_y, direction); + let start_dir = direction; + loop { + let (dx, dz) = step(dir); + if !is_house(grid, gx + dx, gy + dz) { + data.position = rel(data.position, data.rotation, Direction::South, -1); + self.add("wall_corner", data.position, data.rotation, Mirror::None); + data.position = rel(data.position, data.rotation, Direction::South, -7); + data.position = rel(data.position, data.rotation, Direction::West, -6); + data.rotation = data.rotation.get_rotated(Rotation::Clockwise90); + dir = clockwise(dir); + if gx != end_x || gy != end_y || start_dir != dir { + self.wall_piece(data); + } + } else if { + let (cx, cz) = step(counter_clockwise(dir)); + is_house(grid, gx + dx + cx, gy + dz + cz) + } { + data.position = rel(data.position, data.rotation, Direction::South, 6); + data.position = rel(data.position, data.rotation, Direction::East, 8); + data.rotation = data.rotation.get_rotated(Rotation::Counterclockwise90); + gx += dx; + gy += dz; + dir = counter_clockwise(dir); + } else { + gx += dx; + gy += dz; + if gx != end_x || gy != end_y || start_dir != dir { + self.wall_piece(data); + } + } + if gx == end_x && gy == end_y && start_dir == dir { + break; + } + } + } + fn wall_piece(&mut self, data: &mut Placement) { + let p = rel(data.position, data.rotation, Direction::East, 7); + self.add(data.wall, p, data.rotation, Mirror::None); + data.position = rel(data.position, data.rotation, Direction::South, 8); + } + fn roof(&mut self, origin: [i32; 3], rotation: Rotation, grid: &Grid, above: Option<&Grid>) { + use Direction::{East, North, South, West}; + let r = |rot: Rotation| rotation.get_rotated(rot); + let (cw90, cw180, ccw90) = ( + r(Rotation::Clockwise90), + r(Rotation::Clockwise180), + r(Rotation::Counterclockwise90), + ); + let none = Mirror::None; + for y in 0..grid.height { + for x in 0..grid.width { + let pos = self.grid_pos(origin, rotation, x, y, 0); + let is_above = above.is_some_and(|a| is_house(a, x, y)); + if !is_house(grid, x, y) || is_above { + continue; + } + self.add("roof", up(pos, 3), rotation, none); + if !is_house(grid, x + 1, y) { + self.add("roof_front", rel(pos, rotation, East, 6), rotation, none); + } + if !is_house(grid, x - 1, y) { + let p = rel(rel(pos, rotation, East, 0), rotation, South, 7); + self.add("roof_front", p, cw180, none); + } + if !is_house(grid, x, y - 1) { + self.add("roof_front", rel(pos, rotation, West, 1), ccw90, none); + } + if !is_house(grid, x, y + 1) { + let p = rel(rel(pos, rotation, East, 6), rotation, South, 6); + self.add("roof_front", p, cw90, none); + } + } + } + if let Some(above) = above { + for y in 0..grid.height { + for x in 0..grid.width { + let pos = self.grid_pos(origin, rotation, x, y, 0); + if !(is_house(grid, x, y) && is_house(above, x, y)) { + continue; + } + if !is_house(grid, x + 1, y) { + self.add("small_wall", rel(pos, rotation, East, 7), rotation, none); + } + if !is_house(grid, x - 1, y) { + let p = rel(rel(pos, rotation, West, 1), rotation, South, 6); + self.add("small_wall", p, cw180, none); + } + if !is_house(grid, x, y - 1) { + let p = rel(rel(pos, rotation, West, 0), rotation, North, 1); + self.add("small_wall", p, ccw90, none); + } + if !is_house(grid, x, y + 1) { + let p = rel(rel(pos, rotation, East, 6), rotation, South, 7); + self.add("small_wall", p, cw90, none); + } + if !is_house(grid, x + 1, y) { + if !is_house(grid, x, y - 1) { + let p = rel(rel(pos, rotation, East, 7), rotation, North, 2); + self.add("small_wall_corner", p, rotation, none); + } + if !is_house(grid, x, y + 1) { + let p = rel(rel(pos, rotation, East, 8), rotation, South, 7); + self.add("small_wall_corner", p, cw90, none); + } + } + if !is_house(grid, x - 1, y) { + if !is_house(grid, x, y - 1) { + let p = rel(rel(pos, rotation, West, 2), rotation, North, 1); + self.add("small_wall_corner", p, ccw90, none); + } + if !is_house(grid, x, y + 1) { + let p = rel(rel(pos, rotation, West, 1), rotation, South, 8); + self.add("small_wall_corner", p, cw180, none); + } + } + } + } + } + for y in 0..grid.height { + for x in 0..grid.width { + let pos = self.grid_pos(origin, rotation, x, y, 0); + let is_above = above.is_some_and(|a| is_house(a, x, y)); + if !is_house(grid, x, y) || is_above { + continue; + } + if !is_house(grid, x + 1, y) { + let p2 = rel(pos, rotation, East, 6); + if !is_house(grid, x, y + 1) { + self.add("roof_corner", rel(p2, rotation, South, 6), rotation, none); + } else if is_house(grid, x + 1, y + 1) { + self.add("roof_inner_corner", rel(p2, rotation, South, 5), rotation, none); + } + if !is_house(grid, x, y - 1) { + self.add("roof_corner", p2, ccw90, none); + } else if is_house(grid, x + 1, y - 1) { + let p3 = rel(rel(pos, rotation, East, 9), rotation, North, 2); + self.add("roof_inner_corner", p3, cw90, none); + } + } + if !is_house(grid, x - 1, y) { + let p2 = rel(rel(pos, rotation, East, 0), rotation, South, 0); + if !is_house(grid, x, y + 1) { + self.add("roof_corner", rel(p2, rotation, South, 6), cw90, none); + } else if is_house(grid, x - 1, y + 1) { + let p3 = rel(rel(p2, rotation, South, 8), rotation, West, 3); + self.add("roof_inner_corner", p3, ccw90, none); + } + if !is_house(grid, x, y - 1) { + self.add("roof_corner", p2, cw180, none); + } else if is_house(grid, x - 1, y - 1) { + self.add("roof_inner_corner", rel(p2, rotation, South, 1), cw180, none); + } + } + } + } + } + fn room_1x1(&mut self, room_pos: [i32; 3], rotation: Rotation, door: Option, floor: usize) { + let mut piece_rot = Rotation::None; + let mut name = room_name(floor, "1x1", self.random, false); + if door != Some(Direction::East) { + match door { + Some(Direction::North) => piece_rot = Rotation::Counterclockwise90, + Some(Direction::West) => piece_rot = Rotation::Clockwise180, + Some(Direction::South) => piece_rot = Rotation::Clockwise90, + _ => name = room_name(floor, "1x1s", self.random, false), + } + } + let o = match piece_rot { + Rotation::Counterclockwise90 => [1, 0, 6], + Rotation::Clockwise90 => [7, 0, 0], + Rotation::Clockwise180 => [7, 0, 6], + Rotation::None => [1, 0, 0], + }; + let piece_rot = piece_rot.get_rotated(rotation); + let o = match rotation { + Rotation::Clockwise90 => [-o[2], o[1], o[0]], + Rotation::Clockwise180 => [-o[0], o[1], -o[2]], + Rotation::Counterclockwise90 => [o[2], o[1], -o[0]], + Rotation::None => o, + }; + let pos = [room_pos[0] + o[0], room_pos[1], room_pos[2] + o[2]]; + self.add(name, pos, piece_rot, Mirror::None); + } + fn room_1x2( + &mut self, + room_pos: [i32; 3], + rotation: Rotation, + room: Direction, + door: Direction, + floor: usize, + stairs: bool, + ) { + use Direction::{East, North, South, Up, West}; + let r = |rot: Rotation| rotation.get_rotated(rot); + let at = |east: i32, south: i32| { + rel(rel(room_pos, rotation, East, east), rotation, South, south) + }; + let (kind, pos, rot, mirror) = match (door, room) { + (East, South) => ("side", at(1, 0), rotation, Mirror::None), + (East, North) => ("side", at(1, 6), rotation, Mirror::LeftRight), + (West, North) => ("side", at(7, 6), r(Rotation::Clockwise180), Mirror::None), + (West, South) => ("side", at(7, 0), rotation, Mirror::FrontBack), + (South, East) => ("side", at(1, 0), r(Rotation::Clockwise90), Mirror::LeftRight), + (South, West) => ("side", at(7, 0), r(Rotation::Clockwise90), Mirror::None), + (North, West) => ("side", at(7, 6), r(Rotation::Clockwise90), Mirror::FrontBack), + (North, East) => ("side", at(1, 6), r(Rotation::Counterclockwise90), Mirror::None), + (South, North) => ("front", at(1, -8), rotation, Mirror::None), + (North, South) => ("front", at(7, 14), r(Rotation::Clockwise180), Mirror::None), + (West, East) => ("front", at(15, 0), r(Rotation::Clockwise90), Mirror::None), + (East, West) => ("front", at(-7, 6), r(Rotation::Counterclockwise90), Mirror::None), + (Up, East) => ("1x2s", at(15, 0), r(Rotation::Clockwise90), Mirror::None), + (Up, South) => ("1x2s", at(1, 0), rotation, Mirror::None), + _ => return, + }; + let name = room_name(floor, kind, self.random, stairs); + self.add(name, pos, rot, mirror); + } + fn room_2x2(&mut self, room_pos: [i32; 3], rotation: Rotation, room: Direction, door: Direction, floor: usize) { + use Direction::{East, North, South, West}; + let r = |rot: Rotation| rotation.get_rotated(rot); + let (east, south, rot, mirror) = match (door, room) { + (East, South) => (-7, 0, rotation, Mirror::None), + (East, North) => (-7, 6, rotation, Mirror::LeftRight), + (North, East) => (1, 14, r(Rotation::Counterclockwise90), Mirror::None), + (North, West) => (7, 14, r(Rotation::Counterclockwise90), Mirror::LeftRight), + (South, West) => (7, -8, r(Rotation::Clockwise90), Mirror::None), + (South, East) => (1, -8, r(Rotation::Clockwise90), Mirror::LeftRight), + (West, North) => (15, 6, r(Rotation::Clockwise180), Mirror::None), + (West, South) => (15, 0, rotation, Mirror::FrontBack), + _ => (0, 0, rotation, Mirror::None), + }; + let pos = rel(rel(room_pos, rotation, East, east), rotation, South, south); + let name = room_name(floor, "2x2", self.random, false); + self.add(name, pos, rot, mirror); + } +} + +/// WoodlandMansionPieces.generateMansion. +pub fn pieces( + resources: &StructureResources, + origin: [i32; 3], + rotation: Rotation, + random: &mut impl RandomSource, +) -> Option> { + let grid = MansionGrid::new(random); + let mut placer = Placer { + random, + pieces: Vec::new(), + start_x: 0, + start_y: 0, + }; + placer.create(origin, rotation, &grid); + let processors = Arc::new(ProcessorList::default()); + placer + .pieces + .into_iter() + .map(|p| { + let name = format!("minecraft:woodland_mansion/{}", p.name); + Some(TemplatePiece::new( + "minecraft:wmp", + &name, + resources.template(&name)?, + p.position, + p.rotation, + p.mirror, + [0; 3], + processors.clone(), + true, + vec!["minecraft:structure_block".into()], + TemplateHandler::Mansion, + )) + }) + .collect() +} + +/// WoodlandMansionPiece.postProcess with its data markers. +pub fn post_process(piece: &TemplatePiece, c: &mut PieceContext<'_>) -> Result<()> { + let Some(markers) = piece.place_at(c, piece.position, &[])? else { + return Ok(()); + }; + for (marker, pos) in markers { + if marker.starts_with("Chest") { + let facing = match marker.as_str() { + "ChestWest" => Direction::West, + "ChestEast" => Direction::East, + "ChestSouth" => Direction::South, + _ => Direction::North, + }; + let facing = if marker == "Chest" { + Direction::North + } else { + piece.rotation.rotate_direction(facing) + }; + let state = c.state_with("chest", &[("facing", direction_name(facing))]); + create_chest(c, pos, "minecraft:chests/woodland_mansion", Some(state))?; + } else if matches!(marker.as_str(), "Mage" | "Warrior" | "Group of Allays") { + // Mobs are not spawned; the marker is cleared as after a spawn. + if marker == "Group of Allays" { + c.world.level_int(3)?; + } + c.world.set(pos, c.state("air"), 2)?; + } + } + Ok(()) +} + +/// WoodlandMansionStructure.afterPlace: cobblestone columns under the pieces. +pub fn after_place(c: &mut PieceContext<'_>, boxes: &[BoundingBox]) -> Result<()> { + let min_y = c.world.min_y()?; + let bb = super::temple::union(boxes); + let y_start = bb.min[1]; + let cobble = c.state("cobblestone"); + for x in c.chunk_box.min[0]..=c.chunk_box.max[0] { + for z in c.chunk_box.min[2]..=c.chunk_box.max[2] { + let pos = [x, y_start, z]; + let state = c.world.block(pos)?; + if c.is_air(state) || !bb.is_inside(pos) || !boxes.iter().any(|b| b.is_inside(pos)) { + continue; + } + let mut y = y_start - 1; + while y > min_y { + let state = c.world.block([x, y, z])?; + if !c.is_air(state) && !c.is_liquid(state) { + break; + } + c.world.set([x, y, z], cobble, 2)?; + y -= 1; + } + } + } + Ok(()) +} diff --git a/crates/horizon-world/src/worldgen/structure/monument.rs b/crates/horizon-world/src/worldgen/structure/monument.rs new file mode 100644 index 000000000..f875a3ae3 --- /dev/null +++ b/crates/horizon-world/src/worldgen/structure/monument.rs @@ -0,0 +1,1368 @@ +//! Ocean monuments (OceanMonumentStructure, OceanMonumentPieces). +use super::{ + BoundingBox, + legacy::{LegacyPiece, PieceContext, PieceFrame, Result, make_bounding_box}, +}; +use crate::registry::BlockStateId; +use horizon_core::{geometry::Direction, random::RandomSource}; + +const DOWN: usize = 0; +const UP: usize = 1; +const NORTH: usize = 2; +const SOUTH: usize = 3; +const WEST: usize = 4; +const EAST: usize = 5; +const OPPOSITE: [usize; 6] = [UP, DOWN, SOUTH, NORTH, EAST, WEST]; +const STEPS: [[i32; 3]; 6] = [ + [0, -1, 0], + [0, 1, 0], + [0, 0, -1], + [0, 0, 1], + [-1, 0, 0], + [1, 0, 0], +]; + +#[derive(Clone, Debug)] +struct Room { + index: i32, + conn: [Option; 6], + open: [bool; 6], + claimed: bool, + source: bool, + scan: i32, +} +impl Room { + fn new(index: i32) -> Self { + Self { + index, + conn: [None; 6], + open: [false; 6], + claimed: false, + source: false, + scan: 0, + } + } +} +fn room_index(x: i32, y: i32, z: i32) -> i32 { + y * 25 + z * 5 + x +} +struct Graph { + rooms: Vec, +} +impl Graph { + fn connect(&mut self, a: usize, d: usize, b: usize) { + self.rooms[a].conn[d] = Some(b); + self.rooms[b].conn[OPPOSITE[d]] = Some(a); + } + fn update_openings(&mut self, a: usize) { + let conn = self.rooms[a].conn; + self.rooms[a].open = conn.map(|c| c.is_some()); + } + fn find_source(&mut self, a: usize, scan: i32) -> bool { + if self.rooms[a].source { + return true; + } + self.rooms[a].scan = scan; + for i in 0..6 { + if let Some(n) = self.rooms[a].conn[i] + && self.rooms[a].open[i] + && self.rooms[n].scan != scan + && self.find_source(n, scan) + { + return true; + } + } + false + } + fn conn(&self, a: usize, d: usize) -> usize { + self.rooms[a].conn[d].unwrap_or(a) + } + fn open(&self, a: usize, d: usize) -> bool { + self.rooms[a].open[d] + } + fn claim(&mut self, a: usize) { + self.rooms[a].claimed = true; + } +} + +#[derive(Clone, Copy, Debug)] +enum Kind { + Entry, + Core, + DoubleX, + DoubleXY, + DoubleY, + DoubleYZ, + DoubleZ, + Simple(i32), + SimpleTop, + Wing(i32), + Penthouse, +} +#[derive(Debug)] +struct Child { + kind: Kind, + frame: PieceFrame, + room: usize, +} + +#[derive(Debug)] +pub struct MonumentBuilding { + frame: PieceFrame, + rooms: Vec, + children: Vec, +} +fn room_box(direction: Direction, index: i32, w: i32, h: i32, d: i32) -> BoundingBox { + let (rx, rz, ry) = (index % 5, index / 5 % 5, index / 25); + let b = make_bounding_box(0, 0, 0, direction, w * 8, h * 4, d * 8); + b.moved(match direction { + Direction::North => [rx * 8, ry * 4, -(rz + d) * 8 + 1], + Direction::South => [rx * 8, ry * 4, rz * 8], + Direction::West => [-(rz + d) * 8 + 1, ry * 4, rx * 8], + _ => [rz * 8, ry * 4, rx * 8], + }) +} +impl MonumentBuilding { + pub fn new(random: &mut impl RandomSource, west: i32, north: i32, direction: Direction) -> Self { + let frame = PieceFrame::new( + make_bounding_box(west, 39, north, direction, 58, 23, 58), + Some(direction), + ); + let mut g = Graph { rooms: Vec::new() }; + let mut grid: [Option; 75] = [None; 75]; + let mut add = |g: &mut Graph, x: i32, y: i32, z: i32| { + let i = room_index(x, y, z); + grid[i as usize] = Some(g.rooms.len()); + g.rooms.push(Room::new(i)); + }; + for x in 0..5 { + for z in 0..4 { + add(&mut g, x, 0, z); + } + } + for x in 0..5 { + for z in 0..4 { + add(&mut g, x, 1, z); + } + } + for x in 1..4 { + for z in 0..2 { + add(&mut g, x, 2, z); + } + } + let source = grid[room_index(2, 0, 0) as usize].unwrap(); + for x in 0..5 { + for z in 0..5 { + for y in 0..3 { + let Some(a) = grid[room_index(x, y, z) as usize] else { + continue; + }; + for (d, s) in STEPS.iter().enumerate() { + let (nx, ny, nz) = (x + s[0], y + s[1], z + s[2]); + if !(0..5).contains(&nx) || !(0..5).contains(&nz) || !(0..3).contains(&ny) { + continue; + } + if let Some(b) = grid[room_index(nx, ny, nz) as usize] { + let dir = if nz == z { d } else { OPPOSITE[d] }; + g.connect(a, dir, b); + } + } + } + } + } + let roof = g.rooms.len(); + g.rooms.push(Room::new(1003)); + let left = g.rooms.len(); + g.rooms.push(Room::new(1001)); + let right = g.rooms.len(); + g.rooms.push(Room::new(1002)); + g.connect(grid[room_index(2, 2, 0) as usize].unwrap(), UP, roof); + g.connect(grid[room_index(0, 1, 0) as usize].unwrap(), SOUTH, left); + g.connect(grid[room_index(4, 1, 0) as usize].unwrap(), SOUTH, right); + g.claim(roof); + g.claim(left); + g.claim(right); + g.rooms[source].source = true; + let core = grid[room_index(random.next_int_bound(4).unwrap_or(0), 0, 2) as usize].unwrap(); + let (ce, cn, cu) = (g.conn(core, EAST), g.conn(core, NORTH), g.conn(core, UP)); + let cen = g.conn(ce, NORTH); + for r in [ + core, + ce, + cn, + cen, + cu, + g.conn(ce, UP), + g.conn(cn, UP), + g.conn(cen, UP), + ] { + g.claim(r); + } + let mut defs = Vec::new(); + for r in grid.iter().flatten() { + g.update_openings(*r); + defs.push(*r); + } + g.update_openings(roof); + for i in (2..=defs.len()).rev() { + let j = random.next_int_bound(i as i32).unwrap_or(0) as usize; + defs.swap(i - 1, j); + } + let mut scan = 1; + for &def in &defs { + let (mut closed, mut attempts) = (0, 0); + while closed < 2 && attempts < 5 { + attempts += 1; + let f = random.next_int_bound(6).unwrap_or(0) as usize; + if !g.rooms[def].open[f] { + continue; + } + let of = OPPOSITE[f]; + let other = g.conn(def, f); + g.rooms[def].open[f] = false; + g.rooms[other].open[of] = false; + let a = g.find_source(def, scan); + scan += 1; + let ok = a && { + let b = g.find_source(other, scan); + scan += 1; + b + }; + if ok { + closed += 1; + } else { + g.rooms[def].open[f] = true; + g.rooms[other].open[of] = true; + } + } + } + defs.extend([roof, left, right]); + g.claim(source); + let mut children = Vec::new(); + let child = |kind: Kind, room: usize, g: &Graph, w: i32, h: i32, d: i32| Child { + kind, + frame: PieceFrame::new(room_box(direction, g.rooms[room].index, w, h, d), Some(direction)), + room, + }; + children.push(child(Kind::Entry, source, &g, 1, 1, 1)); + children.push(child(Kind::Core, core, &g, 2, 2, 2)); + for def in defs { + if g.rooms[def].claimed || g.rooms[def].index >= 75 { + continue; + } + let free = |g: &Graph, r: usize, d: usize| g.open(r, d) && !g.rooms[g.conn(r, d)].claimed; + if free(&g, def, EAST) && free(&g, def, UP) && free(&g, g.conn(def, EAST), UP) { + let e = g.conn(def, EAST); + for r in [def, e, g.conn(def, UP), g.conn(e, UP)] { + g.claim(r); + } + children.push(child(Kind::DoubleXY, def, &g, 2, 2, 1)); + } else if free(&g, def, NORTH) && free(&g, def, UP) && free(&g, g.conn(def, NORTH), UP) { + let n = g.conn(def, NORTH); + for r in [def, n, g.conn(def, UP), g.conn(n, UP)] { + g.claim(r); + } + children.push(child(Kind::DoubleYZ, def, &g, 1, 2, 2)); + } else if free(&g, def, NORTH) { + g.claim(def); + g.claim(g.conn(def, NORTH)); + children.push(child(Kind::DoubleZ, def, &g, 1, 1, 2)); + } else if free(&g, def, EAST) { + g.claim(def); + g.claim(g.conn(def, EAST)); + children.push(child(Kind::DoubleX, def, &g, 2, 1, 1)); + } else if free(&g, def, UP) { + g.claim(def); + g.claim(g.conn(def, UP)); + children.push(child(Kind::DoubleY, def, &g, 1, 2, 1)); + } else if [WEST, EAST, NORTH, SOUTH, UP].iter().all(|&d| !g.open(def, d)) { + g.claim(def); + children.push(child(Kind::SimpleTop, def, &g, 1, 1, 1)); + } else { + g.claim(def); + let design = random.next_int_bound(3).unwrap_or(0); + children.push(child(Kind::Simple(design), def, &g, 1, 1, 1)); + } + } + let offset = frame.world_pos(9, 0, 22); + for c in &mut children { + c.frame.bounding_box = c.frame.bounding_box.moved(offset); + } + let corners = |a: [i32; 3], b: [i32; 3]| { + BoundingBox::from_corners( + frame.world_pos(a[0], a[1], a[2]), + frame.world_pos(b[0], b[1], b[2]), + ) + }; + let wing_random = random.next_int(); + for (i, b) in [ + corners([1, 1, 1], [23, 8, 21]), + corners([34, 1, 1], [56, 8, 21]), + ] + .into_iter() + .enumerate() + { + children.push(Child { + kind: Kind::Wing(wing_random.wrapping_add(i as i32) & 1), + frame: PieceFrame::new(b, Some(direction)), + room: source, + }); + } + children.push(Child { + kind: Kind::Penthouse, + frame: PieceFrame::new(corners([22, 13, 22], [35, 17, 35]), Some(direction)), + room: source, + }); + Self { + frame, + rooms: g.rooms, + children, + } + } +} + +/// OceanMonumentPiece helpers bound to one frame and chunk. +struct W<'x, 'c> { + f: PieceFrame, + c: &'x mut PieceContext<'c>, + sea: i32, + water: BlockStateId, +} +impl W<'_, '_> { + #[allow(clippy::too_many_arguments)] + fn bx(&mut self, x0: i32, y0: i32, z0: i32, x1: i32, y1: i32, z1: i32, s: BlockStateId) -> Result<()> { + self.f.generate_box(self.c, [x0, y0, z0], [x1, y1, z1], s, s, false) + } + fn put(&mut self, s: BlockStateId, x: i32, y: i32, z: i32) -> Result<()> { + self.f.place(self.c, s, x, y, z) + } + fn water(&mut self, x0: i32, y0: i32, z0: i32, x1: i32, y1: i32, z1: i32) -> Result<()> { + let air = self.c.state("air"); + for y in y0..=y1 { + for x in x0..=x1 { + for z in z0..=z1 { + let s = self.f.get(self.c, x, y, z)?; + if matches!( + self.c.name(s), + "minecraft:ice" | "minecraft:packed_ice" | "minecraft:blue_ice" | "minecraft:water" + ) { + continue; + } + let state = if self.f.world_y(y) >= self.sea && s != self.water { + air + } else { + self.water + }; + self.f.place(self.c, state, x, y, z)?; + } + } + } + Ok(()) + } + #[allow(clippy::too_many_arguments)] + fn fill_only(&mut self, x0: i32, y0: i32, z0: i32, x1: i32, y1: i32, z1: i32, s: BlockStateId) -> Result<()> { + for y in y0..=y1 { + for x in x0..=x1 { + for z in z0..=z1 { + if self.f.get(self.c, x, y, z)? == self.water { + self.f.place(self.c, s, x, y, z)?; + } + } + } + } + Ok(()) + } + fn floor(&mut self, xo: i32, zo: i32, down: bool) -> Result<()> { + let (g, l) = (self.c.state("prismarine"), self.c.state("prismarine_bricks")); + if down { + self.bx(xo, 0, zo, xo + 2, 0, zo + 7, g)?; + self.bx(xo + 5, 0, zo, xo + 7, 0, zo + 7, g)?; + self.bx(xo + 3, 0, zo, xo + 4, 0, zo + 2, g)?; + self.bx(xo + 3, 0, zo + 5, xo + 4, 0, zo + 7, g)?; + self.bx(xo + 3, 0, zo + 2, xo + 4, 0, zo + 2, l)?; + self.bx(xo + 3, 0, zo + 5, xo + 4, 0, zo + 5, l)?; + self.bx(xo + 2, 0, zo + 3, xo + 2, 0, zo + 4, l)?; + self.bx(xo + 5, 0, zo + 3, xo + 5, 0, zo + 4, l) + } else { + self.bx(xo, 0, zo, xo + 7, 0, zo + 7, g) + } + } + fn hits(&self, x0: i32, z0: i32, x1: i32, z1: i32) -> bool { + let (ax, az) = (self.f.world_x(x0, z0), self.f.world_z(x0, z0)); + let (bx, bz) = (self.f.world_x(x1, z1), self.f.world_z(x1, z1)); + let b = self.c.chunk_box; + b.max[0] >= ax.min(bx) && b.min[0] <= ax.max(bx) && b.max[2] >= az.min(bz) && b.min[2] <= az.max(bz) + } +} +struct Palette { + g: BlockStateId, + l: BlockStateId, + k: BlockStateId, + lamp: BlockStateId, +} + +impl LegacyPiece for MonumentBuilding { + fn bounding_box(&self) -> BoundingBox { + self.frame.bounding_box + } + fn type_name(&self) -> &'static str { + "minecraft:omb" + } + fn post_process(&self, c: &mut PieceContext<'_>) -> Result<()> { + let sea = c.world.sea_level()?; + let water = c.state("water"); + let p = Palette { + g: c.state("prismarine"), + l: c.state("prismarine_bricks"), + k: c.state("dark_prismarine"), + lamp: c.state("sea_lantern"), + }; + let mut w = W { + f: self.frame, + c, + sea, + water, + }; + let water_height = sea.max(64) - self.frame.bounding_box.min[1]; + w.water(0, 0, 0, 58, water_height, 58)?; + building(&mut w, &p)?; + let c = w.c; + for child in &self.children { + if child.frame.bounding_box.intersects(&c.chunk_box) { + let mut w = W { + f: child.frame, + c: &mut *c, + sea, + water, + }; + room(&mut w, &p, child, &self.rooms)?; + } + } + Ok(()) + } +} + +fn wing(w: &mut W<'_, '_>, p: &Palette, flipped: bool, xo: i32) -> Result<()> { + let (g, l) = (p.g, p.l); + if !w.hits(xo, 0, xo + 23, 20) { + return Ok(()); + } + w.bx(xo, 0, 0, xo + 24, 0, 20, g)?; + w.water(xo, 1, 0, xo + 24, 10, 20)?; + for i in 0..4 { + w.bx(xo + i, i + 1, i, xo + i, i + 1, 20, l)?; + w.bx(xo + i + 7, i + 5, i + 7, xo + i + 7, i + 5, 20, l)?; + w.bx(xo + 17 - i, i + 5, i + 7, xo + 17 - i, i + 5, 20, l)?; + w.bx(xo + 24 - i, i + 1, i, xo + 24 - i, i + 1, 20, l)?; + w.bx(xo + i + 1, i + 1, i, xo + 23 - i, i + 1, i, l)?; + w.bx(xo + i + 8, i + 5, i + 7, xo + 16 - i, i + 5, i + 7, l)?; + } + w.bx(xo + 4, 4, 4, xo + 6, 4, 20, g)?; + w.bx(xo + 7, 4, 4, xo + 17, 4, 6, g)?; + w.bx(xo + 18, 4, 4, xo + 20, 4, 20, g)?; + w.bx(xo + 11, 8, 11, xo + 13, 8, 20, g)?; + w.put(l, xo + 12, 9, 12)?; + w.put(l, xo + 12, 9, 15)?; + w.put(l, xo + 12, 9, 18)?; + let left = xo + if flipped { 19 } else { 5 }; + let right = xo + if flipped { 5 } else { 19 }; + for z in (5..=20).rev().step_by(3) { + w.put(l, left, 5, z)?; + } + for z in (7..=19).rev().step_by(3) { + w.put(l, right, 5, z)?; + } + for i in 0..4 { + let x = if flipped { + xo + 24 - (17 - i * 3) + } else { + xo + 17 - i * 3 + }; + w.put(l, x, 5, 5)?; + } + w.put(l, right, 5, 5)?; + w.bx(xo + 11, 1, 12, xo + 13, 7, 12, g)?; + w.bx(xo + 12, 1, 11, xo + 12, 7, 13, g) +} +/// MonumentBuilding.postProcess after the first water box (without children). +fn building(w: &mut W<'_, '_>, p: &Palette) -> Result<()> { + let (g, l, k, lamp) = (p.g, p.l, p.k, p.lamp); + wing(w, p, false, 0)?; + wing(w, p, true, 33)?; + if w.hits(22, 5, 35, 17) { + w.water(25, 0, 0, 32, 8, 20)?; + for i in 0..4 { + let z = 5 + i * 4; + w.bx(24, 2, z, 24, 4, z, l)?; + w.bx(22, 4, z, 23, 4, z, l)?; + w.put(l, 25, 5, z)?; + w.put(l, 26, 6, z)?; + w.put(lamp, 26, 5, z)?; + w.bx(33, 2, z, 33, 4, z, l)?; + w.bx(34, 4, z, 35, 4, z, l)?; + w.put(l, 32, 5, z)?; + w.put(l, 31, 6, z)?; + w.put(lamp, 31, 5, z)?; + w.bx(27, 6, z, 30, 6, z, g)?; + } + } + if w.hits(15, 20, 42, 21) { + w.bx(15, 0, 21, 42, 0, 21, g)?; + w.water(26, 1, 21, 31, 3, 21)?; + for [x0, y, x1] in [ + [21, 12, 36], + [17, 11, 40], + [16, 10, 41], + ] { + w.bx(x0, y, 21, x1, y, 21, g)?; + } + w.bx(15, 7, 21, 42, 9, 21, g)?; + for [x0, y, x1] in [ + [16, 6, 41], + [17, 5, 40], + [21, 4, 36], + [22, 3, 26], + [31, 3, 35], + [23, 2, 25], + [32, 2, 34], + ] { + w.bx(x0, y, 21, x1, y, 21, g)?; + } + w.bx(28, 4, 20, 29, 4, 21, l)?; + for [x, y] in [[27, 3], [30, 3], [26, 2], [31, 2], [25, 1], [32, 1]] { + w.put(l, x, y, 21)?; + } + for i in 0..7 { + w.put(k, 28 - i, 6 + i, 21)?; + w.put(k, 29 + i, 6 + i, 21)?; + } + for i in 0..4 { + w.put(k, 28 - i, 9 + i, 21)?; + w.put(k, 29 + i, 9 + i, 21)?; + } + w.put(k, 28, 12, 21)?; + w.put(k, 29, 12, 21)?; + for i in 0..3 { + w.put(k, 22 - i * 2, 8, 21)?; + w.put(k, 22 - i * 2, 9, 21)?; + w.put(k, 35 + i * 2, 8, 21)?; + w.put(k, 35 + i * 2, 9, 21)?; + } + w.water(15, 13, 21, 42, 15, 21)?; + for [x0, y1, x1] in [ + [15, 6, 15], + [16, 5, 16], + [17, 4, 20], + [21, 3, 21], + [22, 2, 22], + [23, 1, 24], + [42, 6, 42], + [41, 5, 41], + [37, 4, 40], + [36, 3, 36], + [33, 1, 34], + [35, 2, 35], + ] { + w.water(x0, 1, 21, x1, y1, 21)?; + } + } + if w.hits(21, 21, 36, 36) { + w.bx(21, 0, 22, 36, 0, 36, g)?; + w.water(21, 1, 22, 36, 23, 36)?; + for i in 0..4 { + w.bx(21 + i, 13 + i, 21 + i, 36 - i, 13 + i, 21 + i, l)?; + w.bx(21 + i, 13 + i, 36 - i, 36 - i, 13 + i, 36 - i, l)?; + w.bx(21 + i, 13 + i, 22 + i, 21 + i, 13 + i, 35 - i, l)?; + w.bx(36 - i, 13 + i, 22 + i, 36 - i, 13 + i, 35 - i, l)?; + } + w.bx(25, 16, 25, 32, 16, 32, g)?; + w.bx(25, 17, 25, 25, 19, 25, l)?; + w.bx(32, 17, 25, 32, 19, 25, l)?; + w.bx(25, 17, 32, 25, 19, 32, l)?; + w.bx(32, 17, 32, 32, 19, 32, l)?; + for [a, b, cx, cz] in [[26, 26, 27, 27], [26, 31, 27, 30], [31, 31, 30, 30], [31, 26, 30, 27]] { + w.put(l, a, 20, b)?; + w.put(l, cx, 21, cz)?; + w.put(lamp, cx, 20, cz)?; + } + w.bx(28, 21, 27, 29, 21, 27, g)?; + w.bx(27, 21, 28, 27, 21, 29, g)?; + w.bx(28, 21, 30, 29, 21, 30, g)?; + w.bx(30, 21, 28, 30, 21, 29, g)?; + } + // generateLowerWall + if w.hits(0, 21, 6, 58) { + w.bx(0, 0, 21, 6, 0, 57, g)?; + w.water(0, 1, 21, 6, 7, 57)?; + w.bx(4, 4, 21, 6, 4, 53, g)?; + for i in 0..4 { + w.bx(i, i + 1, 21, i, i + 1, 57 - i, l)?; + } + for z in (23..53).step_by(3) { + w.put(l, 5, 5, z)?; + } + w.put(l, 5, 5, 52)?; + for i in 0..4 { + w.bx(i, i + 1, 21, i, i + 1, 57 - i, l)?; + } + w.bx(4, 1, 52, 6, 3, 52, g)?; + w.bx(5, 1, 51, 5, 3, 53, g)?; + } + if w.hits(51, 21, 58, 58) { + w.bx(51, 0, 21, 57, 0, 57, g)?; + w.water(51, 1, 21, 57, 7, 57)?; + w.bx(51, 4, 21, 53, 4, 53, g)?; + for i in 0..4 { + w.bx(57 - i, i + 1, 21, 57 - i, i + 1, 57 - i, l)?; + } + for z in (23..53).step_by(3) { + w.put(l, 52, 5, z)?; + } + w.put(l, 52, 5, 52)?; + w.bx(51, 1, 52, 53, 3, 52, g)?; + w.bx(52, 1, 51, 52, 3, 53, g)?; + } + if w.hits(0, 51, 57, 57) { + w.bx(7, 0, 51, 50, 0, 57, g)?; + w.water(7, 1, 51, 50, 10, 57)?; + for i in 0..4 { + w.bx(i + 1, i + 1, 57 - i, 56 - i, i + 1, 57 - i, l)?; + } + } + // generateMiddleWall + if w.hits(7, 21, 13, 50) { + w.bx(7, 0, 21, 13, 0, 50, g)?; + w.water(7, 1, 21, 13, 10, 50)?; + w.bx(11, 8, 21, 13, 8, 53, g)?; + for i in 0..4 { + w.bx(i + 7, i + 5, 21, i + 7, i + 5, 54, l)?; + } + for z in (21..=45).step_by(3) { + w.put(l, 12, 9, z)?; + } + } + if w.hits(44, 21, 50, 54) { + w.bx(44, 0, 21, 50, 0, 50, g)?; + w.water(44, 1, 21, 50, 10, 50)?; + w.bx(44, 8, 21, 46, 8, 53, g)?; + for i in 0..4 { + w.bx(50 - i, i + 5, 21, 50 - i, i + 5, 54, l)?; + } + for z in (21..=45).step_by(3) { + w.put(l, 45, 9, z)?; + } + } + if w.hits(8, 44, 49, 54) { + w.bx(14, 0, 44, 43, 0, 50, g)?; + w.water(14, 1, 44, 43, 10, 50)?; + for x in (12..=45).step_by(3) { + w.put(l, x, 9, 45)?; + w.put(l, x, 9, 52)?; + if matches!(x, 12 | 18 | 24 | 33 | 39 | 45) { + for [y, z] in [ + [9, 47], + [9, 50], + [10, 45], + [10, 46], + [10, 51], + [10, 52], + [11, 47], + [11, 50], + [12, 48], + [12, 49], + ] { + w.put(l, x, y, z)?; + } + } + } + for i in 0..3 { + w.bx(8 + i, 5 + i, 54, 49 - i, 5 + i, 54, g)?; + } + w.bx(11, 8, 54, 46, 8, 54, l)?; + w.bx(14, 8, 44, 43, 8, 53, g)?; + } + // generateUpperWall + if w.hits(14, 21, 20, 43) { + w.bx(14, 0, 21, 20, 0, 43, g)?; + w.water(14, 1, 22, 20, 14, 43)?; + w.bx(18, 12, 22, 20, 12, 39, g)?; + w.bx(18, 12, 21, 20, 12, 21, l)?; + for i in 0..4 { + w.bx(i + 14, i + 9, 21, i + 14, i + 9, 43 - i, l)?; + } + for z in (23..=39).step_by(3) { + w.put(l, 19, 13, z)?; + } + } + if w.hits(37, 21, 43, 43) { + w.bx(37, 0, 21, 43, 0, 43, g)?; + w.water(37, 1, 22, 43, 14, 43)?; + w.bx(37, 12, 22, 39, 12, 39, g)?; + w.bx(37, 12, 21, 39, 12, 21, l)?; + for i in 0..4 { + w.bx(43 - i, i + 9, 21, 43 - i, i + 9, 43 - i, l)?; + } + for z in (23..=39).step_by(3) { + w.put(l, 38, 13, z)?; + } + } + if w.hits(15, 37, 42, 43) { + w.bx(21, 0, 37, 36, 0, 43, g)?; + w.water(21, 1, 37, 36, 14, 43)?; + w.bx(21, 12, 37, 36, 12, 39, g)?; + for i in 0..4 { + w.bx(15 + i, i + 9, 43 - i, 42 - i, i + 9, 43 - i, l)?; + } + for x in (21..=36).step_by(3) { + w.put(l, x, 13, 38)?; + } + } + for px in 0..7 { + let mut pz = 0; + while pz < 7 { + if pz == 0 && px == 3 { + pz = 6; + } + let (bx, bz) = (px * 9, pz * 9); + for dx in 0..4 { + for dz in 0..4 { + w.put(l, bx + dx, 0, bz + dz)?; + w.f.fill_column_down(w.c, l, bx + dx, -1, bz + dz)?; + } + } + pz += if px != 0 && px != 6 { 6 } else { 1 }; + } + } + for i in 0..5 { + w.water(-1 - i, i * 2, -1 - i, -1 - i, 23, 58 + i)?; + w.water(58 + i, i * 2, -1 - i, 58 + i, 23, 58 + i)?; + w.water(-i, i * 2, -1 - i, 57 + i, 23, -1 - i)?; + w.water(-i, i * 2, 58 + i, 57 + i, 23, 58 + i)?; + } + Ok(()) +} + +/// Openings of the doorways shared by the double rooms: (room, side, water box). +fn doorways(w: &mut W<'_, '_>, list: &[(bool, [i32; 6])]) -> Result<()> { + for (open, [x0, y0, z0, x1, y1, z1]) in list { + if *open { + w.water(*x0, *y0, *z0, *x1, *y1, *z1)?; + } + } + Ok(()) +} +fn room(w: &mut W<'_, '_>, p: &Palette, child: &Child, rooms: &[Room]) -> Result<()> { + let (g, l, k, lamp) = (p.g, p.l, p.k, p.lamp); + let r = &rooms[child.room]; + let conn = |a: &Room, d: usize| a.conn[d].map(|i| &rooms[i]); + let upper = r.index / 25 > 0; + match child.kind { + Kind::Core => { + w.fill_only(1, 8, 0, 14, 8, 14, g)?; + w.bx(0, 7, 0, 0, 7, 15, l)?; + w.bx(15, 7, 0, 15, 7, 15, l)?; + w.bx(1, 7, 0, 15, 7, 0, l)?; + w.bx(1, 7, 15, 14, 7, 15, l)?; + for y in 1..=6 { + let b = if y == 2 || y == 6 { g } else { l }; + for x in [0, 15] { + w.bx(x, y, 0, x, y, 1, b)?; + w.bx(x, y, 6, x, y, 9, b)?; + w.bx(x, y, 14, x, y, 15, b)?; + } + w.bx(1, y, 0, 1, y, 0, b)?; + w.bx(6, y, 0, 9, y, 0, b)?; + w.bx(14, y, 0, 14, y, 0, b)?; + w.bx(1, y, 15, 14, y, 15, b)?; + } + w.bx(6, 3, 6, 9, 6, 9, k)?; + let gold = w.c.state("gold_block"); + w.bx(7, 4, 7, 8, 5, 8, gold)?; + for y in [3, 6] { + for x in [6, 9] { + w.put(lamp, x, y, 6)?; + w.put(lamp, x, y, 9)?; + } + } + for [x0, y0, z0, x1, y1, z1] in [ + [5, 1, 6, 5, 2, 6], + [5, 1, 9, 5, 2, 9], + [10, 1, 6, 10, 2, 6], + [10, 1, 9, 10, 2, 9], + [6, 1, 5, 6, 2, 5], + [9, 1, 5, 9, 2, 5], + [6, 1, 10, 6, 2, 10], + [9, 1, 10, 9, 2, 10], + [5, 2, 5, 5, 6, 5], + [5, 2, 10, 5, 6, 10], + [10, 2, 5, 10, 6, 5], + [10, 2, 10, 10, 6, 10], + [5, 7, 1, 5, 7, 6], + [10, 7, 1, 10, 7, 6], + [5, 7, 9, 5, 7, 14], + [10, 7, 9, 10, 7, 14], + [1, 7, 5, 6, 7, 5], + [1, 7, 10, 6, 7, 10], + [9, 7, 5, 14, 7, 5], + [9, 7, 10, 14, 7, 10], + [2, 1, 2, 2, 1, 3], + [3, 1, 2, 3, 1, 2], + [13, 1, 2, 13, 1, 3], + [12, 1, 2, 12, 1, 2], + [2, 1, 12, 2, 1, 13], + [3, 1, 13, 3, 1, 13], + [13, 1, 12, 13, 1, 13], + [12, 1, 13, 12, 1, 13], + ] { + w.bx(x0, y0, z0, x1, y1, z1, l)?; + } + } + Kind::DoubleX => { + let east = conn(r, EAST).unwrap_or(r); + let west = r; + if upper { + w.floor(8, 0, east.open[DOWN])?; + w.floor(0, 0, west.open[DOWN])?; + } + if west.conn[UP].is_none() { + w.fill_only(1, 4, 1, 7, 4, 6, g)?; + } + if east.conn[UP].is_none() { + w.fill_only(8, 4, 1, 14, 4, 6, g)?; + } + for (y, b) in [(3, l), (2, g), (1, l)] { + w.bx(0, y, 0, 0, y, 7, b)?; + w.bx(15, y, 0, 15, y, 7, b)?; + w.bx(1, y, 0, 15, y, 0, b)?; + w.bx(1, y, 7, 14, y, 7, b)?; + } + w.bx(5, 1, 0, 10, 1, 4, l)?; + w.bx(6, 2, 0, 9, 2, 3, g)?; + w.bx(5, 3, 0, 10, 3, 4, l)?; + w.put(lamp, 6, 2, 3)?; + w.put(lamp, 9, 2, 3)?; + doorways( + w, + &[ + (west.open[SOUTH], [3, 1, 0, 4, 2, 0]), + (west.open[NORTH], [3, 1, 7, 4, 2, 7]), + (west.open[WEST], [0, 1, 3, 0, 2, 4]), + (east.open[SOUTH], [11, 1, 0, 12, 2, 0]), + (east.open[NORTH], [11, 1, 7, 12, 2, 7]), + (east.open[EAST], [15, 1, 3, 15, 2, 4]), + ], + )?; + } + Kind::DoubleXY => { + let east = conn(r, EAST).unwrap_or(r); + let west = r; + let west_up = conn(west, UP).unwrap_or(r); + let east_up = conn(east, UP).unwrap_or(r); + if upper { + w.floor(8, 0, east.open[DOWN])?; + w.floor(0, 0, west.open[DOWN])?; + } + if west_up.conn[UP].is_none() { + w.fill_only(1, 8, 1, 7, 8, 6, g)?; + } + if east_up.conn[UP].is_none() { + w.fill_only(8, 8, 1, 14, 8, 6, g)?; + } + for y in 1..=7 { + let b = if y == 2 || y == 6 { g } else { l }; + w.bx(0, y, 0, 0, y, 7, b)?; + w.bx(15, y, 0, 15, y, 7, b)?; + w.bx(1, y, 0, 15, y, 0, b)?; + w.bx(1, y, 7, 14, y, 7, b)?; + } + for [x0, y0, z0, x1, y1, z1] in [ + [2, 1, 3, 2, 7, 4], + [3, 1, 2, 4, 7, 2], + [3, 1, 5, 4, 7, 5], + [13, 1, 3, 13, 7, 4], + [11, 1, 2, 12, 7, 2], + [11, 1, 5, 12, 7, 5], + [5, 1, 3, 5, 3, 4], + [10, 1, 3, 10, 3, 4], + [5, 7, 2, 10, 7, 5], + [5, 5, 2, 5, 7, 2], + [10, 5, 2, 10, 7, 2], + [5, 5, 5, 5, 7, 5], + [10, 5, 5, 10, 7, 5], + ] { + w.bx(x0, y0, z0, x1, y1, z1, l)?; + } + w.put(l, 6, 6, 2)?; + w.put(l, 9, 6, 2)?; + w.put(l, 6, 6, 5)?; + w.put(l, 9, 6, 5)?; + w.bx(5, 4, 3, 6, 4, 4, l)?; + w.bx(9, 4, 3, 10, 4, 4, l)?; + w.put(lamp, 5, 4, 2)?; + w.put(lamp, 5, 4, 5)?; + w.put(lamp, 10, 4, 2)?; + w.put(lamp, 10, 4, 5)?; + doorways( + w, + &[ + (west.open[SOUTH], [3, 1, 0, 4, 2, 0]), + (west.open[NORTH], [3, 1, 7, 4, 2, 7]), + (west.open[WEST], [0, 1, 3, 0, 2, 4]), + (east.open[SOUTH], [11, 1, 0, 12, 2, 0]), + (east.open[NORTH], [11, 1, 7, 12, 2, 7]), + (east.open[EAST], [15, 1, 3, 15, 2, 4]), + (west_up.open[SOUTH], [3, 5, 0, 4, 6, 0]), + (west_up.open[NORTH], [3, 5, 7, 4, 6, 7]), + (west_up.open[WEST], [0, 5, 3, 0, 6, 4]), + (east_up.open[SOUTH], [11, 5, 0, 12, 6, 0]), + (east_up.open[NORTH], [11, 5, 7, 12, 6, 7]), + (east_up.open[EAST], [15, 5, 3, 15, 6, 4]), + ], + )?; + } + Kind::DoubleY => { + if upper { + w.floor(0, 0, r.open[DOWN])?; + } + let above = conn(r, UP).unwrap_or(r); + if above.conn[UP].is_none() { + w.fill_only(1, 8, 1, 6, 8, 6, g)?; + } + for [x0, z0, x1, z1] in [ + [0, 0, 0, 7], + [7, 0, 7, 7], + [1, 0, 6, 0], + [1, 7, 6, 7], + [2, 1, 2, 2], + [1, 2, 1, 2], + [5, 1, 5, 2], + [6, 2, 6, 2], + [2, 5, 2, 6], + [1, 5, 1, 5], + [5, 5, 5, 6], + [6, 5, 6, 5], + ] { + w.bx(x0, 4, z0, x1, 4, z1, l)?; + } + let mut def = r; + for y in [1, 5] { + for (side, z) in [(SOUTH, 0), (NORTH, 7)] { + if def.open[side] { + w.bx(2, y, z, 2, y + 2, z, l)?; + w.bx(5, y, z, 5, y + 2, z, l)?; + w.bx(3, y + 2, z, 4, y + 2, z, l)?; + } else { + w.bx(0, y, z, 7, y + 2, z, l)?; + w.bx(0, y + 1, z, 7, y + 1, z, g)?; + } + } + for (side, x) in [(WEST, 0), (EAST, 7)] { + if def.open[side] { + w.bx(x, y, 2, x, y + 2, 2, l)?; + w.bx(x, y, 5, x, y + 2, 5, l)?; + w.bx(x, y + 2, 3, x, y + 2, 4, l)?; + } else { + w.bx(x, y, 0, x, y + 2, 7, l)?; + w.bx(x, y + 1, 0, x, y + 1, 7, g)?; + } + } + def = above; + } + } + Kind::DoubleYZ => { + let north = conn(r, NORTH).unwrap_or(r); + let south = r; + let north_up = conn(north, UP).unwrap_or(r); + let south_up = conn(south, UP).unwrap_or(r); + if upper { + w.floor(0, 8, north.open[DOWN])?; + w.floor(0, 0, south.open[DOWN])?; + } + if south_up.conn[UP].is_none() { + w.fill_only(1, 8, 1, 6, 8, 7, g)?; + } + if north_up.conn[UP].is_none() { + w.fill_only(1, 8, 8, 6, 8, 14, g)?; + } + for y in 1..=7 { + let b = if y == 2 || y == 6 { g } else { l }; + w.bx(0, y, 0, 0, y, 15, b)?; + w.bx(7, y, 0, 7, y, 15, b)?; + w.bx(1, y, 0, 6, y, 0, b)?; + w.bx(1, y, 15, 6, y, 15, b)?; + } + for y in 1..=7 { + let b = if y == 2 || y == 6 { lamp } else { k }; + w.bx(3, y, 7, 4, y, 8, b)?; + } + doorways( + w, + &[ + (south.open[SOUTH], [3, 1, 0, 4, 2, 0]), + (south.open[EAST], [7, 1, 3, 7, 2, 4]), + (south.open[WEST], [0, 1, 3, 0, 2, 4]), + (north.open[NORTH], [3, 1, 15, 4, 2, 15]), + (north.open[WEST], [0, 1, 11, 0, 2, 12]), + (north.open[EAST], [7, 1, 11, 7, 2, 12]), + (south_up.open[SOUTH], [3, 5, 0, 4, 6, 0]), + ], + )?; + if south_up.open[EAST] { + w.water(7, 5, 3, 7, 6, 4)?; + w.bx(5, 4, 2, 6, 4, 5, l)?; + w.bx(6, 1, 2, 6, 3, 2, l)?; + w.bx(6, 1, 5, 6, 3, 5, l)?; + } + if south_up.open[WEST] { + w.water(0, 5, 3, 0, 6, 4)?; + w.bx(1, 4, 2, 2, 4, 5, l)?; + w.bx(1, 1, 2, 1, 3, 2, l)?; + w.bx(1, 1, 5, 1, 3, 5, l)?; + } + if north_up.open[NORTH] { + w.water(3, 5, 15, 4, 6, 15)?; + } + if north_up.open[WEST] { + w.water(0, 5, 11, 0, 6, 12)?; + w.bx(1, 4, 10, 2, 4, 13, l)?; + w.bx(1, 1, 10, 1, 3, 10, l)?; + w.bx(1, 1, 13, 1, 3, 13, l)?; + } + if north_up.open[EAST] { + w.water(7, 5, 11, 7, 6, 12)?; + w.bx(5, 4, 10, 6, 4, 13, l)?; + w.bx(6, 1, 10, 6, 3, 10, l)?; + w.bx(6, 1, 13, 6, 3, 13, l)?; + } + } + Kind::DoubleZ => { + let north = conn(r, NORTH).unwrap_or(r); + let south = r; + if upper { + w.floor(0, 8, north.open[DOWN])?; + w.floor(0, 0, south.open[DOWN])?; + } + if south.conn[UP].is_none() { + w.fill_only(1, 4, 1, 6, 4, 7, g)?; + } + if north.conn[UP].is_none() { + w.fill_only(1, 4, 8, 6, 4, 14, g)?; + } + for (y, b) in [(3, l), (2, g), (1, l)] { + w.bx(0, y, 0, 0, y, 15, b)?; + w.bx(7, y, 0, 7, y, 15, b)?; + w.bx(1, y, 0, 7, y, 0, b)?; + w.bx(1, y, 15, 6, y, 15, b)?; + } + for [x0, y0, z0, x1, y1, z1] in [ + [1, 1, 1, 1, 1, 2], + [6, 1, 1, 6, 1, 2], + [1, 3, 1, 1, 3, 2], + [6, 3, 1, 6, 3, 2], + [1, 1, 13, 1, 1, 14], + [6, 1, 13, 6, 1, 14], + [1, 3, 13, 1, 3, 14], + [6, 3, 13, 6, 3, 14], + [2, 1, 6, 2, 3, 6], + [5, 1, 6, 5, 3, 6], + [2, 1, 9, 2, 3, 9], + [5, 1, 9, 5, 3, 9], + [3, 2, 6, 4, 2, 6], + [3, 2, 9, 4, 2, 9], + [2, 2, 7, 2, 2, 8], + [5, 2, 7, 5, 2, 8], + ] { + w.bx(x0, y0, z0, x1, y1, z1, l)?; + } + for [x, z] in [[2, 5], [5, 5], [2, 10], [5, 10]] { + w.put(lamp, x, 2, z)?; + } + for [x, z] in [[2, 5], [5, 5], [2, 10], [5, 10]] { + w.put(l, x, 3, z)?; + } + doorways( + w, + &[ + (south.open[SOUTH], [3, 1, 0, 4, 2, 0]), + (south.open[EAST], [7, 1, 3, 7, 2, 4]), + (south.open[WEST], [0, 1, 3, 0, 2, 4]), + (north.open[NORTH], [3, 1, 15, 4, 2, 15]), + (north.open[WEST], [0, 1, 11, 0, 2, 12]), + (north.open[EAST], [7, 1, 11, 7, 2, 12]), + ], + )?; + } + Kind::Entry => { + for [x0, y0, z0, x1, y1, z1] in [ + [0, 3, 0, 2, 3, 7], + [5, 3, 0, 7, 3, 7], + [0, 2, 0, 1, 2, 7], + [6, 2, 0, 7, 2, 7], + [0, 1, 0, 0, 1, 7], + [7, 1, 0, 7, 1, 7], + [0, 1, 7, 7, 3, 7], + [1, 1, 0, 2, 3, 0], + [5, 1, 0, 6, 3, 0], + ] { + w.bx(x0, y0, z0, x1, y1, z1, l)?; + } + doorways( + w, + &[ + (r.open[NORTH], [3, 1, 7, 4, 2, 7]), + (r.open[WEST], [0, 1, 3, 1, 2, 4]), + (r.open[EAST], [6, 1, 3, 7, 2, 4]), + ], + )?; + } + Kind::Penthouse => { + w.bx(2, -1, 2, 11, -1, 11, l)?; + w.bx(0, -1, 0, 1, -1, 11, g)?; + w.bx(12, -1, 0, 13, -1, 11, g)?; + w.bx(2, -1, 0, 11, -1, 1, g)?; + w.bx(2, -1, 12, 11, -1, 13, g)?; + w.bx(0, 0, 0, 0, 0, 13, l)?; + w.bx(13, 0, 0, 13, 0, 13, l)?; + w.bx(1, 0, 0, 12, 0, 0, l)?; + w.bx(1, 0, 13, 12, 0, 13, l)?; + for i in (2..=11).step_by(3) { + w.put(lamp, 0, 0, i)?; + w.put(lamp, 13, 0, i)?; + w.put(lamp, i, 0, 0)?; + } + w.bx(2, 0, 3, 4, 0, 9, l)?; + w.bx(9, 0, 3, 11, 0, 9, l)?; + w.bx(4, 0, 9, 9, 0, 11, l)?; + w.put(l, 5, 0, 8)?; + w.put(l, 8, 0, 8)?; + w.put(l, 10, 0, 10)?; + w.put(l, 3, 0, 10)?; + w.bx(3, 0, 3, 3, 0, 7, k)?; + w.bx(10, 0, 3, 10, 0, 7, k)?; + w.bx(6, 0, 10, 7, 0, 10, k)?; + for x in [3, 10] { + for z in (2..=8).step_by(3) { + w.bx(x, 0, z, x, 2, z, l)?; + } + } + w.bx(5, 0, 10, 5, 2, 10, l)?; + w.bx(8, 0, 10, 8, 2, 10, l)?; + w.bx(6, -1, 7, 7, -1, 8, k)?; + w.water(6, -1, 3, 7, -1, 4)?; + // spawnElder: entities are not created. + } + Kind::Simple(design) => { + if upper { + w.floor(0, 0, r.open[DOWN])?; + } + if r.conn[UP].is_none() { + w.fill_only(1, 4, 1, 6, 4, 6, g)?; + } + let pillar = design != 0 + && w.c.random.boolean() + && !r.open[DOWN] + && !r.open[UP] + && r.open.iter().filter(|o| **o).count() > 1; + match design { + 0 => { + for (xa, xb, za, zb, gx, gz, lx, lz) in [ + (0, 2, 0, 2, [0, 0, 0, 2], [1, 2, 0, 0], 1, 1), + (5, 7, 0, 2, [7, 7, 0, 2], [5, 6, 0, 0], 6, 1), + (0, 2, 5, 7, [0, 0, 5, 7], [1, 2, 7, 7], 1, 6), + (5, 7, 5, 7, [7, 7, 5, 7], [5, 6, 7, 7], 6, 6), + ] { + w.bx(xa, 1, za, xb, 1, zb, l)?; + w.bx(xa, 3, za, xb, 3, zb, l)?; + w.bx(gx[0], 2, gx[2], gx[1], 2, gx[3], g)?; + w.bx(gz[0], 2, gz[2], gz[1], 2, gz[3], g)?; + w.put(lamp, lx, 2, lz)?; + } + if r.open[SOUTH] { + w.bx(3, 3, 0, 4, 3, 0, l)?; + } else { + w.bx(3, 3, 0, 4, 3, 1, l)?; + w.bx(3, 2, 0, 4, 2, 0, g)?; + w.bx(3, 1, 0, 4, 1, 1, l)?; + } + if r.open[NORTH] { + w.bx(3, 3, 7, 4, 3, 7, l)?; + } else { + w.bx(3, 3, 6, 4, 3, 7, l)?; + w.bx(3, 2, 7, 4, 2, 7, g)?; + w.bx(3, 1, 6, 4, 1, 7, l)?; + } + if r.open[WEST] { + w.bx(0, 3, 3, 0, 3, 4, l)?; + } else { + w.bx(0, 3, 3, 1, 3, 4, l)?; + w.bx(0, 2, 3, 0, 2, 4, g)?; + w.bx(0, 1, 3, 1, 1, 4, l)?; + } + if r.open[EAST] { + w.bx(7, 3, 3, 7, 3, 4, l)?; + } else { + w.bx(6, 3, 3, 7, 3, 4, l)?; + w.bx(7, 2, 3, 7, 2, 4, g)?; + w.bx(6, 1, 3, 7, 1, 4, l)?; + } + } + 1 => { + for [x, z] in [[2, 2], [2, 5], [5, 5], [5, 2]] { + w.bx(x, 1, z, x, 3, z, l)?; + } + for [x, z] in [[2, 2], [2, 5], [5, 5], [5, 2]] { + w.put(lamp, x, 2, z)?; + } + for [x0, z0, x1, z1] in [ + [0, 0, 1, 0], + [0, 1, 0, 1], + [0, 7, 1, 7], + [0, 6, 0, 6], + [6, 7, 7, 7], + [7, 6, 7, 6], + [6, 0, 7, 0], + [7, 1, 7, 1], + ] { + w.bx(x0, 1, z0, x1, 3, z1, l)?; + } + for [x, z] in [[1, 0], [0, 1], [1, 7], [0, 6], [6, 7], [7, 6], [6, 0], [7, 1]] { + w.put(g, x, 2, z)?; + } + for (side, [x0, z0, x1, z1]) in [ + (SOUTH, [1, 0, 6, 0]), + (NORTH, [1, 7, 6, 7]), + (WEST, [0, 1, 0, 6]), + (EAST, [7, 1, 7, 6]), + ] { + if !r.open[side] { + w.bx(x0, 3, z0, x1, 3, z1, l)?; + w.bx(x0, 2, z0, x1, 2, z1, g)?; + w.bx(x0, 1, z0, x1, 1, z1, l)?; + } + } + } + 2 => { + for (y, b) in [(1, l), (2, k), (3, l)] { + w.bx(0, y, 0, 0, y, 7, b)?; + w.bx(7, y, 0, 7, y, 7, b)?; + w.bx(1, y, 0, 6, y, 0, b)?; + w.bx(1, y, 7, 6, y, 7, b)?; + } + w.bx(0, 1, 3, 0, 2, 4, k)?; + w.bx(7, 1, 3, 7, 2, 4, k)?; + w.bx(3, 1, 0, 4, 2, 0, k)?; + w.bx(3, 1, 7, 4, 2, 7, k)?; + doorways( + w, + &[ + (r.open[SOUTH], [3, 1, 0, 4, 2, 0]), + (r.open[NORTH], [3, 1, 7, 4, 2, 7]), + (r.open[WEST], [0, 1, 3, 0, 2, 4]), + (r.open[EAST], [7, 1, 3, 7, 2, 4]), + ], + )?; + } + _ => {} + } + if pillar { + w.bx(3, 1, 3, 4, 1, 4, l)?; + w.bx(3, 2, 3, 4, 2, 4, g)?; + w.bx(3, 3, 3, 4, 3, 4, l)?; + } + } + Kind::SimpleTop => { + if upper { + w.floor(0, 0, r.open[DOWN])?; + } + if r.conn[UP].is_none() { + w.fill_only(1, 4, 1, 6, 4, 6, g)?; + } + let sponge = w.c.state("wet_sponge"); + for x in 1..=6 { + for z in 1..=6 { + if w.c.random.int(3) != 0 { + let y0 = 2 + if w.c.random.int(4) == 0 { 0 } else { 1 }; + w.bx(x, y0, z, x, 3, z, sponge)?; + } + } + } + for (y, b) in [(1, l), (2, k), (3, l)] { + w.bx(0, y, 0, 0, y, 7, b)?; + w.bx(7, y, 0, 7, y, 7, b)?; + w.bx(1, y, 0, 6, y, 0, b)?; + w.bx(1, y, 7, 6, y, 7, b)?; + } + w.bx(0, 1, 3, 0, 2, 4, k)?; + w.bx(7, 1, 3, 7, 2, 4, k)?; + w.bx(3, 1, 0, 4, 2, 0, k)?; + w.bx(3, 1, 7, 4, 2, 7, k)?; + if r.open[SOUTH] { + w.water(3, 1, 0, 4, 2, 0)?; + } + } + Kind::Wing(0) => { + for i in 0..4 { + w.bx(10 - i, 3 - i, 20 - i, 12 + i, 3 - i, 20, l)?; + } + for [x0, y0, z0, x1, y1, z1] in [ + [7, 0, 6, 15, 0, 16], + [6, 0, 6, 6, 3, 20], + [16, 0, 6, 16, 3, 20], + [7, 1, 7, 7, 1, 20], + [15, 1, 7, 15, 1, 20], + [7, 1, 6, 9, 3, 6], + [13, 1, 6, 15, 3, 6], + [8, 1, 7, 9, 1, 7], + [13, 1, 7, 14, 1, 7], + [9, 0, 5, 13, 0, 5], + ] { + w.bx(x0, y0, z0, x1, y1, z1, l)?; + } + w.bx(10, 0, 7, 12, 0, 7, k)?; + w.bx(8, 0, 10, 8, 0, 12, k)?; + w.bx(14, 0, 10, 14, 0, 12, k)?; + for z in (7..=18).rev().step_by(3) { + w.put(lamp, 6, 3, z)?; + w.put(lamp, 16, 3, z)?; + } + for [x, y, z] in [[10, 0, 10], [12, 0, 10], [10, 0, 12], [12, 0, 12], [8, 3, 6], [14, 3, 6]] { + w.put(lamp, x, y, z)?; + } + for [x, z] in [[4, 4], [18, 4], [4, 18], [18, 18]] { + w.put(l, x, 2, z)?; + w.put(lamp, x, 1, z)?; + w.put(l, x, 0, z)?; + } + w.put(l, 9, 7, 20)?; + w.put(l, 13, 7, 20)?; + w.bx(6, 0, 21, 7, 4, 21, l)?; + w.bx(15, 0, 21, 16, 4, 21, l)?; + } + Kind::Wing(_) => { + w.bx(9, 3, 18, 13, 3, 20, l)?; + w.bx(9, 0, 18, 9, 2, 18, l)?; + w.bx(13, 0, 18, 13, 2, 18, l)?; + for x in [9, 13] { + w.put(l, x, 6, 20)?; + w.put(lamp, x, 5, 20)?; + w.put(l, x, 4, 20)?; + } + w.bx(7, 3, 7, 15, 3, 14, l)?; + for x in [10, 12] { + w.bx(x, 0, 10, x, 6, 10, l)?; + w.bx(x, 0, 12, x, 6, 12, l)?; + w.put(lamp, x, 0, 10)?; + w.put(lamp, x, 0, 12)?; + w.put(lamp, x, 4, 10)?; + w.put(lamp, x, 4, 12)?; + } + for x in [8, 14] { + w.bx(x, 0, 7, x, 2, 7, l)?; + w.bx(x, 0, 14, x, 2, 14, l)?; + } + w.bx(8, 3, 8, 8, 3, 13, k)?; + w.bx(14, 3, 8, 14, 3, 13, k)?; + } + } + Ok(()) +} diff --git a/crates/horizon-world/src/worldgen/structure/processor.rs b/crates/horizon-world/src/worldgen/structure/processor.rs index 4e50654d6..9e084e32d 100644 --- a/crates/horizon-world/src/worldgen/structure/processor.rs +++ b/crates/horizon-world/src/worldgen/structure/processor.rs @@ -86,6 +86,7 @@ pub enum Processor { JigsawReplacement, BlockAge(f32), LavaSubmerged, + BlackstoneReplace, } #[derive(Clone, Debug, Default)] pub struct ProcessorList(pub Vec); @@ -195,6 +196,7 @@ impl Processor { "minecraft:jigsaw_replacement" => Self::JigsawReplacement, "minecraft:block_age" => Self::BlockAge(float(value, "mossiness")?), "minecraft:lava_submerged_block" => Self::LavaSubmerged, + "minecraft:blackstone_replace" => Self::BlackstoneReplace, "minecraft:capped" => Self::Capped { delegate: Box::new(Self::decode(&value["delegate"], resources, bindings)?), limit: IntProvider::from_json(&value["limit"].to_string())?, @@ -344,6 +346,26 @@ impl<'a> PlaceSettings<'a> { } } +/// BlackstoneReplaceProcessor's block map. +fn blackstone(name: &str) -> Option<&'static str> { + Some(match name.strip_prefix("minecraft:")? { + "cobblestone" | "mossy_cobblestone" => "blackstone", + "stone" => "polished_blackstone", + "stone_bricks" | "mossy_stone_bricks" => "polished_blackstone_bricks", + "cobblestone_stairs" | "mossy_cobblestone_stairs" => "blackstone_stairs", + "stone_stairs" => "polished_blackstone_stairs", + "stone_brick_stairs" | "mossy_stone_brick_stairs" => "polished_blackstone_brick_stairs", + "cobblestone_slab" | "mossy_cobblestone_slab" => "blackstone_slab", + "smooth_stone_slab" | "stone_slab" => "polished_blackstone_slab", + "stone_brick_slab" | "mossy_stone_brick_slab" => "polished_blackstone_brick_slab", + "stone_brick_wall" | "mossy_stone_brick_wall" => "polished_blackstone_brick_wall", + "cobblestone_wall" | "mossy_cobblestone_wall" => "blackstone_wall", + "chiseled_stone_bricks" => "chiseled_polished_blackstone", + "cracked_stone_bricks" => "cracked_polished_blackstone_bricks", + "iron_bars" => "iron_chain", + _ => return None, + }) +} type ProcessResult = Result, PlacementQueryError>; #[allow(clippy::too_many_arguments)] fn process_block( @@ -410,6 +432,20 @@ fn process_block( } } Processor::BlockAge(mossiness) => block_age(*mossiness, info, settings, resources), + Processor::BlackstoneReplace => { + let Some(target) = blackstone(name(info.state)) else { + return Ok(Some(info)); + }; + let old = registry.state(info.state); + let properties: Vec<(&str, &str)> = ["facing", "half", "type"] + .into_iter() + .filter_map(|k| Some((k, old?.properties.get(k)?.as_str()))) + .collect(); + Some(BlockInfo { + state: super::legacy::state(resources, target, &properties), + ..info + }) + } Processor::Rule(rules) => { let mut random = LegacyRandom::new(position_seed(BlockPos::new( info.pos[0], diff --git a/crates/horizon-world/src/worldgen/structure/ruined_portal.rs b/crates/horizon-world/src/worldgen/structure/ruined_portal.rs new file mode 100644 index 000000000..b7a1f0064 --- /dev/null +++ b/crates/horizon-world/src/worldgen/structure/ruined_portal.rs @@ -0,0 +1,370 @@ +//! Ruined portals (RuinedPortalStructure, RuinedPortalPiece). +use super::{ + Mirror, Rotation, StructureResources, + legacy::{ + HORIZONTAL, PieceContext, Result, TemplateHandler, TemplatePiece, direction_name, offset, + opposite, + }, + processor::ProcessorList, + start::StartContext, +}; +use crate::{ + heightmap::HeightmapType, + registry::BlockStateId, + worldgen::block_feature::FeatureBlockBindings, +}; +use horizon_core::{geometry::Direction, position::ChunkPos, random::RandomSource}; +use serde_json::{Value, json}; +use std::sync::Arc; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Vertical { + OnLandSurface, + PartlyBuried, + OnOceanFloor, + InMountain, + Underground, + InNether, +} +impl Vertical { + fn heightmap(self) -> HeightmapType { + if self == Self::OnOceanFloor { + HeightmapType::OceanFloorWg + } else { + HeightmapType::WorldSurfaceWg + } + } +} +#[derive(Clone, Debug)] +pub struct Setup { + placement: Vertical, + air_pocket: f32, + mossiness: f32, + overgrown: bool, + vines: bool, + can_be_cold: bool, + blackstone: bool, + weight: f32, +} +impl Setup { + pub fn decode(v: &Value) -> Self { + let float = |k: &str| v[k].as_f64().unwrap_or(0.) as f32; + let flag = |k: &str| v[k].as_bool().unwrap_or(false); + Self { + placement: match v["placement"].as_str().unwrap_or_default() { + "partly_buried" => Vertical::PartlyBuried, + "on_ocean_floor" => Vertical::OnOceanFloor, + "in_mountain" => Vertical::InMountain, + "underground" => Vertical::Underground, + "in_nether" => Vertical::InNether, + _ => Vertical::OnLandSurface, + }, + air_pocket: float("air_pocket_probability"), + mossiness: float("mossiness"), + overgrown: flag("overgrown"), + vines: flag("vines"), + can_be_cold: flag("can_be_cold"), + blackstone: flag("replace_with_blackstone"), + weight: float("weight"), + } + } +} +#[derive(Clone, Copy, Debug)] +pub struct PortalProps { + placement: Vertical, + cold: bool, + overgrown: bool, + vines: bool, +} + +fn between(random: &mut impl RandomSource, min: i32, max: i32) -> i32 { + random.next_int_bound(max - min + 1).unwrap_or(0) + min +} +fn within(random: &mut impl RandomSource, min: i32, max: i32) -> i32 { + if min < max { + between(random, min, max) + } else { + max + } +} + +/// RuinedPortalStructure.findGenerationPoint: the start position and piece. +pub fn start( + context: &mut impl StartContext, + resources: &StructureResources, + bindings: &FeatureBlockBindings<'_>, + random: &mut impl RandomSource, + chunk: ChunkPos, + setups: &[Setup], +) -> Option<([i32; 3], TemplatePiece)> { + let setup = if setups.len() > 1 { + let total: f32 = setups.iter().map(|s| s.weight).sum(); + let mut pick = random.next_float(); + setups.iter().find(|s| { + pick -= s.weight / total; + pick < 0. + })? + } else { + setups.first()? + }; + let air_pocket = match setup.air_pocket { + 0. => false, + 1. => true, + p => random.next_float() < p, + }; + let name = if random.next_float() < 0.05 { + format!( + "minecraft:ruined_portal/giant_portal_{}", + random.next_int_bound(3).ok()? + 1 + ) + } else { + format!( + "minecraft:ruined_portal/portal_{}", + random.next_int_bound(10).ok()? + 1 + ) + }; + let template = resources.template(&name)?; + let rotation = Rotation::random(random); + let mirror = if random.next_float() < 0.5 { + Mirror::None + } else { + Mirror::FrontBack + }; + let pivot = [template.size[0] / 2, 0, template.size[2] / 2]; + let base = [chunk.x * 16, 0, chunk.z * 16]; + let b = template.bounding_box(base, rotation, pivot, mirror); + let center = b.center(); + let surface = context.base_height(center[0], center[2], setup.placement.heightmap()) - 1; + let span = b.span()[1]; + let min_y = context.min_y() + 15; + let new_y = match setup.placement { + Vertical::InNether => { + if air_pocket { + between(random, 32, 100) + } else if random.next_float() < 0.5 { + between(random, 27, 29) + } else { + between(random, 29, 100) + } + } + Vertical::InMountain => within(random, 70, surface - span), + Vertical::Underground => within(random, min_y, surface - span), + Vertical::PartlyBuried => surface - span + between(random, 2, 8), + _ => surface, + }; + let kind = setup.placement.heightmap(); + let columns: Vec> = [ + [b.min[0], b.min[2]], + [b.max[0], b.min[2]], + [b.min[0], b.max[2]], + [b.max[0], b.max[2]], + ] + .into_iter() + .map(|[x, z]| context.base_column(x, z, kind)) + .collect(); + let world_min = context.min_y(); + let mut y = new_y; + 'search: while y > min_y { + let mut solid = 0; + for column in &columns { + let index = y - world_min; + if index >= 0 && column.get(index as usize).copied().unwrap_or(false) { + solid += 1; + if solid == 3 { + break 'search; + } + } + } + y -= 1; + } + let origin = [base[0], y, base[2]]; + let cold = setup.can_be_cold && { + let biome = context.noise_biome(origin.map(|v| v >> 2)); + context.cold_enough_to_snow(biome, origin) + }; + let props = PortalProps { + placement: setup.placement, + cold, + overgrown: setup.overgrown, + vines: setup.vines, + }; + let rule = |source: &str, probability: Option, target: &str| { + let input = match probability { + Some(p) => json!({"predicate_type": "minecraft:random_block_match", "block": source, "probability": p}), + None => json!({"predicate_type": "minecraft:block_match", "block": source}), + }; + json!({"input_predicate": input, "location_predicate": {"predicate_type": "minecraft:always_true"}, "output_state": target}) + }; + let mut rules = vec![rule("minecraft:gold_block", Some(0.3), "minecraft:air")]; + rules.push(if setup.placement == Vertical::OnOceanFloor { + rule("minecraft:lava", None, "minecraft:magma_block") + } else if cold { + rule("minecraft:lava", None, "minecraft:netherrack") + } else { + rule("minecraft:lava", Some(0.2), "minecraft:magma_block") + }); + if !cold { + rules.push(rule("minecraft:netherrack", Some(0.07), "minecraft:magma_block")); + } + let mut list = vec![ + json!({"processor_type": "minecraft:rule", "rules": rules}), + json!({"processor_type": "minecraft:block_age", "mossiness": setup.mossiness}), + json!({"processor_type": "minecraft:protected_blocks", "value": "#minecraft:features_cannot_replace"}), + json!({"processor_type": "minecraft:lava_submerged_block"}), + ]; + if setup.blackstone { + list.push(json!({"processor_type": "minecraft:blackstone_replace"})); + } + let processors = + ProcessorList::decode(&json!({ "processors": list }).to_string(), resources, bindings) + .ok()?; + let mut ignore = vec!["minecraft:structure_block".to_owned()]; + if !air_pocket { + ignore.push("minecraft:air".into()); + } + let piece = TemplatePiece::new( + "minecraft:rupo", + &name, + template, + origin, + rotation, + mirror, + pivot, + Arc::new(processors), + true, + ignore, + TemplateHandler::RuinedPortal(props), + ); + Some((origin, piece)) +} + +const NETHERRACK_BY_DISTANCE: [f32; 14] = [ + 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.9, 0.9, 0.8, 0.7, 0.6, 0.4, 0.2, +]; +fn replaceable(c: &PieceContext<'_>, props: PortalProps, state: BlockStateId) -> bool { + let name = c.name(state); + name != "minecraft:air" + && name != "minecraft:obsidian" + && !c + .resources + .block_tags + .get("minecraft:features_cannot_replace") + .is_some_and(|t| t.contains(name)) + && (props.placement == Vertical::InNether || name != "minecraft:lava") +} +fn netherrack_or_magma(c: &mut PieceContext<'_>, props: PortalProps, pos: [i32; 3]) -> Result<()> { + let state = c.world.block(pos)?; + if replaceable(c, props, state) { + let block = if !props.cold && c.random.float() < 0.07 { + "magma_block" + } else { + "netherrack" + }; + c.world.set(pos, c.state(block), 3)?; + } + Ok(()) +} +fn drip_column(c: &mut PieceContext<'_>, props: PortalProps, mut pos: [i32; 3]) -> Result<()> { + netherrack_or_magma(c, props, pos)?; + let mut remaining = 8; + while remaining > 0 && c.random.float() < 0.5 { + pos[1] -= 1; + remaining -= 1; + netherrack_or_magma(c, props, pos)?; + } + Ok(()) +} +fn leaves_above(c: &mut PieceContext<'_>, pos: [i32; 3]) -> Result<()> { + if c.random.float() < 0.5 && c.is_block(pos, "minecraft:netherrack")? { + let above = [pos[0], pos[1] + 1, pos[2]]; + let state = c.world.block(above)?; + if c.is_air(state) { + let leaves = c.state_with("jungle_leaves", &[("persistent", "true")]); + c.world.set(above, leaves, 3)?; + } + } + Ok(()) +} +fn vines(c: &mut PieceContext<'_>, pos: [i32; 3]) -> Result<()> { + let state = c.world.block(pos)?; + if c.is_air(state) || c.name(state) == "minecraft:vine" { + return Ok(()); + } + let direction = random_horizontal_dyn(c); + let neighbour = offset(pos, direction, 1); + let n = c.world.block(neighbour)?; + if c.is_air(n) && c.world.face_full(pos, direction)? { + let vine = c.state_with("vine", &[(direction_name(opposite(direction)), "true")]); + c.world.set(neighbour, vine, 3)?; + } + Ok(()) +} +fn random_horizontal_dyn(c: &mut PieceContext<'_>) -> Direction { + HORIZONTAL[c.random.int(4) as usize] +} +/// RuinedPortalPiece.postProcess. +pub fn post_process(piece: &TemplatePiece, c: &mut PieceContext<'_>) -> Result<()> { + let TemplateHandler::RuinedPortal(props) = piece.handler else { + return Ok(()); + }; + let b = piece.bounding_box; + if !c.chunk_box.is_inside(b.center()) { + return Ok(()); + } + c.chunk_box.encapsulate(&b); + piece.place_at(c, piece.position, &[])?; + // spreadNetherrack + let follow = matches!( + props.placement, + Vertical::OnLandSurface | Vertical::OnOceanFloor + ); + let [cx, _, cz] = b.center(); + let max = NETHERRACK_BY_DISTANCE.len() as i32; + let [sx, _, sz] = b.span(); + let adjustment = c.random.int((8 - (sx + sz) / 2 / 2).max(1)); + for x in cx - max..=cx + max { + for z in cz - max..=cz + max { + let distance = (x - cx).abs() + (z - cz).abs(); + let adjusted = (distance + adjustment).max(0); + if adjusted >= max { + continue; + } + if c.random.double() < f64::from(NETHERRACK_BY_DISTANCE[adjusted as usize]) { + let surface = c.world.height(props.placement.heightmap(), x, z)? - 1; + let y = if follow { surface } else { b.min[1].min(surface) }; + let pos = [x, y, z]; + let state = c.world.block(pos)?; + if (y - b.min[1]).abs() <= 3 && replaceable(c, props, state) { + netherrack_or_magma(c, props, pos)?; + if props.overgrown { + leaves_above(c, pos)?; + } + drip_column(c, props, [x, y - 1, z])?; + } + } + } + } + // addNetherrackDripColumnsBelowPortal + for x in b.min[0] + 1..b.max[0] { + for z in b.min[2] + 1..b.max[2] { + if c.is_block([x, b.min[1], z], "minecraft:netherrack")? { + drip_column(c, props, [x, b.min[1] - 1, z])?; + } + } + } + if props.vines || props.overgrown { + for z in b.min[2]..=b.max[2] { + for y in b.min[1]..=b.max[1] { + for x in b.min[0]..=b.max[0] { + if props.vines { + vines(c, [x, y, z])?; + } + if props.overgrown { + leaves_above(c, [x, y, z])?; + } + } + } + } + } + Ok(()) +} diff --git a/crates/horizon-world/src/worldgen/structure/simple.rs b/crates/horizon-world/src/worldgen/structure/simple.rs index 31794f9e3..50a402cc3 100644 --- a/crates/horizon-world/src/worldgen/structure/simple.rs +++ b/crates/horizon-world/src/worldgen/structure/simple.rs @@ -197,6 +197,12 @@ impl LegacyPiece for TemplatePiece { fn bounding_box(&self) -> BoundingBox { self.bounding_box } + fn after_place(&self, c: &mut PieceContext<'_>, pieces: &[BoundingBox]) -> Result<()> { + match self.handler { + TemplateHandler::Mansion => super::mansion::after_place(c, pieces), + _ => Ok(()), + } + } fn type_name(&self) -> &'static str { self.type_name } @@ -312,6 +318,12 @@ impl LegacyPiece for TemplatePiece { TemplateHandler::OceanRuin { .. } => { super::ocean_ruin::post_process(self, c)?; } + TemplateHandler::RuinedPortal(_) => { + super::ruined_portal::post_process(self, c)?; + } + TemplateHandler::Mansion => { + super::mansion::post_process(self, c)?; + } } Ok(()) } diff --git a/crates/horizon-world/src/worldgen/structure/start.rs b/crates/horizon-world/src/worldgen/structure/start.rs index 32c838a54..eac73d4e8 100644 --- a/crates/horizon-world/src/worldgen/structure/start.rs +++ b/crates/horizon-world/src/worldgen/structure/start.rs @@ -62,6 +62,12 @@ pub enum StructureKind { BuriedTreasure, Igloo, SwampHut, + DesertPyramid, + JungleTemple, + RuinedPortal(Vec), + Mansion, + /// The biomes allowed around a monument (#required_ocean_monument_surrounding). + Monument(BTreeSet), Shipwreck { beached: bool, }, @@ -175,6 +181,20 @@ impl StructureRegistry { "minecraft:buried_treasure" => StructureKind::BuriedTreasure, "minecraft:igloo" => StructureKind::Igloo, "minecraft:swamp_hut" => StructureKind::SwampHut, + "minecraft:desert_pyramid" => StructureKind::DesertPyramid, + "minecraft:jungle_temple" => StructureKind::JungleTemple, + "minecraft:woodland_mansion" => StructureKind::Mansion, + "minecraft:ocean_monument" => StructureKind::Monument(biome_set( + &serde_json::json!("#minecraft:required_ocean_monument_surrounding"), + )?), + "minecraft:ruined_portal" => StructureKind::RuinedPortal( + v["setups"] + .as_array() + .into_iter() + .flatten() + .map(super::ruined_portal::Setup::decode) + .collect(), + ), "minecraft:shipwreck" => StructureKind::Shipwreck { beached: v["is_beached"].as_bool().unwrap_or(false), }, @@ -395,6 +415,11 @@ pub struct StructureStart { /// Biome and height queries for one structure generation attempt. pub trait StartContext: JigsawContext { fn seed(&self) -> i64; + fn sea_level(&self) -> i32; + /// ChunkGenerator.getBaseColumn as heightmap opacity, indexed from min_y. + fn base_column(&mut self, x: i32, z: i32, kind: HeightmapType) -> Vec; + /// Biome.coldEnoughToSnow at a position. + fn cold_enough_to_snow(&mut self, biome: BiomeId, pos: [i32; 3]) -> bool; fn generation(&self) -> WorldGenerationContext; /// Noise biome at a quart position (caching resolver, no zoom). fn noise_biome(&mut self, quart: [i32; 3]) -> BiomeId; @@ -530,6 +555,81 @@ fn try_generate( let piece = super::simple::SwampHutPiece::new(&mut random, chunk.x * 16, chunk.z * 16); (stub, legacy(vec![piece])) } + StructureKind::DesertPyramid | StructureKind::JungleTemple => { + let desert = matches!(def.kind, StructureKind::DesertPyramid); + let (width, depth) = if desert { (21, 21) } else { (12, 15) }; + let (x, z) = (chunk.x * 16, chunk.z * 16); + if !context.column_has_valid_biome(x + 8, z + 8, context.min_y() - 1, context.max_y()) { + return None; + } + let lowest = corner_heights(context, x, width, z, depth) + .into_iter() + .min() + .unwrap_or(0); + if lowest < context.sea_level() { + return None; + } + let stub = [x + 8, first_occupied(context, x + 8, z + 8), z + 8]; + let piece: Box = if desert { + Box::new(super::temple::DesertPyramidPiece::new(&mut random, x, z)) + } else { + Box::new(super::temple::JungleTemplePiece::new(&mut random, x, z)) + }; + (stub, vec![Piece::Legacy(piece)]) + } + StructureKind::Monument(surrounding) => { + let (x, z) = (chunk.x * 16 + 9, chunk.z * 16 + 9); + let (y, r) = (context.sea_level(), 29); + for qz in (z - r) >> 2..=(z + r) >> 2 { + for qx in (x - r) >> 2..=(x + r) >> 2 { + for qy in (y - r) >> 2..=(y + r) >> 2 { + if !surrounding.contains(&context.noise_biome([qx, qy, qz])) { + return None; + } + } + } + } + let stub = top_of_chunk_center(context, chunk, HeightmapType::OceanFloorWg)?; + let direction = super::legacy::random_horizontal(&mut random); + let piece = super::monument::MonumentBuilding::new( + &mut random, + chunk.x * 16 - 29, + chunk.z * 16 - 29, + direction, + ); + (stub, legacy(vec![piece])) + } + StructureKind::Mansion => { + let (x, z) = (chunk.x * 16 + 7, chunk.z * 16 + 7); + if !context.column_has_valid_biome(x, z, context.min_y() - 1, context.max_y()) { + return None; + } + let rotation = super::Rotation::random(&mut random); + let (sx, sz) = match rotation { + super::Rotation::None => (5, 5), + super::Rotation::Clockwise90 => (-5, 5), + super::Rotation::Clockwise180 => (-5, -5), + super::Rotation::Counterclockwise90 => (5, -5), + }; + let y = corner_heights(context, x, sx, z, sz).into_iter().min().unwrap_or(0); + if y < 60 { + return None; + } + let start = [x, y, z]; + let pieces = super::mansion::pieces(resources, start, rotation, &mut random)?; + (start, legacy(pieces)) + } + StructureKind::RuinedPortal(setups) => { + let (position, piece) = super::ruined_portal::start( + context, + resources, + bindings, + &mut random, + chunk, + setups, + )?; + (position, legacy(vec![piece])) + } StructureKind::Shipwreck { beached } => { let kind = if *beached { HeightmapType::WorldSurfaceWg @@ -771,5 +871,17 @@ pub fn place_in_chunk = start.pieces.iter().map(Piece::bounding_box).collect(); + let mut context = PieceContext { + world, + resources, + random, + chunk_box, + chunk: ChunkPos::new(chunk_box.min[0] >> 4, chunk_box.min[2] >> 4), + reference, + }; + p.after_place(&mut context, &boxes)?; + } Ok(()) } diff --git a/crates/horizon-world/src/worldgen/structure/temple.rs b/crates/horizon-world/src/worldgen/structure/temple.rs new file mode 100644 index 000000000..8bf9ebe95 --- /dev/null +++ b/crates/horizon-world/src/worldgen/structure/temple.rs @@ -0,0 +1,572 @@ +//! Desert pyramids and jungle temples (DesertPyramidPiece, JungleTemplePiece). +use super::{ + BoundingBox, + legacy::{HORIZONTAL, LegacyPiece, PieceContext, Result, random_horizontal}, + simple::Scattered, +}; +use crate::nbt::{Compound, Tag}; +use horizon_core::{ + geometry::Direction, + position::BlockPos, + random::{LegacyRandom, PositionalRandom, RandomSource}, +}; +use std::sync::Mutex; + +#[derive(Debug)] +pub struct DesertPyramidPiece { + pub base: Scattered, + placed_chest: Mutex<[bool; 4]>, +} +impl DesertPyramidPiece { + pub fn new(random: &mut impl RandomSource, west: i32, north: i32) -> Self { + let direction = random_horizontal(random); + Self { + base: Scattered::new(west, 64, north, 21, 15, 21, direction), + placed_chest: Mutex::new([false; 4]), + } + } + /// getPotentialSuspiciousSandWorldPositions, in placement order. + fn sand_positions(&self) -> Vec<[i32; 3]> { + let f = self.base.frame(); + let (x, y, z) = (16, -4, 13); + let mut out = Vec::new(); + for py in y + 1..=y + 3 { + for px in x - 2..=x + 2 { + for pz in z - 2..=z + 2 { + out.push(f.world_pos(px, py, pz)); + } + } + } + for [px, pz] in [[x + 3, z], [x - 3, z], [x, z + 3], [x, z - 3]] { + out.push(f.world_pos(px, y + 1, pz)); + out.push(f.world_pos(px, y + 2, pz)); + } + out + } + fn collapsed_roof(&self, seed: i64) -> [i32; 3] { + let f = self.base.frame(); + let [x0, y0, z0] = f.world_pos(14, 0, 11); + let mut random = LegacyRandom::new(seed) + .fork_positional() + .at(BlockPos::new(x0, y0, z0)); + let rx = 14 + random.next_int_bound(5).unwrap_or(0); + let rz = 11 + random.next_int_bound(5).unwrap_or(0); + f.world_pos(rx, 0, rz) + } +} +pub fn union(boxes: &[BoundingBox]) -> BoundingBox { + let mut out = boxes[0]; + for b in &boxes[1..] { + out.encapsulate(b); + } + out +} +fn suspicious_sand(c: &mut PieceContext<'_>, pos: [i32; 3]) -> Result<()> { + if c.chunk_box.is_inside(pos) { + c.world.set(pos, c.state("suspicious_sand"), 2)?; + let mut nbt = Compound::new(); + nbt.insert("id".into(), Tag::String("minecraft:brushable_block".into())); + nbt.insert( + "LootTable".into(), + Tag::String("minecraft:archaeology/desert_pyramid".into()), + ); + nbt.insert( + "LootTableSeed".into(), + Tag::Long(BlockPos::new(pos[0], pos[1], pos[2]).pack()), + ); + c.world.block_entity(pos, nbt)?; + } + Ok(()) +} +impl LegacyPiece for DesertPyramidPiece { + fn bounding_box(&self) -> BoundingBox { + self.base.frame().bounding_box + } + fn type_name(&self) -> &'static str { + "minecraft:tedp" + } + fn post_process(&self, c: &mut PieceContext<'_>) -> Result<()> { + let offset = -c.random.int(3); + if !self.base.lowest_ground(c, offset)? { + return Ok(()); + } + let f = self.base.frame(); + let (w, d) = (21, 21); + let sandstone = c.state("sandstone"); + let cut = c.state("cut_sandstone"); + let chiseled = c.state("chiseled_sandstone"); + let air = c.state("air"); + let orange = c.state("orange_terracotta"); + let blue = c.state("blue_terracotta"); + let stairs = |c: &PieceContext<'_>, facing: &str| { + c.state_with("sandstone_stairs", &[("facing", facing)]) + }; + let north = stairs(c, "north"); + let south = stairs(c, "south"); + let east = stairs(c, "east"); + let west = stairs(c, "west"); + f.generate_box(c, [0, -4, 0], [w - 1, 0, d - 1], sandstone, sandstone, false)?; + for p in 1..=9 { + f.generate_box(c, [p, p, p], [w - 1 - p, p, d - 1 - p], sandstone, sandstone, false)?; + f.generate_box(c, [p + 1, p, p + 1], [w - 2 - p, p, d - 2 - p], air, air, false)?; + } + for x in 0..w { + for z in 0..d { + f.fill_column_down(c, sandstone, x, -5, z)?; + } + } + f.generate_box(c, [0, 0, 0], [4, 9, 4], sandstone, air, false)?; + f.generate_box(c, [1, 10, 1], [3, 10, 3], sandstone, sandstone, false)?; + f.place(c, north, 2, 10, 0)?; + f.place(c, south, 2, 10, 4)?; + f.place(c, east, 0, 10, 2)?; + f.place(c, west, 4, 10, 2)?; + f.generate_box(c, [w - 5, 0, 0], [w - 1, 9, 4], sandstone, air, false)?; + f.generate_box(c, [w - 4, 10, 1], [w - 2, 10, 3], sandstone, sandstone, false)?; + f.place(c, north, w - 3, 10, 0)?; + f.place(c, south, w - 3, 10, 4)?; + f.place(c, east, w - 5, 10, 2)?; + f.place(c, west, w - 1, 10, 2)?; + f.generate_box(c, [8, 0, 0], [12, 4, 4], sandstone, air, false)?; + f.generate_box(c, [9, 1, 0], [11, 3, 4], air, air, false)?; + for [x, y] in [[9, 1], [9, 2], [9, 3], [10, 3], [11, 3], [11, 2], [11, 1]] { + f.place(c, cut, x, y, 1)?; + } + f.generate_box(c, [4, 1, 1], [8, 3, 3], sandstone, air, false)?; + f.generate_box(c, [4, 1, 2], [8, 2, 2], air, air, false)?; + f.generate_box(c, [12, 1, 1], [16, 3, 3], sandstone, air, false)?; + f.generate_box(c, [12, 1, 2], [16, 2, 2], air, air, false)?; + f.generate_box(c, [5, 4, 5], [w - 6, 4, d - 6], sandstone, sandstone, false)?; + f.generate_box(c, [9, 4, 9], [11, 4, 11], air, air, false)?; + for [x, z] in [[8, 8], [12, 8], [8, 12], [12, 12]] { + f.generate_box(c, [x, 1, z], [x, 3, z], cut, cut, false)?; + } + f.generate_box(c, [1, 1, 5], [4, 4, 11], sandstone, sandstone, false)?; + f.generate_box(c, [w - 5, 1, 5], [w - 2, 4, 11], sandstone, sandstone, false)?; + f.generate_box(c, [6, 7, 9], [6, 7, 11], sandstone, sandstone, false)?; + f.generate_box(c, [w - 7, 7, 9], [w - 7, 7, 11], sandstone, sandstone, false)?; + f.generate_box(c, [5, 5, 9], [5, 7, 11], cut, cut, false)?; + f.generate_box(c, [w - 6, 5, 9], [w - 6, 7, 11], cut, cut, false)?; + f.place(c, air, 5, 5, 10)?; + f.place(c, air, 5, 6, 10)?; + f.place(c, air, 6, 6, 10)?; + f.place(c, air, w - 6, 5, 10)?; + f.place(c, air, w - 6, 6, 10)?; + f.place(c, air, w - 7, 6, 10)?; + f.generate_box(c, [2, 4, 4], [2, 6, 4], air, air, false)?; + f.generate_box(c, [w - 3, 4, 4], [w - 3, 6, 4], air, air, false)?; + f.place(c, north, 2, 4, 5)?; + f.place(c, north, 2, 3, 4)?; + f.place(c, north, w - 3, 4, 5)?; + f.place(c, north, w - 3, 3, 4)?; + f.generate_box(c, [1, 1, 3], [2, 2, 3], sandstone, sandstone, false)?; + f.generate_box(c, [w - 3, 1, 3], [w - 2, 2, 3], sandstone, sandstone, false)?; + f.place(c, sandstone, 1, 1, 2)?; + f.place(c, sandstone, w - 2, 1, 2)?; + let slab = c.state("sandstone_slab"); + f.place(c, slab, 1, 2, 2)?; + f.place(c, slab, w - 2, 2, 2)?; + f.place(c, west, 2, 1, 2)?; + f.place(c, east, w - 3, 1, 2)?; + f.generate_box(c, [4, 3, 5], [4, 3, 17], sandstone, sandstone, false)?; + f.generate_box(c, [w - 5, 3, 5], [w - 5, 3, 17], sandstone, sandstone, false)?; + f.generate_box(c, [3, 1, 5], [4, 2, 16], air, air, false)?; + f.generate_box(c, [w - 6, 1, 5], [w - 5, 2, 16], air, air, false)?; + for z in (5..=17).step_by(2) { + f.place(c, cut, 4, 1, z)?; + f.place(c, chiseled, 4, 2, z)?; + f.place(c, cut, w - 5, 1, z)?; + f.place(c, chiseled, w - 5, 2, z)?; + } + for [x, z] in [ + [10, 7], + [10, 8], + [9, 9], + [11, 9], + [8, 10], + [12, 10], + [7, 10], + [13, 10], + [9, 11], + [11, 11], + [10, 12], + [10, 13], + ] { + f.place(c, orange, x, 0, z)?; + } + f.place(c, blue, 10, 0, 10)?; + // Rows of the corner towers (y 2..=8, three blocks each). + let rows = [ + [cut, orange, cut], + [cut, orange, cut], + [orange, chiseled, orange], + [cut, orange, cut], + [orange, chiseled, orange], + [orange, orange, orange], + [cut, cut, cut], + ]; + for x in [0, w - 1] { + for (i, row) in rows.iter().enumerate() { + for (j, &state) in row.iter().enumerate() { + f.place(c, state, x, 2 + i as i32, 1 + j as i32)?; + } + } + } + for x in [2, w - 3] { + for (i, row) in rows.iter().enumerate() { + for (j, &state) in row.iter().enumerate() { + f.place(c, state, x - 1 + j as i32, 2 + i as i32, 0)?; + } + } + } + f.generate_box(c, [8, 4, 0], [12, 6, 0], cut, cut, false)?; + f.place(c, air, 8, 6, 0)?; + f.place(c, air, 12, 6, 0)?; + f.place(c, orange, 9, 5, 0)?; + f.place(c, chiseled, 10, 5, 0)?; + f.place(c, orange, 11, 5, 0)?; + f.generate_box(c, [8, -14, 8], [12, -11, 12], cut, cut, false)?; + f.generate_box(c, [8, -10, 8], [12, -10, 12], chiseled, chiseled, false)?; + f.generate_box(c, [8, -9, 8], [12, -9, 12], cut, cut, false)?; + f.generate_box(c, [8, -8, 8], [12, -1, 12], sandstone, sandstone, false)?; + f.generate_box(c, [9, -11, 9], [11, -1, 11], air, air, false)?; + f.place(c, c.state("stone_pressure_plate"), 10, -11, 10)?; + f.generate_box(c, [9, -13, 9], [11, -13, 11], c.state("tnt"), air, false)?; + for [x, z, ox, oz] in [[8, 10, 7, 10], [12, 10, 13, 10], [10, 8, 10, 7], [10, 12, 10, 13]] { + f.place(c, air, x, -11, z)?; + f.place(c, air, x, -10, z)?; + f.place(c, chiseled, ox, -10, oz)?; + f.place(c, cut, ox, -11, oz)?; + } + for (i, d) in HORIZONTAL.into_iter().enumerate() { + if !self.placed_chest.lock().unwrap()[i] { + let s = d.normal(); + let placed = f.create_chest( + c, + 10 + s[0] * 2, + -11, + 10 + s[2] * 2, + "minecraft:chests/desert_pyramid", + )?; + self.placed_chest.lock().unwrap()[i] = placed; + } + } + // addCellar: stairs, then the room. + let (x, y, z) = (16, -4, 13); + let ccw = stairs(c, "west"); + f.place(c, ccw, 13, -1, 17)?; + f.place(c, ccw, 14, -2, 17)?; + f.place(c, ccw, 15, -3, 17)?; + let sand = c.state("sand"); + let variant = c.world.level_boolean()?; + for dx in -4..=0 { + f.place(c, sand, x + dx, y + 4, z + 4)?; + } + f.place(c, sand, x - 2, y + 3, z + 4)?; + f.place(c, if variant { sand } else { sandstone }, x - 1, y + 3, z + 4)?; + f.place(c, if variant { sandstone } else { sand }, x, y + 3, z + 4)?; + f.place(c, sand, x - 1, y + 2, z + 4)?; + f.place(c, sandstone, x, y + 2, z + 4)?; + f.place(c, sand, x, y + 1, z + 4)?; + for (state, yy) in [(cut, y + 1), (chiseled, y + 2), (cut, -1)] { + f.generate_box(c, [x - 3, yy, z - 3], [x - 3, yy, z + 2], state, state, true)?; + f.generate_box(c, [x + 3, yy, z - 3], [x + 3, yy, z + 2], state, state, true)?; + f.generate_box(c, [x - 3, yy, z - 3], [x + 3, yy, z - 2], state, state, true)?; + f.generate_box(c, [x - 3, yy, z + 3], [x + 3, yy, z + 3], state, state, true)?; + } + for px in x - 2..=x + 2 { + for pz in z - 2..=z + 2 { + let state = if c.world.level_float()? < 0.33 { + sandstone + } else { + sand + }; + f.place(c, state, px, y + 4, pz)?; + } + } + f.place(c, blue, x, y, z)?; + for [dx, dz] in [[1, -1], [1, 1], [-1, -1], [-1, 1], [2, 0], [-2, 0], [0, 2], [0, -2]] { + f.place(c, orange, x + dx, y, z + dz)?; + } + f.place(c, orange, x + 3, y, z)?; + f.place(c, cut, x + 4, y + 1, z)?; + f.place(c, chiseled, x + 4, y + 2, z)?; + f.place(c, orange, x - 3, y, z)?; + f.place(c, cut, x - 4, y + 1, z)?; + f.place(c, chiseled, x - 4, y + 2, z)?; + f.place(c, orange, x, y, z + 3)?; + f.place(c, orange, x, y, z - 3)?; + f.place(c, cut, x, y + 1, z - 4)?; + f.place(c, chiseled, x, -2, z - 4)?; + Ok(()) + } + /// DesertPyramidStructure.afterPlace. + fn after_place(&self, c: &mut PieceContext<'_>, pieces: &[BoundingBox]) -> Result<()> { + if *self.base.height_position.lock().unwrap() < 0 { + return Ok(()); + } + let seed = c.world.seed()?; + let mut positions = self.sand_positions(); + positions.sort_by_key(|p| (p[1], p[2], p[0])); + positions.dedup(); + suspicious_sand(c, self.collapsed_roof(seed))?; + let center = union(pieces).center(); + let mut random = LegacyRandom::new(seed) + .fork_positional() + .at(BlockPos::new(center[0], center[1], center[2])); + for i in (2..=positions.len()).rev() { + let j = random.next_int_bound(i as i32).unwrap_or(0) as usize; + positions.swap(i - 1, j); + } + let mut remaining = (positions.len() as i32).min(5 + random.next_int_bound(3).unwrap_or(0)); + let sand = c.state("sand"); + for pos in positions { + if remaining > 0 { + remaining -= 1; + suspicious_sand(c, pos)?; + } else if c.chunk_box.is_inside(pos) { + c.world.set(pos, sand, 2)?; + } + } + Ok(()) + } +} + +#[derive(Debug)] +pub struct JungleTemplePiece { + pub base: Scattered, + /// placedMainChest, placedHiddenChest, placedTrap1, placedTrap2. + placed: Mutex<[bool; 4]>, +} +impl JungleTemplePiece { + pub fn new(random: &mut impl RandomSource, west: i32, north: i32) -> Self { + let direction = random_horizontal(random); + Self { + base: Scattered::new(west, 64, north, 12, 10, 15, direction), + placed: Mutex::new([false; 4]), + } + } +} +impl LegacyPiece for JungleTemplePiece { + fn bounding_box(&self) -> BoundingBox { + self.base.frame().bounding_box + } + fn type_name(&self) -> &'static str { + "minecraft:tejp" + } + fn post_process(&self, c: &mut PieceContext<'_>) -> Result<()> { + if !self.base.average_ground(c, 0)? { + return Ok(()); + } + let f = self.base.frame(); + let cobble = c.state("cobblestone"); + let mossy = c.state("mossy_cobblestone"); + let air = c.state("air"); + let stone = |c: &mut PieceContext<'_>, a: [i32; 3], b: [i32; 3]| { + f.generate_box_selector(c, a, b, false, &mut |c, _, _, _, _| { + if c.random.float() < 0.4 { cobble } else { mossy } + }) + }; + stone(c, [0, -4, 0], [11, 0, 14])?; + for (a, b) in [ + ([2, 1, 2], [9, 2, 2]), + ([2, 1, 12], [9, 2, 12]), + ([2, 1, 3], [2, 2, 11]), + ([9, 1, 3], [9, 2, 11]), + ([1, 3, 1], [10, 6, 1]), + ([1, 3, 13], [10, 6, 13]), + ([1, 3, 2], [1, 6, 12]), + ([10, 3, 2], [10, 6, 12]), + ([2, 3, 2], [9, 3, 12]), + ([2, 6, 2], [9, 6, 12]), + ([3, 7, 3], [8, 7, 11]), + ([4, 8, 4], [7, 8, 10]), + ] { + stone(c, a, b)?; + } + for (a, b) in [ + ([3, 1, 3], [8, 2, 11]), + ([4, 3, 6], [7, 3, 9]), + ([2, 4, 2], [9, 5, 12]), + ([4, 6, 5], [7, 6, 9]), + ([5, 7, 6], [6, 7, 8]), + ([5, 1, 2], [6, 2, 2]), + ([5, 2, 12], [6, 2, 12]), + ([5, 5, 1], [6, 5, 1]), + ([5, 5, 13], [6, 5, 13]), + ] { + f.generate_box(c, a, b, air, air, false)?; + } + f.place(c, air, 1, 5, 5)?; + f.place(c, air, 10, 5, 5)?; + f.place(c, air, 1, 5, 9)?; + f.place(c, air, 10, 5, 9)?; + for z in [0, 14] { + for x in [2, 4, 7, 9] { + stone(c, [x, 4, z], [x, 5, z])?; + } + } + stone(c, [5, 6, 0], [6, 6, 0])?; + for x in [0, 11] { + for z in (2..=12).step_by(2) { + stone(c, [x, 4, z], [x, 5, z])?; + } + stone(c, [x, 6, 5], [x, 6, 5])?; + stone(c, [x, 6, 9], [x, 6, 9])?; + } + for (a, b) in [ + ([2, 7, 2], [2, 9, 2]), + ([9, 7, 2], [9, 9, 2]), + ([2, 7, 12], [2, 9, 12]), + ([9, 7, 12], [9, 9, 12]), + ([4, 9, 4], [4, 9, 4]), + ([7, 9, 4], [7, 9, 4]), + ([4, 9, 10], [4, 9, 10]), + ([7, 9, 10], [7, 9, 10]), + ([5, 9, 7], [6, 9, 7]), + ] { + stone(c, a, b)?; + } + let stairs = + |c: &PieceContext<'_>, facing: &str| c.state_with("cobblestone_stairs", &[("facing", facing)]); + let east = stairs(c, "east"); + let west = stairs(c, "west"); + let south = stairs(c, "south"); + let north = stairs(c, "north"); + f.place(c, north, 5, 9, 6)?; + f.place(c, north, 6, 9, 6)?; + f.place(c, south, 5, 9, 8)?; + f.place(c, south, 6, 9, 8)?; + for [x, y, z] in [ + [4, 0, 0], + [5, 0, 0], + [6, 0, 0], + [7, 0, 0], + [4, 1, 8], + [4, 2, 9], + [4, 3, 10], + [7, 1, 8], + [7, 2, 9], + [7, 3, 10], + ] { + f.place(c, north, x, y, z)?; + } + stone(c, [4, 1, 9], [4, 1, 9])?; + stone(c, [7, 1, 9], [7, 1, 9])?; + stone(c, [4, 1, 10], [7, 2, 10])?; + stone(c, [5, 4, 5], [6, 4, 5])?; + f.place(c, east, 4, 4, 5)?; + f.place(c, west, 7, 4, 5)?; + for i in 0..4 { + f.place(c, south, 5, -i, 6 + i)?; + f.place(c, south, 6, -i, 6 + i)?; + f.generate_box(c, [5, -i, 7 + i], [6, -i, 9 + i], air, air, false)?; + } + f.generate_box(c, [1, -3, 12], [10, -1, 13], air, air, false)?; + f.generate_box(c, [1, -3, 1], [3, -1, 13], air, air, false)?; + f.generate_box(c, [1, -3, 1], [9, -1, 5], air, air, false)?; + for z in (1..=13).step_by(2) { + stone(c, [1, -3, z], [1, -2, z])?; + } + for z in (2..=12).step_by(2) { + stone(c, [1, -1, z], [3, -1, z])?; + } + stone(c, [2, -2, 1], [5, -2, 1])?; + stone(c, [7, -2, 1], [9, -2, 1])?; + stone(c, [6, -3, 1], [6, -3, 1])?; + stone(c, [6, -1, 1], [6, -1, 1])?; + let hook = |c: &PieceContext<'_>, facing: &str| { + c.state_with("tripwire_hook", &[("facing", facing), ("attached", "true")]) + }; + let wire = |c: &PieceContext<'_>, a: &str, b: &str| { + c.state_with("tripwire", &[(a, "true"), (b, "true"), ("attached", "true")]) + }; + let redstone = |c: &PieceContext<'_>, sides: &[(&str, &str)]| c.state_with("redstone_wire", sides); + f.place(c, hook(c, "east"), 1, -3, 8)?; + f.place(c, hook(c, "west"), 4, -3, 8)?; + f.place(c, wire(c, "east", "west"), 2, -3, 8)?; + f.place(c, wire(c, "east", "west"), 3, -3, 8)?; + let ns = redstone(c, &[("north", "side"), ("south", "side")]); + for z in (2..=7).rev() { + f.place(c, ns, 5, -3, z)?; + } + f.place(c, redstone(c, &[("north", "side"), ("west", "side")]), 5, -3, 1)?; + f.place(c, redstone(c, &[("east", "side"), ("west", "side")]), 4, -3, 1)?; + f.place(c, mossy, 3, -3, 1)?; + let dispenser = "minecraft:chests/jungle_temple_dispenser"; + if !self.placed.lock().unwrap()[2] { + let placed = f.create_dispenser(c, 3, -2, 1, Direction::North, dispenser)?; + self.placed.lock().unwrap()[2] = placed; + } + f.place(c, c.state_with("vine", &[("south", "true")]), 3, -2, 2)?; + f.place(c, hook(c, "north"), 7, -3, 1)?; + f.place(c, hook(c, "south"), 7, -3, 5)?; + for z in 2..=4 { + f.place(c, wire(c, "north", "south"), 7, -3, z)?; + } + f.place(c, redstone(c, &[("east", "side"), ("west", "side")]), 8, -3, 6)?; + f.place(c, redstone(c, &[("west", "side"), ("south", "side")]), 9, -3, 6)?; + f.place(c, redstone(c, &[("north", "side"), ("south", "up")]), 9, -3, 5)?; + f.place(c, mossy, 9, -3, 4)?; + f.place(c, ns, 9, -2, 4)?; + if !self.placed.lock().unwrap()[3] { + let placed = f.create_dispenser(c, 9, -2, 3, Direction::West, dispenser)?; + self.placed.lock().unwrap()[3] = placed; + } + let vine_east = c.state_with("vine", &[("east", "true")]); + f.place(c, vine_east, 8, -1, 3)?; + f.place(c, vine_east, 8, -2, 3)?; + if !self.placed.lock().unwrap()[0] { + let placed = f.create_chest(c, 8, -3, 3, "minecraft:chests/jungle_temple")?; + self.placed.lock().unwrap()[0] = placed; + } + for [x, y, z] in [ + [9, -3, 2], + [8, -3, 1], + [4, -3, 5], + [5, -2, 5], + [5, -1, 5], + [6, -3, 5], + [7, -2, 5], + [7, -1, 5], + [8, -3, 5], + ] { + f.place(c, mossy, x, y, z)?; + } + stone(c, [9, -1, 1], [9, -1, 5])?; + f.generate_box(c, [8, -3, 8], [10, -1, 10], air, air, false)?; + let chiseled = c.state("chiseled_stone_bricks"); + let lever = c.state_with("lever", &[("facing", "north"), ("face", "wall")]); + for x in 8..=10 { + f.place(c, chiseled, x, -2, 11)?; + } + for x in 8..=10 { + f.place(c, lever, x, -2, 12)?; + } + stone(c, [8, -3, 8], [8, -3, 10])?; + stone(c, [10, -3, 8], [10, -3, 10])?; + f.place(c, mossy, 10, -2, 9)?; + f.place(c, ns, 8, -2, 9)?; + f.place(c, ns, 8, -2, 10)?; + f.place( + c, + redstone( + c, + &[("north", "side"), ("south", "side"), ("east", "side"), ("west", "side")], + ), + 10, + -1, + 9, + )?; + f.place(c, c.state_with("sticky_piston", &[("facing", "up")]), 9, -2, 8)?; + let piston_west = c.state_with("sticky_piston", &[("facing", "west")]); + f.place(c, piston_west, 10, -2, 8)?; + f.place(c, piston_west, 10, -1, 8)?; + f.place(c, c.state_with("repeater", &[("facing", "north")]), 10, -2, 10)?; + if !self.placed.lock().unwrap()[1] { + let placed = f.create_chest(c, 9, -3, 10, "minecraft:chests/jungle_temple")?; + self.placed.lock().unwrap()[1] = placed; + } + Ok(()) + } +}