diff options
Diffstat (limited to 'crates/fparkan-runtime/src/production.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/production.rs | 77 |
1 files changed, 69 insertions, 8 deletions
diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs index 3b407467..3502c45d 100644 --- a/crates/fparkan-runtime/src/production.rs +++ b/crates/fparkan-runtime/src/production.rs @@ -34,6 +34,7 @@ pub(super) struct Production { reserve_start: usize, schemes: Vec<NormalizedPath>, player_scheme_count: usize, + pub(super) saved_schemes: std::ops::Range<usize>, customized: bool, pub(super) scheme_vfs: Option<Arc<dyn Vfs>>, ai_schemes: Vec<crate::clan_factory_schemes::ScriptDesign>, @@ -107,6 +108,10 @@ impl PaidOrder { } impl Production { + fn player_project(&self, scheme: usize) -> bool { + scheme < self.player_scheme_count || self.saved_schemes.contains(&scheme) + } + pub(super) fn clock_ticks(&self) -> impl Iterator<Item = (u16, u32, u64)> + '_ { self.clock_ticks .iter() @@ -165,6 +170,7 @@ impl Production { reserve_start, schemes, player_scheme_count, + saved_schemes: 0..0, customized, ai_schemes, controls, @@ -384,9 +390,7 @@ pub fn factory_schemes( ) -> Result<Vec<(usize, &str)>, EngineError> { let (mission, production) = loaded_production(engine)?; let (plant, _) = factory_at_communicator(&engine.world, mission, production, cockpit)?; - Ok(schemes_for_plant(mission, production, plant) - .filter(|&(scheme, _)| scheme < production.player_scheme_count) - .collect()) + Ok(recent_schemes_for_plant(mission, production, plant)) } /// Lists the live factories of the player's clan and matching configured schemes. @@ -398,8 +402,8 @@ pub fn clan_factory_schemes( let owner = player_owner(&engine.world, cockpit)?; Ok(owned_factories(&engine.world, mission, production, owner) .flat_map(|(index, factory, _)| { - schemes_for_plant(mission, production, index) - .filter(|&(scheme, _)| scheme < production.player_scheme_count) + recent_schemes_for_plant(mission, production, index) + .into_iter() .map(move |(scheme, path)| (factory, scheme, path)) }) .collect()) @@ -440,10 +444,64 @@ pub fn factory_part_variants( /// Index of the first prepared world root for a player factory project. pub fn factory_project_root(engine: &Engine, scheme: usize) -> Option<usize> { let (_, production) = loaded_production(engine).ok()?; - (scheme < production.player_scheme_count) + production + .player_project(scheme) .then(|| production.reserve_start + scheme * RESERVE_SLOTS) } +/// Prepared project indices usable by the designer, including saved geometry. +pub fn factory_project_schemes(engine: &Engine) -> impl Iterator<Item = usize> { + let (recent, saved) = loaded_production(engine).map_or((0..0, 0..0), |(_, production)| { + ( + 0..production.player_scheme_count, + production.saved_schemes.clone(), + ) + }); + recent.chain(saved) +} + +/// Saved projects prepared for the current factory. +pub fn factory_saved_schemes( + engine: &Engine, + cockpit: StartingCockpit, + plant: Option<ObjectHandle>, +) -> Result<Vec<usize>, EngineError> { + let (mission, production) = loaded_production(engine)?; + let index = match plant { + Some(plant) => { + let owner = player_owner(&engine.world, cockpit)?; + owned_factories(&engine.world, mission, production, owner) + .find(|&(_, handle, _)| handle == plant) + .map(|(index, _, _)| index) + .ok_or_else(|| { + production_error("selected factory no longer belongs to the player") + })? + } + None => factory_at_communicator(&engine.world, mission, production, cockpit)?.0, + }; + Ok(schemes_for_plant(mission, production, index) + .filter_map(|(scheme, _)| production.saved_schemes.contains(&scheme).then_some(scheme)) + .collect()) +} + +fn recent_schemes_for_plant<'a>( + mission: &'a LoadedMissionState, + production: &'a Production, + plant: usize, +) -> Vec<(usize, &'a str)> { + let mut recent = schemes_for_plant(mission, production, plant) + .filter(|&(scheme, _)| scheme < production.player_scheme_count) + .collect::<Vec<_>>(); + if recent.is_empty() { + recent.extend( + schemes_for_plant(mission, production, plant) + .filter(|&(scheme, _)| production.saved_schemes.contains(&scheme)) + .take(1), + ); + } + recent +} + fn schemes_for_plant<'a>( mission: &'a LoadedMissionState, production: &'a Production, @@ -500,7 +558,7 @@ pub fn order_factory_design( if engine .production .as_ref() - .is_some_and(|production| scheme >= production.player_scheme_count) + .is_some_and(|production| !production.player_project(scheme)) { return Err(production_error("unknown factory scheme")); } @@ -592,7 +650,10 @@ fn order_owned_factory( }) .unwrap_or(&draft.unit_components); let (duration_ms, paid) = if is_free { - if changes.is_empty() && production.customized && scheme == 0 { + if changes.is_empty() + && ((production.customized && scheme == 0) + || production.saved_schemes.contains(&scheme)) + { clan_scheme_cost(&engine.services.vfs, mission, owner, records)?; } (free_ms, None) |
