screeps_pathfinding/common/
traits.rs

1use screeps::{Direction, Position, RoomXY};
2
3/// Trait that encapsulates taking a [screeps::Direction] constant and
4/// producing a new node from it.
5pub trait AddDirection {
6    fn checked_add_direction(self, direction: Direction) -> Option<Self>
7    where
8        Self: Sized;
9}
10
11impl AddDirection for RoomXY {
12    fn checked_add_direction(self, direction: Direction) -> Option<Self> {
13        Self::checked_add_direction(self, direction)
14    }
15}
16
17impl AddDirection for Position {
18    fn checked_add_direction(self, direction: Direction) -> Option<Self> {
19        Self::checked_add_direction(self, direction).ok()
20    }
21}
22
23/// Trait that encapsulates being able to get a standardized range value
24/// from one node to another.
25pub trait GetRangeTo {
26    fn get_range_to(self, other: Self) -> u32;
27}
28
29impl GetRangeTo for RoomXY {
30    fn get_range_to(self, other: Self) -> u32 {
31        self.get_range_to(other).into()
32    }
33}
34
35impl GetRangeTo for Position {
36    fn get_range_to(self, other: Self) -> u32 {
37        self.get_range_to(other)
38    }
39}