[][src]Struct screeps::Position

#[repr(transparent)]
pub struct Position { /* fields omitted */ }

Represents a position in a particular room in Screeps.

Note: This is analagous to the RoomPosition JavaScript type.

We've renamed this type to Position in screeps-game-api to reflect the fact that it's implemented entirely as a local type, and does represent a position located within an entire shard, not only within a single room.

This should be a very efficient type to use in most if not all situations. It's represented by a single u32, all math operations are implemented in pure-Rust code, and uploading to / downloading from JavaScript only requires transferring a single i32.

Using Position

You can retrieve a Position by getting the position of a game object using HasPosition::pos, or by creating one from coordinates with Position::new.

You can use any of the math methods available on this page to manipulate Position, and you can pass it to any game methods expecting a position or something with a position.

Serialization

Position implements both serde::Serialize and serde::Deserialize.

When serializing, it will use the format {roomName: String, x: u32, y: u32} in "human readable" formats like JSON, and will serialize as a single i32 in "non-human readable" formats like bincode.

You can also pass Position into JavaScript using the js!{} macro provided by stdweb, or helper methods using the same code like MemoryReference::set. It will be serialized the same as in JSON, as an object with roomName, x and y properties.

Note: serializing using js!{} or MemoryReference::set will not create a JavaScript RoomPosition, only something with the same properties.

If you need a reference to a RoomPosition in JavaScript to use manually, you have two options:

  • Use .remote() to get a stdweb::Reference, and then use that reference in JavaScript

  • Convert the room position to an integer with Position::packed_repr, send that to JS, and use the pos_from_packed JavaScript function provided by this library:

    use stdweb::js;
    use screeps::Position;
    
    let pos = Position::new(20, 21, "E5N6".parse().unwrap());
    let result = js! {
        let pos = pos_from_packed(@{pos.packed_repr()});
        pos.roomName
    };

Methods

impl Position[src]

pub fn offset(&mut self, x: i32, y: i32)[src]

Returns a new position offset from this position by the specified x coords and y coords.

This function operates on world coordinates, and will wrap between rooms if necessary.

To return a new position rather than modifying in place, use pos + (x, y). See the implementation of Add<(i32, i32)> for Position further down on this page.

Panics

Will panic if the new position overflows the world. See Position::from_world_coords.

Example

use screeps::Position;

let e21s21 = "E21S21".parse().unwrap();
let e21s22 = "E21S22".parse().unwrap();

let mut pos = Position::new(21, 21, e21s21);
pos.offset(5, 5);
assert_eq!(pos, Position::new(26, 26, e21s21));

pos.offset(0, 49);
assert_eq!(pos, Position::new(26, 25, e21s22));

impl Position[src]

pub fn get_direction_to<T: ?Sized>(self, target: &T) -> Option<Direction> where
    T: HasPosition
[src]

pub fn get_range_to<T: ?Sized>(self, target: &T) -> u32 where
    T: HasPosition
[src]

pub fn in_range_to<T: ?Sized>(self, target: &T, range: u32) -> bool where
    T: HasPosition
[src]

pub fn is_equal_to<T: ?Sized>(self, target: &T) -> bool where
    T: HasPosition
[src]

pub fn is_near_to<T: ?Sized>(self, target: &T) -> bool where
    T: HasPosition
[src]

True if this position is in the same room as the target, and the range is at most 1.

impl Position[src]

pub fn create_construction_site(self, ty: StructureType) -> ReturnCode[src]

pub fn create_named_construction_site(
    self,
    ty: StructureType,
    name: &str
) -> ReturnCode
[src]

pub fn create_flag(
    self,
    name: &str,
    main_color: Color,
    secondary_color: Color
) -> Result<String, ReturnCode>
[src]

pub fn find_closest_by_range<T>(self, ty: T) -> Option<T::Item> where
    T: FindConstant
[src]

pub fn find_in_range<T>(self, ty: T, range: u32) -> Vec<T::Item> where
    T: FindConstant
[src]

pub fn find_path_to<'a, F, T: ?Sized>(
    self,
    target: &T,
    opts: FindOptions<'a, F>
) -> Path where
    F: Fn(RoomName, CostMatrix) -> Option<CostMatrix<'a>> + 'a,
    T: HasPosition
[src]

pub fn find_path_to_xy<'a, F>(
    self,
    x: u32,
    y: u32,
    opts: FindOptions<'a, F>
) -> Path where
    F: Fn(RoomName, CostMatrix) -> Option<CostMatrix<'a>> + 'a, 
[src]

pub fn look(self) -> Vec<LookResult>[src]

pub fn look_for<T>(self, ty: T) -> Vec<T::Item> where
    T: LookConstant
[src]

impl Position[src]

pub fn coords(&self) -> (u32, u32)[src]

Returns this position's in-room coordinates as a pair of unsigned integers.

pub fn coords_signed(&self) -> (i32, i32)[src]

Returns this position's in-room coordinates as a pair of signed integers.

impl Position[src]

pub fn world_x(self) -> i32[src]

Returns this position's horizontal "world coordinate".

The value is equal to 50 * room_x + x, where room_x is defined as room_x = -xx - 1 for Wxx rooms and as room_x = xx for Exx rooms.

pub fn world_y(self) -> i32[src]

