From 279815255708f7951ddb7fa5ea85dd621fe19468 Mon Sep 17 00:00:00 2001 From: Frankie Fisher Date: Sun, 29 Dec 2024 12:25:20 +0000 Subject: [PATCH 1/2] feat: port in world api's utility functions on Direction type --- src/constants/small_enums.rs | 79 ++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/src/constants/small_enums.rs b/src/constants/small_enums.rs index f7e5622..a4c3d61 100644 --- a/src/constants/small_enums.rs +++ b/src/constants/small_enums.rs @@ -95,6 +95,85 @@ pub enum Direction { TopLeft = 8, } +impl Direction { + /// Whether the direction is orthogonal. + /// + /// Example usage: + /// + /// ``` + /// use screeps::Direction::*; + /// + /// assert_eq!(Top.is_orthogonal(), true); + /// assert_eq!(TopRight.is_orthogonal(), false); + /// ``` + pub fn is_orthogonal(self) -> bool { + use Direction::*; + + matches!(self, Top | Right | Bottom | Left) + } + + /// Whether the direction is diagonal. + /// + /// Example usage: + /// + /// ``` + /// use screeps::Direction::*; + /// + /// assert_eq!(Top.is_diagonal(), false); + /// assert_eq!(TopRight.is_diagonal(), true); + /// ``` + pub fn is_diagonal(self) -> bool { + !self.is_orthogonal() + } + + /// Rotate the direction by a specified number of steps clockwise if + /// positive or counter-clockwise if negative. + /// + /// Example usage: + /// + /// ``` + /// use screeps::Direction::*; + /// + /// assert_eq!(Top.multi_rot(1), TopRight); + /// assert_eq!(Top.multi_rot(2), Right); + /// assert_eq!(Top.multi_rot(-1), TopLeft); + /// assert_eq!(Top.multi_rot(-2), Left); + /// assert_eq!(Top.multi_rot(64), Top); + /// ``` + pub fn multi_rot(self, times: i8) -> Self { + let raw_dir = ((self as u8) - 1).wrapping_add_signed(times) % 8 + 1; + // unwrap should be optimized away, as the integer we ended up with + // is always a valid value + Self::from_u8(raw_dir).unwrap() + } + + /// Rotate the direction clockwise by one step. + /// + /// Example usage: + /// + /// ``` + /// use screeps::Direction::*; + /// + /// assert_eq!(Top.rot_cw(), TopRight); + /// ``` + pub fn rot_cw(self) -> Self { + self.multi_rot(1) + } + + /// Rotate the direction counter-clockwise by one step. + /// + /// Example usage: + /// + /// ``` + /// use screeps::Direction::*; + /// + /// assert_eq!(Top.rot_ccw(), TopLeft); + /// ``` + pub fn rot_ccw(self) -> Self { + self.multi_rot(-1) + } +} + impl From for (i8, i8) { /// Returns the change in (x, y) when moving in each direction. #[inline] From b51e2f91871286414c0c0d0198a4c65dfdbbc731 Mon Sep 17 00:00:00 2001 From: Frankie Fisher Date: Sun, 29 Dec 2024 12:47:30 +0000 Subject: [PATCH 2/2] fix: doc examples use screeps_arena crate rather than screeps world --- src/constants/small_enums.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/constants/small_enums.rs b/src/constants/small_enums.rs index a4c3d61..9edd2aa 100644 --- a/src/constants/small_enums.rs +++ b/src/constants/small_enums.rs @@ -101,7 +101,7 @@ impl Direction { /// Example usage: /// /// ``` - /// use screeps::Direction::*; + /// use screeps_arena::Direction::*; /// /// assert_eq!(Top.is_orthogonal(), true); /// assert_eq!(TopRight.is_orthogonal(), false); @@ -117,7 +117,7 @@ impl Direction { /// Example usage: /// /// ``` - /// use screeps::Direction::*; + /// use screeps_arena::Direction::*; /// /// assert_eq!(Top.is_diagonal(), false); /// assert_eq!(TopRight.is_diagonal(), true); @@ -132,7 +132,7 @@ impl Direction { /// Example usage: /// /// ``` - /// use screeps::Direction::*; + /// use screeps_arena::Direction::*; /// /// assert_eq!(Top.multi_rot(1), TopRight); /// assert_eq!(Top.multi_rot(2), Right); @@ -152,7 +152,7 @@ impl Direction { /// Example usage: /// /// ``` - /// use screeps::Direction::*; + /// use screeps_arena::Direction::*; /// /// assert_eq!(Top.rot_cw(), TopRight); /// ``` @@ -165,7 +165,7 @@ impl Direction { /// Example usage: /// /// ``` - /// use screeps::Direction::*; + /// use screeps_arena::Direction::*; /// /// assert_eq!(Top.rot_ccw(), TopLeft); /// ```