aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/camouflage.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-03 17:39:24 +0300
committerValentin Popov <valentin@popov.link>2026-12-03 17:39:24 +0300
commit4b9277258224ab846b84cafe1d6a3b91e29a05c5 (patch)
tree4f830c515d6a202d87122f95741235be4e30e71d /crates/fparkan-runtime/src/camouflage.rs
parentc85b69296f5349a728e28e308c7d71b367dc6ac1 (diff)
downloadfparkan-4b9277258224ab846b84cafe1d6a3b91e29a05c5.tar.xz
fparkan-4b9277258224ab846b84cafe1d6a3b91e29a05c5.zip
feat(game): enable powered camouflage in the cockpit
Diffstat (limited to 'crates/fparkan-runtime/src/camouflage.rs')
-rw-r--r--crates/fparkan-runtime/src/camouflage.rs81
1 files changed, 81 insertions, 0 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(())
+}