aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/cockpit_status_hud.rs
diff options
context:
space:
mode:
Diffstat (limited to 'apps/fparkan-game/src/cockpit_status_hud.rs')
-rw-r--r--apps/fparkan-game/src/cockpit_status_hud.rs202
1 files changed, 202 insertions, 0 deletions
diff --git a/apps/fparkan-game/src/cockpit_status_hud.rs b/apps/fparkan-game/src/cockpit_status_hud.rs
new file mode 100644
index 00000000..440622af
--- /dev/null
+++ b/apps/fparkan-game/src/cockpit_status_hud.rs
@@ -0,0 +1,202 @@
+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, risk_since: Option<u32>, clock_ms: u32) -> [Lamp; 8] {
+ let mut lamps = [Lamp::Off; 8];
+ lamps[0] = Lamp::from_active(state.self_repair);
+ // Slot 1 follows the infrared camera pass when that pass is available.
+ 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>,
+ 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, 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, Some(100), 3_100),
+ [
+ Lamp::On,
+ Lamp::Off,
+ Lamp::Off,
+ Lamp::Off,
+ Lamp::Red,
+ Lamp::Off,
+ Lamp::On,
+ Lamp::Off
+ ],
+ );
+ assert_eq!(lamps(state, Some(100), 3_101)[DANGER_INDEX], Lamp::Off);
+ }
+}