Archive incari-client (client, 26.3, clean)

This commit is contained in:
2026-11-28 00:54:38 -06:00
parent 7a6174687f
commit 12838e838c
2383 changed files with 280266 additions and 0 deletions
@@ -0,0 +1,4 @@
// What a section's ghosts were made from: meshed again when it changes.
fn incari_schematics_ghost_revision(section: [i32; 3], revision: &mut u64) {
crate::game::incari_schematics_world::ghost_revision(section, revision);
}
@@ -0,0 +1,8 @@
// A section's ghost blocks, while Horizon meshes it.
fn incari_schematics_ghost_section(
game: &crate::game::Game<'_>,
section: [i32; 3],
ghosts: &mut Vec<crate::game::native_hooks::NativeGhost>,
) {
crate::game::incari_schematics_world::ghost_section(game, section, ghosts);
}
@@ -0,0 +1,7 @@
// The ghosts' box, opacity and range this frame, and the wrong blocks' fills.
fn incari_schematics_ghosts(
game: &crate::game::Game<'_>,
ghosts: &mut crate::game::native_hooks::NativeGhosts,
) {
crate::game::incari_schematics_world::ghosts(game, ghosts);
}
@@ -0,0 +1,103 @@
# Incari's Schematics (features/schematics, ADR 0066) for the Rust build (ADR 0068, 0069). A
# structure from a .litematic, .schem or .nbt placed in the world as see-through ghost blocks to
# build by. The readers, placement, checking, material list and HUD are the client's
# (`incari_core::schematic`, no game types); the chooser is its screen (`file_chooser`). This mod is
# their game side: the game's blocks, the module's keys, its HUD elements, and the ghosts and boxes
# drawn in the world.
#
# The hook points `lines`, `ghosts`, `ghost_revision` and `ghost_section` are still pending
# fragments (tools/native-hook-fragments/incari_client_hooks.json and incari_shell_hooks.json), so
# they're targeted by function. Make each `hook = "<name>"` and `hook_version = 1` once Horizon
# stamps them.
[mod]
id = "incari_schematics"
version = "0.1.0"
priority = 100
[[module]]
id = "runtime"
file = "crates/horizon-client/src/game.rs"
name = "incari_schematics"
source = "runtime.rs"
[[module]]
id = "world"
file = "crates/horizon-client/src/game.rs"
name = "incari_schematics_world"
source = "world.rs"
[[injection]]
id = "tick"
hook = "tick"
hook_version = 1
kind = "HEAD"
helper = "tick.rs"
bindings = [
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
]
[[injection]]
id = "key"
hook = "key"
hook_version = 1
kind = "HEAD"
helper = "key.rs"
bindings = [
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
{ name = "key", ty = "&str", value = "key" },
{ name = "pressed", ty = "bool", value = "pressed" },
]
[[injection]]
id = "hud"
hook = "hud"
hook_version = 1
kind = "HEAD"
helper = "hud.rs"
bindings = [
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
{ name = "batch", ty = "&mut horizon_client::hud::HudBatch", value = "&mut *batch" },
{ name = "corners", ty = "&mut horizon_client::extras_hud::Corners", value = "&mut *corners" },
]
[[injection]]
id = "lines"
target = "horizon_client::game::native_hooks::Game<'_>::native_lines"
kind = "HEAD"
helper = "lines.rs"
bindings = [
{ name = "game", ty = "&crate::game::Game<'_>", value = "self" },
{ name = "partial", ty = "f32", value = "partial" },
{ name = "lines", ty = "&mut Vec<crate::game::native_hooks::NativeLine>", value = "&mut *lines" },
]
[[injection]]
id = "ghosts"
target = "horizon_client::game::native_hooks::Game<'_>::native_ghosts"
kind = "HEAD"
helper = "ghosts.rs"
bindings = [
{ name = "game", ty = "&crate::game::Game<'_>", value = "self" },
{ name = "ghosts", ty = "&mut crate::game::native_hooks::NativeGhosts", value = "&mut *ghosts" },
]
[[injection]]
id = "ghost_revision"
target = "horizon_client::game::native_hooks::Game<'_>::native_ghost_revision"
kind = "HEAD"
helper = "ghost_revision.rs"
bindings = [
{ name = "section", ty = "[i32; 3]", value = "section" },
{ name = "revision", ty = "&mut u64", value = "&mut *revision" },
]
[[injection]]
id = "ghost_section"
target = "horizon_client::game::native_hooks::Game<'_>::native_ghost_section"
kind = "HEAD"
helper = "ghost_section.rs"
bindings = [
{ name = "game", ty = "&crate::game::Game<'_>", value = "self" },
{ name = "section", ty = "[i32; 3]", value = "section" },
{ name = "ghosts", ty = "&mut Vec<crate::game::native_hooks::NativeGhost>", value = "&mut *ghosts" },
]
@@ -0,0 +1,9 @@
// The Schematic and Material list elements, where the player placed them.
fn incari_schematics_hud(
game: &mut crate::game::Game<'_>,
batch: &mut horizon_client::hud::HudBatch,
corners: &mut horizon_client::extras_hud::Corners,
) {
let _ = corners;
crate::game::incari_schematics_world::hud(game, batch);
}
@@ -0,0 +1,4 @@
// The module's keys (Place here, Turn, Mirror, Nudge, Up, Layer), while playing.
fn incari_schematics_key(game: &mut crate::game::Game<'_>, key: &str, pressed: bool) {
crate::game::incari_schematics_world::key(game, key, pressed);
}
@@ -0,0 +1,9 @@
// The schematic's box and its wrong blocks' edges.
fn incari_schematics_lines(
game: &crate::game::Game<'_>,
partial: f32,
lines: &mut Vec<crate::game::native_hooks::NativeLine>,
) {
let _ = partial;
crate::game::incari_schematics_world::lines(game, lines);
}
@@ -0,0 +1,122 @@
// Incari's Schematics for the Rust build (ADR 0066, 0068, 0069): the bits of its game side that are
// plain arithmetic. The module itself (readers, placement, checking, materials, HUD) is the
// client's, `incari_core::schematic`; this file is both the module the bundle adds to the game and,
// through rust/lib.rs, the code its tests run: it uses no game types.
/// The module's id, as the Java client's profiles name it.
pub const ID: &str = "schematics";
/// What the browser's file picker is asked for with, to tell its file from another asker's.
#[cfg_attr(not(target_arch = "wasm32"), allow(dead_code))] // the browser's file picker only
pub const PICK_KIND: &str = "incari_schematics";
/// A world block's place in its 16-block section, as the ghost hook packs it: `y << 8 | z << 4 | x`.
#[allow(dead_code)] // the inverse of `cell_block`, which the tests hold it to
pub fn cell(x: i32, y: i32, z: i32) -> u16 {
((y & 15) << 8 | (z & 15) << 4 | (x & 15)) as u16
}
/// The world block at `cell` of section `section`.
pub fn cell_block(section: [i32; 3], cell: u16) -> [i32; 3] {
let c = i32::from(cell);
[
section[0] << 4 | (c & 15),
section[1] << 4 | (c >> 8),
section[2] << 4 | ((c >> 4) & 15),
]
}
/// A box's twelve edges, from block corner `min` to `max`.
pub fn box_edges(min: [i32; 3], max: [i32; 3]) -> [([f64; 3], [f64; 3]); 12] {
let (a, b) = (min.map(f64::from), max.map(f64::from));
let p = |x: bool, y: bool, z: bool| {
[
if x { b[0] } else { a[0] },
if y { b[1] } else { a[1] },
if z { b[2] } else { a[2] },
]
};
[
// Along x.
(p(false, false, false), p(true, false, false)),
(p(false, true, false), p(true, true, false)),
(p(false, false, true), p(true, false, true)),
(p(false, true, true), p(true, true, true)),
// Along y.
(p(false, false, false), p(false, true, false)),
(p(true, false, false), p(true, true, false)),
(p(false, false, true), p(false, true, true)),
(p(true, false, true), p(true, true, true)),
// Along z.
(p(false, false, false), p(false, false, true)),
(p(true, false, false), p(true, false, true)),
(p(false, true, false), p(false, true, true)),
(p(true, true, false), p(true, true, true)),
]
}
/// The translation keys an item's name may be under: blocks' items are named by their block.
pub fn name_keys(id: &str) -> [String; 2] {
let (ns, path) = id.split_once(':').unwrap_or(("minecraft", id));
[format!("item.{ns}.{path}"), format!("block.{ns}.{path}")]
}
/// Which world the player is in, to see them leave it (the schematic's comparison starts afresh,
/// as Java's LEAVE event does).
#[derive(Default)]
pub struct WorldWatch {
world: Option<String>,
}
impl WorldWatch {
/// The world now (`native_world_id`); true when the player just left one.
pub fn left(&mut self, world: Option<&str>) -> bool {
if self.world.as_deref() == world {
return false;
}
let was = self.world.is_some();
self.world = world.map(str::to_owned);
was
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cells_pack_and_unpack() {
for [x, y, z] in [[0, 0, 0], [-1, 70, 33], [15, -64, -17]] {
let section = [x >> 4, y >> 4, z >> 4];
assert_eq!(cell_block(section, cell(x, y, z)), [x, y, z]);
}
assert_eq!(cell(1, 2, 3), 2 << 8 | 3 << 4 | 1);
}
#[test]
fn a_box_has_twelve_unit_long_edges() {
let edges = box_edges([0, 0, 0], [1, 2, 3]);
let lengths: Vec<f64> = edges
.iter()
.map(|(a, b)| (0..3).map(|i| (b[i] - a[i]).abs()).sum())
.collect();
assert_eq!(
lengths,
[1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0]
);
}
#[test]
fn names_and_worlds() {
assert_eq!(
name_keys("minecraft:stone"),
["item.minecraft.stone", "block.minecraft.stone"]
);
let mut w = WorldWatch::default();
assert!(!w.left(Some("world/a")));
assert!(!w.left(Some("world/a")));
assert!(w.left(Some("server/b")));
assert!(w.left(None));
assert!(!w.left(None));
}
}
@@ -0,0 +1,4 @@
// Once a tick: a chosen file loaded, buttons done, the world compared, messages shown.
fn incari_schematics_tick(game: &mut crate::game::Game<'_>) {
crate::game::incari_schematics_world::tick(game);
}
@@ -0,0 +1,265 @@
// The Schematics mod's game side (module `incari_schematics_world`): the game's blocks for the
// client's schematic code, the module's keys, its HUD elements, and the ghosts, boxes and edges it
// draws in the world through the fork's hook points. The logic is the client's
// (`incari_core::schematic`); this is its glue. Not tested alone: `./incari build --only rust`
// compiles it.
use crate::game::incari_client as c;
use crate::game::incari_schematics as m;
use crate::game::native_hooks::{NativeFill, NativeGhost, NativeGhosts, NativeLine};
use horizon_world::registry::BlockStateId;
use incari_core::schematic::hud::{self as panels, Items};
use incari_core::schematic::session::{self, Ghosts, Keys, Look, Player, WorldBox};
use incari_core::schematic::{Blocks, Mirror, UNKNOWN};
use std::cell::RefCell;
use std::rc::Rc;
/// The game's blocks and world, as the schematic code reads them.
struct World<'a, 'g>(&'a super::Game<'g>);
impl Blocks for World<'_, '_> {
fn state(&self, name: &str, properties: &[(String, String)], data_version: i32) -> u32 {
// Horizon has no data fixer: a state by an old name isn't found, and is left out.
let _ = data_version;
let props: Vec<(&str, &str)> = properties.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect();
// Strictly first; a property this version doesn't know then leaves the block as near as can be.
self.0
.native_state_of(name, &props, true)
.or_else(|| self.0.native_state_of(name, &props, false))
.map_or(UNKNOWN, |s| s.0)
}
fn transform(&self, state: u32, mirror: Mirror, turns: u8) -> u32 {
let code = match mirror {
Mirror::None => 0,
Mirror::X => 1,
Mirror::Z => 2,
};
self.0.native_state_transform(BlockStateId(state), code, turns).0
}
fn at(&self, x: i32, y: i32, z: i32) -> u32 {
self.0.native_state_at([x, y, z]).map_or(UNKNOWN, |s| s.0)
}
fn is_air(&self, state: u32) -> bool {
state != UNKNOWN && self.0.native_state_is_air(BlockStateId(state))
}
fn block(&self, state: u32) -> u32 {
self.0.native_state_default(BlockStateId(state)).0
}
fn item_id(&self, state: u32) -> String {
self.0.native_state_item(BlockStateId(state)).unwrap_or_default()
}
}
/// Raw deflate for the gzipped files, at most `limit` bytes.
fn inflate(data: &[u8], limit: usize) -> Option<Vec<u8>> {
miniz_oxide::inflate::decompress_to_vec_with_limit(data, limit).ok()
}
/// The game folder (where the `schematics` folder is).
fn game_dir() -> std::path::PathBuf {
horizon_os::env::current_dir().unwrap_or_default()
}
/// The chooser's picker (the browser's) and writer.
const PLATFORM: incari_core::screens::file_chooser::Platform = incari_core::screens::file_chooser::Platform {
pick: |accept| {
#[cfg(target_arch = "wasm32")]
horizon_os::web::pick_file(m::PICK_KIND, accept);
#[cfg(not(target_arch = "wasm32"))]
let _ = accept;
},
take_picked: || {
#[cfg(target_arch = "wasm32")]
{
horizon_os::web::take_picked_named(m::PICK_KIND)
}
#[cfg(not(target_arch = "wasm32"))]
{
None
}
},
write: |path, data| {
if !incari_core::host_extras::safe_path(path) {
return false;
}
let path = game_dir().join(path);
if let Some(dir) = path.parent() {
let _ = horizon_os::fs::create_dir_all(dir);
}
horizon_os::fs::write(&path, data).is_ok()
},
make_folder: |path| {
if incari_core::host_extras::safe_path(path) {
let _ = horizon_os::fs::create_dir_all(game_dir().join(path));
}
},
};
#[derive(Default)]
struct State {
installed: bool,
watch: m::WorldWatch,
/// This frame's boxes (scratch, kept so a frame allocates nothing once warm).
boxes: Vec<WorldBox>,
}
// The window's thread only: ticks, frames, keys and the HUD all run there.
thread_local! {
static STATE: RefCell<State> = RefCell::new(State::default());
}
fn player(game: &super::Game<'_>) -> Option<Player> {
game.native_player().map(|p| Player {
pos: [p.x, p.y, p.z],
yaw: p.yaw,
dimension: incari_core::schematic::session::dimension_key(&p.dimension),
})
}
/// Once a tick, the window's.
pub(super) fn tick(game: &mut super::Game<'_>) {
if !game.native_window() || !c::ready() {
return;
}
let world = game.native_world_id();
let look = c::settings(m::ID, Look::of);
let g: &super::Game<'_> = game;
let blocks = World(g);
let me = player(g);
let left = STATE.with(|cell| {
let mut state = cell.borrow_mut();
if !state.installed {
state.installed = true;
incari_core::screens::file_chooser::install(PLATFORM);
}
state.watch.left(world.as_deref())
});
let said = session::with(|s| {
if left {
s.left_world(&blocks);
}
if let Some(look) = &look {
s.tick(&blocks, me, look, inflate);
}
(s.take_messages(), std::mem::take(&mut s.wants_one_layer))
});
let Some((messages, one_layer)) = said else {
return;
};
for message in messages {
game.native_chat(&message);
}
if one_layer {
// Layers: One layer (LAYERS' third choice).
c::set_setting(m::ID, "layers", incari_core::framework::setting::Val::Choice(2));
}
}
/// The module's keys, while playing; sneaking reverses Turn, Nudge, Up and Layer.
pub(super) fn key(game: &mut super::Game<'_>, name: &str, pressed: bool) {
if !pressed || !game.native_playing() {
return;
}
let Some((keys, look)) = c::settings(m::ID, |s| (Keys::of(s), Look::of(s))) else {
return;
};
let sneaking = game.native_mapping("key.sneak").is_some_and(|(down, _)| down);
let Some(action) = keys.action(incari_core::input::Key::from_name(name), sneaking) else {
return;
};
let g: &super::Game<'_> = game;
let blocks = World(g);
let me = player(g);
session::with(|s| s.act(action, &blocks, me, &look));
}
/// The two HUD elements. Hot path: their lines are cached by the client's code.
pub(super) fn hud(game: &mut super::Game<'_>, batch: &mut horizon_client::hud::HudBatch) {
let Some(look) = c::settings(m::ID, Look::of) else {
return;
};
let g: &super::Game<'_> = game;
c::element(g, batch, m::ID, "status", |canvas| session::with(|s| panels::status(canvas, s, &look)).flatten());
// An item's name: its own, else its block's (the game's text for a missing key is the key).
let name = |id: &str| {
let keys = m::name_keys(id);
keys.iter().map(|k| g.native_translate(k)).find(|t| !keys.contains(t)).unwrap_or_default()
};
let in_world = g.native_player().is_some();
let carried = |id: &str| in_world.then(|| g.native_item_count(id));
let items = Items { name: &name, carried: &carried };
c::element(g, batch, m::ID, "materials", |canvas| session::with(|s| panels::materials(canvas, s, &items)).flatten());
}
/// This frame's boxes round the player, into `STATE.boxes`, then `f` with them.
fn with_boxes<R>(game: &super::Game<'_>, look: &Look, f: impl FnOnce(&[WorldBox]) -> R) -> Option<R> {
let p = game.native_player()?;
STATE.with(|cell| {
let mut state = cell.try_borrow_mut().ok()?;
let state = &mut *state;
session::with(|s| s.boxes([p.x, p.y, p.z], look, &mut state.boxes))?;
Some(f(&state.boxes))
})
}
/// The schematic's box and its wrong blocks' edges. Horizon keeps 1024 lines a frame: the
/// outline's first, then each problem's while there's room (their fills show them all).
pub(super) fn lines(game: &super::Game<'_>, lines: &mut Vec<NativeLine>) {
let Some(look) = c::settings(m::ID, Look::of) else {
return;
};
with_boxes(game, &look, |boxes| {
for b in boxes {
if lines.len() + 12 > 1024 {
break;
}
lines.extend(m::box_edges(b.min, b.max).map(|(start, end)| NativeLine { start, end, argb: b.argb, width: 2.0 }));
}
});
}
/// The ghosts' box, opacity and range this frame, and the problems' fills.
pub(super) fn ghosts(game: &super::Game<'_>, ghosts: &mut NativeGhosts) {
let Some(look) = c::settings(m::ID, Look::of) else {
return;
};
let shown = session::with(|s| s.loaded().map(|l| (s.ghost_bounds(), Rc::as_ptr(&l.schematic) as u64))).flatten();
let Some((bounds, source)) = shown else {
return;
};
if look.ghosts != Ghosts::None {
ghosts.source = source;
ghosts.bounds = bounds;
ghosts.alpha = look.alpha();
ghosts.range = look.range as u32;
}
with_boxes(game, &look, |boxes| {
ghosts.fills.extend(boxes.iter().filter(|b| b.fill != 0).map(|b| NativeFill {
min: b.min.map(f64::from),
max: b.max.map(f64::from),
argb: b.fill,
}));
});
}
/// What section `section`'s ghosts were made from.
pub(super) fn ghost_revision(section: [i32; 3], revision: &mut u64) {
if let Some(r) = session::with(|s| s.ghost_revision(section)) {
*revision = r;
}
}
/// Section `section`'s ghosts, while Horizon meshes it.
pub(super) fn ghost_section(game: &super::Game<'_>, section: [i32; 3], ghosts: &mut Vec<NativeGhost>) {
let Some(look) = c::settings(m::ID, Look::of) else {
return;
};
let blocks = World(game);
session::with(|s| {
for at in 0..4096u16 {
let [x, y, z] = m::cell_block(section, at);
if let Some(state) = s.ghost_at(x, y, z, &look, &blocks) {
ghosts.push(NativeGhost { at, state: BlockStateId(state) });
}
}
});
}