Archive incari-client (client, 26.3, clean)
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
// A hit: remembered for its kill, and its particles.
|
||||
fn incari_hiteffects_attacked(game: &mut crate::game::Game<'_>, entity: u32, pos: [f64; 3]) {
|
||||
use crate::game::{incari_client as c, incari_hiteffects as m};
|
||||
let Some((on, key, count)) = c::settings(m::ID, |s| {
|
||||
(s.bool("on-hit"), s.choice("hit"), s.int("hit-count"))
|
||||
}) else {
|
||||
return;
|
||||
};
|
||||
// Its size, from Horizon's list (a hit is rare: walking it is fine).
|
||||
let size = (0..game.native_entity_count())
|
||||
.filter_map(|n| game.native_entity_at(n, 1.0))
|
||||
.find(|e| e.id == entity)
|
||||
.map_or(m::SIZE, |e| [e.max[0] - e.min[0], e.max[1] - e.min[1]]);
|
||||
m::with(|s| s.hit(entity, size));
|
||||
if !on {
|
||||
return;
|
||||
}
|
||||
let (particle, speed, _) = m::effect(key);
|
||||
for _ in 0..count.max(0) {
|
||||
let (at, velocity) = m::with(|s| s.spray(pos, size, speed));
|
||||
game.native_spawn_particle(particle, at, velocity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// A death soon after the player's hit: the kill burst and its sound.
|
||||
fn incari_hiteffects_died(game: &mut crate::game::Game<'_>, entity: u32, pos: [f64; 3]) {
|
||||
use crate::game::{incari_client as c, incari_hiteffects as m};
|
||||
let Some((on, key, count, sound)) = c::settings(m::ID, |s| {
|
||||
(
|
||||
s.bool("on-kill"),
|
||||
s.choice("kill"),
|
||||
s.int("kill-count"),
|
||||
s.bool("kill-sound"),
|
||||
)
|
||||
}) else {
|
||||
return;
|
||||
};
|
||||
let Some(size) = m::with(|s| s.killed(entity).then(|| s.size())) else {
|
||||
return;
|
||||
};
|
||||
if !on {
|
||||
return;
|
||||
}
|
||||
let (particle, speed, event) = m::effect(key);
|
||||
for _ in 0..count.max(0) {
|
||||
let (at, velocity) = m::with(|s| s.spray(pos, size, speed));
|
||||
game.native_spawn_particle(particle, at, velocity);
|
||||
}
|
||||
if sound {
|
||||
let at = [pos[0], pos[1] + size[1] / 2.0, pos[2]];
|
||||
game.native_play_local_sound(event, at, m::VOLUME, m::PITCH, 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// Ages the remembered hit.
|
||||
fn incari_hiteffects_frame(elapsed: std::time::Duration) {
|
||||
crate::game::incari_hiteffects::with(|s| s.advance(elapsed.as_secs_f32()));
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
# Incari's Hit & Kill Effects (features/hiteffects) 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), shown in the module menu.
|
||||
[mod]
|
||||
id = "incari_hiteffects"
|
||||
version = "0.1.0"
|
||||
priority = 100
|
||||
|
||||
[[module]]
|
||||
id = "runtime"
|
||||
file = "crates/horizon-client/src/game.rs"
|
||||
name = "incari_hiteffects"
|
||||
source = "runtime.rs"
|
||||
|
||||
[[injection]]
|
||||
id = "attacked"
|
||||
# Horizon's player_attacked hook point is still a pending fragment
|
||||
# (tools/native-hook-fragments/incari_client_hooks.json), so this targets its function. Make it
|
||||
# `hook = "player_attacked"` and `hook_version = 1` once Horizon stamps it.
|
||||
target = "horizon_client::game::native_hooks::Game<'_>::native_player_attacked"
|
||||
kind = "HEAD"
|
||||
helper = "attacked.rs"
|
||||
bindings = [
|
||||
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
|
||||
{ name = "entity", ty = "u32", value = "entity" },
|
||||
{ name = "pos", ty = "[f64; 3]", value = "pos" },
|
||||
]
|
||||
|
||||
[[injection]]
|
||||
id = "died"
|
||||
# Horizon's mob_died hook point is still a pending fragment
|
||||
# (tools/native-hook-fragments/incari_client_hooks.json), so this targets its function. Make it
|
||||
# `hook = "mob_died"` and `hook_version = 1` once Horizon stamps it.
|
||||
target = "horizon_client::game::native_hooks::Game<'_>::native_mob_died"
|
||||
kind = "HEAD"
|
||||
helper = "died.rs"
|
||||
bindings = [
|
||||
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
|
||||
{ name = "entity", ty = "u32", value = "entity" },
|
||||
{ name = "pos", ty = "[f64; 3]", value = "pos" },
|
||||
]
|
||||
|
||||
[[injection]]
|
||||
id = "frame"
|
||||
hook = "frame"
|
||||
hook_version = 1
|
||||
kind = "HEAD"
|
||||
helper = "frame.rs"
|
||||
bindings = [
|
||||
{ name = "elapsed", ty = "std::time::Duration", value = "elapsed" },
|
||||
]
|
||||
@@ -0,0 +1,194 @@
|
||||
// Incari's Hit & Kill Effects for the Rust build (ADR 0068, 0069), as features/hiteffects and the
|
||||
// Java client's ModuleEffects do it: particles on whatever you hit, and a bigger burst with the
|
||||
// effect's sound when it dies within 3 seconds of your last hit. Only you see them. Its settings
|
||||
// are the module's in `incari_core::modules` (the effect keys of `modules::EFFECTS`). No game
|
||||
// types: rust/lib.rs tests it.
|
||||
|
||||
/// The module's id, as the Java client's profiles name it.
|
||||
pub const ID: &str = "hiteffects";
|
||||
|
||||
/// A death counts as your kill this long after your hit on it (the Java client's 60 ticks).
|
||||
pub const KILL_WINDOW: f32 = 3.0;
|
||||
/// The kill sound's volume and pitch.
|
||||
pub const VOLUME: f32 = 0.6;
|
||||
pub const PITCH: f32 = 1.0;
|
||||
/// A mob's size when Horizon doesn't list it: a player's.
|
||||
pub const SIZE: [f64; 2] = [0.6, 1.8];
|
||||
|
||||
/// An effect key's particle, how fast its particles fly apart, and its sound.
|
||||
pub fn effect(key: &str) -> (&'static str, f64, &'static str) {
|
||||
match key {
|
||||
"crits" => ("minecraft:crit", 0.3, "minecraft:entity.player.attack.crit"),
|
||||
"sparks" => (
|
||||
"minecraft:electric_spark",
|
||||
0.3,
|
||||
"minecraft:block.amethyst_block.chime",
|
||||
),
|
||||
"hearts" => (
|
||||
"minecraft:heart",
|
||||
0.05,
|
||||
"minecraft:entity.experience_orb.pickup",
|
||||
),
|
||||
"flames" => ("minecraft:flame", 0.3, "minecraft:item.firecharge.use"),
|
||||
"snow" => (
|
||||
"minecraft:snowflake",
|
||||
0.3,
|
||||
"minecraft:block.powder_snow.break",
|
||||
),
|
||||
"firework" => (
|
||||
"minecraft:firework",
|
||||
0.5,
|
||||
"minecraft:entity.firework_rocket.blast",
|
||||
),
|
||||
"souls" => ("minecraft:soul", 0.05, "minecraft:particle.soul_escape"),
|
||||
"smoke" => ("minecraft:poof", 0.1, "minecraft:block.fire.extinguish"),
|
||||
"totem" => (
|
||||
"minecraft:totem_of_undying",
|
||||
0.5,
|
||||
"minecraft:item.totem.use",
|
||||
),
|
||||
_ => (
|
||||
"minecraft:enchanted_hit",
|
||||
0.3,
|
||||
"minecraft:block.enchantment_table.use",
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// What the player hit last, how long ago, its size, and a little random state.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct State {
|
||||
target: Option<u32>,
|
||||
since: f32,
|
||||
size: [f64; 2],
|
||||
seed: u64,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
target: None,
|
||||
since: 0.0,
|
||||
size: SIZE,
|
||||
seed: 0x9E37_79B9_7F4A_7C15,
|
||||
}
|
||||
}
|
||||
|
||||
/// The player hit `entity`, `size` (width, height) big.
|
||||
pub fn hit(&mut self, entity: u32, size: [f64; 2]) {
|
||||
self.target = Some(entity);
|
||||
self.since = 0.0;
|
||||
self.size = size;
|
||||
}
|
||||
|
||||
pub fn advance(&mut self, seconds: f32) {
|
||||
self.since += seconds;
|
||||
if self.since > KILL_WINDOW {
|
||||
self.target = None;
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `entity` dying is the player's kill, forgetting it.
|
||||
pub fn killed(&mut self, entity: u32) -> bool {
|
||||
if self.target != Some(entity) {
|
||||
return false;
|
||||
}
|
||||
self.target = None;
|
||||
true
|
||||
}
|
||||
|
||||
/// The last hit's size.
|
||||
pub fn size(&self) -> [f64; 2] {
|
||||
self.size
|
||||
}
|
||||
|
||||
/// A number from 0 to 1 (xorshift).
|
||||
pub fn random(&mut self) -> f64 {
|
||||
self.seed ^= self.seed << 13;
|
||||
self.seed ^= self.seed >> 7;
|
||||
self.seed ^= self.seed << 17;
|
||||
(self.seed >> 11) as f64 / (1u64 << 53) as f64
|
||||
}
|
||||
|
||||
/// A particle's start in the box of a mob `size` big standing at `at`, and its velocity, as
|
||||
/// the Java client sprays them.
|
||||
pub fn spray(&mut self, at: [f64; 3], size: [f64; 2], speed: f64) -> ([f64; 3], [f64; 3]) {
|
||||
let [w, h] = size;
|
||||
let start = [
|
||||
at[0] - w / 2.0 + self.random() * w,
|
||||
at[1] + h * (0.2 + self.random() * 0.7),
|
||||
at[2] - w / 2.0 + self.random() * w,
|
||||
];
|
||||
let velocity = [
|
||||
(self.random() - 0.5) * speed,
|
||||
self.random() * speed * 0.6,
|
||||
(self.random() - 0.5) * speed,
|
||||
];
|
||||
(start, velocity)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for State {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
// The window's thread only: attacks, deaths and frames all run there.
|
||||
thread_local! {
|
||||
static STATE: std::cell::RefCell<State> = const { std::cell::RefCell::new(State::new()) };
|
||||
}
|
||||
|
||||
pub fn with<R>(f: impl FnOnce(&mut State) -> R) -> R {
|
||||
STATE.with(|s| f(&mut s.borrow_mut()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn counts_a_death_as_a_kill_only_soon_after_a_hit() {
|
||||
let mut s = State::new();
|
||||
s.hit(7, [1.0, 2.0]);
|
||||
s.advance(1.0);
|
||||
assert!(s.killed(7));
|
||||
assert!(!s.killed(7));
|
||||
s.hit(8, SIZE);
|
||||
s.advance(KILL_WINDOW + 0.1);
|
||||
assert!(!s.killed(8));
|
||||
s.hit(8, SIZE);
|
||||
s.hit(9, SIZE);
|
||||
assert!(!s.killed(8) && s.killed(9));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sprays_in_the_body() {
|
||||
let mut s = State::new();
|
||||
for _ in 0..100 {
|
||||
let r = s.random();
|
||||
assert!((0.0..1.0).contains(&r));
|
||||
let (start, v) = s.spray([0.0; 3], [0.6, 1.8], 0.3);
|
||||
assert!(start[1] >= 0.36 && start[1] <= 1.62 && start[0].abs() <= 0.3);
|
||||
assert!(v[1] >= 0.0 && v[0].abs() <= 0.15);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn knows_every_effect() {
|
||||
assert_eq!(effect("magic").0, "minecraft:enchanted_hit");
|
||||
assert_eq!(
|
||||
effect("firework"),
|
||||
(
|
||||
"minecraft:firework",
|
||||
0.5,
|
||||
"minecraft:entity.firework_rocket.blast"
|
||||
)
|
||||
);
|
||||
for key in [
|
||||
"crits", "sparks", "hearts", "flames", "snow", "souls", "smoke", "totem",
|
||||
] {
|
||||
assert_ne!(effect(key).0, "minecraft:enchanted_hit", "{key}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user