screeps/objects/impls/
structure_controller.rs

1use js_sys::Date;
2use wasm_bindgen::prelude::*;
3
4use crate::{
5    constants::ErrorCode,
6    objects::{OwnedStructure, RoomObject, Structure},
7    prelude::*,
8};
9
10#[wasm_bindgen]
11extern "C" {
12    /// An object representing a [`StructureController`].
13    ///
14    /// [Screeps documentation](https://docs.screeps.com/api/#StructureController)
15    #[wasm_bindgen(extends = RoomObject, extends = Structure, extends = OwnedStructure)]
16    #[derive(Clone, Debug)]
17    pub type StructureController;
18
19    /// Whether power is enabled in the room, allowing power creeps to use
20    /// powers.
21    ///
22    /// [Screeps documentation](https://docs.screeps.com/api/#StructureController.isPowerEnabled)
23    #[wasm_bindgen(method, getter = isPowerEnabled)]
24    pub fn is_power_enabled(this: &StructureController) -> bool;
25
26    /// The current room control level (RCL) of the room.
27    ///
28    /// [Screeps documentation](https://docs.screeps.com/api/#StructureController.level)
29    #[wasm_bindgen(method, getter)]
30    pub fn level(this: &StructureController) -> u8;
31
32    /// The progress toward upgrading the controller to the next level, or
33    /// `None` if the controller is unowned.
34    ///
35    /// [Screeps documentation](https://docs.screeps.com/api/#StructureController.progress)
36    #[wasm_bindgen(method, getter)]
37    pub fn progress(this: &StructureController) -> Option<u32>;
38
39    /// The total [`StructureController::progress`] needed to upgrade the
40    /// controller to the next level, or `None` if the controller is unowned.
41    ///
42    /// [Screeps documentation](https://docs.screeps.com/api/#StructureController.progressTotal)
43    #[wasm_bindgen(method, getter = progressTotal)]
44    pub fn progress_total(this: &StructureController) -> Option<u32>;
45
46    /// Information about the reservation of this controller, if it is currently
47    /// reserved.
48    ///
49    /// [Screeps documentation](https://docs.screeps.com/api/#StructureController.reservation)
50    #[wasm_bindgen(method, getter)]
51    pub fn reservation(this: &StructureController) -> Option<Reservation>;
52
53    /// The number of ticks remaining in safe mode, or 0 if safe mode isn't
54    /// currently active.
55    ///
56    /// [Screeps documentation](https://docs.screeps.com/api/#StructureController.safeMode)
57    #[wasm_bindgen(method, getter = safeMode)]
58    pub fn safe_mode(this: &StructureController) -> Option<u32>;
59
60    /// The number of of available safe mode activations, which can be increased
61    /// by using [`Creep::generate_safe_mode`].
62    ///
63    /// [Screeps documentation](https://docs.screeps.com/api/#StructureController.safeModeAvailable)
64    ///
65    /// [`Creep::generate_safe_mode`]: crate::objects::Creep::generate_safe_mode
66    #[wasm_bindgen(method, getter = safeModeAvailable)]
67    pub fn safe_mode_available(this: &StructureController) -> u32;
68
69    /// The cooldown remaining until safe mode can be activated again.
70    ///
71    /// [Screeps documentation](https://docs.screeps.com/api/#StructureController.safeModeCooldown)
72    #[wasm_bindgen(method, getter = safeModeCooldown)]
73    pub fn safe_mode_cooldown(this: &StructureController) -> Option<u32>;
74
75    /// Information about the sign on this controller, if it has been signed by
76    /// [`Creep::sign_controller`].
77    ///
78    /// [Screeps documentation](https://docs.screeps.com/api/#StructureController.sign)
79    ///
80    /// [`Creep::sign_controller`]: crate::objects::Creep::sign_controller
81    #[wasm_bindgen(method, getter)]
82    pub fn sign(this: &StructureController) -> Option<Sign>;
83
84    /// The number of ticks until the level of the controller will be
85    /// decremented due to a lack of [`Creep::upgrade_controller`] activity, or
86    /// `None` if the controller is unowned.
87    ///
88    /// [Screeps documentation](https://docs.screeps.com/api/#StructureController.ticksToDowngrade)
89    #[wasm_bindgen(method, getter = ticksToDowngrade)]
90    pub fn ticks_to_downgrade(this: &StructureController) -> Option<u32>;
91
92    /// The number of ticks until the controller can be upgraded, or have safe
93    /// mode activated, due to [`Creep::attack_controller`].
94    ///
95    /// [Screeps documentation](https://docs.screeps.com/api/#StructureController.upgradeBlocked)
96    ///
97    /// [`Creep::attack_controller`]: crate::objects::Creep::attack_controller
98    #[wasm_bindgen(method, getter = upgradeBlocked)]
99    pub fn upgrade_blocked(this: &StructureController) -> Option<u32>;
100
101    #[wasm_bindgen(method, js_name = activateSafeMode)]
102    fn activate_safe_mode_internal(this: &StructureController) -> i8;
103
104    #[wasm_bindgen(method, js_name = unclaim)]
105    fn unclaim_internal(this: &StructureController) -> i8;
106}
107
108impl StructureController {
109    /// Activate safe mode for the room, preventing hostile creep actions in the
110    /// room for 20,000 ticks
111    ///
112    /// [Screeps documentation](https://docs.screeps.com/api/#StructureController.activateSafeMode)
113    pub fn activate_safe_mode(&self) -> Result<(), ErrorCode> {
114        ErrorCode::result_from_i8(self.activate_safe_mode_internal())
115    }
116
117    /// Relinquish ownership of the controller and its room.
118    ///
119    /// [Screeps documentation](https://docs.screeps.com/api/#StructureController.unclaim)
120    pub fn unclaim(&self) -> Result<(), ErrorCode> {
121        ErrorCode::result_from_i8(self.unclaim_internal())
122    }
123}
124
125#[wasm_bindgen]
126extern "C" {
127    /// Object with info on who has reserved this [`StructureController`]
128    ///
129    /// [Screeps documentation](https://docs.screeps.com/api/#StructureController.reservation)
130    #[wasm_bindgen]
131    pub type Reservation;
132
133    /// The name of the player that has reserved this controller.
134    #[wasm_bindgen(method, getter)]
135    pub fn username(this: &Reservation) -> String;
136
137    /// The number of ticks until the reservation expires.
138    #[wasm_bindgen(method, getter = ticksToEnd)]
139    pub fn ticks_to_end(this: &Reservation) -> u32;
140}
141
142#[wasm_bindgen]
143extern "C" {
144    /// Object with info on the sign on a [`StructureController`].
145    ///
146    /// [Screeps documentation](https://docs.screeps.com/api/#StructureController.sign)
147    #[wasm_bindgen]
148    pub type Sign;
149
150    /// The name of the player that has reserved this controller.
151    #[wasm_bindgen(method, getter)]
152    pub fn username(this: &Sign) -> String;
153
154    /// The text of the sign on this controller.
155    #[wasm_bindgen(method, getter)]
156    pub fn text(this: &Sign) -> String;
157
158    /// The tick when this sign was written.
159    #[wasm_bindgen(method, getter)]
160    pub fn time(this: &Sign) -> u32;
161
162    /// The timestamp of when this sign was written.
163    #[wasm_bindgen(method, getter)]
164    pub fn datetime(this: &Sign) -> Date;
165}