diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2023-07-14 22:20:40 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-14 22:20:40 -0500 |
| commit | 7405427199e5a994d4a6a706f84434a69cb7a7d9 (patch) | |
| tree | ca537e5d761bc053187d952fced0915c850b92aa /azalea-core/src/math.rs | |
| parent | d1afd02aa84e7b4450c1607277f078eb2a0f1bf3 (diff) | |
| download | azalea-drasl-7405427199e5a994d4a6a706f84434a69cb7a7d9.tar.xz | |
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>
Diffstat (limited to 'azalea-core/src/math.rs')
| -rw-r--r-- | azalea-core/src/math.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/azalea-core/src/math.rs b/azalea-core/src/math.rs new file mode 100644 index 00000000..2e5e78f6 --- /dev/null +++ b/azalea-core/src/math.rs @@ -0,0 +1,56 @@ +// 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); + } +} |
