Archive incari-client (client, 26.3, clean)
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
# Incari's Coordinates (features/coordinates) for the Rust build (ADR 0068, 0069). Hooks by name only
|
||||
# (native-hooks.json). Its settings and HUD elements are the client's (rust/mods/incari): the module
|
||||
# menu shows them and the HUD editor places them.
|
||||
[mod]
|
||||
id = "incari_coordinates"
|
||||
version = "0.1.0"
|
||||
priority = 100
|
||||
|
||||
[[module]]
|
||||
id = "runtime"
|
||||
file = "crates/horizon-client/src/game.rs"
|
||||
name = "incari_coordinates"
|
||||
source = "runtime.rs"
|
||||
|
||||
[[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" },
|
||||
]
|
||||
@@ -0,0 +1,58 @@
|
||||
// The position rows on the glass, where the player placed them (the Java module's Position).
|
||||
fn incari_coordinates_hud(
|
||||
game: &mut crate::game::Game<'_>,
|
||||
batch: &mut horizon_client::hud::HudBatch,
|
||||
corners: &mut horizon_client::extras_hud::Corners,
|
||||
) {
|
||||
use crate::game::{incari_client as c, incari_coordinates as m};
|
||||
use incari_core::hud::{LINE, PAD_X, PAD_Y};
|
||||
use incari_core::theme::Tone;
|
||||
let _ = corners;
|
||||
let spot = game.native_player().map(|p| m::Spot {
|
||||
at: [m::block(p.x), m::block(p.y), m::block(p.z)],
|
||||
overworld_at: [m::overworld_block(p.x), m::overworld_block(p.z)],
|
||||
sector: m::sector(p.yaw),
|
||||
nether: p.dimension == m::NETHER,
|
||||
overworld: p.dimension == m::OVERWORLD,
|
||||
});
|
||||
c::element(game, batch, m::ID, m::ELEMENT, |canvas| {
|
||||
let spot = spot.or(canvas.editing.then_some(m::SAMPLE))?;
|
||||
let s = canvas.module;
|
||||
let shown = m::Shown {
|
||||
one_line: s.choice("layout") == "line",
|
||||
facing: s.bool("show-facing"),
|
||||
other: s.bool("show-other"),
|
||||
};
|
||||
let background = s.bool("background");
|
||||
m::with_rows(spot, shown, |rows| {
|
||||
if rows.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let (pad, pad_y) = if background { (PAD_X, PAD_Y) } else { (0.0, 0.0) };
|
||||
let label_width = |canvas: &incari_core::hud::HudCanvas<'_, '_>, label: &str| {
|
||||
if label.is_empty() { 0.0 } else { canvas.text_width(label) + m::LABEL_GAP }
|
||||
};
|
||||
let width = rows
|
||||
.iter()
|
||||
.map(|(label, value)| label_width(&*canvas, label) + canvas.text_width(value))
|
||||
.fold(0.0, f32::max);
|
||||
let w = width + pad * 2.0;
|
||||
let h = rows.len() as f32 * LINE + if background { pad_y * 2.0 - 1.0 } else { -1.0 };
|
||||
if background {
|
||||
canvas.fill(0.0, 0.0, w, h, Tone::Background);
|
||||
}
|
||||
let mut y = pad_y;
|
||||
for (i, (label, value)) in rows.iter().enumerate() {
|
||||
let mut x = pad;
|
||||
if !label.is_empty() {
|
||||
canvas.text(label, x, y, Tone::Accent);
|
||||
x += label_width(&*canvas, label);
|
||||
}
|
||||
let tone = if m::muted(i, label) { Tone::Muted } else { Tone::Text };
|
||||
canvas.text(value, x, y, tone);
|
||||
y += LINE;
|
||||
}
|
||||
Some([w, h])
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
// Incari's Coordinates for the Rust build (ADR 0068, 0069), as features/coordinates does it: the
|
||||
// block you're in, which way you face, and the matching spot in the other dimension, label beside
|
||||
// value on the glass. Horizon's native API has no biome yet, so that row isn't here. Its settings
|
||||
// and HUD element are the module's in `incari_core::modules` (the Java ids). No game types:
|
||||
// rust/lib.rs tests it.
|
||||
|
||||
/// The module's id, as the Java client's profiles name it.
|
||||
pub const ID: &str = "coordinates";
|
||||
/// Its HUD element's id.
|
||||
pub const ELEMENT: &str = "position";
|
||||
|
||||
pub const NETHER: &str = "minecraft:the_nether";
|
||||
pub const OVERWORLD: &str = "minecraft:overworld";
|
||||
|
||||
/// Between a row's label and its value.
|
||||
pub const LABEL_GAP: f32 = 4.0;
|
||||
|
||||
/// The block a coordinate is in: rounded down, so -0.5 is block -1.
|
||||
pub fn block(coordinate: f64) -> i32 {
|
||||
coordinate.floor() as i32
|
||||
}
|
||||
|
||||
/// Which eighth of the compass the player faces, 0 north, 2 east, from Minecraft's yaw (0 faces
|
||||
/// south, 90 west): the Java module's bearing, yaw + 180.
|
||||
pub fn sector(yaw: f32) -> usize {
|
||||
let heading = (yaw + 180.0).rem_euclid(360.0);
|
||||
((heading / 45.0).round() as usize) % 8
|
||||
}
|
||||
|
||||
/// The compass point of `sector`, with the axis that way runs along.
|
||||
pub fn facing(sector: usize) -> &'static str {
|
||||
[
|
||||
"North (-Z)",
|
||||
"Northeast",
|
||||
"East (+X)",
|
||||
"Southeast",
|
||||
"South (+Z)",
|
||||
"Southwest",
|
||||
"West (-X)",
|
||||
"Northwest",
|
||||
][sector % 8]
|
||||
}
|
||||
|
||||
/// The Overworld block matching a Nether position: from the exact position, as a Nether block
|
||||
/// spans 8 Overworld ones (x 100.9 is 807, not 800).
|
||||
pub fn overworld_block(nether: f64) -> i32 {
|
||||
block(nether * 8.0)
|
||||
}
|
||||
|
||||
/// What the rows are made from.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Spot {
|
||||
pub at: [i32; 3],
|
||||
/// The Overworld x and z matching the exact position, for a player in the Nether.
|
||||
pub overworld_at: [i32; 2],
|
||||
pub sector: usize,
|
||||
pub nether: bool,
|
||||
pub overworld: bool,
|
||||
}
|
||||
|
||||
/// The HUD editor's stand-in with no world: the Java sample player, at 0 0 0 in the Overworld
|
||||
/// facing north.
|
||||
pub const SAMPLE: Spot = Spot {
|
||||
at: [0, 0, 0],
|
||||
overworld_at: [0, 0],
|
||||
sector: 0,
|
||||
nether: false,
|
||||
overworld: true,
|
||||
};
|
||||
|
||||
/// What the settings ask for.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Shown {
|
||||
pub one_line: bool,
|
||||
pub facing: bool,
|
||||
pub other: bool,
|
||||
}
|
||||
|
||||
pub const MAX_ROWS: usize = 5;
|
||||
|
||||
/// A row: its label ("" for none) and its value.
|
||||
pub type Row = (&'static str, String);
|
||||
|
||||
/// The rows for `spot`, up to `MAX_ROWS`.
|
||||
pub fn rows(spot: Spot, shown: Shown) -> Vec<Row> {
|
||||
let [x, y, z] = spot.at;
|
||||
let mut rows = Vec::with_capacity(MAX_ROWS);
|
||||
if shown.one_line {
|
||||
rows.push(("", format!("X {x} Y {y} Z {z}")));
|
||||
} else {
|
||||
rows.extend([
|
||||
("X", x.to_string()),
|
||||
("Y", y.to_string()),
|
||||
("Z", z.to_string()),
|
||||
]);
|
||||
}
|
||||
if shown.facing {
|
||||
rows.push(("", facing(spot.sector).to_owned()));
|
||||
}
|
||||
if shown.other && spot.nether {
|
||||
let [ox, oz] = spot.overworld_at;
|
||||
rows.push(("Overworld", format!("{ox}, {oz}")));
|
||||
} else if shown.other && spot.overworld {
|
||||
rows.push((
|
||||
"Nether",
|
||||
format!("{}, {}", x.div_euclid(8), z.div_euclid(8)),
|
||||
));
|
||||
}
|
||||
rows
|
||||
}
|
||||
|
||||
/// A value's tone: a row without a label after the first is quieter (the facing, as the Java
|
||||
/// module draws it).
|
||||
pub fn muted(row: usize, label: &str) -> bool {
|
||||
label.is_empty() && row >= 1
|
||||
}
|
||||
|
||||
type Cached = (Option<(Spot, Shown)>, Vec<Row>);
|
||||
|
||||
// Hot path, don't allocate: made again only when the block, the facing or a setting changes.
|
||||
thread_local! {
|
||||
static ROWS: std::cell::RefCell<Cached> = const { std::cell::RefCell::new((None, Vec::new())) };
|
||||
}
|
||||
|
||||
/// Calls `f` with `rows(spot, shown)`, made again only when one of them changed.
|
||||
pub fn with_rows<R>(spot: Spot, shown: Shown, f: impl FnOnce(&[Row]) -> R) -> R {
|
||||
ROWS.with(|cell| {
|
||||
let mut cached = cell.borrow_mut();
|
||||
if cached.0 != Some((spot, shown)) {
|
||||
*cached = (Some((spot, shown)), rows(spot, shown));
|
||||
}
|
||||
f(&cached.1)
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn spot(at: [i32; 3], nether: bool) -> Spot {
|
||||
Spot {
|
||||
at,
|
||||
overworld_at: [at[0] * 8, at[2] * 8],
|
||||
sector: 0,
|
||||
nether,
|
||||
overworld: !nether,
|
||||
}
|
||||
}
|
||||
|
||||
const ALL: Shown = Shown {
|
||||
one_line: false,
|
||||
facing: true,
|
||||
other: true,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn faces_as_the_java_module_does() {
|
||||
// Minecraft's yaw: 180 faces north, -90 east, 0 south, 90 west.
|
||||
assert_eq!(facing(sector(180.0)), "North (-Z)");
|
||||
assert_eq!(facing(sector(-90.0)), "East (+X)");
|
||||
assert_eq!(facing(sector(0.0)), "South (+Z)");
|
||||
assert_eq!(facing(sector(90.0)), "West (-X)");
|
||||
assert_eq!(facing(sector(-135.0)), "Northeast");
|
||||
assert_eq!(facing(sector(-180.0 - 720.0)), "North (-Z)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rounds_down_to_the_block() {
|
||||
assert_eq!([block(-0.5), block(0.5), block(-16.0)], [-1, 0, -16]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shows_the_other_dimension() {
|
||||
let rows = rows(spot([-9, 64, 17], false), ALL);
|
||||
assert_eq!(rows.len(), MAX_ROWS);
|
||||
assert_eq!(rows[4], ("Nether", "-2, 2".to_owned()));
|
||||
let line = Shown {
|
||||
one_line: true,
|
||||
facing: false,
|
||||
other: true,
|
||||
};
|
||||
assert_eq!(
|
||||
super::rows(spot([3, 70, -1], true), line),
|
||||
[
|
||||
("", "X 3 Y 70 Z -1".to_owned()),
|
||||
("Overworld", "24, -8".to_owned())
|
||||
]
|
||||
);
|
||||
let end = Spot {
|
||||
overworld: false,
|
||||
..spot([0, 0, 0], false)
|
||||
};
|
||||
assert_eq!(super::rows(end, line).len(), 1);
|
||||
assert_eq!(super::rows(SAMPLE, ALL)[3], ("", "North (-Z)".to_owned()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_overworlds_spot_is_from_the_exact_position() {
|
||||
// Block 100 and -1 in the Nether, but 807 and -4 in the Overworld: not 800 and -8.
|
||||
assert_eq!([overworld_block(100.9), overworld_block(-0.5)], [807, -4]);
|
||||
let there = Spot {
|
||||
overworld_at: [807, -4],
|
||||
..spot([100, 70, -1], true)
|
||||
};
|
||||
assert_eq!(rows(there, ALL)[4], ("Overworld", "807, -4".to_owned()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quietens_the_unlabelled_rows_after_the_first() {
|
||||
assert!(!muted(0, ""));
|
||||
assert!(muted(1, ""));
|
||||
assert!(!muted(3, "Nether"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn makes_the_rows_again_only_when_something_changed() {
|
||||
let s = spot([1, 2, 3], false);
|
||||
let first = with_rows(s, ALL, |r| r.as_ptr());
|
||||
assert_eq!(with_rows(s, ALL, |r| r.as_ptr()), first);
|
||||
let line = Shown {
|
||||
one_line: true,
|
||||
facing: false,
|
||||
other: false,
|
||||
};
|
||||
assert_eq!(with_rows(s, line, <[_]>::len), 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user