aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/strategic_toolbar.rs
blob: a086a4827d3ca51d7bc3fb31f03635a3211493f5 (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
//! Clan membership queries for the strategic toolbar sections.

use crate::{Engine, StartingCockpit};
use fparkan_world::{identity_metadata, registration_sequence};

// Masks in screen order, as queried by iron3d+0x84040 for the eight lists.
const SECTION_MASKS: [u32; 8] = [
    0x0101_8000,
    0x0100_4000,
    0x0100_2000,
    0x8000_0400,
    0x8000_0010,
    0x8030_0000,
    crate::strategic_buildings::BUNKERS,
    crate::strategic_buildings::OTHER_BUILDINGS,
];

/// Availability for warbots, builders, transports, labs, factories, towers,
/// bunkers, and other buildings, based on live objects of the cockpit's clan.
#[must_use]
pub fn available_sections(engine: &Engine, cockpit: StartingCockpit) -> [bool; 8] {
    let Some((mission, owner)) = engine.loaded.as_ref().zip(
        identity_metadata(&engine.world, cockpit.handle)
            .ok()
            .and_then(|identity| identity.owner_id),
    ) else {
        return [false; 8];
    };
    let classes = mission.control_device_tables.iter().filter_map(|table| {
        let handle = table.handle;
        if table.node_provider_depletion_marked
            || identity_metadata(&engine.world, handle).ok()?.owner_id != Some(owner)
            || registration_sequence(&engine.world, handle).ok()?.is_none()
        {
            return None;
        }
        mission
            .object_drafts
            .get(table.original_id?.0 as usize)?
            .unit_dat_header_flags
    });
    sections_for_classes(classes)
}

fn sections_for_classes(classes: impl Iterator<Item = u32>) -> [bool; 8] {
    let mut available = [false; 8];
    for class in classes.filter(|&class| class != 0) {
        for (slot, mask) in available.iter_mut().zip(SECTION_MASKS) {
            *slot |= class & mask == class;
        }
    }
    available
}

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

    #[test]
    fn command_warbot_and_laboratory_enable_native_sections() {
        assert_eq!(
            sections_for_classes(
                [0, 0x0101_0000, 0x0100_4000, 0x8000_0400, 0x8004_0000].into_iter()
            ),
            [true, true, false, true, false, false, true, true],
        );
    }
}