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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use std::{
    cmp::{Ord, Ordering, PartialOrd},
    error,
    fmt::{self, Write},
    ops,
    str::FromStr,
};

use arrayvec::ArrayString;

use super::{HALF_WORLD_SIZE, VALID_ROOM_NAME_COORDINATES};

/// A structure representing a room name.
///
/// # Ordering
///
/// To facilitate use as a key in a [`BTreeMap`] or other similar data
/// structures, `RoomName` implements [`PartialOrd`] and [`Ord`].
///
/// `RoomName`s are ordered first by y position, then by x position. North is
/// considered less than south, and west less than east.
///
/// The total ordering is `N127W127`, `N127W126`, `N127W125`, ..., `N127W0`,
/// `N127E0`, ..., `N127E127`, `N126W127`, ..., `S127E126`, `S127E127`.
///
/// This follows left-to-right reading order when looking at the Screeps map
/// from above.
///
/// [`BTreeMap`]: std::collections::BTreeMap
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct RoomName {
    /// A bit-packed integer, containing, from highest-order to lowest:
    ///
    /// - 1 byte: (room_x) + 128
    /// - 1 byte: (room_y) + 128
    ///
    /// For `Wxx` rooms, `room_x = -xx - 1`. For `Exx` rooms, `room_x = xx`.
    ///
    /// For `Nyy` rooms, `room_y = -yy - 1`. For `Syy` rooms, `room_y = yy`.
    ///
    /// This is the same representation of the upper 16 bits of [`Position`]'s
    /// packed representation.
    ///
    /// [`Position`]: crate::local::Position
    packed: u16,
}

impl fmt::Display for RoomName {
    /// Formats this room name into the format the game expects.
    ///
    /// Resulting string will be `(E|W)[0-9]+(N|S)[0-9]+`, and will result
    /// in the same RoomName if passed into [`RoomName::new`].
    ///
    /// [`RoomName::new`]: struct.RoomName.html#method.new
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let x_coord = self.x_coord();
        let y_coord = self.y_coord();

        if self.packed == 0 {
            write!(f, "sim")?;
        } else {
            if x_coord >= 0 {
                write!(f, "E{}", x_coord)?;
            } else {
                write!(f, "W{}", -x_coord - 1)?;
            }

            if y_coord >= 0 {
                write!(f, "S{}", y_coord)?;
            } else {
                write!(f, "N{}", -y_coord - 1)?;
            }
        }

        Ok(())
    }
}

impl RoomName {
    /// Parses a room name from a string.
    ///
    /// This will parse the input string, returning an error if it is in an
    /// invalid room name.
    ///
    /// The expected format can be represented by the regex
    /// `[ewEW][0-9]+[nsNS][0-9]+`.
    #[inline]
    pub fn new<T>(x: &T) -> Result<Self, RoomNameParseError>
    where
        T: AsRef<str> + ?Sized,
    {
        x.as_ref().parse()
    }

    #[inline]
    pub(crate) fn from_packed(packed: u16) -> Self {
        RoomName { packed }
    }

    /// Creates a new room name from room coords with direction implicit in
    /// sign.
    ///
    /// For `Wxx` rooms, `room_x = -xx - 1`. For `Exx` rooms, `room_x = xx`.
    ///
    /// For `Nyy` rooms, `room_y = -yy - 1`. For `Syy` rooms, `room_y = yy`.
    ///
    /// # Errors
    ///
    /// Returns an error if the coordinates are outside of the valid room name
    /// bounds.
    pub(super) fn from_coords(x_coord: i32, y_coord: i32) -> Result<Self, RoomNameParseError> {
        if !VALID_ROOM_NAME_COORDINATES.contains(&x_coord)
            || !VALID_ROOM_NAME_COORDINATES.contains(&y_coord)
        {
            return Err(RoomNameParseError::PositionOutOfBounds { x_coord, y_coord });
        }

        let room_x = (x_coord + HALF_WORLD_SIZE) as u16;
        let room_y = (y_coord + HALF_WORLD_SIZE) as u16;

        Ok(Self::from_packed((room_x << 8) | room_y))
    }

    /// Gets the x coordinate.
    ///
    /// For `Wxx` rooms, returns `-xx - 1`. For `Exx` rooms, returns `xx`.
    #[inline]
    pub(super) fn x_coord(&self) -> i32 {
        ((self.packed >> 8) & 0xFF) as i32 - HALF_WORLD_SIZE
    }

    /// Gets the y coordinate.
    ///
    /// For `Nyy` rooms, returns `-yy - 1`. For `Syy` rooms, returns `yy`.
    #[inline]
    pub(super) fn y_coord(&self) -> i32 {
        (self.packed & 0xFF) as i32 - HALF_WORLD_SIZE
    }

