screeps/
game.rs

1//! The main interface to objects in the Screeps game world.
2//!
3//! This contains all functionality from the `Game` object in Screeps. That
4//! generally means all state which is true this tick throughout the world.
5//!
6//! # Safety
7//!
8//! All returned game objects must be used only during the tick they are
9//! retreived or resolved. They are considered "stale" on subsequent ticks, and
10//! the behavior of stale game objects is undefined.
11//!
12//! [Screeps documentation](http://docs.screeps.com/api/#Game)
13use js_sys::{JsString, Object};
14use wasm_bindgen::prelude::*;
15
16use crate::{
17    constants::IntershardResourceType,
18    enums::StructureObject,
19    js_collections::{JsHashMap, JsObjectId},
20    local::{ObjectId, RawObjectId, RoomName},
21    objects::{
22        AccountPowerCreep, ConstructionSite, Creep, Flag, Room, RoomObject, Structure,
23        StructureSpawn,
24    },
25    traits::MaybeHasId,
26};
27
28pub mod cpu;
29pub mod gcl;
30pub mod gpl;
31pub mod map;
32pub mod market;
33pub mod shard;
34
35#[wasm_bindgen]
36extern "C" {
37    type Game;
38
39    #[wasm_bindgen(static_method_of = Game, getter = constructionSites)]
40    fn construction_sites() -> Object;
41
42    #[wasm_bindgen(static_method_of = Game, getter = creeps)]
43    fn creeps() -> Object;
44
45    #[wasm_bindgen(static_method_of = Game, getter = flags)]
46    fn flags() -> Object;
47
48    #[wasm_bindgen(static_method_of = Game, getter = powerCreeps)]
49    fn power_creeps() -> Object;
50
51    #[wasm_bindgen(static_method_of = Game, getter = resources)]
52    fn resources() -> Object;
53
54    #[wasm_bindgen(static_method_of = Game, getter = rooms)]
55    fn rooms() -> Object;
56
57    #[wasm_bindgen(static_method_of = Game, getter = spawns)]
58    fn spawns() -> Object;
59
60    #[wasm_bindgen(static_method_of = Game, getter = structures)]
61    fn structures() -> Object;
62
63    #[wasm_bindgen(static_method_of = Game, getter = time)]
64    fn time() -> u32;
65
66    #[cfg(feature = "seasonal-season-2")]
67    #[wasm_bindgen(static_method_of = Game, getter = score)]
68    fn score() -> u32;
69
70    #[cfg(feature = "seasonal-season-2")]
71    #[wasm_bindgen(static_method_of = Game, getter = symbols)]
72    fn symbols() -> Object;
73
74    #[wasm_bindgen(static_method_of = Game, js_name = getObjectById)]
75    fn get_object_by_id(id: &JsString) -> Option<RoomObject>;
76
77    #[wasm_bindgen(static_method_of = Game, js_name = notify)]
78    fn notify(message: &JsString, group_interval: Option<u32>);
79}
80
81/// Get a [`JsHashMap<ObjectId<ConstructionSite>, ConstructionSite>`] with all
82/// of your construction sites.
83///
84/// [Screeps documentation](https://docs.screeps.com/api/#Game.constructionSites)
85pub fn construction_sites() -> JsHashMap<ObjectId<ConstructionSite>, ConstructionSite> {
86    Game::construction_sites().into()
87}
88
89/// Get a [`JsHashMap<String, Creep>`] with all of your creeps, which has creep
90/// names as keys.
91///
92/// Note that newly spawned creeps are immediately added when spawned, but will
93/// not have an id until the following tick.
94///
95/// [Screeps documentation](https://docs.screeps.com/api/#Game.creeps)
96pub fn creeps() -> JsHashMap<String, Creep> {
97    Game::creeps().into()
98}
99
100/// Get a [`JsHashMap<JsString, Creep>`] with all of your creeps, which has
101/// creep names as keys.
102///
103/// Note that newly spawned creeps are immediately added when spawned, but will
104/// not have an id until the following tick.
105///
106/// [Screeps documentation](https://docs.screeps.com/api/#Game.creeps)
107pub fn creeps_jsstring() -> JsHashMap<JsString, Creep> {
108    Game::creeps().into()
109}
110
111/// Get a [`JsHashMap<String, Flag>`] with all of your flags, which has flag
112/// names as keys.
113///
114/// [Screeps documentation](https://docs.screeps.com/api/#Game.flags)
115pub fn flags() -> JsHashMap<String, Flag> {
116    Game::flags().into()
117}
118
119/// Get a [`JsHashMap<JsString, Flag>`] with all of your flags, which has flag
120/// names as keys.
121///
122/// [Screeps documentation](https://docs.screeps.com/api/#Game.flags)
123pub fn flags_jsstring() -> JsHashMap<JsString, Flag> {
124    Game::flags().into()
125}
126
127/// Get a [`JsHashMap<String, AccountPowerCreep>`] with all of your power
128/// creeps, which has power creep names as keys.
129///
130/// [Screeps documentation](https://docs.screeps.com/api/#Game.powerCreeps)
131pub fn power_creeps() -> JsHashMap<String, AccountPowerCreep> {
132    Game::power_creeps().into()
133}
134
135/// Get a [`JsHashMap<JsString, AccountPowerCreep>`] with all of your power
136/// creeps, which has power creep names as keys.
137///
138/// [Screeps documentation](https://docs.screeps.com/api/#Game.powerCreeps)
139pub fn power_creeps_jsstring() -> JsHashMap<JsString, AccountPowerCreep> {
140    Game::power_creeps().into()
141}
142
143/// Get a [`JsHashMap<IntershardResourceType, u32>`] with all of your account
144/// resources.
145///
146/// [Screeps documentation](https://docs.screeps.com/api/#Game.resources)
147pub fn resources() -> JsHashMap<IntershardResourceType, u32> {
148    Game::resources().into()
149}
150
151/// Get a [`JsHashMap<RoomName, Room>`] with the rooms visible for the current
152/// tick.
153///
154/// [Screeps documentation](https://docs.screeps.com/api/#Game.rooms)
155pub fn rooms() -> JsHashMap<RoomName, Room> {
156    Game::rooms().into()
157}
158
159/// Get a [`JsHashMap<String, StructureSpawn>`] with all of your spawns, which
160/// has spawn names as keys.
161///
162/// [Screeps documentation](https://docs.screeps.com/api/#Game.spawns)
163pub fn spawns() -> JsHashMap<String, StructureSpawn> {
164    Game::spawns().into()
165}
166
167/// Get a [`JsHashMap<JsString, StructureSpawn>`] with all of your spawns, which
168/// has spawn names as keys.
169///
170/// [Screeps documentation](https://docs.screeps.com/api/#Game.spawns)
171pub fn spawns_jsstring() -> JsHashMap<JsString, StructureSpawn> {
172    Game::spawns().into()
173}
174
175/// Get a [`JsHashMap<ObjectId<Structure>, StructureObject>`] with all of your
176/// owned structures.
177///
178/// [Screeps documentation](https://docs.screeps.com/api/#Game.spawns)
179pub fn structures() -> JsHashMap<ObjectId<Structure>, StructureObject> {
180    Game::structures().into()
181}
182
183/// Get the current time, the number of ticks the game has been running.
184///
185/// [Screeps documentation](http://docs.screeps.com/api/#Game.time)
186pub fn time() -> u32 {
187    Game::time()
188}
189
190/// Your current score, as determined by the symbols you have decoded.
191///
192/// [Screeps documentation](https://docs-season.screeps.com/api/#Game.score)
193#[cfg(feature = "seasonal-season-2")]
194pub fn score() -> u32 {
195    Game::score()
196}
197
198/// The symbols you've decoded after multiplier adjustments, used to
199/// determine your score.
200///
201/// [Screeps documentation](https://docs-season.screeps.com/api/#Game.symbols)
202#[cfg(feature = "seasonal-season-2")]
203pub fn symbols() -> JsHashMap<crate::ResourceType, u32> {
204    Game::symbols().into()
205}
206
207/// Get the typed object represented by a given [`JsObjectId`], if it's
208/// still alive and visible.
209///
210/// [Screeps documentation](http://docs.screeps.com/api/#Game.getObjectById)
211pub fn get_object_by_js_id_typed<T>(id: &JsObjectId<T>) -> Option<T>
212where
213    T: MaybeHasId + JsCast,
214{
215    Game::get_object_by_id(&id.raw).map(JsCast::unchecked_into)
216}
217
218/// Get the typed object represented by a given [`ObjectId`], if it's still
219/// alive and visible.
220///
221/// [Screeps documentation](http://docs.screeps.com/api/#Game.getObjectById)
222pub fn get_object_by_id_typed<T>(id: &ObjectId<T>) -> Option<T>
223where
224    T: MaybeHasId + JsCast,
225{
226    // construct a reference to a javascript string using the id data
227    let js_str = JsString::from(id.to_string());
228
229    Game::get_object_by_id(&js_str).map(JsCast::unchecked_into)
230}
231
232/// Get the [`RoomObject`] represented by a given [`RawObjectId`], if it's
233/// still alive and visible.
234///
235/// [Screeps documentation](http://docs.screeps.com/api/#Game.getObjectById)
236pub fn get_object_by_id_erased(id: &RawObjectId) -> Option<RoomObject> {
237    // construct a reference to a javascript string using the id data
238    let js_str = JsString::from(id.to_string());
239
240    Game::get_object_by_id(&js_str)
241}
242
243/// Send an email message to yourself with a given message.
244///
245/// Set a `group_interval` with a limit, in minutes, on how frequently emails
246/// are allowed to be sent. Message will be truncated to [`NOTIFY_MAX_LENGTH`]
247/// characters.
248///
249/// [Screeps documentation](https://docs.screeps.com/api/#Game.notify)
250///
251/// [`NOTIFY_MAX_LENGTH`]: crate::constants::NOTIFY_MAX_LENGTH
252pub fn notify(message: &str, group_interval: Option<u32>) {
253    let message: JsString = message.into();
254
255    Game::notify(&message, group_interval)
256}