aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/unit_size_class.rs
blob: a786c610452122879c2d677093270a673a982650 (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
use crate::{mission_events::MissionEvents, MissionDocument, MissionObjectDraft};
use fparkan_world::{identity_metadata, World};

pub(super) fn query(
    mission: &MissionDocument,
    roster: &MissionEvents,
    drafts: &[MissionObjectDraft],
    world: &World,
    id: u32,
) -> Result<u32, String> {
    let Some(unit) = roster.logic_objects(mission, world, id).next() else {
        return Ok(u32::MAX);
    };
    let draft = identity_metadata(world, unit)
        .ok()
        .and_then(|identity| identity.original_id)
        .and_then(|id| drafts.get(id.0 as usize))
        .ok_or("Handler(72) unit descriptor is unavailable")?;
    let resource = &draft
        .unit_components
        .first()
        .ok_or("Handler(72) root prototype is unavailable")?
        .resource_raw;
    // shortcut: infer the descriptor kind from Unit DAT until native descriptor interfaces are wired.
    let kind = match draft.unit_dat_header_flags {
        Some(class) if class & 0x8000_0000 != 0 || class == 0x0101_0000 => 3,
        Some(class) if class & 0x0100_0000 != 0 => 4,
        _ => return Err("Handler(72) descriptor kind is unavailable".into()),
    };
    Ok(classify(kind, resource))
}

pub(super) fn classify(kind: u32, name: &[u8]) -> u32 {
    let name = &name[..name
        .iter()
        .position(|&byte| byte == 0)
        .unwrap_or(name.len())];
    let index = match kind {
        3 => 3,
        4 => 2,
        _ => return 0,
    };
    match (kind, name.get(index).map(u8::to_ascii_lowercase)) {
        (4, Some(b't')) => 1,
        (3 | 4, Some(b'l')) | (4, Some(b'h')) => 2,
        (3 | 4, Some(b'm')) => 3,
        (3 | 4, Some(b'b')) => 4,
        (3, Some(b'e')) => 5,
        _ => 0,
    }
}

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

    #[test]
    fn native_descriptor_classes_use_the_kind_and_fixed_character() {
        for (kind, name, expected) in [
            (4, "R_T_02", 1),
            (4, "r_l_01", 2),
            (4, "r_h_03", 2),
            (4, "r_m_02", 3),
            (4, "r_b_01", 4),
            (4, "r_e_01", 0),
            (3, "fr_l_bunker", 2),
            (3, "fr_m_cargo", 3),
            (3, "fr_b_plant", 4),
            (3, "fr_e_lab", 5),
            (3, "fr_h_invalid", 0),
            (3, "fr_t_invalid", 0),
            (3, "xx_m", 3),
            (2, "r_l_01", 0),
            (4, "r", 0),
        ] {
            assert_eq!(classify(kind, name.as_bytes()), expected, "{kind} {name}");
        }
        assert_eq!(classify(4, b"r_\0m"), 0);
    }
}