screeps/
lib.rs

1//! Typed bindings to the Screeps: World in-game API for WASM Rust AIs.
2//!
3//! # Performance
4//!
5//! Due to the limited CPU available to the player in Screeps: World, the
6//! performance implications of running WebAssembly are good to be aware of.
7//!
8//! WebAssembly instances have a dedicated linear memory, and data being passed
9//! in or out must be copied. Additionally, Rust uses UTF-8 strings while
10//! JavaScript uses UTF-16 strings, so any strings being copied must also be
11//! converted between encodings.
12//!
13//! Additionally, compile time for the `WebAssembly.Module` can be considerable,
14//! requiring a lot of CPU time on each initial startup tick (and potentially
15//! requiring skipped ticks for larger bots). However, global resets are very
16//! infrequent in most environments, so the impact of this isn't severe.
17//!
18//! After compilation, the WebAssembly environment has near-native performance,
19//! and no Javascript garbage collection happening for its memory space,
20//! allowing for faster execution of some types of workloads. Overall, the
21//! advantages and disadvantages of WebAssembly in Screeps are relatively small,
22//! especially when compared to the relatively high 0.2ms cost of game actions.
23//!
24//! # Data Persistence
25//!
26//! In the Screeps: World JavaScript environment, the `Memory` object is the
27//! typical way to store data in a way that persists through the environment
28//! resets that happen occasionally, either triggered by deploying a new version
29//! of your code or due to natural expiration in the server. It provides a
30//! wrapper that automatically deserializes the contents of `RawMemory` via the
31//! `JSON.parse()` JavaScript function when accessed for the first time each
32//! tick, then gets serialized by `JSON.stringify()` at the end of the tick.
33//!
34//! Using this untyped `Memory` object (or the reference to a part of it, which
35//! can be obtained from the `memory` function on various game objects) from
36//! within WebAssembly can be awkward, but is recommended if you need to
37//! maintain compatibility with the default `Memory` object.
38//!
39//! An alternative that you may prefer is to use `RawMemory` instead, fetching
40//! the stored data in string format using [`raw_memory::get`] and deserializing
41//! within WebAssembly using [`serde`] or another serializion approach, then
42//! serializing and using [`raw_memory::set`] to store the data.
43//!
44//! If you choose the `RawMemory` approach, be aware that some game methods
45//! (notably [`StructureSpawn::spawn_creep`] and [`Creep::move_to`]) directly
46//! store data in the `Memory` object; replacing the 'special' `Memory` object
47//! with one that doesn't attempt to deserialize the contents of `RawMemory` may
48//! be advisable if you're using it directly (note that this needs to be done
49//! each tick to be effective).
50//!
51//! # Cargo Features
52//!
53//! ## `mmo`
54//!
55//! Enables a number of functions for CPU management and inter-shard
56//! communication present on the Screeps: World official servers but not on
57//! private servers.
58//!
59//! ## `seasonal-season-1`
60//!
61//! Enables the score resource and entities, introduced for Screeps Seasonal's
62//! first season, as well as enabling constants relevant to season 1.
63//!
64//! ## `seasonal-season-2`
65//!
66//! Enables the symbol resources and entities, introduced for Screeps Seasonal's
67//! second season, as well as enabling constants relevant to season 2.
68//!
69//! ## `seasonal-season-5`
70//!
71//! Enables the thorium resource and reactor object, introduced for Screeps
72//! Seasonal's fifth season, as well as enabling constants relevant to season 5.
73//!
74//! ## `sim`
75//!
76//! Enables special-case handling of the unique room name present in the
77//! simulator - must be enabled to build code that is compatible with that
78//! environment. If this is enabled, the top-left valid room coordinate has the
79//! name `sim`, otherwise it's named `W127N127`.
80//!
81//! ## `unsafe-return-conversion`
82//!
83//! Enables return code conversion from game functions that presumes all return
84//! code values are in the expected ranges skipping checks, and risks undefined
85//! behavior if they are not.
86#![recursion_limit = "128"]
87// to build locally with doc_cfg enabled, run:
88// `RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --all-features`
89#![cfg_attr(docsrs, feature(doc_auto_cfg))]
90// warn when functions can safely be given the const keyword, see
91// https://rust-lang.github.io/rust-clippy/master/index.html#/missing_const_for_fn
92// unfortunately this warns for bindgen-attached functions so we can't leave it
93// enabled
94
95// #![warn(clippy::missing_const_for_fn)]
96
97// disable deprecation warnings - TODO need to figure out how to get wasm_bindgen's new thread_local
98// attribute working
99#![allow(deprecated)]
100
101pub mod console;
102pub mod constants;
103pub mod enums;
104pub mod game;
105#[cfg(feature = "mmo")]
106pub mod inter_shard_memory;
107pub mod js_collections;
108pub mod local;
109pub mod memory;
110pub mod objects;
111pub mod pathfinder;
112pub(crate) mod prototypes;
113pub mod raw_memory;
114pub mod traits;
115
116pub use crate::{constants::*, enums::*, js_collections::*, local::*, objects::*, traits::*};
117
118/// Traits which implement base functionalities for Screeps types.
119///
120/// # Example
121///
122/// ```no_run
123/// use js_sys::{JsString, Reflect};
124/// use screeps::{game, prelude::*, Creep};
125///
126/// let c = game::creeps().get(String::from("Bob")).unwrap();
127///
128/// // `HasId` trait brought in from prelude
129/// let id = c.try_id().unwrap();
130/// ```
131///
132/// This module contains all base functionality traits, and no structures.
133pub mod prelude {
134    pub use crate::{js_collections::*, traits::*};
135}