diff options
| author | mat <git@matdoes.dev> | 2026-01-16 23:45:08 -0845 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2026-01-17 07:03:37 -0200 |
| commit | 5ec2c28109b9c4e17546f03e80508e0ef960fc77 (patch) | |
| tree | 1f2799cec1b9c0257e63c204eb5124a16dec09c3 | |
| parent | ddb21fdd4acf6e03fecef33b16f3acc25e4eaf30 (diff) | |
| download | azalea-drasl-5ec2c28109b9c4e17546f03e80508e0ef960fc77.tar.xz | |
move NodeMap to a module
| -rw-r--r-- | azalea/benches/pathfinder.rs | 2 | ||||
| -rw-r--r-- | azalea/examples/testbot/commands/debug.rs | 2 | ||||
| -rw-r--r-- | azalea/src/pathfinder/astar/mod.rs | 22 | ||||
| -rw-r--r-- | azalea/src/pathfinder/astar/nodemap.rs | 30 |
4 files changed, 43 insertions, 13 deletions
diff --git a/azalea/benches/pathfinder.rs b/azalea/benches/pathfinder.rs index 9f8d2c34..5c06474b 100644 --- a/azalea/benches/pathfinder.rs +++ b/azalea/benches/pathfinder.rs @@ -3,7 +3,7 @@ use std::{hint::black_box, sync::Arc, time::Duration}; use azalea::{ BlockPos, pathfinder::{ - astar::{self, PathfinderTimeout, WeightedNode, a_star}, + astar::{self, PathfinderTimeout, a_star, heap::WeightedNode}, custom_state::CustomPathfinderStateRef, goals::{BlockPosGoal, Goal}, mining::MiningCache, diff --git a/azalea/examples/testbot/commands/debug.rs b/azalea/examples/testbot/commands/debug.rs index 4cde3335..3c72c85f 100644 --- a/azalea/examples/testbot/commands/debug.rs +++ b/azalea/examples/testbot/commands/debug.rs @@ -15,7 +15,7 @@ use azalea::{ }; use azalea_core::hit_result::HitResult; use azalea_entity::{EntityKindComponent, metadata}; -use azalea_inventory::{Menu, Player, components::MaxStackSize}; +use azalea_inventory::{Menu, components::MaxStackSize}; use azalea_world::Worlds; use bevy_app::AppExit; use bevy_ecs::{message::Messages, query::With, world::EntityRef}; diff --git a/azalea/src/pathfinder/astar/mod.rs b/azalea/src/pathfinder/astar/mod.rs index 484ab572..e6118bc0 100644 --- a/azalea/src/pathfinder/astar/mod.rs +++ b/azalea/src/pathfinder/astar/mod.rs @@ -1,17 +1,19 @@ pub mod heap; +pub mod nodemap; use std::{ fmt::{self, Debug}, - hash::{BuildHasherDefault, Hash}, + hash::Hash, time::{Duration, Instant}, }; -use indexmap::IndexMap; use num_format::ToFormattedString; -use rustc_hash::FxHasher; use tracing::{debug, trace, warn}; -use crate::pathfinder::astar::heap::{PathfinderHeap, WeightedNode}; +use crate::pathfinder::astar::{ + heap::{PathfinderHeap, WeightedNode}, + nodemap::NodeMap, +}; pub struct Path<P, M> where @@ -32,8 +34,6 @@ const COEFFICIENTS: [f32; 7] = [1.5, 2., 2.5, 3., 4., 5., 10.]; const MIN_IMPROVEMENT: f32 = 0.01; -type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>; - // Sources: // - https://en.wikipedia.org/wiki/A*_search_algorithm // - https://github.com/evenfurther/pathfinding/blob/main/src/directed/astar.rs @@ -60,7 +60,7 @@ where f_score: 0., index: 0, }); - let mut nodes: FxIndexMap<P, Node> = IndexMap::default(); + let mut nodes: NodeMap<P> = NodeMap::default(); nodes.insert( start, Node { @@ -76,7 +76,7 @@ where let mut num_movements = 0; while let Some(WeightedNode { index, g_score, .. }) = open_set.pop() { - let (&node, node_data) = nodes.get_index(index as usize).unwrap(); + let (&node, node_data) = nodes.get_index(index).unwrap(); if g_score > node_data.g_score { continue; } @@ -212,7 +212,7 @@ fn determine_best_path_idx(best_paths: [u32; 7], start: u32) -> usize { } fn reconstruct_path<P, M, SuccessorsFn>( - nodes: FxIndexMap<P, Node>, + nodes: NodeMap<P>, mut current_index: u32, mut successors: SuccessorsFn, ) -> Vec<Movement<P, M>> @@ -221,11 +221,11 @@ where SuccessorsFn: FnMut(P) -> Vec<Edge<P, M>>, { let mut path = Vec::new(); - while let Some((&node_position, node)) = nodes.get_index(current_index as usize) { + while let Some((&node_position, node)) = nodes.get_index(current_index) { if node.came_from == u32::MAX { break; } - let came_from_position = *nodes.get_index(node.came_from as usize).unwrap().0; + let came_from_position = *nodes.get_index(node.came_from).unwrap().0; // find the movement data for this successor, we have to do this again because // we don't include the movement data in the Node (as an optimization) diff --git a/azalea/src/pathfinder/astar/nodemap.rs b/azalea/src/pathfinder/astar/nodemap.rs new file mode 100644 index 00000000..62d97c9c --- /dev/null +++ b/azalea/src/pathfinder/astar/nodemap.rs @@ -0,0 +1,30 @@ +use std::hash::{BuildHasherDefault, Hash}; + +use indexmap::{IndexMap, map::Entry}; +use rustc_hash::FxHasher; + +use crate::pathfinder::astar::Node; + +type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>; + +pub struct NodeMap<P> { + map: FxIndexMap<P, Node>, +} +impl<P: Hash + Eq> NodeMap<P> { + pub fn insert(&mut self, key: P, value: Node) { + self.map.insert(key, value); + } + pub fn get_index(&self, index: u32) -> Option<(&P, &Node)> { + self.map.get_index(index as usize) + } + pub fn entry(&mut self, key: P) -> Entry<'_, P, Node> { + self.map.entry(key) + } +} +impl<P> Default for NodeMap<P> { + fn default() -> Self { + Self { + map: Default::default(), + } + } +} |
