diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 8de66667b..a95a21e43 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -27,6 +27,7 @@ mod fov; mod frames; mod game_mode; mod ghasts; +mod golems; mod growth; mod jukebox; mod leads; @@ -1371,6 +1372,14 @@ impl Game<'_> { kind: "hatch_sniffer", .. } => self.hatch_sniffer([pos.x, pos.y, pos.z]), + LevelEvent::BlockEvent { + pos, + kind: kind @ ("spawn_snow_golem" | "spawn_iron_golem" | "spawn_copper_golem"), + .. + } => self.spawn_built_golem( + [pos.x, pos.y, pos.z], + &format!("minecraft:{}", &kind["spawn_".len()..]), + ), LevelEvent::BlockEvent { pos, state, diff --git a/crates/horizon-client/src/game/golems.rs b/crates/horizon-client/src/game/golems.rs new file mode 100644 index 000000000..c50e0605c --- /dev/null +++ b/crates/horizon-client/src/game/golems.rs @@ -0,0 +1,15 @@ +//! Golems built from blocks: a carved pumpkin on snow blocks, an iron T or a +//! copper block (`CarvedPumpkinBlock.trySpawnGolem`). +use super::Game; + +impl Game<'_> { + /// Brings a freshly built golem to life at the base block of its pattern. + pub(super) fn spawn_built_golem(&mut self, pos: [i32; 3], kind: &str) { + let at = [ + f64::from(pos[0]) + 0.5, + f64::from(pos[1]) + 0.05, + f64::from(pos[2]) + 0.5, + ]; + let _ = self.spawn_mob(kind, at, true); + } +} diff --git a/crates/horizon-world/src/level/golems.rs b/crates/horizon-world/src/level/golems.rs new file mode 100644 index 000000000..e0ed881c1 --- /dev/null +++ b/crates/horizon-world/src/level/golems.rs @@ -0,0 +1,161 @@ +//! `CarvedPumpkinBlock.trySpawnGolem`: a carved pumpkin on top of snow +//! blocks, an iron T or a copper block brings a golem to life. The block +//! patterns match in every orientation, like vanilla's `BlockPattern`. +use super::{LevelAccess, LevelEvent, Sim, update::rel}; +use crate::registry::BlockStateId; +use horizon_core::{geometry::Direction, position::BlockPos}; + +/// The blocks of a matched pattern, rows top to bottom. +struct Match { + cells: Vec>, +} +impl Match { + fn at(&self, col: usize, row: usize) -> BlockPos { + self.cells[row][col] + } +} +#[derive(Clone, Copy, PartialEq, Eq)] +enum Body { + Snow, + Iron, + Copper, +} + +impl Sim<'_, L> { + fn is_pumpkin(&self, state: BlockStateId) -> bool { + matches!( + self.b.table.name(state), + "minecraft:carved_pumpkin" | "minecraft:jack_o_lantern" + ) + } + fn is_golem_body(&self, state: BlockStateId, body: Body) -> bool { + match body { + Body::Snow => self.b.table.name(state) == "minecraft:snow_block", + Body::Iron => self.b.table.name(state) == "minecraft:iron_block", + Body::Copper => self + .b + .tag("minecraft:copper") + .is_some_and(|set| self.b.in_set(state, set)), + } + } + /// `BlockPattern.find` for a pattern whose only pumpkin is `top`: + /// `rows` read top to bottom, `^` the pumpkin, `#` the body, `~` air. + fn find_golem(&self, rows: &[&str], body: Body, top: BlockPos) -> Option { + let pumpkin_col = rows[0].find('^')? as i32; + for up in Direction::ALL { + for finger in Direction::ALL { + if finger == up || finger == up.opposite() { + continue; + } + let step = |origin: BlockPos, col: i32, row: i32| { + let mut p = origin; + for _ in 0..col { + p = rel(p, finger); + } + for _ in 0..row { + p = rel(p, up.opposite()); + } + p + }; + let mut origin = top; + for _ in 0..pumpkin_col { + origin = rel(origin, finger.opposite()); + } + let mut cells = Vec::new(); + let mut ok = true; + 'rows: for (r, row) in rows.iter().enumerate() { + let mut line = Vec::new(); + for (c, ch) in row.chars().enumerate() { + let pos = step(origin, c as i32, r as i32); + let state = self.state(pos); + let good = match ch { + '^' => self.is_pumpkin(state), + '#' => self.is_golem_body(state, body), + '~' => self.b.is_air(state), + _ => true, + }; + if !good { + ok = false; + break 'rows; + } + line.push(pos); + } + cells.push(line); + } + if ok { + return Some(Match { cells }); + } + } + } + None + } + fn clear_golem_blocks(&mut self, found: &Match) { + let air = self.b.default_of(self.b.named.air); + for row in &found.cells { + for &pos in row { + let old = self.state(pos); + self.set_block(pos, air, 2); + self.level_event(2001, pos, old.0 as i32); + } + } + } + fn update_golem_blocks(&mut self, found: &Match) { + let air = self.b.named.air; + for row in &found.cells { + for &pos in row { + self.update_neighbors_at(pos, air); + } + } + } + /// Called when a carved pumpkin or jack o'lantern is placed at `top`. + pub(crate) fn carved_pumpkin_try_spawn_golem(&mut self, top: BlockPos) { + if let Some(found) = self.find_golem(&["^", "#", "#"], Body::Snow, top) { + let spawn = found.at(0, 2); + let old = self.state(spawn); + self.clear_golem_blocks(&found); + self.emit(LevelEvent::BlockEvent { + pos: spawn, + state: old, + kind: "spawn_snow_golem", + }); + self.update_golem_blocks(&found); + return; + } + if let Some(found) = self.find_golem(&["~^~", "###", "~#~"], Body::Iron, top) { + let spawn = found.at(1, 2); + let old = self.state(spawn); + self.clear_golem_blocks(&found); + self.emit(LevelEvent::BlockEvent { + pos: spawn, + state: old, + kind: "spawn_iron_golem", + }); + self.update_golem_blocks(&found); + return; + } + if let Some(found) = self.find_golem(&["^", "#"], Body::Copper, top) { + let pumpkin = self.state(found.at(0, 0)); + let copper_pos = found.at(0, 1); + let copper = self.state(copper_pos); + let facing = self.b.facing(pumpkin); + self.clear_golem_blocks(&found); + self.emit(LevelEvent::BlockEvent { + pos: found.at(0, 0), + state: copper, + kind: "spawn_copper_golem", + }); + self.update_golem_blocks(&found); + // CopperChestBlock.getFromCopperBlock. + let name = self.b.table.name(copper).trim_start_matches("minecraft:"); + let chest = match name.strip_suffix("_block") { + Some(stem) => format!("{stem}_chest"), + None => format!("{name}_chest"), + }; + if let Some(id) = self.b.table.block_by_name(&format!("minecraft:{chest}")) { + let state = self.b.default_of(id); + let state = self.b.table.set_dir(state, self.b.props.facing, facing); + self.set_block(copper_pos, state, 2); + } + } + } +} diff --git a/crates/horizon-world/src/level/growth_tests.rs b/crates/horizon-world/src/level/growth_tests.rs index ad66b5718..774a588ea 100644 --- a/crates/horizon-world/src/level/growth_tests.rs +++ b/crates/horizon-world/src/level/growth_tests.rs @@ -447,3 +447,55 @@ fn sniffer_eggs_crack_twice_then_hatch_a_snifflet() { } ))); } +#[test] +fn a_pumpkin_on_a_copper_block_makes_a_copper_golem_and_a_chest() { + let Some(f) = fixture() else { return }; + let mut w = World::new(f, 5); + w.floor("stone", 1); + let copper = w.id("weathered_copper", &[]); + let pos = BlockPos::new(0, 64, 0); + w.put(pos, copper); + let pumpkin = w.id("carved_pumpkin", &[("facing", "east")]); + f.behaviours.set_block( + &mut w.level, + &mut w.ticks, + pos.offset(0, 1, 0), + pumpkin, + flags::UPDATE_ALL, + ); + assert_eq!(w.name(pos.offset(0, 1, 0)), "minecraft:air"); + assert_eq!(w.name(pos), "minecraft:weathered_copper_chest"); + assert!(w.level.events.iter().any(|e| matches!( + e, + LevelEvent::BlockEvent { + kind: "spawn_copper_golem", + .. + } + ))); +} +#[test] +fn a_pumpkin_on_an_iron_t_makes_an_iron_golem() { + let Some(f) = fixture() else { return }; + let mut w = World::new(f, 5); + w.floor("stone", 2); + let iron = w.id("iron_block", &[]); + for (x, y) in [(-1, 65), (0, 65), (1, 65), (0, 64)] { + w.put(BlockPos::new(x, y, 0), iron); + } + let pumpkin = w.id("carved_pumpkin", &[("facing", "north")]); + f.behaviours.set_block( + &mut w.level, + &mut w.ticks, + BlockPos::new(0, 66, 0), + pumpkin, + flags::UPDATE_ALL, + ); + assert_eq!(w.name(BlockPos::new(0, 65, 0)), "minecraft:air"); + assert!(w.level.events.iter().any(|e| matches!( + e, + LevelEvent::BlockEvent { + kind: "spawn_iron_golem", + .. + } + ))); +} diff --git a/crates/horizon-world/src/level/hooks.rs b/crates/horizon-world/src/level/hooks.rs index d663e5c84..8a7074004 100644 --- a/crates/horizon-world/src/level/hooks.rs +++ b/crates/horizon-world/src/level/hooks.rs @@ -96,6 +96,11 @@ impl Sim<'_, L> { Class::Brushable => { self.schedule_tick(pos, block, 2); } + Class::CarvedPumpkin => { + if !same { + self.carved_pumpkin_try_spawn_golem(pos); + } + } Class::Composter => { if b.table.int(state, b.props.level) == 7 { self.schedule_tick(pos, block, 20); diff --git a/crates/horizon-world/src/level/mod.rs b/crates/horizon-world/src/level/mod.rs index 1a066cafc..53f0d0b33 100644 --- a/crates/horizon-world/src/level/mod.rs +++ b/crates/horizon-world/src/level/mod.rs @@ -104,6 +104,7 @@ mod bonemeal; mod falling; mod feature_world; mod fluids; +mod golems; mod growth; mod hooks; mod interact;