From 84e2e6e5c68294d227de01d149b2b1ae2f5fb8e1 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 9 Oct 2026 03:50:36 +0200 Subject: [PATCH] Add beacons BeaconBlockEntity: beam sections tinted and blended by stained glass, pyramid levels from beacon_base_blocks every 80 ticks, primary and secondary powers for players in range (amplified when doubled), and the activate, ambient, deactivate and power-select sounds; saved with the vanilla keys. The 230x219 BeaconScreen has the power buttons, the upgrade button, payment slot and confirm/cancel. BeaconRenderer's rotating beam and glow scroll through a new wrapped-sprite mode of the entity shader. /setblock and /fill now replace block entities with the new block's. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/containers.rs | 25 +- crates/horizon-client/src/entity.wgsl | 10 +- crates/horizon-client/src/game.rs | 11 +- crates/horizon-client/src/game/beacon.rs | 613 +++++++++++++++++++++ crates/horizon-client/src/game/commands.rs | 16 +- crates/horizon-client/src/game/machines.rs | 7 + crates/horizon-client/src/game/stations.rs | 40 +- crates/horizon-client/src/save.rs | 21 +- 8 files changed, 732 insertions(+), 11 deletions(-) create mode 100644 crates/horizon-client/src/game/beacon.rs diff --git a/crates/horizon-client/src/containers.rs b/crates/horizon-client/src/containers.rs index c4fa3d8c5..1cb1919ac 100644 --- a/crates/horizon-client/src/containers.rs +++ b/crates/horizon-client/src/containers.rs @@ -54,6 +54,19 @@ pub enum BlockEntity { Sign(crate::signs::SignData), /// `JukeboxBlockEntity`: the record and its `JukeboxSongPlayer`. Jukebox(Jukebox), + /// `BeaconBlockEntity`. + Beacon(Box), +} +#[derive(Clone, Debug, Default)] +pub struct Beacon { + pub levels: i32, + /// Effect ids (`minecraft:speed`). + pub primary: Option, + pub secondary: Option, + /// Beam sections (RGB colour, height) and the ones being rebuilt. + pub sections: Vec<(u32, i32)>, + pub checking: Vec<(u32, i32)>, + pub last_check_y: i32, } #[derive(Clone, Debug, Default)] pub struct Jukebox { @@ -81,6 +94,12 @@ impl BlockEntity { speed: 1.0, })); } + if name == "minecraft:beacon" { + return Some(Self::Beacon(Box::new(Beacon { + last_check_y: i32::MIN / 2, + ..Default::default() + }))); + } if name == "minecraft:brewing_stand" { return Some(Self::Brewing(Box::new(crate::brewing::BrewingStand::new()))); } @@ -124,6 +143,7 @@ impl BlockEntity { Self::Brewing(_) => "minecraft:brewing_stand", Self::Sign(s) => s.id(), Self::Jukebox(_) => "minecraft:jukebox", + Self::Beacon(_) => "minecraft:beacon", } } pub fn contents(&self) -> Vec { @@ -133,6 +153,7 @@ impl BlockEntity { Self::Brewing(b) => b.items.iter().flatten().cloned().collect(), Self::Sign(_) => Vec::new(), Self::Jukebox(j) => j.record.iter().cloned().collect(), + Self::Beacon(_) => Vec::new(), } } pub fn slots(&self) -> &[Option] { @@ -140,7 +161,7 @@ impl BlockEntity { Self::Chest(items) => items, Self::Furnace(f) => &f.items, Self::Brewing(b) => &b.items, - Self::Sign(_) | Self::Jukebox(_) => &[], + Self::Sign(_) | Self::Jukebox(_) | Self::Beacon(_) => &[], } } pub fn slots_mut(&mut self) -> &mut [Option] { @@ -148,7 +169,7 @@ impl BlockEntity { Self::Chest(items) => items, Self::Furnace(f) => &mut f.items, Self::Brewing(b) => &mut b.items, - Self::Sign(_) | Self::Jukebox(_) => &mut [], + Self::Sign(_) | Self::Jukebox(_) | Self::Beacon(_) => &mut [], } } } diff --git a/crates/horizon-client/src/entity.wgsl b/crates/horizon-client/src/entity.wgsl index 021e2bbb4..a7a0c2a66 100644 --- a/crates/horizon-client/src/entity.wgsl +++ b/crates/horizon-client/src/entity.wgsl @@ -25,6 +25,8 @@ struct Out { @location(3) cylindrical: f32, @location(5) flash: f32, @location(4) @interpolate(flat) overlay: u32, + // Overlay 6: the sprite's v origin and height, for v repeated inside it. + @location(6) @interpolate(flat) wrap: vec2, } @vertex fn vs_entity(@location(0) position: vec3, @location(1) uv: vec2, @location(2) overlay: u32, @location(3) color: vec4, @location(4) light: vec4) -> Out { @@ -35,7 +37,9 @@ struct Out { let lm = textureSampleLevel(lightmap, lightmap_sampler, clamp(l / 256.0 + 0.5 / 16.0, vec2(0.5 / 16.0), vec2(15.5 / 16.0)), 0.0); // overlay 2: celestial bodies, 4: clouds; both use their vertex colour unlit. - var c = select(color * lm, color, overlay == 2u || overlay == 4u); + var c = select(color * lm, color, overlay == 2u || overlay == 4u || overlay == 6u); + // Overlay 6 (beacon beams): the light bytes carry the sprite's v range. + out.wrap = vec2(f32(light.x | (light.y << 8u)), f32(light.z | (light.w << 8u))) / 65535.0; // Hurt overlay (OverlayTexture red tint). if overlay == 1u { c = vec4(mix(c.rgb, vec3(1.0, 0.0, 0.0), 0.3), c.a); } out.color = c; @@ -54,7 +58,9 @@ fn linear_fog(d: f32, start: f32, end: f32) -> f32 { return (d - start) / (end - start); } @fragment fn fs_entity(in: Out) -> @location(0) vec4 { - var color = textureSample(atlas, atlas_sampler, in.uv) * in.color; + var uv = in.uv; + if in.overlay == 6u { uv.y = in.wrap.x + fract(in.uv.y) * in.wrap.y; } + var color = textureSample(atlas, atlas_sampler, uv) * in.color; if in.overlay == 4u { // clouds.fsh: alpha fades out towards the cloud fog end. return vec4(color.rgb, color.a * (1.0 - linear_fog(in.spherical, 0.0, globals.fog_ends.y))); diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index ba1bf0c41..48a84f848 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1,6 +1,7 @@ //! The playable game loop: streaming world, player simulation at 20 ticks/s, //! block interaction, inventory and HUD. mod armor_stands; +mod beacon; mod blockctl; mod boats; mod chat; @@ -1308,6 +1309,10 @@ impl Game<'_> { let pitch = self.world.random.next_float() * 0.1 + 0.9; self.audio .play("minecraft:block.ender_chest.open", Some(at), 0.5, pitch); + } else if &*name == "minecraft:beacon" && self.open_station(pos, &name) { + self.world.block_entities.entry(key).or_insert_with(|| { + BlockEntity::for_block(&name).unwrap_or(BlockEntity::Chest(Vec::new())) + }); } else if let Some(entity) = BlockEntity::for_block(&name) { self.world.block_entities.entry(key).or_insert(entity); self.container = Some(key); @@ -2823,7 +2828,7 @@ impl Game<'_> { Some(BlockEntity::Chest(items)) => v.extend((0..items.len()).map(|i| 300 + i)), Some(BlockEntity::Furnace(_)) => v.extend(300..303), Some(BlockEntity::Brewing(_)) => v.extend(300..305), - Some(BlockEntity::Sign(_) | BlockEntity::Jukebox(_)) => {} + Some(BlockEntity::Sign(_) | BlockEntity::Jukebox(_) | BlockEntity::Beacon(_)) => {} None => { v.extend((0..self.craft_size * self.craft_size).map(|i| 100 + i)); v.push(200); @@ -3043,7 +3048,7 @@ impl Game<'_> { } } Some(BlockEntity::Brewing(_)) => {} - Some(BlockEntity::Sign(_) | BlockEntity::Jukebox(_)) => {} + Some(BlockEntity::Sign(_) | BlockEntity::Jukebox(_) | BlockEntity::Beacon(_)) => {} None => { let texture = if self.craft_size == 3 { "gui/container/crafting_table" @@ -3728,6 +3733,7 @@ impl Game<'_> { self.armor_stand_geometry(camera, alpha, &mut out); self.boat_geometry(camera, alpha, &mut out); self.cart_geometry(camera, alpha, &mut out); + self.beacon_geometry(camera, alpha, &mut out); self.fishing_geometry(camera, alpha, &mut out); self.trident_geometry(camera, alpha, &mut out); self.rocket_geometry(camera, alpha, &mut out); @@ -4030,6 +4036,7 @@ impl Game<'_> { fn tick_block_entities(&mut self) -> Result<()> { self.tick_hoppers()?; self.tick_jukeboxes(); + self.tick_beacons(); // ChestLidController: 0.1 per tick towards open/closed. if let Some(key) = self.container { self.chest_open.entry(key).or_insert((0.0, 0.0)); diff --git a/crates/horizon-client/src/game/beacon.rs b/crates/horizon-client/src/game/beacon.rs new file mode 100644 index 000000000..3a0a89e83 --- /dev/null +++ b/crates/horizon-client/src/game/beacon.rs @@ -0,0 +1,613 @@ +//! Beacons (`BeaconBlockEntity`, `BeaconMenu`, `BeaconScreen`, +//! `BeaconRenderer`): the beam sections tinted by stained glass, the pyramid +//! levels every 80 ticks, the primary/secondary effects for players in +//! range, the activate/ambient/deactivate sounds, the 230x219 menu with its +//! power buttons and payment slot, and the rotating, scrolling beam. +use super::{ + Game, + stations::{INPUT, Kind}, +}; +use horizon_client::{ + containers::{Beacon, BlockEntity}, + effects::EffectInstance, + hud::{HudBatch, rgba}, + items::ItemStack, + mesher::Vertex, + mobs::render::{DYE, DYE_NAMES}, +}; + +/// The beacon screen is 54 px wider than the 176-px menus it shares an origin with. +pub(super) const SHIFT: f32 = -27.0; +pub(super) const HEIGHT: f32 = 219.0; +/// `BEACON_EFFECTS` in order, with the pyramid level each needs. +pub(super) const EFFECTS: [(&str, i32); 6] = [ + ("speed", 1), + ("haste", 1), + ("resistance", 2), + ("jump_boost", 2), + ("strength", 3), + ("regeneration", 4), +]; +const PAYMENT: [&str; 5] = [ + "minecraft:iron_ingot", + "minecraft:gold_ingot", + "minecraft:emerald", + "minecraft:diamond", + "minecraft:netherite_ingot", +]; +pub(super) fn is_payment(item: &str) -> bool { + PAYMENT.contains(&item) +} +/// `BeaconMenu.PaymentSlot` and the player inventory, relative to the 176 origin. +pub(super) fn slot_position(i: usize) -> Option<(f32, f32)> { + Some(match i { + 0..9 => (36.0 + SHIFT + i as f32 * 18.0, 195.0), + 9..36 => ( + 36.0 + SHIFT + ((i - 9) % 9) as f32 * 18.0, + 137.0 + ((i - 9) / 9) as f32 * 18.0, + ), + INPUT => (136.0 + SHIFT, 110.0), + _ => return None, + }) +} +/// A power button: (effect index, x, y, primary) relative to the beacon's left edge. +fn buttons() -> Vec<(usize, f32, f32, bool)> { + let mut out = Vec::new(); + let tiers: [&[usize]; 3] = [&[0, 1], &[2, 3], &[4]]; + for (tier, effects) in tiers.iter().enumerate() { + let count = effects.len() as i32; + let total = count * 22 + (count - 1) * 2; + for (c, &e) in effects.iter().enumerate() { + let x = 76 + c as i32 * 24 - total / 2; + out.push((e, x as f32, 22.0 + tier as f32 * 25.0, true)); + } + } + // Tier 4: regeneration, then the upgrade (primary II) button. + let total = 2 * 22 + 2; + out.push((5, (167 - total / 2) as f32, 47.0, false)); + out +} +fn upgrade_button() -> (f32, f32) { + let total = 2 * 22 + 2; + ((167 + 24 - total / 2) as f32, 47.0) +} +fn inside(p: (f32, f32), x: f32, y: f32, w: f32, h: f32) -> bool { + p.0 >= x && p.1 >= y && p.0 < x + w && p.1 < y + h +} +fn effect_id(name: &str) -> Option<&'static str> { + let n = name.strip_prefix("minecraft:").unwrap_or(name); + EFFECTS.iter().map(|(e, _)| *e).find(|e| *e == n) +} +/// `ARGB.average`. +fn average(a: u32, b: u32) -> u32 { + let ch = |c: u32, s: u32| (c >> s) & 255; + [16, 8, 0] + .iter() + .fold(0, |acc, &s| acc | ((ch(a, s) + ch(b, s)) / 2) << s) +} +impl Game<'_> { + /// The beam color of a `BeaconBeamBlock` (stained glass and panes, the beacon). + fn beam_color(&self, name: &str) -> Option { + if name == "minecraft:beacon" { + return Some(DYE[0]); + } + let n = name.strip_prefix("minecraft:")?; + let dye = n + .strip_suffix("_stained_glass") + .or_else(|| n.strip_suffix("_stained_glass_pane"))?; + DYE_NAMES.iter().position(|d| *d == dye).map(|i| DYE[i]) + } + /// `getHeight(WORLD_SURFACE)`: above the highest non-air block. + fn world_surface(&self, x: i32, z: i32) -> i32 { + let (min, max) = (super::MIN_Y, super::MIN_Y + super::HEIGHT); + (min..max) + .rev() + .find(|&y| !self.res.facts.is_air(self.world.block(x, y, z))) + .map_or(min, |y| y + 1) + } + /// `BeaconBlockEntity.tick` for every beacon. + pub(super) fn tick_beacons(&mut self) { + let keys: Vec<(i32, i32, i32)> = self + .world + .block_entities + .iter() + .filter(|(_, e)| matches!(e, BlockEntity::Beacon(_))) + .map(|(k, _)| *k) + .collect(); + for key in keys { + let Some(BlockEntity::Beacon(mut b)) = self.world.block_entities.remove(&key) else { + continue; + }; + self.tick_beacon(key, &mut b); + self.world + .block_entities + .insert(key, BlockEntity::Beacon(b)); + } + } + fn tick_beacon(&mut self, (x, y, z): (i32, i32, i32), b: &mut Beacon) { + let mut check = if b.last_check_y < y { + b.checking.clear(); + b.last_check_y = y - 1; + y + } else { + b.last_check_y + 1 + }; + let surface = self.world_surface(x, z); + let mut i = 0; + while i < 10 && check <= surface { + let state = self.world.block(x, check, z); + let f = self.res.facts.get(state); + if let Some(color) = self.beam_color(&f.name) { + if b.checking.len() <= 1 { + b.checking.push((color, 1)); + } else if let Some(last) = b.checking.last_mut() { + if color == last.0 { + last.1 += 1; + } else { + let mixed = average(last.0, color); + b.checking.push((mixed, 1)); + } + } + } else { + let Some(last) = b + .checking + .last_mut() + .filter(|_| f.dampening < 15 || &*f.name == "minecraft:bedrock") + else { + b.checking.clear(); + b.last_check_y = surface; + break; + }; + last.1 += 1; + } + check += 1; + b.last_check_y += 1; + i += 1; + } + let previous = b.levels; + if self.world.game_time % 80 == 0 { + if !b.sections.is_empty() { + b.levels = self.beacon_base(x, y, z); + } + if b.levels > 0 && !b.sections.is_empty() { + self.beacon_effects([x, y, z], b); + self.beacon_sound([x, y, z], "minecraft:block.beacon.ambient"); + } + } + if b.last_check_y >= surface { + b.last_check_y = super::MIN_Y - 1; + b.sections = std::mem::take(&mut b.checking); + let (was, is) = (previous > 0, b.levels > 0); + if !was && is { + self.beacon_sound([x, y, z], "minecraft:block.beacon.activate"); + } else if was && !is { + self.beacon_sound([x, y, z], "minecraft:block.beacon.deactivate"); + } + } + } + fn beacon_sound(&mut self, p: [i32; 3], sound: &str) { + let at = [ + f64::from(p[0]) + 0.5, + f64::from(p[1]) + 0.5, + f64::from(p[2]) + 0.5, + ]; + self.audio.play(sound, Some(at), 1.0, 1.0); + } + /// `updateBase`: complete layers of `beacon_base_blocks` below. + fn beacon_base(&self, x: i32, y: i32, z: i32) -> i32 { + let base = self + .res + .items + .block_tags + .get("minecraft:beacon_base_blocks"); + let mut levels = 0; + for step in 1..=4 { + let ly = y - step; + if ly < super::MIN_Y { + break; + } + let ok = (x - step..=x + step).all(|lx| { + (z - step..=z + step).all(|lz| { + let name = &self.res.facts.get(self.world.block(lx, ly, lz)).name; + base.is_some_and(|t| t.contains(&**name)) + }) + }); + if !ok { + break; + } + levels = step; + } + levels + } + /// `applyEffects`: the powers for players within `levels * 10 + 10` (any height above). + fn beacon_effects(&mut self, p: [i32; 3], b: &Beacon) { + let Some(primary) = b.primary.as_deref().and_then(effect_id) else { + return; + }; + let range = f64::from(b.levels * 10 + 10); + let Some(pp) = self.player.as_ref().map(|p| p.position) else { + return; + }; + let inside = pp.x >= f64::from(p[0]) - range + && pp.x <= f64::from(p[0] + 1) + range + && pp.z >= f64::from(p[2]) - range + && pp.z <= f64::from(p[2] + 1) + range + && pp.y >= f64::from(p[1]) - range; + if !inside { + return; + } + let secondary = b.secondary.as_deref().and_then(effect_id); + let amplifier = i32::from(b.levels >= 4 && secondary == Some(primary)); + let duration = (9 + b.levels * 2) * 20; + let ambient = |id, amp| EffectInstance { + ambient: true, + ..EffectInstance::new(id, duration, amp) + }; + self.potions.effects.add(ambient(primary, amplifier)); + if let Some(s) = secondary.filter(|&s| b.levels >= 4 && s != primary) { + self.potions.effects.add(ambient(s, 0)); + } + } + /// Opening the menu shows the beacon's current powers. + pub(super) fn beacon_opened(&mut self) { + let Some(s) = self.station.as_ref().filter(|s| s.kind == Kind::Beacon) else { + return; + }; + let pos = s.pos; + let (p, sec) = match self.world.block_entities.get(&(pos[0], pos[1], pos[2])) { + Some(BlockEntity::Beacon(b)) => (b.primary.clone(), b.secondary.clone()), + _ => (None, None), + }; + let index = + |e: Option| e.and_then(|e| EFFECTS.iter().position(|(n, _)| e.ends_with(n))); + if let Some(s) = self.station.as_mut() { + s.beacon = [index(p), index(sec)]; + } + } + fn beacon_levels(&self) -> i32 { + let Some(pos) = self.station.as_ref().map(|s| s.pos) else { + return 0; + }; + match self.world.block_entities.get(&(pos[0], pos[1], pos[2])) { + Some(BlockEntity::Beacon(b)) => b.levels, + _ => 0, + } + } + /// Clicks on the power, confirm and cancel buttons. + pub(super) fn beacon_click(&mut self, local: (f32, f32)) -> bool { + let l = (local.0 - SHIFT, local.1); + let levels = self.beacon_levels(); + let Some(sel) = self.station.as_ref().map(|s| s.beacon) else { + return false; + }; + for (e, x, y, primary) in buttons() { + if !inside(l, x, y, 22.0, 22.0) { + continue; + } + let tier = EFFECTS[e].1; + let selected = if primary { + sel[0] == Some(e) + } else { + sel[1] == Some(e) + }; + if tier <= levels.min(if primary { 3 } else { 4 }) + && !selected + && (primary || levels >= 4) + { + if let Some(s) = self.station.as_mut() { + if primary { + s.beacon[0] = Some(e); + if s.beacon[1] != Some(e) { + s.beacon[1] = None; + } + } else { + s.beacon[1] = Some(e); + } + } + self.audio + .play("minecraft:ui.button.click", None, 0.25, 1.0); + } + return true; + } + let (ux, uy) = upgrade_button(); + if inside(l, ux, uy, 22.0, 22.0) { + if levels >= 4 + && let Some(p) = sel[0] + && sel[1] != Some(p) + && let Some(s) = self.station.as_mut() + { + s.beacon[1] = Some(p); + self.audio + .play("minecraft:ui.button.click", None, 0.25, 1.0); + } + return true; + } + if inside(l, 164.0, 107.0, 22.0, 22.0) { + let paid = self.station.as_ref().is_some_and(|s| s.inputs[0].is_some()); + if paid && sel[0].is_some() { + self.audio + .play("minecraft:ui.button.click", None, 0.25, 1.0); + self.confirm_beacon(sel); + self.close_beacon_screen(); + } + return true; + } + if inside(l, 190.0, 107.0, 22.0, 22.0) { + self.audio + .play("minecraft:ui.button.click", None, 0.25, 1.0); + self.close_beacon_screen(); + return true; + } + false + } + fn close_beacon_screen(&mut self) { + self.close_container(); + self.screen = super::Screen::Playing; + self.capture(true); + } + /// `BeaconMenu.updateEffects`: spend the payment and set the powers. + fn confirm_beacon(&mut self, sel: [Option; 2]) { + let Some(s) = self.station.as_mut() else { + return; + }; + let pos = s.pos; + s.inputs[0] = None; + let name = |e: Option| e.map(|e| format!("minecraft:{}", EFFECTS[e].0)); + let mut select_sound = false; + if let Some(BlockEntity::Beacon(b)) = + self.world.block_entities.get_mut(&(pos[0], pos[1], pos[2])) + { + b.primary = name(sel[0]); + b.secondary = name(sel[1]); + select_sound = !b.sections.is_empty(); + } + if select_sound { + self.beacon_sound(pos, "minecraft:block.beacon.power_select"); + } + self.world.modified.insert((pos[0] >> 4, pos[2] >> 4)); + } + /// `BeaconScreen`: background, labels, payment icons, power buttons. + pub(super) fn draw_beacon(&mut self, batch: &mut HudBatch) { + let (ox, oy) = self.inventory_origin(batch); + let (bx, by) = (ox + SHIFT, oy); + let local = (self.cursor.0 - bx, self.cursor.1 - by); + let levels = self.beacon_levels(); + let sel = self.station.as_ref().map_or([None; 2], |s| s.beacon); + let paid = self.station.as_ref().is_some_and(|s| s.inputs[0].is_some()); + let atlas = &self.res.atlas; + batch.fill( + atlas, + 0.0, + 0.0, + batch.width, + batch.height, + rgba(16, 16, 16, 160), + ); + if let Some(mut r) = atlas.sprites.get("gui/container/beacon").copied() { + let (du, dv) = ((r.u1 - r.u0) / r.w as f32, (r.v1 - r.v0) / r.h as f32); + r.u1 = r.u0 + 230.0 * du; + r.v1 = r.v0 + HEIGHT * dv; + batch.rect_uv(bx, by, 230.0, HEIGHT, r, u32::MAX); + } + let label = 0xFFE0_E0E0; + for (key, fallback, cx) in [ + ("block.minecraft.beacon.primary", "Primary Power", 62.0), + ("block.minecraft.beacon.secondary", "Secondary Power", 169.0), + ] { + let text = self.translate(key, fallback); + let atlas = &self.res.atlas; + let w = HudBatch::text_width(atlas, &text); + batch.text( + atlas, + &text, + bx + cx - (w / 2.0).floor(), + by + 10.0, + label, + false, + ); + } + for (i, item) in PAYMENT.iter().rev().enumerate() { + let x = [20.0, 41.0, 63.0, 86.0, 108.0][i]; + self.draw_item(batch, &ItemStack::new(item, 1), bx + x, by + 109.0); + } + let atlas = &self.res.atlas; + let mut tooltip = None; + let button = + |batch: &mut HudBatch, x: f32, y: f32, active: bool, selected: bool, icon: &str| { + let hover = inside(local, x, y, 22.0, 22.0); + let sprite = if !active { + "button_disabled" + } else if selected { + "button_selected" + } else if hover { + "button_highlighted" + } else { + "button" + }; + batch.sprite( + atlas, + &format!("gui/sprites/container/beacon/{sprite}"), + bx + x, + by + y, + ); + batch.sprite_scaled( + atlas, + icon, + bx + x + 2.0, + by + y + 2.0, + 18.0, + 18.0, + u32::MAX, + ); + hover + }; + for (e, x, y, primary) in buttons() { + let tier = EFFECTS[e].1; + let active = if primary { + tier <= levels && tier < 4 + } else { + levels >= 4 + }; + let selected = if primary { + sel[0] == Some(e) + } else { + sel[1] == Some(e) + }; + let icon = format!("mob_effect/{}", EFFECTS[e].0); + if button(batch, x, y, active, selected, &icon) { + tooltip = Some(format!("effect.minecraft.{}", EFFECTS[e].0)); + } + } + if let Some(p) = sel[0] { + let (x, y) = upgrade_button(); + let icon = format!("mob_effect/{}", EFFECTS[p].0); + if button(batch, x, y, levels >= 4, sel[1] == Some(p), &icon) { + tooltip = Some(format!("effect.minecraft.{}", EFFECTS[p].0)); + } + } + button( + batch, + 164.0, + 107.0, + paid && sel[0].is_some(), + false, + "gui/sprites/container/beacon/confirm", + ); + button( + batch, + 190.0, + 107.0, + true, + false, + "gui/sprites/container/beacon/cancel", + ); + let slots: Vec = (0..36).chain([INPUT]).collect(); + for i in slots { + let (sx, sy) = self.slot_position(i); + if let Some(stack) = self.slot_mut(i).clone() { + self.draw_item(batch, &stack, ox + sx, oy + sy); + } + } + let mut stack_tip = None; + if let Some(slot) = self.slot_at(batch, self.cursor.0, self.cursor.1) { + let (sx, sy) = self.slot_position(slot); + batch.fill( + &self.res.atlas, + ox + sx, + oy + sy, + 16.0, + 16.0, + rgba(255, 255, 255, 128), + ); + stack_tip = self.slot_mut(slot).clone(); + } + if let Some(stack) = self.carried.clone() { + self.draw_item(batch, &stack, self.cursor.0 - 8.0, self.cursor.1 - 8.0); + } else if let Some(stack) = stack_tip { + let lines = self.stack_tooltip(&stack); + self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1); + } else if let Some(key) = tooltip { + let text = self.translate(&key, &key); + self.draw_tooltip_lines(batch, &[(text, u32::MAX)], self.cursor.0, self.cursor.1); + } + } + /// `BeaconRenderer`: each section's rotating solid beam and its glow. + pub(super) fn beacon_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec) { + let Some(s) = self + .res + .atlas + .sprites + .get("entity/beacon/beacon_beam") + .copied() + else { + return; + }; + // The sprite's v range rides in the light bytes (entity shader overlay 6). + let q = |v: f32| (v * 65535.0).round().clamp(0.0, 65535.0) as u32; + let wrap = q(s.v0) | q(s.v1 - s.v0) << 16; + let time = (self.world.game_time.rem_euclid(40)) as f32 + alpha as f32; + let scoping = self.scoping(); + for (k, e) in &self.world.block_entities { + let BlockEntity::Beacon(b) = e else { continue }; + if b.levels == 0 || b.sections.is_empty() { + continue; + } + let base = glam::DVec3::new(f64::from(k.0) + 0.5, f64::from(k.1), f64::from(k.2) + 0.5); + let distance = (base - camera).truncate().length() as f32; + let scale = if scoping { + 1.0 + } else { + (distance / 96.0).max(1.0) + }; + let o = (base - camera).as_vec3(); + let mut start = 0i32; + for (i, &(color, height)) in b.sections.iter().enumerate() { + let height = if i == b.sections.len() - 1 { + 2048 + } else { + height + }; + let end = start + height; + let scroll = -time; + let v_off = (scroll * 0.2 - (scroll * 0.1).floor()).rem_euclid(1.0); + let rgb = |a: u32| { + a << 24 | (color & 255) << 16 | (color >> 8 & 255) << 8 | (color >> 16 & 255) + }; + // Solid beam: radius 0.2, rotated with time. + let r = 0.2 * scale; + let rot = glam::Mat3::from_rotation_y((time * 2.25 - 45.0).to_radians()); + let corners = [ + rot * glam::Vec3::new(0.0, 0.0, r), + rot * glam::Vec3::new(r, 0.0, 0.0), + rot * glam::Vec3::new(-r, 0.0, 0.0), + rot * glam::Vec3::new(0.0, 0.0, -r), + ]; + let v2 = -1.0 + v_off; + let v1 = height as f32 * (0.5 / r) + v2; + beam_part(out, o, start, end, corners, [v1, v2], rgb(255), s, wrap); + // Glow: radius 0.25, translucent. + let g = 0.25 * scale; + let corners = [ + glam::Vec3::new(-g, 0.0, -g), + glam::Vec3::new(g, 0.0, -g), + glam::Vec3::new(-g, 0.0, g), + glam::Vec3::new(g, 0.0, g), + ]; + let v1 = height as f32 + v2; + beam_part(out, o, start, end, corners, [v1, v2], rgb(32), s, wrap); + start = end; + } + } + } +} +/// `renderPart`: four quads between the corners (wn, en, ws, es). +#[allow(clippy::too_many_arguments)] +fn beam_part( + out: &mut Vec, + o: glam::Vec3, + start: i32, + end: i32, + [wn, en, ws, es]: [glam::Vec3; 4], + [v1, v2]: [f32; 2], + color: u32, + s: horizon_client::hud::Rect, + wrap: u32, +) { + for (a, b) in [(wn, en), (es, ws), (en, es), (ws, wn)] { + let at = |c: glam::Vec3, y: i32| o + c + glam::Vec3::new(0.0, y as f32, 0.0); + for (p, uv) in [ + (at(a, end), [s.u1, v1]), + (at(a, start), [s.u1, v2]), + (at(b, start), [s.u0, v2]), + (at(b, end), [s.u0, v1]), + ] { + out.push(Vertex { + pos: p.to_array(), + uv, + layer: 6, + color, + light: wrap, + }); + } + } +} diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index 41b1977db..6b23bd624 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -1127,9 +1127,23 @@ impl Game<'_> { } /// Place a state like BlockInput.place: false when nothing changed. fn place_state(&mut self, pos: [i32; 3], state: BlockStateId, strict: bool) -> R { - if self.world.block(pos[0], pos[1], pos[2]) == state { + let previous = self.world.block(pos[0], pos[1], pos[2]); + if previous == state { return Ok(false); } + // A different block replaces the block entity (Level.setBlock). + let (old, new) = ( + self.res.facts.get(previous).name.clone(), + self.res.facts.get(state).name.clone(), + ); + if old != new { + let key = (pos[0], pos[1], pos[2]); + self.jukebox_removed(pos); + self.world.block_entities.remove(&key); + if let Some(entity) = horizon_client::containers::BlockEntity::for_block(&new) { + self.world.block_entities.insert(key, entity); + } + } if strict { self.with_level(|b, level, ticks| { b.set_block( diff --git a/crates/horizon-client/src/game/machines.rs b/crates/horizon-client/src/game/machines.rs index 2299bc87a..835056c09 100644 --- a/crates/horizon-client/src/game/machines.rs +++ b/crates/horizon-client/src/game/machines.rs @@ -1028,6 +1028,13 @@ impl Game<'_> { } /// `imageHeight` of the open screen (HopperScreen is 133 tall). pub(super) fn screen_height(&self) -> f32 { + if self + .station + .as_ref() + .is_some_and(|s| s.kind == super::stations::Kind::Beacon) + { + return super::beacon::HEIGHT; + } if self.machine_screen() == Some(Screen::Hopper) { 133.0 } else { diff --git a/crates/horizon-client/src/game/stations.rs b/crates/horizon-client/src/game/stations.rs index 59e8e1277..ef866a323 100644 --- a/crates/horizon-client/src/game/stations.rs +++ b/crates/horizon-client/src/game/stations.rs @@ -28,6 +28,8 @@ pub(super) enum Kind { Stonecutter, /// Villager trading (game/trading.rs). Merchant, + /// The beacon menu (game/beacon.rs). + Beacon, } impl Kind { fn from_block(name: &str) -> Option { @@ -39,13 +41,14 @@ impl Kind { "minecraft:grindstone" => Self::Grindstone, "minecraft:smithing_table" => Self::Smithing, "minecraft:stonecutter" => Self::Stonecutter, + "minecraft:beacon" => Self::Beacon, _ => return None, }) } fn inputs(self) -> usize { match self { Self::Smithing => 3, - Self::Stonecutter => 1, + Self::Stonecutter | Self::Beacon => 1, _ => 2, } } @@ -57,6 +60,7 @@ impl Kind { Self::Smithing => "gui/container/smithing", Self::Stonecutter => "gui/container/stonecutter", Self::Merchant => "gui/container/villager", + Self::Beacon => "gui/container/beacon", } } fn title_key(self) -> &'static str { @@ -67,6 +71,7 @@ impl Kind { Self::Smithing => "container.upgrade", Self::Stonecutter => "container.stonecutter", Self::Merchant => "merchant.trades", + Self::Beacon => "container.beacon", } } /// (titleLabelX, titleLabelY). @@ -86,6 +91,7 @@ impl Kind { Self::Smithing => &[(8.0, 48.0), (26.0, 48.0), (44.0, 48.0)], Self::Stonecutter => &[(20.0, 33.0)], Self::Merchant => &[(86.0, 37.0), (112.0, 37.0)], + Self::Beacon => &[(109.0, 110.0)], }; table[i.min(table.len() - 1)] } @@ -97,6 +103,7 @@ impl Kind { Self::Smithing => Some((98.0, 48.0)), Self::Stonecutter => Some((143.0, 33.0)), Self::Merchant => Some((170.0, 37.0)), + Self::Beacon => None, } } } @@ -121,6 +128,8 @@ pub(super) struct Station { last_sound: u64, /// Villager trading state. pub(super) merchant: Option>, + /// Beacon menu: the selected primary and secondary power (EFFECTS index). + pub(super) beacon: [Option; 2], } impl Station { fn new(kind: Kind, pos: [i32; 3]) -> Self { @@ -140,6 +149,7 @@ impl Station { anvil_last: None, last_sound: 0, merchant: None, + beacon: [None; 2], } } pub(super) fn new_kind(kind: Kind, pos: [i32; 3]) -> Self { @@ -234,7 +244,7 @@ impl Game<'_> { fn data(&self) -> &stations::StationData { &self.res.stations } - fn translate(&self, key: &str, fallback: &str) -> String { + pub(super) fn translate(&self, key: &str, fallback: &str) -> String { self.data() .enchantments .translate(key) @@ -254,6 +264,7 @@ impl Game<'_> { } self.station = Some(Station::new(kind, pos)); self.station_changed(); + self.beacon_opened(); true } /// `AbstractContainerMenu.clearContainer`: inputs go back to the player. @@ -294,6 +305,9 @@ impl Game<'_> { if s.kind == Kind::Merchant && i < 36 { return Some(super::trading::inventory_slot(i)); } + if s.kind == Kind::Beacon { + return super::beacon::slot_position(i); + } match i { RESULT => s.kind.result_pos(), _ if i >= INPUT && i < INPUT + s.inputs.len() => Some(s.kind.input_pos(i - INPUT)), @@ -391,6 +405,7 @@ impl Game<'_> { ); } Kind::Merchant => super::trading::update(&mut s, items), + Kind::Beacon => {} Kind::Stonecutter => { let item = s.inputs[0].as_ref().map(|i| i.item.clone()); if item != s.input_item { @@ -451,6 +466,7 @@ impl Game<'_> { let i = slot - INPUT; match s.kind { Kind::Enchanting if i == 0 => (true, 1), + Kind::Beacon => (super::beacon::is_payment(&stack.item), 1), Kind::Enchanting => (&*stack.item == "minecraft:lapis_lazuli", 64), Kind::Grindstone => (stations::grindstone_accepts(&self.res.items, stack), 64), Kind::Smithing => (data.recipes.smithing_accepts(i, stack), 64), @@ -619,6 +635,12 @@ impl Game<'_> { } } Some(Kind::Merchant) => {} + Some(Kind::Beacon) => { + // BeaconMenu.quickMoveStack: one payment item into the empty slot. + if super::beacon::is_payment(&stack.item) { + moved = self.move_to(&mut stack, &[INPUT], false); + } + } Some(Kind::Smithing) | Some(Kind::Anvil) | Some(Kind::Stonecutter) => { let n = self.station.as_ref().map_or(0, |s| s.inputs.len()); let inputs: Vec = (0..n).map(|i| INPUT + i).collect(); @@ -781,6 +803,7 @@ impl Game<'_> { } } Kind::Merchant => self.merchant_take(&mut s), + Kind::Beacon => {} Kind::Enchanting => {} } self.station = Some(s); @@ -902,6 +925,9 @@ impl Game<'_> { if kind == Kind::Merchant && !right && self.merchant_click(local) { return true; } + if kind == Kind::Beacon && !right && self.beacon_click(local) { + return true; + } if kind == Kind::Enchanting && !right { for i in 0..3 { let (x, y, w, h) = enchant_button(i); @@ -1088,6 +1114,14 @@ impl Game<'_> { { return self.draw_merchant(batch); } + if self + .station + .as_ref() + .is_some_and(|s| s.kind == Kind::Beacon) + { + self.draw_beacon(batch); + return true; + } let brewing = self.brewing_open(); if self.station.is_none() && !brewing { return false; @@ -1492,7 +1526,7 @@ impl Game<'_> { } } } - Kind::Merchant => {} + Kind::Merchant | Kind::Beacon => {} Kind::Stonecutter => { let display = s.inputs[0].is_some() && !s.recipes.is_empty(); let active = display && s.recipes.len() > 12; diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index a0c1cde75..c8d4cefff 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -385,6 +385,15 @@ fn encode_block_entity(p: (i32, i32, i32), e: &crate::containers::BlockEntity, b } entries.push(("ticks_since_song_started", Tag::Long(j.ticks))); } + BlockEntity::Beacon(b) => { + if let Some(p) = &b.primary { + entries.push(("primary_effect", string(p))); + } + if let Some(s) = &b.secondary { + entries.push(("secondary_effect", string(s))); + } + entries.push(("Levels", Tag::Int(b.levels))); + } } Tag::compound(entries) } @@ -406,6 +415,16 @@ fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::containers: }), )); } + if id == "minecraft:beacon" { + let beacon = crate::containers::Beacon { + levels: int("Levels").unwrap_or(0), + primary: text(tag.get("primary_effect")), + secondary: text(tag.get("secondary_effect")), + last_check_y: i32::MIN / 2, + ..Default::default() + }; + return Some((pos, BlockEntity::Beacon(Box::new(beacon)))); + } let mut entity = BlockEntity::for_block(&id)?; match &mut entity { BlockEntity::Chest(items) => *items = decode_items(tag.get("Items"), items.len()), @@ -442,7 +461,7 @@ fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::containers: }; } BlockEntity::Sign(sign) => sign.decode(tag), - BlockEntity::Jukebox(_) => {} + BlockEntity::Jukebox(_) | BlockEntity::Beacon(_) => {} } Some((pos, entity)) }