Skip to content

Commit

Permalink
Improved RoomCoordinate and RoomXY, new RoomOffset for optimize…
Browse files Browse the repository at this point in the history
…d arithmetic (#543)
  • Loading branch information
khoover authored Oct 29, 2024
1 parent d8cc9c8 commit 5f1ea09
Show file tree
Hide file tree
Showing 8 changed files with 690 additions and 54 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Unreleased

- Add `s7_score_cycle_at_tick` seasonal constant function to reflect the reversed score cycle in
season 7
- Add indexing implementations for `RoomCoordinate` and `RoomXY` as well as `XMajor` / `YMajor`
wrapper types to control which indexing approach is used; switch to using these for
`LocalCostMatrix` and `LocalRoomTerrain`
- Add `RoomOffset` type representing a difference between coordinates and associated functions
for manipulating `RoomCoordinate` and `RoomXY`

0.22.0 (2024-08-27)
===================
Expand Down
7 changes: 6 additions & 1 deletion src/constants/extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,15 @@ pub const ROOM_VISUAL_PER_ROOM_SIZE_LIMIT: u32 = 500 * 1024;
/// [`Room`]: crate::objects::Room
pub const ROOM_SIZE: u8 = 50;

/// ['ROOM_SIZE'] cast into a `usize`, for indexing convenience.
///
/// ['ROOM_SIZE']: self::ROOM_SIZE
pub const ROOM_USIZE: usize = ROOM_SIZE as usize;

/// The number of total tiles in each [`Room`] in the game
///
/// [`Room`]: crate::objects::Room
pub const ROOM_AREA: usize = (ROOM_SIZE as usize) * (ROOM_SIZE as usize);
pub const ROOM_AREA: usize = ROOM_USIZE * ROOM_USIZE;

/// Owner username of hostile non-player structures and creeps which occupy
/// sector center rooms.
Expand Down
20 changes: 15 additions & 5 deletions src/local/cost_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
traits::{CostMatrixGet, CostMatrixSet},
};

use super::{linear_index_to_xy, xy_to_linear_index, Position, RoomXY};
use super::{linear_index_to_xy, Position, RoomXY, XMajor};

/// A matrix of pathing costs for a room, stored in Rust memory.
///
Expand Down Expand Up @@ -121,19 +121,29 @@ impl From<&CostMatrix> for LocalCostMatrix {
}
}

impl AsRef<XMajor<u8>> for LocalCostMatrix {
fn as_ref(&self) -> &XMajor<u8> {
XMajor::from_flat_ref(&self.bits)
}
}

impl AsMut<XMajor<u8>> for LocalCostMatrix {
fn as_mut(&mut self) -> &mut XMajor<u8> {
XMajor::from_flat_mut(&mut self.bits)
}
}

impl Index<RoomXY> for LocalCostMatrix {
type Output = u8;

fn index(&self, xy: RoomXY) -> &Self::Output {
// SAFETY: RoomXY is always a valid coordinate.
unsafe { self.bits.get_unchecked(xy_to_linear_index(xy)) }
&self.bits[xy.x][xy.y]
}
}

impl IndexMut<RoomXY> for LocalCostMatrix {
fn index_mut(&mut self, xy: RoomXY) -> &mut Self::Output {
// SAFETY: RoomXY is always a valid coordinate.
unsafe { self.bits.get_unchecked_mut(xy_to_linear_index(xy)) }
&mut self.bits[xy.x][xy.y]
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/local/object_id/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Serialize for RawObjectId {

struct RawObjectIdVisitor;

impl<'de> Visitor<'de> for RawObjectIdVisitor {
impl Visitor<'_> for RawObjectIdVisitor {
type Value = RawObjectId;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
Expand Down
Loading

0 comments on commit 5f1ea09

Please sign in to comment.