From c3e2add9a189879b5e800344e07645af591a2cbd Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 00:14:47 +0400 Subject: feat(ui): show live weapon readings in the hero cockpit --- apps/fparkan-game/src/main.rs | 90 +++-------- apps/fparkan-game/src/mission_notices.rs | 3 +- apps/fparkan-game/src/mission_overlay.rs | 125 +++++++++++++++ apps/fparkan-game/src/weapon_hud.rs | 259 +++++++++++++++++++++++++++++++ 4 files changed, 406 insertions(+), 71 deletions(-) create mode 100644 apps/fparkan-game/src/mission_overlay.rs create mode 100644 apps/fparkan-game/src/weapon_hud.rs (limited to 'apps/fparkan-game/src') diff --git a/apps/fparkan-game/src/main.rs b/apps/fparkan-game/src/main.rs index 0206b86a..afb553e4 100644 --- a/apps/fparkan-game/src/main.rs +++ b/apps/fparkan-game/src/main.rs @@ -17,10 +17,13 @@ mod destruction_effects; mod mission_hints; mod mission_loading; mod mission_notices; +mod mission_overlay; mod mission_pause; mod motion_effects; mod player_collision; mod satellite_map; +#[cfg(windows)] +mod weapon_hud; #[cfg(test)] use fparkan_runtime::query_building_segment_world; #[cfg(test)] @@ -9588,28 +9591,13 @@ fn run_static_vulkan_mode( .map(|engine| cockpit_geometry::CockpitGeometry::prepare(engine, &mut preview)) .transpose()? .unwrap_or_default(); - #[cfg(windows)] - let mission_hints = if mission_engine.is_some() && demo_camera.is_none() { - mission_hints::MissionHints::load(root, mission, &mut preview) - .map(Some) - .unwrap_or_else(|error| { - eprintln!("sim_coverage mission_text_unavailable=1 reason={error}"); - None - }) - } else { - None - }; - let satellite_map = - if let Some(engine) = mission_engine.as_ref().filter(|_| demo_camera.is_none()) { - satellite_map::SatelliteMap::load(root, mission, engine, &mut preview).unwrap_or_else( - |error| { - eprintln!("satellite_map_unavailable mission={mission} reason={error}"); - None - }, - ) - } else { - None - }; + let overlay = mission_overlay::MissionOverlay::load( + root, + mission, + mission_engine.as_ref().filter(|_| demo_camera.is_none()), + cockpit, + &mut preview, + ); let shadow_base_vertex = preview.mesh.vertices.len(); let shadow_base_index = preview.mesh.indices.len(); let mut app = StaticVulkanApp::new( @@ -9644,22 +9632,11 @@ fn run_static_vulkan_mode( .retain(|index| app.world_range_index_counts.contains_key(index)); } app.shield_effects = shield_effects; - if let Some(map) = &satellite_map { - for range in map.ranges() { - app.world_range_indices.retain(|&index| index != range); - app.world_range_index_counts.remove(&range); - } - } - app.satellite_map = satellite_map; - #[cfg(windows)] - { - if let Some(hints) = &mission_hints { - let range = hints.range_index(); - app.world_range_indices.retain(|&index| index != range); - app.world_range_index_counts.remove(&range); - } - app.mission_hints = mission_hints; - } + overlay.remove_world_ranges( + &mut app.world_range_indices, + &mut app.world_range_index_counts, + ); + app.overlay = overlay; app.demo_camera = demo_camera; app.cockpit = cockpit; app.cockpit_geometry = cockpit_geometry; @@ -9882,9 +9859,7 @@ struct StaticVulkanApp { boarded_hero_index: Option, laboratory_status: Option, mission_title: String, - #[cfg(windows)] - mission_hints: Option, - satellite_map: Option, + overlay: mission_overlay::MissionOverlay, audio: Option, camera_mode: &'static str, mesh_components: usize, @@ -10053,9 +10028,7 @@ impl StaticVulkanApp { boarded_hero_index: None, laboratory_status: None, mission_title: String::new(), - #[cfg(windows)] - mission_hints: None, - satellite_map: None, + overlay: mission_overlay::MissionOverlay::default(), audio, camera_mode: preview.camera_mode, mesh_components: preview.mesh_components, @@ -11664,36 +11637,13 @@ impl StaticVulkanApp { fn render_scene_update(&mut self) -> Result<(), String> { self.upload_live_basements()?; + self.draw_mission_overlay()?; let Some(frame) = self.pending_scene_update.as_ref() else { return Ok(()); }; let Some(renderer) = self.renderer.as_mut() else { return Ok(()); }; - if let Some(map) = &mut self.satellite_map { - map.draw( - renderer, - &mut self.mesh, - frame.viewport, - self.mission_engine - .as_ref() - .zip(self.cockpit) - .map(|(engine, cockpit)| (engine, cockpit, self.boarding_target)), - )?; - } - #[cfg(windows)] - if let Some(hints) = &mut self.mission_hints { - hints.draw( - renderer, - &mut self.mesh, - frame.clock.time_ms, - frame.viewport, - self.mission_engine - .as_ref() - .and_then(fparkan_runtime::loaded_mission_script_outcome) - .unwrap_or_default(), - )?; - } renderer .set_camera(frame.camera.vulkan_camera()) .map_err(|error| format!("apply sampled scene camera: {error}"))?; @@ -12452,7 +12402,7 @@ impl ApplicationHandler for StaticVulkanApp { && !event.repeat && !self.mission_paused { - if let Some(hints) = &mut self.mission_hints { + if let Some(hints) = &mut self.overlay.hints { hints.toggle_current(); } } @@ -12490,7 +12440,7 @@ impl ApplicationHandler for StaticVulkanApp { || self.pressed_keys.contains(&KeyCode::ControlRight) { self.player_mine_pressed = true; - } else if let Some(map) = &mut self.satellite_map { + } else if let Some(map) = &mut self.overlay.satellite_map { map.toggle(); } } diff --git a/apps/fparkan-game/src/mission_notices.rs b/apps/fparkan-game/src/mission_notices.rs index 1a0bc6a2..6b876e3d 100644 --- a/apps/fparkan-game/src/mission_notices.rs +++ b/apps/fparkan-game/src/mission_notices.rs @@ -22,7 +22,8 @@ impl StaticVulkanApp { #[cfg(windows)] { let posted = self - .mission_hints + .overlay + .hints .as_mut() .ok_or_else(|| "mission text resources unavailable".to_string()) .and_then(|hints| hints.post(index, clock_ms)); diff --git a/apps/fparkan-game/src/mission_overlay.rs b/apps/fparkan-game/src/mission_overlay.rs new file mode 100644 index 00000000..6c87492c --- /dev/null +++ b/apps/fparkan-game/src/mission_overlay.rs @@ -0,0 +1,125 @@ +#[cfg(windows)] +use super::{mission_hints::MissionHints, weapon_hud::WeaponHud}; +use super::{satellite_map::SatelliteMap, StaticPreviewScene, StaticVulkanApp}; +use fparkan_runtime::{Engine, StartingCockpit}; +use std::path::Path; + +#[derive(Default)] +pub(super) struct MissionOverlay { + pub(super) satellite_map: Option, + #[cfg(windows)] + pub(super) hints: Option, + #[cfg(windows)] + weapons: Option, +} + +impl MissionOverlay { + pub(super) fn load( + root: &Path, + mission: &str, + engine: Option<&Engine>, + cockpit: Option, + preview: &mut StaticPreviewScene, + ) -> Self { + let Some(engine) = engine else { + return Self::default(); + }; + #[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, preview) + .map(Some) + .unwrap_or_else(|error| { + eprintln!("weapon_hud_unavailable reason={error}"); + None + }) + }); + #[cfg(not(windows))] + let _ = cockpit; + Self { + satellite_map, + #[cfg(windows)] + hints, + #[cfg(windows)] + weapons, + } + } + + pub(super) fn remove_world_ranges( + &self, + ranges: &mut Vec, + counts: &mut std::collections::HashMap, + ) { + let remove = |range: usize, + ranges: &mut Vec, + counts: &mut std::collections::HashMap| { + ranges.retain(|&index| index != range); + counts.remove(&range); + }; + if let Some(map) = &self.satellite_map { + for range in map.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)?; + } + #[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 = self.cockpit.and_then(|cockpit| { + self.player_weapon_banks + .get(&cockpit.handle) + .map(|bank| (cockpit.handle, bank)) + }); + weapons.draw(renderer, &mut self.mesh, frame.viewport, current)?; + } + Ok(()) + } +} diff --git a/apps/fparkan-game/src/weapon_hud.rs b/apps/fparkan-game/src/weapon_hud.rs new file mode 100644 index 00000000..68144b28 --- /dev/null +++ b/apps/fparkan-game/src/weapon_hud.rs @@ -0,0 +1,259 @@ +use super::{ + append_environment_gradient_material, append_environment_range, environment_vertex, + quad_indices, screen_tl_quad_vertices_pixels, world_pipeline_state, DirectoryVfs, + EnvironmentGpuRange, LegacyBlendMode, LegacyDepthMode, StaticPreviewScene, VulkanSmokeRenderer, + VulkanStaticMesh, VulkanStaticTexture, +}; +use fparkan_assets::{ + game_font::{load_layout_font, BitmapFont}, + ui_sprite::load_ui_sprites, + RgbaImage, +}; +use fparkan_platform_winit::text::StringResources; +use fparkan_runtime::{PlayerWeapons, WeaponIndicator, WeaponLamp}; +use fparkan_world::ObjectHandle; +use std::path::Path; + +const ROW_HEIGHT: u16 = 19; +const AMMO_WIDTH: u16 = 24; +const SLOT_WIDTH: u16 = 12; +const NAME_WIDTH: u16 = 120; +const ROW_COUNT: usize = 4; +// The native renderer lays out cells from the right edge, with a one-pixel overlap. +const WIDTH: u32 = 202; +const HEIGHT: u32 = ROW_COUNT as u32 * ROW_HEIGHT as u32; +const UV: [[f32; 2]; 4] = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]]; + +pub(super) struct WeaponHud { + hero: ObjectHandle, + range: EnvironmentGpuRange, + labels: [RgbaImage; ROW_COUNT], + font: BitmapFont, + unlimited: String, + lamps: [RgbaImage; 5], + end: RgbaImage, + digits: RgbaImage, + separator: RgbaImage, + emitter: RgbaImage, + body: RgbaImage, + cap: RgbaImage, + previous: [Option; ROW_COUNT], +} + +impl WeaponHud { + pub(super) fn load( + root: &Path, + hero: ObjectHandle, + preview: &mut StaticPreviewScene, + ) -> Result { + let vfs = DirectoryVfs::new(root); + let [red, yellow1, yellow2, green, black, end, digits, separator, emitter, body, cap] = + load_ui_sprites( + &vfs, + "ui/compaund.cfg", + [ + "ccres_red_lamp", + "ccres_yellow1_lamp", + "ccres_yellow2_lamp", + "ccres_green_lamp", + "ccres_black_lamp", + "ccres_ending_text", + "ccres_body_text", + "ccres_separator_left_text", + "ccres_ray_emitter_off", + "ccres_ray_body", + "ccres_ray_ending", + ], + )?; + let lamps = [red, yellow1, yellow2, green, black]; + if lamps + .iter() + .chain([&end, &digits, &separator, &emitter, &body, &cap]) + .any(|sprite| sprite.height != u32::from(ROW_HEIGHT)) + || lamps.iter().any(|lamp| lamp.width != 19) + || [end.width, separator.width, emitter.width, cap.width] != [5, 5, 10, 6] + { + return Err("inconsistent original weapon HUD sprite dimensions".into()); + } + let font = load_layout_font(&vfs)?; + let strings = StringResources::open(&root.join("iron3d.dll"))?; + let mut labels = Vec::with_capacity(ROW_COUNT); + // The hero uses the ordered labels loaded by iron3d+0x74f40. + for id in 0x0bff..=0x0c02 { + let text = strings + .get(id) + .ok_or_else(|| format!("missing weapon label {id:#x}"))?; + labels.push(font.centered_line(&text, NAME_WIDTH, ROW_HEIGHT)?); + } + let unlimited = strings + .get(0x13e6) + .ok_or("missing unlimited-ammunition label")?; + let material = append_environment_gradient_material(&mut preview.materials)?; + preview.materials[material].screen_tl = true; + preview.materials[material].sky_far_depth = false; + preview.materials[material].texture = blank_texture(); + let range = append_environment_range( + &mut preview.mesh, + material, + vec![environment_vertex([0.0; 3], [1.0; 3], [0.0; 2]); 4], + quad_indices(1)?, + world_pipeline_state(LegacyBlendMode::SourceAlpha, LegacyDepthMode::Disabled), + )?; + Ok(Self { + hero, + range, + font, + unlimited, + end, + digits, + separator, + emitter, + body, + cap, + labels: labels + .try_into() + .map_err(|_| "unexpected weapon label count")?, + lamps, + previous: [None; ROW_COUNT], + }) + } + + pub(super) fn range_index(&self) -> usize { + self.range.range_index + } + + pub(super) fn draw( + &mut self, + renderer: &mut VulkanSmokeRenderer, + mesh: &mut VulkanStaticMesh, + viewport: [f32; 2], + current: Option<(ObjectHandle, &PlayerWeapons)>, + ) -> Result<(), String> { + let bank = + current.filter(|(owner, bank)| *owner == self.hero && !bank.weapons().is_empty()); + renderer + .set_draw_range_index_count(self.range_index(), if bank.is_some() { 6 } else { 0 }) + .map_err(|error| error.to_string())?; + let Some((_, bank)) = bank else { return Ok(()) }; + let mut slots = bank.indicators(); + let state = std::array::from_fn(|_| slots.next()); + if state != self.previous { + let mut image = blank_texture(); + for (index, indicator) in state.iter().enumerate() { + if let Some(indicator) = indicator { + self.draw_row(&mut image, index, *indicator)?; + } + } + renderer + .update_material_texture(self.range.material_index, 0, &image) + .map_err(|error| error.to_string())?; + self.previous = state; + } + let width = WIDTH as f32 * viewport[0] / 640.0; + let height = HEIGHT as f32 * viewport[1] / 480.0; + let start = self.range.vertex_start; + mesh.vertices[start..start + 4].copy_from_slice(&screen_tl_quad_vertices_pixels( + [viewport[0] - width * 0.5, height * 0.5], + width * 0.5, + height * 0.5, + viewport, + [1.0; 3], + 1.0, + UV, + )); + Ok(()) + } + + fn draw_row( + &self, + image: &mut VulkanStaticTexture, + index: usize, + state: WeaponIndicator, + ) -> Result<(), String> { + let y = index as u32 * u32::from(ROW_HEIGHT); + let lamp = &self.lamps[match state.lamp { + WeaponLamp::Unavailable => 0, + WeaponLamp::Reloading => 1, + WeaponLamp::Cycling => 2, + WeaponLamp::Ready => 3, + WeaponLamp::Unselected => 4, + }]; + let mut right = WIDTH; + let mut cell = |sprite: &RgbaImage, width: u32| { + right -= width; + paint(image, sprite, right - 1, y, width + 1); + right + }; + cell(&self.end, self.end.width); + let slot_x = cell(&self.digits, u32::from(SLOT_WIDTH)); + cell(lamp, lamp.width); + let ammo_x = cell(&self.digits, u32::from(AMMO_WIDTH)); + cell(&self.separator, self.separator.width); + cell(&self.emitter, self.emitter.width); + let name_x = cell(&self.body, u32::from(NAME_WIDTH)); + cell(&self.cap, self.cap.width); + let tint = match state.charge_percent { + 0..=19 => [128, 0, 0, 128], + 20..=79 => [128, 128, 0, 128], + _ => [0, 128, 0, 128], + }; + let charged = (f64::from(state.charge_percent) * f64::from(NAME_WIDTH) / 100.0) + .round_ties_even() as u32; + for row in y + 3..y + u32::from(ROW_HEIGHT) - 3 { + for x in name_x + u32::from(NAME_WIDTH) - charged..name_x + u32::from(NAME_WIDTH) - 2 { + paint_pixel(image, x, row, &tint); + } + } + let slot = self + .font + .centered_line(&(index + 1).to_string(), SLOT_WIDTH, ROW_HEIGHT)?; + let rounds = state + .rounds + .map_or_else(|| self.unlimited.clone(), |count| count.to_string()); + let ammo = self.font.centered_line(&rounds, AMMO_WIDTH, ROW_HEIGHT)?; + for (x, glyphs) in [ + (slot_x, &slot), + (ammo_x, &ammo), + (name_x, &self.labels[index]), + ] { + paint(image, glyphs, x, y, glyphs.width); + } + Ok(()) + } +} + +fn blank_texture() -> VulkanStaticTexture { + VulkanStaticTexture { + width: WIDTH, + height: HEIGHT, + rgba8: vec![0; (WIDTH * HEIGHT * 4) as usize], + mip_levels: Vec::new(), + } +} + +fn paint(image: &mut VulkanStaticTexture, sprite: &RgbaImage, left: u32, top: u32, width: u32) { + for y in 0..sprite.height { + for x in 0..width { + let source_x = x * sprite.width / width; + let offset = ((y * sprite.width + source_x) * 4) as usize; + paint_pixel(image, left + x, top + y, &sprite.rgba8[offset..offset + 4]); + } + } +} + +fn paint_pixel(image: &mut VulkanStaticTexture, x: u32, y: u32, source: &[u8]) { + let alpha = u32::from(source[3]); + if alpha == 0 { + return; + } + let start = ((y * WIDTH + x) * 4) as usize; + let pixel = &mut image.rgba8[start..start + 4]; + 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; +} -- cgit v1.2.3