screeps/game/
cpu.rs

1//! Information about, and functions to manage, your code's resource utilization
2//!
3//! [Screeps documentation](http://docs.screeps.com/api/#Game.cpu)
4use wasm_bindgen::prelude::*;
5
6#[cfg(feature = "mmo")]
7use crate::{enums::action_error_codes::game::cpu::*, prelude::*};
8#[cfg(feature = "mmo")]
9use js_sys::{JsString, Object};
10
11#[wasm_bindgen]
12extern "C" {
13    #[wasm_bindgen(js_name = "cpu")]
14    type Cpu;
15
16    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, getter, js_name = limit)]
17    fn limit() -> u32;
18
19    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, getter, js_name = tickLimit)]
20    fn tick_limit() -> f64;
21
22    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, getter, js_name = bucket)]
23    fn bucket() -> i32;
24
25    #[cfg(feature = "mmo")]
26    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, js_name = shardLimits)]
27    fn shard_limits() -> Object;
28
29    #[cfg(feature = "mmo")]
30    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, getter, js_name = unlocked)]
31    fn unlocked() -> bool;
32
33    #[cfg(feature = "mmo")]
34    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, getter, js_name = unlockedTime)]
35    fn unlocked_time() -> Option<f64>;
36
37    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, js_name = getHeapStatistics)]
38    fn get_heap_statistics() -> HeapStatistics;
39
40    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, js_name = getUsed)]
41    fn get_used() -> f64;
42
43    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, js_name = halt)]
44    fn halt();
45
46    #[cfg(feature = "mmo")]
47    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, js_name = setShardLimits)]
48    fn set_shard_limits(limits: &Object) -> i8;
49
50    #[cfg(feature = "mmo")]
51    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, js_name = unlock)]
52    fn unlock() -> i8;
53
54    #[cfg(feature = "mmo")]
55    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, js_name = generatePixel)]
56    fn generate_pixel() -> i8;
57}
58
59/// Your assigned CPU for the current shard.
60pub fn limit() -> u32 {
61    Cpu::limit()
62}
63
64/// The amount of CPU available for execution in a given tick.
65///
66/// Consists of your per-tick CPU [`limit`] plus your accrued [`bucket`], up to
67/// a maximum of 500 ([`CPU_TICK_LIMIT_MAX`]); [`f64::INFINITY`] on sim.
68///
69/// [`CPU_TICK_LIMIT_MAX`]: crate::constants::extra::CPU_TICK_LIMIT_MAX
70pub fn tick_limit() -> f64 {
71    Cpu::tick_limit()
72}
73
74/// The amount of CPU that has accumulated in your bucket.
75pub fn bucket() -> i32 {
76    Cpu::bucket()
77}
78
79/// Your assigned CPU limits for each shard in an [`Object`], with shard
80/// names in [`JsString`] form as keys and numbers as values. This is the
81/// same format accepted by [`set_shard_limits`].
82#[cfg(feature = "mmo")]
83pub fn shard_limits() -> JsHashMap<JsString, u32> {
84    Cpu::shard_limits().into()
85}
86
87/// Whether your account is unlocked to have full CPU.
88#[cfg(feature = "mmo")]
89pub fn unlocked() -> bool {
90    Cpu::unlocked()
91}
92
93// note: f64 due to https://github.com/rustwasm/wasm-bindgen/issues/4113
94/// If your account has been unlocked for a limited time, contains the time
95/// it's unlocked until in milliseconds since epoch.
96#[cfg(feature = "mmo")]
97pub fn unlocked_time() -> Option<f64> {
98    Cpu::unlocked_time()
99}
100
101/// Get information about your script's memory heap usage.
102///
103/// [Screeps documentation](https://docs.screeps.com/api/#Game.cpu.getHeapStatistics)
104pub fn get_heap_statistics() -> HeapStatistics {
105    Cpu::get_heap_statistics()
106}
107
108/// Get the amount of CPU time used for execution so far this tick.
109///
110/// [Screeps documentation](https://docs.screeps.com/api/#Game.cpu.getUsed)
111pub fn get_used() -> f64 {
112    Cpu::get_used()
113}
114
115/// Stop execution of your script immediately and requests the destruction of
116/// your code's environment, which will start fresh on the following tick.
117///
118/// Note that this will cause your code to not complete API calls called earlier
119/// in the current tick; no log messages will be sent to the console, email
120/// messages sent via `game::notify` are not sent, and game actions taken should
121/// not complete.
122///
123/// [Screeps documentation](https://docs.screeps.com/api/#Game.cpu.halt)
124pub fn halt() {
125    Cpu::halt()
126}
127
128/// Set the allocation of your CPU among the server shards.
129///
130/// Limits should be in an [`Object`], with shard names in [`JsString`] form as
131/// keys and numbers as values. This is the same format returned by
132/// [`shard_limits`]. Total amount of CPU should remain equal to the sum of the
133/// values of [`shard_limits`]. This method can be used only once per 12 hours
134/// ([`CPU_SET_SHARD_LIMITS_COOLDOWN`]).
135///
136/// [Screeps documentation](https://docs.screeps.com/api/#Game.cpu.setShardLimits)
137///
138/// [`CPU_SET_SHARD_LIMITS_COOLDOWN`]: crate::constants::CPU_SET_SHARD_LIMITS_COOLDOWN
139#[cfg(feature = "mmo")]
140pub fn set_shard_limits(limits: &Object) -> Result<(), SetShardLimitsErrorCode> {
141    SetShardLimitsErrorCode::result_from_i8(Cpu::set_shard_limits(limits))
142}
143
144/// Consume a [`CpuUnlock`] to unlock your full CPU for 24 hours.
145///
146/// [Screeps documentation](https://docs.screeps.com/api/#Game.cpu.unlock)
147///
148/// [`CpuUnlock`]: crate::constants::IntershardResourceType::CpuUnlock
149#[cfg(feature = "mmo")]
150pub fn unlock() -> Result<(), UnlockErrorCode> {
151    UnlockErrorCode::result_from_i8(Cpu::unlock())
152}
153
154/// Generate a [`Pixel`], consuming [`PIXEL_CPU_COST`] CPU from your bucket.
155///
156/// [Screeps documentation](https://docs.screeps.com/api/#Game.cpu.generatePixel)
157///
158/// [`Pixel`]: crate::constants::IntershardResourceType::Pixel
159/// [`PIXEL_CPU_COST`]: crate::constants::PIXEL_CPU_COST
160#[cfg(feature = "mmo")]
161pub fn generate_pixel() -> Result<(), GeneratePixelErrorCode> {
162    GeneratePixelErrorCode::result_from_i8(Cpu::generate_pixel())
163}
164
165#[wasm_bindgen]
166extern "C" {
167    /// Object with info about the memory heap of your virtual machine.
168    ///
169    /// [Screeps documentation](https://docs.screeps.com/api/#Game.cpu.getHeapStatistics)
170    #[wasm_bindgen]
171    pub type HeapStatistics;
172
173    /// The total heap consumed.
174    #[wasm_bindgen(method, getter)]
175    pub fn total_heap_size(this: &HeapStatistics) -> u32;
176
177    /// The total heap consumed by executable code.
178    #[wasm_bindgen(method, getter)]
179    pub fn total_heap_size_executable(this: &HeapStatistics) -> u32;
180
181    /// The total amount of heap committed to memory.
182    #[wasm_bindgen(method, getter)]
183    pub fn total_physical_size(this: &HeapStatistics) -> u32;
184
185    /// Amount of heap available for allocation.
186    #[wasm_bindgen(method, getter)]
187    pub fn total_available_size(this: &HeapStatistics) -> u32;
188
189    /// Total heap consumed by application data.
190    #[wasm_bindgen(method, getter)]
191    pub fn used_heap_size(this: &HeapStatistics) -> u32;
192
193    /// The allowed limit for total heap memory.
194    #[wasm_bindgen(method, getter)]
195    pub fn heap_size_limit(this: &HeapStatistics) -> u32;
196
197    /// Total amount of memory obtained by malloc.
198    #[wasm_bindgen(method, getter)]
199    pub fn malloced_memory(this: &HeapStatistics) -> u32;
200
201    /// Maximum amount of memory obtained by malloc.
202    #[wasm_bindgen(method, getter)]
203    pub fn peak_malloced_memory(this: &HeapStatistics) -> u32;
204
205    /// Whether the virtual machine overwrites memory as it deallocates -
206    /// usually 0.
207    #[wasm_bindgen(method, getter)]
208    pub fn does_zap_garbage(this: &HeapStatistics) -> u32;
209
210    /// External allocations that are outside of the v8 heap but still count
211    /// against the memory limit.
212    #[wasm_bindgen(method, getter)]
213    pub fn externally_allocated_size(this: &HeapStatistics) -> u32;
214}