Skip to content

Commit

Permalink
bump minimum rust version and improve pathfinder docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Dec 28, 2024
1 parent ebaf512 commit 615d8f9
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 28 deletions.
4 changes: 2 additions & 2 deletions azalea-core/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand All @@ -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,
}
}

Expand Down
8 changes: 4 additions & 4 deletions azalea-protocol/src/packets/game/c_section_blocks_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ 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 })
}
}

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(())
}
Expand Down
8 changes: 4 additions & 4 deletions azalea-world/src/bit_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand All @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions azalea/examples/testbot/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
31 changes: 18 additions & 13 deletions azalea/src/pathfinder/astar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;

// Sources:
Expand Down Expand Up @@ -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))
}
}
13 changes: 13 additions & 0 deletions azalea/src/pathfinder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 3 additions & 2 deletions azalea/src/pathfinder/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
Expand Down

0 comments on commit 615d8f9

Please sign in to comment.