fix: Pathfinding propagation misuse

This commit is contained in:
demenik
2025-11-24 15:14:16 +01:00
parent d525271b51
commit ff7cd5698b

View File

@@ -27,7 +27,7 @@ impl PartialOrd for Node {
} }
pub fn manhattan_distance(x1: u32, y1: u32, x2: u32, y2: u32) -> u32 { pub fn manhattan_distance(x1: u32, y1: u32, x2: u32, y2: u32) -> u32 {
(x1 as i32 - x2 as i32).abs() as u32 + (y1 as i32 - y2 as i32).abs() as u32 x1.abs_diff(x2) + y1.abs_diff(y2)
} }
pub fn find_path( pub fn find_path(
@@ -37,8 +37,9 @@ pub fn find_path(
tile_query: &Query<&TileState>, tile_query: &Query<&TileState>,
) -> Option<VecDeque<(u32, u32)>> { ) -> Option<VecDeque<(u32, u32)>> {
let target_entity = grid.get_tile(end).ok()?; let target_entity = grid.get_tile(end).ok()?;
let state = tile_query.get(target_entity).unwrap(); let target_state = tile_query.get(target_entity).ok()?;
if (*state).is_blocking() {
if target_state.is_blocking() {
return None; return None;
} }
@@ -68,6 +69,12 @@ pub fn find_path(
return Some(path); return Some(path);
} }
if let Some(&best_g) = g_score.get(&current_pos) {
if current.cost > best_g {
continue;
}
}
let neighbors = [ let neighbors = [
(current.x as i32 + 1, current.y as i32), (current.x as i32 + 1, current.y as i32),
(current.x as i32 - 1, current.y as i32), (current.x as i32 - 1, current.y as i32),
@@ -81,9 +88,12 @@ pub fn find_path(
} }
let next_pos = (nx as u32, ny as u32); let next_pos = (nx as u32, ny as u32);
let tile_entity = grid.get_tile(next_pos).ok()?; let tile_entity = match grid.get_tile(next_pos) {
let state = tile_query.get(tile_entity).unwrap(); Ok(e) => e,
if (*state).is_blocking() { Err(_) => continue,
};
let state = tile_query.get(tile_entity).ok()?;
if state.is_blocking() {
continue; continue;
} }
@@ -91,6 +101,7 @@ pub fn find_path(
if tentative_g_score < *g_score.get(&next_pos).unwrap_or(&u32::MAX) { if tentative_g_score < *g_score.get(&next_pos).unwrap_or(&u32::MAX) {
came_from.insert(next_pos, current_pos); came_from.insert(next_pos, current_pos);
g_score.insert(next_pos, tentative_g_score); g_score.insert(next_pos, tentative_g_score);
open_set.push(Node { open_set.push(Node {
x: next_pos.0, x: next_pos.0,
y: next_pos.1, y: next_pos.1,