aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/cockpit_status_hud.rs
blob: b263414baf8c6109c596583f4c8154278b568dd4 (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
use super::{
    cockpit_dial::{append_range, atlas_material},
    quad_indices, screen_tl_quad_vertices_pixels, DirectoryVfs, EnvironmentGpuRange,
    StaticPreviewScene, VulkanSmokeRenderer, VulkanStaticMesh, VulkanStaticVertex,
};
use fparkan_assets::ui_sprite::load_ui_page;
use fparkan_runtime::{cockpit_indicators::CockpitIndicators, RemoteWarbotControl};
use std::path::Path;

// iron3d+0x3f6xx: left edge of each frame in 640×480 coordinates.
const FRAME_X: [u32; 8] = [222, 240, 258, 280, 344, 366, 384, 402];
const FRAME_SRC_X: [u32; 3] = [202, 220, 238];
// iron3d+0x3f0d0: the danger icon uses ui_menu3, the others use ui_menu.
const ICON_SRC: [[u32; 2]; 8] = [
    [97, 126],
    [238, 222],
    [239, 126],
    [223, 142],
    [180, 27],
    [113, 94],
    [113, 110],
    [81, 94],
];
const DANGER_INDEX: usize = 4;
const MAIN_ICON_INDICES: [usize; 7] = [0, 1, 2, 3, 5, 6, 7];

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Lamp {
    Off,
    On,
    Red,
}

impl Lamp {
    fn from_active(active: bool) -> Self {
        if active {
            Self::On
        } else {
            Self::Off
        }
    }

    fn color(self) -> [f32; 3] {
        match self {
            Self::Off => [128.0 / 255.0; 3],
            Self::On => [1.0; 3],
            Self::Red => [1.0, 0.0, 0.0],
        }
    }
}

fn lamps(
    state: CockpitIndicators,
    infrared: bool,
    risk_since: Option<u32>,
    clock_ms: u32,
) -> [Lamp; 8] {
    let mut lamps = [Lamp::Off; 8];
    lamps[0] = Lamp::from_active(state.self_repair);
    lamps[1] = Lamp::from_active(infrared);
    lamps[2] = Lamp::from_active(state.camouflage);
    lamps[3] = Lamp::from_active(state.onsite);
    if risk_since.is_some_and(|start| clock_ms.wrapping_sub(start) <= 3_000) {
        lamps[DANGER_INDEX] = Lamp::Red;
    }
    let mode = match state.control {
        RemoteWarbotControl::Manual => 5,
        RemoteWarbotControl::AutoGunner => 6,
        RemoteWarbotControl::Autopilot => 7,
    };
    lamps[mode] = Lamp::On;
    lamps
}

pub(super) struct CockpitStatusHud {
    frames: EnvironmentGpuRange,
    main_icons: EnvironmentGpuRange,
    danger_icon: EnvironmentGpuRange,
    risk_since: Option<u32>,
}

impl CockpitStatusHud {
    pub(super) fn load(root: &Path, preview: &mut StaticPreviewScene) -> Result<Self, String> {
        let vfs = DirectoryVfs::new(root);
        let frames = load_ui_page(&vfs, "page6")?;
        let icons = load_ui_page(&vfs, "ui_menu")?;
        let danger = load_ui_page(&vfs, "ui_menu3")?;
        if [&frames, &icons, &danger]
            .iter()
            .any(|page| [page.width, page.height] != [256; 2])
        {
            return Err("unexpected cockpit indicator atlas dimensions".into());
        }
        let frames = atlas_material(preview, frames)?;
        let icons = atlas_material(preview, icons)?;
        let danger = atlas_material(preview, danger)?;
        Ok(Self {
            frames: append_range(preview, frames, 32, quad_indices(8)?)?,
            main_icons: append_range(preview, icons, 28, quad_indices(7)?)?,
            danger_icon: append_range(preview, danger, 4, quad_indices(1)?)?,
            risk_since: None,
        })
    }

    pub(super) fn note_exit_result(&mut self, clock_ms: u32, rejected: bool) {
        self.risk_since = rejected.then_some(clock_ms);
    }

    pub(super) fn ranges(&self) -> [usize; 3] {
        [
            self.frames.range_index,
            self.main_icons.range_index,
            self.danger_icon.range_index,
        ]
    }

    pub(super) fn draw(
        &self,
        renderer: &mut VulkanSmokeRenderer,
        mesh: &mut VulkanStaticMesh,
        state: Option<CockpitIndicators>,
        infrared: bool,
        clock_ms: u32,
    ) -> Result<(), String> {
        for (range, count) in self.ranges().into_iter().zip([48, 42, 6]) {
            renderer
                .set_draw_range_index_count(range, if state.is_some() { count } else { 0 })
                .map_err(|error| error.to_string())?;
        }
        let Some(state) = state else { return Ok(()) };
        let lamps = lamps(state, infrared, self.risk_since, clock_ms);
        for (index, lamp) in lamps.into_iter().enumerate() {
            let x = FRAME_X[index];
            let start = self.frames.vertex_start + index * 4;
            mesh.vertices[start..start + 4].copy_from_slice(&quad(
                [x, 459, 18, 25],
                [FRAME_SRC_X[lamp as usize], 154, 17, 20],
                [1.0; 3],
            ));
        }
        let icon = |index: usize| {
            let [u, v] = ICON_SRC[index];
            quad(
                [FRAME_X[index] + 1, 463, 15, 15],
                [u, v, 15, 15],
                lamps[index].color(),
            )
        };
        for (slot, index) in MAIN_ICON_INDICES.into_iter().enumerate() {
            let start = self.main_icons.vertex_start + slot * 4;
            mesh.vertices[start..start + 4].copy_from_slice(&icon(index));
        }
        let start = self.danger_icon.vertex_start;
        mesh.vertices[start..start + 4].copy_from_slice(&icon(DANGER_INDEX));
        Ok(())
    }
}

fn quad(
    [left, top, width, height]: [u32; 4],
    [u, v, tex_width, tex_height]: [u32; 4],
    color: [f32; 3],
) -> [VulkanStaticVertex; 4] {
    let [hw, hh] = [width as f32 * 0.5, height as f32 * 0.5];
    let [u0, v0] = [(u as f32 + 0.5) / 256.0, (v as f32 + 0.5) / 256.0];
    let [u1, v1] = [
        (u + tex_width) as f32 / 256.0,
        (v + tex_height) as f32 / 256.0,
    ];
    screen_tl_quad_vertices_pixels(
        [left as f32 + hw, top as f32 + hh],
        hw,
        hh,
        [640.0, 480.0],
        color,
        1.0,
        [[u0, v0], [u0, v1], [u1, v1], [u1, v0]],
    )
}

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

    #[test]
    fn marks_the_active_control_mode_and_expires_the_risk_warning() {
        let state = CockpitIndicators {
            self_repair: true,
            camouflage: false,
            onsite: false,
            control: RemoteWarbotControl::AutoGunner,
        };
        assert_eq!(
            lamps(state, true, Some(100), 3_100),
            [
                Lamp::On,
                Lamp::On,
                Lamp::Off,
                Lamp::Off,
                Lamp::Red,
                Lamp::Off,
                Lamp::On,
                Lamp::Off
            ],
        );
        assert_eq!(
            lamps(state, false, Some(100), 3_101)[DANGER_INDEX],
            Lamp::Off
        );
    }
}