screeps/objects/impls/
structure.rs

1use js_sys::JsString;
2use wasm_bindgen::prelude::*;
3
4use crate::{
5    constants::StructureType, enums::action_error_codes::structure::*, objects::RoomObject,
6    prelude::*,
7};
8
9#[wasm_bindgen]
10extern "C" {
11    /// Parent class for all objects that represent a structure in the game
12    /// world.
13    ///
14    /// [Screeps documentation](https://docs.screeps.com/api/#Structure)
15    #[wasm_bindgen(extends = RoomObject)]
16    #[derive(Clone, Debug)]
17    pub type Structure;
18
19    #[wasm_bindgen(method, getter = hits)]
20    fn hits_internal(this: &Structure) -> Option<u32>;
21
22    #[wasm_bindgen(method, getter = hitsMax)]
23    fn hits_max_internal(this: &Structure) -> Option<u32>;
24
25    /// Object ID of the structure, which can be used to efficiently fetch a
26    /// fresh reference to the object on subsequent ticks.
27    ///
28    /// [Screeps documentation](https://docs.screeps.com/api/#Structure.id)
29    #[wasm_bindgen(method, getter = id)]
30    fn id_internal(this: &Structure) -> JsString;
31
32    /// The type of structure this is.
33    ///
34    /// [Screeps documentation](https://docs.screeps.com/api/#Structure.structureType)
35    #[wasm_bindgen(method, getter = structureType)]
36    pub fn structure_type(this: &Structure) -> StructureType;
37
38    #[wasm_bindgen(method, js_name = destroy)]
39    fn destroy_internal(this: &Structure) -> i8;
40
41    /// Determine if the structure is active and can be used at the current RCL.
42    ///
43    /// [Screeps documentation](https://docs.screeps.com/api/#Structure.isActive)
44    #[wasm_bindgen(method, js_name = isActive)]
45    pub fn is_active(this: &Structure) -> bool;
46
47    #[wasm_bindgen(method, js_name = notifyWhenAttacked)]
48    fn notify_when_attacked_internal(this: &Structure, val: bool) -> i8;
49}
50
51impl Structure {
52    /// Retrieve the current hits of this structure, or `0` if this structure is
53    /// indestructible, such as a notice area border wall, portal, or room
54    /// controller.
55    ///
56    /// [Screeps documentation](https://docs.screeps.com/api/#Structure.hits)
57    pub fn hits(&self) -> u32 {
58        self.hits_internal().unwrap_or(0)
59    }
60
61    /// Retrieve the maximum hits of this structure, or `0` if this structure is
62    /// indestructible, such as a notice area border wall, portal, or room
63    /// controller.
64    ///
65    /// [Screeps documentation](https://docs.screeps.com/api/#Structure.hitsMax)
66    pub fn hits_max(&self) -> u32 {
67        self.hits_max_internal().unwrap_or(0)
68    }
69
70    /// Destroy the structure, if possible.
71    ///
72    /// [Screeps documentation](https://docs.screeps.com/api/#Structure.destroy)
73    pub fn destroy(&self) -> Result<(), DestroyErrorCode> {
74        DestroyErrorCode::result_from_i8(self.destroy_internal())
75    }
76
77    /// Set whether a notification email should be sent when the structure is
78    /// attacked.
79    ///
80    /// [Screeps documentation](https://docs.screeps.com/api/#Structure.notifyWhenAttacked)
81    pub fn notify_when_attacked(
82        &self,
83        val: bool,
84    ) -> Result<(), StructureNotifyWhenAttackedErrorCode> {
85        StructureNotifyWhenAttackedErrorCode::result_from_i8(
86            self.notify_when_attacked_internal(val),
87        )
88    }
89}
90
91impl<T> HasId for T
92where
93    T: AsRef<Structure> + JsCast,
94{
95    fn js_raw_id(&self) -> JsString {
96        Structure::id_internal(self.as_ref())
97    }
98}
99
100impl<T> HasHits for T
101where
102    T: AsRef<Structure>,
103{
104    fn hits(&self) -> u32 {
105        Structure::hits(self.as_ref())
106    }
107
108    fn hits_max(&self) -> u32 {
109        Structure::hits_max(self.as_ref())
110    }
111}
112
113impl<T> StructureProperties for T
114where
115    T: AsRef<Structure>,
116{
117    fn structure_type(&self) -> StructureType {
118        Structure::structure_type(self.as_ref())
119    }
120
121    fn destroy(&self) -> Result<(), DestroyErrorCode> {
122        Structure::destroy(self.as_ref())
123    }
124
125    fn is_active(&self) -> bool {
126        Structure::is_active(self.as_ref())
127    }
128
129    fn notify_when_attacked(&self, val: bool) -> Result<(), StructureNotifyWhenAttackedErrorCode> {
130        Structure::notify_when_attacked(self.as_ref(), val)
131    }
132}