Skip to content

Commit

Permalink
Lots of breaking changes to RoomXY math.
Browse files Browse the repository at this point in the history
  • Loading branch information
khoover committed Nov 15, 2024
1 parent c967322 commit 775d9a8
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 120 deletions.
32 changes: 31 additions & 1 deletion src/local/room_coordinate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,18 @@ impl Sub for RoomCoordinate {
}
}

impl std::cmp::PartialOrd<u8> for RoomCoordinate {
fn partial_cmp(&self, other: &u8) -> Option<std::cmp::Ordering> {
Some(self.0.cmp(other))
}
}

impl std::cmp::PartialEq<u8> for RoomCoordinate {
fn eq(&self, other: &u8) -> bool {
self.0 == *other
}
}

const ROOM_SIZE_I8: i8 = {
// If this fails, we need to rework the arithmetic code
debug_assert!(2 * ROOM_SIZE <= i8::MAX as u8);
Expand Down Expand Up @@ -439,7 +451,7 @@ impl RoomOffset {
pub fn saturating_add(self, rhs: Self) -> Self {
self.assume_bounds_constraint();
rhs.assume_bounds_constraint();
Self::new((self.0 + rhs.0).clamp(-ROOM_SIZE_I8 + 1, ROOM_SIZE_I8 - 1)).unwrap_throw()
Self::saturating_new(self.0 + rhs.0)
}

/// Add two offsets together, wrapping around at the ends of the valid
Expand Down Expand Up @@ -528,6 +540,12 @@ impl RoomOffset {
}
}

impl fmt::Display for RoomOffset {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}

impl From<RoomOffset> for i8 {
fn from(offset: RoomOffset) -> i8 {
offset.0
Expand Down Expand Up @@ -566,6 +584,18 @@ impl Neg for RoomOffset {
}
}

impl std::cmp::PartialOrd<i8> for RoomOffset {
fn partial_cmp(&self, other: &i8) -> Option<std::cmp::Ordering> {
Some(self.0.cmp(other))
}
}

impl std::cmp::PartialEq<i8> for RoomOffset {
fn eq(&self, other: &i8) -> bool {
self.0 == *other
}
}

#[cfg(feature = "nightly")]
impl std::iter::Step for RoomCoordinate {
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
Expand Down
Loading

0 comments on commit 775d9a8

Please sign in to comment.