Skip to content

Commit

Permalink
Support options in spawnCreep (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
chlobes authored Aug 12, 2022
1 parent 1b618ad commit eff6958
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 16 deletions.
5 changes: 1 addition & 4 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub use self::impls::{
StructureController, StructureExtension, StructureExtractor, StructureFactory,
StructureInvaderCore, StructureKeeperLair, StructureLab, StructureLink, StructureNuker,
StructureObserver, StructurePortal, StructurePowerBank, StructurePowerSpawn, StructureRampart,
StructureRoad, StructureSpawn, StructureStorage, StructureTerminal, StructureTower,
StructureRoad, StructureSpawn, SpawnOptions, StructureStorage, StructureTerminal, StructureTower,
StructureWall, Tombstone,
};

Expand Down Expand Up @@ -499,9 +499,6 @@ pub use self::impls::{MapVisual, MapVisualShape};
// // NOTE: keep impls for Structure* in sync with accessor methods in
// // src/objects/structure.rs

// unsafe impl HasEnergyForSpawn for StructureExtension {}
// unsafe impl HasEnergyForSpawn for StructureSpawn {}

// // NOTE: keep impls for Structure* in sync with accessor methods in
// // src/objects/structure.rs

Expand Down
2 changes: 1 addition & 1 deletion src/objects/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub use self::{
structure_power_spawn::StructurePowerSpawn,
structure_rampart::StructureRampart,
structure_road::StructureRoad,
structure_spawn::{Spawning, StructureSpawn},
structure_spawn::{Spawning, StructureSpawn, SpawnOptions},
structure_storage::StructureStorage,
structure_terminal::StructureTerminal,
structure_tower::StructureTower,
Expand Down
73 changes: 71 additions & 2 deletions src/objects/impls/structure_spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
containers::JsContainerFromValue,
objects::{Creep, OwnedStructure, RoomObject, Store, Structure},
prelude::*,
Part,
Direction,Part,
};
use js_sys::{Array, JsString, Object};
use wasm_bindgen::{prelude::*, JsCast};
Expand Down Expand Up @@ -85,9 +85,32 @@ impl StructureSpawn {
pub fn spawn_creep(&self, body: &[Part], name: &str) -> ReturnCode {
let body = body.iter().cloned().map(JsValue::from).collect();

//TODO: wiarchbe: Support options.
Self::spawn_creep_internal(self, &body, name, None)
}

pub fn spawn_creep_with_options(&self, body: &[Part], name: &str, opts: &SpawnOptions) -> ReturnCode {
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 {
//TODO
}*/

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);
}

Self::spawn_creep_internal(self, &body, name, Some(&js_opts))
}
}

impl JsContainerFromValue for StructureSpawn {
Expand All @@ -102,6 +125,52 @@ impl HasStore for StructureSpawn {
}
}

#[wasm_bindgen]
extern "C" {
#[derive(Clone)]
#[wasm_bindgen(extends = Object)]
type ObjectExt;

#[wasm_bindgen(method, structural, indexing_setter)]
fn set(this: &ObjectExt, prop: &str, val: &JsValue);
}

#[derive(Default)]
pub struct SpawnOptions {
//memory: Option<MemoryReference>,
energy_structures: Option<Array>,
dry_run: bool,
directions: Option<Array>,
}

impl SpawnOptions {
pub fn new() -> Self {
Self::default()
}

//TODO
/*pub fn memory<T: Into<Option<MemoryReference>>>(mut self, mem: T) -> Self {
self.memory = mem.into();
self
}*/

/// Structures other than [`StructureSpawn`] and [`StructureExtension`] will be ignored.
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
Expand Down
9 changes: 0 additions & 9 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,15 +300,6 @@ pub trait StructureProperties {
fn notify_when_attacked(&self, val: bool) -> ReturnCode;
}

/// Used to specify which structures can use their stored energy for spawning
/// creeps.
///
/// # Contract
///
/// The reference returned from `AsRef<Reference>::as_ref` must be able to be
/// used by a spawner to create a new creep.
pub trait HasEnergyForSpawn: HasStore + AsRef<RoomObject> {}

/// Trait for all wrappers over Screeps JavaScript objects which can be the
/// target of `Creep.transfer`.
///
Expand Down

0 comments on commit eff6958

Please sign in to comment.