Fix NPC navigation near obstacles

Unwalkable obstacles, such as walls or open space can affect path-finding.
We need to use precise calculation to avoid NPC being stuck in corser and
near sharp turns.
This commit is contained in:
Dmitry Kashitsyn 2024-04-09 14:16:15 +05:00
parent 529bcd7119
commit c8f39e3beb
No known key found for this signature in database
GPG Key ID: 6C747B56FC5F1ABA

@ -131,7 +131,8 @@ impl Route {
return None;
}
let be_precise = DIAGONALS.iter().any(|pos| {
// If, in any direction, there is a column of open air of several blocks
let open_space_nearby = DIAGONALS.iter().any(|pos| {
(-1..2).all(|z| {
vol.get(next0 + Vec3::new(pos.x, pos.y, z))
.map(|b| !b.is_solid())
@ -139,6 +140,18 @@ impl Route {
})
});
// If, in any direction, there is a solid wall
let wall_nearby = DIAGONALS.iter().any(|pos| {
(0..2).all(|z| {
vol.get(next0 + Vec3::new(pos.x, pos.y, z))
.map(|b| b.is_solid())
.unwrap_or(true)
})
});
// Unwalkable obstacles, such as walls or open space can affect path-finding
let be_precise = open_space_nearby | wall_nearby;
// Map position of node to middle of block
let next_tgt = next0.map(|e| e as f32) + Vec3::new(0.5, 0.5, 0.0);
let closest_tgt = next_tgt.map2(pos, |tgt, pos| pos.clamped(tgt.floor(), tgt.ceil()));