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 | 40 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategic_buildings.rs | 176 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategic_factory.rs | 4 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategy_toolbar.rs | 3 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/satellite_contacts.rs | 24 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/strategic_buildings.rs | 112 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/strategic_toolbar.rs | 2 |
9 files changed, 342 insertions, 21 deletions
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs index 5d4973ef..295ed772 100644 --- a/crates/fparkan-assets/src/lib.rs +++ b/crates/fparkan-assets/src/lib.rs @@ -48,6 +48,7 @@ pub mod object_descriptions; pub mod satellite_map; pub mod satellite_map_chrome; pub mod strategic_balance; +pub mod strategic_buildings; pub mod strategic_factory; 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 index 30b41e06..5ae81bea 100644 --- a/crates/fparkan-assets/src/satellite_map_chrome.rs +++ b/crates/fparkan-assets/src/satellite_map_chrome.rs @@ -172,9 +172,7 @@ impl MapChrome { (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], + (true, size) => building_glyph_tint(size), _ => [255; 3], }; let atlas = if contact.class == 0x8000_0200 { @@ -186,16 +184,7 @@ impl MapChrome { 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, + class => building_glyph(class), }; if let Some(glyph) = class.and_then(|[x, y]| atlas_region(atlas, [x, y, 15, 15])) { stamp(image, &glyph, [26, 2, 41, 17], tint); @@ -215,6 +204,31 @@ impl MapChrome { } } +// The list and map readout both call iron3d+0x346a0 for building glyphs. +pub(crate) fn building_glyph(class: u32) -> Option<[u32; 2]> { + Some(match class { + 0x8000_0002 => [113, 126], + 0x8000_0004 => [129, 126], + 0x8000_0008 => [81, 110], + 0x8000_0010 => [129, 94], + 0x8000_0040 => [97, 126], + 0x8000_0200 => [148, 27], + 0x8000_0400 => [49, 126], + 0x8001_0000 | 0x8002_0000 | 0x8004_0000 => [65, 126], + 0x8010_0000 | 0x8020_0000 => [81, 126], + _ => return None, + }) +} + +pub(crate) fn building_glyph_tint(size: u32) -> [u8; 3] { + match size { + 2 => [255, 0, 0], + 3 => [0, 255, 0], + 4 | 5 => [0, 0, 255], + _ => [255; 3], + } +} + fn empty_strip() -> RgbaImage { RgbaImage { width: WIDTH, diff --git a/crates/fparkan-assets/src/strategic_buildings.rs b/crates/fparkan-assets/src/strategic_buildings.rs new file mode 100644 index 00000000..53deabdf --- /dev/null +++ b/crates/fparkan-assets/src/strategic_buildings.rs @@ -0,0 +1,176 @@ +//! Compound controls of the strategic «Другие здания» section. + +use crate::{ + game_font::{load_layout_font, BitmapFont}, + satellite_map_chrome::{building_glyph, building_glyph_tint}, + strategic_factory::{stamp, text}, + strategic_roster::durability_fill, + ui_sprite::{atlas_region, load_ui_page, load_ui_sprites}, + RgbaImage, +}; +use fparkan_vfs::Vfs; + +pub use crate::strategic_factory::{EXTENT, ORIGIN}; +const ROW_PITCH: u32 = 20; +const ROW_HEIGHT: u32 = 19; +const TEXT_LEFT: u32 = 44; +const TEXT_RIGHT: u32 = 313; +pub const TITLE: u32 = 0x64a; +pub const VISIBLE_ROWS: usize = (EXTENT[1] / ROW_PITCH - 1) as usize; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Hit { + Close, + Row(usize), +} + +pub fn hit_test([x, y]: [f32; 2], rows: usize) -> Option<Hit> { + let x = x.round_ties_even() - ORIGIN[0] as f32; + let y = y.round_ties_even(); + if (283.0..318.0).contains(&x) && (0.0..19.0).contains(&y) { + return Some(Hit::Close); + } + if !(0.0..(EXTENT[0] - 1) as f32).contains(&x) || !(20.0..480.0).contains(&y) { + return None; + } + let y = y as u32 - ROW_PITCH; + let row = (y / ROW_PITCH) as usize; + (row < rows.min(VISIBLE_ROWS) && y % ROW_PITCH < ROW_HEIGHT).then_some(Hit::Row(row)) +} + +pub struct Row<'a> { + pub label: &'a str, + pub class: u32, + pub size: u32, + pub selected: bool, + pub durability_percent: Option<i32>, +} + +pub struct Images { + header: RgbaImage, + font: BitmapFont, + cap: [RgbaImage; 2], + frame: RgbaImage, + separator: RgbaImage, + emitter: [RgbaImage; 2], + ray: RgbaImage, + end: RgbaImage, + atlas: RgbaImage, +} + +impl Images { + pub fn load(vfs: &dyn Vfs, title: &str) -> Result<Self, String> { + let [title_cap, frame, close, cap_off, cap_on, separator, emitter_off, emitter_on, ray, end] = + load_ui_sprites( + vfs, + "ui/compaund.cfg", + [ + "ccres_ending_text", + "ccres_body_text", + "ccres_exit_button_pressed", + "ccres_lamp_stub_ending_off", + "ccres_lamp_stub_ending_normal", + "ccres_separator_left_text", + "ccres_ray_emitter_off", + "ccres_ray_emitter_normal", + "ccres_ray_body", + "ccres_ray_ending", + ], + )?; + let [exit] = load_ui_sprites(vfs, "ui/hq.cfg", ["exit_icon"])?; + let font = load_layout_font(vfs)?; + let [width, height] = EXTENT; + let mut header = RgbaImage { + width, + height, + rgba8: vec![0; (width * height * 4) as usize], + }; + for (sprite, rect) in [ + (&title_cap, [0, 0, 6, 19]), + (&frame, [5, 0, 284, 19]), + (&close, [283, 0, 318, 19]), + (&exit, [298, 3, 311, 16]), + ] { + stamp(&mut header, sprite, rect, [255; 3]); + } + text(&font, &mut header, title, [5, 0, 283, 19], [255; 3])?; + Ok(Self { + header, + font, + cap: [cap_off, cap_on], + frame, + separator, + emitter: [emitter_off, emitter_on], + ray, + end, + atlas: load_ui_page(vfs, "ui_menu")?, + }) + } + + pub fn paint<'a>(&self, rows: impl Iterator<Item = Row<'a>>) -> Result<RgbaImage, String> { + let mut image = self.header.clone(); + for (index, row) in rows.take(VISIBLE_ROWS).enumerate() { + let y = (index as u32 + 1) * ROW_PITCH; + let bottom = y + ROW_HEIGHT; + let selected = usize::from(row.selected); + for (sprite, left, right) in [ + (&self.cap[selected], 0, 11), + (&self.frame, 10, 30), + (&self.separator, 29, 35), + (&self.emitter[selected], 34, 45), + (&self.ray, TEXT_LEFT, TEXT_RIGHT + 1), + (&self.end, TEXT_RIGHT, EXTENT[0]), + ] { + stamp(&mut image, sprite, [left, y, right, bottom], [255; 3]); + } + if let Some([x, atlas_y]) = building_glyph(row.class) { + let glyph = atlas_region(&self.atlas, [x, atlas_y, 15, 15]) + .ok_or("building glyph outside ui_menu")?; + stamp( + &mut image, + &glyph, + [12, y + 2, 27, y + 17], + building_glyph_tint(row.size), + ); + } + if let Some((width, rgba)) = row + .durability_percent + .and_then(|percent| durability_fill(percent, TEXT_RIGHT - TEXT_LEFT)) + { + let fill = RgbaImage { + width: 1, + height: 1, + rgba8: rgba.to_vec(), + }; + stamp( + &mut image, + &fill, + [TEXT_LEFT + 2, y + 3, TEXT_LEFT + width, bottom - 3], + [255; 3], + ); + } + text( + &self.font, + &mut image, + row.label, + [TEXT_LEFT, y, TEXT_RIGHT, bottom], + if row.selected { [255; 3] } else { [128; 3] }, + )?; + } + Ok(image) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn building_list_hits_labels_icons_and_close_with_native_row_gaps() { + assert_eq!(hit_test([350.0, 9.0], 4), Some(Hit::Close)); + assert_eq!(hit_test([68.0, 39.0], 4), None); + assert_eq!(hit_test([68.0, 40.0], 4), Some(Hit::Row(1))); + assert_eq!(hit_test([340.0, 49.0], 4), Some(Hit::Row(1))); + assert_eq!(hit_test([340.0, 49.0], 1), None); + } +} diff --git a/crates/fparkan-assets/src/strategic_factory.rs b/crates/fparkan-assets/src/strategic_factory.rs index 1cf6d8a4..cc4eb491 100644 --- a/crates/fparkan-assets/src/strategic_factory.rs +++ b/crates/fparkan-assets/src/strategic_factory.rs @@ -373,11 +373,11 @@ impl FactoryImages { } } -fn stamp(image: &mut RgbaImage, sprite: &RgbaImage, rect: [u32; 4], tint: [u8; 3]) { +pub(crate) fn stamp(image: &mut RgbaImage, sprite: &RgbaImage, rect: [u32; 4], tint: [u8; 3]) { copy_sprite(image, sprite, rect, [false; 2], tint); } -fn text( +pub(crate) fn text( font: &BitmapFont, image: &mut RgbaImage, label: &str, diff --git a/crates/fparkan-assets/src/strategy_toolbar.rs b/crates/fparkan-assets/src/strategy_toolbar.rs index a50520c2..29737335 100644 --- a/crates/fparkan-assets/src/strategy_toolbar.rs +++ b/crates/fparkan-assets/src/strategy_toolbar.rs @@ -31,6 +31,8 @@ pub enum ToolbarAction { Warbots, /// Toggle the clan's factory panel. Factories, + /// Toggle the clan's other buildings. + OtherBuildings, /// Toggle the satellite map. Map, } @@ -42,6 +44,7 @@ pub fn command_button_at([x, y]: [f32; 2]) -> Option<ToolbarAction> { 0 => Some(ToolbarAction::Hero), 1 => Some(ToolbarAction::Warbots), 5 => Some(ToolbarAction::Factories), + 8 => Some(ToolbarAction::OtherBuildings), 10 => Some(ToolbarAction::Map), _ => None, } diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 83aac64c..ad030b00 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_buildings; pub mod strategic_map_readout; pub mod strategic_orders; pub mod strategic_roster; diff --git a/crates/fparkan-runtime/src/satellite_contacts.rs b/crates/fparkan-runtime/src/satellite_contacts.rs index 31181bc1..13c918a4 100644 --- a/crates/fparkan-runtime/src/satellite_contacts.rs +++ b/crates/fparkan-runtime/src/satellite_contacts.rs @@ -73,12 +73,26 @@ pub fn visible_buildings( engine: &Engine, cockpit: StartingCockpit, extent: f32, + selected: Option<ObjectHandle>, ) -> impl Iterator<Item = BuildingContact> + '_ { + let selected = selected.filter(|&target| { + identity_metadata(&engine.world, target) + .ok() + .and_then(|identity| identity.owner_id) + .is_some_and(|owner| { + identity_metadata(&engine.world, cockpit.handle) + .is_ok_and(|identity| identity.owner_id == Some(owner)) + }) + }); visible_contacts(engine, cockpit, extent, |_, class| icon_x(class)).map( - |(pixel, _, _, tint, icon_x)| BuildingContact { + move |(pixel, handle, _, tint, icon_x)| BuildingContact { pixel, icon_x, - tint, + tint: if Some(handle) == selected { + [255; 3] + } else { + tint + }, }, ) } @@ -460,14 +474,14 @@ mod tests { icon_x: 72, tint: super::OWN_TINT, }; - assert!(!visible_buildings(&engine, cockpit, extent).any(|contact| contact == marker)); + assert!(!visible_buildings(&engine, cockpit, extent, None).any(|contact| contact == marker)); let owner = identity_metadata(&engine.world, cockpit.handle) .unwrap() .owner_id; assert!(owner.is_some()); set_owner(&mut engine.world, handle, owner).unwrap(); - assert!(visible_buildings(&engine, cockpit, extent).any(|contact| contact == marker)); + assert!(visible_buildings(&engine, cockpit, extent, None).any(|contact| contact == marker)); let table = loaded_mission_control_device_tables(&engine) .unwrap() @@ -488,6 +502,6 @@ mod tests { assert!(registration_sequence(&engine.world, handle) .unwrap() .is_some()); - assert!(!visible_buildings(&engine, cockpit, extent).any(|contact| contact == marker)); + assert!(!visible_buildings(&engine, cockpit, extent, None).any(|contact| contact == marker)); } } diff --git a/crates/fparkan-runtime/src/strategic_buildings.rs b/crates/fparkan-runtime/src/strategic_buildings.rs new file mode 100644 index 00000000..91024136 --- /dev/null +++ b/crates/fparkan-runtime/src/strategic_buildings.rs @@ -0,0 +1,112 @@ +//! Live rows of the strategic «Другие здания» section. + +use crate::{strategic_map_readout::building_label, Engine, StartingCockpit}; +use fparkan_world::{identity_metadata, registration_sequence, ObjectHandle}; +use std::cmp::Reverse; + +pub(super) const OTHER_BUILDINGS: u32 = 0x8000_044e; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct BuildingRow { + pub handle: ObjectHandle, + pub class: u32, + pub size: u32, + pub label: u32, + pub durability_percent: Option<i32>, +} + +/// Buildings of the operator's clan, most recently registered first. +pub fn other_buildings(engine: &Engine, cockpit: StartingCockpit) -> Vec<BuildingRow> { + let Some((mission, owner)) = engine.loaded.as_ref().zip( + identity_metadata(&engine.world, cockpit.handle) + .ok() + .and_then(|identity| identity.owner_id), + ) else { + return Vec::new(); + }; + let mut rows: Vec<_> = mission + .control_device_tables + .iter() + .filter_map(|table| { + let handle = table.handle; + let draft = mission.object_drafts.get(table.original_id?.0 as usize)?; + let class = draft.unit_dat_header_flags?; + if class == 0 + || class & OTHER_BUILDINGS != class + || table.node_provider_depletion_marked + || identity_metadata(&engine.world, handle).ok()?.owner_id != Some(owner) + { + return None; + } + let sequence = registration_sequence(&engine.world, handle).ok()??; + let size = + crate::unit_size_class::classify(3, &draft.unit_components.first()?.resource_raw); + let row = BuildingRow { + handle, + class, + size, + label: building_label(class, size), + durability_percent: crate::warbot_stats::durability_percent(table), + }; + Some((sequence, row)) + }) + .collect(); + rows.sort_unstable_by_key(|(sequence, _)| Reverse(*sequence)); + rows.into_iter().map(|(_, row)| row).collect() +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + create, frame, load_gameplay_mission, satellite_contacts, starting_cockpit, EngineServices, + MissionMode, MissionRequest, MissionStartupInput, + }; + use fparkan_vfs::DirectoryVfs; + use std::sync::Arc; + + #[test] + #[ignore = "requires licensed mission resources"] + fn two_fortresses_lists_owned_buildings_and_whitens_only_the_selected_map_icon() { + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); + let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap(); + load_gameplay_mission( + &mut engine, + MissionRequest { + key: "MISSIONS/Single.01/data.tma".into(), + mode: MissionMode::SingleMission, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + frame(&mut engine, 1).unwrap(); + let cockpit = starting_cockpit(&engine).unwrap(); + let rows = other_buildings(&engine, cockpit); + assert_eq!( + rows.iter().map(|row| row.label).collect::<Vec<_>>(), + [0x179e, 0x178f, 0x1794, 0x1799] + ); + let extent = satellite_contacts::landscape_extent(&engine).unwrap(); + let before: Vec<_> = + satellite_contacts::visible_buildings(&engine, cockpit, extent, None).collect(); + let after: Vec<_> = + satellite_contacts::visible_buildings(&engine, cockpit, extent, Some(rows[1].handle)) + .collect(); + assert_eq!(before.len(), after.len()); + let changed: Vec<_> = before.iter().zip(&after).filter(|(a, b)| a != b).collect(); + assert_eq!(changed.len(), 1); + let (normal, selected) = changed[0]; + assert_eq!(normal.tint, satellite_contacts::OWN_TINT); + assert_eq!( + *selected, + satellite_contacts::BuildingContact { + tint: [255; 3], + ..*normal + } + ); + } +} diff --git a/crates/fparkan-runtime/src/strategic_toolbar.rs b/crates/fparkan-runtime/src/strategic_toolbar.rs index e35c19b0..de35f695 100644 --- a/crates/fparkan-runtime/src/strategic_toolbar.rs +++ b/crates/fparkan-runtime/src/strategic_toolbar.rs @@ -12,7 +12,7 @@ const SECTION_MASKS: [u32; 8] = [ 0x8000_0010, 0x8030_0000, 0x8007_0000, - 0x8000_044e, + crate::strategic_buildings::OTHER_BUILDINGS, ]; /// Availability for warbots, builders, transports, labs, factories, towers, |
