diff options
Diffstat (limited to 'crates/fparkan-runtime')
| -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 |
4 files changed, 133 insertions, 6 deletions
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, |
