screeps/objects/impls/
structure.rs

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