Archive incari-client (client, 26.3, clean)
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
// The camera draws from the Freelook rotation while it's on.
|
||||
fn incari_freelook_camera(yaw: &mut f32, pitch: &mut f32) {
|
||||
crate::game::incari_freelook::with(|f| f.camera(yaw, pitch));
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
# Incari's Freelook (features/freelook) 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 are the client's (rust/mods/incari): the module menu shows them.
|
||||
[mod]
|
||||
id = "incari_freelook"
|
||||
version = "0.1.0"
|
||||
priority = 100
|
||||
|
||||
[[module]]
|
||||
id = "runtime"
|
||||
file = "crates/horizon-client/src/game.rs"
|
||||
name = "incari_freelook"
|
||||
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 = "tick"
|
||||
hook = "tick"
|
||||
hook_version = 1
|
||||
kind = "HEAD"
|
||||
helper = "tick.rs"
|
||||
bindings = [
|
||||
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
|
||||
]
|
||||
|
||||
[[injection]]
|
||||
id = "turn"
|
||||
# Horizon's turn hook point is still a pending fragment
|
||||
# (tools/native-hook-fragments/incari_client_hooks.json), so this targets its function. Make it
|
||||
# `hook = "turn"` and `hook_version = 1` once Horizon stamps it.
|
||||
target = "horizon_client::game::native_hooks::Game<'_>::native_turn"
|
||||
kind = "HEAD"
|
||||
helper = "turn.rs"
|
||||
bindings = [
|
||||
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
|
||||
{ name = "dx", ty = "f64", value = "dx" },
|
||||
{ name = "dy", ty = "f64", value = "dy" },
|
||||
{ name = "claimed", ty = "&mut bool", value = "&mut *claimed" },
|
||||
]
|
||||
|
||||
[[injection]]
|
||||
id = "camera"
|
||||
# Horizon's camera_rotation hook point is still a pending fragment
|
||||
# (tools/native-hook-fragments/incari_client_hooks.json), so this targets its function. Make it
|
||||
# `hook = "camera_rotation"` and `hook_version = 1` once Horizon stamps it.
|
||||
target = "horizon_client::game::native_hooks::Game<'_>::native_camera_rotation"
|
||||
kind = "HEAD"
|
||||
helper = "camera.rs"
|
||||
bindings = [
|
||||
{ name = "yaw", ty = "&mut f32", value = "&mut *yaw" },
|
||||
{ name = "pitch", ty = "&mut f32", value = "&mut *pitch" },
|
||||
]
|
||||
@@ -0,0 +1,10 @@
|
||||
// The Freelook key (the module's `look` setting), held or pressed.
|
||||
fn incari_freelook_key(game: &mut crate::game::Game<'_>, key: &str, pressed: bool) {
|
||||
use crate::game::{incari_client as c, incari_freelook as m};
|
||||
let bound = c::settings(m::ID, |s| s.key("look")).filter(|k| k.is_bound());
|
||||
if bound != Some(incari_core::input::Key::from_name(key)) {
|
||||
return;
|
||||
}
|
||||
let playing = game.native_playing();
|
||||
m::with(|f| f.key(pressed, playing));
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
// Incari's Freelook for the Rust build (ADR 0068, 0069), as features/freelook does it: hold (or
|
||||
// toggle) the key to look around without turning your player; letting go puts the view back.
|
||||
// Horizon's camera hooks turn the view only: the server, movement and aiming keep the player's.
|
||||
// Its settings are the module's in `incari_core::modules` (the Java ids), in the module menu.
|
||||
// No game types: rust/lib.rs tests it.
|
||||
|
||||
/// The module's id, as the Java client's profiles name it.
|
||||
pub const ID: &str = "freelook";
|
||||
|
||||
/// Horizon's view (`native_set_camera_type`: 0 first person, 1 behind, 2 in front) for the `view`
|
||||
/// setting's key.
|
||||
pub fn camera_type(view: &str) -> u8 {
|
||||
match view {
|
||||
"front" => 2,
|
||||
"first" => 0,
|
||||
_ => 1,
|
||||
}
|
||||
}
|
||||
|
||||
/// Degrees a mouse unit turns (`Entity.turn`).
|
||||
pub const DEGREES: f64 = 0.15;
|
||||
|
||||
/// What a tick asks of the game.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Change {
|
||||
None,
|
||||
/// Freelook started: set the view to this (Horizon's camera type), if it isn't already.
|
||||
Start(u8),
|
||||
/// Freelook stopped: put the view back to this.
|
||||
Stop(u8),
|
||||
}
|
||||
|
||||
/// Freelook's state, as the Java module keeps it.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Freelook {
|
||||
/// Flipped by each press (the key's `onPress`).
|
||||
pub toggled: bool,
|
||||
/// The key is held (it went down with no screen open and hasn't come up).
|
||||
pub held: bool,
|
||||
pub active: bool,
|
||||
/// The camera's angles are still to be taken from the player's on the next frame.
|
||||
pub fresh: bool,
|
||||
pub yaw: f32,
|
||||
pub pitch: f32,
|
||||
/// The view to go back to.
|
||||
pub before: u8,
|
||||
}
|
||||
|
||||
impl Freelook {
|
||||
pub const OFF: Self = Self {
|
||||
toggled: false,
|
||||
held: false,
|
||||
active: false,
|
||||
fresh: false,
|
||||
yaw: 0.0,
|
||||
pitch: 0.0,
|
||||
before: 0,
|
||||
};
|
||||
|
||||
/// The module's key went down (`pressed`) or up; `playing` with no screen open. Binds fire
|
||||
/// only with no screen open, as the game's do.
|
||||
pub fn key(&mut self, pressed: bool, playing: bool) {
|
||||
if !pressed {
|
||||
self.held = false;
|
||||
} else if playing {
|
||||
self.held = true;
|
||||
self.toggled = !self.toggled;
|
||||
}
|
||||
}
|
||||
|
||||
/// Once a tick. `mode` is the `mode` setting's key, None while the module is off; `player`
|
||||
/// whether there's a player; `view` the camera Freelook wants and `now` the camera now.
|
||||
pub fn tick(
|
||||
&mut self,
|
||||
mode: Option<&str>,
|
||||
playing: bool,
|
||||
player: bool,
|
||||
view: u8,
|
||||
now: u8,
|
||||
) -> Change {
|
||||
let want = match mode {
|
||||
None => false,
|
||||
Some(mode) => {
|
||||
player
|
||||
&& if mode == "hold" {
|
||||
self.held && playing
|
||||
} else {
|
||||
self.toggled
|
||||
}
|
||||
}
|
||||
};
|
||||
let change = if want == self.active {
|
||||
Change::None
|
||||
} else if want {
|
||||
self.active = true;
|
||||
self.fresh = true;
|
||||
self.before = now;
|
||||
if view == now {
|
||||
Change::None
|
||||
} else {
|
||||
Change::Start(view)
|
||||
}
|
||||
} else {
|
||||
self.active = false;
|
||||
self.toggled = false;
|
||||
if now == self.before {
|
||||
Change::None
|
||||
} else {
|
||||
Change::Stop(self.before)
|
||||
}
|
||||
};
|
||||
if mode.is_none() {
|
||||
// Off: as the Java module's onDisable.
|
||||
self.toggled = false;
|
||||
}
|
||||
change
|
||||
}
|
||||
|
||||
/// The mouse turned `dx`, `dy` (positive right and down): true when Freelook took it.
|
||||
pub fn turn(&mut self, dx: f64, dy: f64, invert: bool) -> bool {
|
||||
if !self.active {
|
||||
return false;
|
||||
}
|
||||
let dy = if invert { -dy } else { dy };
|
||||
self.yaw += (dx * DEGREES) as f32;
|
||||
self.pitch = (self.pitch + (dy * DEGREES) as f32).clamp(-90.0, 90.0);
|
||||
true
|
||||
}
|
||||
|
||||
/// The camera's rotation this frame, arriving as the player's.
|
||||
pub fn camera(&mut self, yaw: &mut f32, pitch: &mut f32) {
|
||||
if !self.active {
|
||||
return;
|
||||
}
|
||||
if self.fresh {
|
||||
(self.yaw, self.pitch) = (*yaw, *pitch);
|
||||
self.fresh = false;
|
||||
}
|
||||
(*yaw, *pitch) = (self.yaw, self.pitch);
|
||||
}
|
||||
}
|
||||
|
||||
// The window's thread only: keys, ticks, the mouse and the camera all run there.
|
||||
thread_local! {
|
||||
static STATE: std::cell::Cell<Freelook> = const { std::cell::Cell::new(Freelook::OFF) };
|
||||
}
|
||||
|
||||
/// Runs `f` on Freelook's state.
|
||||
pub fn with<R>(f: impl FnOnce(&mut Freelook) -> R) -> R {
|
||||
STATE.with(|cell| {
|
||||
let mut state = cell.get();
|
||||
let r = f(&mut state);
|
||||
cell.set(state);
|
||||
r
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn holds_the_key_to_look_from_behind_and_puts_the_view_back() {
|
||||
let mut f = Freelook::OFF;
|
||||
f.key(true, true);
|
||||
assert_eq!(
|
||||
f.tick(Some("hold"), true, true, camera_type("back"), 0),
|
||||
Change::Start(1)
|
||||
);
|
||||
let (mut yaw, mut pitch) = (10.0, 80.0);
|
||||
f.camera(&mut yaw, &mut pitch);
|
||||
assert!(f.turn(100.0, 100.0, false));
|
||||
f.camera(&mut yaw, &mut pitch);
|
||||
assert_eq!((yaw, pitch), (25.0, 90.0));
|
||||
assert!(f.turn(0.0, 100.0, true));
|
||||
assert_eq!(f.pitch, 75.0);
|
||||
f.key(false, true);
|
||||
assert_eq!(f.tick(Some("hold"), true, true, 1, 1), Change::Stop(0));
|
||||
assert!(!f.turn(1.0, 1.0, false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn toggles_and_stops_when_turned_off() {
|
||||
let mut f = Freelook::OFF;
|
||||
f.key(true, false);
|
||||
assert!(!f.toggled, "a press on a screen does nothing");
|
||||
f.key(true, true);
|
||||
f.key(false, true);
|
||||
assert_eq!(f.tick(Some("toggle"), true, true, 0, 0), Change::None);
|
||||
assert!(f.active);
|
||||
assert_eq!(f.tick(None, true, true, 0, 0), Change::None);
|
||||
assert!(!f.active && !f.toggled);
|
||||
assert_eq!(camera_type("front"), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_its_state() {
|
||||
with(|f| f.held = true);
|
||||
assert!(with(|f| f.held));
|
||||
with(|f| *f = Freelook::OFF);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Once a tick: Freelook starts or stops as the key says, and the view changes with it.
|
||||
fn incari_freelook_tick(game: &mut crate::game::Game<'_>) {
|
||||
use crate::game::{incari_client as c, incari_freelook as m};
|
||||
if !game.native_window() {
|
||||
return;
|
||||
}
|
||||
let mode = c::settings(m::ID, |s| {
|
||||
(s.choice("mode"), m::camera_type(s.choice("view")))
|
||||
});
|
||||
let (playing, player, now) = (
|
||||
game.native_playing(),
|
||||
game.native_player().is_some(),
|
||||
game.native_camera_type(),
|
||||
);
|
||||
let change = m::with(|f| {
|
||||
f.tick(
|
||||
mode.map(|x| x.0),
|
||||
playing,
|
||||
player,
|
||||
mode.map_or(now, |x| x.1),
|
||||
now,
|
||||
)
|
||||
});
|
||||
match change {
|
||||
m::Change::Start(view) | m::Change::Stop(view) => game.native_set_camera_type(view),
|
||||
m::Change::None => {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// The mouse turns the Freelook camera, not the player, while it's on.
|
||||
fn incari_freelook_turn(game: &mut crate::game::Game<'_>, dx: f64, dy: f64, claimed: &mut bool) {
|
||||
use crate::game::{incari_client as c, incari_freelook as m};
|
||||
let _ = game;
|
||||
let invert = c::settings(m::ID, |s| s.bool("invert")).unwrap_or(false);
|
||||
if m::with(|f| f.turn(dx, dy, invert)) {
|
||||
*claimed = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user