aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/strategic_map_readout.rs
blob: 00eae097089cb581a7c1d0eba09c3aeb8c2399fb (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! Live information for the hovered object on the strategic satellite map.

use crate::{
    satellite_contacts,
    strategic_roster::{order_label, unit_prefix},
    Engine, StartingCockpit, HERO_UNIT_CLASS,
};
use fparkan_world::identity_metadata;

/// Object data and original resource selectors for the map footer.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ContactReadout {
    pub class: u32,
    pub size: u32,
    pub chassis: u32,
    pub clan: u16,
    pub emblem_tint: [u8; 3],
    pub name_label: u32,
    pub callsign: Option<([char; 3], u32)>,
    pub order_label: Option<u32>,
    pub durability_percent: Option<i32>,
}

/// Resolves the hovered map contact. Empty terrain and depleted objects have no readout.
pub fn contact_at(
    engine: &Engine,
    cockpit: StartingCockpit,
    point: [f32; 2],
) -> Option<ContactReadout> {
    let handle = satellite_contacts::pick_contact(engine, cockpit, point)?;
    contact_readout(engine, cockpit, handle)
}

pub(super) fn contact_readout(
    engine: &Engine,
    cockpit: StartingCockpit,
    handle: fparkan_world::ObjectHandle,
) -> Option<ContactReadout> {
    let mission = engine.loaded.as_ref()?;
    let player = identity_metadata(&engine.world, cockpit.handle)
        .ok()?
        .owner_id?;
    let identity = identity_metadata(&engine.world, handle).ok()?;
    let owner = identity.owner_id?;
    let original = identity.original_id?.0;
    let draft = mission.object_drafts.get(original as usize)?;
    let class = draft.unit_dat_header_flags?;
    let building = class & 0x8000_0000 != 0;
    let root = &draft.unit_components.first()?.resource_raw;
    let size = crate::unit_size_class::classify(if building { 3 } else { 4 }, root);
    let chassis = mission
        .unit_chassis_flags
        .get(&original)
        .and_then(|flags| flags.chassis_type)
        .unwrap_or(0);
    let (name_label, callsign, order_label) = if building {
        (building_label(class, size), None, None)
    } else {
        let (role, label) = match class {
            0x0100_2000 => ('T', 0x1838),
            0x0100_4000 => ('B', 0x1839),
            0x0100_8000 => ('W', 0x183a),
            0x0101_0000 => ('C', 0x183b),
            HERO_UNIT_CLASS => ('?', 0x1856),
            // Animals have map marks but no footer.
            _ => return None,
        };
        let callsign = (class != HERO_UNIT_CLASS)
            .then(|| {
                Some((
                    unit_prefix(size, chassis, role),
                    mission.unit_serials.get(handle)?,
                ))
            })
            .flatten();
        let task = mission
            .unit_patrol
            .as_ref()
            .map_or(0, |patrol| patrol.active_order_code(handle));
        (label, callsign, Some(order_label(task)))
    };
    let observer = mission.mission.clans.get(usize::from(player.0))?;
    let target = mission.mission.clans.get(usize::from(owner.0))?;
    let relation = observer
        .relations
        .iter()
        .find(|entry| entry.other_clan_name.raw == target.name.raw)
        .map(|entry| entry.relation_value);
    let emblem_tint = emblem_tint(owner == player, relation, building);
    let durability_percent = mission
        .control_device_tables
        .iter()
        .find(|table| table.handle == handle)
        .and_then(crate::warbot_stats::durability_percent);
    Some(ContactReadout {
        class,
        size,
        chassis,
        clan: owner.0,
        emblem_tint,
        name_label,
        callsign,
        order_label,
        durability_percent,
    })
}

fn emblem_tint(own: bool, relation: Option<i32>, building: bool) -> [u8; 3] {
    match (own, relation, building) {
        (false, Some(0), true) => [255; 3],
        (false, Some(0), false) => [255, 0, 0],
        (false, Some(1), _) => [128; 3],
        _ => satellite_contacts::OWN_TINT,
    }
}

pub(super) fn building_label(class: u32, size: u32) -> u32 {
    // iron3d+0x33a90 uses the type and Behavior's size selector.
    match (class, size) {
        (0x8000_0002, _) => 0x1799,
        (0x8000_0004, 2..=4) => 0x178d + size,
        (0x8000_0008, 2..=4) => 0x1792 + size,
        (0x8000_0010, 2..=4) => 0x17a1 + size,
        (0x8000_0040, _) => 0x17a8,
        (0x8000_0200, 2..=3) => 0x17b5 + size,
        (0x8000_0400, 2..=5) => 0x179c + size,
        (0x8000_1000, 2..=5) => 0x17b0 + size,
        (0x8001_0000, _) => 0x17c6,
        (0x8002_0000, _) => 0x17cc,
        (0x8004_0000, _) => 0x17d2,
        (0x8010_0000, _) => 0x17bd,
        (0x8020_0000, _) => 0x17c3,
        _ => 0x183d,
    }
}

#[cfg(test)]
mod tests {
    use super::{building_label, emblem_tint};

    #[test]
    fn warehouse_size_changes_the_name_while_bunker_class_fixes_it() {
        assert_eq!(building_label(0x8000_0008, 2), 0x1794);
        assert_eq!(building_label(0x8000_0008, 3), 0x1795);
        assert_eq!(building_label(0x8002_0000, 3), 0x17cc);
        assert_eq!(building_label(0x8000_0008, 5), 0x183d);
    }

    #[test]
    fn the_own_clan_keeps_its_blue_emblem() {
        assert_eq!(emblem_tint(true, Some(1), false), [128, 128, 255]);
        assert_eq!(emblem_tint(false, Some(1), false), [128; 3]);
        assert_eq!(emblem_tint(false, Some(0), true), [255; 3]);
        assert_eq!(emblem_tint(false, Some(0), false), [255, 0, 0]);
    }
}