1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use std::cmp::{Eq, PartialEq};

use stdweb::unstable::TryInto;

use {Color, Direction, FindConstant, HasPosition, LookConstant, ReturnCode, RoomPosition,
     StructureType};

impl RoomPosition {
    pub fn new(x: u8, y: u8, room_name: &str) -> Self {
        js_unwrap!(new RoomPosition(@{x}, @{y}, @{room_name}))
    }

    pub fn x(&self) -> u8 {
        js_unwrap!(@{&self.0}.x)
    }

    pub fn y(&self) -> u8 {
        js_unwrap!(@{&self.0}.y)
    }

    pub fn room_name(&self) -> String {
        js_unwrap!(@{&self.0}.roomName)
    }

    pub fn create_construction_site(&self, ty: StructureType) -> ReturnCode {
        js_unwrap!(@{&self.0}.createConstructionSite(__structure_type_num_to_str(@{ty as i32})))
    }

    pub fn create_flag(&self, name: &str, main_color: Color, secondary_color: Color) -> ReturnCode {
        // TODO: determine if ERR_NOT_IN_RANGE is the best choice here
        (js! {
            var flag = @{&self.0};
            if (flag.roomName in Game.rooms) {
                return flag.createFlag(@{name}, @{main_color as i32}, @{secondary_color as i32});
            } else {
                return ERR_NOT_IN_RANGE;
            }
        }).try_into()
            .expect("expected Flag.createFlag to return ReturnCode")
    }

    pub fn find_closest_by_range<T>(&self, ty: T) -> Vec<T::Item>
    where
        T: FindConstant,
    {
        js_unwrap_array!(@{&self.0}.findClosestByRange(
            __structure_type_num_to_str(@{ty.find_code()}
        )))
    }

    pub fn find_in_range<T>(&self, ty: T, range: i32) -> Vec<T::Item>
    where
        T: FindConstant,
    {
        js_unwrap_array!(@{&self.0}.findInRange(
            __structure_type_num_to_str(@{ty.find_code()}),
            @{range}
        ))
    }

    pub fn get_direction_to<T>(&self, target: &T) -> Direction
    where
        T: HasPosition,
    {
        js_unwrap!(@{&self.0}.getDirectionTo(@{&target.pos().0}))
    }

    pub fn get_range_to<T>(&self, target: &T) -> i32
    where
        T: HasPosition,
    {
        js_unwrap!(@{&self.0}.getRangeTo(@{&target.pos().0}))
    }

    pub fn in_range_to<T>(&self, target: &T, range: i32) -> bool
    where
        T: HasPosition,
    {
        js_unwrap!(@{&self.0}.inRangeTo(@{&target.pos().0}, @{range}))
    }

    pub fn is_equal_to<T>(&self, target: &T) -> bool
    where
        T: HasPosition,
    {
        js_unwrap!(@{&self.0}.isEqualTo(@{&target.pos().0}))
    }

    pub fn is_near_to<T>(&self, target: &T) -> bool
    where
        T: HasPosition,
    {
        js_unwrap!(@{&self.0}.isNearTo(@{&target.pos().0}))
    }

    pub fn look_for<T, U>(&self, ty: T) -> Vec<T::Item>
    where
        T: LookConstant,
        U: HasPosition,
    {
        js_unwrap_array!(@{&self.0}.lookFor(__look_num_to_str(@{ty.look_code() as i32})))
    }
}

impl<T: HasPosition> PartialEq<T> for RoomPosition {
    fn eq(&self, other: &T) -> bool {
        (js!{
            var a = @{&self.0};
            var b = @{&other.pos().0};
            return a.x == b.x && a.y == b.y && a.roomName == b.roomName;
        }).try_into()
            .expect("expected a boolean to be a boolean")
    }
}

impl Eq for RoomPosition {}