aboutsummaryrefslogtreecommitdiff
path: root/azalea/src/pathfinder/custom_state.rs
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2025-06-01 21:01:31 -0545
committermat <git@matdoes.dev>2025-06-01 21:01:31 -0545
commit99659bd9a33fad276c2a5ecfb68f094c4f544d48 (patch)
tree531aceb09b3db86dde407e331d5b13bc5d821a4e /azalea/src/pathfinder/custom_state.rs
parent1d3a7c969f430a8cea4296930df0d6c04e253747 (diff)
downloadazalea-drasl-99659bd9a33fad276c2a5ecfb68f094c4f544d48.tar.xz
add CustomPathfinderState
Diffstat (limited to 'azalea/src/pathfinder/custom_state.rs')
-rw-r--r--azalea/src/pathfinder/custom_state.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/azalea/src/pathfinder/custom_state.rs b/azalea/src/pathfinder/custom_state.rs
new file mode 100644
index 00000000..f5129516
--- /dev/null
+++ b/azalea/src/pathfinder/custom_state.rs
@@ -0,0 +1,46 @@
+use std::{
+ any::{Any, TypeId},
+ sync::Arc,
+};
+
+use bevy_ecs::component::Component;
+use parking_lot::RwLock;
+use rustc_hash::FxHashMap;
+
+/// The component that holds the custom pathfinder state for one of our bots.
+///
+/// See [`CustomPathfinderStateRef`] for more information about the inner type.
+///
+/// Azalea won't automatically insert this component, so if you're trying to use
+/// it then you should also have logic to insert the component if it's not
+/// present.
+///
+/// Be aware that a read lock is held on the `RwLock` while a path is being
+/// calculated, which may take up to several seconds. For this reason, it may be
+/// favorable to use [`RwLock::try_write`] instead of [`RwLock::write`] when
+/// updating it to avoid blocking the current thread.
+#[derive(Clone, Component, Default)]
+pub struct CustomPathfinderState(pub Arc<RwLock<CustomPathfinderStateRef>>);
+
+/// Arbitrary state that's passed to the pathfinder, intended to be used for
+/// custom moves that need to access things that are usually inaccessible.
+///
+/// This is included in [`PathfinderCtx`].
+///
+/// [`PathfinderCtx`]: crate::pathfinder::PathfinderCtx
+#[derive(Debug, Default)]
+pub struct CustomPathfinderStateRef {
+ map: FxHashMap<TypeId, Box<dyn Any + Send + Sync>>,
+}
+
+impl CustomPathfinderStateRef {
+ pub fn insert<T: 'static + Send + Sync>(&mut self, t: T) {
+ self.map.insert(TypeId::of::<T>(), Box::new(t));
+ }
+
+ pub fn get<T: 'static + Send + Sync>(&self) -> Option<&T> {
+ self.map
+ .get(&TypeId::of::<T>())
+ .map(|value| value.downcast_ref().unwrap())
+ }
+}