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
use stdweb::{InstanceOf, Reference, ReferenceType, Value};

use super::*;
use crate::{
    constants::StructureType,
    objects::{Attackable, CanDecay, HasCooldown, HasEnergyForSpawn, HasStore},
    traits::FromExpectedType,
    ConversionError,
};

/// Wrapper which can be any of the game Structures.
///
/// This is somewhat useful by itself, but has additional utility methods. Some
/// tricks:
///
/// To get a particular type, `match` on the structure:
///
/// ```no_run
/// use screeps::Structure;
///
/// # let my_struct: Structure = unimplemented!();
/// match my_struct {
///     Structure::Container(cont) => {
///         // cont here is StructureContainer
///     }
///     _ => {
///         // other structure
///     }
/// }
/// ```
///
/// To use structures of a particular type, like something that can be attacked,
/// or something that can be transferred to, use helper methods:
/// ```no_run
/// use screeps::Structure;
///
/// # let my_struct: Structure = unimplemented!();
/// match my_struct.as_transferable() {
///     Some(transf) => {
///         // transf is a reference to `dyn Transferable`, and you can transfer to it.
///     }
///     None => {
///         // my_struct is not transferable
///     }
/// }
/// ```
///
/// See method documentation for a full list of possible helpers.
#[derive(Clone)]
pub enum Structure {
    Container(StructureContainer),
    Controller(StructureController),
    Extension(StructureExtension),
    Extractor(StructureExtractor),
    Factory(StructureFactory),
    InvaderCore(StructureInvaderCore),
    KeeperLair(StructureKeeperLair),
    Lab(StructureLab),
    Link(StructureLink),
    Nuker(StructureNuker),
    Observer(StructureObserver),
    PowerBank(StructurePowerBank),
    PowerSpawn(StructurePowerSpawn),
    Portal(StructurePortal),
    Rampart(StructureRampart),
    Road(StructureRoad),
    Spawn(StructureSpawn),
    Storage(StructureStorage),
    Terminal(StructureTerminal),
    Tower(StructureTower),
    Wall(StructureWall),
}

impl Structure {
    /// Cast this structure as something Transferable, or return None if it
    /// isn't.
    ///
    /// Example usage:
    ///
    /// ```no_run
    /// use screeps::{Creep, ResourceType, Structure};
    ///
    /// # let my_struct: Structure = unimplemented!();
    /// # let my_creep: Creep = unimplemented!();
    /// match my_struct.as_transferable() {
    ///     Some(transf) => {
    ///         // transf is a reference to `dyn Transferable`, and you can transfer to it.
    ///         my_creep.transfer_all(transf, ResourceType::Energy);
    ///     }
    ///     None => {
    ///         // my_struct cannot be transferred to
    ///     }
    /// }
    /// ```
    pub fn as_transferable(&self) -> Option<&dyn Transferable> {
        match_some_structure_variants!(
            self,
            {
                Container, Extension, Factory, Lab, Link, Nuker, PowerSpawn, Spawn, Storage, Terminal, Tower
            },
            v => v
        )
    }

    /// Cast this as something which can be withdrawn from
    pub fn as_withdrawable(&self) -> Option<&dyn Withdrawable> {
        match_some_structure_variants!(
            self,
            {
                Container, Extension, Factory, Lab, Link, PowerSpawn, Spawn, Storage, Terminal, Tower
            },
            v => v
        )
    }

    /// Cast this as something which can be attacked and has hit points.
    ///
    /// The only Structure which cannot be attacked is `StructureController`.
    pub fn as_attackable(&self) -> Option<&dyn Attackable> {
        // We're not using `match_some_structure_variants!` here or in `as_owned` so we
        // won't have a `_ => None` branch and instead we'll be forced to add
        // new structures to the match explicitly. The other functions would be
        // using `_ => None` anyways since they have more `None` branches.
        match self {
            Structure::Controller(_) => None,
            Structure::Container(v) => Some(v),
            Structure::Extension(v) => Some(v),
            Structure::Extractor(v) => Some(v),
            Structure::Factory(v) => Some(v),
            Structure::InvaderCore(v) => Some(v),
            Structure::KeeperLair(v) => Some(v),
            Structure::Lab(v) => Some(v),
            Structure::Link(v) => Some(v),
            Structure::Nuker(v) => Some(v),
            Structure::Observer(v) => Some(v),
            Structure::PowerBank(v) => Some(v),
            Structure::PowerSpawn(v) => Some(v),
            Structure::Portal(_) => None,
            Structure::Rampart(v) => Some(v),
            Structure::Road(v) => Some(v),
            Structure::Spawn(v) => Some(v),
            Structure::Storage(v) => Some(v),
            Structure::Terminal(v) => Some(v),
            Structure::Tower(v) => Some(v),
            Structure::Wall(v) => Some(v),
        }
    }

