aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/mission_overlay.rs
blob: e191aed5a248b055d57327bb727f106c6502e6c4 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use super::{
    cockpit_dial::CockpitDial, cockpit_radar_hud::CockpitRadarHud, horizontal_fov,
    satellite_map::SatelliteMap, StaticPreviewScene, StaticVulkanApp,
};
#[cfg(windows)]
use super::{mission_hints::MissionHints, weapon_hud::WeaponHud};
use fparkan_runtime::{
    cockpit_altimeter::cockpit_altimeter,
    radar::{cockpit_radar_period, cockpit_range_readout},
    Engine, StartingCockpit,
};
use std::path::Path;

/// Source-over composition for original sprite texels and their alpha masks.
pub(super) fn source_over(pixel: &mut [u8], source: &[u8]) {
    let alpha = u32::from(source[3]);
    if alpha == 0 {
        return;
    }
    let under = (u32::from(pixel[3]) * (255 - alpha) + 127) / 255;
    let combined = alpha + under;
    for index in 0..3 {
        pixel[index] =
            ((u32::from(source[index]) * alpha + u32::from(pixel[index]) * under + combined / 2)
                / combined) as u8;
    }
    pixel[3] = combined as u8;
}

#[derive(Default)]
pub(super) struct MissionOverlay {
    pub(super) satellite_map: Option<SatelliteMap>,
    dial: Option<CockpitDial>,
    pub(super) radar: Option<CockpitRadarHud>,
    #[cfg(windows)]
    pub(super) hints: Option<MissionHints>,
    #[cfg(windows)]
    weapons: Option<WeaponHud>,
}

impl MissionOverlay {
    pub(super) fn load(
        root: &Path,
        mission: &str,
        engine: Option<&Engine>,
        cockpit: Option<StartingCockpit>,
        weapon_rows: usize,
        preview: &mut StaticPreviewScene,
    ) -> Self {
        let Some(engine) = engine else {
            return Self::default();
        };
        // The original draws the cockpit instruments before the satellite map.
        let dial = cockpit.and_then(|_| {
            CockpitDial::load(root, preview)
                .map(Some)
                .unwrap_or_else(|error| {
                    eprintln!("cockpit_dial_unavailable reason={error}");
                    None
                })
        });
        let radar = cockpit.and_then(|_| {
            CockpitRadarHud::load(root, preview)
                .map(Some)
                .unwrap_or_else(|error| {
                    eprintln!("cockpit_radar_unavailable reason={error}");
                    None
                })
        });
        #[cfg(windows)]
        let hints = MissionHints::load(root, mission, preview)
            .map(Some)
            .unwrap_or_else(|error| {
                eprintln!("sim_coverage mission_text_unavailable=1 reason={error}");
                None
            });
        let satellite_map =
            SatelliteMap::load(root, mission, engine, preview).unwrap_or_else(|error| {
                eprintln!("satellite_map_unavailable mission={mission} reason={error}");
                None
            });
        #[cfg(windows)]
        let weapons = cockpit.and_then(|cockpit| {
            WeaponHud::load(root, cockpit.handle, weapon_rows, preview)
                .map(Some)
                .unwrap_or_else(|error| {
                    eprintln!("weapon_hud_unavailable reason={error}");
                    None
                })
        });
        #[cfg(not(windows))]
        let _ = weapon_rows;
        Self {
            satellite_map,
            dial,
            radar,
            #[cfg(windows)]
            hints,
            #[cfg(windows)]
            weapons,
        }
    }

    pub(super) fn remove_world_ranges(
        &self,
        ranges: &mut Vec<usize>,
        counts: &mut std::collections::HashMap<usize, u32>,
    ) {
        let remove = |range: usize,
                      ranges: &mut Vec<usize>,
                      counts: &mut std::collections::HashMap<usize, u32>| {
            ranges.retain(|&index| index != range);
            counts.remove(&range);
        };
        if let Some(map) = &self.satellite_map {
            for range in map.ranges() {
                remove(range, ranges, counts);
            }
        }
        if let Some(radar) = &self.radar {
            for range in radar.ranges() {
                remove(range, ranges, counts);
            }
        }
        if let Some(dial) = &self.dial {
            for range in dial.ranges() {
                remove(range, ranges, counts);
            }
        }
        #[cfg(windows)]
        if let Some(hints) = &self.hints {
            remove(hints.range_index(), ranges, counts);
        }
        #[cfg(windows)]
        if let Some(weapons) = &self.weapons {
            remove(weapons.range_index(), ranges, counts);
        }
    }
}

impl StaticVulkanApp {
    pub(super) fn draw_mission_overlay(&mut self) -> Result<(), String> {
        let (Some(renderer), Some(frame)) = (&mut self.renderer, &self.pending_scene_update) else {
            return Ok(());
        };
        if let Some(map) = &mut self.overlay.satellite_map {
            let player = self
                .mission_engine
                .as_ref()
                .zip(self.cockpit)
                .map(|(engine, cockpit)| (engine, cockpit, self.boarding_target));
            map.draw(renderer, &mut self.mesh, frame.viewport, player)?;
        }
        let occupant = self.mission_engine.as_ref().zip(self.cockpit);
        if let Some(dial) = &mut self.overlay.dial {
            let view = self.cockpit.map(|_| {
                let [x, y, _] = frame.camera.forward();
                ([x, y], horizontal_fov(&frame.camera, frame.aspect))
            });
            let altitude =
                occupant.and_then(|(engine, cockpit)| cockpit_altimeter(engine, cockpit));
            dial.draw(renderer, &mut self.mesh, frame.viewport, view, altitude)?;
        }
        if let Some(radar) = &mut self.overlay.radar {
            let period =
                occupant.and_then(|(engine, cockpit)| cockpit_radar_period(engine, cockpit));
            if radar.draw_sweep(
                renderer,
                &mut self.mesh,
                frame.viewport,
                frame.clock.time_ms,
                period,
            )? {
                if let Some(audio) = &mut self.audio {
                    if let Err(error) = audio.play_radar_pulse() {
                        eprintln!("radar_audio_unavailable reason={error}");
                    }
                }
            }
            radar.draw_readout(
                renderer,
                occupant.map(|(engine, cockpit)| cockpit_range_readout(engine, cockpit)),
            )?;
        }
        #[cfg(windows)]
        if let Some(hints) = &mut self.overlay.hints {
            let outcome = self
                .mission_engine
                .as_ref()
                .and_then(fparkan_runtime::loaded_mission_script_outcome)
                .unwrap_or_default();
            hints.draw(
                renderer,
                &mut self.mesh,
                frame.clock.time_ms,
                frame.viewport,
                outcome,
            )?;
        }
        #[cfg(windows)]
        if let Some(weapons) = &mut self.overlay.weapons {
            let current = occupant.and_then(|(engine, cockpit)| {
                let bank = self.player_weapon_banks.get(&cockpit.handle)?;
                let components = fparkan_runtime::loaded_mission_object_drafts(engine)
                    .and_then(|drafts| drafts.get(cockpit.object_index))
                    .map_or(&[][..], |draft| draft.unit_components.as_slice());
                Some((cockpit.handle, bank, components))
            });
            weapons.draw(renderer, &mut self.mesh, frame.viewport, current)?;
        }
        Ok(())
    }
}