    #[inline]
    pub(super) fn packed_repr(&self) -> u16 {
        self.packed
    }

    /// Converts this RoomName into an efficient, stack-based string.
    ///
    /// This is equivalent to [`ToString::to_string`], but involves no
    /// allocation.
    pub fn to_array_string(&self) -> ArrayString<[u8; 8]> {
        let mut res = ArrayString::new();
        write!(res, "{}", self).expect("expected ArrayString write to be infallible");
        res
    }
}

impl ops::Add<(i32, i32)> for RoomName {
    type Output = Self;

    /// Offsets this room name by a given horizontal and vertical (x, y) pair.
    ///
    /// The first number offsets to the west when negative and to the east when
    /// positive. The first number offsets to the north when negative and to
    /// the south when positive.
    ///
    /// # Panics
    ///
    /// Will panic if the addition overflows the boundaries of RoomName.
    #[inline]
    fn add(self, (x, y): (i32, i32)) -> Self {
        RoomName::from_coords(self.x_coord() + x, self.y_coord() + y)
            .expect("expected addition to keep RoomName in-bounds")
    }
}

impl ops::Sub<(i32, i32)> for RoomName {
    type Output = Self;

    /// Offsets this room name in the opposite direction from the coordinates.
    ///
    /// See the implementation for `Add<(i32, i32)>`.
    ///
    /// # Panics
    ///
    /// Will panic if the subtraction overflows the boundaries of RoomName.
    #[inline]
    fn sub(self, (x, y): (i32, i32)) -> Self {
        RoomName::from_coords(self.x_coord() - x, self.y_coord() - y)
            .expect("expected addition to keep RoomName in-bounds")
    }
}

impl ops::Sub<RoomName> for RoomName {
    type Output = (i32, i32);

    /// Subtracts one room name from the other, extracting the difference.
    ///
    /// The first return value represents east/west offset, with 'more east'
    /// being positive and 'more west' being negative.
    ///
    /// The second return value represents north/south offset, with 'more south'
    /// being positive and 'more north' being negative.
    ///
    /// This coordinate system agrees with the implementations `Add<(i32, i32)>
    /// for RoomName` and `Sub<(i32, i32)> for RoomName`.
    #[inline]
    fn sub(self, other: RoomName) -> (i32, i32) {
        (
            self.x_coord() - other.x_coord(),
            self.y_coord() - other.y_coord(),
        )
    }
}

impl FromStr for RoomName {
    type Err = RoomNameParseError;

    fn from_str(s: &str) -> Result<Self, RoomNameParseError> {
        parse_to_coords(s)
            .map_err(|()| RoomNameParseError::new(s))
            .and_then(|(x, y)| RoomName::from_coords(x, y))
    }
}

fn parse_to_coords(s: &str) -> Result<(i32, i32), ()> {
    if s == "sim" {
        return Ok((-HALF_WORLD_SIZE, -HALF_WORLD_SIZE));
    }

    let mut chars = s.char_indices();

    let east = match chars.next() {
        Some((_, 'E')) | Some((_, 'e')) => true,
        Some((_, 'W')) | Some((_, 'w')) => false,
        _ => return Err(()),
    };

    let (x_coord, south): (i32, bool) = {
        // we assume there's at least one number character. If there isn't,
        // we'll catch it when we try to parse this substr.
        let (start_index, _) = chars.next().ok_or(())?;
        let end_index;
        let south;
        loop {
            match chars.next().ok_or(())? {
                (i, 'N') | (i, 'n') => {
                    end_index = i;
                    south = false;
                    break;
                }
                (i, 'S') | (i, 's') => {
                    end_index = i;
                    south = true;
                    break;
                }
                _ => continue,
            }
        }

        let x_coord = s[start_index..end_index].parse().map_err(|_| ())?;

        (x_coord, south)
    };

    let y_coord: i32 = {
        let (start_index, _) = chars.next().ok_or(())?;

        s[start_index..s.len()].parse().map_err(|_| ())?
    };

    let room_x = if east { x_coord } else { -x_coord - 1 };
    let room_y = if south { y_coord } else { -y_coord - 1 };

    Ok((room_x, room_y))
}

/// An error representing when a string can't be parsed into a
/// [`RoomName`].
///
/// [`RoomName`]: struct.RoomName.html
#[derive(Clone, Debug)]
pub enum RoomNameParseError {
    TooLarge { length: usize },
    InvalidString { string: ArrayString<[u8; 8]> },
    PositionOutOfBounds { x_coord: i32, y_coord: i32 },
}