Returns this position's vertical "world coordinate".

The value is equal to 50 * room_y + y, where room_y is defined as room_y = -yy - 1 for Nyy rooms and as room_y = yy for Syy rooms.

pub fn world_coords(self) -> (i32, i32)[src]

Returns this position's "world coordinates".

The first value is equal to 50 * room_x + x, where room_x is defined as room_x = -xx - 1 for Wxx rooms and as room_x = xx for Exx rooms.

The second value is equal to 50 * room_y + y, where room_y is defined as room_y = -yy - 1 for Nyy rooms and as room_y = yy for Syy rooms.

See also Position::world_x and Position::world_y.

pub fn from_world_coords(x: i32, y: i32) -> Self[src]

Creates a room position from world coords.

Panics

Panics if either x or y is out of the range -128 * 50 .. +128 * 50.

See Position::world_coords.

impl Position[src]

pub fn remote(self) -> Reference[src]

impl Position[src]

pub fn new(x: u32, y: u32, room_name: RoomName) -> Self[src]

Create a new Position

Panics

Will panic if either x or y is larger than 49, or if room_name is outside of the range E127N127 - W127S127.

pub fn packed_repr(self) -> i32[src]

pub fn from_packed(packed: i32) -> Self[src]

pub fn x(self) -> u32[src]

Gets this position's in-room x coordinate.

pub fn y(self) -> u32[src]

Gets this position's in-room y coordinate.

pub fn room_name(self) -> RoomName[src]

pub fn set_x(&mut self, x: u32)[src]

pub fn set_y(&mut self, y: u32)[src]

pub fn set_room_name(&mut self, room_name: RoomName)[src]

pub fn with_x(self, x: u32) -> Self[src]

pub fn with_y(self, y: u32) -> Self[src]

pub fn with_room_name(self, room_name: RoomName) -> Self[src]

Trait Implementations

impl HasPosition for Position[src]

impl FromExpectedType<Reference> for Position[src]

impl Clone for Position[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Into<(u8, u8)> for Position[src]

impl Into<(u16, u16)> for Position[src]

impl Into<(u32, u32)> for Position[src]

impl Into<(u64, u64)> for Position[src]

impl Into<(i8, i8)> for Position[src]

impl Into<(i16, i16)> for Position[src]

impl Into<(i32, i32)> for Position[src]

impl Into<(i64, i64)> for Position[src]

impl PartialEq<Position> for Position[src]

impl Copy for Position[src]

impl Eq for Position[src]

impl Display for Position[src]

impl Debug for Position[src]

impl Add<(i32, i32)> for Position[src]

type Output = Position

The resulting type after applying the + operator.

fn add(self, (x, y): (i32, i32)) -> Self[src]

Adds an (x, y) pair to this room position's world coordinates.

Will change rooms if necessary.

Panics

Will panic if the new position's room is outside bounds. See Position::from_world_coords.

Example

use screeps::Position;

let w5s6 = "W5S6".parse().unwrap();
let w5s5 = "W5S5".parse().unwrap();

let pos1 = Position::new(42, 42, w5s6);
let pos2 = pos1 + (7, 7);
assert_eq!(pos2, Position::new(49, 49, w5s6));

let pos3 = pos2 + (0, -59);
assert_eq!(pos3, Position::new(49, 40, w5s5));

let pos4 = pos3 - (49, 0);
assert_eq!(pos4, Position::new(0, 40, w5s5));

impl Sub<(i32, i32)> for Position[src]

type Output = Position

The resulting type after applying the - operator.

fn sub(self, (x, y): (i32, i32)) -> Self[src]

See the implementation of Add<(i32, i32)> for Position.

impl Sub<Position> for Position[src]

type Output = (i32, i32)

The resulting type after applying the - operator.

fn sub(self, other: Position) -> (i32, i32)[src]

Subtracts the other room position from this one, extracting the difference as the output.

Example

use screeps::Position;

let e5n5 = "E5N5".parse().unwrap();
let e5n6 = "E5N6".parse().unwrap();

let pos1 = Position::new(40, 40, e5n5);
let pos2 = Position::new(0, 20, e5n6);
assert_eq!(pos1 - pos2, (40, 70));

impl Hash for Position[src]

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl JsSerialize for Position

impl TryFrom<Value> for Position[src]

type Error = <Value as TryInto<String>>::Error

The type returned in the event of a conversion error.

impl TryFrom<Position> for Value

type Error = ConversionError

The type returned in the event of a conversion error.

impl<'_a> TryFrom<&'_a Position> for Value

type Error = ConversionError

The type returned in the event of a conversion error.

impl<'_a> TryFrom<&'_a mut Position> for Value

type Error = ConversionError

The type returned in the event of a conversion error.

impl JsSerializeOwned for Position

impl<'_r> JsSerializeOwned for &'_r Position

impl Serialize for Position[src]

impl<'de> Deserialize<'de> for Position[src]

Auto Trait Implementations

Blanket Implementations

impl<T> HasPosition for T where
    T: RoomObjectProperties
[src]

impl<T> FromExpectedType<Value> for T where
    T: FromExpectedType<Reference>, 
[src]

impl<T, U> IntoExpectedType<U> for T where
    U: FromExpectedType<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> DeserializeOwned for T where
    T: Deserialize<'de>, 
[src]