1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00
azalea/azalea-core/src/math.rs
mat 7405427199
Mining (#95)
* more mining stuff

* initialize azalea-tags crate

* more mining stuff 2

* mining in ecs

* well technically mining works but

no codegen for how long it takes to mine each block yet

* rename downloads to __cache__

it was bothering me since it's not *just* downloads

* codegen block behavior

* fix not sending packet to finish breaking block

* mining animation 🎉

* clippy

* cleanup, move Client::mine into a client extension

* add azalea/src/mining.rs

---------

Co-authored-by: mat <git@matdoes.dev>
2023-07-14 22:20:40 -05:00

56 lines
1.1 KiB
Rust

// TODO: make this generic
pub fn binary_search(mut min: i32, max: i32, predicate: &dyn Fn(i32) -> bool) -> i32 {
let mut diff = max - min;
while diff > 0 {
let diff_mid = diff / 2;
let mid = min + diff_mid;
if predicate(mid) {
diff = diff_mid;
} else {
min = mid + 1;
diff -= diff_mid + 1;
}
}
min
}
pub fn lcm(a: u32, b: u32) -> u64 {
let gcd = gcd(a, b);
(a as u64) * (b / gcd) as u64
}
pub fn gcd(mut a: u32, mut b: u32) -> u32 {
while b != 0 {
let t = b;
b = a % b;
a = t;
}
a
}
pub fn lerp<T: num_traits::Float>(amount: T, a: T, b: T) -> T {
a + amount * (b - a)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_gcd() {
assert_eq!(gcd(0, 0), 0);
assert_eq!(gcd(1, 1), 1);
assert_eq!(gcd(0, 1), 1);
assert_eq!(gcd(1, 0), 1);
assert_eq!(gcd(12, 8), 4);
assert_eq!(gcd(8, 12), 4);
assert_eq!(gcd(12, 9), 3);
assert_eq!(gcd(9, 12), 3);
assert_eq!(gcd(12, 7), 1);
assert_eq!(gcd(7, 12), 1);
}
}