diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-14 12:37:08 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-14 12:37:08 +0300 |
| commit | 0a70413dffce6ff2f2b91488ec62fc88acce8623 (patch) | |
| tree | 66536e9876c9d44b8f9c37fa67ddae83a6d67474 /crates | |
| parent | 94b7365f2b9ef060d00b4b510f143c5f9e59c131 (diff) | |
| download | fparkan-0a70413dffce6ff2f2b91488ec62fc88acce8623.tar.xz fparkan-0a70413dffce6ff2f2b91488ec62fc88acce8623.zip | |
feat(runtime): use the mission prebuild for factory orders
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 43 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/production.rs | 57 |
2 files changed, 83 insertions, 17 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 6780cf0a..e1236d5d 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -1834,6 +1834,34 @@ fn load_mission_with_options( let vfs = engine.services.vfs.clone().ok_or(EngineError::MissingVfs)?; let mission_path = normalize_engine_path("mission", &request.key)?; let mission_bytes = read_vfs(&vfs, &mission_path)?; + let config: Arc<[u8]> = if options.asset_scope == MissionAssetScope::Full { + let config_path = mission_path.as_str().rsplit_once('/').map_or_else( + || "mission.cfg".to_owned(), + |(directory, _)| format!("{directory}/mission.cfg"), + ); + let config_path = normalize_engine_path("mission config", &config_path)?; + match vfs.read(&config_path) { + Ok(bytes) => bytes, + Err(VfsError::NotFound(_)) => Arc::from(&[][..]), + Err(source) => { + return Err(EngineError::Vfs { + path: config_path.as_str().into(), + source, + }) + } + } + } else { + Arc::from(&[][..]) + }; + let default_scheme = if build_scheme.is_none() + && options.start_autodemo + && request.mode == MissionMode::SingleMission + { + production::default_prebuild(&config)? + } else { + None + }; + let build_scheme = build_scheme.or(default_scheme.as_ref()); let land_path = decode_mission_land_path(&mission_bytes, TmaProfile::Strict).map_err(|source| { @@ -2299,21 +2327,6 @@ fn load_mission_with_options( }; let mission_events = if options.asset_scope == MissionAssetScope::Full { - let config_path = mission_path.as_str().rsplit_once('/').map_or_else( - || "mission.cfg".to_owned(), - |(directory, _)| format!("{directory}/mission.cfg"), - ); - let config_path = normalize_engine_path("mission config", &config_path)?; - let config = match vfs.read(&config_path) { - Ok(bytes) => bytes, - Err(VfsError::NotFound(_)) => Arc::from(&[][..]), - Err(source) => { - return Err(EngineError::Vfs { - path: config_path.as_str().into(), - source, - }) - } - }; MissionEvents::with_placed_objects( &mut new_runtime_world, &object_drafts, diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs index 54ccef42..e8232581 100644 --- a/crates/fparkan-runtime/src/production.rs +++ b/crates/fparkan-runtime/src/production.rs @@ -10,6 +10,7 @@ use super::{ MissionObjectDraft, StartingCockpit, World, }; use fparkan_assets::ResearchTrfDocument; +use fparkan_path::NormalizedPath; use fparkan_prototype::{unit_component_tree, UnitComponentRecord}; use fparkan_world::{ handle_by_original_id, identity_metadata, transform_state, ObjectHandle, OwnerId, @@ -21,6 +22,42 @@ pub(super) const RESERVE_SLOTS: usize = 8; const PAID_MINIMUM_MS: u32 = 5_000; const RESOURCE_EPSILON: f32 = 0.01; +pub(super) fn default_prebuild(config: &[u8]) -> Result<Option<NormalizedPath>, EngineError> { + let mut in_prebuild = false; + for line in config.split(|&byte| byte == b'\n') { + let line = line.trim_ascii(); + let mut words = line + .split(u8::is_ascii_whitespace) + .filter(|word| !word.is_empty()); + match words.next() { + Some(word) if word.eq_ignore_ascii_case(b"object") => { + in_prebuild = words + .next() + .is_some_and(|name| name.eq_ignore_ascii_case(b"prebuild")); + } + Some(word) if word.eq_ignore_ascii_case(b"end") => in_prebuild = false, + Some(word) if in_prebuild && !word.starts_with(b"#") && !word.starts_with(b"//") => { + let mut fields = line.splitn(3, |&byte| byte == b'"'); + let key = fields.next().unwrap_or_default(); + let name = fields.next().filter(|name| !name.is_empty()); + if key.trim_ascii_end().ends_with(b"=") && fields.next().is_some() { + if let Some(name) = name { + let name = std::str::from_utf8(name).map_err(production_error)?; + // shortcut: choose the first configured design until the factory menu can select one. + let path = normalize_engine_path( + "mission prebuild", + &format!("UNITS/UNITS/PREBLD/{name}"), + )?; + return Ok(Some(path)); + } + } + } + _ => {} + } + } + Ok(None) +} + pub(super) struct Production { reserve_start: usize, controls: Vec<MissionControlDeviceTableState>, @@ -224,7 +261,7 @@ pub fn order_factory_robot( ) -> Result<u32, EngineError> { let (Some(mission), Some(production)) = (&mut engine.loaded, &mut engine.production) else { return Err(EngineError::Production( - "launch with --build-scheme to choose a robot".into(), + "mission has no prepared robot design; use --build-scheme to choose one".into(), )); }; if production.pending.is_some() { @@ -473,6 +510,22 @@ mod tests { use fparkan_world::{set_transform, InputSnapshot, OriginalObjectId}; #[test] + fn first_mission_prebuild_supplies_the_default_design() { + let config = b"object other\n model1 = \"ignored.dat\"\nend\r\n\ + OBJECT PreBuild\r\n # model1 = \"comment.dat\"\r\n\ + model2=\"first.dat\"\r\n model1 = \"second.dat\"\r\nend\n"; + assert_eq!( + default_prebuild(config).unwrap().unwrap().as_str(), + "UNITS/UNITS/PREBLD/first.dat" + ); + assert!(default_prebuild( + b"object prebuild\nend\nobject other\nmodel1 = \"ignored.dat\"\nend" + ) + .unwrap() + .is_none()); + } + + #[test] #[ignore = "requires licensed corpus"] fn licensed_field_camp_runs_paid_factory_order() { let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); @@ -488,7 +541,7 @@ mod tests { queue_time_ms: 0, research_creation_gate: false, }, - Some("UNITS/UNITS/PREBLD/tut3_p1.dat"), + None, ) .unwrap(); let cockpit = starting_cockpit(&engine).unwrap(); |
