aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-01 21:44:16 +0300
committerValentin Popov <valentin@popov.link>2026-12-01 21:44:16 +0300
commit8d775c601f492e8f1e2ec1a28835f1ab3a37b076 (patch)
treea33e243b4ed36befe08be8940a6b813a58847d2d /crates/fparkan-runtime/src
parentb48fb2caccc7a72b85f07e6ab652081af57c7855 (diff)
downloadfparkan-8d775c601f492e8f1e2ec1a28835f1ab3a37b076.tar.xz
fparkan-8d775c601f492e8f1e2ec1a28835f1ab3a37b076.zip
feat(ui): show owned buildings on the satellite map
Use the original landscape projection and building sprites. Resolve ownership from live registered objects so a captured building appears at its existing location. Checked the original DLL and verified the factory and storage icons in the stock Field Camp window.
Diffstat (limited to 'crates/fparkan-runtime/src')
-rw-r--r--crates/fparkan-runtime/src/lib.rs1
-rw-r--r--crates/fparkan-runtime/src/satellite_contacts.rs153
2 files changed, 154 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 0f2b7e49..6ebe380c 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -70,6 +70,7 @@ mod control_resources;
mod mobile_budget;
mod node_amount;
mod node_stage;
+pub mod satellite_contacts;
pub use clan_clock::note_mission_pause_elapsed;
use control_profile::project_initial as project_control_owner_profile_fca0;
pub use control_profile::{ControlProfileReport, ControlProfileScalars};
diff --git a/crates/fparkan-runtime/src/satellite_contacts.rs b/crates/fparkan-runtime/src/satellite_contacts.rs
new file mode 100644
index 00000000..534ad6f6
--- /dev/null
+++ b/crates/fparkan-runtime/src/satellite_contacts.rs
@@ -0,0 +1,153 @@
+//! Live building contacts for the satellite map.
+
+use crate::{Engine, StartingCockpit};
+use fparkan_world::{
+ handle_by_original_id, identity_metadata, registration_sequence, transform_state,
+};
+
+/// The original icon crop and position within the 256-pixel satellite image.
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub struct BuildingContact {
+ pub pixel: [i32; 2],
+ pub icon_x: u32,
+}
+
+/// 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()?;
+ let extent = source
+ .positions
+ .iter()
+ .map(|position| position[0])
+ .reduce(f32::max)?;
+ (extent.is_finite() && extent > 0.0).then_some(extent)
+}
+
+/// Current contacts for the player's own clan, which needs no detection test.
+pub fn owned_buildings(
+ engine: &Engine,
+ cockpit: StartingCockpit,
+ extent: f32,
+) -> impl Iterator<Item = BuildingContact> + '_ {
+ 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 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()??;
+ if Some(clan) != owner {
+ return None;
+ }
+ let [world_x, world_y, _] = transform_state(&engine.world, handle)
+ .ok()?
+ .position
+ .map(f32::from_bits);
+ Some(BuildingContact {
+ pixel: project([world_x, world_y], extent)?,
+ icon_x: x,
+ })
+ })
+ })
+}
+
+fn icon_x(class: u32) -> Option<u32> {
+ // iron3d+0x9f460 selects the icon. The atlas positions come from +0x65370.
+ Some(match class {
+ 0x8000_0002 => 0,
+ 0x8000_0004 => 24,
+ 0x8000_0008 | 0x8000_0400 => 48,
+ 0x8000_0010 => 72,
+ 0x8001_0000 | 0x8002_0000 | 0x8004_0000 => 96,
+ 0x8000_0040 => 120,
+ 0x8010_0000 | 0x8020_0000 => 168,
+ 0x8000_0200 => 216,
+ _ => return None,
+ })
+}
+
+fn project([x, y]: [f32; 2], extent: f32) -> Option<[i32; 2]> {
+ if !x.is_finite() || !y.is_finite() || !extent.is_finite() || extent <= 0.0 {
+ return None;
+ }
+ // iron3d+0x349b0 rounds world coordinates before +0x745f0 scales them.
+ let scale = 256.0 / f64::from(extent);
+ let x = (f64::from(x.round_ties_even()) * scale).round_ties_even() as i32;
+ let y = (f64::from(y.round_ties_even()) * scale).round_ties_even() as i32;
+ Some([x, 256i32.saturating_sub(y)])
+}
+
+#[cfg(test)]
+mod tests {
+ use super::{icon_x, project};
+
+ #[test]
+ fn satellite_coordinates_match_the_original_inset_and_rounding() {
+ assert_eq!(project([300.31543, 556.2101], 1696.6862), Some([45, 172]));
+ assert_eq!(project([0.0, 0.0], 256.0), Some([0, 256]));
+ assert_eq!(project([256.0, 256.0], 256.0), Some([256, 0]));
+ assert_eq!(project([2.5, 3.5], 256.0), Some([2, 252]));
+ assert_eq!(icon_x(0x8000_0010), Some(72));
+ assert_eq!(icon_x(0x8000_0400), Some(48));
+ assert_eq!(icon_x(0x0100_8000), None);
+ }
+
+ #[test]
+ #[ignore = "requires licensed corpus"]
+ fn captured_factory_appears_at_its_existing_position() {
+ use super::{landscape_extent, owned_buildings, BuildingContact};
+ 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, 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.02/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();
+ assert_eq!(extent.to_bits(), 1696.6862f32.to_bits());
+ let factory = engine
+ .loaded
+ .as_ref()
+ .unwrap()
+ .object_drafts
+ .iter()
+ .find(|draft| draft.unit_dat_header_flags == Some(0x8000_0010))
+ .unwrap();
+ let handle = handle_by_original_id(&engine.world, factory.original_id.unwrap()).unwrap();
+ let [x, y, _] = transform_state(&engine.world, handle)
+ .unwrap()
+ .position
+ .map(f32::from_bits);
+ let marker = BuildingContact {
+ pixel: project([x, y], extent).unwrap(),
+ icon_x: 72,
+ };
+ assert!(!owned_buildings(&engine, cockpit, extent).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!(owned_buildings(&engine, cockpit, extent).any(|contact| contact == marker));
+ }
+}