shortest_path_position_multiple_goals

Function shortest_path_position_multiple_goals 

Source
pub fn shortest_path_position_multiple_goals<G>(
    start: Position,
    goals: &[Position],
    cost_fn: G,
) -> AStarSearchResults<Position, u32>
where G: Fn(Position) -> Option<u32>,
Expand description

Convenience function for the common use-case of searching from a single starting node to multiple goal nodes.

Uses sane default values for maximum operations and travel costs. For more fine-grained control, see: shortest_path_generic_grid

ยงExample

use screeps::{LocalRoomTerrain, Position, RoomCoordinate};

fn new_position(room_name: &str, x: u8, y: u8) -> Position {
       Position::new(
           RoomCoordinate::try_from(x).unwrap(),
           RoomCoordinate::try_from(y).unwrap(),
           room_name.parse().unwrap(),
       )
   }

let room_name = "E5N6";
let start = new_position(room_name, 24, 18);
let goal_a = new_position(room_name, 34, 40);
let goal_b = new_position(room_name, 34, 45);
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::astar_movement_costs_from_lcm(&costs);

let search_results = screeps_pathfinding::algorithms::astar::shortest_path_position_multiple_goals(
    start,
    &[goal_a, goal_b],
    costs_fn,
);

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