From f78d5445aded9796883b87b49181a423c7f9ba32 Mon Sep 17 00:00:00 2001 From: asquared31415 <34665709+asquared31415@users.noreply.github.com> Date: Thu, 29 Feb 2024 10:29:36 -0500 Subject: [PATCH] add `RESOURCES_ALL` constant and `StoreObject::resource_types` (#499) --- CHANGELOG.md | 8 ++- src/constants.rs | 5 +- src/constants/types.rs | 144 ++++++++++++++++++++++++++++++++++++++++- src/enums.rs | 57 +++++++++++++++- 4 files changed, 209 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0931767b..e0107575 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,13 @@ Unreleased - Add `RoomCoordinate::is_room_edge`, `RoomXY::is_room_edge`, and `Position::is_room_edge` - Add new extra constant `constants::extra::CONTROLLER_DOWNGRADE_PROGRESS_RATIO`, which is the percentage toward the previous level that a downgrading controller's progress is set to -- Add new extra constants `constants::extra::MOVE_COST_PLAIN`, `MOVE_COST_ROAD`, `MOVE_COST_SWAMP` +- Add new extra constants `constants::extra::{MOVE_COST_PLAIN, MOVE_COST_ROAD, MOVE_COST_SWAMP}` +- Add constant `screeps::constants::resources::RESOURCES_ALL` (also accessible as + `screeps::RESOURCES_ALL`) that contains every resource type +- Add function `StoreObject::resource_types` to get the types of resources that a store can hold + - This can be used with a `StructureObject` as `StoreObject::try_from(structure_object).map(|store| store.resource_types())` + which returns a `Result<&'static [ResourceType], StoreObjectConversionError>` +- Add missing `StoreObject::Reactor` to the `seasonal-season-5` feature. 0.20.1 (2024-01-09) =================== diff --git a/src/constants.rs b/src/constants.rs index 1849b770..da87423f 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -256,7 +256,10 @@ pub mod observer { /// /// [`Resource`]: crate::objects::Resource pub mod resource { - pub use super::{numbers::ENERGY_DECAY, types::ResourceType}; + pub use super::{ + numbers::ENERGY_DECAY, + types::{ResourceType, RESOURCES_ALL}, + }; } /// Re-export of all constants related to [`Tombstone`]s. diff --git a/src/constants/types.rs b/src/constants/types.rs index d9854d54..9782f2e7 100644 --- a/src/constants/types.rs +++ b/src/constants/types.rs @@ -199,8 +199,8 @@ pub enum IntershardResourceType { named_enum_serialize_deserialize!(IntershardResourceType); -/// Translates `RESOURCES_ALL` constant, representing all possible in-game -/// (non-intershard) resources. +/// Translates the values of the `RESOURCES_ALL` constant, representing all +/// possible in-game (non-intershard) resources. #[wasm_bindgen] #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Sequence)] pub enum ResourceType { @@ -485,6 +485,146 @@ impl ResourceType { } } +/// A collection of all resource types. This is a direct translation of the +/// `RESOURCES_ALL` constant in game using the rust `ResourceType` enum. +/// +/// Feature-specific resources are included in this list when the appropriate +/// feature is enabled. +pub const RESOURCES_ALL: &[ResourceType] = &[ + ResourceType::Power, + ResourceType::Energy, + ResourceType::Hydrogen, + ResourceType::Oxygen, + ResourceType::Utrium, + ResourceType::Lemergium, + ResourceType::Keanium, + ResourceType::Zynthium, + ResourceType::Catalyst, + ResourceType::Ghodium, + ResourceType::Silicon, + ResourceType::Metal, + ResourceType::Biomass, + ResourceType::Mist, + ResourceType::Hydroxide, + ResourceType::ZynthiumKeanite, + ResourceType::UtriumLemergite, + ResourceType::UtriumHydride, + ResourceType::UtriumOxide, + ResourceType::KeaniumHydride, + ResourceType::KeaniumOxide, + ResourceType::LemergiumHydride, + ResourceType::LemergiumOxide, + ResourceType::ZynthiumHydride, + ResourceType::ZynthiumOxide, + ResourceType::GhodiumHydride, + ResourceType::GhodiumOxide, + ResourceType::UtriumAcid, + ResourceType::UtriumAlkalide, + ResourceType::KeaniumAcid, + ResourceType::KeaniumAlkalide, + ResourceType::LemergiumAcid, + ResourceType::LemergiumAlkalide, + ResourceType::ZynthiumAcid, + ResourceType::ZynthiumAlkalide, + ResourceType::GhodiumAcid, + ResourceType::GhodiumAlkalide, + ResourceType::CatalyzedUtriumAcid, + ResourceType::CatalyzedUtriumAlkalide, + ResourceType::CatalyzedKeaniumAcid, + ResourceType::CatalyzedKeaniumAlkalide, + ResourceType::CatalyzedLemergiumAcid, + ResourceType::CatalyzedLemergiumAlkalide, + ResourceType::CatalyzedZynthiumAcid, + ResourceType::CatalyzedZynthiumAlkalide, + ResourceType::CatalyzedGhodiumAcid, + ResourceType::CatalyzedGhodiumAlkalide, + ResourceType::Ops, + ResourceType::UtriumBar, + ResourceType::LemergiumBar, + ResourceType::ZynthiumBar, + ResourceType::KeaniumBar, + ResourceType::GhodiumMelt, + ResourceType::Oxidant, + ResourceType::Reductant, + ResourceType::Purifier, + ResourceType::Battery, + ResourceType::Composite, + ResourceType::Crystal, + ResourceType::Liquid, + ResourceType::Wire, + ResourceType::Switch, + ResourceType::Transistor, + ResourceType::Microchip, + ResourceType::Circuit, + ResourceType::Device, + ResourceType::Cell, + ResourceType::Phlegm, + ResourceType::Tissue, + ResourceType::Muscle, + ResourceType::Organoid, + ResourceType::Organism, + ResourceType::Alloy, + ResourceType::Tube, + ResourceType::Fixtures, + ResourceType::Frame, + ResourceType::Hydraulics, + ResourceType::Machine, + ResourceType::Condensate, + ResourceType::Concentrate, + ResourceType::Extract, + ResourceType::Spirit, + ResourceType::Emanation, + ResourceType::Essence, + #[cfg(feature = "seasonal-season-1")] + ResourceType::Score, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolAleph, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolBeth, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolGimmel, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolDaleth, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolHe, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolWaw, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolZayin, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolHeth, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolTeth, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolYodh, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolKaph, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolLamedh, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolMem, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolNun, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolSamekh, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolAyin, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolPe, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolTsade, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolQoph, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolRes, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolSin, + #[cfg(feature = "seasonal-season-2")] + ResourceType::SymbolTaw, + #[cfg(feature = "seasonal-season-5")] + ResourceType::Thorium, +]; + /// Returned values from [`ResourceType::boost`] representing the effect of /// boosting a creep with the given resource. #[derive(Copy, Clone, Debug)] diff --git a/src/enums.rs b/src/enums.rs index 45f3920f..165016af 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -5,7 +5,7 @@ use enum_dispatch::enum_dispatch; use wasm_bindgen::{JsCast, JsValue}; -use crate::{objects::*, prelude::*}; +use crate::{objects::*, prelude::*, ResourceType, RESOURCES_ALL}; #[enum_dispatch(Attackable)] pub enum AttackableObject { @@ -165,6 +165,8 @@ pub enum ObjectWithPosition { pub enum StoreObject { Creep, PowerCreep, + #[cfg(feature = "seasonal-season-5")] + Reactor, Ruin, #[cfg(feature = "seasonal-season-1")] ScoreCollector, @@ -186,6 +188,59 @@ pub enum StoreObject { Tombstone, } +impl StoreObject { + /// All possible resources that this store may hold. + /// + /// Note: [`StructureLab`] is slightly odd in that it can hold any possible + /// resource (including non-lab resources like power), but only one + /// non-energy resource at a time. + /// + /// # Example + /// Assuming that `store_object` is a [`StoreObject::StructureExtension`]: + /// ```no_run + /// # use screeps::{ResourceType, StoreObject}; + /// # let store_object: StoreObject = todo!(); + /// assert_eq!(store_object.resource_types(), &[ResourceType::Energy]); + /// ``` + /// To access this property from a [`StructureObject`], use the following + /// conversion: + /// ```no_run + /// # use screeps::{ResourceType, StoreObject, StoreObjectConversionError, StructureObject}; + /// # let structure_object: StructureObject = todo!(); + /// let ty: Result<&[ResourceType], StoreObjectConversionError> = + /// StoreObject::try_from(structure_object).map(|store_obj| store_obj.resource_types()); + /// ``` + /// + /// Data collected on 2024-02-29 from + pub const fn resource_types(&self) -> &'static [ResourceType] { + match self { + StoreObject::Creep(_) => RESOURCES_ALL, + StoreObject::PowerCreep(_) => RESOURCES_ALL, + #[cfg(feature = "seasonal-season-5")] + StoreObject::Reactor(_) => &[ResourceType::Thorium], + StoreObject::Ruin(_) => RESOURCES_ALL, + #[cfg(feature = "seasonal-season-1")] + StoreObject::ScoreCollector(_) => &[ResourceType::Score], + #[cfg(feature = "seasonal-season-1")] + StoreObject::ScoreContainer(_) => &[ResourceType::Score], + StoreObject::StructureContainer(_) => RESOURCES_ALL, + StoreObject::StructureExtension(_) => &[ResourceType::Energy], + StoreObject::StructureFactory(_) => RESOURCES_ALL, + StoreObject::StructureLab(_) => RESOURCES_ALL, + StoreObject::StructureLink(_) => &[ResourceType::Energy], + StoreObject::StructureNuker(_) => &[ResourceType::Energy, ResourceType::Ghodium], + StoreObject::StructurePowerSpawn(_) => &[ResourceType::Energy, ResourceType::Power], + StoreObject::StructureSpawn(_) => &[ResourceType::Energy], + StoreObject::StructureStorage(_) => RESOURCES_ALL, + StoreObject::StructureTerminal(_) => RESOURCES_ALL, + StoreObject::StructureTower(_) => &[ResourceType::Energy], + #[cfg(feature = "seasonal-season-2")] + StoreObject::SymbolContainer(_) => &crate::constants::seasonal::season_2::SYMBOLS, + StoreObject::Tombstone(_) => RESOURCES_ALL, + } + } +} + /// Enum used for converting a [`Structure`] into a typed object of its specific /// structure type. #[enum_dispatch(OwnedStructureProperties)]