1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
use js_sys::{Array, JsString, Object};
use wasm_bindgen::{prelude::*, JsCast};
use crate::{
constants::{Direction, ErrorCode, Part},
objects::{Creep, OwnedStructure, RoomObject, Store, Structure},
prelude::*,
};
#[wasm_bindgen]
extern "C" {
/// An object representing a [`StructureSpawn`], which creates your creeps.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn)
#[wasm_bindgen(extends = RoomObject, extends = Structure, extends = OwnedStructure)]
#[derive(Clone, Debug)]
pub type StructureSpawn;
/// A shortcut to `Memory.spawns[spawn.name]`.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn.memory)
#[wasm_bindgen(method, getter)]
pub fn memory(this: &StructureSpawn) -> JsValue;
/// Sets a new value to `Memory.spawns[spawn.name]`.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn.memory)
#[wasm_bindgen(method, setter)]
pub fn set_memory(this: &StructureSpawn, val: &JsValue);
/// The spawn's name as an owned reference to a [`JsString`].
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn.name)
#[wasm_bindgen(method, getter)]
pub fn name(this: &StructureSpawn) -> JsString;
/// Information about the spawning creep, if one is currently being spawned.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn.spawning)
#[wasm_bindgen(method, getter)]
pub fn spawning(this: &StructureSpawn) -> Option<Spawning>;
/// The [`Store`] of the spawn, which contains information about what
/// resources it is it holding.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn.store)
#[wasm_bindgen(method, getter)]
pub fn store(this: &StructureSpawn) -> Store;
#[wasm_bindgen(method, js_name = spawnCreep)]
fn spawn_creep_internal(
this: &StructureSpawn,
body: &Array,
name: &str,
options: Option<&Object>,
) -> i8;
#[wasm_bindgen(method, js_name = recycleCreep)]
fn recycle_creep_internal(this: &StructureSpawn, creep: &Creep) -> i8;
#[wasm_bindgen(method, js_name = renewCreep)]
fn renew_creep_internal(this: &StructureSpawn, creep: &Creep) -> i8;
}
impl StructureSpawn {
/// Create a new creep with the specified body part [`Array`], name
/// [`JsString`], and optional spawning options. Note that successfully
/// spawning will store data in `Memory.creeps[creep_name]` _regardless
/// of whether any memory data was passed in the options object_ and enable
/// the default serialization behavior of the `Memory` object, which may
/// hamper attempts to directly use `RawMemory`. todo, add note+docs
/// about how to replace Memory and/or delete RawMemory._parsed
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn.spawnCreep)
pub fn spawn_creep(&self, body: &[Part], name: &str) -> Result<(), ErrorCode> {
let body = body.iter().cloned().map(JsValue::from).collect();
ErrorCode::result_from_i8(Self::spawn_creep_internal(self, &body, name, None))
}
/// Create a new creep with the specified body part [`Array`], name
/// [`JsString`], and optional spawning options. Note that successfully
/// spawning will store data in `Memory.creeps[creep_name]` _regardless
/// of whether any memory data was passed in the options object_ and enable
/// the default serialization behavior of the `Memory` object, which may
/// hamper attempts to directly use `RawMemory`. todo, add note+docs
/// about how to replace Memory and/or delete RawMemory._parsed
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn.spawnCreep)
pub fn spawn_creep_with_options(
&self,
body: &[Part],
name: &str,
opts: &SpawnOptions,
) -> Result<(), ErrorCode> {
let body = body.iter().cloned().map(JsValue::from).collect();
let js_opts = ObjectExt::unchecked_from_js(JsValue::from(Object::new()));
if let Some(mem) = &opts.memory {
ObjectExt::set(&js_opts, "memory", mem);
}
if let Some(array) = &opts.energy_structures {
ObjectExt::set(&js_opts, "energyStructures", array);
}
if opts.dry_run {
ObjectExt::set(&js_opts, "dryRun", &true.into());
}
if let Some(array) = &opts.directions {
ObjectExt::set(&js_opts, "directions", array);
}
ErrorCode::result_from_i8(Self::spawn_creep_internal(
self,
&body,
name,
Some(&js_opts),
))
}
/// Kill a [`Creep`] in melee range, returning 100% of its TTL-adjusted
/// resources (5x more than if the creep is killed another way). Can be used
/// while spawning.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn.recycleCreep)
pub fn recycle_creep(&self, creep: &Creep) -> Result<(), ErrorCode> {
ErrorCode::result_from_i8(self.recycle_creep_internal(creep))
}
/// Renew a [`Creep`] in melee range, removing all boosts adding to its TTL.
/// Cannot be used while spawning.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn.renewCreep)
pub fn renew_creep(&self, creep: &Creep) -> Result<(), ErrorCode> {
ErrorCode::result_from_i8(self.renew_creep_internal(creep))
}
}
impl JsCollectionFromValue for StructureSpawn {
fn from_value(val: JsValue) -> Self {
val.unchecked_into()
}
}
impl HasStore for StructureSpawn {
fn store(&self) -> Store {
Self::store(self)
}
}
impl Attackable for StructureSpawn {}
impl Dismantleable for StructureSpawn {}
impl Repairable for StructureSpawn {}
impl Transferable for StructureSpawn {}
impl Withdrawable for StructureSpawn {}
#[derive(Default)]
pub struct SpawnOptions {
memory: Option<JsValue>,
energy_structures: Option<Array>,
dry_run: bool,
directions: Option<Array>,
}
impl SpawnOptions {
pub fn new() -> Self {
Self::default()
}
pub fn memory(mut self, mem: JsValue) -> Self {
self.memory = Some(mem);
self
}
/// Structures other than [`StructureSpawn`] and [`StructureExtension`] will
/// be ignored.
///
/// [`StructureExtension`]: crate::objects::StructureExtension
pub fn energy_structures<T: IntoIterator<Item = V>, V: AsRef<Structure>>(
mut self,
structures: T,
) -> Self {
self.energy_structures = Some(
structures
.into_iter()
.map(|structure| JsValue::from(structure.as_ref()))
.collect(),
);
self
}
pub fn dry_run(mut self, dry_run: bool) -> Self {
self.dry_run = dry_run;
self
}
pub fn directions(mut self, directions: &[Direction]) -> Self {
self.directions = Some(
directions
.iter()
.map(|&d| JsValue::from(d as u32))
.collect(),
);
self
}
}
#[wasm_bindgen]
extern "C" {
/// Object with info on what a [`StructureSpawn`] or
/// [`StructureInvaderCore`] is currently spawning.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn-Spawning)
///
/// [`StructureInvaderCore`]: crate::objects::StructureInvaderCore
#[wasm_bindgen(js_namespace = StructureSpawn)]
pub type Spawning;
/// Allowed directions for the creep to exit the spawn; can be changed with
/// [`Spawning::set_directions`].
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn.Spawning.directions)
#[wasm_bindgen(method, getter)]
pub fn directions(this: &Spawning) -> Array;
/// The name of the spawning creep.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn.Spawning.name)
#[wasm_bindgen(method, getter)]
pub fn name(this: &Spawning) -> JsString;
/// Total time needed to spawn this creep.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn.Spawning.needTime)
#[wasm_bindgen(method, getter = needTime)]
pub fn need_time(this: &Spawning) -> u32;
/// Total time remaining to spawn this creep.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn.Spawning.remainingTime)
#[wasm_bindgen(method, getter = remainingTime)]
pub fn remaining_time(this: &Spawning) -> u32;
/// Get a reference to the [`Structure`] spawning the creep, either a
/// [`StructureSpawn`] or a [`StructureInvaderCore`].
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn.Spawning.spawn)
#[wasm_bindgen(method, getter)]
pub fn spawn(this: &Spawning) -> Structure;
#[wasm_bindgen(method, js_name = cancel)]
fn cancel_internal(this: &Spawning) -> i8;
#[wasm_bindgen(method, js_name = setDirections)]
fn set_directions_internal(this: &Spawning, directions: &Array) -> i8;
}
impl Spawning {
/// Cancel spawning this creep, without refunding any energy.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn.Spawning.cancel)
pub fn cancel(&self) -> Result<(), ErrorCode> {
ErrorCode::result_from_i8(self.cancel_internal())
}
/// Change allowed directions for the creep to leave the spawn once it's
/// ready.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureSpawn.Spawning.setDirections)
pub fn set_directions(&self, directions: &Array) -> Result<(), ErrorCode> {
ErrorCode::result_from_i8(self.set_directions_internal(directions))
}
}