aboutsummaryrefslogtreecommitdiff
path: root/azalea/src/pathfinder/moves
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2023-10-04 20:35:01 -0500
committermat <git@matdoes.dev>2023-10-04 20:35:01 -0500
commit9db542f342c40478af230fd65b0bcf8cc069b5be (patch)
tree5451f1eb48d31151edb89519a160909ae1ee74bb /azalea/src/pathfinder/moves
parent17734cdcbf8ea30ded09a0b14372d92c11d1cc8c (diff)
downloadazalea-drasl-9db542f342c40478af230fd65b0bcf8cc069b5be.tar.xz
preallocate edges vec in pathfinder
Diffstat (limited to 'azalea/src/pathfinder/moves')
-rw-r--r--azalea/src/pathfinder/moves/basic.rs29
-rw-r--r--azalea/src/pathfinder/moves/mod.rs10
-rw-r--r--azalea/src/pathfinder/moves/parkour.rs25
3 files changed, 20 insertions, 44 deletions
diff --git a/azalea/src/pathfinder/moves/basic.rs b/azalea/src/pathfinder/moves/basic.rs
index 013b3c19..ccf4ab79 100644
--- a/azalea/src/pathfinder/moves/basic.rs
+++ b/azalea/src/pathfinder/moves/basic.rs
@@ -13,17 +13,14 @@ use crate::{
use super::{default_is_reached, Edge, ExecuteCtx, IsReachedCtx, MoveData, PathfinderCtx};
-pub fn basic_move(ctx: &PathfinderCtx, node: BlockPos) -> Vec<Edge> {
- let mut edges = Vec::with_capacity(8);
- edges.extend(forward_move(ctx, node));
- edges.extend(ascend_move(ctx, node));
- edges.extend(descend_move(ctx, node));
- edges.extend(diagonal_move(ctx, node));
- edges
+pub fn basic_move(edges: &mut Vec<Edge>, ctx: &PathfinderCtx, node: BlockPos) {
+ forward_move(edges, ctx, node);
+ ascend_move(edges, ctx, node);
+ descend_move(edges, ctx, node);
+ diagonal_move(edges, ctx, node);
}
-fn forward_move(ctx: &PathfinderCtx, pos: BlockPos) -> Vec<Edge> {
- let mut edges = Vec::new();
+fn forward_move(edges: &mut Vec<Edge>, ctx: &PathfinderCtx, pos: BlockPos) {
for dir in CardinalDirection::iter() {
let offset = BlockPos::new(dir.x(), 0, dir.z());
@@ -44,8 +41,6 @@ fn forward_move(ctx: &PathfinderCtx, pos: BlockPos) -> Vec<Edge> {
cost,
})
}
-
- edges
}
fn execute_forward_move(
@@ -68,8 +63,7 @@ fn execute_forward_move(
});
}
-fn ascend_move(ctx: &PathfinderCtx, pos: BlockPos) -> Vec<Edge> {
- let mut edges = Vec::new();
+fn ascend_move(edges: &mut Vec<Edge>, ctx: &PathfinderCtx, pos: BlockPos) {
for dir in CardinalDirection::iter() {
let offset = BlockPos::new(dir.x(), 1, dir.z());
@@ -93,7 +87,6 @@ fn ascend_move(ctx: &PathfinderCtx, pos: BlockPos) -> Vec<Edge> {
cost,
})
}
- edges
}
fn execute_ascend_move(
ExecuteCtx {
@@ -152,8 +145,7 @@ pub fn ascend_is_reached(
BlockPos::from(position) == target || BlockPos::from(position) == target.down(1)
}
-fn descend_move(ctx: &PathfinderCtx, pos: BlockPos) -> Vec<Edge> {
- let mut edges = Vec::new();
+fn descend_move(edges: &mut Vec<Edge>, ctx: &PathfinderCtx, pos: BlockPos) {
for dir in CardinalDirection::iter() {
let dir_delta = BlockPos::new(dir.x(), 0, dir.z());
let new_horizontal_position = pos + dir_delta;
@@ -187,7 +179,6 @@ fn descend_move(ctx: &PathfinderCtx, pos: BlockPos) -> Vec<Edge> {
cost,
})
}
- edges
}
fn execute_descend_move(
ExecuteCtx {
@@ -260,8 +251,7 @@ pub fn descend_is_reached(
&& (position.y - target.y as f64) < 0.5
}
-fn diagonal_move(ctx: &PathfinderCtx, pos: BlockPos) -> Vec<Edge> {
- let mut edges = Vec::new();
+fn diagonal_move(edges: &mut Vec<Edge>, ctx: &PathfinderCtx, pos: BlockPos) {
for dir in CardinalDirection::iter() {
let right = dir.right();
let offset = BlockPos::new(dir.x() + right.x(), 0, dir.z() + right.z());
@@ -292,7 +282,6 @@ fn diagonal_move(ctx: &PathfinderCtx, pos: BlockPos) -> Vec<Edge> {
cost,
})
}
- edges
}
fn execute_diagonal_move(
ExecuteCtx {
diff --git a/azalea/src/pathfinder/moves/mod.rs b/azalea/src/pathfinder/moves/mod.rs
index 90109346..81a184aa 100644
--- a/azalea/src/pathfinder/moves/mod.rs
+++ b/azalea/src/pathfinder/moves/mod.rs
@@ -23,7 +23,7 @@ use parking_lot::RwLock;
type Edge = astar::Edge<BlockPos, MoveData>;
-pub type SuccessorsFn = fn(&PathfinderCtx, BlockPos) -> Vec<astar::Edge<BlockPos, MoveData>>;
+pub type SuccessorsFn = fn(&mut Vec<Edge>, &PathfinderCtx, BlockPos);
#[derive(Clone)]
pub struct MoveData {
@@ -272,11 +272,9 @@ pub struct IsReachedCtx<'a> {
pub physics: &'a azalea_entity::Physics,
}
-pub fn default_move(ctx: &PathfinderCtx, node: BlockPos) -> Vec<Edge> {
- let mut edges = Vec::new();
- edges.extend(basic::basic_move(ctx, node));
- edges.extend(parkour::parkour_move(ctx, node));
- edges
+pub fn default_move(edges: &mut Vec<Edge>, ctx: &PathfinderCtx, node: BlockPos) {
+ basic::basic_move(edges, ctx, node);
+ parkour::parkour_move(edges, ctx, node);
}
/// Returns whether the entity is at the node and should start going to the
diff --git a/azalea/src/pathfinder/moves/parkour.rs b/azalea/src/pathfinder/moves/parkour.rs
index d14da735..fc03dc48 100644
--- a/azalea/src/pathfinder/moves/parkour.rs
+++ b/azalea/src/pathfinder/moves/parkour.rs
@@ -8,16 +8,13 @@ use crate::{
use super::{default_is_reached, Edge, ExecuteCtx, IsReachedCtx, MoveData, PathfinderCtx};
-pub fn parkour_move(ctx: &PathfinderCtx, node: BlockPos) -> Vec<Edge> {
- let mut edges = Vec::new();
- edges.extend(parkour_forward_1_move(ctx, node));
- edges.extend(parkour_headhitter_forward_1_move(ctx, node));
- edges.extend(parkour_forward_2_move(ctx, node));
- edges
+pub fn parkour_move(edges: &mut Vec<Edge>, ctx: &PathfinderCtx, node: BlockPos) {
+ parkour_forward_1_move(edges, ctx, node);
+ parkour_headhitter_forward_1_move(edges, ctx, node);
+ parkour_forward_2_move(edges, ctx, node);
}
-fn parkour_forward_1_move(ctx: &PathfinderCtx, pos: BlockPos) -> Vec<Edge> {
- let mut edges = Vec::new();
+fn parkour_forward_1_move(edges: &mut Vec<Edge>, ctx: &PathfinderCtx, pos: BlockPos) {
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);
@@ -53,12 +50,9 @@ fn parkour_forward_1_move(ctx: &PathfinderCtx, pos: BlockPos) -> Vec<Edge> {
cost,
})
}
-
- edges
}
-fn parkour_forward_2_move(ctx: &PathfinderCtx, pos: BlockPos) -> Vec<Edge> {
- let mut edges = Vec::new();
+fn parkour_forward_2_move(edges: &mut Vec<Edge>, ctx: &PathfinderCtx, pos: BlockPos) {
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);
@@ -104,12 +98,9 @@ fn parkour_forward_2_move(ctx: &PathfinderCtx, pos: BlockPos) -> Vec<Edge> {
cost,
})
}
-
- edges
}
-fn parkour_headhitter_forward_1_move(ctx: &PathfinderCtx, pos: BlockPos) -> Vec<Edge> {
- let mut edges = Vec::new();
+fn parkour_headhitter_forward_1_move(edges: &mut Vec<Edge>, ctx: &PathfinderCtx, pos: BlockPos) {
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);
@@ -145,8 +136,6 @@ fn parkour_headhitter_forward_1_move(ctx: &PathfinderCtx, pos: BlockPos) -> Vec<
cost,
})
}
-
- edges
}
fn execute_parkour_move(