Skip to content

Commit

Permalink
add RESOURCES_ALL constant and StoreObject::resource_types (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
asquared31415 authored Feb 29, 2024
1 parent c9abd31 commit f78d544
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 5 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
===================
Expand Down
5 changes: 4 additions & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
144 changes: 142 additions & 2 deletions src/constants/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)]
Expand Down
57 changes: 56 additions & 1 deletion src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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 <https://github.com/screeps/engine/tree/97c9d12385fed686655c13b09f5f2457dd83a2bf>
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)]
Expand Down

0 comments on commit f78d544

Please sign in to comment.