impl RoomNameParseError {
    /// Private method to construct a `RoomNameParseError`.
    fn new(failed_room_name: &str) -> Self {
        match ArrayString::from(failed_room_name) {
            Ok(string) => RoomNameParseError::InvalidString { string },
            Err(_) => RoomNameParseError::TooLarge {
                length: failed_room_name.len(),
            },
        }
    }
}

impl error::Error for RoomNameParseError {}

impl fmt::Display for RoomNameParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RoomNameParseError::TooLarge { length } => write!(
                f,
                "got invalid room name, too large to stick in error. \
                 expected length 8 or less, got length {}",
                length
            ),
            RoomNameParseError::InvalidString { string } => write!(
                f,
                "expected room name formatted `[ewEW][0-9]+[nsNS][0-9]+`, found `{}`",
                string
            ),
            RoomNameParseError::PositionOutOfBounds { x_coord, y_coord } => write!(
                f,
                "expected room name with coords within -128..+128, found {}, {}",
                x_coord, y_coord,
            ),
        }
    }
}

impl PartialEq<str> for RoomName {
    fn eq(&self, other: &str) -> bool {
        let s = self.to_array_string();
        s.eq_ignore_ascii_case(other)
    }
}
impl PartialEq<RoomName> for str {
    #[inline]
    fn eq(&self, other: &RoomName) -> bool {
        // Explicitly call the impl for `PartialEq<str>` so that we don't end up
        // accidentally calling one of the other implementations and ending up in an
        // infinite loop.
        //
        // This one in particular would probably be OK, but I've written it this way to
        // be consistent with the others, and to ensure that if this code changes in
        // this future it'll stay working.
        <RoomName as PartialEq<str>>::eq(other, self)
    }
}

impl PartialEq<&str> for RoomName {
    #[inline]
    fn eq(&self, other: &&str) -> bool {
        <RoomName as PartialEq<str>>::eq(self, other)
    }
}

impl PartialEq<RoomName> for &str {
    #[inline]
    fn eq(&self, other: &RoomName) -> bool {
        <RoomName as PartialEq<str>>::eq(other, self)
    }
}

impl PartialEq<String> for RoomName {
    #[inline]
    fn eq(&self, other: &String) -> bool {
        <RoomName as PartialEq<str>>::eq(self, &other)
    }
}

impl PartialEq<RoomName> for String {
    #[inline]
    fn eq(&self, other: &RoomName) -> bool {
        <RoomName as PartialEq<str>>::eq(other, self)
    }
}

impl PartialEq<&String> for RoomName {
    #[inline]
    fn eq(&self, other: &&String) -> bool {
        <RoomName as PartialEq<str>>::eq(self, other)
    }
}

impl PartialEq<RoomName> for &String {
    #[inline]
    fn eq(&self, other: &RoomName) -> bool {
        <RoomName as PartialEq<str>>::eq(other, self)
    }
}

impl PartialOrd for RoomName {
    #[inline]
    fn partial_cmp(&self, other: &RoomName) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for RoomName {
    fn cmp(&self, other: &Self) -> Ordering {
        self.y_coord()
            .cmp(&other.y_coord())
            .then_with(|| self.x_coord().cmp(&other.x_coord()))
    }
}

mod serde {
    use std::fmt;

    use serde::{
        de::{Error, Unexpected, Visitor},
        Deserialize, Deserializer, Serialize, Serializer,
    };

    use super::RoomName;

    impl Serialize for RoomName {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            serializer.serialize_str(&self.to_array_string())
        }
    }

    struct RoomNameVisitor;

    impl<'de> Visitor<'de> for RoomNameVisitor {
        type Value = RoomName;

        fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            formatter.write_str(
                "room name formatted `(E|W)[0-9]+(N|S)[0-9]+` with both numbers within -128..128",
            )
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: Error,
        {
            v.parse()
                .map_err(|_| E::invalid_value(Unexpected::Str(v), &self))
        }
    }

    impl<'de> Deserialize<'de> for RoomName {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            deserializer.deserialize_str(RoomNameVisitor)
        }
    }

    js_deserializable!(RoomName);
    js_serializable!(RoomName);
}

#[cfg(test)]
mod test {
    #[test]
    fn test_string_equality() {
        use super::RoomName;
        let room_names = vec!["E21N4", "w6S42", "W17s5", "e2n5", "sim"];
        for room_name in room_names {
            assert_eq!(room_name, RoomName::new(room_name).unwrap());
            assert_eq!(RoomName::new(room_name).unwrap(), room_name);
            assert_eq!(RoomName::new(room_name).unwrap(), &room_name.to_string());
            assert_eq!(&room_name.to_string(), RoomName::new(room_name).unwrap());
        }
    }
}