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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
use std::{fmt, marker::PhantomData, mem, ops::Range};

use num_traits::FromPrimitive;
use scoped_tls::scoped_thread_local;
use serde::{
    self,
    de::{self, Deserializer, MapAccess, Visitor},
    Deserialize, Serialize,
};
use serde_json;
use serde_repr::{Deserialize_repr, Serialize_repr};
use stdweb::{Reference, Value};

use crate::{
    constants::{
        Color, Direction, EffectType, ExitDirection, FindConstant, Look, LookConstant, PowerType,
        ResourceType, ReturnCode, StructureType, Terrain,
    },
    local::{Position, RoomName},
    memory::MemoryReference,
    objects::{
        ConstructionSite, Creep, Deposit, Flag, HasPosition, Mineral, Nuke, PowerCreep, Resource,
        Room, RoomTerrain, RoomVisual, Ruin, Source, Structure, StructureController,
        StructureStorage, StructureTerminal, Tombstone,
    },
    pathfinder::CostMatrix,
    traits::{TryFrom, TryInto},
    ConversionError,
};

simple_accessors! {
    impl Room {
        pub fn controller() -> Option<StructureController> = controller;
        pub fn energy_available() -> u32 = energyAvailable;
        pub fn energy_capacity_available() -> u32 = energyCapacityAvailable;
        pub fn name() -> RoomName = name;
        pub fn storage() -> Option<StructureStorage> = storage;
        pub fn terminal() -> Option<StructureTerminal> = terminal;
    }
}

