shortest_path_generic

Function shortest_path_generic 

Source
pub fn shortest_path_generic<T: DijkstraNode, P, G, N, I>(
    start: &[T],
    goal_fn: &P,
    cost_fn: G,
    neighbors: N,
    max_ops: u32,
    max_cost: u32,
) -> DijkstraSearchResults<T>
where P: Fn(T) -> bool, G: Fn(T) -> u32, N: Fn(T) -> I, I: IntoIterator<Item = T, IntoIter: Iterator<Item = T>>,
Expand description

Calculates a shortest path from start to goal using Dijkstra’s Algorithm.

The algorithm itself doesn’t care what type the nodes are, as long as you provide a cost function that can convert a node of that type into a u32 cost to move to that node, and a neighbors function that can generate a slice of nodes to explore.

The cost function should return u32::MAX for unpassable tiles.

§Example

use screeps::{LocalRoomTerrain, RoomXY};
use screeps_pathfinding::utils::goals::goal_exact_node;

let start = RoomXY::checked_new(24, 18).unwrap();
let goal = RoomXY::checked_new(34, 40).unwrap();
let room_terrain = LocalRoomTerrain::new_from_bits(Box::new([0; 2500])); // Terrain that's all plains
let plain_cost = 1;
let swamp_cost = 5;
let costs = screeps_pathfinding::utils::movement_costs::get_movement_cost_lcm_from_terrain(&room_terrain, plain_cost, swamp_cost);
let costs_fn = screeps_pathfinding::utils::movement_costs::movement_costs_from_lcm(&costs);
let neighbors_fn = screeps_pathfinding::utils::neighbors::room_xy_neighbors;
let max_ops = 2000;
let max_cost = 2000;

let search_results = screeps_pathfinding::algorithms::dijkstra::shortest_path_generic(
    &[start],
    &goal_exact_node(goal),
    costs_fn,
    neighbors_fn,
    max_ops,
    max_cost,
);

if !search_results.incomplete() {
  let path = search_results.path();
  println!("Path: {:?}", path);
}
else {
  println!("Could not find Dijkstra shortest path.");
  println!("Search Results: {:?}", search_results);
}

Reference: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm