aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/clan_combat_strength.rs
blob: 0dcbd7e2cc72209daf175a864c1482163c6e56e9 (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
use crate::{
    control_device_provider_value_from_status, EffectiveControlDevice,
    MissionControlDeviceTableState, MissionControlDeviceTableStatus,
    MissionControlNodeProviderTableStatus, ObjectHandle,
};

pub(super) fn unit_strength(
    tables: &[MissionControlDeviceTableState],
    handle: ObjectHandle,
) -> f32 {
    let Some(table) = tables.iter().find(|table| table.handle == handle) else {
        return 0.0;
    };
    let capacity = table.node_provider_amount_bits.map_or(0.0, f32::from_bits);
    let MissionControlDeviceTableStatus::Available(devices) = &table.status else {
        return rating(capacity, 0.0);
    };
    // shortcut: assume full design ammunition and use the live Control amount
    // until selector 0x36 scaling and live weapon counters are wired.
    rating(capacity, weapon_power(devices, &table.node_provider_status))
}

pub(super) fn weapon_power(
    devices: &[EffectiveControlDevice],
    providers: &MissionControlNodeProviderTableStatus,
) -> f32 {
    let mut grouped = false;
    let mut weapon_power = 0.0;
    for device in devices {
        match device.descriptor.tag {
            1 => grouped = true,
            2 if grouped && device.factory_tag == 2 => {
                let (Some(coefficients), Some(volume)) =
                    (device.coefficient_bits, device.device174_bits)
                else {
                    continue;
                };
                if control_device_provider_value_from_status(providers, device)
                    .is_ok_and(|health| health == 0.0)
                {
                    continue;
                }
                let rounds = f32::from_bits(coefficients[0]).round_ties_even() as i32 as u32;
                let volume = f32::from_bits(volume);
                if rounds > 0 && volume.is_finite() {
                    weapon_power += (volume / rounds as f32) * rounds as f32;
                }
            }
            _ => {}
        }
    }
    weapon_power
}

pub(super) fn rating(capacity: f32, weapon_power: f32) -> f32 {
    ((weapon_power + 0.8_f32) * capacity) * 0.00001_f32
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn strength_scales_active_weapon_power_by_remaining_capacity() {
        assert_eq!(rating(200_000.0, 3.2), 8.0);
        assert_eq!(rating(100_000.0, 3.2), 4.0);
        assert_eq!(rating(0.0, 3.2), 0.0);
    }
}