scoped_thread_local!(static COST_CALLBACK: &'static dyn Fn(RoomName, Reference) -> Option<Reference>);

impl Room {
    pub fn serialize_path(&self, path: &[Step]) -> String {
        js_unwrap! {@{self.as_ref()}.serializePath(@{path})}
    }

    pub fn deserialize_path(&self, path: &str) -> Vec<Step> {
        js_unwrap! {@{self.as_ref()}.deserializePath(@{path})}
    }

    pub fn create_construction_site<T>(&self, at: &T, ty: StructureType) -> ReturnCode
    where
        T: ?Sized + HasPosition,
    {
        let pos = at.pos();
        js_unwrap!(@{self.as_ref()}.createConstructionSite(
            pos_from_packed(@{pos.packed_repr()}),
            __structure_type_num_to_str(@{ty as u32})
        ))
    }

    pub fn create_named_construction_site<T>(
        &self,
        at: &T,
        ty: StructureType,
        name: &str,
    ) -> ReturnCode
    where
        T: ?Sized + HasPosition,
    {
        let pos = at.pos();
        js_unwrap!(@{self.as_ref()}.createConstructionSite(
            // pos_from_packed(@{pos.packed_repr()}),
            // workaround - passing with a position and a name
            // currently broken, use x,y instead
            @{pos.x()},
            @{pos.y()},
            __structure_type_num_to_str(@{ty as u32}),
            @{name}
        ))
    }

    pub fn create_flag<T>(
        &self,
        at: &T,
        name: &str,
        main_color: Color,
        secondary_color: Color,
    ) -> Result<String, ReturnCode>
    where
        T: ?Sized + HasPosition,
    {
        let pos = at.pos();
        Flag::interpret_creation_ret_value(js! {
            return @{self.as_ref()}.createFlag(
                pos_from_packed(@{pos.packed_repr()}),
                @{name},
                @{main_color as u32},
                @{secondary_color as u32}
            );
        })
        .expect("expected Room.createFlag to return ReturnCode or String name")
    }

    pub fn find<T>(&self, ty: T) -> Vec<T::Item>
    where
        T: FindConstant,
    {
        js_unwrap_ref!(@{self.as_ref()}.find(@{ty.find_code()}))
    }

    pub fn find_exit_to(&self, room: &Room) -> Result<ExitDirection, ReturnCode> {
        let code_val = js! {return @{self.as_ref()}.findExitTo(@{room.as_ref()});};
        let code_int: i32 = code_val.try_into().unwrap();

        if code_int < 0 {
            Err(ReturnCode::from_i32(code_int)
                .expect("expected find_exit_to return value < 0 to be a valid ReturnCode"))
        } else {
            Ok(ExitDirection::from_i32(code_int)
                .expect("expected find_exit_to return value >= 0 to be a valid Exit"))
        }
    }

    pub fn get_event_log(&self) -> Vec<Event> {
        serde_json::from_str(&self.get_event_log_raw()).expect("Malformed Event Log")
    }

    pub fn get_event_log_raw(&self) -> String {
        js_unwrap! {@{self.as_ref()}.getEventLog(true)}
    }

    pub fn get_position_at(&self, x: u32, y: u32) -> Option<Position> {
        let v = js! {
            let value = @{self.as_ref()}.getPositionAt(@{x}, @{y});
            if (value == null) {
                return null;
            } else {
                return value.__packedPos;
            }
        };
        match v {
            Value::Number(_) => Some(
                v.try_into()
                    .expect("expected Position::try_from(pos.__packedPos) to succeed"),
            ),
            Value::Null => None,
            _ => panic!(
                "unexpected return value for JS binding to Room.getPositionAt. \
                 expected null or number, found {:?}",
                v
            ),
        }
    }

    pub fn get_terrain(&self) -> RoomTerrain {
        js_unwrap!(@{self.as_ref()}.getTerrain())
    }

    pub fn look_at<T: ?Sized + HasPosition>(&self, target: &T) -> Vec<LookResult> {
        let pos = target.pos();
        js_unwrap!(@{self.as_ref()}.lookAt(pos_from_packed(@{pos.packed_repr()})))
    }

    pub fn look_at_xy(&self, x: u32, y: u32) -> Vec<LookResult> {
        js_unwrap!(@{self.as_ref()}.lookAt(@{x}, @{y}))
    }

    pub fn look_at_area(
        &self,
        top: u32,
        left: u32,
        bottom: u32,
        right: u32,
    ) -> Vec<PositionedLookResult> {
        js_unwrap!(@{self.as_ref()}.lookAtArea(@{top}, @{left}, @{bottom}, @{right}, true))
    }

    pub fn find_path<'a, O, T, F>(&self, from_pos: &O, to_pos: &T, opts: FindOptions<'a, F>) -> Path
    where
        O: ?Sized + HasPosition,
        T: ?Sized + HasPosition,
        F: Fn(RoomName, CostMatrix<'_>) -> Option<CostMatrix<'a>> + 'a,
    {
        let from = from_pos.pos();
        let to = to_pos.pos();

        // This callback is the one actually passed to JavaScript.
        fn callback(room_name: String, cost_matrix: Reference) -> Option<Reference> {
            let room_name = room_name.parse().expect(
                "expected room name passed into Room.findPath \
                 callback to be a valid room name",
            );
            COST_CALLBACK.with(|callback| callback(room_name, cost_matrix))
        }

        // User provided callback: rust String, CostMatrix -> Option<CostMatrix>
        let raw_callback = opts.cost_callback;

        // Wrapped user callback: rust String, Reference -> Option<Reference>
        let callback_boxed = move |room_name, cost_matrix_ref| {
            let cmatrix = CostMatrix {
                inner: cost_matrix_ref,
                lifetime: PhantomData,
            };
            raw_callback(room_name, cmatrix).map(|cm| cm.inner)
        };

        // Type erased and boxed callback: no longer a type specific to the closure
        // passed in, now unified as &Fn
        let callback_type_erased: &(dyn Fn(RoomName, Reference) -> Option<Reference> + 'a) =
            &callback_boxed;

        // Overwrite lifetime of reference so it can be stuck in scoped_thread_local
        // storage: it's now pretending to be static data. This should be entirely safe
        // because we're only sticking it in scoped storage and we control the
        // only use of it, but it's still necessary because "some lifetime above
        // the  current scope but otherwise unknown" is not a valid lifetime to
        // have PF_CALLBACK have.
        let callback_lifetime_erased: &'static dyn Fn(RoomName, Reference) -> Option<Reference> =
            unsafe { mem::transmute(callback_type_erased) };

        let FindOptions {
            ignore_creeps,
            ignore_destructible_structures,
            max_ops,
            heuristic_weight,
            serialize,
            max_rooms,
            range,
            plain_cost,
            swamp_cost,
            ..
        } = opts;

        // Store callback_lifetime_erased in COST_CALLBACK for the duration of the
        // PathFinder call and make the call to PathFinder.
        //
        // See https://docs.rs/scoped-tls/0.1/scoped_tls/
        COST_CALLBACK.set(&callback_lifetime_erased, || {
            let v = js! {
                return @{&self.as_ref()}.findPath(
                    pos_from_packed(@{from.packed_repr()}),
                    pos_from_packed(@{to.packed_repr()}),
                    {
                        ignoreCreeps: @{ignore_creeps},
                        ignoreDestructibleStructures: @{ignore_destructible_structures},
                        costCallback: @{callback},
                        maxOps: @{max_ops},
                        heuristicWeight: @{heuristic_weight},
                        serialize: @{serialize},
                        maxRooms: @{max_rooms},
                        range: @{range},
                        plainCost: @{plain_cost},
                        swampCost: @{swamp_cost}
                    }
                );
            };
            if serialize {
                Path::Serialized(v.try_into().unwrap())
            } else {
                Path::Vectorized(v.try_into().unwrap())
            }
        })
    }

    pub fn look_for_at<T, U>(&self, ty: T, target: &U) -> Vec<T::Item>
    where
        T: LookConstant,
        U: HasPosition,
    {
        let pos = target.pos();
        T::convert_and_check_items(js_unwrap!(@{self.as_ref()}.lookForAt(
            __look_num_to_str(@{ty.look_code() as u32}),
            pos_from_packed(@{pos.packed_repr()}),
        )))
    }

    pub fn look_for_at_xy<T>(&self, ty: T, x: u32, y: u32) -> Vec<T::Item>
    where
        T: LookConstant,
    {
        T::convert_and_check_items(js_unwrap!(@{self.as_ref()}.lookForAt(
            __look_num_to_str(@{ty.look_code() as u32}),
            @{x},
            @{y},
        )))
    }

    /// Looks for a given thing over a given area of bounds.
    ///
    /// To keep with `Range` convention, the start is inclusive, and the end
    /// is _exclusive_.
    ///
    /// Note: to ease the implementation and efficiency of the rust interface,
    /// this is limited to returning an array of values without their
    /// positions. If position data is needed, all room objects *should*
    /// contain positions alongside them. (for terrain data, I would recommend
    /// using a different method?)
    ///
    /// If you really do need more information here, I would recommend making a
    /// PR to add it!
    ///
    /// # Panics
    ///
    /// Panics if start>end for either range, or if end>50 for either range.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # let room: ::screeps::Room = unimplemented!();
    /// use screeps::constants::look;
    /// room.look_for_at_area(look::ENERGY, 20..26, 20..26);
    /// ```
    pub fn look_for_at_area<T>(&self, ty: T, horiz: Range<u8>, vert: Range<u8>) -> Vec<T::Item>
    where
        T: LookConstant,
    {
        assert!(horiz.start <= horiz.end);
        assert!(vert.start <= vert.end);
        assert!(horiz.end <= 50);
        assert!(vert.end <= 50);

        T::convert_and_check_items(js_unwrap! {@{self.as_ref()}.lookForAtArea(
            __look_num_to_str(@{ty.look_code() as u32}),
            @{vert.start},
            @{horiz.start},
            @{vert.end},
            @{horiz.end},
            true
        ).map((obj) => obj[__look_num_to_str(@{ty.look_code() as u32})])})
    }

    pub fn memory(&self) -> MemoryReference {
        js_unwrap!(@{self.as_ref()}.memory)
    }

    pub fn name_local(&self) -> RoomName {
        js_unwrap!(@{self.as_ref()}.name)
    }

    pub fn visual(&self) -> RoomVisual {
        RoomVisual::new(Some(self.name()))
    }
}

impl PartialEq for Room {
    fn eq(&self, other: &Room) -> bool {
        self.name() == other.name()
    }
}

impl Eq for Room {}

pub struct FindOptions<'a, F>
where
    F: Fn(RoomName, CostMatrix<'_>) -> Option<CostMatrix<'a>>,
{
    pub(crate) ignore_creeps: bool,
    pub(crate) ignore_destructible_structures: bool,
    pub(crate) cost_callback: F,
    pub(crate) max_ops: u32,
    pub(crate) heuristic_weight: f64,
    pub(crate) serialize: bool,
    pub(crate) max_rooms: u32,
    pub(crate) range: u32,
    pub(crate) plain_cost: u8,
    pub(crate) swamp_cost: u8,
}

impl Default for FindOptions<'static, fn(RoomName, CostMatrix<'_>) -> Option<CostMatrix<'static>>> {
    fn default() -> Self {
        fn cost_callback(_: RoomName, _: CostMatrix<'_>) -> Option<CostMatrix<'static>> {
            None
        }

        // TODO: should we fall back onto the game's default values, or is
        // it alright to copy them here?
        FindOptions {
            ignore_creeps: false,
            ignore_destructible_structures: false,
            cost_callback,
            max_ops: 2000,
            heuristic_weight: 1.2,
            serialize: false,
            max_rooms: 16,
            range: 0,
            plain_cost: 1,
            swamp_cost: 5,
        }
    }
}

impl FindOptions<'static, fn(RoomName, CostMatrix<'_>) -> Option<CostMatrix<'static>>> {
    /// Creates default SearchOptions
    pub fn new() -> Self {
        Self::default()
    }
}

