aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/camouflage.rs
blob: 464853e60d62ee6978d078e6c8de7c196a9db11c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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(())
}