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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
//! Traits associated with how specific [game objects] can be used.
//!
//! [game objects]: crate::objects
use std::str::FromStr;
use enum_dispatch::enum_dispatch;
use js_sys::{Array, JsString};
use wasm_bindgen::prelude::*;
use crate::{
constants::*,
enums::*,
local::{ObjectId, Position, RawObjectId, RoomName, RoomXY},
objects::*,
pathfinder::SingleRoomCostResult,
prelude::*,
};
pub trait FromReturnCode {
type Error;
fn result_from_i8(val: i8) -> Result<(), Self::Error>;
fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>>;
fn try_result_from_jsvalue(val: &JsValue) -> Option<Result<(), Self::Error>> {
val.as_f64().and_then(|f| Self::try_result_from_i8(f as i8))
}
}
#[enum_dispatch]
pub trait HasHits {
/// Retrieve the current hits of this object.
fn hits(&self) -> u32;
/// Retrieve the maximum hits of this object.
fn hits_max(&self) -> u32;
}
#[enum_dispatch]
pub trait CanDecay {
/// The number of ticks until the object will decay, losing hits.
fn ticks_to_decay(&self) -> u32;
}
#[enum_dispatch]
pub trait HasCooldown {
/// The number of ticks until the object can be used again.
fn cooldown(&self) -> u32;
}
/// Trait for all game objects which have an associated unique identifier.
pub trait HasId: MaybeHasId {
/// Object ID of the object stored in Rust memory, which can be used to
/// efficiently fetch a fresh reference to the object on subsequent
/// ticks.
fn id(&self) -> ObjectId<Self>
where
Self: Sized,
{
self.raw_id().into()
}
/// Object ID of the object stored in Rust memory, without its associated
/// type information.
fn raw_id(&self) -> RawObjectId {
let id: String = self.js_raw_id().into();
RawObjectId::from_str(&id).expect("expected object ID to be parseable")
}
/// Object ID of the object stored in JavaScript memory, which can be used
/// to efficiently fetch a fresh reference to the object on subsequent
/// ticks.
fn js_id(&self) -> JsObjectId<Self>
where
Self: Sized,
{
self.js_raw_id().into()
}
/// Object ID of the object stored in JavaScript memory, without its
/// associated type information.
fn js_raw_id(&self) -> JsString;
}
/// Trait for all game objects which may (or may not) have an associated unique
/// identifier.
pub trait MaybeHasId {
/// Object ID of the object, which can be used to efficiently fetch a
/// fresh reference to the object on subsequent ticks, or `None` if the
/// object doesn't currently have an ID.
fn try_id(&self) -> Option<ObjectId<Self>>
where
Self: Sized,
{
self.try_raw_id().map(Into::into)
}
/// Object ID of the object, without its associated type information, or
/// `None` if the object doesn't currently have an ID.
fn try_raw_id(&self) -> Option<RawObjectId> {
self.try_js_raw_id()
.map(String::from)
.map(|id| RawObjectId::from_str(&id).expect("expected object ID to be parseable"))
}
/// Object ID of the object stored in JavaScript memory, which can be used
/// to efficiently fetch a fresh reference to the object on subsequent
/// ticks, or `None` if the object doesn't currently have an ID.
fn try_js_id(&self) -> Option<JsObjectId<Self>>
where
Self: Sized,
{
self.try_js_raw_id().map(Into::into)
}
/// Object ID of the object stored in JavaScript memory, without its
/// associated type information, or `None` if the object doesn't currently
/// have an ID.
fn try_js_raw_id(&self) -> Option<JsString>;
}
impl<T> MaybeHasId for T
where
T: HasId,
{
fn try_js_raw_id(&self) -> Option<JsString> {
Some(self.js_raw_id())
}
}
#[enum_dispatch]
pub trait HasPosition {
/// Position of the object.
fn pos(&self) -> Position;
}
#[enum_dispatch]
pub trait MaybeHasPosition {
/// Position of the object, or `None` if the object is a power creep not
/// spawned on the current shard.
fn try_pos(&self) -> Option<Position>;
}
pub trait CostMatrixSet {
fn set_xy(&mut self, xy: RoomXY, cost: u8);
}
pub trait CostMatrixGet {
fn get_xy(&mut self, xy: RoomXY) -> u8;
}
#[enum_dispatch]
pub trait HasStore {
/// The store of the object, containing information about the resources it
/// is holding.
fn store(&self) -> Store;
}
#[enum_dispatch]
pub trait OwnedStructureProperties {
/// Whether this structure is owned by the player.
///
/// [Screeps documentation](https://docs.screeps.com/api/#OwnedStructure.my)
fn my(&self) -> bool;
/// The [`Owner`] of this structure that contains the owner's username, or
/// `None` if it's an ownable structure currently not under a player's
/// control.
///
/// [Screeps documentation](https://docs.screeps.com/api/#OwnedStructure.owner)
fn owner(&self) -> Option<Owner>;
}
#[enum_dispatch]
pub trait RoomObjectProperties {
/// Effects applied to the object.
///
/// [Screeps documentation](https://docs.screeps.com/api/#RoomObject.effects)
fn effects(&self) -> Vec<Effect>;
/// Effects applied to the object.
///
/// [Screeps documentation](https://docs.screeps.com/api/#RoomObject.effects)
fn effects_raw(&self) -> Option<Array>;
/// A link to the room that the object is currently in, or `None` if the
/// object is a power creep not spawned on the current shard, or a flag or
/// construction site not in a visible room.
///
/// [Screeps documentation](https://docs.screeps.com/api/#RoomObject.room)
fn room(&self) -> Option<Room>;
}
#[enum_dispatch]
pub trait SharedCreepProperties {
/// A shortcut to the part of the `Memory` tree used for this creep by
/// default
fn memory(&self) -> JsValue;
/// Sets a new value to the memory object shortcut for this creep.
fn set_memory(&self, val: &JsValue);
/// Whether this creep is owned by the player.
fn my(&self) -> bool;
/// The creep's name as an owned reference to a [`String`].
///
/// [Screeps documentation](https://docs.screeps.com/api/#Creep.name)
fn name(&self) -> String;
/// The [`Owner`] of this creep that contains the owner's username.
fn owner(&self) -> Owner;
/// What the creep said last tick.
fn saying(&self) -> Option<JsString>;
/// The number of ticks the creep has left to live.
fn ticks_to_live(&self) -> Option<u32>;
/// Cancel an a successfully called creep function from earlier in the tick,
/// with a [`JsString`] that must contain the JS version of the function
/// name.
fn cancel_order(&self, target: &JsString) -> Result<(), ErrorCode>;
/// Drop a resource on the ground from the creep's [`Store`].
fn drop(&self, ty: ResourceType, amount: Option<u32>) -> Result<(), ErrorCode>;
/// Move one square in the specified direction.
fn move_direction(&self, direction: Direction) -> Result<(), ErrorCode>;
/// Move the creep along a previously determined path returned from a
/// pathfinding function, in array or serialized string form.
fn move_by_path(&self, path: &JsValue) -> Result<(), ErrorCode>;
/// Move the creep toward the specified goal, either a [`RoomPosition`] or
/// [`RoomObject`]. Note that using this function will store data in
/// `Memory.creeps[creep_name]` and enable the default serialization
/// behavior of the `Memory` object, which may hamper attempts to directly
/// use `RawMemory`.
fn move_to<T>(&self, target: T) -> Result<(), ErrorCode>
where
T: HasPosition;
/// Move the creep toward the specified goal, either a [`RoomPosition`] or
/// [`RoomObject`]. Note that using this function will store data in
/// `Memory.creeps[creep_name]` and enable the default serialization
/// behavior of the `Memory` object, which may hamper attempts to directly
/// use `RawMemory`.
fn move_to_with_options<T, F>(
&self,
target: T,
options: Option<MoveToOptions<F>>,
) -> Result<(), ErrorCode>
where
T: HasPosition,
F: FnMut(RoomName, CostMatrix) -> SingleRoomCostResult;
/// Whether to send an email notification when this creep is attacked.
fn notify_when_attacked(&self, enabled: bool) -> Result<(), ErrorCode>;
/// Pick up a [`Resource`] in melee range (or at the same position as the
/// creep).
fn pickup(&self, target: &Resource) -> Result<(), ErrorCode>;
/// Display a string in a bubble above the creep next tick. 10 character
/// limit.
fn say(&self, message: &str, public: bool) -> Result<(), ErrorCode>;
/// Immediately kill the creep.
fn suicide(&self) -> Result<(), ErrorCode>;
/// Transfer a resource from the creep's store to [`Structure`],
/// [`PowerCreep`], or another [`Creep`].
fn transfer<T>(
&self,
target: &T,
ty: ResourceType,
amount: Option<u32>,
) -> Result<(), ErrorCode>
where
T: Transferable + ?Sized;
/// Withdraw a resource from a [`Structure`], [`Tombstone`], or [`Ruin`].
fn withdraw<T>(
&self,
target: &T,
ty: ResourceType,
amount: Option<u32>,
) -> Result<(), ErrorCode>
where
T: Withdrawable + ?Sized;
}
#[enum_dispatch]
pub trait StructureProperties {
fn structure_type(&self) -> StructureType;
fn destroy(&self) -> Result<(), ErrorCode>;
fn is_active(&self) -> bool;
fn notify_when_attacked(&self, val: bool) -> Result<(), ErrorCode>;
}
/// Trait for all wrappers over Screeps JavaScript objects which can be the
/// target of `Creep.transfer`.
///
/// # Contracts
///
/// The reference returned from `AsRef<RoomObject>::as_ref` must be a valid
/// target for `Creep.transfer`.
#[enum_dispatch]
pub trait Transferable: AsRef<RoomObject> {}
/// Trait for all wrappers over Screeps JavaScript objects which can be the
/// target of `Creep.withdraw`.
///
/// # Contracts
///
/// The reference returned from `AsRef<RoomObject>::as_ref` must be a valid
/// target for `Creep.withdraw`.
pub trait Withdrawable: AsRef<RoomObject> {}
/// Trait for all wrappers over Screeps JavaScript objects which can be the
/// target of `Creep.harvest`.
///
/// # Contracts
///
/// The reference returned from `AsRef<RoomObject>::as_ref` must be a valid
/// target for `Creep.harvest`.
pub trait Harvestable: AsRef<RoomObject> {}
/// Trait for all wrappers over Screeps JavaScript objects which can be the
/// target of `Creep.attack`.
///
/// # Contracts
///
/// The reference returned from `AsRef<RoomObject>::as_ref` must be a valid
/// target for `Creep.attack`.
pub trait Attackable: HasHits + AsRef<RoomObject> {}
/// Trait for all wrappers over Screeps JavaScript objects which can be the
/// target of `Creep.dismantle`.
///
/// # Contracts
///
/// The reference returned from `AsRef<Structure>::as_ref` must be a valid
/// target for `Creep.dismantle`.
pub trait Dismantleable: HasHits + AsRef<Structure> {}
/// Trait for all wrappers over Screeps JavaScript objects which can be the
/// target of `Creep.repair` or `StructureTower.repair`.
///
/// # Contracts
///
/// The reference returned from `AsRef<Structure>::as_ref` must be a valid
/// target for repair.
pub trait Repairable: HasHits + AsRef<Structure> {}
/// Trait for all wrappers over Screeps JavaScript objects which can be the
/// target of `Creep.heal`.
///
/// # Contracts
///
/// The reference returned from `AsRef<RoomObject>::as_ref` must be a valid
/// target for `Creep.heal`.
pub trait Healable: AsRef<RoomObject> {}