impl<'a, F> FindOptions<'a, F>
where
    F: Fn(RoomName, CostMatrix<'_>) -> Option<CostMatrix<'a>>,
{
    /// Sets whether the algorithm considers creeps as walkable. Default: False.
    pub fn ignore_creeps(mut self, ignore: bool) -> Self {
        self.ignore_creeps = ignore;
        self
    }

    /// Sets whether the algorithm considers destructible structure as
    /// walkable. Default: False.
    pub fn ignore_destructible_structures(mut self, ignore: bool) -> Self {
        self.ignore_destructible_structures = ignore;
        self
    }

    /// Sets cost callback - default `|_, _| {}`.
    pub fn cost_callback<'b, F2>(self, cost_callback: F2) -> FindOptions<'b, F2>
    where
        F2: Fn(RoomName, CostMatrix<'_>) -> Option<CostMatrix<'b>>,
    {
        let FindOptions {
            ignore_creeps,
            ignore_destructible_structures,
            cost_callback: _,
            max_ops,
            heuristic_weight,
            serialize,
            max_rooms,
            range,
            plain_cost,
            swamp_cost,
        } = self;
        FindOptions {
            ignore_creeps,
            ignore_destructible_structures,
            cost_callback,
            max_ops,
            heuristic_weight,
            serialize,
            max_rooms,
            range,
            plain_cost,
            swamp_cost,
        }
    }

    /// Sets maximum ops - default `2000`.
    pub fn max_ops(mut self, ops: u32) -> Self {
        self.max_ops = ops;
        self
    }

    /// Sets heuristic weight - default `1.2`.
    pub fn heuristic_weight(mut self, weight: f64) -> Self {
        self.heuristic_weight = weight;
        self
    }

    /// Sets whether the returned path should be passed to `Room.serializePath`.
    pub fn serialize(mut self, s: bool) -> Self {
        self.serialize = s;
        self
    }

    /// Sets maximum rooms - default `16`, max `16`.
    pub fn max_rooms(mut self, rooms: u32) -> Self {
        self.max_rooms = rooms;
        self
    }

    pub fn range(mut self, k: u32) -> Self {
        self.range = k;
        self
    }

    /// Sets plain cost - default `1`.
    pub fn plain_cost(mut self, cost: u8) -> Self {
        self.plain_cost = cost;
        self
    }

    /// Sets swamp cost - default `5`.
    pub fn swamp_cost(mut self, cost: u8) -> Self {
        self.swamp_cost = cost;
        self
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Step {
    pub x: u32,
    pub y: u32,
    pub dx: i32,
    pub dy: i32,
    pub direction: Direction,
}

js_deserializable! {Step}
js_serializable! {Step}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum Path {
    Vectorized(Vec<Step>),
    Serialized(String),
}

js_deserializable! {Path}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Event {
    pub event: EventType,
    pub object_id: String,
}

impl<'de> Deserialize<'de> for Event {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(field_identifier, rename_all = "camelCase")]
        enum Field {
            Event,
            ObjectId,
            Data,
        };

        struct EventVisitor;

        impl<'de> Visitor<'de> for EventVisitor {
            type Value = Event;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str("struct Event")
            }

            fn visit_map<V>(self, mut map: V) -> Result<Event, V::Error>
            where
                V: MapAccess<'de>,
            {
                let mut event_type = None;
                let mut obj_id = None;
                let mut data = None;
                let mut data_buffer: Option<serde_json::Value> = None;

                while let Some(key) = map.next_key()? {
                    match key {
                        Field::Event => {
                            if event_type.is_some() {
                                return Err(de::Error::duplicate_field("event"));
                            }
                            event_type = Some(map.next_value()?);
                        }
                        Field::ObjectId => {
                            if obj_id.is_some() {
                                return Err(de::Error::duplicate_field("objectId"));
                            }
                            obj_id = Some(map.next_value()?);
                        }
                        Field::Data => {
                            if data.is_some() {
                                return Err(de::Error::duplicate_field("data"));
                            }

                            match event_type {
                                None => data_buffer = map.next_value()?,
                                Some(event_id) => {
                                    data = match event_id {
                                        1 => Some(EventType::Attack(map.next_value()?)),
                                        2 => Some(EventType::ObjectDestroyed(map.next_value()?)),
                                        3 => Some(EventType::AttackController),
                                        4 => Some(EventType::Build(map.next_value()?)),
                                        5 => Some(EventType::Harvest(map.next_value()?)),
                                        6 => Some(EventType::Heal(map.next_value()?)),
                                        7 => Some(EventType::Repair(map.next_value()?)),
                                        8 => Some(EventType::ReserveController(map.next_value()?)),
                                        9 => Some(EventType::UpgradeController(map.next_value()?)),
                                        10 => Some(EventType::Exit(map.next_value()?)),
                                        _ => {
                                            return Err(de::Error::custom(format!(
                                                "Event Type Unrecognized: {}",
                                                event_id
                                            )));
                                        }
                                    };
                                }
                            };
                        }
                    }
                }

                if data.is_none() {
                    let err = |e| {
                        de::Error::custom(format_args!(
                            "can't parse event data due to inner error {}",
                            e
                        ))
                    };

                    if let (Some(val), Some(event_id)) = (data_buffer, event_type) {
                        data = match event_id {
                            1 => Some(EventType::Attack(serde_json::from_value(val).map_err(err)?)),
                            2 => Some(EventType::ObjectDestroyed(
                                serde_json::from_value(val).map_err(err)?,
                            )),
                            3 => Some(EventType::AttackController),
                            4 => Some(EventType::Build(serde_json::from_value(val).map_err(err)?)),
                            5 => Some(EventType::Harvest(
                                serde_json::from_value(val).map_err(err)?,
                            )),
                            6 => Some(EventType::Heal(serde_json::from_value(val).map_err(err)?)),
                            7 => Some(EventType::Repair(serde_json::from_value(val).map_err(err)?)),
                            8 => Some(EventType::ReserveController(
                                serde_json::from_value(val).map_err(err)?,
                            )),
                            9 => Some(EventType::UpgradeController(
                                serde_json::from_value(val).map_err(err)?,
                            )),
                            10 => Some(EventType::Exit(serde_json::from_value(val).map_err(err)?)),
                            11 => Some(EventType::Power(serde_json::from_value(val).map_err(err)?)),
                            12 => Some(EventType::Transfer(
                                serde_json::from_value(val).map_err(err)?,
                            )),
                            _ => {
                                return Err(de::Error::custom(format!(
                                    "Event Type Unrecognized: {}",
                                    event_id
                                )));
                            }
                        };
                    }
                }

                let data = data.ok_or_else(|| de::Error::missing_field("data"))?;
                let obj_id = obj_id.ok_or_else(|| de::Error::missing_field("objectId"))?;

                Ok(Event {
                    event: data,
                    object_id: obj_id,
                })
            }
        }

        const FIELDS: &[&str] = &["event", "objectId", "data"];
        deserializer.deserialize_struct("Event", FIELDS, EventVisitor)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EventType {
    Attack(AttackEvent),
    ObjectDestroyed(ObjectDestroyedEvent),
    AttackController,
    Build(BuildEvent),
    Harvest(HarvestEvent),
    Heal(HealEvent),
    Repair(RepairEvent),
    ReserveController(ReserveControllerEvent),
    UpgradeController(UpgradeControllerEvent),
    Exit(ExitEvent),
    Power(PowerEvent),
    Transfer(TransferEvent),
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AttackEvent {
    pub target_id: String,
    pub damage: u32,
    pub attack_type: AttackType,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize_repr, Serialize_repr)]
#[repr(u8)]
pub enum AttackType {
    Melee = 1,
    Ranged = 2,
    RangedMass = 3,
    Dismantle = 4,
    HitBack = 5,
    Nuke = 6,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
pub struct ObjectDestroyedEvent {
    #[serde(rename = "type")]
    pub object_type: String,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BuildEvent {
    pub target_id: String,
    pub amount: u32,
    pub energy_spent: u32,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HarvestEvent {
    pub target_id: String,
    pub amount: u32,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HealEvent {
    pub target_id: String,
    pub amount: u32,
    pub heal_type: HealType,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize_repr, Serialize_repr)]
#[repr(u8)]
pub enum HealType {
    Melee = 1,
    Ranged = 2,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RepairEvent {
    pub target_id: String,
    pub amount: u32,
    pub energy_spent: u32,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReserveControllerEvent {
    pub amount: u32,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpgradeControllerEvent {
    pub amount: u32,
    pub energy_spent: u32,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExitEvent {
    pub room: String,
    pub x: u32,
    pub y: u32,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransferEvent {
    pub target_id: String,
    #[serde(deserialize_with = "crate::ResourceType::deserialize_from_str")]
    pub resource_type: ResourceType,
    pub amount: u32,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PowerEvent {
    pub target_id: String,
    pub power: PowerType,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Effect {
    pub effect: EffectType,
    pub level: Option<u8>,
    pub ticks_remaining: u32,
}
js_deserializable! {Effect}

pub enum LookResult {
    Creep(Creep),
    Energy(Resource),
    Resource(Resource),
    Source(Source),
    Mineral(Mineral),
    Deposit(Deposit),
    Structure(Structure),
    Flag(Flag),
    ConstructionSite(ConstructionSite),
    Nuke(Nuke),
    Terrain(Terrain),
    Tombstone(Tombstone),
    PowerCreep(PowerCreep),
    Ruin(Ruin),
}

impl TryFrom<Value> for LookResult {
    type Error = ConversionError;

    fn try_from(v: Value) -> Result<LookResult, Self::Error> {
        let look_type = js! (
            return __look_str_to_num(@{&v}.type);
        )
        .try_into()?;

        let lr = match look_type {
            Look::Creeps => LookResult::Creep(js_unwrap_ref!(@{v}.creep)),
            Look::Energy => LookResult::Energy(js_unwrap_ref!(@{v}.energy)),
            Look::Resources => LookResult::Resource(js_unwrap_ref!(@{v}.resource)),
            Look::Sources => LookResult::Source(js_unwrap_ref!(@{v}.source)),
            Look::Minerals => LookResult::Mineral(js_unwrap_ref!(@{v}.mineral)),
            Look::Deposits => LookResult::Deposit(js_unwrap_ref!(@{v}.deposit)),
            Look::Structures => LookResult::Structure(js_unwrap_ref!(@{v}.structure)),
            Look::Flags => LookResult::Flag(js_unwrap_ref!(@{v}.flag)),
            Look::ConstructionSites => {
                LookResult::ConstructionSite(js_unwrap_ref!(@{v}.constructionSite))
            }
            Look::Nukes => LookResult::Nuke(js_unwrap_ref!(@{v}.nuke)),
            Look::Terrain => LookResult::Terrain(js_unwrap!(__terrain_str_to_num(@{v}.terrain))),
            Look::Tombstones => LookResult::Tombstone(js_unwrap_ref!(@{v}.tombstone)),
            Look::PowerCreeps => LookResult::PowerCreep(js_unwrap_ref!(@{v}.powerCreep)),
            Look::Ruins => LookResult::Ruin(js_unwrap_ref!(@{v}.ruin)),
        };
        Ok(lr)
    }
}

pub struct PositionedLookResult {
    pub x: u32,
    pub y: u32,
    pub look_result: LookResult,
}

impl TryFrom<Value> for PositionedLookResult {
    type Error = ConversionError;

    fn try_from(v: Value) -> Result<PositionedLookResult, Self::Error> {
        let x: u32 = js!(return @{&v}.x;).try_into()?;
        let y: u32 = js!(return @{&v}.y;).try_into()?;
        let look_result: LookResult = v.try_into()?;

        Ok(PositionedLookResult { x, y, look_result })
    }
}