diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-01 21:54:54 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-01 21:54:54 +0300 |
| commit | ff0a199a9162f08e60bcd3bd645d8c2c9199776f (patch) | |
| tree | 0aa02203b4ba6f7dd97b21e3d75f14eb935ac0fe /crates | |
| parent | 8d775c601f492e8f1e2ec1a28835f1ab3a37b076 (diff) | |
| download | fparkan-ff0a199a9162f08e60bcd3bd645d8c2c9199776f.tar.xz fparkan-ff0a199a9162f08e60bcd3bd645d8c2c9199776f.zip | |
feat(ui): show owned units on the satellite map
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-runtime/src/satellite_contacts.rs | 121 |
1 files changed, 114 insertions, 7 deletions
diff --git a/crates/fparkan-runtime/src/satellite_contacts.rs b/crates/fparkan-runtime/src/satellite_contacts.rs index 534ad6f6..a7c4cb7b 100644 --- a/crates/fparkan-runtime/src/satellite_contacts.rs +++ b/crates/fparkan-runtime/src/satellite_contacts.rs @@ -1,6 +1,6 @@ -//! Live building contacts for the satellite map. +//! Live contacts for the satellite map. -use crate::{Engine, StartingCockpit}; +use crate::{Engine, MissionObjectDraft, StartingCockpit}; use fparkan_world::{ handle_by_original_id, identity_metadata, registration_sequence, transform_state, }; @@ -12,6 +12,13 @@ pub struct BuildingContact { pub icon_x: u32, } +/// The original mark for a moving unit within the satellite image. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct UnitContact { + pub pixel: [i32; 2], + pub cross: bool, +} + /// Width of the original landscape square used by the map projection. pub fn landscape_extent(engine: &Engine) -> Option<f32> { let source = engine.loaded.as_ref()?.terrain.source_mesh()?; @@ -29,12 +36,43 @@ pub fn owned_buildings( cockpit: StartingCockpit, extent: f32, ) -> impl Iterator<Item = BuildingContact> + '_ { + owned_contacts(engine, cockpit, extent, |_, class| icon_x(class)) + .map(|(pixel, icon_x)| BuildingContact { pixel, icon_x }) +} + +/// Current marks for the player's vehicles, including newly registered factory output. +pub fn owned_units( + engine: &Engine, + cockpit: StartingCockpit, + extent: f32, +) -> impl Iterator<Item = UnitContact> + '_ { + owned_contacts(engine, cockpit, extent, move |draft, class| { + if class & crate::autodemo::MOBILE_UNIT == 0 || class == crate::HERO_UNIT_CLASS { + return None; + } + // iron3d+0x77f33 reads Behavior property 0x207 (ChassisType). + let flags = engine + .loaded + .as_ref()? + .unit_chassis_flags + .get(&draft.original_id?.0)?; + Some(flags.chassis_type == Some(1)) + }) + .map(|(pixel, cross)| UnitContact { pixel, cross }) +} + +fn owned_contacts<'a, T>( + engine: &'a Engine, + cockpit: StartingCockpit, + extent: f32, + marker: impl Fn(&MissionObjectDraft, u32) -> Option<T> + Copy + 'a, +) -> impl Iterator<Item = ([i32; 2], T)> + 'a { let owner = identity_metadata(&engine.world, cockpit.handle) .ok() .and_then(|identity| identity.owner_id); engine.loaded.iter().flat_map(move |mission| { mission.object_drafts.iter().filter_map(move |draft| { - let x = icon_x(draft.unit_dat_header_flags?)?; + let marker = marker(draft, draft.unit_dat_header_flags?)?; let handle = handle_by_original_id(&engine.world, draft.original_id?)?; let clan = identity_metadata(&engine.world, handle).ok()?.owner_id?; registration_sequence(&engine.world, handle).ok()??; @@ -45,10 +83,7 @@ pub fn owned_buildings( .ok()? .position .map(f32::from_bits); - Some(BuildingContact { - pixel: project([world_x, world_y], extent)?, - icon_x: x, - }) + Some((project([world_x, world_y], extent)?, marker)) }) }) } @@ -96,6 +131,78 @@ mod tests { #[test] #[ignore = "requires licensed corpus"] + fn captured_warbot_mark_uses_its_live_owner_and_position() { + use super::{landscape_extent, owned_units, UnitContact}; + use crate::{ + create, load_gameplay_mission, starting_cockpit, EngineServices, MissionMode, + MissionRequest, MissionStartupInput, + }; + use fparkan_vfs::DirectoryVfs; + use fparkan_world::{ + handle_by_original_id, identity_metadata, set_owner, set_transform, transform_state, + }; + use std::sync::Arc; + + 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/CAMPAIGN/CAMPAIGN.00/Mission.01/data.tma".into(), + mode: MissionMode::SingleMission, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + let cockpit = starting_cockpit(&engine).unwrap(); + let extent = landscape_extent(&engine).unwrap(); + let id = engine + .loaded + .as_ref() + .unwrap() + .object_drafts + .iter() + .find(|draft| draft.unit_dat_header_flags == Some(crate::boarding::WARBOT_CLASS)) + .unwrap() + .original_id + .unwrap(); + let handle = handle_by_original_id(&engine.world, id).unwrap(); + let owner = identity_metadata(&engine.world, cockpit.handle) + .unwrap() + .owner_id; + assert_ne!( + identity_metadata(&engine.world, handle).unwrap().owner_id, + owner + ); + let mut pose = transform_state(&engine.world, handle).unwrap(); + let [x, y, _] = pose.position.map(f32::from_bits); + let start = project([x, y], extent).unwrap(); + assert!(!owned_units(&engine, cockpit, extent).any(|contact| contact.pixel == start)); + set_owner(&mut engine.world, handle, owner).unwrap(); + assert!(owned_units(&engine, cockpit, extent).any(|contact| contact + == UnitContact { + pixel: start, + cross: false + })); + + pose.position[0] = (x + extent / 8.0).to_bits(); + set_transform(&mut engine.world, handle, pose).unwrap(); + let moved = project([x + extent / 8.0, y], extent).unwrap(); + assert_eq!(moved, [start[0] + 32, start[1]]); + let units: Vec<_> = owned_units(&engine, cockpit, extent).collect(); + assert!(units.contains(&UnitContact { + pixel: moved, + cross: false + })); + assert!(!units.iter().any(|contact| contact.pixel == start)); + } + + #[test] + #[ignore = "requires licensed corpus"] fn captured_factory_appears_at_its_existing_position() { use super::{landscape_extent, owned_buildings, BuildingContact}; use crate::{ |
