mirror of
https://github.com/mat-1/azalea.git
synced 2024-09-19 22:52:32 +00:00
add more pathfinder goals
This commit is contained in:
parent
3f62ff1113
commit
f61a6d1633
1 changed files with 80 additions and 1 deletions
|
@ -1,4 +1,4 @@
|
||||||
use azalea_core::BlockPos;
|
use azalea_core::{BlockPos, Vec3};
|
||||||
|
|
||||||
use super::Goal;
|
use super::Goal;
|
||||||
|
|
||||||
|
@ -14,3 +14,82 @@ impl Goal for BlockPosGoal {
|
||||||
n == self.0
|
n == self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct XZGoal {
|
||||||
|
pub x: i32,
|
||||||
|
pub z: i32,
|
||||||
|
}
|
||||||
|
impl Goal for XZGoal {
|
||||||
|
fn heuristic(&self, n: BlockPos) -> f32 {
|
||||||
|
let dx = (self.x - n.x) as f32;
|
||||||
|
let dz = (self.z - n.z) as f32;
|
||||||
|
dx * dx + dz * dz
|
||||||
|
}
|
||||||
|
fn success(&self, n: BlockPos) -> bool {
|
||||||
|
n.x == self.x && n.z == self.z
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct YGoal {
|
||||||
|
pub y: i32,
|
||||||
|
}
|
||||||
|
impl Goal for YGoal {
|
||||||
|
fn heuristic(&self, n: BlockPos) -> f32 {
|
||||||
|
let dy = (self.y - n.y) as f32;
|
||||||
|
dy * dy
|
||||||
|
}
|
||||||
|
fn success(&self, n: BlockPos) -> bool {
|
||||||
|
n.y == self.y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct RadiusGoal {
|
||||||
|
pub pos: Vec3,
|
||||||
|
pub radius: f32,
|
||||||
|
}
|
||||||
|
impl Goal for RadiusGoal {
|
||||||
|
fn heuristic(&self, n: BlockPos) -> f32 {
|
||||||
|
let n = n.center();
|
||||||
|
let dx = (self.pos.x - n.x) as f32;
|
||||||
|
let dy = (self.pos.y - n.y) as f32;
|
||||||
|
let dz = (self.pos.z - n.z) as f32;
|
||||||
|
dx * dx + dy * dy + dz * dz
|
||||||
|
}
|
||||||
|
fn success(&self, n: BlockPos) -> bool {
|
||||||
|
let n = n.center();
|
||||||
|
let dx = (self.pos.x - n.x) as f32;
|
||||||
|
let dy = (self.pos.y - n.y) as f32;
|
||||||
|
let dz = (self.pos.z - n.z) as f32;
|
||||||
|
dx * dx + dy * dy + dz * dz <= self.radius * self.radius
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct InverseGoal<T: Goal>(pub T);
|
||||||
|
impl<T: Goal> Goal for InverseGoal<T> {
|
||||||
|
fn heuristic(&self, n: BlockPos) -> f32 {
|
||||||
|
-self.0.heuristic(n)
|
||||||
|
}
|
||||||
|
fn success(&self, n: BlockPos) -> bool {
|
||||||
|
!self.0.success(n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct OrGoal<T: Goal, U: Goal>(pub T, pub U);
|
||||||
|
impl<T: Goal, U: Goal> Goal for OrGoal<T, U> {
|
||||||
|
fn heuristic(&self, n: BlockPos) -> f32 {
|
||||||
|
self.0.heuristic(n).min(self.1.heuristic(n))
|
||||||
|
}
|
||||||
|
fn success(&self, n: BlockPos) -> bool {
|
||||||
|
self.0.success(n) || self.1.success(n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct AndGoal<T: Goal, U: Goal>(pub T, pub U);
|
||||||
|
impl<T: Goal, U: Goal> Goal for AndGoal<T, U> {
|
||||||
|
fn heuristic(&self, n: BlockPos) -> f32 {
|
||||||
|
self.0.heuristic(n).max(self.1.heuristic(n))
|
||||||
|
}
|
||||||
|
fn success(&self, n: BlockPos) -> bool {
|
||||||
|
self.0.success(n) && self.1.success(n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue