use alloc::vec::Vec;
use search_core::{Budget, Heuristic};
use crate::best_first::a_star_multi;
use crate::grid::{
GridCell, GridStep, Octile, OctileDistance, UniformGrid, can_move, is_open, offset, passable,
};
use crate::outcome::{Outcome, SearchResult, Solution, Stats};
use crate::problem::Successors;
pub fn jps<G: UniformGrid>(
grid: &G,
start: GridCell,
goal: GridCell,
budget: impl Budget<Stats>,
) -> SearchResult<GridCell, GridStep, Octile> {
jps_multi(grid, [start], goal, budget)
}
pub fn jps_multi<G: UniformGrid>(
grid: &G,
starts: impl IntoIterator<Item = GridCell>,
goal: GridCell,
budget: impl Budget<Stats>,
) -> SearchResult<GridCell, GridStep, Octile> {
let graph = JumpGraph { grid, goal };
let starts = starts
.into_iter()
.filter(|&cell| is_open(grid, cell))
.map(|cell| (cell, (0, 0)));
let mut distance = OctileDistance(goal);
let heuristic =
crate::heuristic_fn(|&(cell, _): &(GridCell, GridStep)| distance.estimate(&cell));
let result = a_star_multi(
&graph,
starts,
&|&(cell, _): &(GridCell, GridStep)| cell == goal,
heuristic,
budget,
);
let outcome = match result.outcome {
Outcome::Found(solution) => Outcome::Found(Solution {
states: solution
.states
.into_iter()
.map(|(cell, _)| cell)
.collect::<Vec<_>>(),
actions: solution.actions,
cost: solution.cost,
}),
Outcome::Unsolvable => Outcome::Unsolvable,
Outcome::LimitReached => Outcome::LimitReached,
Outcome::Cutoff => Outcome::Cutoff,
};
SearchResult {
outcome,
stats: result.stats,
}
}
struct JumpGraph<'a, G> {
grid: &'a G,
goal: GridCell,
}
impl<G: UniformGrid> Successors for JumpGraph<'_, G> {
type State = (GridCell, GridStep);
type Action = GridStep;
type Cost = Octile;
fn successors(
&self,
&(cell, arrival): &(GridCell, GridStep),
mut emit: impl FnMut(GridStep, (GridCell, GridStep), Octile),
) {
for step in self.pruned_steps(cell, arrival) {
if let Some((point, moves)) = self.jump(cell, step) {
let cost = if step.0 == 0 || step.1 == 0 {
Octile::new(moves, 0)
} else {
Octile::new(0, moves)
};
emit(step, (point, step), cost);
}
}
}
}
impl<G: UniformGrid> JumpGraph<'_, G> {
fn pruned_steps(&self, cell: GridCell, (dx, dy): GridStep) -> impl Iterator<Item = GridStep> {
let pad = (0, 0);
let (candidates, count) = match (dx, dy) {
(0, 0) => (crate::grid::STEPS, 8),
(_, 0) => (
[(dx, 0), (0, 1), (0, -1), (dx, 1), (dx, -1), pad, pad, pad],
5,
),
(0, _) => (
[(0, dy), (1, 0), (-1, 0), (1, dy), (-1, dy), pad, pad, pad],
5,
),
_ => ([(dx, 0), (0, dy), (dx, dy), pad, pad, pad, pad, pad], 3),
};
candidates
.into_iter()
.take(count)
.filter(move |&step| can_move(self.grid, cell, step))
}
fn jump(&self, cell: GridCell, step: GridStep) -> Option<(GridCell, u32)> {
if step.0 == 0 || step.1 == 0 {
self.jump_straight(cell, step)
} else {
self.jump_diagonal(cell, step)
}
}
fn jump_straight(&self, mut cell: GridCell, step: GridStep) -> Option<(GridCell, u32)> {
let mut moves = 0;
loop {
if !passable(self.grid, cell, step) {
return None;
}
cell = offset(self.grid, cell, step).expect("a passable cell is on the grid");
moves += 1;
if cell == self.goal || self.has_forced_neighbour(cell, step) {
return Some((cell, moves));
}
}
}
fn jump_diagonal(&self, mut cell: GridCell, step: GridStep) -> Option<(GridCell, u32)> {
let mut moves = 0;
loop {
if !can_move(self.grid, cell, step) {
return None;
}
cell = offset(self.grid, cell, step).expect("a passable cell is on the grid");
moves += 1;
if cell == self.goal
|| self.jump_straight(cell, (step.0, 0)).is_some()
|| self.jump_straight(cell, (0, step.1)).is_some()
{
return Some((cell, moves));
}
}
}
fn has_forced_neighbour(&self, cell: GridCell, (dx, dy): GridStep) -> bool {
let (side, back) = ((dy, dx), (-dx, -dy));
[1, -1].into_iter().any(|sign| {
let lateral = (side.0 * sign, side.1 * sign);
passable(self.grid, cell, lateral)
&& !passable(self.grid, cell, (lateral.0 + back.0, lateral.1 + back.1))
})
}
}