aboutsummaryrefslogtreecommitdiff
path: root/azalea/src/pathfinder/mod.rs
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2023-10-10 23:21:23 -0500
committermat <git@matdoes.dev>2023-10-10 23:21:23 -0500
commit9a687f0ffebad80441e036aabe14e7cf80c774d3 (patch)
tree2f6ad5a1a14cae23723550c934df395aab1733f6 /azalea/src/pathfinder/mod.rs
parent0297b8aacee27d9e86cea781b3751591e32df401 (diff)
downloadazalea-drasl-9a687f0ffebad80441e036aabe14e7cf80c774d3.tar.xz
start adding mining to pathfinder
Diffstat (limited to 'azalea/src/pathfinder/mod.rs')
-rw-r--r--azalea/src/pathfinder/mod.rs52
1 files changed, 41 insertions, 11 deletions
diff --git a/azalea/src/pathfinder/mod.rs b/azalea/src/pathfinder/mod.rs
index e92457b8..73ce2967 100644
--- a/azalea/src/pathfinder/mod.rs
+++ b/azalea/src/pathfinder/mod.rs
@@ -4,6 +4,7 @@
pub mod astar;
pub mod costs;
pub mod goals;
+pub mod mining;
pub mod moves;
pub mod simulation;
pub mod world;
@@ -23,6 +24,7 @@ use crate::ecs::{
use crate::pathfinder::moves::PathfinderCtx;
use crate::pathfinder::world::CachedWorld;
use azalea_client::chat::SendChatEvent;
+use azalea_client::inventory::{InventoryComponent, InventorySet};
use azalea_client::movement::walk_listener;
use azalea_client::{StartSprintEvent, StartWalkEvent};
use azalea_core::position::{BlockPos, Vec3};
@@ -45,6 +47,7 @@ use std::sync::atomic::{self, AtomicUsize};
use std::sync::Arc;
use std::time::{Duration, Instant};
+use self::mining::MiningCache;
use self::moves::{ExecuteCtx, IsReachedCtx, SuccessorsFn};
#[derive(Clone, Default)]
@@ -82,7 +85,8 @@ impl Plugin for PathfinderPlugin {
handle_stop_pathfinding_event,
)
.chain()
- .before(walk_listener),
+ .before(walk_listener)
+ .before(InventorySet),
);
}
}
@@ -116,7 +120,7 @@ pub struct GotoEvent {
/// `pathfinder::moves::default_move`
pub successors_fn: SuccessorsFn,
}
-#[derive(Event)]
+#[derive(Event, Clone)]
pub struct PathFoundEvent {
pub entity: Entity,
pub start: BlockPos,
@@ -175,13 +179,14 @@ fn goto_listener(
Option<&ExecutingPath>,
&Position,
&InstanceName,
+ &InventoryComponent,
)>,
instance_container: Res<InstanceContainer>,
) {
let thread_pool = AsyncComputeTaskPool::get();
for event in events.iter() {
- let (mut pathfinder, executing_path, position, instance_name) = query
+ let (mut pathfinder, executing_path, position, instance_name, inventory) = query
.get_mut(event.entity)
.expect("Called goto on an entity that's not in the world");
@@ -217,12 +222,15 @@ fn goto_listener(
let goto_id_atomic = pathfinder.goto_id.clone();
let goto_id = goto_id_atomic.fetch_add(1, atomic::Ordering::Relaxed) + 1;
+ let mining_cache = MiningCache::new(inventory.inventory_menu.clone());
let task = thread_pool.spawn(async move {
debug!("start: {start:?}");
let cached_world = CachedWorld::new(world_lock);
- let successors = |pos: BlockPos| call_successors_fn(&cached_world, successors_fn, pos);
+ let successors = |pos: BlockPos| {
+ call_successors_fn(&cached_world, &mining_cache, successors_fn, pos)
+ };
let mut attempt_number = 0;
@@ -311,12 +319,17 @@ fn handle_tasks(
// set the path for the target entity when we get the PathFoundEvent
fn path_found_listener(
mut events: EventReader<PathFoundEvent>,
- mut query: Query<(&mut Pathfinder, Option<&mut ExecutingPath>, &InstanceName)>,
+ mut query: Query<(
+ &mut Pathfinder,
+ Option<&mut ExecutingPath>,
+ &InstanceName,
+ &InventoryComponent,
+ )>,
instance_container: Res<InstanceContainer>,
mut commands: Commands,
) {
for event in events.iter() {
- let (mut pathfinder, executing_path, instance_name) = query
+ let (mut pathfinder, executing_path, instance_name, inventory) = query
.get_mut(event.entity)
.expect("Path found for an entity that doesn't have a pathfinder");
if let Some(path) = &event.path {
@@ -331,8 +344,10 @@ fn path_found_listener(
.expect("Entity tried to pathfind but the entity isn't in a valid world");
let successors_fn: moves::SuccessorsFn = event.successors_fn;
let cached_world = CachedWorld::new(world_lock);
- let successors =
- |pos: BlockPos| call_successors_fn(&cached_world, successors_fn, pos);
+ let mining_cache = MiningCache::new(inventory.inventory_menu.clone());
+ let successors = |pos: BlockPos| {
+ call_successors_fn(&cached_world, &mining_cache, successors_fn, pos)
+ };
if let Some(first_node_of_new_path) = path.front() {
if successors(last_node_of_current_path.target)
@@ -503,10 +518,15 @@ fn check_node_reached(
}
fn check_for_path_obstruction(
- mut query: Query<(&Pathfinder, &mut ExecutingPath, &InstanceName)>,
+ mut query: Query<(
+ &Pathfinder,
+ &mut ExecutingPath,
+ &InstanceName,
+ &InventoryComponent,
+ )>,
instance_container: Res<InstanceContainer>,
) {
- for (pathfinder, mut executing_path, instance_name) in &mut query {
+ for (pathfinder, mut executing_path, instance_name, inventory) in &mut query {
let Some(successors_fn) = pathfinder.successors_fn else {
continue;
};
@@ -517,7 +537,9 @@ fn check_for_path_obstruction(
// obstruction check (the path we're executing isn't possible anymore)
let cached_world = CachedWorld::new(world_lock);
- let successors = |pos: BlockPos| call_successors_fn(&cached_world, successors_fn, pos);
+ let mining_cache = MiningCache::new(inventory.inventory_menu.clone());
+ let successors =
+ |pos: BlockPos| call_successors_fn(&cached_world, &mining_cache, successors_fn, pos);
if let Some(obstructed_index) = check_path_obstructed(
executing_path.last_reached_node,
@@ -694,6 +716,11 @@ fn stop_pathfinding_on_instance_change(
/// permissions, and it'll make them spam *a lot* of commands.
///
/// ```
+/// # use azalea::prelude::*;
+/// # use azalea::pathfinder::PathfinderDebugParticles;
+/// # #[derive(Component, Clone, Default)]
+/// # pub struct State;
+///
/// async fn handle(mut bot: Client, event: azalea::Event, state: State) -> anyhow::Result<()> {
/// match event {
/// azalea::Event::Init => {
@@ -704,6 +731,7 @@ fn stop_pathfinding_on_instance_change(
/// }
/// _ => {}
/// }
+/// Ok(())
/// }
/// ```
#[derive(Component)]
@@ -809,6 +837,7 @@ where
pub fn call_successors_fn(
cached_world: &CachedWorld,
+ mining_cache: &MiningCache,
successors_fn: SuccessorsFn,
pos: BlockPos,
) -> Vec<astar::Edge<BlockPos, moves::MoveData>> {
@@ -816,6 +845,7 @@ pub fn call_successors_fn(
let mut ctx = PathfinderCtx {
edges: &mut edges,
world: cached_world,
+ mining_cache,
};
successors_fn(&mut ctx, pos);
edges