diff options
Diffstat (limited to 'crates/fparkan-runtime/src')
| -rw-r--r-- | crates/fparkan-runtime/src/control_resources.rs | 27 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/laboratory.rs | 126 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 26 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/production.rs | 4 |
4 files changed, 158 insertions, 25 deletions
diff --git a/crates/fparkan-runtime/src/control_resources.rs b/crates/fparkan-runtime/src/control_resources.rs index e58c5a7b..18af1975 100644 --- a/crates/fparkan-runtime/src/control_resources.rs +++ b/crates/fparkan-runtime/src/control_resources.rs @@ -2,15 +2,15 @@ use crate::{control_resource_budget_pass, LoadedMissionState}; use fparkan_world::{transform_state, ObjectHandle, World}; use std::collections::BTreeMap; -/// Factory Control updates; other owners and global Control ordering remain open. -pub(super) struct FactoryResources { +/// Factory and laboratory Control budgets; other owners and global RNG ordering remain open. +pub(super) struct Resources { clocks: Vec<(ObjectHandle, ResourceClock)>, rng: u32, ticks: BTreeMap<u32, u64>, stops: BTreeMap<u32, (u64, String)>, } -impl Default for FactoryResources { +impl Default for Resources { fn default() -> Self { Self { clocks: Vec::new(), @@ -22,7 +22,7 @@ impl Default for FactoryResources { } } -impl FactoryResources { +impl Resources { pub(super) fn ticks(&self) -> impl Iterator<Item = (u32, u64)> + '_ { self.ticks.iter().map(|(&unit, &count)| (unit, count)) } @@ -35,7 +35,7 @@ impl FactoryResources { } pub(super) fn tick(mission: &mut LoadedMissionState, world: &World, now_ms: u32) { - let state = &mut mission.factory_resources; + let state = &mut mission.control_resources; state .clocks .retain(|(handle, _)| transform_state(world, *handle).is_ok()); @@ -43,7 +43,12 @@ pub(super) fn tick(mission: &mut LoadedMissionState, world: &World, now_ms: u32) if !table .original_id .and_then(|id| mission.object_drafts.get(id.0 as usize)) - .is_some_and(|draft| draft.unit_dat_header_flags == Some(crate::autodemo::PLANT_CLASS)) + .is_some_and(|draft| { + matches!( + draft.unit_dat_header_flags, + Some(crate::autodemo::PLANT_CLASS | crate::laboratory::LAB_CLASS) + ) + }) || transform_state(world, table.handle).is_err() { continue; @@ -63,7 +68,11 @@ pub(super) fn tick(mission: &mut LoadedMissionState, world: &World, now_ms: u32) forced: 0, }, )); - &mut state.clocks.last_mut().expect("new factory clock").1 + &mut state + .clocks + .last_mut() + .expect("new Control resource clock") + .1 }; // Control.dll+0x42480: default resource period; this is a separate RNG from Behavior. let Some(seconds) = clock.step(now_ms as f32, true, 250.0, &mut state.rng) else { @@ -74,7 +83,7 @@ pub(super) fn tick(mission: &mut LoadedMissionState, world: &World, now_ms: u32) let count = state.ticks.entry(table.handle.slot).or_default(); *count += 1; if *count == 1 { - eprintln!("sim_coverage factory_control_clock_approx=1 unit={} (factory owners; Control RNG seed and global order remain open)", table.handle.slot); + eprintln!("sim_coverage control_resource_clock_approx=1 unit={} (factory and laboratory owners; Control RNG seed and global order remain open)", table.handle.slot); } } Err(reason) => { @@ -85,7 +94,7 @@ pub(super) fn tick(mission: &mut LoadedMissionState, world: &World, now_ms: u32) stop.0 += 1; if stop.0 == 1 { eprintln!( - "sim_coverage factory_control_budget_stub=1 unit={} reason={}", + "sim_coverage control_resource_budget_stub=1 unit={} reason={}", table.handle.slot, stop.1 ); } diff --git a/crates/fparkan-runtime/src/laboratory.rs b/crates/fparkan-runtime/src/laboratory.rs index 08953eb9..610a7c0c 100644 --- a/crates/fparkan-runtime/src/laboratory.rs +++ b/crates/fparkan-runtime/src/laboratory.rs @@ -4,7 +4,7 @@ use fparkan_vfs::Vfs; use fparkan_world::{identity_metadata, ObjectHandle, OwnerId, World}; use std::collections::BTreeMap; -const LAB_CLASS: u32 = 0x8000_0400; +pub(super) const LAB_CLASS: u32 = 0x8000_0400; /// Executed laboratory orders and technology state changes. #[derive(Default, Debug, Clone, PartialEq, Eq)] @@ -443,6 +443,130 @@ mod tests { #[test] #[ignore = "requires licensed corpus"] + fn licensed_paid_research_waits_for_control_power_and_resumes_after_supply() { + use crate::{ + create, frame, load_gameplay_mission, EngineServices, MissionMode, MissionRequest, + MissionStartupInput, + }; + use fparkan_vfs::DirectoryVfs; + use std::sync::Arc; + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap(); + let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap(); + load_gameplay_mission( + &mut engine, + MissionRequest { + key: "MISSIONS/Single.01/data.tma".into(), + mode: MissionMode::SingleMission, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + let loaded = engine.loaded.as_mut().unwrap(); + let lab = loaded + .control_device_tables + .iter() + .find(|table| { + table.original_id.is_some_and(|id| { + loaded.object_drafts[id.0 as usize].unit_dat_header_flags == Some(LAB_CLASS) + }) && identity_metadata(&engine.world, table.handle) + .unwrap() + .owner_id + == Some(OwnerId(1)) + }) + .unwrap() + .handle; + let slot = identity_metadata(&engine.world, lab) + .unwrap() + .original_id + .unwrap() + .0 as usize; + // Debug outage: consume free jobs, empty the local reserve and stop external generation. + while loaded.free_research.take_free(slot) {} + let supply = loaded + .behavior_profiles + .iter() + .map(|profile| profile.transfer_power_out_bits) + .collect::<Vec<_>>(); + for profile in &mut loaded.behavior_profiles { + profile.transfer_power_out_bits = 0; + } + crate::set_control_device_table_multiplier( + loaded + .control_device_tables + .iter_mut() + .find(|table| table.handle == lab) + .unwrap(), + 0.0, + ) + .unwrap(); + for now in (0..=1_000).step_by(50) { + frame(&mut engine, now).unwrap(); + } + let loaded = engine.loaded.as_ref().unwrap(); + assert!( + loaded + .control_resources + .ticks() + .any(|(unit, count)| unit == lab.slot && count > 0), + "laboratory Control clock must run" + ); + assert!(!loaded + .control_resources + .stops() + .any(|(unit, _, _)| unit == lab.slot)); + let task = loaded + .laboratories + .tasks + .iter() + .find(|task| task.lab == lab) + .unwrap(); + let (row, work) = task.current.as_ref().expect("active paid research"); + assert!(work.power_cost > 0.0); + let (row, before, elapsed) = (*row, work.power, work.time); + for now in (1_050..=2_000).step_by(50) { + frame(&mut engine, now).unwrap(); + } + let loaded = engine.loaded.as_ref().unwrap(); + let task = loaded + .laboratories + .tasks + .iter() + .find(|task| task.lab == lab) + .unwrap(); + let (current, work) = task.current.as_ref().unwrap(); + assert_eq!(*current, row); + assert_eq!( + work.power, before, + "no power work while the reserve and supply are empty" + ); + assert!(work.time > elapsed); + let loaded = engine.loaded.as_mut().unwrap(); + for (profile, bits) in loaded.behavior_profiles.iter_mut().zip(supply) { + profile.transfer_power_out_bits = bits; + } + for now in (2_050..=10_000).step_by(50) { + frame(&mut engine, now).unwrap(); + let state = &engine.loaded.as_ref().unwrap().laboratories; + if state.completed.contains_key(&(1, lab.slot, row)) + || state + .tasks + .iter() + .find(|task| task.lab == lab) + .and_then(|task| task.current.as_ref()) + .is_some_and(|(current, work)| *current == row && work.power > before) + { + return; + } + } + panic!("research power work must resume after restoring the original generation"); + } + + #[test] + #[ignore = "requires licensed corpus"] fn licensed_stock_ai_laboratories_continue_research_and_keep_the_request_pending() { use crate::{ create, frame, load_gameplay_mission, EngineServices, MissionMode, MissionRequest, diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 762c18f3..bb5d496f 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -1369,7 +1369,7 @@ struct LoadedMissionState { research_trf_tables: Vec<MissionResearchTrfTable>, free_research: research::FreeResearch, laboratories: laboratory::Laboratories, - factory_resources: control_resources::FactoryResources, + control_resources: control_resources::Resources, ai_startup_states: Vec<MissionAiStartupState>, script_bundles: Vec<MissionScriptBundle>, script_varset: Option<MissionScriptVarSet>, @@ -2699,7 +2699,7 @@ fn load_mission_with_options( research_trf_tables, free_research, laboratories: Default::default(), - factory_resources: Default::default(), + control_resources: Default::default(), ai_startup_states: loaded_scripts.ai_startup_states, script_bundles: loaded_scripts.bundles, script_varset: loaded_scripts.varset, @@ -2947,8 +2947,8 @@ pub struct SimulationReport { pub script_factory_orders: Vec<(u16, u32, u64)>, /// Active factory task updates; idle owner phases and global RNG order remain approximate. pub factory_clock_ticks: Vec<(u16, u32, u64)>, - pub factory_control_ticks: Vec<(u32, u64)>, - pub factory_control_stops: Vec<(u32, u64, String)>, + pub control_resource_ticks: Vec<(u32, u64)>, + pub control_resource_stops: Vec<(u32, u64, String)>, /// Approximate clan strength queries, grouped by caller and target clan. pub script_strength_queries: Vec<(usize, u16, u64)>, pub script_capture_queries: Vec<(usize, u64)>, @@ -3023,8 +3023,8 @@ pub fn simulate_mission_report( script_factory_rejections: Vec::new(), script_factory_orders: Vec::new(), factory_clock_ticks: Vec::new(), - factory_control_ticks: Vec::new(), - factory_control_stops: Vec::new(), + control_resource_ticks: Vec::new(), + control_resource_stops: Vec::new(), script_strength_queries: Vec::new(), script_capture_queries: Vec::new(), script_attack_queries: Vec::new(), @@ -3133,11 +3133,11 @@ pub fn simulate_mission_report( .extend(loaded.clan_ai_events.research_catalogs()); report.laboratory = loaded.laboratories.report(); report - .factory_control_ticks - .extend(loaded.factory_resources.ticks()); - report.factory_control_stops.extend( + .control_resource_ticks + .extend(loaded.control_resources.ticks()); + report.control_resource_stops.extend( loaded - .factory_resources + .control_resources .stops() .map(|(unit, count, reason)| (unit, count, reason.to_owned())), ); @@ -3743,7 +3743,7 @@ pub fn query_loaded_mission_control_resource( /// readers use the device key to read the current provider value and multiply /// it by coefficient 0. Only current tag 26 also multiplies by `M`; current /// tag 5 ignores `M` and descriptor word 8 for this selector. Explicit Control -/// update calls can change current provider or multiplier values; factory +/// update calls can change current provider or multiplier values; factory and laboratory /// resource budgets also advance during mission steps. Selector `6` first checks the /// current descriptor tag: a non-2 tag returns `Ok(None)`. For the represented /// factory-2 layout it reads `+0x174`, subject to the normal-or-zero float @@ -3812,7 +3812,7 @@ pub fn set_loaded_mission_control_device_selector( /// `Transfer_Ore_OffBoard` value. Unsupported table/query paths return an error. /// Profile values come from full mission loading, while `0xe00` reads the /// current CPU-projected provider and device state. Explicit Control updates -/// can therefore change `0x1002`; factory Control resource budgets also run +/// can therefore change `0x1002`; factory and laboratory Control resource budgets also run /// during mission steps. /// /// # Errors @@ -4114,7 +4114,7 @@ pub fn distribute_loaded_mission_control_provider_amount( /// The pass supports the original factory callbacks for tags 9, 15, 19, and 26, /// plus the generic default callback pair. Current descriptor category tags are /// supported for the native map prefix `0..=34`. This is an explicit CPU update; -/// factories also run this pass on their resource clock. Rate outputs at `+0x5dc/+0x5e0` are +/// factories and laboratories also run it on their resource clock. Rate outputs at `+0x5dc/+0x5e0` are /// stored on the table after a successful pass. /// /// # Errors diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs index 078fef31..90256903 100644 --- a/crates/fparkan-runtime/src/production.rs +++ b/crates/fparkan-runtime/src/production.rs @@ -1113,11 +1113,11 @@ mod tests { } let loaded = engine.loaded.as_ref().unwrap(); assert!(loaded - .factory_resources + .control_resources .ticks() .any(|(unit, count)| unit == plant.slot && count > 0)); assert!(!loaded - .factory_resources + .control_resources .stops() .any(|(unit, _, _)| unit == plant.slot)); let table = loaded |
