use super::{ append_environment_gradient_material, append_environment_range, environment_vertex, quad_indices, screen_tl_quad_vertices_pixels, world_pipeline_state, EnvironmentGpuRange, LegacyBlendMode, LegacyDepthMode, StaticPreviewScene, VulkanSmokeRenderer, VulkanStaticMesh, VulkanStaticTexture, }; use fparkan_assets::{satellite_map::load_satellite_map, RgbaImage}; use fparkan_runtime::{satellite_contacts, Engine, StartingCockpit}; use fparkan_vfs::DirectoryVfs; use fparkan_world::ObjectHandle; use std::{path::Path, sync::Arc}; const UV: [[f32; 2]; 4] = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]]; // Native initial background colour, iron3d+0x74cf0. const MAP_TINT: [f32; 3] = [55.0 / 255.0, 1.0, 55.0 / 255.0]; const MAP_ALPHA: f32 = 128.0 / 255.0; const MAP_SIZE: usize = 256; pub(super) struct SatelliteMap { background: EnvironmentGpuRange, compass: EnvironmentGpuRange, compass_size: [f32; 2], contact_layer: EnvironmentGpuRange, icons: RgbaImage, extent: Option, buildings: Vec, units: Vec, visible: bool, } impl SatelliteMap { pub(super) fn load( root: &Path, mission: &str, engine: &Engine, preview: &mut StaticPreviewScene, ) -> Result, String> { let Some(assets) = load_satellite_map(Arc::new(DirectoryVfs::new(root)), mission)? else { return Ok(None); }; let compass_size = [assets.compass.width as f32, assets.compass.height as f32]; Ok(Some(Self { background: add_sprite(preview, assets.background)?, compass: add_sprite(preview, assets.compass)?, compass_size, contact_layer: add_sprite(preview, empty_layer())?, icons: assets.icons, extent: satellite_contacts::landscape_extent(engine), buildings: Vec::new(), units: Vec::new(), visible: false, })) } pub(super) fn ranges(&self) -> [usize; 3] { [ self.background.range_index, self.compass.range_index, self.contact_layer.range_index, ] } pub(super) fn toggle(&mut self) { self.visible = !self.visible; } pub(super) fn draw( &mut self, renderer: &mut VulkanSmokeRenderer, mesh: &mut VulkanStaticMesh, viewport: [f32; 2], player: Option<(&Engine, StartingCockpit, Option)>, ) -> Result<(), String> { let count = if self.visible { 6 } else { 0 }; for index in self.ranges() { renderer .set_draw_range_index_count(index, count) .map_err(|error| error.to_string())?; } if !self.visible { return Ok(()); } let size = (viewport[0].min(viewport[1]) - 32.0).clamp(1.0, 256.0); let left = 16.0; let bottom = viewport[1] - 16.0; let center = [left + size * 0.5, bottom - size * 0.5]; let background = screen_tl_quad_vertices_pixels( center, size * 0.5, size * 0.5, viewport, MAP_TINT, MAP_ALPHA, UV, ); mesh.vertices[self.background.vertex_start..self.background.vertex_start + 4] .copy_from_slice(&background); self.draw_contacts(renderer, mesh, viewport, center, size, player)?; let scale = size / 256.0; let [width, height] = self.compass_size.map(|dimension| dimension * scale); let center = [ left + size - scale - width * 0.5, bottom - scale - height * 0.5, ]; let compass = screen_tl_quad_vertices_pixels( center, width * 0.5, height * 0.5, viewport, [1.0; 3], 1.0, UV, ); mesh.vertices[self.compass.vertex_start..self.compass.vertex_start + 4] .copy_from_slice(&compass); Ok(()) } fn draw_contacts( &mut self, renderer: &mut VulkanSmokeRenderer, mesh: &mut VulkanStaticMesh, viewport: [f32; 2], center: [f32; 2], size: f32, player: Option<(&Engine, StartingCockpit, Option)>, ) -> Result<(), String> { let (buildings, units) = player.zip(self.extent).map_or_else( || (Vec::new(), Vec::new()), |((engine, cockpit, selected), extent)| { ( satellite_contacts::owned_buildings(engine, cockpit, extent).collect(), satellite_contacts::owned_units(engine, cockpit, extent, selected).collect(), ) }, ); if buildings != self.buildings || units != self.units { let mut layer = empty_layer(); for building in &buildings { paint_building(&self.icons, &mut layer, *building); } for unit in &units { paint_unit(&mut layer, unit); } renderer .update_material_texture(self.contact_layer.material_index, 0, &texture(layer)) .map_err(|error| error.to_string())?; self.buildings = buildings; self.units = units; } let vertices = screen_tl_quad_vertices_pixels( center, size * 0.5, size * 0.5, viewport, [1.0; 3], 1.0, UV, ); let start = self.contact_layer.vertex_start; mesh.vertices[start..start + 4].copy_from_slice(&vertices); Ok(()) } } fn empty_layer() -> RgbaImage { RgbaImage { width: MAP_SIZE as u32, height: MAP_SIZE as u32, rgba8: vec![0; MAP_SIZE * MAP_SIZE * 4], } } fn paint_unit(layer: &mut RgbaImage, unit: &satellite_contacts::UnitContact) { let mut from = unit.pixel; for &point in &unit.route { paint_line(layer, from, point, [100, 200, 100, 255]); for dy in -1..=1 { for dx in -1..=1 { paint_unit_pixel(layer, point, [dx, dy], [200, 200, 100, 255]); } } from = point; } let radius = if unit.cross { 2 } else { 1 }; for dy in -radius..=radius { for dx in -radius..=radius { if !unit.cross || dx == 0 || dy == 0 { paint_unit_pixel(layer, unit.pixel, [dx, dy], [128, 128, 255, 255]); } } } let Some(heading) = unit.heading else { return; }; let color = if heading.selected { [255; 4] } else { [0, 255, 0, 255] }; let border = radius + 2; for offset in -border..=border { for point in [ [offset, -border], [offset, border], [-border, offset], [border, offset], ] { paint_unit_pixel(layer, unit.pixel, point, color); } } let tip = std::array::from_fn(|axis| unit.pixel[axis].saturating_add(heading.tip_offset[axis])); paint_line(layer, unit.pixel, tip, color); } fn paint_line(layer: &mut RgbaImage, from: [i32; 2], to: [i32; 2], color: [u8; 4]) { let [dx, dy] = std::array::from_fn(|axis| i128::from(to[axis]) - i128::from(from[axis])); let major = usize::from(dy.abs() > dx.abs()); let minor = 1 - major; let span = [dx, dy][major].abs(); if span == 0 { paint_unit_pixel(layer, from, [0, 0], color); return; } // Intersect the major-axis interval with the map before rasterizing. Off-map // orders retain the direction of the original segment without a long loop. let start = from[major].min(to[major]).max(0); let end = from[major].max(to[major]).min(MAP_SIZE as i32 - 1); let rise = [dx, dy][minor]; for coordinate in start..=end { let step = (i128::from(coordinate) - i128::from(from[major])).abs(); let offset = ((step * rise.abs() + span / 2) / span) * rise.signum(); let mut point = from; point[major] = coordinate; point[minor] = (i128::from(from[minor]) + offset) as i32; paint_unit_pixel(layer, point, [0, 0], color); } } fn paint_unit_pixel(layer: &mut RgbaImage, center: [i32; 2], offset: [i32; 2], color: [u8; 4]) { let [x, y] = std::array::from_fn(|axis| center[axis].saturating_add(offset[axis])); if (0..MAP_SIZE as i32).contains(&x) && (0..MAP_SIZE as i32).contains(&y) { let dst = (y as usize * MAP_SIZE + x as usize) * 4; layer.rgba8[dst..dst + 4].copy_from_slice(&color); } } fn paint_building( atlas: &RgbaImage, layer: &mut RgbaImage, building: satellite_contacts::BuildingContact, ) { let [left, top] = building.pixel.map(|center| center.saturating_sub(10)); // Native 24×24 icon tile at y=24, displayed as a 20×20 square. for dy in 0..20 { let y = top.saturating_add(dy); if !(0..MAP_SIZE as i32).contains(&y) { continue; } for dx in 0..20 { let x = left.saturating_add(dx); if !(0..MAP_SIZE as i32).contains(&x) { continue; } let src_x = building.icon_x as usize + (dx as usize * 24 + 12) / 20; let src_y = 24 + (dy as usize * 24 + 12) / 20; let src = (src_y * atlas.width as usize + src_x) * 4; let dst = (y as usize * MAP_SIZE + x as usize) * 4; let alpha = u32::from(atlas.rgba8[src + 3]); if alpha == 0 { continue; } let retained = u32::from(layer.rgba8[dst + 3]) * (255 - alpha) / 255; let coverage = alpha + retained; for (axis, tint) in [128, 128, 255].into_iter().enumerate() { let source = u32::from(atlas.rgba8[src + axis]) * tint / 255; let under = u32::from(layer.rgba8[dst + axis]); layer.rgba8[dst + axis] = ((source * alpha + under * retained) / coverage) as u8; } layer.rgba8[dst + 3] = coverage as u8; } } } fn texture(image: RgbaImage) -> VulkanStaticTexture { VulkanStaticTexture { width: image.width, height: image.height, rgba8: image.rgba8, mip_levels: Vec::new(), } } fn add_sprite( preview: &mut StaticPreviewScene, image: RgbaImage, ) -> Result { 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 = texture(image); 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), ) }