diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index bcc24f172..8ecc49629 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -4,6 +4,7 @@ mod armor_stands; mod beacon; mod blockctl; mod boats; +mod cartography; mod chat; mod commands; mod control; @@ -2566,7 +2567,6 @@ impl Game<'_> { } fn hud(&mut self, size: [u32; 2]) -> HudBatch { let mut batch = HudBatch::new(size); - batch.patches.append(&mut self.map_patches); let (w, h) = (batch.width, batch.height); if self.screen == Screen::Options { self.draw_options(&mut batch); @@ -3129,7 +3129,7 @@ impl Game<'_> { } self.draw_slot_tooltip(batch); } - fn close_container(&mut self) { + pub(super) fn close_container(&mut self) { self.close_station(); if self.remote.is_some() { return self.remote_close_container(); @@ -6370,7 +6370,9 @@ impl Game<'_> { } else { Vec::new() }; - let hud = self.hud(size); + let mut hud = self.hud(size); + // Map textures changed since the last drawn frame (game/maps.rs). + hud.patches.append(&mut self.map_patches); let title = self.title.is_some(); let entities = if title { Vec::new() @@ -6686,6 +6688,7 @@ impl ApplicationHandler for Game<'_> { match event { WindowEvent::CloseRequested => { if self.title.is_none() { + self.close_container(); self.save_game()?; } event_loop.exit(); diff --git a/crates/horizon-client/src/game/cartography.rs b/crates/horizon-client/src/game/cartography.rs new file mode 100644 index 000000000..a59a31669 --- /dev/null +++ b/crates/horizon-client/src/game/cartography.rs @@ -0,0 +1,163 @@ +//! The cartography table (`CartographyTableMenu`, `CartographyTableScreen`): +//! zooming a map out with paper, cloning it with an empty map, locking it +//! with a glass pane, the post-processing on take (`MapItem.scaleMap`, +//! `lockMap`) and the preview with its error and lock sprites. +use super::{ + Game, + maps::{MapData, map_id}, + stations::Station, +}; +use horizon_client::{hud::HudBatch, items::ItemStack}; + +impl Game<'_> { + /// `setupResultSlot`. + pub(super) fn cartography_result(&self, s: &Station) -> Option { + let (map, extra) = (s.inputs[0].as_ref()?, s.inputs[1].as_ref()?); + let data = self.maps.data.get(&map_id(map)?)?; + match &*extra.item { + "minecraft:paper" if !data.locked && data.scale < 4 => Some(ItemStack { + count: 1, + ..map.clone() + }), + "minecraft:glass_pane" if !data.locked => Some(ItemStack { + count: 1, + ..map.clone() + }), + "minecraft:map" => Some(ItemStack { + count: 2, + ..map.clone() + }), + _ => None, + } + } + /// `onCraftedPostProcess` for the taken result: a scaled or locked copy + /// under a new map id. Also spends the inputs' extra item kind check. + pub(super) fn cartography_post(&mut self, mut result: ItemStack) -> ItemStack { + let extra = self + .station + .as_ref() + .and_then(|s| s.inputs[1].as_ref()) + .map(|e| e.item.clone()); + let Some(id) = map_id(&result) else { + return result; + }; + let new = match extra.as_deref() { + Some("minecraft:paper") => self.map(id).map(|m| m.scaled()), + Some("minecraft:glass_pane") => self.map(id).map(|m| m.locked_copy()), + _ => None, + }; + if let Some(data) = new { + let new_id = self.new_map_id(data); + result.data_mut().map_id = Some(new_id); + } + result + } + /// The take sound (once per tick). + pub(super) fn cartography_taken(&mut self, pos: [i32; 3]) { + let at = [ + f64::from(pos[0]) + 0.5, + f64::from(pos[1]) + 0.5, + f64::from(pos[2]) + 0.5, + ]; + self.audio.play( + "minecraft:ui.cartography_table.take_result", + Some(at), + 1.0, + 1.0, + ); + } + /// `CartographyTableScreen.extractBackground` after the background. + pub(super) fn draw_cartography(&mut self, batch: &mut HudBatch, ox: f32, oy: f32) { + let Some((map, extra)) = self + .station + .as_ref() + .map(|s| (s.inputs[0].clone(), s.inputs[1].clone())) + else { + return; + }; + let extra = extra.map(|e| e.item.to_string()).unwrap_or_default(); + let (duplicate, scaling, locking) = ( + extra == "minecraft:map", + extra == "minecraft:paper", + extra == "minecraft:glass_pane", + ); + let id = map.as_ref().and_then(map_id); + let state = id.and_then(|id| self.map(id).map(|m| (m.locked, m.scale))); + let mut locked = false; + let error = "gui/sprites/container/cartography_table/error"; + if let Some((is_locked, scale)) = state { + if is_locked { + locked = true; + if scaling || locking { + batch.sprite(&self.res.atlas, error, ox + 35.0, oy + 31.0); + } + } + if scaling && scale >= 4 { + locked = true; + batch.sprite(&self.res.atlas, error, ox + 35.0, oy + 31.0); + } + } + let sprite = |batch: &mut HudBatch, game: &Self, name: &str, x: f32, y: f32| { + batch.sprite( + &game.res.atlas, + &format!("gui/sprites/container/cartography_table/{name}"), + ox + x, + oy + y, + ); + }; + let preview = |batch: &mut HudBatch, game: &mut Self, x: f32, y: f32, scale: f32| { + if let Some(rect) = id + .filter(|_| state.is_some()) + .and_then(|id| game.map_texture(id)) + { + let size = 128.0 * scale; + batch.rect_uv(ox + x, oy + y, size, size, rect, u32::MAX); + } + }; + if scaling && !locked { + sprite(batch, self, "scaled_map", 67.0, 13.0); + preview(batch, self, 85.0, 31.0, 0.226); + } else if duplicate { + sprite(batch, self, "duplicated_map", 83.0, 13.0); + preview(batch, self, 86.0, 16.0, 0.34); + sprite(batch, self, "duplicated_map", 67.0, 29.0); + preview(batch, self, 70.0, 32.0, 0.34); + } else if locking { + sprite(batch, self, "map", 67.0, 13.0); + preview(batch, self, 71.0, 17.0, 0.45); + sprite(batch, self, "locked", 118.0, 60.0); + } else { + sprite(batch, self, "map", 67.0, 13.0); + preview(batch, self, 71.0, 17.0, 0.45); + } + } +} +impl MapData { + /// `MapItemSavedData.scaled`: a fresh map one scale up around the same centre. + pub(super) fn scaled(&self) -> MapData { + let mut m = MapData::fresh( + f64::from(self.center[0]), + f64::from(self.center[1]), + (self.scale + 1).min(4), + &self.dimension, + ); + m.tracking = self.tracking; + m.unlimited = self.unlimited; + m + } + /// `MapItemSavedData.locked`: the same pixels, locked. + pub(super) fn locked_copy(&self) -> MapData { + let mut m = MapData::fresh( + f64::from(self.center[0]), + f64::from(self.center[1]), + self.scale, + &self.dimension, + ); + m.center = self.center; + m.colors = self.colors.clone(); + m.tracking = self.tracking; + m.unlimited = self.unlimited; + m.locked = true; + m + } +} diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index be74c0e88..859909b38 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -384,6 +384,8 @@ impl Game<'_> { } "quit" => { if self.title.is_none() { + // Player.closeContainer: menu items go back before saving. + self.close_container(); self.save_game()?; } event_loop.exit(); diff --git a/crates/horizon-client/src/game/maps.rs b/crates/horizon-client/src/game/maps.rs index 9a6477c71..8f0992ae4 100644 --- a/crates/horizon-client/src/game/maps.rs +++ b/crates/horizon-client/src/game/maps.rs @@ -39,7 +39,7 @@ pub(super) struct MapData { } impl MapData { /// `MapItemSavedData.createFresh`: centred on the scale's grid cell. - fn new(x: f64, z: f64, scale: u8, dimension: &str) -> Self { + pub(super) fn fresh(x: f64, z: f64, scale: u8, dimension: &str) -> Self { let size = 128 * (1 << scale); let cell = |v: f64| (v.floor() as i32 + 64).div_euclid(size) * size + size / 2 - 64; Self { @@ -183,6 +183,12 @@ impl Game<'_> { self.maps.last_id = Some(last + 1); last + 1 } + /// Registers `data` under a free map id. + pub(super) fn new_map_id(&mut self, data: MapData) -> i32 { + let id = self.next_map_id(); + self.maps.data.insert(id, data); + id + } /// `EmptyMapItem.use`: a new map centred around the player. pub(super) fn use_empty_map(&mut self, stack: &ItemStack) -> bool { if &*stack.item != "minecraft:map" { @@ -192,7 +198,7 @@ impl Game<'_> { return false; }; let id = self.next_map_id(); - let data = MapData::new(p.x, p.z, 0, self.dimension_id()); + let data = MapData::fresh(p.x, p.z, 0, self.dimension_id()); self.maps.data.insert(id, data); let mut filled = ItemStack::new("minecraft:filled_map", 1); filled.data_mut().map_id = Some(id); diff --git a/crates/horizon-client/src/game/stations.rs b/crates/horizon-client/src/game/stations.rs index ef866a323..d1593cada 100644 --- a/crates/horizon-client/src/game/stations.rs +++ b/crates/horizon-client/src/game/stations.rs @@ -30,6 +30,8 @@ pub(super) enum Kind { Merchant, /// The beacon menu (game/beacon.rs). Beacon, + /// The cartography table (game/cartography.rs). + Cartography, } impl Kind { fn from_block(name: &str) -> Option { @@ -42,6 +44,7 @@ impl Kind { "minecraft:smithing_table" => Self::Smithing, "minecraft:stonecutter" => Self::Stonecutter, "minecraft:beacon" => Self::Beacon, + "minecraft:cartography_table" => Self::Cartography, _ => return None, }) } @@ -61,6 +64,7 @@ impl Kind { Self::Stonecutter => "gui/container/stonecutter", Self::Merchant => "gui/container/villager", Self::Beacon => "gui/container/beacon", + Self::Cartography => "gui/container/cartography_table", } } fn title_key(self) -> &'static str { @@ -72,6 +76,7 @@ impl Kind { Self::Stonecutter => "container.stonecutter", Self::Merchant => "merchant.trades", Self::Beacon => "container.beacon", + Self::Cartography => "container.cartography_table", } } /// (titleLabelX, titleLabelY). @@ -80,6 +85,7 @@ impl Kind { Self::Anvil => (60.0, 6.0), Self::Smithing => (44.0, 15.0), Self::Stonecutter => (8.0, 5.0), + Self::Cartography => (8.0, 4.0), _ => (8.0, 6.0), } } @@ -92,6 +98,7 @@ impl Kind { Self::Stonecutter => &[(20.0, 33.0)], Self::Merchant => &[(86.0, 37.0), (112.0, 37.0)], Self::Beacon => &[(109.0, 110.0)], + Self::Cartography => &[(15.0, 15.0), (15.0, 52.0)], }; table[i.min(table.len() - 1)] } @@ -104,6 +111,7 @@ impl Kind { Self::Stonecutter => Some((143.0, 33.0)), Self::Merchant => Some((170.0, 37.0)), Self::Beacon => None, + Self::Cartography => Some((145.0, 39.0)), } } } @@ -406,6 +414,7 @@ impl Game<'_> { } Kind::Merchant => super::trading::update(&mut s, items), Kind::Beacon => {} + Kind::Cartography => s.result = self.cartography_result(&s), Kind::Stonecutter => { let item = s.inputs[0].as_ref().map(|i| i.item.clone()); if item != s.input_item { @@ -467,6 +476,14 @@ impl Game<'_> { match s.kind { Kind::Enchanting if i == 0 => (true, 1), Kind::Beacon => (super::beacon::is_payment(&stack.item), 1), + Kind::Cartography if i == 0 => (super::maps::map_id(stack).is_some(), 64), + Kind::Cartography => ( + matches!( + &*stack.item, + "minecraft:paper" | "minecraft:map" | "minecraft:glass_pane" + ), + 64, + ), 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), @@ -641,7 +658,10 @@ impl Game<'_> { moved = self.move_to(&mut stack, &[INPUT], false); } } - Some(Kind::Smithing) | Some(Kind::Anvil) | Some(Kind::Stonecutter) => { + Some(Kind::Smithing) + | Some(Kind::Anvil) + | Some(Kind::Stonecutter) + | Some(Kind::Cartography) => { let n = self.station.as_ref().map_or(0, |s| s.inputs.len()); let inputs: Vec = (0..n).map(|i| INPUT + i).collect(); moved = self.move_to(&mut stack, &inputs, false); @@ -804,6 +824,20 @@ impl Game<'_> { } Kind::Merchant => self.merchant_take(&mut s), Kind::Beacon => {} + Kind::Cartography => { + for slot in &mut s.inputs { + if let Some(x) = slot.as_mut() { + x.count -= 1; + if x.count == 0 { + *slot = None; + } + } + } + if s.last_sound != self.ticks { + s.last_sound = self.ticks; + self.cartography_taken(pos); + } + } Kind::Enchanting => {} } self.station = Some(s); @@ -817,6 +851,15 @@ impl Game<'_> { if !self.may_take_result() { return; } + let result = if self + .station + .as_ref() + .is_some_and(|s| s.kind == Kind::Cartography) + { + self.cartography_post(result) + } else { + result + }; if shift { let mut stack = result.clone(); let player = Self::player_slots(); @@ -1225,18 +1268,24 @@ impl Game<'_> { } } else if let Some(kind) = self.station.as_ref().map(|s| s.kind) { let title = self.translate(kind.title_key(), ""); + { + let atlas = &self.res.atlas; + Self::part( + batch, + atlas, + kind.texture(), + (0.0, 0.0), + (176.0, 166.0), + (ox, oy), + ); + let (tx, ty) = kind.title_pos(); + batch.text(atlas, &title, ox + tx, oy + ty, GREY, false); + batch.text(atlas, &inventory_label, ox + 8.0, oy + 72.0, GREY, false); + } + if kind == Kind::Cartography { + self.draw_cartography(batch, ox, oy); + } let atlas = &self.res.atlas; - Self::part( - batch, - atlas, - kind.texture(), - (0.0, 0.0), - (176.0, 166.0), - (ox, oy), - ); - let (tx, ty) = kind.title_pos(); - batch.text(atlas, &title, ox + tx, oy + ty, GREY, false); - batch.text(atlas, &inventory_label, ox + 8.0, oy + 72.0, GREY, false); let s = self.station.as_ref().expect("open station"); let has_input = s.inputs.iter().any(Option::is_some); match kind { @@ -1526,7 +1575,7 @@ impl Game<'_> { } } } - Kind::Merchant | Kind::Beacon => {} + Kind::Merchant | Kind::Beacon | Kind::Cartography => {} Kind::Stonecutter => { let display = s.inputs[0].is_some() && !s.recipes.is_empty(); let active = display && s.recipes.len() > 12;