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,25 @@
# Incari's Shield & Totem HUD (features/shieldtotem) 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_shieldtotem"
version = "0.1.0"
priority = 100
[[module]]
id = "runtime"
file = "crates/horizon-client/src/game.rs"
name = "incari_shieldtotem"
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,86 @@
// The shield and the totems on the glass, each where the player placed it (the Java module's
// Shield and Totems).
fn incari_shieldtotem_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_shieldtotem as m};
use incari_core::hud::LINE;
use incari_core::theme::Tone;
let _ = corners;
let game = &*game;
let tone = |t: m::Tone| match t {
m::Tone::Text => Tone::Text,
m::Tone::Positive => Tone::Positive,
m::Tone::Warning => Tone::Warning,
m::Tone::Negative => Tone::Negative,
};
let in_world = game.native_player().is_some();
let text_y = ((m::ICON - LINE) / 2.0).floor() + 1.0;
c::element(game, batch, m::ID, m::SHIELD_ELEMENT, |canvas| {
let s = canvas.module;
if !s.bool("show-shield") {
return None;
}
// The off hand first, where it's usually carried.
let held = ["offhand", "mainhand"]
.into_iter()
.filter(|_| in_world)
.filter_map(|slot| game.native_equipped(slot))
.find(|st| &*st.item == m::SHIELD)
.map(|st| m::Shield {
damage: st.damage,
max: game.native_max_damage(&st.item),
cooldown: game.native_item_cooldown(st),
});
// The HUD editor's: a new shield.
let sample = || m::Shield {
damage: 0,
max: game.native_max_damage(m::SHIELD),
cooldown: 0.0,
};
let shield = held.or_else(|| canvas.editing.then(sample))?;
canvas.item(m::SHIELD, 0.0, 0.0);
if let Some((width, rgb)) = m::bar(shield) {
canvas.fill_argb(2.0, 13.0, 13.0, 2.0, 0xFF00_0000);
canvas.fill_argb(2.0, 13.0, width, 1.0, 0xFF00_0000 | rgb);
}
let text_tone = tone(m::shield_tone(shield, s.bool("wear-colors")));
m::with_shield_text(shield, |text| {
canvas.text(text, m::ICON + m::TEXT_GAP, text_y, text_tone);
});
if shield.cooldown > 0.0 {
// What's left of the cooldown, as a bar under the word.
let bar = (canvas.text_width(m::DISABLED) * shield.cooldown).round();
canvas.fill(m::ICON + m::TEXT_GAP, m::ICON - 1.0, bar, 1.0, Tone::Negative);
}
Some([m::WIDTH, m::ICON])
});
c::element(game, batch, m::ID, m::TOTEMS_ELEMENT, |canvas| {
let s = canvas.module;
if !s.bool("show-totems") {
return None;
}
let count = if in_world {
let count = game.native_item_count(m::TOTEM);
// None carried and not warned of: hidden, but placeable in the HUD editor.
if count == 0 && !s.bool("show-no-totems") && !canvas.editing {
return None;
}
count
} else if canvas.editing {
m::SAMPLE_TOTEMS
} else {
return None;
};
canvas.item(m::TOTEM, 0.0, 0.0);
let text_tone = if count == 0 { Tone::Negative } else { Tone::Text };
m::with_count_text(count, |text| {
canvas.text(text, m::ICON + m::TEXT_GAP, text_y, text_tone);
});
Some([m::WIDTH, m::ICON])
});
}
@@ -0,0 +1,179 @@
// Incari's Shield & Totem HUD for the Rust build (ADR 0068, 0069), as features/shieldtotem does
// it: the shield you hold with its durability ("Disabled" while an axe has it on cooldown), and how
// many totems of undying you carry, each a HUD element of its own. Its settings and HUD elements 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 = "shieldtotem";
/// Its HUD elements' ids.
pub const SHIELD_ELEMENT: &str = "shield";
pub const TOTEMS_ELEMENT: &str = "totems";
pub const SHIELD: &str = "minecraft:shield";
pub const TOTEM: &str = "minecraft:totem_of_undying";
pub const ICON: f32 = 16.0;
pub const TEXT_GAP: f32 = 2.0;
pub const DISABLED: &str = "Disabled";
/// Each element's width, whatever it says (the Java module's WIDTH): the icons hold still as the
/// text changes, and the two share one column however the screen clamps them.
pub const WIDTH: f32 = 60.0;
/// The HUD editor's totem count with no world.
pub const SAMPLE_TOTEMS: u32 = 2;
/// The text's tone (`incari_core::theme::Tone`'s of the same names).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Tone {
Text,
Positive,
Warning,
Negative,
}
/// A held shield: its wear, the most it takes and what's left of its cooldown (0 to 1).
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Shield {
pub damage: u32,
pub max: u32,
pub cooldown: f32,
}
/// Whole percent left, rounded down, but 1 while anything's left; 100 for one that doesn't wear.
pub fn percent(shield: Shield) -> u32 {
if shield.max == 0 {
return 100;
}
let remaining = shield.max.saturating_sub(shield.damage);
if remaining == 0 {
return 0;
}
(u64::from(remaining) * 100 / u64::from(shield.max)).max(1) as u32
}
/// The shield's tone: red while disabled, else by wear (green, amber below half, red below a
/// fifth) when coloured.
pub fn shield_tone(shield: Shield, colors: bool) -> Tone {
if shield.cooldown > 0.0 {
return Tone::Negative;
}
if !colors || shield.max == 0 {
return Tone::Text;
}
let (remaining, max) = (
u64::from(shield.max.saturating_sub(shield.damage)),
u64::from(shield.max),
);
if remaining * 5 < max {
Tone::Negative
} else if remaining * 2 < max {
Tone::Warning
} else {
Tone::Positive
}
}
/// The game's durability bar on the shield's icon: its width of 13 and its RGB, None while unworn.
pub fn bar(shield: Shield) -> Option<(f32, u32)> {
if shield.max == 0 || shield.damage == 0 {
return None;
}
let (damage, max) = (shield.damage as f32, shield.max as f32);
let width = (13.0 - damage * 13.0 / max + 0.5).floor().clamp(0.0, 13.0);
let hue = ((max - damage) / max).max(0.0) / 3.0;
// `Mth.hsvToRgb(hue, 1, 1)` for a hue from red (0) to green (1/3).
let f = hue * 6.0;
let byte = |v: f32| ((v * 255.0) as u32).min(255);
let rgb = if f < 1.0 {
0xFF_0000 | byte(f) << 8
} else if f < 2.0 {
byte(2.0 - f) << 16 | 0x00_FF00
} else {
0x00_FF00
};
Some((width, rgb))
}
// Hot path, don't allocate: each text made again only when its number changes.
thread_local! {
static PERCENT: std::cell::RefCell<(Option<u32>, String)> = const { std::cell::RefCell::new((None, String::new())) };
static COUNT: std::cell::RefCell<(Option<u32>, String)> = const { std::cell::RefCell::new((None, String::new())) };
}
/// Calls `f` with the shield's text: "Disabled" on cooldown, else "86%".
pub fn with_shield_text<R>(shield: Shield, f: impl FnOnce(&str) -> R) -> R {
if shield.cooldown > 0.0 {
return f(DISABLED);
}
let percent = percent(shield);
PERCENT.with(|cell| {
let mut cached = cell.borrow_mut();
if cached.0 != Some(percent) {
*cached = (Some(percent), format!("{percent}%"));
}
f(&cached.1)
})
}
/// Calls `f` with "x3".
pub fn with_count_text<R>(count: u32, f: impl FnOnce(&str) -> R) -> R {
COUNT.with(|cell| {
let mut cached = cell.borrow_mut();
if cached.0 != Some(count) {
*cached = (Some(count), format!("x{count}"));
}
f(&cached.1)
})
}
#[cfg(test)]
mod tests {
use super::*;
fn shield(damage: u32, cooldown: f32) -> Shield {
Shield {
damage,
max: 336,
cooldown,
}
}
#[test]
fn says_what_the_java_module_says() {
assert_eq!(with_shield_text(shield(84, 0.0), str::to_owned), "75%");
assert_eq!(with_shield_text(shield(0, 0.5), str::to_owned), "Disabled");
assert_eq!(with_shield_text(shield(335, 0.0), str::to_owned), "1%");
assert_eq!(
percent(Shield {
damage: 0,
max: 0,
cooldown: 0.0
}),
100
);
assert_eq!(with_count_text(0, str::to_owned), "x0");
}
#[test]
fn colours_by_wear_and_cooldown() {
assert_eq!(shield_tone(shield(0, 0.0), true), Tone::Positive);
assert_eq!(shield_tone(shield(200, 0.0), true), Tone::Warning);
assert_eq!(shield_tone(shield(300, 0.0), true), Tone::Negative);
assert_eq!(shield_tone(shield(300, 0.0), false), Tone::Text);
assert_eq!(shield_tone(shield(0, 0.1), false), Tone::Negative);
}
#[test]
fn draws_the_games_bar() {
assert_eq!(bar(shield(0, 0.0)), None);
assert_eq!(bar(shield(168, 0.0)), Some((7.0, 0xFF_FF00)));
assert_eq!(
bar(Shield {
damage: 1,
max: 1000,
cooldown: 0.0
}),
Some((13.0, 0x00_FF00))
);
}
}