#[must_use]
pub fn java_string_hash(s: &str) -> i32 {
let mut h: i32 = 0;
for u in s.encode_utf16() {
h = h.wrapping_mul(31).wrapping_add(i32::from(u));
}
h
}
#[must_use]
pub fn identifier_hash(id: &str) -> i32 {
let (ns, path) = id.split_once(':').unwrap_or(("minecraft", id));
java_string_hash(ns)
.wrapping_mul(31)
.wrapping_add(java_string_hash(path))
}
#[must_use]
pub fn identifier_key(id: &str) -> u64 {
let full;
let id = if id.contains(':') {
id
} else {
full = format!("minecraft:{id}");
&full
};
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
for b in id.bytes() {
h = (h ^ u64::from(b)).wrapping_mul(0x0000_0100_0000_01b3);
}
h
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ModifierId {
pub hash: i32,
pub key: u64,
}
impl ModifierId {
#[must_use]
pub fn new(id: &str) -> ModifierId {
ModifierId {
hash: identifier_hash(id),
key: identifier_key(id),
}
}
}
#[must_use]
pub fn sprinting_id() -> ModifierId {
ModifierId::new("minecraft:sprinting")
}
pub const SPRINT_AMOUNT: f64 = 0.3f32 as f64;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Modifier {
pub id: ModifierId,
pub amount: f64,
}
const SLOTS: usize = 32;
pub const MAX_MODIFIERS: usize = 12;
#[inline]
fn mix(x: i32) -> i32 {
let h = x.wrapping_mul(0x9E37_79B9_u32 as i32);
h ^ ((h as u32) >> 16) as i32
}
#[inline]
fn home(id: ModifierId) -> usize {
(mix(id.hash) as u32 as usize) & (SLOTS - 1)
}
#[derive(Clone, Copy, Debug, PartialEq)]
struct Entry {
m: Modifier,
op: u8,
slot: u8,
}
const EMPTY_ENTRY: Entry = Entry {
m: Modifier {
id: ModifierId { hash: 0, key: 0 },
amount: 0.0,
},
op: 0,
slot: 0,
};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Attribute {
base: f64,
min: f64,
max: f64,
value: f64,
entries: [Entry; MAX_MODIFIERS],
len: u8,
}
impl Attribute {
#[must_use]
pub fn new(base: f64, min: f64, max: f64) -> Attribute {
let mut a = Attribute {
base,
min,
max,
value: 0.0,
entries: [EMPTY_ENTRY; MAX_MODIFIERS],
len: 0,
};
a.recompute();
a
}
#[must_use]
pub fn base(&self) -> f64 {
self.base
}
#[inline]
#[must_use]
pub fn value(&self) -> f64 {
self.value
}
pub fn set_base(&mut self, base: f64) {
self.base = base;
self.recompute();
}
fn entries(&self) -> &[Entry] {
&self.entries[..usize::from(self.len)]
}
fn table(&self, op: u8) -> [u8; SLOTS] {
let mut t = [0u8; SLOTS];
for (i, e) in self.entries().iter().enumerate() {
if e.op == op {
t[usize::from(e.slot)] = i as u8 + 1;
}
}
t
}
fn recompute(&mut self) {
let mut base = self.base;
for &i in self.table(0).iter().rev() {
if i != 0 {
base += self.entries[usize::from(i - 1)].m.amount;
}
}
let mut result = base;
for &i in self.table(1).iter().rev() {
if i != 0 {
result += base * self.entries[usize::from(i - 1)].m.amount;
}
}
for &i in self.table(2).iter().rev() {
if i != 0 {
result *= 1.0 + self.entries[usize::from(i - 1)].m.amount;
}
}
self.value = if result.is_nan() || result < self.min {
self.min
} else {
crate::math::java_min(result, self.max)
};
}
#[must_use]
pub fn contains(&self, id: ModifierId) -> bool {
self.entries().iter().any(|e| e.m.id == id)
}
pub fn add(&mut self, id: ModifierId, amount: f64, operation: u8) {
if operation > 2 || self.contains(id) || usize::from(self.len) >= MAX_MODIFIERS {
return;
}
let t = self.table(operation);
let mut pos = home(id);
while t[pos] != 0 {
pos = (pos + 1) & (SLOTS - 1);
}
self.entries[usize::from(self.len)] = Entry {
m: Modifier { id, amount },
op: operation,
slot: pos as u8,
};
self.len += 1;
self.recompute();
}
pub fn remove(&mut self, id: ModifierId) -> bool {
let Some(idx) = self.entries().iter().position(|e| e.m.id == id) else {
return false;
};
let op = self.entries[idx].op;
let mut t = self.table(op);
let mut pos = usize::from(self.entries[idx].slot);
t[pos] = 0;
'outer: loop {
let last = pos;
pos = (last + 1) & (SLOTS - 1);
loop {
let cur = t[pos];
if cur == 0 {
break 'outer;
}
let slot = home(self.entries[usize::from(cur - 1)].m.id);
let stays = if last <= pos {
last >= slot || slot > pos
} else {
last >= slot && slot > pos
};
if stays {
t[last] = cur;
t[pos] = 0;
break;
}
pos = (pos + 1) & (SLOTS - 1);
}
}
for (s, &i) in t.iter().enumerate() {
if i != 0 {
self.entries[usize::from(i - 1)].slot = s as u8;
}
}
let n = usize::from(self.len);
self.entries.copy_within(idx + 1..n, idx);
self.len -= 1;
self.recompute();
true
}
pub fn apply_packet(&mut self, base: f64, modifiers: &[(ModifierId, f64, u8)]) {
self.base = base;
self.len = 0;
for &(id, amount, op) in modifiers {
self.add(id, amount, op);
}
self.recompute();
}
pub fn modifiers(&self, operation: u8) -> impl Iterator<Item = Modifier> + '_ {
let t = self.table(operation);
(0..SLOTS)
.rev()
.filter(move |&s| t[s] != 0)
.map(move |s| self.entries[usize::from(t[s] - 1)].m)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn identifier_hash_is_java() {
assert_eq!(java_string_hash("minecraft"), 695_073_197);
assert_eq!(java_string_hash("sprinting"), 1_429_809_032);
assert_eq!(
identifier_hash("minecraft:sprinting"),
java_string_hash("minecraft")
.wrapping_mul(31)
.wrapping_add(java_string_hash("sprinting"))
);
}
#[test]
fn shift_delete_keeps_chain() {
let mut a = Attribute::new(0.0, -1.0e9, 1.0e9);
let ids: Vec<ModifierId> = (0..10)
.map(|i| ModifierId::new(&format!("t:m{i}")))
.collect();
for (i, &id) in ids.iter().enumerate() {
a.add(id, i as f64, 0);
}
for &id in &ids[..5] {
assert!(a.remove(id));
}
for &id in &ids[5..] {
assert!(a.contains(id));
}
assert_eq!(a.value(), 5.0 + 6.0 + 7.0 + 8.0 + 9.0);
}
#[test]
fn iteration_order_matches_fastutil() {
let mut a = Attribute::new(0.0, -1.0e9, 1.0e9);
let first = ModifierId::new("oracle:m9346f493eec5");
let second = ModifierId::new("oracle:mb720f71bb819");
a.add(second, 1.0, 0);
a.add(first, 2.0, 0);
let order: Vec<ModifierId> = a.modifiers(0).map(|m| m.id).collect();
assert_eq!(order, vec![first, second]);
}
}