preallocate edges vec in pathfinder

This commit is contained in:
mat 2023-10-04 20:35:01 -05:00
parent 17734cdcbf
commit 9db542f342
4 changed files with 36 additions and 48 deletions

View file

@ -179,7 +179,11 @@ fn goto_listener(
debug!("start: {start:?}");
let ctx = PathfinderCtx::new(world_lock);
let successors = |pos: BlockPos| successors_fn(&ctx, pos);
let successors = |pos: BlockPos| {
let mut edges = Vec::with_capacity(16);
successors_fn(&mut edges, &ctx, pos);
edges
};
let mut attempt_number = 0;
@ -281,9 +285,13 @@ fn path_found_listener(
let world_lock = instance_container.get(instance_name).expect(
"Entity tried to pathfind but the entity isn't in a valid world",
);
let ctx = PathfinderCtx::new(world_lock);
let successors_fn: moves::SuccessorsFn = event.successors_fn;
let successors = |pos: BlockPos| successors_fn(&ctx, pos);
let ctx = PathfinderCtx::new(world_lock);
let successors = |pos: BlockPos| {
let mut edges = Vec::with_capacity(16);
successors_fn(&mut edges, &ctx, pos);
edges
};
if successors(last_node.target)
.iter()
@ -444,7 +452,11 @@ fn tick_execute_path(
{
// obstruction check (the path we're executing isn't possible anymore)
let ctx = PathfinderCtx::new(world_lock);
let successors = |pos: BlockPos| successors_fn(&ctx, pos);
let successors = |pos: BlockPos| {
let mut edges = Vec::with_capacity(16);
successors_fn(&mut edges, &ctx, pos);
edges
};
if let Some(last_reached_node) = pathfinder.last_reached_node {
if let Some(obstructed_index) =

View file

@ -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 {

View file

@ -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

View file

@ -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(