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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
//! Traits associated with how specific [game objects] can be used.
//!
//! [game objects]: crate::objects
use std::{borrow::Borrow, 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},
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;
}
pub trait HasNativeId {
fn native_id(&self) -> JsString;
}
pub trait MaybeHasNativeId {
fn try_native_id(&self) -> Option<JsString>;
}
impl<T> MaybeHasNativeId for T
where
T: HasNativeId,
{
fn try_native_id(&self) -> Option<JsString> {
Some(<Self as HasNativeId>::native_id(self))
}
}
pub trait Resolvable: From<JsValue> {}
impl<T> Resolvable for T where T: MaybeHasTypedId<T> + From<JsValue> {}
#[enum_dispatch]
pub trait HasId {
/// Object ID of the object, which can be used to efficiently fetch a
/// fresh reference to the object on subsequent ticks.
fn raw_id(&self) -> RawObjectId;
}
impl<T> HasId for T
where
T: HasNativeId,
{
fn raw_id(&self) -> RawObjectId {
let id: String = self.native_id().into();
RawObjectId::from_str(&id).expect("expected object ID to be parseable")
}
}
#[enum_dispatch]
pub trait HasTypedId<T> {
/// Object ID of the object, which can be used to efficiently fetch a
/// fresh reference to the object on subsequent ticks.
fn id(&self) -> ObjectId<T>;
fn js_id(&self) -> JsObjectId<T>;
}
impl<T> HasTypedId<T> for T
where
T: HasId + HasNativeId,
{
fn id(&self) -> ObjectId<T> {
self.raw_id().into()
}
fn js_id(&self) -> JsObjectId<T> {
self.native_id().into()
}
}
impl<T> HasTypedId<T> for &T
where
T: HasId + HasNativeId,
{
fn id(&self) -> ObjectId<T> {
self.raw_id().into()
}
fn js_id(&self) -> JsObjectId<T> {
self.native_id().into()
}
}
#[enum_dispatch]
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_raw_id(&self) -> Option<RawObjectId>;
}
impl<T> MaybeHasId for T
where
T: MaybeHasNativeId,
{
fn try_raw_id(&self) -> Option<RawObjectId> {
self.try_native_id()
.map(String::from)
.map(|id| RawObjectId::from_str(&id).expect("expected object ID to be parseable"))
}
}
#[enum_dispatch]
pub trait MaybeHasTypedId<T> {
/// 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<T>>;
}
impl<T> MaybeHasTypedId<T> for T
where
T: MaybeHasId,
{
fn try_id(&self) -> Option<ObjectId<T>> {
self.try_raw_id().map(Into::into)
}
}
impl<T> MaybeHasTypedId<T> for &T
where
T: MaybeHasId,
{
fn try_id(&self) -> Option<ObjectId<T>> {
self.try_raw_id().map(Into::into)
}
}
#[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 HasLocalPosition {
fn x(&self) -> u8;
fn y(&self) -> u8;
}
pub trait CostMatrixSet {
fn set<P, V>(&mut self, position: P, cost: V)
where
P: HasLocalPosition,
V: Borrow<u8>;
fn set_multi<D, B, P, V>(&mut self, data: D)
where
D: IntoIterator<Item = B>,
B: Borrow<(P, V)>,
P: HasLocalPosition,
V: Borrow<u8>;
}
#[inline]
fn pos_as_idx(x: u8, y: u8) -> usize {
(x as usize) * ROOM_SIZE as usize + (y as usize)
}
impl CostMatrixSet for CostMatrix {
fn set<P, V>(&mut self, position: P, cost: V)
where
P: HasLocalPosition,
V: Borrow<u8>,
{
CostMatrix::set(self, position.x(), position.y(), *cost.borrow());
}
fn set_multi<D, B, P, V>(&mut self, data: D)
where
D: IntoIterator<Item = B>,
B: Borrow<(P, V)>,
P: HasLocalPosition,
V: Borrow<u8>,
{
let matrix_buffer = self.get_bits();
for entry in data.into_iter() {
let (pos, cost) = entry.borrow();
let offset = pos_as_idx(pos.x(), pos.y());
matrix_buffer.set_index(offset as u32, *cost.borrow());
}
}
}
#[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.heal`.
///
/// # Contracts
///
/// The reference returned from `AsRef<RoomObject>::as_ref` must be a valid
/// target for `Creep.heal`.
pub trait Healable: AsRef<RoomObject> {}
// NOTE: keep impls for Structure* in sync with accessor methods in
// src/objects/structure.rs
impl Transferable for StructureExtension {}
impl Transferable for Creep {}
impl Transferable for StructureContainer {}
impl Transferable for StructureFactory {}
impl Transferable for StructureLab {}
impl Transferable for StructureLink {}
impl Transferable for StructureNuker {}
impl Transferable for StructureSpawn {}
impl Transferable for StructureStorage {}
impl Transferable for StructureTower {}
impl Transferable for StructurePowerSpawn {}
impl Transferable for StructureTerminal {}
impl Transferable for PowerCreep {}
#[cfg(feature = "score")]
impl Transferable for ScoreCollector {}
#[cfg(feature = "symbols")]
impl Transferable for SymbolDecoder {}
#[cfg(feature = "thorium")]
impl Transferable for Reactor {}
// NOTE: keep impls for Structure* in sync with accessor methods in
// src/objects/structure.rs
impl Withdrawable for Ruin {}
impl Withdrawable for StructureExtension {}
impl Withdrawable for StructureContainer {}
impl Withdrawable for StructureFactory {}
impl Withdrawable for StructureLab {}
impl Withdrawable for StructureLink {}
impl Withdrawable for StructureSpawn {}
impl Withdrawable for StructureStorage {}
impl Withdrawable for StructureTower {}
impl Withdrawable for StructurePowerSpawn {}
impl Withdrawable for StructureTerminal {}
impl Withdrawable for Tombstone {}
#[cfg(feature = "score")]
impl Withdrawable for ScoreContainer {}
#[cfg(feature = "symbols")]
impl Withdrawable for SymbolContainer {}
impl Harvestable for Deposit {}
impl Harvestable for Mineral {}
impl Harvestable for Source {}
// NOTE: keep impls for Structure* in sync with accessor methods in
// src/objects/structure.rs
impl Attackable for Creep {}
impl Attackable for OwnedStructure {}
impl Attackable for StructureContainer {}
impl Attackable for StructureExtension {}
impl Attackable for StructureExtractor {}
impl Attackable for StructureFactory {}
impl Attackable for StructureInvaderCore {}
impl Attackable for StructureKeeperLair {}
impl Attackable for StructureLab {}
impl Attackable for StructureLink {}
impl Attackable for StructureNuker {}
impl Attackable for StructureObserver {}
impl Attackable for StructurePowerBank {}
impl Attackable for StructurePowerSpawn {}
impl Attackable for StructureRampart {}
impl Attackable for StructureRoad {}
impl Attackable for StructureSpawn {}
impl Attackable for StructureStorage {}
impl Attackable for StructureTerminal {}
impl Attackable for StructureTower {}
impl Attackable for StructureWall {}
impl Attackable for PowerCreep {}
// NOTE: keep impls for Structure* in sync with accessor methods in
// src/objects/structure.rs
impl Dismantleable for StructureContainer {}
impl Dismantleable for StructureExtension {}
impl Dismantleable for StructureExtractor {}
impl Dismantleable for StructureFactory {}
impl Dismantleable for StructureLab {}
impl Dismantleable for StructureLink {}
impl Dismantleable for StructureNuker {}
impl Dismantleable for StructureObserver {}
impl Dismantleable for StructurePowerBank {}
impl Dismantleable for StructurePowerSpawn {}
impl Dismantleable for StructureRampart {}
impl Dismantleable for StructureRoad {}
impl Dismantleable for StructureSpawn {}
impl Dismantleable for StructureStorage {}
impl Dismantleable for StructureTerminal {}
impl Dismantleable for StructureTower {}
impl Dismantleable for StructureWall {}
impl Healable for Creep {}
impl Healable for PowerCreep {}
//TODO: wiarchbe: Add Repairable trait.