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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use std::convert::TryInto;

use js_sys::{Array, JsString, Object};
use num_traits::*;
use wasm_bindgen::{prelude::*, JsCast};

use crate::{
    constants::{find::*, look::*, Color, Direction, ErrorCode, ReturnCode, StructureType},
    local::{Position, RoomCoordinate, RoomName},
    objects::{CostMatrix, FindPathOptions, Path},
    pathfinder::RoomCostResult,
    prelude::*,
    prototypes::ROOM_POSITION_PROTOTYPE,
};

#[wasm_bindgen]
extern "C" {
    /// An object representing a position in a room.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition)
    pub type RoomPosition;

    #[wasm_bindgen(constructor)]
    fn new_internal(x: u8, y: u8, room_name: &JsString) -> RoomPosition;

    #[wasm_bindgen(method, getter = roomName)]
    fn room_name_internal(this: &RoomPosition) -> JsString;

    /// Change the room the position refers to; must be a valid room name.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.roomName)
    #[wasm_bindgen(method, setter = roomName)]
    pub fn set_room_name(this: &RoomPosition) -> JsString;

    /// X coordinate of the position within the room.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.x)
    #[wasm_bindgen(method, getter)]
    pub fn x(this: &RoomPosition) -> u8;

    /// Set a new X coordinate; must be in the range 0..49.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.x)
    #[wasm_bindgen(method, setter)]
    pub fn set_x(this: &RoomPosition) -> u8;

    /// Y coordinate of the position within the room.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.y)
    #[wasm_bindgen(method, getter)]
    pub fn y(this: &RoomPosition) -> u8;

    /// Set a new Y coordinate; must be in the range 0..49.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.y)
    #[wasm_bindgen(method, setter)]
    pub fn set_y(this: &RoomPosition) -> u8;

    // todo, get this as native
    /// Gets the efficient internal i32 representation of the position.
    #[wasm_bindgen(method, getter = __packedPos)]
    pub fn packed(this: &RoomPosition) -> u32;

    // todo, as native
    /// Sets the position to a new one by passing an i32 that represents a
    /// packed position.
    #[wasm_bindgen(method, setter = __packedPos)]
    pub fn set_packed(this: &RoomPosition, val: u32);

    /// Creates a [`ConstructionSite`] at this position. If it's a
    /// [`StructureSpawn`], a name can optionally be assigned for the structure.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.createConstructionSite)
    ///
    /// [`ConstructionSite`]: crate::objects::ConstructionSite
    #[wasm_bindgen(method, js_name = createConstructionSite)]
    pub fn create_construction_site(
        this: &RoomPosition,
        ty: StructureType,
        name: Option<&JsString>,
    ) -> ReturnCode;

    #[wasm_bindgen(method, catch, js_name = createFlag)]
    fn create_flag_internal(
        this: &RoomPosition,
        name: Option<&JsString>,
        color: Option<Color>,
        secondary_color: Option<Color>,
    ) -> Result<JsValue, JsValue>;

    // todo FindOptions
    #[wasm_bindgen(method, js_name = findClosestByPath)]
    fn find_closest_by_path_internal(
        this: &RoomPosition,
        goal: Find,
        options: Option<&Object>,
    ) -> Option<Object>;

    // todo FindOptions
    #[wasm_bindgen(method, js_name = findClosestByRange)]
    fn find_closest_by_range_internal(
        this: &RoomPosition,
        goal: Find,
        options: Option<&Object>,
    ) -> Option<Object>;

    // todo FindOptions
    #[wasm_bindgen(method, js_name = findInRange)]
    fn find_in_range_internal(
        this: &RoomPosition,
        goal: Find,
        range: u8,
        options: Option<&Object>,
    ) -> Option<Array>;

    #[wasm_bindgen(method, js_name = findPathTo)]
    fn find_path_to_internal(
        this: &RoomPosition,
        target: &JsValue,
        options: Option<&Object>,
    ) -> JsValue;

    #[wasm_bindgen(method, js_name = findPathTo)]
    fn find_path_to_xy_internal(
        this: &RoomPosition,
        x: u8,
        y: u8,
        options: Option<&Object>,
    ) -> JsValue;

    /// Get the [`Direction`] toward a position or room object.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.getDirectionTo)
    #[wasm_bindgen(method, js_name = getDirectionTo)]
    pub fn get_direction_to(this: &RoomPosition, goal: &JsValue) -> Direction;

    /// Get the [`Direction`] toward the given coordinates in the same room.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.getDirectionTo)
    #[wasm_bindgen(method, js_name = getDirectionTo)]
    pub fn get_direction_to_xy(this: &RoomPosition, x: u8, y: u8) -> Direction;

    /// Get the range to a position or room object in the same room.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.getRangeTo)
    #[wasm_bindgen(method, js_name = getRangeTo)]
    pub fn get_range_to(this: &RoomPosition, goal: &JsValue) -> u32;

    /// Get the range to the given coordinates in the same room.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.getRangeTo)
    #[wasm_bindgen(method, js_name = getRangeTo)]
    pub fn get_range_to_xy(this: &RoomPosition, x: u8, y: u8) -> u32;

    /// Get the range to a position or room object in the same room.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.inRangeTo)
    #[wasm_bindgen(method, js_name = inRangeTo)]
    pub fn in_range_to(this: &RoomPosition, goal: &JsValue, range: u8) -> bool;

    /// Get the range to the given coordinates in the same room.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.inRangeTo)
    #[wasm_bindgen(method, js_name = inRangeTo)]
    pub fn in_range_to_xy(this: &RoomPosition, x: u8, y: u8, range: u8) -> bool;

    /// Determine whether this position is at the same position as another
    /// position or room object.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.isEqualTo)
    #[wasm_bindgen(method, js_name = isEqualTo)]
    pub fn is_equal_to(this: &RoomPosition, goal: &JsValue) -> bool;

    /// Determine whether this position is at the given coordinates in the room.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.isEqualTo)
    #[wasm_bindgen(method, js_name = isEqualTo)]
    pub fn is_equal_to_xy(this: &RoomPosition, x: u8, y: u8) -> bool;

    /// Determine whether this position is within 1 range of another position or
    /// room object.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.isNearTo)
    #[wasm_bindgen(method, js_name = isNearTo)]
    pub fn is_near_to(this: &RoomPosition, goal: &JsValue) -> bool;

    /// Determine whether this position is within 1 range of the given
    /// coordinates in the room.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.isNearTo)
    #[wasm_bindgen(method, js_name = isNearTo)]
    pub fn is_near_to_xy(this: &RoomPosition, x: u8, y: u8) -> bool;

    #[wasm_bindgen(method, js_name = look)]
    fn look_internal(this: &RoomPosition) -> Array;

    #[wasm_bindgen(method, js_name = lookFor)]
    fn look_for_internal(this: &RoomPosition, ty: Look) -> Option<Array>;
}

