1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

turns out my floor_mod impl was wrong

and i32::rem_euclid has the correct behavior LOL
This commit is contained in:
mat 2022-11-23 20:31:43 -06:00
parent 1d359dace7
commit 155b620b38
4 changed files with 21 additions and 27 deletions

View file

@ -766,7 +766,7 @@ impl Client {
.update_view_center(&ChunkPos::new(p.x, p.z));
}
ClientboundGamePacket::LevelChunkWithLight(p) => {
// warn!("Got chunk with light packet {} {}", p.x, p.z);
// debug!("Got chunk with light packet {} {}", p.x, p.z);
let pos = ChunkPos::new(p.x, p.z);
// OPTIMIZATION: if we already know about the chunk from the

View file

@ -1,7 +1,5 @@
use azalea_buf::McBuf;
use crate::floor_mod;
#[derive(Clone, Copy, Debug, McBuf, Default)]
pub enum Direction {
#[default]
@ -116,7 +114,7 @@ impl AxisCycle {
}
}
pub fn between(axis0: Axis, axis1: Axis) -> Self {
Self::from_ordinal(floor_mod(axis1 as i32 - axis0 as i32, 3))
Self::from_ordinal(i32::rem_euclid(axis1 as i32 - axis0 as i32, 3) as u32)
}
pub fn inverse(self) -> Self {
match self {
@ -128,8 +126,8 @@ impl AxisCycle {
pub fn cycle(self, axis: Axis) -> Axis {
match self {
Self::None => axis,
Self::Forward => Axis::from_ordinal(floor_mod(axis as i32 + 1, 3)),
Self::Backward => Axis::from_ordinal(floor_mod(axis as i32 - 1, 3)),
Self::Forward => Axis::from_ordinal(i32::rem_euclid(axis as i32 + 1, 3) as u32),
Self::Backward => Axis::from_ordinal(i32::rem_euclid(axis as i32 - 1, 3) as u32),
}
}
pub fn cycle_xyz(self, x: i32, y: i32, z: i32, axis: Axis) -> i32 {

View file

@ -38,16 +38,6 @@ pub use aabb::*;
mod block_hit_result;
pub use block_hit_result::*;
// java moment
// TODO: add tests and optimize/simplify this
pub fn floor_mod(x: i32, y: u32) -> u32 {
if x < 0 {
y - ((-x) as u32 % y)
} else {
x as u32 % y
}
}
// TODO: make this generic
pub fn binary_search(mut min: i32, max: i32, predicate: &dyn Fn(i32) -> bool) -> i32 {
let mut diff = max - min;

View file

@ -4,10 +4,10 @@ use crate::World;
use azalea_block::BlockState;
use azalea_buf::BufReadError;
use azalea_buf::{McBufReadable, McBufWritable};
use azalea_core::floor_mod;
use azalea_core::{BlockPos, ChunkBlockPos, ChunkPos, ChunkSectionBlockPos};
use log::debug;
use log::trace;
use log::warn;
use parking_lot::Mutex;
use parking_lot::RwLock;
use std::collections::HashMap;
@ -104,8 +104,8 @@ impl PartialChunkStorage {
}
fn get_index(&self, chunk_pos: &ChunkPos) -> usize {
(floor_mod(chunk_pos.x, self.view_range) * self.view_range
+ floor_mod(chunk_pos.z, self.view_range)) as usize
(i32::rem_euclid(chunk_pos.x, self.view_range as i32) * (self.view_range as i32)
+ i32::rem_euclid(chunk_pos.z, self.view_range as i32)) as usize
}
pub fn in_range(&self, chunk_pos: &ChunkPos) -> bool {
@ -158,19 +158,25 @@ impl PartialChunkStorage {
/// Get a [`Chunk`] within render distance, or `None` if it's not loaded.
/// Use [`PartialChunkStorage::get`] to get a chunk from the shared storage.
///
/// # Panics
/// If the chunk is not in the render distance.
pub fn limited_get(&self, pos: &ChunkPos) -> &Option<Arc<Mutex<Chunk>>> {
pub fn limited_get(&self, pos: &ChunkPos) -> Option<&Arc<Mutex<Chunk>>> {
let index = self.get_index(pos);
&self.chunks[index]
if index >= self.chunks.len() {
warn!(
"Chunk at {:?} is not in the render distance (center: {:?}, {} chunks, {} >= {})",
pos,
self.view_center,
self.chunk_radius,
index,
self.chunks.len()
);
None
} else {
self.chunks[index].as_ref()
}
}
/// Get a mutable reference to a [`Chunk`] within render distance, or
/// `None` if it's not loaded. Use [`PartialChunkStorage::get`] to get
/// a chunk from the shared storage.
///
/// # Panics
/// If the chunk is not in the render distance.
pub fn limited_get_mut(&mut self, pos: &ChunkPos) -> Option<&mut Option<Arc<Mutex<Chunk>>>> {
let index = self.get_index(pos);
if index >= self.chunks.len() {