de8cb0dfa6
The last two workstation menus guests couldn't use, both now on the shared menu engine, which single player runs too: - Lectern: LecternMenu on the engine (Kind::Lectern): the page buttons (1, 2, 100+) and Take Book (3, mayBuild) as ContainerButtonClick, the page as data slot 0 clamped to the book, the book in its one slot (removeItem, setItem a no-op, max 1). The host opens minecraft:lectern for a guest, applies the menu to the block entity (powered pulse, page turn level event 1043, has_book), resends changes and closes it on stillValid. Guests drive it from the book screen. - Cartography table: CartographyTableMenu on the engine (Kind::Cartography, setupResultSlot, onTake, onCraftedBy's new map ids through a Maps env), replacing the old station code; guests get the same through guest_menu_op. - LAN item stacks carry map_post_processing and writable/written book contents (they were blank for guests); map_post_processing is no longer saved (ItemStack.CODEC never saves it). - A guest who enchants gets the enchant_item stat. - Click parity: VanillaMenuOracle and menu_fixture gain both menus. - lan_e2e: new lectern and cartography checks; enchanting checks the stat; server_count/server_reply ignore the guest's own operator broadcast echo on --server-thread; the grindstone check counts levels as well as points. Fixes #868 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
174 lines
6.1 KiB
Rust
174 lines
6.1 KiB
Rust
//! The cartography table (`CartographyTableMenu` on the menu engine,
|
|
//! `CartographyTableScreen`): zooming a map out with paper, cloning it with
|
|
//! an empty map, locking it with a glass pane. The engine reads the level's
|
|
//! maps through `LevelMaps`; the scaled and locked copies a take makes
|
|
//! (`MapItem.scaleMap`, `lockMap`) are registered after the click. The
|
|
//! screen draws the preview with its error and lock sprites.
|
|
use super::{
|
|
Game,
|
|
maps::{MapData, map_id},
|
|
};
|
|
use horizon_client::{hud::HudBatch, items::ItemStack, menu::Maps};
|
|
use std::{
|
|
cell::{Cell, RefCell},
|
|
collections::HashMap,
|
|
};
|
|
|
|
/// The level's maps as a menu sees them (`ContainerLevelAccess`): the
|
|
/// states of the maps the click can reach, and the copies it makes.
|
|
pub(super) struct LevelMaps {
|
|
states: RefCell<HashMap<i32, (bool, u8)>>,
|
|
next_id: Cell<i32>,
|
|
/// (new id, source id, scaled) for each copy made.
|
|
made: RefCell<Vec<(i32, i32, bool)>>,
|
|
}
|
|
|
|
impl Maps for LevelMaps {
|
|
fn state(&self, id: i32) -> Option<(bool, u8)> {
|
|
self.states.borrow().get(&id).copied()
|
|
}
|
|
|
|
fn derive(&self, id: i32, scale: bool) -> Option<i32> {
|
|
let (_, s) = self.state(id)?;
|
|
let new = self.next_id.get();
|
|
self.next_id.set(new + 1);
|
|
let made = if scale {
|
|
(false, (s + 1).min(4))
|
|
} else {
|
|
(true, s)
|
|
};
|
|
self.states.borrow_mut().insert(new, made);
|
|
self.made.borrow_mut().push((new, id, scale));
|
|
Some(new)
|
|
}
|
|
}
|
|
|
|
impl Game<'_> {
|
|
/// The maps of `stacks` (loaded as needed) for a menu over this level.
|
|
pub(super) fn level_maps<'s>(
|
|
&mut self,
|
|
stacks: impl IntoIterator<Item = &'s ItemStack>,
|
|
) -> LevelMaps {
|
|
let ids: Vec<i32> = stacks.into_iter().filter_map(map_id).collect();
|
|
let mut states = HashMap::new();
|
|
for id in ids {
|
|
if let Some(m) = self.map(id) {
|
|
states.insert(id, (m.locked, m.scale));
|
|
}
|
|
}
|
|
LevelMaps {
|
|
states: RefCell::new(states),
|
|
next_id: Cell::new(self.peek_map_id()),
|
|
made: RefCell::new(Vec::new()),
|
|
}
|
|
}
|
|
|
|
/// The scaled and locked copies a menu made (`level.setMapData`).
|
|
pub(super) fn register_made_maps(&mut self, maps: LevelMaps) {
|
|
for (id, source, scale) in maps.made.into_inner() {
|
|
let Some(data) = self
|
|
.map(source)
|
|
.map(|m| if scale { m.scaled() } else { m.locked_copy() })
|
|
else {
|
|
continue;
|
|
};
|
|
let new = self.new_map_id(data);
|
|
debug_assert_eq!(new, id, "map ids follow peek_map_id");
|
|
}
|
|
}
|
|
/// `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.gui.atlas, error, ox + 35.0, oy + 31.0);
|
|
}
|
|
}
|
|
if scaling && scale >= 4 {
|
|
locked = true;
|
|
batch.sprite(&self.gui.atlas, error, ox + 35.0, oy + 31.0);
|
|
}
|
|
}
|
|
let sprite = |batch: &mut HudBatch, game: &Self, name: &str, x: f32, y: f32| {
|
|
batch.sprite(
|
|
&game.gui.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
|
|
}
|
|
}
|