Archive incari-client (client, 26.3, clean)
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
# Incari's Toggle Sprint (features/togglesprint) 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_togglesprint"
|
||||
version = "0.1.0"
|
||||
priority = 100
|
||||
|
||||
[[module]]
|
||||
id = "runtime"
|
||||
file = "crates/horizon-client/src/game.rs"
|
||||
name = "incari_togglesprint"
|
||||
source = "runtime.rs"
|
||||
|
||||
[[injection]]
|
||||
id = "keys"
|
||||
# Horizon's movement_keys hook point is still a pending fragment
|
||||
# (tools/native-hook-fragments/incari_client_hooks.json), so this targets its function. Make it
|
||||
# `hook = "movement_keys"` and `hook_version = 1` once Horizon stamps it.
|
||||
target = "horizon_client::game::native_hooks::Game<'_>::native_movement_keys"
|
||||
kind = "HEAD"
|
||||
helper = "keys.rs"
|
||||
bindings = [
|
||||
{ name = "game", ty = "&crate::game::Game<'_>", value = "self" },
|
||||
{ name = "sprint", ty = "&mut bool", value = "&mut *sprint" },
|
||||
{ name = "sneak", ty = "&mut bool", value = "&mut *sneak" },
|
||||
]
|
||||
|
||||
[[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 = "tick"
|
||||
hook = "tick"
|
||||
hook_version = 1
|
||||
kind = "HEAD"
|
||||
helper = "tick.rs"
|
||||
bindings = [
|
||||
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
|
||||
]
|
||||
@@ -0,0 +1,27 @@
|
||||
// The status line, where the player placed it.
|
||||
fn incari_togglesprint_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_togglesprint as m};
|
||||
use incari_core::theme::Tone;
|
||||
let _ = corners;
|
||||
let keys = m::keys();
|
||||
c::element(game, batch, m::ID, "status", |canvas| {
|
||||
let s = canvas.module;
|
||||
if !s.bool("show-status") {
|
||||
return None;
|
||||
}
|
||||
let shown = keys.status(s.choice("sprint"), s.choice("sneak"));
|
||||
// In the HUD editor with nothing latched, the longest common line.
|
||||
let text = shown.or(canvas.editing.then_some(m::SPRINTING_TOGGLED))?;
|
||||
let argb = if s.bool("custom-color") {
|
||||
s.color("color") | 0xFF00_0000
|
||||
} else {
|
||||
canvas.p.theme.tone(Tone::Text)
|
||||
};
|
||||
canvas.text_argb(text, 0.0, 0.0, argb);
|
||||
Some([canvas.text_width(text), incari_core::hud::LINE])
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Sprint and sneak as held, from the module's latches.
|
||||
fn incari_togglesprint_keys(game: &crate::game::Game<'_>, sprint: &mut bool, sneak: &mut bool) {
|
||||
use crate::game::{incari_client as c, incari_togglesprint as m};
|
||||
let Some((sprint_mode, sneak_mode)) = c::settings(m::ID, |s| (s.choice("sprint"), s.choice("sneak"))) else {
|
||||
m::update(m::Keys::unlatch);
|
||||
return;
|
||||
};
|
||||
if game.native_ui_screen() == "dead" {
|
||||
m::update(m::Keys::unlatch);
|
||||
}
|
||||
(*sprint, *sneak) = m::update(|k| k.tick(*sprint, *sneak, sprint_mode, sneak_mode));
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
// Incari's Toggle Sprint for the Rust build (ADR 0068, 0069), as features/togglesprint does it:
|
||||
// latch sprint (or always sprint) and optionally sneak, with a HUD line saying what's active.
|
||||
// Whether the player can sprint is still the game's call: the mod only says the key is held. Its
|
||||
// settings and HUD element are the module's in `incari_core::modules` (the Java ids and choice
|
||||
// keys). No game types: rust/lib.rs tests it.
|
||||
|
||||
/// The module's id, as the Java client's profiles name it.
|
||||
pub const ID: &str = "togglesprint";
|
||||
|
||||
pub const SPRINTING_TOGGLED: &str = "[Sprinting (Toggled)]";
|
||||
pub const SPRINTING_ALWAYS: &str = "[Sprinting (Always)]";
|
||||
pub const SPRINTING_HELD: &str = "[Sprinting (Key Held)]";
|
||||
pub const SNEAKING_TOGGLED: &str = "[Sneaking (Toggled)]";
|
||||
pub const SNEAKING_HELD: &str = "[Sneaking (Key Held)]";
|
||||
|
||||
/// The keys as the player holds them, the latches the mod keeps, and the modes (the `sprint` and
|
||||
/// `sneak` settings' keys) they were latched in.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Keys {
|
||||
pub sprint_held: bool,
|
||||
pub sneak_held: bool,
|
||||
pub sprint_latched: bool,
|
||||
pub sneak_latched: bool,
|
||||
pub modes: Option<(&'static str, &'static str)>,
|
||||
}
|
||||
|
||||
impl Keys {
|
||||
pub const NONE: Keys = Keys {
|
||||
sprint_held: false,
|
||||
sneak_held: false,
|
||||
sprint_latched: false,
|
||||
sneak_latched: false,
|
||||
modes: None,
|
||||
};
|
||||
|
||||
/// A tick's keys as the game reads them (`sprint`, `sneak` held), in modes `sprint_mode`
|
||||
/// (`hold`, `toggle`, `always`) and `sneak_mode` (`hold`, `toggle`): a press flips a latch
|
||||
/// when toggling, and a change of mode lets go of both. Returns whether the game should take
|
||||
/// sprint and sneak as held.
|
||||
pub fn tick(
|
||||
&mut self,
|
||||
sprint: bool,
|
||||
sneak: bool,
|
||||
sprint_mode: &'static str,
|
||||
sneak_mode: &'static str,
|
||||
) -> (bool, bool) {
|
||||
if self.modes != Some((sprint_mode, sneak_mode)) {
|
||||
self.unlatch();
|
||||
self.modes = Some((sprint_mode, sneak_mode));
|
||||
}
|
||||
if sprint_mode != "toggle" {
|
||||
self.sprint_latched = false;
|
||||
} else if sprint && !self.sprint_held {
|
||||
self.sprint_latched = !self.sprint_latched;
|
||||
}
|
||||
if sneak_mode != "toggle" {
|
||||
self.sneak_latched = false;
|
||||
} else if sneak && !self.sneak_held {
|
||||
self.sneak_latched = !self.sneak_latched;
|
||||
}
|
||||
self.sprint_held = sprint;
|
||||
self.sneak_held = sneak;
|
||||
(self.sprinting(sprint_mode), self.sneaking(sneak_mode))
|
||||
}
|
||||
|
||||
/// Lets go of the latches: death, leaving the world, or the module turned off.
|
||||
pub fn unlatch(&mut self) {
|
||||
self.sprint_latched = false;
|
||||
self.sneak_latched = false;
|
||||
}
|
||||
|
||||
/// Whether the game should take sprint as held.
|
||||
pub fn sprinting(&self, mode: &str) -> bool {
|
||||
match mode {
|
||||
"always" => true,
|
||||
"toggle" => self.sprint_latched || self.sprint_held,
|
||||
_ => self.sprint_held,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sneaking(&self, mode: &str) -> bool {
|
||||
self.sneak_held || (mode == "toggle" && self.sneak_latched)
|
||||
}
|
||||
|
||||
/// The HUD line, or none. Sneaking first: it stops sprinting, so it's what the player is doing.
|
||||
pub fn status(&self, sprint_mode: &str, sneak_mode: &str) -> Option<&'static str> {
|
||||
if sneak_mode == "toggle" && self.sneak_latched {
|
||||
Some(SNEAKING_TOGGLED)
|
||||
} else if self.sneak_held {
|
||||
Some(SNEAKING_HELD)
|
||||
} else if sprint_mode == "always" {
|
||||
Some(SPRINTING_ALWAYS)
|
||||
} else if sprint_mode == "toggle" && self.sprint_latched {
|
||||
Some(SPRINTING_TOGGLED)
|
||||
} else if self.sprint_held {
|
||||
Some(SPRINTING_HELD)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The window's thread only: keys, ticks and the HUD all run there.
|
||||
thread_local! {
|
||||
static HELD: std::cell::Cell<Keys> = const { std::cell::Cell::new(Keys::NONE) };
|
||||
}
|
||||
|
||||
pub fn keys() -> Keys {
|
||||
HELD.with(std::cell::Cell::get)
|
||||
}
|
||||
|
||||
pub fn update<R>(f: impl FnOnce(&mut Keys) -> R) -> R {
|
||||
HELD.with(|k| {
|
||||
let mut keys = k.get();
|
||||
let r = f(&mut keys);
|
||||
k.set(keys);
|
||||
r
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn toggles_sprint_and_holds_sneak_by_default() {
|
||||
let (sprint, sneak) = ("toggle", "hold");
|
||||
let mut k = Keys::NONE;
|
||||
k.tick(true, false, sprint, sneak);
|
||||
assert_eq!(k.tick(false, false, sprint, sneak), (true, false));
|
||||
assert_eq!(k.status(sprint, sneak), Some(SPRINTING_TOGGLED));
|
||||
k.tick(false, true, sprint, sneak);
|
||||
assert_eq!(k.status(sprint, sneak), Some(SNEAKING_HELD));
|
||||
assert_eq!(k.tick(false, false, sprint, sneak), (true, false));
|
||||
k.tick(true, false, sprint, sneak);
|
||||
assert_eq!(k.tick(false, false, sprint, sneak), (false, false));
|
||||
assert_eq!(k.status(sprint, sneak), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn always_sprints_and_latches_sneak() {
|
||||
let mut k = Keys::NONE;
|
||||
assert_eq!(k.tick(false, false, "always", "toggle"), (true, false));
|
||||
assert_eq!(k.status("always", "hold"), Some(SPRINTING_ALWAYS));
|
||||
k.tick(false, true, "always", "toggle");
|
||||
assert_eq!(k.tick(false, false, "always", "toggle"), (true, true));
|
||||
assert_eq!(k.status("always", "toggle"), Some(SNEAKING_TOGGLED));
|
||||
k.unlatch();
|
||||
assert!(!k.sneaking("toggle"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_held_key_and_a_new_mode_dont_keep_the_latch() {
|
||||
let mut k = Keys::NONE;
|
||||
k.tick(true, false, "toggle", "hold");
|
||||
k.tick(true, false, "toggle", "hold");
|
||||
assert!(k.sprint_latched, "held over ticks: one press");
|
||||
k.tick(false, false, "hold", "hold");
|
||||
assert!(!k.sprint_latched, "changing the mode lets go");
|
||||
update(|keys| keys.tick(true, false, "toggle", "hold"));
|
||||
assert!(keys().sprint_latched);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// Death and leaving the world let go of the latches, as the game lets go of its own on respawn.
|
||||
fn incari_togglesprint_tick(game: &mut crate::game::Game<'_>) {
|
||||
use crate::game::incari_togglesprint as m;
|
||||
if game.native_window() && (game.native_player().is_none() || game.native_ui_screen() == "dead") {
|
||||
m::update(m::Keys::unlatch);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user