diff options
| author | mat <git@matdoes.dev> | 2024-12-25 03:00:48 +0000 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2024-12-25 03:00:48 +0000 |
| commit | 0ee9ed50e30222784d094e20302cadc879f2b6db (patch) | |
| tree | f5d730bb298c83e30f67d748d1c5e69d602c1200 /azalea/src/pathfinder/moves | |
| parent | d67aa07c13c335b135080efc59f82df69aa34a95 (diff) | |
| download | azalea-drasl-0ee9ed50e30222784d094e20302cadc879f2b6db.tar.xz | |
optimize pathfinder
Diffstat (limited to 'azalea/src/pathfinder/moves')
| -rw-r--r-- | azalea/src/pathfinder/moves/basic.rs | 32 | ||||
| -rw-r--r-- | azalea/src/pathfinder/moves/mod.rs | 7 | ||||
| -rw-r--r-- | azalea/src/pathfinder/moves/parkour.rs | 28 |
3 files changed, 34 insertions, 33 deletions
diff --git a/azalea/src/pathfinder/moves/basic.rs b/azalea/src/pathfinder/moves/basic.rs index 89ba9acc..8a679376 100644 --- a/azalea/src/pathfinder/moves/basic.rs +++ b/azalea/src/pathfinder/moves/basic.rs @@ -7,9 +7,9 @@ use azalea_core::{ }; use super::{default_is_reached, Edge, ExecuteCtx, IsReachedCtx, MoveData, PathfinderCtx}; -use crate::pathfinder::{astar, costs::*}; +use crate::pathfinder::{astar, costs::*, rel_block_pos::RelBlockPos}; -pub fn basic_move(ctx: &mut PathfinderCtx, node: BlockPos) { +pub fn basic_move(ctx: &mut PathfinderCtx, node: RelBlockPos) { forward_move(ctx, node); ascend_move(ctx, node); descend_move(ctx, node); @@ -18,9 +18,9 @@ pub fn basic_move(ctx: &mut PathfinderCtx, node: BlockPos) { downward_move(ctx, node); } -fn forward_move(ctx: &mut PathfinderCtx, pos: BlockPos) { +fn forward_move(ctx: &mut PathfinderCtx, pos: RelBlockPos) { for dir in CardinalDirection::iter() { - let offset = BlockPos::new(dir.x(), 0, dir.z()); + let offset = RelBlockPos::new(dir.x(), 0, dir.z()); let mut cost = SPRINT_ONE_BLOCK_COST; @@ -57,9 +57,9 @@ fn execute_forward_move(mut ctx: ExecuteCtx) { ctx.sprint(SprintDirection::Forward); } -fn ascend_move(ctx: &mut PathfinderCtx, pos: BlockPos) { +fn ascend_move(ctx: &mut PathfinderCtx, pos: RelBlockPos) { for dir in CardinalDirection::iter() { - let offset = BlockPos::new(dir.x(), 1, dir.z()); + let offset = RelBlockPos::new(dir.x(), 1, dir.z()); let break_cost_1 = ctx .world @@ -126,7 +126,7 @@ fn execute_ascend_move(mut ctx: ExecuteCtx) { + x_axis as f64 * (target_center.z - position.z).abs(); let lateral_motion = x_axis as f64 * physics.velocity.z + z_axis as f64 * physics.velocity.x; - if lateral_motion > 0.1 { + if lateral_motion.abs() > 0.1 { return; } @@ -147,9 +147,9 @@ pub fn ascend_is_reached( BlockPos::from(position) == target || BlockPos::from(position) == target.down(1) } -fn descend_move(ctx: &mut PathfinderCtx, pos: BlockPos) { +fn descend_move(ctx: &mut PathfinderCtx, pos: RelBlockPos) { for dir in CardinalDirection::iter() { - let dir_delta = BlockPos::new(dir.x(), 0, dir.z()); + let dir_delta = RelBlockPos::new(dir.x(), 0, dir.z()); let new_horizontal_position = pos + dir_delta; let break_cost_1 = ctx @@ -271,9 +271,9 @@ pub fn descend_is_reached( && (position.y - target.y as f64) < 0.5 } -fn descend_forward_1_move(ctx: &mut PathfinderCtx, pos: BlockPos) { +fn descend_forward_1_move(ctx: &mut PathfinderCtx, pos: RelBlockPos) { for dir in CardinalDirection::iter() { - let dir_delta = BlockPos::new(dir.x(), 0, dir.z()); + let dir_delta = RelBlockPos::new(dir.x(), 0, dir.z()); let gap_horizontal_position = pos + dir_delta; let new_horizontal_position = pos + dir_delta * 2; @@ -323,12 +323,12 @@ fn descend_forward_1_move(ctx: &mut PathfinderCtx, pos: BlockPos) { } } -fn diagonal_move(ctx: &mut PathfinderCtx, pos: BlockPos) { +fn diagonal_move(ctx: &mut PathfinderCtx, pos: RelBlockPos) { for dir in CardinalDirection::iter() { let right = dir.right(); - let offset = BlockPos::new(dir.x() + right.x(), 0, dir.z() + right.z()); - let left_pos = BlockPos::new(pos.x + dir.x(), pos.y, pos.z + dir.z()); - let right_pos = BlockPos::new(pos.x + right.x(), pos.y, pos.z + right.z()); + let offset = RelBlockPos::new(dir.x() + right.x(), 0, dir.z() + right.z()); + let left_pos = RelBlockPos::new(pos.x + dir.x(), pos.y, pos.z + dir.z()); + let right_pos = RelBlockPos::new(pos.x + right.x(), pos.y, pos.z + right.z()); // +0.001 so it doesn't unnecessarily go diagonal sometimes let mut cost = SPRINT_ONE_BLOCK_COST * SQRT_2 + 0.001; @@ -369,7 +369,7 @@ fn execute_diagonal_move(mut ctx: ExecuteCtx) { } /// Go directly down, usually by mining. -fn downward_move(ctx: &mut PathfinderCtx, pos: BlockPos) { +fn downward_move(ctx: &mut PathfinderCtx, pos: RelBlockPos) { // make sure we land on a solid block after breaking the one below us if !ctx.world.is_block_solid(pos.down(2)) { return; diff --git a/azalea/src/pathfinder/moves/mod.rs b/azalea/src/pathfinder/moves/mod.rs index 1a435b5f..68c65d5d 100644 --- a/azalea/src/pathfinder/moves/mod.rs +++ b/azalea/src/pathfinder/moves/mod.rs @@ -16,15 +16,16 @@ use parking_lot::RwLock; use super::{ astar, mining::MiningCache, + rel_block_pos::RelBlockPos, world::{is_block_state_passable, CachedWorld}, }; use crate::{auto_tool::best_tool_in_hotbar_for_block, JumpEvent, LookAtEvent}; -type Edge = astar::Edge<BlockPos, MoveData>; +type Edge = astar::Edge<RelBlockPos, MoveData>; -pub type SuccessorsFn = fn(&mut PathfinderCtx, BlockPos); +pub type SuccessorsFn = fn(&mut PathfinderCtx, RelBlockPos); -pub fn default_move(ctx: &mut PathfinderCtx, node: BlockPos) { +pub fn default_move(ctx: &mut PathfinderCtx, node: RelBlockPos) { basic::basic_move(ctx, node); parkour::parkour_move(ctx, node); } diff --git a/azalea/src/pathfinder/moves/parkour.rs b/azalea/src/pathfinder/moves/parkour.rs index 1d35f323..1816a5e1 100644 --- a/azalea/src/pathfinder/moves/parkour.rs +++ b/azalea/src/pathfinder/moves/parkour.rs @@ -3,18 +3,18 @@ use azalea_core::{direction::CardinalDirection, position::BlockPos}; use tracing::trace; use super::{Edge, ExecuteCtx, IsReachedCtx, MoveData, PathfinderCtx}; -use crate::pathfinder::{astar, costs::*}; +use crate::pathfinder::{astar, costs::*, rel_block_pos::RelBlockPos}; -pub fn parkour_move(ctx: &mut PathfinderCtx, node: BlockPos) { +pub fn parkour_move(ctx: &mut PathfinderCtx, node: RelBlockPos) { parkour_forward_1_move(ctx, node); parkour_forward_2_move(ctx, node); parkour_forward_3_move(ctx, node); } -fn parkour_forward_1_move(ctx: &mut PathfinderCtx, pos: BlockPos) { +fn parkour_forward_1_move(ctx: &mut PathfinderCtx, pos: RelBlockPos) { for dir in CardinalDirection::iter() { - let gap_offset = BlockPos::new(dir.x(), 0, dir.z()); - let offset = BlockPos::new(dir.x() * 2, 0, dir.z() * 2); + let gap_offset = RelBlockPos::new(dir.x(), 0, dir.z()); + let offset = RelBlockPos::new(dir.x() * 2, 0, dir.z() * 2); // make sure we actually have to jump if ctx.world.is_block_solid((pos + gap_offset).down(1)) { @@ -63,11 +63,11 @@ fn parkour_forward_1_move(ctx: &mut PathfinderCtx, pos: BlockPos) { } } -fn parkour_forward_2_move(ctx: &mut PathfinderCtx, pos: BlockPos) { +fn parkour_forward_2_move(ctx: &mut PathfinderCtx, pos: RelBlockPos) { 'dir: for dir in CardinalDirection::iter() { - let gap_1_offset = BlockPos::new(dir.x(), 0, dir.z()); - let gap_2_offset = BlockPos::new(dir.x() * 2, 0, dir.z() * 2); - let offset = BlockPos::new(dir.x() * 3, 0, dir.z() * 3); + let gap_1_offset = RelBlockPos::new(dir.x(), 0, dir.z()); + let gap_2_offset = RelBlockPos::new(dir.x() * 2, 0, dir.z() * 2); + let offset = RelBlockPos::new(dir.x() * 3, 0, dir.z() * 3); // make sure we actually have to jump if ctx.world.is_block_solid((pos + gap_1_offset).down(1)) @@ -117,12 +117,12 @@ fn parkour_forward_2_move(ctx: &mut PathfinderCtx, pos: BlockPos) { } } -fn parkour_forward_3_move(ctx: &mut PathfinderCtx, pos: BlockPos) { +fn parkour_forward_3_move(ctx: &mut PathfinderCtx, pos: RelBlockPos) { 'dir: for dir in CardinalDirection::iter() { - let gap_1_offset = BlockPos::new(dir.x(), 0, dir.z()); - let gap_2_offset = BlockPos::new(dir.x() * 2, 0, dir.z() * 2); - let gap_3_offset = BlockPos::new(dir.x() * 3, 0, dir.z() * 3); - let offset = BlockPos::new(dir.x() * 4, 0, dir.z() * 4); + let gap_1_offset = RelBlockPos::new(dir.x(), 0, dir.z()); + let gap_2_offset = RelBlockPos::new(dir.x() * 2, 0, dir.z() * 2); + let gap_3_offset = RelBlockPos::new(dir.x() * 3, 0, dir.z() * 3); + let offset = RelBlockPos::new(dir.x() * 4, 0, dir.z() * 4); // make sure we actually have to jump if ctx.world.is_block_solid((pos + gap_1_offset).down(1)) |
