Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
A* now checks at most 100 squares when running the version with no zo…
Browse files Browse the repository at this point in the history
…ne hints (#58)
  • Loading branch information
porkbrain authored Feb 23, 2024
1 parent 4347b15 commit 0541f3b
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions common/top_down/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,6 @@ where

/// Path finding algorithm that returns partial path to the target.
///
/// TODO: limit the number of iterations of the A* algorithm to prevent FPS
/// drops.
///
/// It's good when both squares are in some zone group as we can find
/// minimum spanning tree between zones in the same group.
pub fn find_partial_path(
Expand All @@ -554,6 +551,8 @@ where
return Some(vec![]);
}

trace!("find_partial_path {from} -> {to}");

// 3 possible situations:
//
// a) current and target are in the same zone group
Expand Down Expand Up @@ -649,13 +648,23 @@ where
}

/// The default success cond is max iterations or reaching the target.
// TODO: cap *A iterations
fn partial_astar(
&self,
who: Entity,
from: Square,
to: Square,
) -> Option<Vec<Square>> {
/// Every time the search explores successors of a square, it increments
/// an iteration counter.
/// If the counter grows over this limit, the next square with better
/// distance to the target than found so far is returned.
const MAX_PARTIAL_ASTAR_EXPLORED_SQUARES: usize = 100;

// see MAX_PARTIAL_ASTAR_EXPLORED_SQUARES
let mut explored_squares = 0;
// the best distance found so far with Manhattan distance
let mut shortest_distance = i32::MAX;

pathfinding::prelude::astar(
&from,
// successors
Expand All @@ -668,7 +677,17 @@ where
// heuristic
|square: &Square| square.manhattan_distance(to),
// success
|square| square == &to,
|square| {
if explored_squares < MAX_PARTIAL_ASTAR_EXPLORED_SQUARES {
explored_squares += 1;
shortest_distance =
shortest_distance.min(square.manhattan_distance(to));

square == &to
} else {
square.manhattan_distance(to) <= shortest_distance
}
},
)
.map(|(path, _)| path)
}
Expand Down

0 comments on commit 0541f3b

Please sign in to comment.