pub struct RoomXY {
pub x: RoomCoordinate,
pub y: RoomCoordinate,
}
Expand description
An X/Y pair representing a given coordinate relative to any room.
Fields§
§x: RoomCoordinate
§y: RoomCoordinate
Implementations§
Source§impl RoomXY
impl RoomXY
Sourcepub fn towards(self, target: RoomXY, distance_towards_target: i8) -> RoomXY
pub fn towards(self, target: RoomXY, distance_towards_target: i8) -> RoomXY
Calculates an approximate midpoint between this point and the target.
In case of a tie, rounds towards this point.
If distance_towards_target
is bigger than the distance to the target,
the target is returned.
§Example
// Exact distances
let start = RoomXY::checked_new(10, 10).unwrap();
let target = RoomXY::checked_new(10, 15).unwrap();
assert_eq!(
start.towards(target, 1),
RoomXY::checked_new(10, 11).unwrap()
);
assert_eq!(
start.towards(target, 4),
RoomXY::checked_new(10, 14).unwrap()
);
assert_eq!(
start.towards(target, 10),
RoomXY::checked_new(10, 15).unwrap()
);
// Approximate/rounded distances
let start = RoomXY::checked_new(10, 10).unwrap();
let target_1 = RoomXY::checked_new(15, 20).unwrap();
let target_2 = RoomXY::checked_new(0, 5).unwrap();
assert_eq!(
start.towards(target_1, 1),
RoomXY::checked_new(10, 11).unwrap()
);
assert_eq!(
start.towards(target_1, 9),
RoomXY::checked_new(14, 19).unwrap()
);
assert_eq!(
start.towards(target_2, 1),
RoomXY::checked_new(9, 10).unwrap()
);
Sourcepub fn between(self, target: RoomXY, distance_from_target: i8) -> RoomXY
pub fn between(self, target: RoomXY, distance_from_target: i8) -> RoomXY
Calculates an approximate midpoint between this point and the target.
In case of a tie, rounds towards the target.
If distance_from_target
is bigger than the distance to the target,
this position is returned.
Note: This is essentially the same as RoomXY::towards
, just rounding
towards the target instead of the starting position.
§Example
// Exact distances
let start = RoomXY::checked_new(10, 15).unwrap();
let target = RoomXY::checked_new(10, 10).unwrap();
assert_eq!(
start.between(target, 1),
RoomXY::checked_new(10, 11).unwrap()
);
assert_eq!(
start.between(target, 4),
RoomXY::checked_new(10, 14).unwrap()
);
assert_eq!(
start.between(target, 10),
RoomXY::checked_new(10, 15).unwrap()
);
// Approximate/rounded distances
let start_1 = RoomXY::checked_new(15, 20).unwrap();
let start_2 = RoomXY::checked_new(0, 5).unwrap();
let target = RoomXY::checked_new(10, 10).unwrap();
assert_eq!(
start_1.between(target, 1),
RoomXY::checked_new(10, 11).unwrap()
);
assert_eq!(
start_1.between(target, 9),
RoomXY::checked_new(14, 19).unwrap()
);
assert_eq!(
start_2.between(target, 1),
RoomXY::checked_new(9, 10).unwrap()
);
Sourcepub fn midpoint_between(self, target: RoomXY) -> RoomXY
pub fn midpoint_between(self, target: RoomXY) -> RoomXY
Calculates an approximate midpoint between this point and the target.
In case of a tie, rounds towards the target.
§Example
// Exact distances
let start = RoomXY::checked_new(10, 10).unwrap();
let target_1 = RoomXY::checked_new(10, 16).unwrap();
assert_eq!(
start.midpoint_between(target_1),
RoomXY::checked_new(10, 13).unwrap()
);
let target_2 = RoomXY::checked_new(20, 10).unwrap();
assert_eq!(
start.midpoint_between(target_2),
RoomXY::checked_new(15, 10).unwrap()
);
let target_3 = RoomXY::checked_new(12, 12).unwrap();
assert_eq!(
start.midpoint_between(target_3),
RoomXY::checked_new(11, 11).unwrap()
);
let target_4 = RoomXY::checked_new(4, 4).unwrap();
assert_eq!(
start.midpoint_between(target_4),
RoomXY::checked_new(7, 7).unwrap()
);
// Approximate/rounded distances
let start = RoomXY::checked_new(10, 10).unwrap();
let target_1 = RoomXY::checked_new(10, 15).unwrap();
assert_eq!(
start.midpoint_between(target_1),
RoomXY::checked_new(10, 13).unwrap()
);
let target_2 = RoomXY::checked_new(19, 10).unwrap();
assert_eq!(
start.midpoint_between(target_2),
RoomXY::checked_new(15, 10).unwrap()
);
let target_3 = RoomXY::checked_new(11, 11).unwrap();
assert_eq!(
start.midpoint_between(target_3),
RoomXY::checked_new(11, 11).unwrap()
);
let target_4 = RoomXY::checked_new(15, 15).unwrap();
assert_eq!(
start.midpoint_between(target_4),
RoomXY::checked_new(13, 13).unwrap()
);
Source§impl RoomXY
impl RoomXY
Sourcepub fn offset(&mut self, x: i8, y: i8)
pub fn offset(&mut self, x: i8, y: i8)
Returns a new position offset from this position by the specified x coords and y coords.
Unlike Position::offset
, this function operates on room coordinates,
and will panic if the new position overflows the room.
To return a new position rather than modifying in place, use pos + (x, y)
. See the implementation of Add<(i8, i8)>
for
RoomXY
further down on this page.
§Panics
Will panic if the new position overflows the room.
§Example
let mut pos = RoomXY::checked_new(21, 21).unwrap();
pos.offset(5, 5);
assert_eq!(pos, RoomXY::checked_new(26, 26).unwrap());
let mut pos = RoomXY::checked_new(21, 21).unwrap();
pos.offset(-5, 5);
assert_eq!(pos, RoomXY::checked_new(16, 26).unwrap());
Source§impl RoomXY
impl RoomXY
Sourcepub fn get_direction_to(self, target: RoomXY) -> Option<Direction>
pub fn get_direction_to(self, target: RoomXY) -> Option<Direction>
Gets linear direction to the specified position.
Note that this chooses between Top
/Bottom
/Left
/Right
and
TopLeft
/TopRight
/BottomLeft
/BottomRight
by the magnitude in both
directions. For instance, Direction::Top
can be returned even
if the target has a slightly different x
coordinate.
Sourcepub fn get_range_to(self, target: RoomXY) -> u8
pub fn get_range_to(self, target: RoomXY) -> u8
Gets linear range to the specified position.
Linear range (also called Chebyshev Distance) is an alternate calculation of distance, calculated as the greater of the distance along the x axis or the y axis. Most calculations in Screeps use this distance metric. For more information see Chebeshev Distance.
§Examples
let pos_1 = RoomXY::checked_new(5, 10).unwrap();
let pos_2 = RoomXY::checked_new(8, 15).unwrap();
// The differences are 3 along the X axis and 5 along the Y axis
// so the linear distance is 5.
assert_eq!(pos_1.get_range_to(pos_2), 5);
Sourcepub fn in_range_to(self, target: RoomXY, range: u8) -> bool
pub fn in_range_to(self, target: RoomXY, range: u8) -> bool
Checks whether this position is in the given range of another position.
Linear range (also called Chebyshev Distance) is an alternate calculation of distance, calculated as the greater of the distance along the x axis or the y axis. Most calculations in Screeps use this distance metric. For more information see Chebeshev Distance.
§Examples
let pos_1 = RoomXY::checked_new(5, 10).unwrap();
let pos_2 = RoomXY::checked_new(8, 10).unwrap();
// The differences are 3 along the X axis and 0 along the Y axis
// so the linear distance is 3.
assert_eq!(pos_1.in_range_to(pos_2, 5), true);
let pos_3 = RoomXY::checked_new(8, 15).unwrap();
// The differences are 3 along the X axis and 5 along the Y axis
// so the linear distance is 5.
// `in_range_to` returns true if the linear distance is equal to the range
assert_eq!(pos_1.in_range_to(pos_3, 5), true);
let pos_4 = RoomXY::checked_new(20, 20).unwrap();
// The differences are 15 along the X axis and 10 along the Y axis
// so the linear distance is 15.
assert_eq!(pos_1.in_range_to(pos_4, 5), false);
assert_eq!(pos_1.in_range_to(pos_4, 10), false);
assert_eq!(pos_1.in_range_to(pos_4, 15), true);
Sourcepub fn is_equal_to(self, target: RoomXY) -> bool
pub fn is_equal_to(self, target: RoomXY) -> bool
Checks whether this position is the same as the specified position.
§Examples
let pos_1 = RoomXY::checked_new(5, 10).unwrap();
let pos_2 = RoomXY::checked_new(5, 10).unwrap();
let pos_3 = RoomXY::checked_new(4, 9).unwrap();
assert_eq!(pos_1.is_equal_to(pos_2), true);
assert_eq!(pos_1.is_equal_to(pos_3), false);
Sourcepub fn is_near_to(self, target: RoomXY) -> bool
pub fn is_near_to(self, target: RoomXY) -> bool
True if the range from this position to the target is at most 1.
§Examples
let pos_1 = RoomXY::checked_new(5, 10).unwrap();
let pos_2 = RoomXY::checked_new(6, 10).unwrap();
let pos_3 = RoomXY::checked_new(4, 9).unwrap();
let pos_4 = RoomXY::checked_new(20, 20).unwrap();
assert_eq!(pos_1.is_near_to(pos_2), true);
assert_eq!(pos_1.is_near_to(pos_3), true);
assert_eq!(pos_1.is_near_to(pos_4), false);
Source§impl RoomXY
impl RoomXY
Sourcepub fn new(x: RoomCoordinate, y: RoomCoordinate) -> Self
pub fn new(x: RoomCoordinate, y: RoomCoordinate) -> Self
Create a new RoomXY
from a pair of RoomCoordinate
.
Sourcepub fn checked_new(x: u8, y: u8) -> Result<RoomXY, OutOfBoundsError>
pub fn checked_new(x: u8, y: u8) -> Result<RoomXY, OutOfBoundsError>
Create a new RoomXY
from a pair of u8
, checking that they’re in
the range of valid values.
Sourcepub unsafe fn unchecked_new(x: u8, y: u8) -> Self
pub unsafe fn unchecked_new(x: u8, y: u8) -> Self
Create a RoomXY
from a pair of u8
, without checking whether it’s in
the range of valid values.
§Safety
Calling this method with x >= ROOM_SIZE
or y >= ROOM_SIZE
can
result in undefined behaviour when the resulting RoomXY
is used.
Sourcepub const fn is_room_edge(self) -> bool
pub const fn is_room_edge(self) -> bool
Get whether this coordinate pair represents an edge position (0 or 49 for either coordinate)
Sourcepub fn checked_add(self, rhs: (i8, i8)) -> Option<RoomXY>
pub fn checked_add(self, rhs: (i8, i8)) -> Option<RoomXY>
Get the coordinate adjusted by a certain value, returning None
if the
result is outside the valid room area.
Example usage:
use screeps::local::RoomXY;
let zero = unsafe { RoomXY::unchecked_new(0, 0) };
let one = unsafe { RoomXY::unchecked_new(1, 1) };
let forty_nine = unsafe { RoomXY::unchecked_new(49, 49) };
assert_eq!(zero.checked_add((1, 1)), Some(one));
assert_eq!(zero.checked_add((-1, 0)), None);
assert_eq!(zero.checked_add((49, 49)), Some(forty_nine));
assert_eq!(forty_nine.checked_add((1, 1)), None);
Sourcepub fn saturating_add(self, rhs: (i8, i8)) -> RoomXY
pub fn saturating_add(self, rhs: (i8, i8)) -> RoomXY
Get the coordinate adjusted by a certain value, saturating at the edges of the room if the result would be outside the valid room area.
Example usage:
use screeps::local::RoomXY;
let zero = unsafe { RoomXY::unchecked_new(0, 0) };
let one = unsafe { RoomXY::unchecked_new(1, 1) };
let forty_nine = unsafe { RoomXY::unchecked_new(49, 49) };
assert_eq!(zero.saturating_add((1, 1)), one);
assert_eq!(zero.saturating_add((-1, 0)), zero);
assert_eq!(zero.saturating_add((49, 49)), forty_nine);
assert_eq!(zero.saturating_add((i8::MAX, i8::MAX)), forty_nine);
assert_eq!(forty_nine.saturating_add((1, 1)), forty_nine);
assert_eq!(forty_nine.saturating_add((i8::MIN, i8::MIN)), zero);
Sourcepub fn checked_add_direction(self, rhs: Direction) -> Option<RoomXY>
pub fn checked_add_direction(self, rhs: Direction) -> Option<RoomXY>
Get the neighbor of a given RoomXY
in the given direction, returning
None
if the result is outside the valid room area.
Example usage:
use screeps::{constants::Direction::*, local::RoomXY};
let zero = unsafe { RoomXY::unchecked_new(0, 0) };
let one = unsafe { RoomXY::unchecked_new(1, 1) };
let forty_nine = unsafe { RoomXY::unchecked_new(49, 49) };
assert_eq!(zero.checked_add_direction(BottomRight), Some(one));
assert_eq!(zero.checked_add_direction(TopLeft), None);
assert_eq!(one.checked_add_direction(TopLeft), Some(zero));
assert_eq!(forty_nine.checked_add_direction(BottomRight), None);
Sourcepub fn saturating_add_direction(self, rhs: Direction) -> RoomXY
pub fn saturating_add_direction(self, rhs: Direction) -> RoomXY
Get the neighbor of a given RoomXY
in the given direction, saturating
at the edges if the result is outside the valid room area.
Example usage:
use screeps::{constants::Direction::*, local::RoomXY};
let zero = unsafe { RoomXY::unchecked_new(0, 0) };
let one = unsafe { RoomXY::unchecked_new(1, 1) };
let forty_nine = unsafe { RoomXY::unchecked_new(49, 49) };
assert_eq!(zero.saturating_add_direction(BottomRight), one);
assert_eq!(zero.saturating_add_direction(TopLeft), zero);
assert_eq!(one.saturating_add_direction(TopLeft), zero);
assert_eq!(forty_nine.saturating_add_direction(BottomRight), forty_nine);
Sourcepub fn neighbors(self) -> Vec<RoomXY>
pub fn neighbors(self) -> Vec<RoomXY>
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));
Trait Implementations§
Source§impl<'de> Deserialize<'de> for RoomXY
impl<'de> Deserialize<'de> for RoomXY
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl From<(RoomCoordinate, RoomCoordinate)> for RoomXY
impl From<(RoomCoordinate, RoomCoordinate)> for RoomXY
Source§fn from(xy: (RoomCoordinate, RoomCoordinate)) -> RoomXY
fn from(xy: (RoomCoordinate, RoomCoordinate)) -> RoomXY
Source§impl From<RoomXY> for (RoomCoordinate, RoomCoordinate)
impl From<RoomXY> for (RoomCoordinate, RoomCoordinate)
Source§fn from(xy: RoomXY) -> (RoomCoordinate, RoomCoordinate)
fn from(xy: RoomXY) -> (RoomCoordinate, RoomCoordinate)
Source§impl Index<RoomXY> for LocalCostMatrix
impl Index<RoomXY> for LocalCostMatrix
Source§impl IndexMut<RoomXY> for LocalCostMatrix
impl IndexMut<RoomXY> for LocalCostMatrix
Source§impl Ord for RoomXY
impl Ord for RoomXY
Source§impl PartialOrd for RoomXY
impl PartialOrd for RoomXY
Source§impl Sub for RoomXY
impl Sub for RoomXY
Source§fn sub(self, other: RoomXY) -> (i8, i8)
fn sub(self, other: RoomXY) -> (i8, i8)
Subtracts the other position from this one, extracting the difference as the output.
§Example
let pos1 = RoomXY::checked_new(40, 40).unwrap();
let pos2 = RoomXY::checked_new(0, 20).unwrap();
assert_eq!(pos1 - pos2, (40, 20));
let pos3 = RoomXY::checked_new(45, 45).unwrap();
assert_eq!(pos1 - pos3, (-5, -5));