screeps_pathfinding/utils/pathing.rs
1use screeps::{Direction, Position};
2
3/// Utility function for converting a position and a path
4/// into a direction for the next movement on the path.
5///
6/// If the position is not on the path, it will return
7/// a direction that moves the creep towards the first
8/// position of the path.
9///
10/// Returns None if the current position is the final
11/// position in the path, or if the path is empty.
12pub fn get_next_step_direction_in_vec_pos(
13 current_pos: &Position,
14 path: &[Position],
15) -> Option<Direction> {
16 if path.is_empty() {
17 return None;
18 }
19
20 for i in 0..path.len() {
21 let path_pos = path[i];
22 if path_pos == *current_pos {
23 // Check if the current position is the last entry in the path
24 if i == (path.len() - 1) {
25 // info!(target: "next_step", "Current position is last entry in path");
26 return None; // No next step if we're at the last entry
27 } else {
28 let next_pos = path[i + 1];
29 let direction = current_pos.get_direction_to(next_pos);
30 // info!(target: "next_step", "Direction: {:?}", direction);
31 return direction;
32 }
33 }
34 }
35
36 // No next step if we're not on the path at all, move towards the path start
37 // info!(target: "next_step", "Current position is not in path");
38 if let Some(path_pos) = path.first() {
39 current_pos.get_direction_to(*path_pos)
40 } else {
41 // Final fallback, should be captured by the initial length check, but
42 // the compiler isn't smart enough to know that.
43 None
44 }
45}
46
47// #[derive(Debug)]
48// pub struct PathMovementCache {
49// current_position: Position,
50// path_index: usize,
51// path: &[Position],
52// }
53
54// impl PathMovementCache {
55// pub fn new(current_position: Position, path_index: usize, path: &[Position]) -> Self {
56// Self {
57// current_position,
58// path_index,
59// path,
60// }
61// }
62
63// pub fn did_creep_move(&self) -> bool {
64
65// }
66
67// }