diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-26 14:01:53 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-26 14:01:53 +0300 |
| commit | 9dc88e53f0f289056d8dc45dda3b24cc89d7ed34 (patch) | |
| tree | 226bc9a24a630ac9fbca625ddb3b136943ef9740 /crates | |
| parent | b64fec455724bf659a3ed9418f144d2c21b8f26a (diff) | |
| download | fparkan-9dc88e53f0f289056d8dc45dda3b24cc89d7ed34.tar.xz fparkan-9dc88e53f0f289056d8dc45dda3b24cc89d7ed34.zip | |
feat(factory): order schemes with selected internal parts
On the stock Constructor map, walk to the factory communicator, claim it, order the revised scheme, and walk back outside. The released custom robot advances the production objective. Check that an unresearched part prevents a new custom order before it uses the free quota.
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 48 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/production.rs | 60 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/robot_designer.rs | 250 |
3 files changed, 301 insertions, 57 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 64c908a3..092472e6 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -115,6 +115,7 @@ pub use projectile_shield::{ mod repair_zone; mod research; mod resource_distribution; +mod robot_designer; mod route_motion; mod route_obstacles; mod route_traffic; @@ -173,6 +174,7 @@ pub use projectile::{ }; pub use projectile_parts::depleted_chassis_nodes; pub use research::order_lab_research; +pub use robot_designer::{load_gameplay_mission, load_gameplay_mission_with_build_parts}; pub use unit_combat::{AutoDemoCombatEvent, AutoDemoProjectileType, FiringEffectInstance}; pub use unit_spawn::SpawnedUnit; @@ -1867,38 +1869,6 @@ pub fn load_mission_with_init_stubs( load_gameplay_mission(engine, request, startup_input, None) } -/// Loads the gameplay world and prepares the player's optional Unit DAT design. -/// -/// # Errors -/// -/// Returns an error if the mission or selected design cannot be loaded. -pub fn load_gameplay_mission( - engine: &mut Engine, - request: MissionRequest, - startup_input: MissionStartupInput, - build_scheme: Option<&str>, -) -> Result<LoadedMission, EngineError> { - if build_scheme.is_some() && request.mode != MissionMode::SingleMission { - return Err(EngineError::Production( - "build schemes require a player mission".into(), - )); - } - let build_scheme = build_scheme - .map(|path| normalize_engine_path("factory Unit DAT", path)) - .transpose()?; - load_mission_with_options( - engine, - request, - MissionLoadOptions { - allow_partial_init: true, - start_autodemo: true, - ..MissionLoadOptions::default() - }, - Some(startup_input), - build_scheme.as_ref(), - ) -} - /// Loads a mission using the bounded static-preview asset scope. /// /// This mode preserves map, TMA, graph, construction and registration work, @@ -1957,9 +1927,9 @@ fn load_mission_with_options( request: MissionRequest, options: MissionLoadOptions, startup_input: Option<MissionStartupInput>, - build_scheme: Option<&NormalizedPath>, + build_scheme: Option<(&NormalizedPath, &[(String, String)])>, ) -> Result<LoadedMission, EngineError> { - let vfs = engine.services.vfs.clone().ok_or(EngineError::MissingVfs)?; + let mut 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 { @@ -1981,7 +1951,12 @@ fn load_mission_with_options( } else { Arc::from(&[][..]) }; - let mut build_schemes = if let Some(scheme) = build_scheme { + let customized = build_scheme.is_some_and(|(_, changes)| !changes.is_empty()); + let mut build_schemes = if let Some((scheme, changes)) = build_scheme.filter(|_| customized) { + let (overlay, custom) = robot_designer::prepare(vfs, scheme, changes)?; + vfs = overlay; + vec![custom, scheme.clone()] + } else if let Some((scheme, _)) = build_scheme { vec![scheme.clone()] } else if options.start_autodemo && request.mode == MissionMode::SingleMission { production::mission_prebuilds(&config)? @@ -2040,7 +2015,7 @@ fn load_mission_with_options( MISSION_DECODED_PAYLOAD_CACHE_ENTRIES, )); let repository = asset_manager.repository(); - let player_scheme_count = build_schemes.len(); + let player_scheme_count = build_schemes.len() - usize::from(customized); let ai_schemes = if options.start_autodemo && request.mode == MissionMode::SingleMission { clan_factory_schemes::available( &asset_manager, @@ -2748,6 +2723,7 @@ node_provider_distribution_suspended: false, reserve_start, build_schemes, player_scheme_count, + customized, ai_schemes, reserve_controls, &object_drafts[..construction_reserve_start], diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs index 1af9e6c7..820bb40f 100644 --- a/crates/fparkan-runtime/src/production.rs +++ b/crates/fparkan-runtime/src/production.rs @@ -14,10 +14,12 @@ use super::{ use fparkan_assets::ResearchTrfDocument; use fparkan_path::NormalizedPath; use fparkan_prototype::{unit_component_tree, UnitComponentRecord}; +use fparkan_vfs::Vfs; use fparkan_world::{ handle_by_original_id, identity_metadata, transform_state, ObjectHandle, OwnerId, }; use std::collections::VecDeque; +use std::sync::Arc; // shortcut: GPU geometry is prepared for a bounded number of live builds. // Lift the cap when the renderer can instantiate new roots during a mission. @@ -69,6 +71,7 @@ pub(super) struct Production { reserve_start: usize, schemes: Vec<NormalizedPath>, player_scheme_count: usize, + customized: bool, ai_schemes: Vec<(OwnerId, u32, NormalizedPath)>, controls: Vec<MissionControlDeviceTableState>, plants: Vec<(usize, u32)>, @@ -166,6 +169,7 @@ impl Production { reserve_start: usize, schemes: Vec<NormalizedPath>, player_scheme_count: usize, + customized: bool, ai_schemes: Vec<(OwnerId, u32, NormalizedPath)>, controls: Vec<MissionControlDeviceTableState>, drafts: &[MissionObjectDraft], @@ -195,6 +199,7 @@ impl Production { reserve_start, schemes, player_scheme_count, + customized, ai_schemes, controls, plants: Vec::new(), @@ -526,27 +531,13 @@ fn order_owned_factory( EngineError::Production("scheme size is unsupported by this factory".into()) })?; let (duration_ms, paid) = if is_free { + if production.customized && scheme == 0 { + clan_scheme_cost(&engine.services.vfs, mission, owner, &draft.unit_components)?; + } (free_ms, None) } else { - let cached = mission - .research_trf_tables - .iter() - .find(|table| table.clan_index == usize::from(owner.0)); - let document; - let research = if let Some(table) = cached { - &table.document - } else { - let vfs = engine - .services - .vfs - .as_ref() - .ok_or(EngineError::MissingVfs)?; - document = load_clan_table(vfs, mission, owner)?; - &document - }; - let mut cost = scheme_cost(&draft.unit_components, research, |row| { - mission.free_research.learned.contains(&(owner, row)) - })?; + let mut cost = + clan_scheme_cost(&engine.services.vfs, mission, owner, &draft.unit_components)?; let (table, ore) = factory_resources(mission, plant)?; let rate = factory_rate(table)?; if rate > RESOURCE_EPSILON { @@ -619,8 +610,12 @@ pub(super) fn order_script_factory( schemes_for_plant(mission, production, index).find(|&(scheme, _)| { let draft = &mission.object_drafts[production.reserve_start + scheme * RESERVE_SLOTS]; - scheme < production.player_scheme_count - && draft.unit_dat_header_flags == Some(class) + let fallback = if production.customized { + scheme == production.player_scheme_count + } else { + scheme < production.player_scheme_count + }; + fallback && draft.unit_dat_header_flags == Some(class) }) }) .map(|(scheme, _)| scheme) @@ -690,6 +685,29 @@ fn production_error(error: impl std::fmt::Display) -> EngineError { EngineError::Production(error.to_string()) } +fn clan_scheme_cost( + vfs: &Option<Arc<dyn Vfs>>, + mission: &LoadedMissionState, + owner: OwnerId, + records: &[UnitComponentRecord], +) -> Result<Cost, EngineError> { + let document; + let research = if let Some(table) = mission + .research_trf_tables + .iter() + .find(|table| table.clan_index == usize::from(owner.0)) + { + &table.document + } else { + let vfs = vfs.as_ref().ok_or(EngineError::MissingVfs)?; + document = load_clan_table(vfs, mission, owner)?; + &document + }; + scheme_cost(records, research, |row| { + mission.free_research.learned.contains(&(owner, row)) + }) +} + pub(super) fn scheme_cost( records: &[UnitComponentRecord], research: &ResearchTrfDocument, diff --git a/crates/fparkan-runtime/src/robot_designer.rs b/crates/fparkan-runtime/src/robot_designer.rs new file mode 100644 index 00000000..78edacb8 --- /dev/null +++ b/crates/fparkan-runtime/src/robot_designer.rs @@ -0,0 +1,250 @@ +//! Applies slot-preserving changes to a prepared player factory scheme. + +use crate::{ + load_mission_with_options, normalize_engine_path, read_vfs, Engine, EngineError, LoadedMission, + MissionLoadOptions, MissionMode, MissionRequest, MissionStartupInput, +}; +use fparkan_path::NormalizedPath; +use fparkan_prototype::{decode_unit_dat, unit_component_tree}; +use fparkan_vfs::{MemoryVfs, OverlayVfs, Vfs}; +use std::sync::Arc; + +/// Loads the gameplay world and prepares the player's optional Unit DAT design. +/// +/// # Errors +/// +/// Returns an error if the mission or selected design cannot be loaded. +pub fn load_gameplay_mission( + engine: &mut Engine, + request: MissionRequest, + startup_input: MissionStartupInput, + build_scheme: Option<&str>, +) -> Result<LoadedMission, EngineError> { + load_gameplay_mission_with_build_parts(engine, request, startup_input, build_scheme, &[]) +} + +/// Loads a mission with replacements for internal parts of the selected player scheme. +/// +/// # Errors +/// +/// Returns an error if the replacements have no compatible slot or the design +/// or mission cannot be loaded. +pub fn load_gameplay_mission_with_build_parts( + engine: &mut Engine, + request: MissionRequest, + startup_input: MissionStartupInput, + build_scheme: Option<&str>, + changes: &[(String, String)], +) -> Result<LoadedMission, EngineError> { + if build_scheme.is_some() && request.mode != MissionMode::SingleMission { + return Err(invalid("build schemes require a player mission")); + } + if build_scheme.is_none() && !changes.is_empty() { + return Err(invalid("component replacements require a base scheme")); + } + let build_scheme = build_scheme + .map(|path| normalize_engine_path("factory Unit DAT", path)) + .transpose()?; + load_mission_with_options( + engine, + request, + MissionLoadOptions { + allow_partial_init: true, + start_autodemo: true, + ..MissionLoadOptions::default() + }, + Some(startup_input), + build_scheme.as_ref().map(|path| (path, changes)), + ) +} + +const HEADER_BYTES: usize = 8; +const RECORD_BYTES: usize = 112; +const RESOURCE_KEY: std::ops::Range<usize> = 32..64; +const DESCRIPTION: std::ops::Range<usize> = 72..104; + +pub(super) fn prepare( + vfs: Arc<dyn Vfs>, + source: &NormalizedPath, + changes: &[(String, String)], +) -> Result<(Arc<dyn Vfs>, NormalizedPath), EngineError> { + let mut bytes = read_vfs(&vfs, source)?.to_vec(); + let scheme = decode_unit_dat(&bytes).map_err(invalid)?; + unit_component_tree(&scheme.records).map_err(invalid)?; + for (index, (from, to)) in changes.iter().enumerate() { + validate_change(from, to)?; + if changes[..index] + .iter() + .any(|(other, _)| other.eq_ignore_ascii_case(from)) + { + return Err(invalid(format!( + "component {from} has already been replaced" + ))); + } + let mut matched = false; + for (index, part) in scheme.records.iter().enumerate().skip(1) { + if !key(&part.resource_raw).eq_ignore_ascii_case(from.as_bytes()) { + continue; + } + if part.kind != 1 + || part.immediate_child_count != 0 + || !key(&part.archive_raw).eq_ignore_ascii_case(b"objects.rlb") + { + return Err(invalid(format!( + "component {from} is not a replaceable leaf" + ))); + } + let offset = HEADER_BYTES + index * RECORD_BYTES; + let record = &mut bytes[offset..offset + RECORD_BYTES]; + let mut replacement = [0; 32]; + replacement[..to.len()].copy_from_slice(to.as_bytes()); + record[RESOURCE_KEY].copy_from_slice(&replacement); + record[DESCRIPTION].copy_from_slice(&replacement); + matched = true; + } + if !matched { + return Err(invalid(format!( + "scheme has no replaceable component {from}" + ))); + } + } + + let path = normalize_engine_path("custom robot Unit DAT", "fparkan/custom.dat")?; + let mut overlay = MemoryVfs::default(); + overlay.insert(path.clone(), bytes.into()); + Ok(( + Arc::new(OverlayVfs::from_layers(vec![Arc::new(overlay), vfs])), + path, + )) +} + +fn key(raw: &[u8; 32]) -> &[u8] { + raw.split(|&byte| byte == 0).next().unwrap_or_default() +} + +fn family(resource: &str) -> Option<&str> { + let (family, variant) = resource.rsplit_once('_')?; + (family.len() > 2 + && resource.len() < 32 + && variant.len() == 2 + && family + .get(..2) + .is_some_and(|prefix| prefix.eq_ignore_ascii_case("i_")) + && resource + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')) + .then_some(family) +} + +fn validate_change(from: &str, to: &str) -> Result<(), EngineError> { + if family(from) + .zip(family(to)) + .is_none_or(|(a, b)| !a.eq_ignore_ascii_case(b)) + { + return Err(invalid(format!( + "components {from} and {to} must belong to the same internal family" + ))); + } + if from.eq_ignore_ascii_case(to) { + return Err(invalid(format!( + "replacement for {from} must select another component" + ))); + } + Ok(()) +} + +fn invalid(error: impl std::fmt::Display) -> EngineError { + EngineError::Production(error.to_string()) +} + +#[cfg(test)] +mod tests { + use super::validate_change; + + #[test] + fn design_changes_keep_the_component_family_and_chassis_size() { + assert!(validate_change("i_pws_b_02", "I_PWS_B_01").is_ok()); + for (from, to) in [ + ("i_pws_b_02", "i_pws_l_02"), + ("i_pws_b_02", "i_eng_b_02"), + ("i_pws_b_02", "i_pws_b_02"), + ("i_pws_b_02", "../i_pws_b_02"), + ("e_tur_bb_01", "e_tur_bb_02"), + ] { + assert!(validate_change(from, to).is_err()); + } + } + + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_constructor_spawns_a_modified_scheme_with_known_parts() { + use crate::{ + capture_neutral_building, create, factory_schemes, frame, loaded_mission_object_drafts, + order_factory_robot, starting_cockpit, EngineServices, + }; + use fparkan_vfs::DirectoryVfs; + use fparkan_world::{set_transform, transform_state, OriginalObjectId}; + use std::sync::Arc; + + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed root"); + let vfs = Arc::new(DirectoryVfs::new(std::path::PathBuf::from(root))); + for unknown_armor in [true, false] { + let mut engine = create(EngineServices::new(vfs.clone())).unwrap(); + let mut parts = vec![ + ("i_pws_b_02".into(), "i_pws_b_01".into()), + ("i_rdr_b_02".into(), "i_rdr_b_01".into()), + ]; + if unknown_armor { + parts.push(("i_arm_b_df".into(), "i_arm_b_03".into())); + } + super::load_gameplay_mission_with_build_parts( + &mut engine, + crate::MissionRequest { + key: "MISSIONS/CAMPAIGN/CAMPAIGN.00/Mission.02/data.tma".into(), + mode: crate::MissionMode::SingleMission, + }, + crate::MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + Some("UNITS/UNITS/PREBLD/tut2_f1.dat"), + &parts, + ) + .unwrap(); + frame(&mut engine, 0).unwrap(); + let hero = starting_cockpit(&engine).unwrap(); + let mut pose = transform_state(&engine.world, hero.handle).unwrap(); + pose.position = [391.3_f32, 740.1, 140.74].map(f32::to_bits); + set_transform(&mut engine.world, hero.handle, pose).unwrap(); + assert_eq!( + capture_neutral_building(&mut engine, hero).unwrap(), + Some(OriginalObjectId(0)) + ); + assert_eq!( + factory_schemes(&engine, hero).unwrap(), + [(0, "fparkan/custom.dat")] + ); + let order = order_factory_robot(&mut engine, hero, 1); + if unknown_armor { + assert!(order.unwrap_err().to_string().contains("i_arm_b_03")); + continue; + } + assert_eq!(order.unwrap(), 60_000); + let robot = (60_001..80_000) + .step_by(100) + .find_map(|now| frame(&mut engine, now).unwrap().produced_unit) + .expect("factory released the custom scheme"); + let draft = &loaded_mission_object_drafts(&engine).unwrap()[robot.object_index]; + for (previous, new) in &parts { + assert!(draft + .unit_components + .iter() + .any(|part| super::key(&part.resource_raw) == new.as_bytes())); + assert!(!draft + .unit_components + .iter() + .any(|part| super::key(&part.resource_raw) == previous.as_bytes())); + } + } + } +} |
