Occasional path recalculate, stopped pathfinder thrashing

This commit is contained in:
Joshua Barretto 2020-01-22 14:24:11 +00:00
parent 2d9aa21eef
commit f14037e4a3
2 changed files with 20 additions and 32 deletions

View File

@ -83,6 +83,7 @@ impl Route {
#[derive(Default, Clone, Debug)]
pub struct Chaser {
last_search_tgt: Option<Vec3<f32>>,
route: Route,
}
@ -102,6 +103,11 @@ impl Chaser {
if end_to_tgt > pos_to_tgt * 0.3 + 5.0 {
None
} else {
if rand::random::<f32>() < 0.005 {
// TODO: Only re-calculate route when we're stuck
self.route = Route::default();
}
self.route.traverse(vol, pos)
}
} else {
@ -112,14 +118,20 @@ impl Chaser {
if let Some(bearing) = bearing {
Some(bearing)
} else {
let path: Path = WorldPath::find(vol, pos, tgt)
.ok()
.and_then(|wp| wp.path.map(|nodes| nodes.into_iter().rev()))
.into_iter()
.flatten()
.collect();
if self
.last_search_tgt
.map(|last_tgt| last_tgt.distance(tgt) > pos_to_tgt * 0.15 + 5.0)
.unwrap_or(true)
{
let path: Path = WorldPath::find(vol, pos, tgt)
.ok()
.and_then(|wp| wp.path.map(|nodes| nodes.into_iter().rev()))
.into_iter()
.flatten()
.collect();
self.route = path.into();
self.route = path.into();
}
Some(tgt - pos)
}

View File

@ -105,32 +105,8 @@ impl<'a> System<'a> for Sys {
if let Some(tgt_pos) = positions.get(*target) {
if let Some(bearing) = chaser.chase(&*terrain, tgt_pos.0, pos.0) {
inputs.move_dir = Vec2::from(bearing).normalized();
inputs.jump.set_state(bearing.z > 0.9);
inputs.jump.set_state(bearing.z > 1.0);
}
/*
const HAPPY_DIST: i32 = 4;
let plot_path = if let Some(dir) = route.traverse(&*terrain, pos.0) {
inputs.move_dir = Vec2::from(dir).normalized();
inputs.jump.set_state(dir.z > 0.9);
// Sometimes recalculate to avoid getting stuck
rand::random::<f32>() < 0.005
} else {
true
};
if plot_path {
let path: Path = WorldPath::find(&*terrain, pos.0, tgt_pos.0)
.ok()
.and_then(|wp| wp.path.map(|nodes| nodes.into_iter().rev()))
.into_iter()
.flatten()
.collect();
*route = path.into();
}
*/
} else {
inputs.move_dir = Vec2::zero();
}