screeps/
constants.rs

1//! Constants, most copied from [the game constants][1].
2//!
3//! Last updated on 2020-12-13, at commit
4//! `e4589666113334bb1f967b9a5540b642141b6dab`.
5//!
6//! Currently missing:
7//! - OBSTACLE_OBJECT_TYPES
8//! - WORLD_WIDTH / WORLD_HEIGHT (deprecated in Screeps)
9//! - POWER_INFO
10//!
11//! [1]: https://github.com/screeps/common/commits/master/lib/constants.js
12
13use std::{error::Error, fmt};
14
15#[derive(Debug, Clone)]
16pub struct InvalidConstantString(String);
17
18impl fmt::Display for InvalidConstantString {
19    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20        write!(f, "value not valid for constant: {}", self.0)
21    }
22}
23
24impl Error for InvalidConstantString {}
25
26pub(crate) mod macros {
27    macro_rules! named_enum_serialize_deserialize {
28        ($ty:ty) => {
29            impl<'de> Deserialize<'de> for $ty {
30                fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
31                where
32                    D: serde::Deserializer<'de>,
33                {
34                    let s: Cow<'de, str> = Cow::deserialize(deserializer)?;
35                    <$ty>::from_str(&s).ok_or(D::Error::invalid_value(
36                        Unexpected::Str(&s),
37                        &stringify!($ty),
38                    ))
39                }
40            }
41            impl Serialize for $ty {
42                fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
43                where
44                    S: serde::Serializer,
45                {
46                    serializer.serialize_str(self.to_str())
47                }
48            }
49            impl fmt::Display for $ty {
50                fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51                    write!(f, "{}", self.to_str())
52                }
53            }
54            impl std::str::FromStr for $ty {
55                type Err = InvalidConstantString;
56
57                fn from_str(s: &str) -> Result<Self, Self::Err> {
58                    Self::from_str(s).ok_or(InvalidConstantString(s.to_owned()))
59                }
60            }
61        };
62    }
63    pub(crate) use named_enum_serialize_deserialize;
64}
65
66pub mod extra;
67pub mod find;
68pub mod look;
69mod numbers;
70mod recipes;
71pub mod seasonal;
72mod small_enums;
73mod types;
74
75pub use self::{
76    extra::*, find::FindConstant, look::LookConstant, numbers::*, recipes::FactoryRecipe,
77    small_enums::*, types::*,
78};
79
80/// Re-export of all constants related to [`Creep`] behavior and operations.
81///
82/// [`Creep`]: crate::objects::Creep
83pub mod creep {
84    pub use super::{
85        extra::{
86            CREEP_HITS_PER_PART, CREEP_NAME_MAX_LENGTH, CREEP_RANGED_ACTION_RANGE,
87            CREEP_SAY_MAX_LENGTH, MOVE_COST_PLAIN, MOVE_COST_ROAD, MOVE_COST_SWAMP, MOVE_POWER,
88            RANGED_MASS_ATTACK_POWER_RANGE_1, RANGED_MASS_ATTACK_POWER_RANGE_2,
89            RANGED_MASS_ATTACK_POWER_RANGE_3,
90        },
91        numbers::{
92            ATTACK_POWER, BUILD_POWER, CARRY_CAPACITY, CREEP_CLAIM_LIFE_TIME, CREEP_CORPSE_RATE,
93            CREEP_LIFE_TIME, CREEP_PART_MAX_ENERGY, CREEP_SPAWN_TIME, DISMANTLE_COST,
94            HARVEST_DEPOSIT_POWER, HARVEST_MINERAL_POWER, HARVEST_POWER, HEAL_POWER,
95            MAX_CREEP_SIZE, RANGED_HEAL_POWER, REPAIR_COST, REPAIR_POWER, SPAWN_RENEW_RATIO,
96            UPGRADE_CONTROLLER_POWER,
97        },
98        small_enums::Part,
99    };
100}
101
102/// Re-export of all constants related to structures.
103pub mod structure {
104    pub use super::{
105        extra::{CONSTRUCTION_SITE_DROP_RATIO, LAB_REACTION_RANGE, TERMINAL_SEND_COST_SCALE},
106        numbers::{
107            extension_energy_capacity, invader_core_creep_spawn_time, invader_core_expand_time,
108            rampart_hits_max, ruin_decay_structures, stronghold_rampart_hits,
109            CONSTRUCTION_COST_ROAD_SWAMP_RATIO, CONSTRUCTION_COST_ROAD_WALL_RATIO,
110            CONTAINER_CAPACITY, CONTAINER_DECAY, CONTAINER_DECAY_TIME, CONTAINER_DECAY_TIME_OWNED,
111            CONTAINER_HITS, EXTENSION_HITS, EXTRACTOR_COOLDOWN, EXTRACTOR_HITS, FACTORY_CAPACITY,
112            FACTORY_HITS, INVADER_CORE_CONTROLLER_DOWNGRADE, INVADER_CORE_CONTROLLER_POWER,
113            INVADER_CORE_HITS, LAB_ENERGY_CAPACITY, LAB_HITS, LAB_MINERAL_CAPACITY, LINK_CAPACITY,
114            LINK_COOLDOWN, LINK_HITS, LINK_LOSS_RATIO, NUKER_COOLDOWN, NUKER_ENERGY_CAPACITY,
115            NUKER_GHODIUM_CAPACITY, NUKER_HITS, OBSERVER_HITS, POWER_BANK_HITS,
116            POWER_SPAWN_ENERGY_CAPACITY, POWER_SPAWN_HITS, POWER_SPAWN_POWER_CAPACITY,
117            RAMPART_DECAY_AMOUNT, RAMPART_DECAY_TIME, RAMPART_HITS, RAMPART_HITS_MAX_RCL2,
118            RAMPART_HITS_MAX_RCL3, RAMPART_HITS_MAX_RCL4, RAMPART_HITS_MAX_RCL5,
119            RAMPART_HITS_MAX_RCL6, RAMPART_HITS_MAX_RCL7, RAMPART_HITS_MAX_RCL8, ROAD_DECAY_AMOUNT,
120            ROAD_DECAY_TIME, ROAD_HITS, ROAD_WEAROUT, ROAD_WEAROUT_POWER_CREEP, RUIN_DECAY,
121            SPAWN_ENERGY_CAPACITY, SPAWN_HITS, STORAGE_CAPACITY, STORAGE_HITS,
122            STRONGHOLD_DECAY_TICKS, TERMINAL_CAPACITY, TERMINAL_HITS, TERMINAL_SEND_COST,
123            TOWER_CAPACITY, TOWER_HITS, WALL_HITS, WALL_HITS_MAX,
124        },
125        types::StructureType,
126    };
127}
128
129/// Re-export of all constants related to [`Mineral`] behavior and
130/// [`StructureLab`] operations.
131///
132/// [`StructureLab`]: crate::objects::StructureLab
133/// [`Mineral`]: crate::objects::Mineral
134pub mod minerals {
135    pub use super::{
136        numbers::{
137            mineral_min_amount, LAB_BOOST_ENERGY, LAB_BOOST_MINERAL, LAB_ENERGY_CAPACITY,
138            LAB_MINERAL_CAPACITY, LAB_REACTION_AMOUNT, LAB_UNBOOST_ENERGY, LAB_UNBOOST_MINERAL,
139            MINERAL_DENSITY_CHANGE, MINERAL_RANDOM_FACTOR, MINERAL_REGEN_TIME,
140        },
141        small_enums::Density,
142        types::ResourceType,
143    };
144}
145
146/// Re-export of all constants related to [`StructureController`] behavior and
147/// GCL.
148///
149/// [`StructureController`]: crate::objects::StructureController
150pub mod control {
151    pub use super::{
152        extra::{CONTROLLER_DOWNGRADE_PROGRESS_RATIO, CONTROLLER_SIGN_MAX_LENGTH},
153        numbers::{
154            controller_downgrade, controller_levels, CONTROLLER_ATTACK_BLOCKED_UPGRADE,
155            CONTROLLER_CLAIM_DOWNGRADE, CONTROLLER_DOWNGRADE_RESTORE,
156            CONTROLLER_DOWNGRADE_SAFEMODE_THRESHOLD, CONTROLLER_MAX_UPGRADE_PER_TICK,
157            CONTROLLER_NUKE_BLOCKED_UPGRADE, CONTROLLER_RESERVE, CONTROLLER_RESERVE_MAX,
158            GCL_MULTIPLY, GCL_NOVICE, GCL_POW, SAFE_MODE_COOLDOWN, SAFE_MODE_COST,
159            SAFE_MODE_DURATION, SIGN_PLANNED_AREA, SYSTEM_USERNAME,
160        },
161    };
162}
163
164/// Re-export of all constants related to power.
165pub mod power {
166    pub use super::{
167        extra::{
168            POWER_CREEP_CARRY_CAPACITY_PER_LEVEL, POWER_CREEP_HITS_PER_LEVEL,
169            POWER_CREEP_NAME_MAX_LENGTH,
170        },
171        numbers::{
172            POWER_BANK_CAPACITY_MAX, POWER_BANK_CAPACITY_MIN, POWER_BANK_DECAY, POWER_BANK_HITS,
173            POWER_BANK_HIT_BACK, POWER_BANK_RESPAWN_TIME, POWER_CREEP_DELETE_COOLDOWN,
174            POWER_CREEP_LIFE_TIME, POWER_CREEP_MAX_LEVEL, POWER_CREEP_SPAWN_COOLDOWN,
175            POWER_LEVEL_MULTIPLY, POWER_LEVEL_POW, POWER_SPAWN_ENERGY_CAPACITY,
176            POWER_SPAWN_ENERGY_RATIO, POWER_SPAWN_POWER_CAPACITY,
177        },
178        types::{PowerCreepClass, PowerType},
179    };
180}
181
182/// Re-export of all constants related to [`StructurePortal`] behavior.
183///
184/// [`StructurePortal`]: crate::objects::StructurePortal
185pub mod portal {
186    pub use super::numbers::{
187        PORTAL_DECAY, PORTAL_MAX_TIMEOUT, PORTAL_MIN_TIMEOUT, PORTAL_UNSTABLE,
188    };
189}
190
191/// Re-export of all constants related to [`Source`] behavior.
192///
193/// [`Source`]: crate::objects::Source
194pub mod source {
195    pub use super::numbers::{
196        ENERGY_DECAY, ENERGY_REGEN_TIME, INVADERS_ENERGY_GOAL, SOURCE_ENERGY_CAPACITY,
197        SOURCE_ENERGY_KEEPER_CAPACITY, SOURCE_ENERGY_NEUTRAL_CAPACITY,
198    };
199}
200
201/// Re-export of all constants related to the [market].
202///
203/// [market]: crate::game::market
204pub mod market {
205    pub use super::{
206        extra::MARKET_MAX_DEALS_PER_TICK,
207        numbers::{
208            MARKET_FEE, MARKET_MAX_ORDERS, MARKET_ORDER_LIFE_TIME, TERMINAL_CAPACITY,
209            TERMINAL_COOLDOWN, TERMINAL_MIN_SEND, TERMINAL_SEND_COST,
210        },
211    };
212}
213
214/// Re-export of all constants related to [`StructureSpawn`] operations.
215///
216/// [`StructureSpawn`]: crate::objects::StructureSpawn
217pub mod spawn {
218    pub use super::{
219        extra::SPAWN_NAME_MAX_LENGTH,
220        numbers::{
221            extension_energy_capacity, CREEP_SPAWN_TIME, ENERGY_REGEN_TIME, MAX_CREEP_SIZE,
222            SPAWN_ENERGY_CAPACITY, SPAWN_ENERGY_START, SPAWN_RENEW_RATIO,
223        },
224    };
225}
226
227/// Re-export of all constants related to [`StructureTower`] operations.
228///
229/// [`StructureTower`]: crate::objects::StructureTower
230pub mod tower {
231    pub use super::numbers::{
232        TOWER_CAPACITY, TOWER_ENERGY_COST, TOWER_FALLOFF, TOWER_FALLOFF_RANGE, TOWER_OPTIMAL_RANGE,
233        TOWER_POWER_ATTACK, TOWER_POWER_HEAL, TOWER_POWER_REPAIR,
234    };
235}
236
237/// Re-export of all constants related to [`StructureNuker`] and [`Nuke`].
238///
239/// [`StructureNuker`]: crate::objects::StructureNuker
240/// [`Nuke`]: crate::objects::Nuke
241pub mod nuke {
242    pub use super::numbers::{
243        NUKER_COOLDOWN, NUKER_ENERGY_CAPACITY, NUKER_GHODIUM_CAPACITY, NUKE_DAMAGE_RANGE_0,
244        NUKE_DAMAGE_RANGE_2, NUKE_LAND_TIME, NUKE_RANGE,
245    };
246}
247
248/// Re-export of all constants related to [`StructureObserver`] operations.
249///
250/// [`StructureObserver`]: crate::objects::StructureObserver
251pub mod observer {
252    pub use super::numbers::OBSERVER_RANGE;
253}
254
255/// Re-export of all constants related to [`Resource`]s.
256///
257/// [`Resource`]: crate::objects::Resource
258pub mod resource {
259    pub use super::{
260        numbers::ENERGY_DECAY,
261        types::{ResourceType, RESOURCES_ALL},
262    };
263}
264
265/// Re-export of all constants related to [`Tombstone`]s.
266///
267/// [`Tombstone`]: crate::objects::Tombstone
268pub mod tombstone {
269    pub use super::numbers::{TOMBSTONE_DECAY_PER_PART, TOMBSTONE_DECAY_POWER_CREEP};
270}
271
272/// Re-export of all constants related to [`Flag`]s.
273///
274/// [`Flag`]: crate::objects::Flag
275pub mod flags {
276    pub use super::{extra::FLAG_NAME_MAX_LENGTH, numbers::FLAGS_LIMIT, small_enums::Color};
277}