diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-runtime/src/camouflage.rs | 81 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/cockpit_indicators.rs | 6 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 43 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/mobile_budget.rs | 18 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/radar.rs | 64 |
5 files changed, 187 insertions, 25 deletions
diff --git a/crates/fparkan-runtime/src/camouflage.rs b/crates/fparkan-runtime/src/camouflage.rs new file mode 100644 index 00000000..464853e6 --- /dev/null +++ b/crates/fparkan-runtime/src/camouflage.rs @@ -0,0 +1,81 @@ +//! State and resource callbacks for the Control detection shield. + +use crate::{ + checked_control_float, checked_control_product, checked_control_sum, control_budget_device, + control_device_budget_coefficient, control_device_budget_descriptor_float, + ControlResourceAccessError, EffectiveControlDevice, Engine, MissionControlDeviceTableState, + MissionControlDeviceTableStatus, StartingCockpit, +}; + +pub(super) const OFF: u32 = 0x2000; +const ON: u32 = 0x1000; + +pub(super) fn is_controlled_device(device: &EffectiveControlDevice) -> bool { + device.key == 0 && device.descriptor.tag == 10 && device.factory_tag == 10 +} + +pub(super) fn is_enabled(device: &EffectiveControlDevice) -> bool { + device.native_word_50_bits == Some(ON) +} + +/// Switches the detection shield on the body controlled by the player. +pub fn toggle_camouflage(engine: &mut Engine, cockpit: StartingCockpit) -> Option<bool> { + let table = engine + .loaded + .as_mut()? + .control_device_tables + .iter_mut() + .find(|table| table.handle == cockpit.handle)?; + let MissionControlDeviceTableStatus::Available(devices) = &mut table.status else { + return None; + }; + let device = devices + .iter_mut() + .find(|device| is_controlled_device(device))?; + // Control+0x26570 handles inverse command 0x4000. + let enabled = device.native_word_50_bits? == OFF; + device.native_word_50_bits = Some(if enabled { ON } else { OFF }); + Some(enabled) +} + +pub(super) fn resource_rate( + device: &EffectiveControlDevice, + seconds: f32, + upkeep: f32, +) -> Result<f32, ControlResourceAccessError> { + let passive = -checked_control_product(10, "detection shield upkeep", seconds, upkeep)?; + if !is_enabled(device) { + return Ok(passive); + } + let surcharge = control_device_budget_coefficient(device, 4)?; + let extra = checked_control_product(10, "camouflage upkeep", seconds, surcharge)?; + checked_control_sum(10, "active detection shield demand", passive, -extra) +} + +pub(super) fn update_resource( + table: &mut MissionControlDeviceTableState, + index: usize, + seconds: f32, + allocation: f32, +) -> Result<(), ControlResourceAccessError> { + let device = control_budget_device(table, index)?; + let allocation = checked_control_float(10, "detection shield allocation", allocation)?; + let switch_off = if is_enabled(device) { + let upkeep = control_device_budget_descriptor_float(device, 8, "detection shield rate")?; + let rate = resource_rate(device, seconds, upkeep)?; + let received = -checked_control_product(10, "detection shield receipt", rate, allocation)?; + let passive = checked_control_product(10, "detection shield upkeep", seconds, upkeep)?; + received < passive + } else { + false + }; + let MissionControlDeviceTableStatus::Available(devices) = &mut table.status else { + unreachable!("checked device table") + }; + let device = &mut devices[index]; + device.multiplier_bits = Some(allocation.to_bits()); + if switch_off { + device.native_word_50_bits = Some(OFF); + } + Ok(()) +} diff --git a/crates/fparkan-runtime/src/cockpit_indicators.rs b/crates/fparkan-runtime/src/cockpit_indicators.rs index e51e00bb..651949dd 100644 --- a/crates/fparkan-runtime/src/cockpit_indicators.rs +++ b/crates/fparkan-runtime/src/cockpit_indicators.rs @@ -1,7 +1,7 @@ //! Control device flags and control mode shown at the base of the cockpit dial. use crate::{ - repair_system::is_self_repair_device, Engine, MissionControlDeviceTableStatus, + camouflage, repair_system::is_self_repair_device, Engine, MissionControlDeviceTableStatus, RemoteWarbotControl, StartingCockpit, }; @@ -40,8 +40,8 @@ pub fn cockpit_indicators(engine: &Engine, cockpit: StartingCockpit) -> Option<C .is_some_and(|device| device.native_word_50_bits == Some(0x20)); let camouflage = devices .iter() - .find(|device| device.key == 0 && device.descriptor.tag == 10 && device.factory_tag == 10) - .is_some_and(|device| device.native_word_50_bits == Some(0x1000)); + .find(|device| camouflage::is_controlled_device(device)) + .is_some_and(camouflage::is_enabled); Some(CockpitIndicators { self_repair, camouflage, diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 7170b78f..2ac3f856 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -50,6 +50,7 @@ mod basement_geometry; mod boarding; mod builder_supply; mod building_door; +mod camouflage; pub use basement_geometry::{ mission_basement_footprint, mission_basement_footprints, place_constructed_basement, MissionBasementFootprint, @@ -196,6 +197,7 @@ pub use boarding::{ select_boarding_target, RemoteWarbotControl, VehicleEntry, }; pub use building_door::{has_proximity_doors, sample_capture_doors, sample_proximity_doors}; +pub use camouflage::toggle_camouflage; pub use capture_barrier::sample_capture_barrier; pub use designer_attachments::{edit_factory_socket, factory_socket_choices}; pub use designer_chassis::{factory_chassis_choices, prepare_factory_chassis}; @@ -4933,7 +4935,7 @@ fn control_device_resource_rate( match device.factory_tag { 5 => Ok(mobile_budget::unmodeled_motion_rate()), - 10 => mobile_budget::passive_device_rate(device, delta_time_seconds), + 10 => camouflage::resource_rate(device, delta_time_seconds, descriptor_rate), 9 => { let coefficient_0 = control_device_budget_coefficient(device, 0)?; let coefficient_1 = control_device_budget_coefficient(device, 1)?; @@ -5413,7 +5415,8 @@ fn control_device_resource_update( )?; } } - 5 | 10 | 26 => set_control_device_multiplier(table, device_index, allocation)?, + 10 => camouflage::update_resource(table, device_index, delta_time_seconds, allocation)?, + 5 | 26 => set_control_device_multiplier(table, device_index, allocation)?, factory_tag if control_factory_uses_generic_budget_callbacks(factory_tag) => { set_control_device_multiplier(table, device_index, allocation)?; } @@ -6721,7 +6724,7 @@ fn native_control_factory_defaults(factory_tag: u32) -> (Option<u32>, Option<u32 match factory_tag { 1 | 4 => (Some(0x3f80_0000), Some(0x200)), 3 | 5 | 8 | 21 | 25 | 26 => (Some(0x3f80_0000), Some(0x5)), - 10 => (Some(0x3f80_0000), Some(0x2000)), + 10 => (Some(0x3f80_0000), Some(camouflage::OFF)), 17 | 19 => (Some(0x3f80_0000), Some(0x20)), 2 | 27 | 30 => (Some(0x3f80_0000), Some(0)), 9 => (Some(0x3f80_0000), Some(0x20)), @@ -9878,6 +9881,40 @@ mod tests { } #[test] + fn control_budget_camouflage_matches_native_demand_and_shutdown_threshold() { + let mut device = synthetic_effective_control_device( + 10, + 10, + 1.0_f32.to_bits(), + Some(1.0_f32.to_bits()), + None, + ); + device.key = 0; + device.native_word_50_bits = Some(0x1000); + let provider = synthetic_node_provider_row(0, 0, 1.0_f32.to_bits()); + for (seconds, base, extra, allocation_bits, demand, expected_enabled) in [ + (1.0_f32, 3.0_f32, 9.0_f32, 0x3e80_0000, 0xc140_0000, true), + (1.0, 3.0, 9.0, 0x3e7f_ffff, 0xc140_0000, false), + (0.375491, 17.258, 19.81, 0x3e6f_e3b0, 0xc15e_b2fe, false), + ] { + device.coefficient_bits.as_mut().unwrap()[4] = extra.to_bits(); + device.descriptor.fixed_words[8] = base.to_bits(); + let mut table = synthetic_control_device_table_with_providers( + MissionControlDeviceTableStatus::Available(vec![device.clone()]), + MissionControlNodeProviderTableStatus::Available(vec![provider.clone()]), + ); + let active = control_budget_device(&table, 0).unwrap(); + let rate = control_device_resource_rate(&table, active, seconds).unwrap(); + assert_eq!(rate.to_bits(), demand); + control_device_resource_update(&mut table, 0, seconds, f32::from_bits(allocation_bits)) + .unwrap(); + let updated = control_budget_device(&table, 0).unwrap(); + assert_eq!(updated.multiplier_bits, Some(allocation_bits)); + assert_eq!(camouflage::is_enabled(updated), expected_enabled); + } + } + + #[test] fn control_budget_tag26_rate_matches_native_additive_cost() { let mut device = synthetic_effective_control_device( 26, diff --git a/crates/fparkan-runtime/src/mobile_budget.rs b/crates/fparkan-runtime/src/mobile_budget.rs index fb8dad48..6f778725 100644 --- a/crates/fparkan-runtime/src/mobile_budget.rs +++ b/crates/fparkan-runtime/src/mobile_budget.rs @@ -150,24 +150,6 @@ pub(super) fn weapon_update( Ok(()) } -pub(super) fn passive_device_rate( - device: &EffectiveControlDevice, - dt: f32, -) -> Result<f32, ControlResourceAccessError> { - if device.native_word_50_bits != Some(0x2000) { - return Err(ControlResourceAccessError::UnsupportedField { - factory_tag: 10, - field: "active mode +0x50", - }); - } - Ok(-checked_control_product( - 10, - "passive device upkeep", - dt, - control_device_budget_descriptor_float(device, 8, "passive device rate")?, - )?) -} - pub(super) fn unmodeled_motion_rate() -> f32 { // shortcut: omit demand where the live CTL state has not been recovered. 0.0 diff --git a/crates/fparkan-runtime/src/radar.rs b/crates/fparkan-runtime/src/radar.rs index 9bb0dfde..e114790d 100644 --- a/crates/fparkan-runtime/src/radar.rs +++ b/crates/fparkan-runtime/src/radar.rs @@ -493,7 +493,7 @@ fn signature(table: &MissionControlDeviceTableState) -> Option<[f32; 3]> { } // Control+0x2bb40 attenuates each channel with the tag-10 getter. let mut factor = (1.0 - f64::from(gain) * state * multiplier).max(0.0) as f32; - if device.native_word_50_bits == Some(0x1000) { + if crate::camouflage::is_enabled(device) { factor *= (1.0 - coefficients[3]).max(0.0); } *value *= factor; @@ -545,6 +545,68 @@ mod tests { #[test] #[ignore = "requires licensed corpus"] + fn stock_camouflage_attenuates_radar_and_shuts_off_without_power() { + use super::signature; + use crate::{ + camouflage, cockpit_indicators::cockpit_indicators, create, load_gameplay_mission, + loaded_mission_control_device_tables, starting_cockpit, toggle_camouflage, + EngineServices, MissionControlDeviceTableStatus, MissionMode, MissionRequest, + MissionStartupInput, + }; + use fparkan_vfs::DirectoryVfs; + use std::sync::Arc; + + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); + let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap(); + load_gameplay_mission( + &mut engine, + MissionRequest { + key: "MISSIONS/CAMPAIGN/CAMPAIGN.00/Mission.02/data.tma".into(), + mode: MissionMode::SingleMission, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + let hero = starting_cockpit(&engine).unwrap(); + let table_index = loaded_mission_control_device_tables(&engine) + .unwrap() + .iter() + .position(|table| table.handle == hero.handle) + .unwrap(); + let table = &loaded_mission_control_device_tables(&engine).unwrap()[table_index]; + let MissionControlDeviceTableStatus::Available(devices) = &table.status else { + panic!("the hero requires a prepared Control device table"); + }; + let index = devices + .iter() + .position(camouflage::is_controlled_device) + .unwrap(); + let before = signature(table).unwrap(); + let attenuation = f32::from_bits(devices[index].coefficient_bits.unwrap()[3]); + assert!(attenuation > 0.0); + assert!(!cockpit_indicators(&engine, hero).unwrap().camouflage); + assert_eq!(toggle_camouflage(&mut engine, hero), Some(true)); + assert!(cockpit_indicators(&engine, hero).unwrap().camouflage); + let table = &mut engine.loaded.as_mut().unwrap().control_device_tables[table_index]; + let after = signature(table).unwrap(); + assert!(before.iter().zip(after).any(|(&off, on)| off > on)); + assert_eq!( + after, + before.map(|value| value * (1.0 - attenuation).max(0.0)) + ); + crate::control_device_resource_update(table, index, 1.0, 1.0).unwrap(); + assert!(cockpit_indicators(&engine, hero).unwrap().camouflage); + let table = &mut engine.loaded.as_mut().unwrap().control_device_tables[table_index]; + crate::control_device_resource_update(table, index, 1.0, 0.0).unwrap(); + assert!(!cockpit_indicators(&engine, hero).unwrap().camouflage); + } + + #[test] + #[ignore = "requires licensed corpus"] fn stock_neutral_warbot_appears_and_expires_on_the_hero_radar_clock() { use crate::{ create, frame, load_gameplay_mission, loaded_mission_control_device_tables, |
