From 5be1b800d028ba30c5e2a4ff84983c700b4e9175 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 07:35:08 +0400 Subject: feat(factory): register new designs in a running mission Load control devices and visuals for saved Unit DAT designs, keep the project bytes pinned for later orders, and select mine reserve drafts by identity. The Field Camp runtime test registers a design without a startup reserve, supplies the factory from a new mine, and checks the released mounts and devices. Stock mission window starts with Vulkan validation clean. Connecting the in-game designer order and world rendering is the next step. --- crates/fparkan-runtime/src/production.rs | 100 +++++++++++++++++++------------ 1 file changed, 63 insertions(+), 37 deletions(-) (limited to 'crates/fparkan-runtime/src/production.rs') diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs index 0817cfb8..ff67b95b 100644 --- a/crates/fparkan-runtime/src/production.rs +++ b/crates/fparkan-runtime/src/production.rs @@ -31,14 +31,15 @@ const PAID_MINIMUM_MS: u32 = 5_000; const RESOURCE_EPSILON: f32 = 0.01; pub(super) struct Production { - reserve_start: usize, schemes: Vec, + slot_starts: Vec, player_scheme_count: usize, pub(super) saved_schemes: std::ops::Range, + live_records: std::collections::BTreeMap>, customized: bool, pub(super) scheme_vfs: Option>, ai_schemes: Vec, - controls: Vec, + controls: std::collections::BTreeMap, plants: Vec<(usize, u32)>, pending: VecDeque, clock_ticks: std::collections::BTreeMap<(u16, u32), u64>, @@ -167,13 +168,16 @@ impl Production { } let mut production = Self { scheme_vfs: None, - reserve_start, + slot_starts: (0..schemes.len()) + .map(|scheme| reserve_start + scheme * RESERVE_SLOTS) + .collect(), schemes, player_scheme_count, saved_schemes: 0..0, + live_records: Default::default(), customized, ai_schemes, - controls, + controls: (reserve_start..).zip(controls).collect(), plants: Vec::new(), pending: VecDeque::new(), clock_ticks: Default::default(), @@ -184,6 +188,44 @@ impl Production { Ok(production) } + pub(super) fn add_saved_scheme( + &mut self, + path: NormalizedPath, + first_slot: usize, + controls: Vec, + records: &[UnitComponentRecord], + vfs: Arc, + ) -> usize { + let scheme = self.schemes.len(); + self.schemes.push(path); + self.slot_starts.push(first_slot); + self.controls.extend((first_slot..).zip(controls)); + for slot in first_slot..first_slot + RESERVE_SLOTS { + self.live_records.insert(slot, records.to_vec()); + } + if self.saved_schemes.is_empty() { + self.saved_schemes.start = scheme; + } + self.saved_schemes.end = scheme + 1; + self.scheme_vfs = Some(vfs); + scheme + } + + pub(super) fn next_scheme(&self) -> usize { + self.schemes.len() + } + + fn source_records<'a>( + &'a self, + mission: &'a LoadedMissionState, + slot: usize, + ) -> Option<&'a [UnitComponentRecord]> { + self.live_records + .get(&slot) + .or_else(|| mission.prototype_graph.root_unit_components.get(slot)) + .map(Vec::as_slice) + } + pub(super) fn register_factories(&mut self, first_slot: usize, drafts: &[MissionObjectDraft]) { self.plants .retain(|&(index, _)| !(first_slot..first_slot + drafts.len()).contains(&index)); @@ -301,7 +343,7 @@ impl Production { }; let design = order.design.as_ref(); let controls = design.map_or_else( - || self.controls[order.slot - self.reserve_start].clone(), + || self.controls[&order.slot].clone(), |(_, table)| table.clone(), ); let handle = spawn_prepared_unit( @@ -314,10 +356,8 @@ impl Production { )?; if let Some((records, _)) = order.design.take() { mission.object_drafts[order.slot].unit_components = records; - } else if let Some(records) = mission.prototype_graph.root_unit_components.get(order.slot) { - mission.object_drafts[order.slot] - .unit_components - .clone_from(records); + } else if let Some(records) = self.source_records(mission, order.slot) { + mission.object_drafts[order.slot].unit_components = records.to_vec(); } let draft = &mission.object_drafts[order.slot]; let class = draft @@ -414,13 +454,8 @@ pub fn factory_project_components( engine: &Engine, scheme: usize, ) -> Option<&[UnitComponentRecord]> { - engine - .loaded - .as_ref()? - .prototype_graph - .root_unit_components - .get(factory_project_root(engine, scheme)?) - .map(Vec::as_slice) + let (mission, production) = loaded_production(engine).ok()?; + production.source_records(mission, factory_project_root(engine, scheme)?) } /// Available internal equipment for this exact record, in the clan's TRF order. @@ -446,7 +481,7 @@ pub fn factory_project_root(engine: &Engine, scheme: usize) -> Option { let (_, production) = loaded_production(engine).ok()?; production .player_project(scheme) - .then(|| production.reserve_start + scheme * RESERVE_SLOTS) + .then(|| production.slot_starts[scheme]) } /// Prepared project indices usable by the designer, including saved geometry. @@ -522,7 +557,7 @@ fn schemes_for_plant<'a>( .iter() .enumerate() .filter_map(|(index, path)| { - let draft = &mission.object_drafts[production.reserve_start + index * RESERVE_SLOTS]; + let draft = &mission.object_drafts[production.slot_starts[index]]; free_duration_ms(plant_key, &draft.unit_components[0].resource_raw)?; Some((index, path.as_str())) }) @@ -602,7 +637,7 @@ fn order_owned_factory( if scheme >= production.schemes.len() { return Err(EngineError::Production("unknown factory scheme".into())); } - let slot_start = production.reserve_start + scheme * RESERVE_SLOTS; + let slot_start = production.slot_starts[scheme]; let free_slot = |index: usize| { mission.object_drafts[index] .original_id @@ -624,10 +659,8 @@ fn order_owned_factory( let design = if changes.is_empty() { None } else { - let source = mission - .prototype_graph - .root_unit_components - .get(slot) + let source = production + .source_records(mission, slot) .ok_or_else(|| production_error("factory scheme has no Unit DAT"))?; let records = factory_design_parts(source, changes)?; if is_free { @@ -638,25 +671,19 @@ fn order_owned_factory( .clone() .ok_or(EngineError::MissingVfs)?; let mut base = draft.clone(); - base.unit_components.clone_from(source); + base.unit_components = source.to_vec(); let control = prepare_factory_parts( vfs, &base, records.clone(), - production.controls[slot - production.reserve_start].clone(), + production.controls[&slot].clone(), )?; Some((records, control)) }; let records = design .as_ref() .map(|(records, _)| records.as_slice()) - .or_else(|| { - mission - .prototype_graph - .root_unit_components - .get(slot) - .map(Vec::as_slice) - }) + .or_else(|| production.source_records(mission, slot)) .unwrap_or(&draft.unit_components); let (duration_ms, paid) = if is_free { if changes.is_empty() @@ -731,8 +758,7 @@ pub(super) fn order_script_factory( } // shortcut: retain the compatible reserve for unsupported catalog requests. schemes_for_plant(mission, production, index).find(|&(scheme, path)| { - let draft = - &mission.object_drafts[production.reserve_start + scheme * RESERVE_SLOTS]; + let draft = &mission.object_drafts[production.slot_starts[scheme]]; let fallback = if production.customized { scheme == production.player_scheme_count } else { @@ -1039,7 +1065,7 @@ mod tests { let production = engine.production.as_ref().unwrap(); assert!(production.player_scheme_count < production.schemes.len()); - let enemy_start = production.reserve_start + production.player_scheme_count * RESERVE_SLOTS; + let enemy_start = production.slot_starts[production.player_scheme_count]; for index in enemy_start..enemy_start + RESERVE_SLOTS { let id = engine.loaded.as_ref().unwrap().object_drafts[index].original_id; let handle = fparkan_world::construct_replacement( @@ -1055,7 +1081,7 @@ mod tests { let (factory, scheme, _) = clan_factory_schemes(&engine, cockpit).unwrap()[0]; assert!(order_clan_factory_scheme(&mut engine, cockpit, factory, scheme, 0).unwrap() > 0); let production = engine.production.as_ref().unwrap(); - let player_start = production.reserve_start + scheme * RESERVE_SLOTS; + let player_start = production.slot_starts[scheme]; assert!((player_start..player_start + RESERVE_SLOTS).contains(&production.pending[0].slot)); } @@ -1220,7 +1246,7 @@ mod tests { let production = engine.production.as_ref().unwrap(); assert_eq!(production.pending.len(), 2); assert_ne!(production.pending[0].slot, production.pending[1].slot); - let cargo_slots = production.reserve_start + transport_index * RESERVE_SLOTS; + let cargo_slots = production.slot_starts[transport_index]; assert!((cargo_slots..cargo_slots + RESERVE_SLOTS).contains(&production.pending[0].slot)); let mut completed = Vec::new(); let mut departures = Vec::new(); -- cgit v1.2.3