Archive incari-client (client, 26.3, clean)
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
// After Horizon composed the map this frame: the next time it asks is to draw it in a corner.
|
||||
fn incari_minimap_frame(game: &mut crate::game::Game<'_>) {
|
||||
let _ = game;
|
||||
crate::game::incari_minimap::set_drawing(true);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
# Incari's Minimap (features/minimap) for the Rust build (ADR 0068, 0069). Hooks by name where Horizon
|
||||
# has stamped the hook point (native-hooks.json), by function where it's still pending. Its
|
||||
# settings and HUD element are the client's (rust/mods/incari): the module menu shows them and the HUD editor places it.
|
||||
[mod]
|
||||
id = "incari_minimap"
|
||||
version = "0.1.0"
|
||||
priority = 100
|
||||
|
||||
[[module]]
|
||||
id = "runtime"
|
||||
file = "crates/horizon-client/src/game.rs"
|
||||
name = "incari_minimap"
|
||||
source = "runtime.rs"
|
||||
|
||||
[[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 = "minimap"
|
||||
hook = "minimap"
|
||||
hook_version = 1
|
||||
kind = "HEAD"
|
||||
helper = "minimap.rs"
|
||||
bindings = [
|
||||
{ name = "game", ty = "&crate::game::Game<'_>", value = "self" },
|
||||
{ name = "minimap", ty = "&mut Option<crate::game::native_hooks::NativeMinimap>", value = "&mut *minimap" },
|
||||
]
|
||||
|
||||
[[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 = "frame"
|
||||
hook = "frame"
|
||||
hook_version = 1
|
||||
kind = "HEAD"
|
||||
helper = "frame.rs"
|
||||
bindings = [
|
||||
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
|
||||
]
|
||||
@@ -0,0 +1,114 @@
|
||||
// The map where the player placed it (MapElement.draw): the frame, Horizon's terrain, the arrow,
|
||||
// the compass letters and the coordinates' tag.
|
||||
fn incari_minimap_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_minimap as m};
|
||||
use incari_core::paint::Shape;
|
||||
use incari_core::theme::Tone;
|
||||
let _ = corners;
|
||||
// Horizon has drawn (or not) its own corner map: asking again is to compose.
|
||||
m::set_drawing(false);
|
||||
let player = game.native_player().map(|p| ([p.x, p.y, p.z], p.yaw));
|
||||
c::element(game, batch, m::ID, m::ELEMENT, |canvas| {
|
||||
if player.is_none() && !canvas.editing {
|
||||
return None;
|
||||
}
|
||||
let s = canvas.module;
|
||||
let round = s.choice("shape") == "round";
|
||||
let turn = s.bool("turn");
|
||||
let coordinates = s.bool("coordinates");
|
||||
let th = canvas.p.theme;
|
||||
let (size, frame) = (m::SIZE, m::FRAME);
|
||||
let center = size / 2.0;
|
||||
let inner = size - 2.0 * frame;
|
||||
let radius = inner / 2.0;
|
||||
|
||||
// The frame, and the backdrop where nothing's loaded.
|
||||
if round {
|
||||
canvas.round(0.0, 0.0, size, size, center, th.background);
|
||||
} else {
|
||||
canvas.fill(0.0, 0.0, size, size, Tone::Background);
|
||||
}
|
||||
let heading = player.map_or(0.0, |p| m::heading(p.1));
|
||||
let rotation = if turn { heading } else { 0.0 };
|
||||
if player.is_some() {
|
||||
let (_, src) = m::view(m::blocks_across(m::zoom_level(s.int("zoom"))), round);
|
||||
canvas.p.sprite(horizon_client::hud::MINIMAP_SPRITE, src, [frame, frame, inner, inner], 0xFFFF_FFFF);
|
||||
}
|
||||
if round {
|
||||
canvas.p.ring(center, center, center - 0.5, 1.0, th.tone(Tone::Edge));
|
||||
}
|
||||
|
||||
// Waypoints on the map (MapElement): those beyond its rim sit on its edge.
|
||||
if s.bool("waypoints") {
|
||||
let bpu = m::blocks_across(m::zoom_level(s.int("zoom"))) as f32 / inner;
|
||||
let (sin, cos) = rotation.to_radians().sin_cos();
|
||||
incari_core::waypoint::with(|store| {
|
||||
incari_core::waypoint::with_player(|pl| {
|
||||
let Some(pl) = pl else { return };
|
||||
for w in store.all().iter().filter(|w| pl.sees(w)) {
|
||||
let dx = (w.x as f64 + 0.5 - pl.x) as f32 / bpu;
|
||||
let dz = (w.z as f64 + 0.5 - pl.z) as f32 / bpu;
|
||||
let (mut x, mut y) = (dx * cos + dz * sin, -dx * sin + dz * cos);
|
||||
let r = radius - 4.0;
|
||||
if round {
|
||||
let d = (x * x + y * y).sqrt();
|
||||
if d > r {
|
||||
x *= r / d;
|
||||
y *= r / d;
|
||||
}
|
||||
} else {
|
||||
x = x.clamp(-r, r);
|
||||
y = y.clamp(-r, r);
|
||||
}
|
||||
canvas.shape(w.icon, center + x, center + y, 7.0, w.argb());
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
// The player: an arrow pointing up while the map turns; north up, the arrow can't turn
|
||||
// (the canvas draws no rotated shapes), so a dot marks the way they face.
|
||||
let ink = th.tone(Tone::Text);
|
||||
if turn {
|
||||
canvas.shape(Shape::Arrow, center, center, m::ARROW, ink);
|
||||
} else {
|
||||
let [fx, fy] = m::facing(heading);
|
||||
canvas.shape(Shape::Circle, center, center, 6.0, ink);
|
||||
canvas.shape(Shape::Circle, center + fx * 5.0, center + fy * 5.0, 3.0, ink);
|
||||
}
|
||||
|
||||
// Every badge, then every letter: one layer each.
|
||||
if s.bool("compass") {
|
||||
for i in 0..4 {
|
||||
let [x, y] = m::compass_point(i, rotation, radius - 1.0, round);
|
||||
canvas.round(center + x - m::BADGE, center + y - m::BADGE, 2.0 * m::BADGE, 2.0 * m::BADGE, m::BADGE, th.background);
|
||||
}
|
||||
for (i, letter) in m::LETTERS.iter().enumerate() {
|
||||
let [x, y] = m::compass_point(i, rotation, radius - 1.0, round);
|
||||
let argb = th.tone(if i == 0 { Tone::Accent } else { Tone::Text });
|
||||
let w = canvas.text_width(letter);
|
||||
canvas.p.push(center + x, center + y, 0.75, |p| {
|
||||
p.text(letter, -(w / 2.0).round(), -(m::LINE / 2.0).floor() + 1.0, argb, true);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// "x, y, z" on a tag below; in the HUD editor without a world, a sample.
|
||||
if coordinates {
|
||||
let at = player.map_or([0, 64, 0], |p| p.0.map(|v| v.floor() as i32));
|
||||
m::with_text(at, |text| {
|
||||
let w = canvas.text_width(text) + 10.0;
|
||||
let h = m::LINE + 3.0;
|
||||
let left = ((size - w) / 2.0).floor();
|
||||
let top = size + m::COORDINATES_GAP;
|
||||
canvas.fill(left, top, w, h, Tone::Background);
|
||||
canvas.text(text, left + 5.0, top + 2.0, Tone::Text);
|
||||
});
|
||||
}
|
||||
Some([size, m::height(coordinates)])
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// The zoom keys, while playing.
|
||||
fn incari_minimap_key(game: &mut crate::game::Game<'_>, key: &str, pressed: bool) {
|
||||
use crate::game::{incari_client as c, incari_minimap as m};
|
||||
use incari_core::input::Key;
|
||||
if !pressed || !game.native_playing() {
|
||||
return;
|
||||
}
|
||||
let key = Key::from_name(key);
|
||||
if key == Key::None {
|
||||
return;
|
||||
}
|
||||
let Some((zoom_in, zoom_out, zoom, world_map)) =
|
||||
c::settings(m::ID, |s| (s.key("zoom-in"), s.key("zoom-out"), s.int("zoom"), s.key("world-map")))
|
||||
else {
|
||||
return;
|
||||
};
|
||||
// The zoom is the module's setting, kept in the profile as the Java module keeps it.
|
||||
let set = |steps: i32| {
|
||||
c::set_setting(m::ID, "zoom", incari_core::framework::setting::Val::Int(m::zoomed(zoom, steps)))
|
||||
};
|
||||
if key == zoom_in {
|
||||
set(1);
|
||||
} else if key == zoom_out {
|
||||
set(-1);
|
||||
} else if key == world_map {
|
||||
c::open(game, Box::new(incari_core::screens::world_map::WorldMap::new()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// The map Horizon composes, as the settings ask; none while it would draw it in a corner, since the
|
||||
// HUD element draws it where the player placed it.
|
||||
fn incari_minimap_map(
|
||||
game: &crate::game::Game<'_>,
|
||||
minimap: &mut Option<crate::game::native_hooks::NativeMinimap>,
|
||||
) {
|
||||
use crate::game::{incari_client as c, incari_minimap as m};
|
||||
let _ = game;
|
||||
if minimap.is_some() || m::drawing() {
|
||||
return;
|
||||
}
|
||||
let Some((round, turn, zoom)) = c::settings(m::ID, |s| (s.choice("shape") == "round", s.bool("turn"), s.int("zoom"))) else {
|
||||
return;
|
||||
};
|
||||
let (blocks_per_pixel, _) = m::view(m::blocks_across(m::zoom_level(zoom)), round);
|
||||
*minimap = Some(crate::game::native_hooks::NativeMinimap {
|
||||
corner: horizon_client::extras::Corner::TopRight,
|
||||
size: m::SIZE - 2.0 * m::FRAME,
|
||||
blocks_per_pixel,
|
||||
rotate: turn,
|
||||
round,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
// Incari's Minimap for the Rust build (ADR 0068, 0069), as features/minimap's MapElement draws it:
|
||||
// the live map in a round or square frame where the player placed it in the HUD editor, turning
|
||||
// with them or north up, the arrow in the middle, N, E, S and W on the rim and the coordinates on a
|
||||
// tag below. Horizon composes the terrain (vanilla's map colours, height shading, a floor slice
|
||||
// under roofs and in the Nether) into its minimap sprite; the element draws that sprite itself, so
|
||||
// Horizon is asked for the map only while it composes, not while it would draw it in a corner.
|
||||
// Its settings and HUD element are the module's in `incari_core::modules`. No game types:
|
||||
// rust/lib.rs tests it.
|
||||
use std::cell::{Cell, RefCell};
|
||||
|
||||
/// The module's id, as the Java client's profiles name it.
|
||||
pub const ID: &str = "minimap";
|
||||
/// Its HUD element.
|
||||
pub const ELEMENT: &str = "map";
|
||||
|
||||
/// How many blocks the map's width shows at each zoom level (1 to 7), out to in.
|
||||
pub const ZOOMS: [i32; 7] = [256, 192, 128, 96, 64, 48, 32];
|
||||
|
||||
/// The map's size, frame and all, in GUI units at scale 1.
|
||||
pub const SIZE: f32 = 96.0;
|
||||
/// The frame's width around the terrain.
|
||||
pub const FRAME: f32 = 2.0;
|
||||
/// The player's arrow.
|
||||
pub const ARROW: f32 = 10.0;
|
||||
/// The compass letters' badges' radius.
|
||||
pub const BADGE: f32 = 5.5;
|
||||
/// The gap between the map and the coordinates below it.
|
||||
pub const COORDINATES_GAP: f32 = 3.0;
|
||||
/// A line of text's height.
|
||||
pub const LINE: f32 = 9.0;
|
||||
pub const LETTERS: [&str; 4] = ["N", "E", "S", "W"];
|
||||
|
||||
/// Horizon's map image is this many pixels across, at 0.5 to 4 blocks a pixel.
|
||||
pub const MAP_PIXELS: f32 = 128.0;
|
||||
const FINEST: f32 = 0.5;
|
||||
|
||||
/// Blocks across at zoom `level`.
|
||||
pub fn blocks_across(level: i32) -> i32 {
|
||||
ZOOMS[(level.clamp(1, ZOOMS.len() as i32) - 1) as usize]
|
||||
}
|
||||
|
||||
/// Zoom `level` after `steps` presses of the zoom in (+) or out (-) key.
|
||||
pub fn zoomed(level: i32, steps: i32) -> i32 {
|
||||
(level + steps).clamp(1, ZOOMS.len() as i32)
|
||||
}
|
||||
|
||||
/// What Horizon composes for `across` blocks (its blocks a pixel), and the part of its image that
|
||||
/// shows them (`[x, y, w, h]` in image pixels). Closer than Horizon goes, a square map shows the
|
||||
/// middle of its image; a round one can't (its image is round), so it stays at the closest.
|
||||
pub fn view(across: i32, round: bool) -> (f32, [f32; 4]) {
|
||||
let bpp = across as f32 / MAP_PIXELS;
|
||||
if bpp >= FINEST || round {
|
||||
return (bpp.clamp(FINEST, 4.0), [0.0, 0.0, MAP_PIXELS, MAP_PIXELS]);
|
||||
}
|
||||
let shown = across as f32 / FINEST;
|
||||
let at = (MAP_PIXELS - shown) / 2.0;
|
||||
(FINEST, [at, at, shown, shown])
|
||||
}
|
||||
|
||||
/// The element's height: the map, and the coordinates' tag when they show.
|
||||
pub fn height(coordinates: bool) -> f32 {
|
||||
if coordinates {
|
||||
SIZE + COORDINATES_GAP + LINE + 3.0
|
||||
} else {
|
||||
SIZE
|
||||
}
|
||||
}
|
||||
|
||||
/// The compass bearing the player faces, degrees clockwise from north, from the game's yaw (0
|
||||
/// faces south).
|
||||
pub fn heading(yaw: f32) -> f32 {
|
||||
(yaw + 180.0).rem_euclid(360.0)
|
||||
}
|
||||
|
||||
/// Compass point `i` (N, E, S, W) on a map turned `rotation` degrees: where it is from the middle,
|
||||
/// on the rim `at` out (a round rim, or a square one's edge).
|
||||
pub fn compass_point(i: usize, rotation: f32, at: f32, round: bool) -> [f32; 2] {
|
||||
let (sin, cos) = rotation.to_radians().sin_cos();
|
||||
let wx = [0.0, 1.0, 0.0, -1.0][i % 4];
|
||||
let wz = [-1.0, 0.0, 1.0, 0.0][i % 4];
|
||||
let x = wx * cos + wz * sin;
|
||||
let y = -wx * sin + wz * cos;
|
||||
let reach: f32 = if round {
|
||||
1.0
|
||||
} else {
|
||||
f32::abs(x).max(f32::abs(y))
|
||||
};
|
||||
[x / reach * at, y / reach * at]
|
||||
}
|
||||
|
||||
/// The way the player faces on a north-up map, as a unit step (x right, y down).
|
||||
pub fn facing(heading: f32) -> [f32; 2] {
|
||||
let (sin, cos) = heading.to_radians().sin_cos();
|
||||
[sin, -cos]
|
||||
}
|
||||
|
||||
// The window's thread only: keys, the HUD and Horizon's map all run there.
|
||||
thread_local! {
|
||||
/// The zoom the keys set, with the setting's value it was set from: a session's, since the
|
||||
/// client's settings can't be written from a hook. The setting changed in the menu wins.
|
||||
static ZOOM: Cell<Option<(i32, i32)>> = const { Cell::new(None) };
|
||||
/// Between the frame hook and the HUD hook: Horizon is about to draw its map in a corner.
|
||||
static DRAWING: Cell<bool> = const { Cell::new(false) };
|
||||
static TEXT: RefCell<Cached> = const { RefCell::new((None, String::new())) };
|
||||
}
|
||||
|
||||
/// The zoom level shown, for the `zoom` setting's `setting`.
|
||||
pub fn zoom_level(setting: i32) -> i32 {
|
||||
match ZOOM.get() {
|
||||
Some((from, level)) if from == setting => level,
|
||||
_ => setting,
|
||||
}
|
||||
}
|
||||
|
||||
/// A zoom key: `steps` in (+) or out (-) from what shows, for this session only (the keys now
|
||||
/// change the module's setting itself, as Java's do).
|
||||
#[allow(dead_code)]
|
||||
pub fn zoom_by(setting: i32, steps: i32) {
|
||||
ZOOM.set(Some((setting, zoomed(zoom_level(setting), steps))));
|
||||
}
|
||||
|
||||
/// Whether Horizon is asking for the map to draw it (no) rather than to compose it (yes).
|
||||
pub fn drawing() -> bool {
|
||||
DRAWING.get()
|
||||
}
|
||||
|
||||
/// The frame hook (after Horizon composed) says true; the HUD hook (after it drew) false.
|
||||
pub fn set_drawing(drawing: bool) {
|
||||
DRAWING.set(drawing);
|
||||
}
|
||||
|
||||
type Cached = (Option<[i32; 3]>, String);
|
||||
|
||||
/// Calls `f` with "12, 64, -30" for the block `at`, made again only when the block changes.
|
||||
/// Hot path, don't allocate.
|
||||
pub fn with_text<R>(at: [i32; 3], f: impl FnOnce(&str) -> R) -> R {
|
||||
TEXT.with(|cell| {
|
||||
let mut cached = cell.borrow_mut();
|
||||
if cached.0 != Some(at) {
|
||||
*cached = (Some(at), format!("{}, {}, {}", at[0], at[1], at[2]));
|
||||
}
|
||||
f(&cached.1)
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn near(a: [f32; 2], b: [f32; 2]) -> bool {
|
||||
(a[0] - b[0]).abs() < 1e-4 && (a[1] - b[1]).abs() < 1e-4
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zooms_as_the_java_module_does() {
|
||||
assert_eq!(blocks_across(4), 96);
|
||||
assert_eq!(view(96, true), (0.75, [0.0, 0.0, 128.0, 128.0]));
|
||||
assert_eq!(view(32, false), (0.5, [32.0, 32.0, 64.0, 64.0]));
|
||||
assert_eq!(view(32, true).0, 0.5);
|
||||
assert!(
|
||||
ZOOMS
|
||||
.iter()
|
||||
.all(|&z| (0.5..=4.0).contains(&view(z, false).0))
|
||||
);
|
||||
assert_eq!(zoomed(7, 1), 7);
|
||||
assert_eq!(zoomed(1, -1), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_keys_zoom_until_the_setting_changes() {
|
||||
assert_eq!(zoom_level(4), 4);
|
||||
zoom_by(4, 1);
|
||||
zoom_by(4, 1);
|
||||
assert_eq!(zoom_level(4), 6);
|
||||
assert_eq!(zoom_level(2), 2, "set in the menu");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn puts_north_where_it_lies() {
|
||||
assert!(near(compass_point(0, 0.0, 10.0, true), [0.0, -10.0]));
|
||||
assert!(near(compass_point(1, 0.0, 10.0, true), [10.0, 0.0]));
|
||||
// Facing south (heading 180), turning with the player: north is at the bottom.
|
||||
assert_eq!(heading(0.0), 180.0);
|
||||
assert!(near(compass_point(0, 180.0, 10.0, true), [0.0, 10.0]));
|
||||
// A square rim at 45 degrees: on the corner.
|
||||
assert!(near(compass_point(0, 45.0, 10.0, false), [-10.0, -10.0]));
|
||||
assert!(near(facing(90.0), [1.0, 0.0]));
|
||||
assert_eq!(height(true), 111.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn writes_the_block() {
|
||||
assert_eq!(with_text([12, 64, -30], str::to_owned), "12, 64, -30");
|
||||
set_drawing(true);
|
||||
assert!(drawing());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user