    /// Cast this as something which can be owned.
    ///
    /// Example:
    ///
    /// ```no_run
    /// use screeps::Structure;
    ///
    /// # let my_struct: Structure = unimplemented!();
    /// let is_my = my_struct.as_owned().map(|os| os.my()).unwrap_or(false);
    /// ```
    pub fn as_owned(&self) -> Option<&dyn OwnedStructureProperties> {
        match self {
            Structure::Container(_) => None,
            Structure::Controller(v) => Some(v),
            Structure::Extension(v) => Some(v),
            Structure::Extractor(v) => Some(v),
            Structure::Factory(v) => Some(v),
            Structure::InvaderCore(v) => Some(v),
            Structure::KeeperLair(v) => Some(v),
            Structure::Lab(v) => Some(v),
            Structure::Link(v) => Some(v),
            Structure::Nuker(v) => Some(v),
            Structure::Observer(v) => Some(v),
            Structure::PowerBank(v) => Some(v),
            Structure::PowerSpawn(v) => Some(v),
            Structure::Portal(_) => None,
            Structure::Rampart(v) => Some(v),
            Structure::Road(_) => None,
            Structure::Spawn(v) => Some(v),
            Structure::Storage(v) => Some(v),
            Structure::Terminal(v) => Some(v),
            Structure::Tower(v) => Some(v),
            Structure::Wall(_) => None,
        }
    }

    pub fn as_can_decay(&self) -> Option<&dyn CanDecay> {
        match_some_structure_variants!(
            self,
            {
                Container, Portal, PowerBank, Rampart, Road
            },
            v => v
        )
    }

    pub fn as_has_cooldown(&self) -> Option<&dyn HasCooldown> {
        match_some_structure_variants!(
            self,
            {
                Extractor, Factory, Lab, Link, Nuker, Terminal
            },
            v => v
        )
    }

    pub fn as_has_energy_for_spawn(&self) -> Option<&dyn HasEnergyForSpawn> {
        match_some_structure_variants!(
            self,
            {
                Extension, Spawn
            },
            v => v
        )
    }

    pub fn as_has_store(&self) -> Option<&dyn HasStore> {
        match_some_structure_variants!(
            self,
            {
                Container, Extension, Factory, Lab, Link, Nuker, PowerSpawn, Spawn, Storage, Terminal, Tower
            },
            v => v
        )
    }
}

impl AsRef<Reference> for Structure {
    fn as_ref(&self) -> &Reference {
        match_structure_variants!(self, v => v.as_ref())
    }
}

impl From<Structure> for Reference {
    fn from(wrapper: Structure) -> Reference {
        match_structure_variants!(wrapper, v => v.0)
    }
}

fn get_structure_type(structure: &Reference) -> Result<StructureType, ConversionError> {
    (js! {
        return __structure_type_str_to_num(@{structure}.structureType);
    })
    .try_into()
    .map_err(Into::into)
}

impl FromExpectedType<Reference> for Structure {
    fn from_expected_type(reference: Reference) -> Result<Self, ConversionError> {
        let structure_type = get_structure_type(&reference)?;
        let structure = construct_structure_variants!(
            structure_type => reference.into_expected_type()?
        );

        Ok(structure)
    }
}

impl TryFrom<Reference> for Structure {
    type Error = ConversionError;

    fn try_from(reference: Reference) -> Result<Self, ConversionError> {
        let structure_type = get_structure_type(&reference)?;

        let structure = construct_structure_variants!(
            structure_type => reference.try_into()?
        );

        Ok(structure)
    }
}

impl InstanceOf for Structure {
    fn instance_of(reference: &Reference) -> bool {
        js_unwrap!(@{reference} instanceof Structure)
    }
}

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

    fn try_from(v: Value) -> Result<Structure, Self::Error> {
        Reference::try_from(v).and_then(Self::try_from)
    }
}

impl ReferenceType for Structure {
    unsafe fn from_reference_unchecked(reference: Reference) -> Self {
        let structure_type = js_unwrap!(__structure_type_str_to_num(@{&reference}.structureType));

        construct_structure_variants!(
            structure_type => ReferenceType::from_reference_unchecked(reference)
        )
    }
}