impl RoomPosition {
    /// Create a new RoomPosition using the normal constructor, taking
    /// coordinates and the room name.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.constructor)
    pub fn new(x: u8, y: u8, room_name: RoomName) -> RoomPosition {
        let room_name = room_name.into();

        Self::new_internal(x, y, &room_name)
    }

    /// Name of the room the position is in, as an owned [`JsString`] reference
    /// to a string in Javascript memory.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.roomName)
    pub fn room_name(&self) -> RoomName {
        Self::room_name_internal(self)
            .try_into()
            .expect("expected parseable room name")
    }

    /// Creates a [`Flag`] at this position. If successful, returns the name of
    /// the created flag.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.createFlag)
    ///
    /// [`Flag`]: crate::objects::Flag
    pub fn create_flag(
        &self,
        name: Option<&JsString>,
        color: Option<Color>,
        secondary_color: Option<Color>,
    ) -> Result<JsString, ErrorCode> {
        match self.create_flag_internal(name, color, secondary_color) {
            Ok(result) => {
                if result.is_string() {
                    Ok(result.unchecked_into())
                } else {
                    Err(ErrorCode::from_f64(
                        result
                            .as_f64()
                            .expect("expected non-string flag return to be a number"),
                    )
                    .expect("expected valid error code"))
                }
            }
            Err(_) => {
                // js code threw an exception; we're going to assume it's a non-visible room.
                Err(ErrorCode::NotInRange)
            }
        }
    }

    // todo typed options and version that allows passing target roomobjects
    /// Find the closest object by path among a list of objects, or use
    /// a [`find` constant] to search for all objects of that type in the room.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.findClosestByPath)
    ///
    /// [`find` constant]: crate::constants::find
    pub fn find_closest_by_path<T>(&self, find: T, options: Option<&Object>) -> Option<T::Item>
    where
        T: FindConstant,
    {
        self.find_closest_by_path_internal(find.find_code(), options)
            .map(|reference| T::convert_and_check_item(reference.into()))
    }

