diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-assets/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/satellite_map_chrome.rs | 242 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategic_roster.rs | 10 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/strategic_map_readout.rs | 148 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/strategic_roster.rs | 10 |
6 files changed, 406 insertions, 6 deletions
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs index 1f2cb172..72401c66 100644 --- a/crates/fparkan-assets/src/lib.rs +++ b/crates/fparkan-assets/src/lib.rs @@ -46,6 +46,7 @@ pub mod mission_loader; pub mod mission_pager; pub mod object_descriptions; pub mod satellite_map; +pub mod satellite_map_chrome; pub mod strategic_balance; pub mod strategic_roster; pub mod strategy_cursor; diff --git a/crates/fparkan-assets/src/satellite_map_chrome.rs b/crates/fparkan-assets/src/satellite_map_chrome.rs new file mode 100644 index 00000000..30b41e06 --- /dev/null +++ b/crates/fparkan-assets/src/satellite_map_chrome.rs @@ -0,0 +1,242 @@ +//! Compound header and contact footer from the strategic satellite map. + +use crate::{ + game_font::{load_layout_font, BitmapFont}, + mission_pager::copy_sprite, + strategic_roster::durability_fill, + ui_sprite::{atlas_region, load_ui_page, load_ui_sprites}, + RgbaImage, +}; +use fparkan_vfs::Vfs; + +/// Header and footer bounds in the original 640×480 strategic layout. +pub const HEADER: [u32; 4] = [374, 43, 640, 62]; +pub const FOOTER: [u32; 4] = [374, 330, 640, 349]; +const CLOSE: [u32; 4] = [605, 43, 640, 63]; +const WIDTH: u32 = 266; +const HEIGHT: u32 = 19; +const TEXT_LEFT: u32 = 72; +const TEXT_WIDTH: u32 = 183; + +/// Tests the original close rectangle, including the last logical row. +pub fn close_at(pointer: [f32; 2]) -> bool { + contains(CLOSE, pointer) +} + +/// Whether an event belongs to the header or footer instead of the map or scene. +pub fn covers_pointer(pointer: [f32; 2]) -> bool { + contains([HEADER[0], HEADER[1], HEADER[2], CLOSE[3]], pointer) || contains(FOOTER, pointer) +} + +fn contains([left, top, right, bottom]: [u32; 4], pointer: [f32; 2]) -> bool { + let [x, y] = pointer.map(f32::round_ties_even); + (left as f32..right as f32).contains(&x) && (top as f32..bottom as f32).contains(&y) +} + +/// Live text and art selectors for a single contact. +pub struct Contact<'a> { + pub label: &'a str, + pub class: u32, + pub size: u32, + pub chassis: u32, + pub clan: u16, + pub emblem_tint: [u8; 3], + pub durability_percent: Option<i32>, +} + +/// Loaded material for the two strips. +pub struct MapChrome { + font: BitmapFont, + cap: RgbaImage, + body: RgbaImage, + close: RgbaImage, + exit_icon: RgbaImage, + emitter: RgbaImage, + ray: RgbaImage, + end: RgbaImage, + unit_icons: RgbaImage, + extra_icons: RgbaImage, + contact_icons: RgbaImage, +} + +impl MapChrome { + /// Reads the compound controls and marker atlas from the installed game. + pub fn load(vfs: &dyn Vfs, contact_icons: RgbaImage) -> Result<Self, String> { + let [cap, body, close, emitter, ray, end] = load_ui_sprites( + vfs, + "ui/compaund.cfg", + [ + "ccres_ending_text", + "ccres_body_text", + "ccres_exit_button_normal", + "ccres_ray_emitter_off", + "ccres_ray_body", + "ccres_ray_ending", + ], + )?; + let [exit_icon] = load_ui_sprites(vfs, "ui/hq.cfg", ["exit_icon"])?; + Ok(Self { + font: load_layout_font(vfs)?, + cap, + body, + close, + exit_icon, + emitter, + ray, + end, + unit_icons: load_ui_page(vfs, "ui_menu")?, + extra_icons: load_ui_page(vfs, "ui_menu3")?, + contact_icons, + }) + } + + /// Renders the map title and its close button. + pub fn header(&self, title: &str) -> Result<RgbaImage, String> { + let mut image = empty_strip(); + let text = self.font.centered_line(title, 226, HEIGHT as u16)?; + stamp(&mut image, &self.cap, [0, 0, 5, HEIGHT], [255; 3]); + stamp(&mut image, &self.body, [5, 0, 231, HEIGHT], [255; 3]); + stamp(&mut image, &text, [5, 0, 231, HEIGHT], [128, 255, 128]); + stamp(&mut image, &self.close, [231, 0, WIDTH, HEIGHT], [255; 3]); + stamp(&mut image, &self.exit_icon, [246, 3, 259, 16], [255; 3]); + Ok(image) + } + + /// Renders the stationary footer and its optional hovered contact. + pub fn footer(&self, contact: Option<Contact<'_>>) -> Result<RgbaImage, String> { + let mut image = empty_strip(); + stamp(&mut image, &self.cap, [0, 0, 5, HEIGHT], [255; 3]); + stamp(&mut image, &self.body, [5, 0, 62, HEIGHT], [255; 3]); + copy_sprite( + &mut image, + &self.cap, + [61, 0, 67, HEIGHT], + [true, false], + [255; 3], + ); + stamp( + &mut image, + &self.emitter, + [62, 0, TEXT_LEFT + 1, HEIGHT], + [255; 3], + ); + let right = TEXT_LEFT + TEXT_WIDTH; + stamp( + &mut image, + &self.ray, + [TEXT_LEFT, 0, right + 1, HEIGHT], + [255; 3], + ); + stamp( + &mut image, + &self.end, + [right, 0, right + 6, HEIGHT], + [255; 3], + ); + let Some(contact) = contact else { + return Ok(image); + }; + if let Some(emblem) = atlas_region( + &self.contact_icons, + [u32::from(contact.clan) * 32, 224, 32, 32], + ) { + stamp(&mut image, &emblem, [7, 2, 22, 17], contact.emblem_tint); + } + self.paint_glyphs(&mut image, &contact); + if let Some((width, rgba)) = contact + .durability_percent + .and_then(|percent| durability_fill(percent, TEXT_WIDTH)) + { + stamp( + &mut image, + &RgbaImage { + width: 1, + height: 1, + rgba8: rgba.to_vec(), + }, + [TEXT_LEFT + 2, 3, TEXT_LEFT + width, HEIGHT - 3], + [255; 3], + ); + } + let label = self + .font + .centered_line(contact.label, TEXT_WIDTH as u16, HEIGHT as u16)?; + stamp(&mut image, &label, [TEXT_LEFT, 0, right, HEIGHT], [255; 3]); + Ok(image) + } + + fn paint_glyphs(&self, image: &mut RgbaImage, contact: &Contact<'_>) { + let building = contact.class & 0x8000_0000 != 0; + let tint = match (building, contact.size) { + (false, 1) => [255, 231, 255], + (false, 2) => [255, 128, 128], + (false, 3) => [128, 255, 128], + (false, 4) => [128, 128, 255], + (true, 2) => [255, 0, 0], + (true, 3) => [0, 255, 0], + (true, 4 | 5) => [0, 0, 255], + _ => [255; 3], + }; + let atlas = if contact.class == 0x8000_0200 { + &self.extra_icons + } else { + &self.unit_icons + }; + let class = match contact.class { + 0x0100_2000 => Some([81, 110]), + 0x0100_4000 => Some([65, 110]), + 0x0100_8000 | 0x0101_0000 | 0x0102_0000 => Some([49, 110]), + 0x8000_0002 => Some([113, 126]), + 0x8000_0004 => Some([129, 126]), + 0x8000_0008 => Some([81, 110]), + 0x8000_0010 => Some([129, 94]), + 0x8000_0040 => Some([97, 126]), + 0x8000_0200 => Some([148, 27]), + 0x8000_0400 => Some([49, 126]), + 0x8001_0000 | 0x8002_0000 | 0x8004_0000 => Some([65, 126]), + 0x8010_0000 | 0x8020_0000 => Some([81, 126]), + _ => None, + }; + if let Some(glyph) = class.and_then(|[x, y]| atlas_region(atlas, [x, y, 15, 15])) { + stamp(image, &glyph, [26, 2, 41, 17], tint); + } + if !building { + let x = match contact.chassis { + 1 => 97, + 2 => 49, + 3 => 65, + 4 => 81, + _ => return, + }; + if let Some(glyph) = atlas_region(&self.unit_icons, [x, 94, 15, 15]) { + stamp(image, &glyph, [45, 2, 60, 17], tint); + } + } + } +} + +fn empty_strip() -> RgbaImage { + RgbaImage { + width: WIDTH, + height: HEIGHT, + rgba8: vec![0; (WIDTH * HEIGHT * 4) as usize], + } +} + +fn stamp(image: &mut RgbaImage, sprite: &RgbaImage, bounds: [u32; 4], tint: [u8; 3]) { + copy_sprite(image, sprite, bounds, [false; 2], tint); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn close_button_is_separate_from_the_image_and_the_title() { + assert!(close_at([605.0, 43.0])); + assert!(close_at([639.0, 62.0])); + assert!(!close_at([604.0, 43.0])); + assert!(!covers_pointer([620.0, 63.0])); + assert!(covers_pointer([620.0, 335.0])); + } +} diff --git a/crates/fparkan-assets/src/strategic_roster.rs b/crates/fparkan-assets/src/strategic_roster.rs index 6c755300..b41e4102 100644 --- a/crates/fparkan-assets/src/strategic_roster.rs +++ b/crates/fparkan-assets/src/strategic_roster.rs @@ -203,7 +203,10 @@ impl RosterImages { ); } self.icons(&mut panel, row, [12, y + 2]); - if let Some((width, rgba)) = row.durability_percent.and_then(durability_fill) { + if let Some((width, rgba)) = row + .durability_percent + .and_then(|percent| durability_fill(percent, TEXT_WIDTH)) + { let fill = RgbaImage { width: 1, height: 1, @@ -262,8 +265,8 @@ fn stamp(panel: &mut RgbaImage, sprite: &RgbaImage, bounds: [u32; 4], tint: [u8; copy_sprite(panel, sprite, bounds, [false; 2], tint); } -fn durability_fill(percent: i32) -> Option<(u32, [u8; 4])> { - let width = (f64::from(percent.min(100)) * f64::from(TEXT_WIDTH) * f64::from(0.01_f32)) +pub(crate) fn durability_fill(percent: i32, span: u32) -> Option<(u32, [u8; 4])> { + let width = (f64::from(percent.min(100)) * f64::from(span) * f64::from(0.01_f32)) .round_ties_even() as u32; let rgba = match percent { ..=19 => [128, 0, 0, 128], @@ -289,6 +292,7 @@ mod tests { #[test] fn original_durability_colours_follow_the_rounded_health_percentage() { + let durability_fill = |percent| super::durability_fill(percent, TEXT_WIDTH); assert_eq!(durability_fill(0), None); assert_eq!(durability_fill(19), Some((47, [128, 0, 0, 128]))); assert_eq!(durability_fill(20), Some((50, [128, 128, 0, 128]))); diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 1db75dbc..ad5709b3 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -73,6 +73,7 @@ mod node_stage; mod projectile_capacity; pub mod satellite_contacts; pub mod strategic_balance; +pub mod strategic_map_readout; pub mod strategic_orders; pub mod strategic_roster; pub mod strategic_view; diff --git a/crates/fparkan-runtime/src/strategic_map_readout.rs b/crates/fparkan-runtime/src/strategic_map_readout.rs new file mode 100644 index 00000000..33d24b0f --- /dev/null +++ b/crates/fparkan-runtime/src/strategic_map_readout.rs @@ -0,0 +1,148 @@ +//! Live information for the hovered object on the strategic satellite map. + +use crate::{ + satellite_contacts, + strategic_roster::{order_label, unit_prefix}, + Engine, StartingCockpit, HERO_UNIT_CLASS, +}; +use fparkan_world::identity_metadata; + +/// Object data and original resource selectors for the map footer. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct ContactReadout { + pub class: u32, + pub size: u32, + pub chassis: u32, + pub clan: u16, + pub emblem_tint: [u8; 3], + pub name_label: u32, + pub callsign: Option<([char; 3], u32)>, + pub order_label: Option<u32>, + pub durability_percent: Option<i32>, +} + +/// Resolves the hovered map contact. Empty terrain and depleted objects have no readout. +pub fn contact_at( + engine: &Engine, + cockpit: StartingCockpit, + point: [f32; 2], +) -> Option<ContactReadout> { + let handle = satellite_contacts::pick_contact(engine, cockpit, point)?; + let mission = engine.loaded.as_ref()?; + let player = identity_metadata(&engine.world, cockpit.handle) + .ok()? + .owner_id?; + let identity = identity_metadata(&engine.world, handle).ok()?; + let owner = identity.owner_id?; + let original = identity.original_id?.0; + let draft = mission.object_drafts.get(original as usize)?; + let class = draft.unit_dat_header_flags?; + let building = class & 0x8000_0000 != 0; + let root = &draft.unit_components.first()?.resource_raw; + let size = crate::unit_size_class::classify(if building { 3 } else { 4 }, root); + let chassis = mission + .unit_chassis_flags + .get(&original) + .and_then(|flags| flags.chassis_type) + .unwrap_or(0); + let (name_label, callsign, order_label) = if building { + (building_label(class, size), None, None) + } else { + let (role, label) = match class { + 0x0100_2000 => ('T', 0x1838), + 0x0100_4000 => ('B', 0x1839), + 0x0100_8000 => ('W', 0x183a), + 0x0101_0000 => ('C', 0x183b), + HERO_UNIT_CLASS => ('?', 0x1856), + // Animals have map marks but no footer. + _ => return None, + }; + let callsign = (class != HERO_UNIT_CLASS) + .then(|| { + Some(( + unit_prefix(size, chassis, role), + mission.unit_serials.get(handle)?, + )) + }) + .flatten(); + let task = mission + .unit_patrol + .as_ref() + .map_or(0, |patrol| patrol.active_order_code(handle)); + (label, callsign, Some(order_label(task))) + }; + let observer = mission.mission.clans.get(usize::from(player.0))?; + let target = mission.mission.clans.get(usize::from(owner.0))?; + let relation = observer + .relations + .iter() + .find(|entry| entry.other_clan_name.raw == target.name.raw) + .map(|entry| entry.relation_value); + let emblem_tint = emblem_tint(owner == player, relation, building); + let durability_percent = mission + .control_device_tables + .iter() + .find(|table| table.handle == handle) + .and_then(crate::warbot_stats::durability_percent); + Some(ContactReadout { + class, + size, + chassis, + clan: owner.0, + emblem_tint, + name_label, + callsign, + order_label, + durability_percent, + }) +} + +fn emblem_tint(own: bool, relation: Option<i32>, building: bool) -> [u8; 3] { + match (own, relation, building) { + (false, Some(0), true) => [255; 3], + (false, Some(0), false) => [255, 0, 0], + (false, Some(1), _) => [128; 3], + _ => satellite_contacts::OWN_TINT, + } +} + +fn building_label(class: u32, size: u32) -> u32 { + // iron3d+0x33a90 uses the type and Behavior's size selector. + match (class, size) { + (0x8000_0002, _) => 0x1799, + (0x8000_0004, 2..=4) => 0x178d + size, + (0x8000_0008, 2..=4) => 0x1792 + size, + (0x8000_0010, 2..=4) => 0x17a1 + size, + (0x8000_0040, _) => 0x17a8, + (0x8000_0200, 2..=3) => 0x17b5 + size, + (0x8000_0400, 2..=5) => 0x179c + size, + (0x8000_1000, 2..=5) => 0x17b0 + size, + (0x8001_0000, _) => 0x17c6, + (0x8002_0000, _) => 0x17cc, + (0x8004_0000, _) => 0x17d2, + (0x8010_0000, _) => 0x17bd, + (0x8020_0000, _) => 0x17c3, + _ => 0x183d, + } +} + +#[cfg(test)] +mod tests { + use super::{building_label, emblem_tint}; + + #[test] + fn warehouse_size_changes_the_name_while_bunker_class_fixes_it() { + assert_eq!(building_label(0x8000_0008, 2), 0x1794); + assert_eq!(building_label(0x8000_0008, 3), 0x1795); + assert_eq!(building_label(0x8002_0000, 3), 0x17cc); + assert_eq!(building_label(0x8000_0008, 5), 0x183d); + } + + #[test] + fn the_own_clan_keeps_its_blue_emblem() { + assert_eq!(emblem_tint(true, Some(1), false), [128, 128, 255]); + assert_eq!(emblem_tint(false, Some(1), false), [128; 3]); + assert_eq!(emblem_tint(false, Some(0), true), [255; 3]); + assert_eq!(emblem_tint(false, Some(0), false), [255, 0, 0]); + } +} diff --git a/crates/fparkan-runtime/src/strategic_roster.rs b/crates/fparkan-runtime/src/strategic_roster.rs index 07735aff..1e3bd8bd 100644 --- a/crates/fparkan-runtime/src/strategic_roster.rs +++ b/crates/fparkan-runtime/src/strategic_roster.rs @@ -46,7 +46,7 @@ impl UnitSerials { self.slots.insert(handle.slot, (handle.generation, *serial)); } - fn get(&self, handle: ObjectHandle) -> Option<u32> { + pub(super) fn get(&self, handle: ObjectHandle) -> Option<u32> { let &(generation, serial) = self.slots.get(&handle.slot)?; (generation == handle.generation).then_some(serial) } @@ -117,7 +117,7 @@ pub fn warbot_rows(engine: &Engine, cockpit: StartingCockpit) -> Vec<WarbotRow> .map_or(0, |patrol| patrol.active_order_code(handle)); let row = WarbotRow { handle, - prefix: [letter(b"?TSML", size), letter(b"?FSWTAU", chassis), role], + prefix: unit_prefix(size, chassis, role), serial, class_label, order_label: order_label(order), @@ -137,7 +137,11 @@ fn letter(alphabet: &[u8], value: u32) -> char { char::from(alphabet.get(value as usize).copied().unwrap_or(b'?')) } -fn order_label(order: u32) -> u32 { +pub(super) fn unit_prefix(size: u32, chassis: u32, role: char) -> [char; 3] { + [letter(b"?TSML", size), letter(b"?FSWTAU", chassis), role] +} + +pub(super) fn order_label(order: u32) -> u32 { match order { 0 => 0x1824, 2 => 0x1828, |
