diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-01 21:20:20 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-01 21:20:20 +0300 |
| commit | 6954eb74b535341bed234091e67f62ed13266c25 (patch) | |
| tree | e44b9869785dd8c11ad6772eda0b3ab12ade85b4 /crates | |
| parent | 501779c19f5a86f0035683840f176d371abafc27 (diff) | |
| download | fparkan-6954eb74b535341bed234091e67f62ed13266c25.tar.xz fparkan-6954eb74b535341bed234091e67f62ed13266c25.zip | |
feat(ui): open the authored mission satellite map
Read the minimap archive binding from mission.cfg and resolve the compass through the original UI atlas key and crop. M toggles the overlay and Ctrl+M retains the current mine command. Checked show/hide in the Constructor window and the original DLL.
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-assets/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/satellite_map.rs | 187 |
2 files changed, 188 insertions, 0 deletions
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs index 53f58568..db7484cf 100644 --- a/crates/fparkan-assets/src/lib.rs +++ b/crates/fparkan-assets/src/lib.rs @@ -35,6 +35,7 @@ mod chassis_profile; mod control_drive; mod control_weapon_sound; mod explosion_profile; +pub mod satellite_map; mod turret_elevation; pub use behavior_profile_values::{ decode_behavior_go_speed_percent, decode_behavior_work_rates, BehaviorWorkRates, diff --git a/crates/fparkan-assets/src/satellite_map.rs b/crates/fparkan-assets/src/satellite_map.rs new file mode 100644 index 00000000..49f8894e --- /dev/null +++ b/crates/fparkan-assets/src/satellite_map.rs @@ -0,0 +1,187 @@ +//! Resources used by the satellite map overlay. + +use fparkan_nres::{decode, EntryId, ReadProfile}; +use fparkan_path::{normalize_relative, PathPolicy}; +use fparkan_resource::{resource_name, CachedResourceRepository, ResourceRepository}; +use fparkan_texm::{decode_mip_rgba8, decode_texm, RgbaImage}; +use fparkan_vfs::Vfs; +use std::collections::BTreeMap; +use std::sync::Arc; + +/// The mission map and compass cut from the original interface texture atlas. +pub struct SatelliteMapAssets { + pub background: RgbaImage, + pub compass: RgbaImage, +} + +/// Resolves `minimap` through the mission CFG and the compass through the game UI CFG. +pub fn load_satellite_map( + vfs: Arc<dyn Vfs>, + mission: &str, +) -> Result<Option<SatelliteMapAssets>, String> { + let cfg = mission.rsplit_once('/').map_or_else( + || "mission.cfg".to_owned(), + |(dir, _)| format!("{dir}/mission.cfg"), + ); + let bytes = read(vfs.as_ref(), &cfg)?; + let cfg = String::from_utf8_lossy(&bytes); + let Some(minimap) = object(&cfg, "minimap") else { + return Ok(None); + }; + let archive = normalize_relative( + field(&minimap, "LIBRARY")?.as_bytes(), + PathPolicy::StrictLegacy, + ) + .map_err(|error| error.to_string())?; + let repo = CachedResourceRepository::new(vfs.clone()); + let archive_id = repo + .open_archive(&archive) + .map_err(|error| error.to_string())?; + let file = field(&minimap, "MINIMAP")?; + let entry = repo + .find(archive_id, &resource_name(file)) + .map_err(|error| error.to_string())? + .ok_or_else(|| format!("missing mission map {file} in {}", archive.as_str()))?; + let payload = repo.read(entry).map_err(|error| error.to_string())?; + let background = texture(payload.as_slice())?; + + let hq = read(vfs.as_ref(), "ui/hq.cfg")?; + let hq = String::from_utf8_lossy(&hq); + let icon = object(&hq, "map_compass_icon").ok_or("missing map_compass_icon in ui/hq.cfg")?; + let game = read(vfs.as_ref(), "ui/game_resources.cfg")?; + let game = String::from_utf8_lossy(&game); + let textures = object(&game, "textures").ok_or("missing textures in ui/game_resources.cfg")?; + let atlas = field(&textures, "LIBRARY")?; + let page = field(&icon, "TEXTURE")?.to_ascii_uppercase(); + let pages = decode(read(vfs.as_ref(), atlas)?, ReadProfile::Strict) + .map_err(|error| error.to_string())?; + let page = pages + .payload(EntryId(number(&textures, &page)?)) + .map_err(|error| error.to_string())?; + let compass = crop( + &texture(page)?, + [number(&icon, "OFFSET_X")?, number(&icon, "OFFSET_Y")?], + [number(&icon, "WIDTH")?, number(&icon, "HEIGHT")?], + )?; + Ok(Some(SatelliteMapAssets { + background, + compass, + })) +} + +fn read(vfs: &dyn Vfs, path: &str) -> Result<Arc<[u8]>, String> { + let path = normalize_relative( + path.replace("\\\\", "\\").as_bytes(), + PathPolicy::StrictLegacy, + ) + .map_err(|error| error.to_string())?; + vfs.read(&path).map_err(|error| error.to_string()) +} + +fn object<'a>(text: &'a str, name: &str) -> Option<BTreeMap<String, &'a str>> { + let mut lines = text + .lines() + .map(|line| line.split('#').next().unwrap_or_default().trim()); + for line in lines.by_ref() { + let mut words = line.split_whitespace(); + if words + .next() + .is_some_and(|word| word.eq_ignore_ascii_case("object")) + && words + .next() + .is_some_and(|word| word.eq_ignore_ascii_case(name)) + { + return Some( + lines + .take_while(|line| !line.eq_ignore_ascii_case("end")) + .filter_map(|line| line.split_once('=')) + .map(|(key, value)| (key.trim().to_ascii_uppercase(), value.trim())) + .collect(), + ); + } + } + None +} + +fn field<'a>(fields: &BTreeMap<String, &'a str>, key: &str) -> Result<&'a str, String> { + let raw = fields.get(key).ok_or_else(|| format!("missing {key}"))?; + if let Some(quoted) = raw.strip_prefix('"') { + return quoted + .split_once('"') + .map(|(value, _)| value) + .ok_or_else(|| format!("invalid quoted value for {key}")); + } + Ok(raw) +} + +fn number(fields: &BTreeMap<String, &str>, key: &str) -> Result<u32, String> { + field(fields, key)? + .parse() + .map_err(|_| format!("invalid number for {key}")) +} + +fn texture(bytes: &[u8]) -> Result<RgbaImage, String> { + let document = decode_texm(Arc::from(bytes)).map_err(|error| error.to_string())?; + decode_mip_rgba8(&document, 0).map_err(|error| error.to_string()) +} + +fn crop( + image: &RgbaImage, + [x, y]: [u32; 2], + [width, height]: [u32; 2], +) -> Result<RgbaImage, String> { + let right = x + .checked_add(width) + .filter(|&right| right <= image.width && width != 0); + let bottom = y + .checked_add(height) + .filter(|&bottom| bottom <= image.height && height != 0); + let (Some(right), Some(bottom)) = (right, bottom) else { + return Err("map compass rectangle is outside the texture atlas".into()); + }; + let stride = image.width as usize * 4; + let mut rgba8 = Vec::new(); + for row in y as usize..bottom as usize { + rgba8.extend_from_slice( + &image.rgba8[row * stride + x as usize * 4..row * stride + right as usize * 4], + ); + } + Ok(RgbaImage { + width, + height, + rgba8, + }) +} + +#[cfg(test)] +mod tests { + use super::load_satellite_map; + use fparkan_vfs::DirectoryVfs; + use std::sync::Arc; + + #[test] + #[ignore = "requires licensed corpus"] + fn loads_distinct_mission_textures_and_the_shared_compass() { + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); + let vfs = Arc::new(DirectoryVfs::new(root)); + let load = |mission| { + load_satellite_map(vfs.clone(), &format!("MISSIONS/{mission}/data.tma")) + .unwrap() + .expect("authored map") + }; + let range = load("CAMPAIGN/CAMPAIGN.00/Mission.01"); + let fortresses = load("Single.01"); + assert_eq!( + [range.background.width, range.background.height], + [256, 256] + ); + assert_eq!([range.compass.width, range.compass.height], [21, 42]); + assert_ne!(range.background.rgba8, fortresses.background.rgba8); + assert_eq!(range.compass, fortresses.compass); + assert!(range + .compass + .rgba8 + .chunks_exact(4) + .any(|pixel| pixel[3] != 0)); + } +} |
