From 615d8f9d2ac56b3244d328587243301da253eafd Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 28 Dec 2024 02:10:05 +0000 Subject: [PATCH] bump minimum rust version and improve pathfinder docs --- azalea-core/src/color.rs | 4 +-- .../packets/game/c_section_blocks_update.rs | 8 ++--- azalea-world/src/bit_storage.rs | 8 ++--- azalea/examples/testbot/main.rs | 5 ++- azalea/src/pathfinder/astar.rs | 31 +++++++++++-------- azalea/src/pathfinder/mod.rs | 13 ++++++++ azalea/src/pathfinder/world.rs | 5 +-- 7 files changed, 46 insertions(+), 28 deletions(-) diff --git a/azalea-core/src/color.rs b/azalea-core/src/color.rs index eddf50359..ff727ccda 100644 --- a/azalea-core/src/color.rs +++ b/azalea-core/src/color.rs @@ -8,7 +8,7 @@ pub struct RgbColor { impl RgbColor { pub fn new(r: u8, g: u8, b: u8) -> Self { Self { - value: (r as u32) << 16 | (g as u32) << 8 | b as u32, + value: ((r as u32) << 16) | ((g as u32) << 8) | (b as u32), } } @@ -33,7 +33,7 @@ pub struct ArgbColor { impl ArgbColor { pub fn new(a: u8, r: u8, g: u8, b: u8) -> Self { Self { - value: (a as u32) << 24 | (r as u32) << 16 | (g as u32) << 8 | b as u32, + value: ((a as u32) << 24) | ((r as u32) << 16) | ((g as u32) << 8) | b as u32, } } diff --git a/azalea-protocol/src/packets/game/c_section_blocks_update.rs b/azalea-protocol/src/packets/game/c_section_blocks_update.rs index 4554c015d..05ceb30cd 100755 --- a/azalea-protocol/src/packets/game/c_section_blocks_update.rs +++ b/azalea-protocol/src/packets/game/c_section_blocks_update.rs @@ -25,9 +25,9 @@ impl AzaleaRead for BlockStateWithPosition { let state = BlockState::try_from(state) .map_err(|_| BufReadError::UnexpectedEnumVariant { id: state as i32 })?; let pos = ChunkSectionBlockPos { - x: (position_part >> 8 & 15) as u8, + x: ((position_part >> 8) & 15) as u8, y: (position_part & 15) as u8, - z: (position_part >> 4 & 15) as u8, + z: ((position_part >> 4) & 15) as u8, }; Ok(BlockStateWithPosition { pos, state }) } @@ -35,8 +35,8 @@ impl AzaleaRead for BlockStateWithPosition { impl AzaleaWrite for BlockStateWithPosition { fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - let data = (self.state.id as u64) << 12 - | (u64::from(self.pos.x) << 8 | u64::from(self.pos.z) << 4 | u64::from(self.pos.y)); + let data = ((self.state.id as u64) << 12) + | ((u64::from(self.pos.x) << 8) | (u64::from(self.pos.z) << 4) | u64::from(self.pos.y)); u64::azalea_write_var(&data, buf)?; Ok(()) } diff --git a/azalea-world/src/bit_storage.rs b/azalea-world/src/bit_storage.rs index cf3ee7e42..5d92f795e 100755 --- a/azalea-world/src/bit_storage.rs +++ b/azalea-world/src/bit_storage.rs @@ -176,7 +176,7 @@ impl BitStorage { let cell_index = self.cell_index(index as u64); let cell = &self.data[cell_index]; let bit_index = (index - cell_index * self.values_per_long) * self.bits; - cell >> bit_index & self.mask + (cell >> bit_index) & self.mask } pub fn get_and_set(&mut self, index: usize, value: u64) -> u64 { @@ -190,8 +190,8 @@ impl BitStorage { let cell_index = self.cell_index(index as u64); let cell = &mut self.data[cell_index]; let bit_index = (index - cell_index * self.values_per_long) * self.bits; - let old_value = *cell >> (bit_index as u64) & self.mask; - *cell = *cell & !(self.mask << bit_index) | (value & self.mask) << bit_index; + let old_value = (*cell >> (bit_index as u64)) & self.mask; + *cell = (*cell & !(self.mask << bit_index)) | ((value & self.mask) << bit_index); old_value } @@ -206,7 +206,7 @@ impl BitStorage { let cell_index = self.cell_index(index as u64); let cell = &mut self.data[cell_index]; let bit_index = (index - cell_index * self.values_per_long) * self.bits; - *cell = *cell & !(self.mask << bit_index) | (value & self.mask) << bit_index; + *cell = (*cell & !(self.mask << bit_index)) | ((value & self.mask) << bit_index); } /// The number of entries. diff --git a/azalea/examples/testbot/main.rs b/azalea/examples/testbot/main.rs index 680363e6d..0034e946d 100644 --- a/azalea/examples/testbot/main.rs +++ b/azalea/examples/testbot/main.rs @@ -20,7 +20,6 @@ //! only have this on if the bot has operator permissions, otherwise it'll //! just spam the server console unnecessarily. -#![feature(async_closure)] #![feature(trivial_bounds)] mod commands; @@ -53,9 +52,9 @@ async fn main() { for username_or_email in &args.accounts { let account = if username_or_email.contains('@') { - Account::microsoft(&username_or_email).await.unwrap() + Account::microsoft(username_or_email).await.unwrap() } else { - Account::offline(&username_or_email) + Account::offline(username_or_email) }; let mut commands = CommandDispatcher::new(); diff --git a/azalea/src/pathfinder/astar.rs b/azalea/src/pathfinder/astar.rs index c36ea790a..dbdc98367 100644 --- a/azalea/src/pathfinder/astar.rs +++ b/azalea/src/pathfinder/astar.rs @@ -25,19 +25,6 @@ const COEFFICIENTS: [f32; 7] = [1.5, 2., 2.5, 3., 4., 5., 10.]; const MIN_IMPROVEMENT: f32 = 0.01; -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum PathfinderTimeout { - /// Time out after a certain duration has passed. This is a good default so - /// you don't waste too much time calculating a path if you're on a slow - /// computer. - Time(Duration), - /// Time out after this many nodes have been considered. - /// - /// This is useful as an alternative to a time limit if you're doing - /// something like running tests where you want consistent results. - Nodes(usize), -} - type FxIndexMap = IndexMap>; // Sources: @@ -300,3 +287,21 @@ impl PartialOrd for WeightedNode { Some(self.cmp(other)) } } + +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum PathfinderTimeout { + /// Time out after a certain duration has passed. This is a good default so + /// you don't waste too much time calculating a path if you're on a slow + /// computer. + Time(Duration), + /// Time out after this many nodes have been considered. + /// + /// This is useful as an alternative to a time limit if you're doing + /// something like running tests where you want consistent results. + Nodes(usize), +} +impl Default for PathfinderTimeout { + fn default() -> Self { + Self::Time(Duration::from_secs(1)) + } +} diff --git a/azalea/src/pathfinder/mod.rs b/azalea/src/pathfinder/mod.rs index 95982215a..0c926d032 100644 --- a/azalea/src/pathfinder/mod.rs +++ b/azalea/src/pathfinder/mod.rs @@ -142,8 +142,21 @@ pub struct GotoEvent { /// Whether the bot is allowed to break blocks while pathfinding. pub allow_mining: bool, + /// The minimum amount of time that should pass before the A* pathfinder + /// function can return a timeout. It may take up to [`Self::max_timeout`] + /// if it can't immediately find a usable path. + /// + /// A good default value for this is + /// `PathfinderTimeout::Time(Duration::from_secs(1))`. + /// /// Also see [`PathfinderTimeout::Nodes`] pub min_timeout: PathfinderTimeout, + /// The absolute maximum amount of time that the pathfinder function can + /// take to find a path. If it takes this long, it means no usable path was + /// found (so it might be impossible). + /// + /// A good default value for this is + /// `PathfinderTimeout::Time(Duration::from_secs(5))`. pub max_timeout: PathfinderTimeout, } #[derive(Event, Clone, Debug)] diff --git a/azalea/src/pathfinder/world.rs b/azalea/src/pathfinder/world.rs index f518523f9..c50791b8d 100644 --- a/azalea/src/pathfinder/world.rs +++ b/azalea/src/pathfinder/world.rs @@ -262,8 +262,9 @@ impl CachedWorld { let cached_mining_costs = unsafe { &mut *self.cached_mining_costs.get() }; // 20 bits total: // 8 bits for x, 4 bits for y, 8 bits for z - let hash_index = - (pos.x as usize & 0xff) << 12 | (pos.y as usize & 0xf) << 8 | (pos.z as usize & 0xff); + let hash_index = ((pos.x as usize & 0xff) << 12) + | ((pos.y as usize & 0xf) << 8) + | (pos.z as usize & 0xff); debug_assert!(hash_index < 1048576); let &(cached_pos, potential_cost) = unsafe { cached_mining_costs.get_unchecked(hash_index) };