From 570788ac8d844a6d0cdc2c5645336d55338904b3 Mon Sep 17 00:00:00 2001 From: Joe Ciskey Date: Mon, 27 May 2024 18:14:17 -0600 Subject: [PATCH 1/4] Add convenience iterator method for Direction enum --- src/constants/small_enums.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/constants/small_enums.rs b/src/constants/small_enums.rs index 59d7f1f1..b9564722 100644 --- a/src/constants/small_enums.rs +++ b/src/constants/small_enums.rs @@ -1,5 +1,5 @@ //! Various constants translated as small enums. -use std::{borrow::Cow, fmt}; +use std::{borrow::Cow, fmt, slice::Iter}; use enum_iterator::Sequence; use js_sys::JsString; @@ -193,6 +193,39 @@ impl Direction { pub fn rot_ccw(self) -> Self { self.multi_rot(-1) } + + /// Returns an iterator over all 8 direction constants, in clockwise order. + /// + /// Example usage: + /// + /// ``` + /// use screeps::Direction; + /// + /// for dir in Direction::iter() { + /// println!("{:?}", dir); + /// } + /// ``` + /// + /// Alternatively: + /// ``` + /// use screeps::Direction; + /// let mut dirs = Direction::iter(); + /// + /// assert_eq!(dirs.next(), Some(&Direction::Top)); + /// assert_eq!(dirs.next(), Some(&Direction::TopRight)); + /// assert_eq!(dirs.next(), Some(&Direction::Right)); + /// assert_eq!(dirs.next(), Some(&Direction::BottomRight)); + /// assert_eq!(dirs.next(), Some(&Direction::Bottom)); + /// assert_eq!(dirs.next(), Some(&Direction::BottomLeft)); + /// assert_eq!(dirs.next(), Some(&Direction::Left)); + /// assert_eq!(dirs.next(), Some(&Direction::TopLeft)); + /// assert_eq!(dirs.next(), None); + /// ``` + pub fn iter() -> Iter<'static, Direction> { + use crate::Direction::*; + static DIRECTIONS: [Direction; 8] = [Top, TopRight, Right, BottomRight, Bottom, BottomLeft, Left, TopLeft]; + DIRECTIONS.iter() + } } impl JsCollectionIntoValue for Direction { From 0d85bafdccb3a5e1690ed343d8ca14b64503823b Mon Sep 17 00:00:00 2001 From: Joe Ciskey Date: Mon, 27 May 2024 18:46:32 -0600 Subject: [PATCH 2/4] Add helper method to get all valid neighbors of a local RoomXY --- src/local/room_xy.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/local/room_xy.rs b/src/local/room_xy.rs index 86d77dbf..acf2ba14 100644 --- a/src/local/room_xy.rs +++ b/src/local/room_xy.rs @@ -184,6 +184,49 @@ impl RoomXY { let (dx, dy) = rhs.into(); self.saturating_add((dx as i8, dy as i8)) } + + /// Get all the valid neighbors of a given `RoomXY`. + /// + /// Example usage: + /// + /// ``` + /// use screeps::local::RoomXY; + /// + /// let zero_zero = unsafe { RoomXY::unchecked_new(0, 0) }; + /// let zero_one = unsafe { RoomXY::unchecked_new(0, 1) }; + /// let one_zero = unsafe { RoomXY::unchecked_new(1, 0) }; + /// let one_one = unsafe { RoomXY::unchecked_new(1, 1) }; + /// + /// let zero_two = unsafe { RoomXY::unchecked_new(0, 2) }; + /// let one_two = unsafe { RoomXY::unchecked_new(1, 2) }; + /// let two_two = unsafe { RoomXY::unchecked_new(2, 2) }; + /// let two_one = unsafe { RoomXY::unchecked_new(2, 1) }; + /// let two_zero = unsafe { RoomXY::unchecked_new(2, 0) }; + /// + /// let zero_zero_neighbors = zero_zero.neighbors(); + /// + /// assert_eq!(zero_zero_neighbors.len(), 3); + /// assert!(zero_zero_neighbors.contains(&zero_one)); + /// assert!(zero_zero_neighbors.contains(&one_one)); + /// assert!(zero_zero_neighbors.contains(&one_zero)); + /// + /// let one_one_neighbors = one_one.neighbors(); + /// + /// assert_eq!(one_one_neighbors.len(), 8); + /// assert!(one_one_neighbors.contains(&zero_zero)); + /// assert!(one_one_neighbors.contains(&zero_one)); + /// assert!(one_one_neighbors.contains(&one_zero)); + /// assert!(one_one_neighbors.contains(&zero_two)); + /// assert!(one_one_neighbors.contains(&one_two)); + /// assert!(one_one_neighbors.contains(&two_two)); + /// assert!(one_one_neighbors.contains(&two_one)); + /// assert!(one_one_neighbors.contains(&two_zero)); + /// ``` + pub fn neighbors(self) -> Vec { + Direction::iter() + .filter_map(|dir| self.checked_add_direction(*dir)) + .collect() + } } impl fmt::Display for RoomXY { From 27a11c5c8d01c911b367ffbc6606b18d37b26b27 Mon Sep 17 00:00:00 2001 From: Joe Ciskey Date: Mon, 27 May 2024 18:49:20 -0600 Subject: [PATCH 3/4] Formatting changes --- src/constants/small_enums.rs | 13 +++++++++++-- src/local/room_xy.rs | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/constants/small_enums.rs b/src/constants/small_enums.rs index b9564722..e4d9709d 100644 --- a/src/constants/small_enums.rs +++ b/src/constants/small_enums.rs @@ -202,7 +202,7 @@ impl Direction { /// use screeps::Direction; /// /// for dir in Direction::iter() { - /// println!("{:?}", dir); + /// println!("{:?}", dir); /// } /// ``` /// @@ -223,7 +223,16 @@ impl Direction { /// ``` pub fn iter() -> Iter<'static, Direction> { use crate::Direction::*; - static DIRECTIONS: [Direction; 8] = [Top, TopRight, Right, BottomRight, Bottom, BottomLeft, Left, TopLeft]; + static DIRECTIONS: [Direction; 8] = [ + Top, + TopRight, + Right, + BottomRight, + Bottom, + BottomLeft, + Left, + TopLeft, + ]; DIRECTIONS.iter() } } diff --git a/src/local/room_xy.rs b/src/local/room_xy.rs index acf2ba14..368c9156 100644 --- a/src/local/room_xy.rs +++ b/src/local/room_xy.rs @@ -224,8 +224,8 @@ impl RoomXY { /// ``` pub fn neighbors(self) -> Vec { Direction::iter() - .filter_map(|dir| self.checked_add_direction(*dir)) - .collect() + .filter_map(|dir| self.checked_add_direction(*dir)) + .collect() } } From 745e960f6ef563387b66954e75c9bd96cd203f85 Mon Sep 17 00:00:00 2001 From: Joe Ciskey Date: Tue, 28 May 2024 15:40:26 -0600 Subject: [PATCH 4/4] Add changelog entries for updates --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e86b15e..38306c40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ Unreleased ========== +### Additions: + +- Add function `Direction::iter` which returns an iterator over all the `Direction` enum values +- Add function `RoomXY::neighbors` which returns an iterator over all the valid neighbors of + a given `RoomXY` position + 0.21.0 (2024-05-14) ===================