Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved RoomCoordinate and RoomXY, new RoomOffset for optimized arithmetic #543

Merged
merged 27 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9902299
feat: add indexing impls for `RoomCoordinate`
khoover Oct 9, 2024
9293f72
feat: add `ROOM_USIZE` constant
khoover Oct 9, 2024
3cf8c14
feat: create x/y-major wrappers for `RoomXY` indexing
khoover Oct 9, 2024
e7cad72
feat: allow taking `LocalCostMatrix` as `XMajor<u8>` ref
khoover Oct 9, 2024
3271f97
refactor: use `RoomCoordinate` indexing for `LocalRoomTerrain`
khoover Oct 9, 2024
1f0ff51
refactor: use ROOM_USIZE instead of ROOM_SIZE as usize
khoover Oct 9, 2024
2324310
style: import ROOM_AREA from constants too
khoover Oct 9, 2024
2e69bd9
refactor: Replaced match with ?
khoover Oct 13, 2024
1257f7c
feat: Added size assumption function, removed unsafe
khoover Oct 13, 2024
f149aa7
fix: Fix clippy lints
khoover Oct 13, 2024
934be55
perf: better wasm emission for RoomCoordinate arithmetic
khoover Oct 13, 2024
469cdc4
perf: Added RoomCoord tests, improved checked add
khoover Oct 14, 2024
494f7fd
cleanup: Use copied and all in tests
khoover Oct 14, 2024
619c188
test: refine the room area test
khoover Oct 14, 2024
ada73d8
test: replace .min().max() with clamp
khoover Oct 14, 2024
b27b803
perf: faster saturating add for RoomCoordinate
khoover Oct 14, 2024
fb107cd
fix: eliminate all ROOM_SIZE-related literals
khoover Oct 14, 2024
bd2cfde
fix: fmt
khoover Oct 14, 2024
8f7ef85
fix: missing parens
khoover Oct 14, 2024
34b23db
feat: Add RoomOffset and more arithmetic
khoover Oct 14, 2024
7dba3a5
refactor: rename assume_size_constraint
khoover Oct 14, 2024
5bff038
More docs, `RoomOffset::abs`
khoover Oct 15, 2024
7701804
refactor: Add doc comments, return Result over Option
khoover Oct 29, 2024
9568dc7
fix: added correct import for doctest
khoover Oct 29, 2024
394a303
fix: fmt
khoover Oct 29, 2024
2f10f6f
fix: Fix wrapping arithmetic for overflowing_add.
khoover Oct 29, 2024
63ca176
Changelog
shanemadden Oct 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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