Archive incari-client (client, 26.3, clean)
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
// A hit on a server's object: a crystal it breaks is taken off this client at once.
|
||||
fn incari_fastcrystal_attacked(game: &mut crate::game::Game<'_>, entity: u32) {
|
||||
use crate::game::{incari_client as c, incari_fastcrystal as m};
|
||||
// The server breaks it only if the hit does damage: weakness with a bare hand doesn't.
|
||||
if c::settings(m::ID, |s| s.bool("crystals")) != Some(true) || !game.native_attack_hurts() {
|
||||
return;
|
||||
}
|
||||
if game.native_take_crystal(entity) {
|
||||
m::taken(entity, game.native_latency());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
# Incari's Fast Crystal (features/fastcrystal) for the Rust build (ADR 0068, 0069, 0073). 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_fastcrystal"
|
||||
version = "0.1.0"
|
||||
priority = 100
|
||||
|
||||
[[module]]
|
||||
id = "runtime"
|
||||
file = "crates/horizon-client/src/game.rs"
|
||||
name = "incari_fastcrystal"
|
||||
source = "runtime.rs"
|
||||
|
||||
[[injection]]
|
||||
id = "attacked"
|
||||
# Horizon's object_attacked hook point is still a pending fragment
|
||||
# (tools/native-hook-fragments/incari_fast_crystal.json), so this targets its function. Make it
|
||||
# `hook = "object_attacked"` and `hook_version = 1` once Horizon stamps it.
|
||||
target = "horizon_client::game::native_hooks::Game<'_>::native_object_attacked"
|
||||
kind = "HEAD"
|
||||
helper = "attacked.rs"
|
||||
bindings = [
|
||||
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
|
||||
{ name = "entity", ty = "u32", value = "entity" },
|
||||
]
|
||||
|
||||
[[injection]]
|
||||
id = "used"
|
||||
# Horizon's block_used hook point is still a pending fragment
|
||||
# (tools/native-hook-fragments/incari_fast_crystal.json), so this targets its function. Make it
|
||||
# `hook = "block_used"` and `hook_version = 1` once Horizon stamps it.
|
||||
target = "horizon_client::game::native_hooks::Game<'_>::native_block_used"
|
||||
kind = "HEAD"
|
||||
helper = "used.rs"
|
||||
bindings = [
|
||||
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
|
||||
{ name = "pos", ty = "[i32; 3]", value = "pos" },
|
||||
{ name = "main_hand", ty = "bool", value = "main_hand" },
|
||||
{ name = "sequence", ty = "i32", value = "sequence" },
|
||||
]
|
||||
|
||||
[[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,114 @@
|
||||
// Incari's Fast Crystal for the Rust build (ADR 0068, 0069, 0073), as features/fastcrystal does
|
||||
// it: a crystal the player hits, and a charged anchor they set off outside the Nether, gone on
|
||||
// this client at once instead of a ping later. Prediction only: Horizon takes the crystal off
|
||||
// (`native_take_crystal`) or shows the anchor as air under the use's sequence
|
||||
// (`native_predict_air`); nothing more is sent. This keeps the crystals waiting for the server,
|
||||
// and when each is due to come back. No game types: rust/lib.rs tests it.
|
||||
|
||||
use std::cell::RefCell;
|
||||
|
||||
/// The module's id, as the Java client's profiles name it.
|
||||
pub const ID: &str = "fastcrystal";
|
||||
|
||||
/// The least and most a crystal waits for the server before coming back, in milliseconds.
|
||||
const MIN_WAIT_MS: i64 = 600;
|
||||
const MAX_WAIT_MS: i64 = 3000;
|
||||
const TICK_MS: i64 = 50;
|
||||
|
||||
/// Ticks a crystal waits for the server: twice the ping and 0.3 s, from 0.6 s to 3 s, as Java.
|
||||
pub fn wait_ticks(ping_ms: Option<i32>) -> u32 {
|
||||
let ms = (2 * i64::from(ping_ms.unwrap_or(0).max(0)) + 300).clamp(MIN_WAIT_MS, MAX_WAIT_MS);
|
||||
// Whole ticks, rounded up: never back sooner than the wait.
|
||||
((ms + TICK_MS - 1) / TICK_MS) as u32
|
||||
}
|
||||
|
||||
/// A crystal taken off this client: its entity id, and the ticks left before it's due back.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
struct Waiting {
|
||||
id: u32,
|
||||
ticks: u32,
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
/// Rarely more than a few: one per crystal hit in the last few seconds.
|
||||
static WAITING: RefCell<Vec<Waiting>> = const { RefCell::new(Vec::new()) };
|
||||
}
|
||||
|
||||
/// The crystal `id` was taken off this client; it waits `wait_ticks(ping_ms)` for the server.
|
||||
pub fn taken(id: u32, ping_ms: Option<i32>) {
|
||||
let ticks = wait_ticks(ping_ms);
|
||||
WAITING.with(|w| {
|
||||
let mut w = w.borrow_mut();
|
||||
w.retain(|c| c.id != id);
|
||||
w.push(Waiting { id, ticks });
|
||||
});
|
||||
}
|
||||
|
||||
/// One client tick: `back` gets each crystal whose wait ran out, to put back if the server kept
|
||||
/// it (Horizon forgets the ones the server removed). Hot path: nothing allocated.
|
||||
pub fn tick(mut back: impl FnMut(u32)) {
|
||||
WAITING.with(|w| {
|
||||
w.borrow_mut().retain_mut(|c| {
|
||||
if c.ticks == 0 {
|
||||
back(c.id);
|
||||
false
|
||||
} else {
|
||||
c.ticks -= 1;
|
||||
true
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn drain() {
|
||||
while WAITING.with(|w| !w.borrow().is_empty()) {
|
||||
tick(|_| {});
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_wait_is_twice_the_ping_and_some_within_bounds() {
|
||||
assert_eq!(wait_ticks(None), 12); // 0.6 s at least
|
||||
assert_eq!(wait_ticks(Some(100)), 12); // 500 ms: still the least
|
||||
assert_eq!(wait_ticks(Some(200)), 14); // 700 ms
|
||||
assert_eq!(wait_ticks(Some(201)), 15); // 702 ms rounds up
|
||||
assert_eq!(wait_ticks(Some(5000)), 60); // 3 s at most
|
||||
assert_eq!(wait_ticks(Some(-7)), 12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_crystal_comes_due_once_after_its_wait() {
|
||||
drain();
|
||||
taken(42, None);
|
||||
let mut due = Vec::new();
|
||||
for _ in 0..12 {
|
||||
tick(|id| due.push(id));
|
||||
}
|
||||
assert!(due.is_empty());
|
||||
tick(|id| due.push(id));
|
||||
assert_eq!(due, [42]);
|
||||
tick(|id| due.push(id));
|
||||
assert_eq!(due, [42]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hitting_the_same_crystal_again_restarts_its_wait() {
|
||||
drain();
|
||||
taken(7, None);
|
||||
for _ in 0..10 {
|
||||
tick(|_| {});
|
||||
}
|
||||
taken(7, None);
|
||||
let mut due = 0;
|
||||
for _ in 0..12 {
|
||||
tick(|_| due += 1);
|
||||
}
|
||||
assert_eq!(due, 0);
|
||||
tick(|_| due += 1);
|
||||
assert_eq!(due, 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// Crystals the server kept past their wait come back.
|
||||
fn incari_fastcrystal_tick(game: &mut crate::game::Game<'_>) {
|
||||
use crate::game::incari_fastcrystal as m;
|
||||
m::tick(|id| {
|
||||
game.native_return_crystal(id);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// A block used on a server: a charged anchor it sets off is shown as air until the server answers.
|
||||
fn incari_fastcrystal_used(game: &mut crate::game::Game<'_>, pos: [i32; 3], main_hand: bool, sequence: i32) {
|
||||
use crate::game::{incari_client as c, incari_fastcrystal as m};
|
||||
if !main_hand
|
||||
|| c::settings(m::ID, |s| s.bool("anchors")) != Some(true)
|
||||
|| !game.native_anchor_set_off(pos)
|
||||
{
|
||||
return;
|
||||
}
|
||||
game.native_predict_air(pos, sequence);
|
||||
}
|
||||
Reference in New Issue
Block a user