    // todo version for passing target roomobjects
    /// Find the closest object by range among a list of objects, or use
    /// a [`find` constant] to search for all objects of that type in the room.
    /// Will not work for objects in other rooms.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.findClosestByRange)
    ///
    /// [`find` constant]: crate::constants::find
    pub fn find_closest_by_range<T>(&self, find: T) -> Option<T::Item>
    where
        T: FindConstant,
    {
        self.find_closest_by_range_internal(find.find_code(), None)
            .map(|reference| T::convert_and_check_item(reference.into()))
    }

    // todo version for passing target roomobjects
    /// Find all relevant objects within a certain range among a list of
    /// objects, or use a [`find` constant] to search all objects of that type
    /// in the room.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.findInRange)
    ///
    /// [`find` constant]: crate::constants::find
    pub fn find_in_range<T>(&self, find: T, range: u8) -> Vec<T::Item>
    where
        T: FindConstant,
    {
        self.find_in_range_internal(find.find_code(), range, None)
            .map(|arr| arr.iter().map(T::convert_and_check_item).collect())
            .unwrap_or_else(Vec::new)
    }

    /// Find a path from this position to a position or room object, with an
    /// optional options object
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.findPathTo)
    pub fn find_path_to<T, F, R>(&self, target: &T, options: Option<FindPathOptions<F, R>>) -> Path
    where
        T: HasPosition,
        F: FnMut(RoomName, CostMatrix) -> R,
        R: RoomCostResult,
    {
        let target: RoomPosition = target.pos().into();

        if let Some(options) = options {
            options.into_js_options(|js_options| {
                serde_wasm_bindgen::from_value(
                    self.find_path_to_internal(&target, Some(js_options.unchecked_ref())),
                )
                .expect("invalid path from RoomPosition.findPathTo")
            })
        } else {
            serde_wasm_bindgen::from_value(self.find_path_to_internal(&target, None))
                .expect("invalid path from RoomPosition.findPathTo")
        }
    }

    /// Find a path from this position to the given coordinates in the same
    /// room, with an optional options object.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.findPathTo)
    pub fn find_path_to_xy<F, R>(
        &self,
        x: RoomCoordinate,
        y: RoomCoordinate,
        options: Option<FindPathOptions<F, R>>,
    ) -> Path
    where
        F: FnMut(RoomName, CostMatrix) -> R,
        R: RoomCostResult,
    {
        if let Some(options) = options {
            options.into_js_options(|js_options| {
                serde_wasm_bindgen::from_value(self.find_path_to_xy_internal(
                    x.into(),
                    y.into(),
                    Some(js_options.unchecked_ref()),
                ))
                .expect("invalid path from RoomPosition.findPathTo")
            })
        } else {
            serde_wasm_bindgen::from_value(self.find_path_to_xy_internal(x.into(), y.into(), None))
                .expect("invalid path from RoomPosition.findPathTo")
        }
    }

    /// Get all objects at this position.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.look)
    pub fn look(&self) -> Vec<LookResult> {
        self.look_internal()
            .iter()
            .map(LookResult::from_jsvalue_unknown_type)
            .collect()
    }

    /// Get all objects of a given type at this position, if any.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#RoomPosition.lookFor)
    pub fn look_for<T>(&self, _ty: T) -> Vec<T::Item>
    where
        T: LookConstant,
    {
        self.look_for_internal(T::look_code())
            .map(|arr| arr.iter().map(T::convert_and_check_item).collect())
            .unwrap_or_else(Vec::new)
    }
}

impl Clone for RoomPosition {
    fn clone(&self) -> Self {
        let new_pos = RoomPosition::from(JsValue::from(Object::create(&ROOM_POSITION_PROTOTYPE)));
        new_pos.set_packed(self.packed());
        new_pos
    }
}

impl HasPosition for RoomPosition {
    fn pos(&self) -> Position {
        self.into()
    }
}

impl From<Position> for RoomPosition {
    fn from(pos: Position) -> Self {
        let js_pos = RoomPosition::from(JsValue::from(Object::create(&ROOM_POSITION_PROTOTYPE)));
        js_pos.set_packed(pos.packed_repr());
        js_pos
    }
}

impl From<&Position> for RoomPosition {
    fn from(pos: &Position) -> Self {
        let js_pos = RoomPosition::from(JsValue::from(Object::create(&ROOM_POSITION_PROTOTYPE)));
        js_pos.set_packed(pos.packed_repr());
        js_pos
    }
}