From 752d34af83acf9a818e22c621fb034913079acff Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 01:04:24 +0400 Subject: feat(ui): plot local detections on the cockpit radar Draw each current detection from the occupied unit sensor in camera-relative coordinates, with the original range scale, clan color and screen-pixel marks. Check stock Two Fortresses before and after a free-look turn and exercise the native rectangle and line callbacks. --- crates/fparkan-runtime/src/radar.rs | 107 +++++++++++++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) (limited to 'crates/fparkan-runtime/src/radar.rs') diff --git a/crates/fparkan-runtime/src/radar.rs b/crates/fparkan-runtime/src/radar.rs index a5c64206..bdd34811 100644 --- a/crates/fparkan-runtime/src/radar.rs +++ b/crates/fparkan-runtime/src/radar.rs @@ -1,9 +1,12 @@ //! Control radar returns a cached scan. The world rebuilds each clan's contacts every frame. +use crate::satellite_contacts::{clan_tint, OWN_TINT}; use crate::{ control_device_provider_value, Engine, MissionControlDeviceTableState, - MissionControlDeviceTableStatus, HERO_UNIT_CLASS, UNIT_DAT_CONTROL_CLASS_HIGH_BIT, + MissionControlDeviceTableStatus, StartingCockpit, HERO_UNIT_CLASS, + UNIT_DAT_CONTROL_CLASS_HIGH_BIT, }; +use fparkan_render::cockpit_radar::ContactMark; use fparkan_world::{ identity_metadata, registration_sequence, transform_state, ObjectHandle, OwnerId, }; @@ -27,6 +30,99 @@ impl Radar { } } +/// A target on the controlled object's own radar screen. +pub struct CockpitContact { + pub offset: [f32; 2], + pub mark: ContactMark, + pub tint: [u8; 3], + pub selected: bool, +} + +/// Range in world units and the contacts from this cockpit's scan. +pub fn cockpit_contacts( + engine: &Engine, + cockpit: StartingCockpit, + selected: Option, +) -> Option<(f32, impl Iterator + '_)> { + let mission = engine.loaded.as_ref()?; + let world = &engine.world; + let observer = identity_metadata(world, cockpit.handle).ok()?.owner_id?; + let origin = transform_state(world, cockpit.handle) + .ok()? + .position + .map(f32::from_bits); + let scan = mission.radar.scans.get(&cockpit.handle)?; + let local_table = mission + .control_device_tables + .iter() + .find(|table| table.handle == cockpit.handle)?; + let range = antenna(local_table)?.range; + let contacts = scan.targets.iter().filter_map(move |&handle| { + registration_sequence(world, handle).ok()??; + let table = mission + .control_device_tables + .iter() + .find(|table| table.handle == handle)?; + if table.node_provider_depletion_marked + || mission + .boarding + .is_some_and(|board| board.hero.handle == handle) + { + return None; + } + let id = table.original_id?.0; + let class = mission + .object_drafts + .get(id as usize)? + .unit_dat_header_flags?; + let clan = identity_metadata(world, handle).ok()?.owner_id?; + let mark = if class & UNIT_DAT_CONTROL_CLASS_HIGH_BIT != 0 { + ContactMark::Building + } else if mission + .unit_chassis_flags + .get(&id) + .is_some_and(|flags| flags.chassis_type == Some(1)) + { + ContactMark::Cross + } else { + ContactMark::Unit + }; + let tint = if clan == observer { + OWN_TINT + } else { + let observer = mission.mission.clans.get(usize::from(observer.0))?; + let target = mission.mission.clans.get(usize::from(clan.0))?; + let relation = target + .relations + .iter() + .find(|entry| entry.other_clan_name.raw == observer.name.raw) + .map(|entry| entry.relation_value); + if mark != ContactMark::Building + && target.mode != 0 + && relation == Some(0) + && mission + .unit_patrol + .as_ref() + .is_some_and(|patrol| patrol.is_shut_down(handle)) + { + return None; + } + clan_tint(target.mode, relation) + }; + let [x, y, _] = transform_state(world, handle) + .ok()? + .position + .map(f32::from_bits); + Some(CockpitContact { + offset: [x - origin[0], y - origin[1]], + mark, + tint, + selected: selected == Some(handle), + }) + }); + Some((range, contacts)) +} + struct Actor<'a> { table: &'a MissionControlDeviceTableState, owner: OwnerId, @@ -270,8 +366,15 @@ mod tests { .radar .contains(owner, target) }; + let displayed = |engine: &crate::Engine| { + let (range, mut contacts) = + super::cockpit_contacts(engine, hero, Some(target)).unwrap(); + assert_eq!(range, 300.0); + contacts.any(|mark| mark.selected && mark.tint == [160; 3]) + }; frame(&mut engine, 0).unwrap(); assert!(!seen(&engine)); + assert!(!displayed(&engine)); let mut near = original; near.position = transform_state(&engine.world, target).unwrap().position; @@ -281,6 +384,7 @@ mod tests { assert!(!seen(&engine)); frame(&mut engine, period + 1).unwrap(); assert!(seen(&engine)); + assert!(displayed(&engine)); assert!(visible_units(&engine, hero, extent, None).any(|mark| mark.tint == [160; 3])); set_transform(&mut engine.world, hero.handle, original).unwrap(); @@ -288,5 +392,6 @@ mod tests { assert!(seen(&engine)); frame(&mut engine, period * 2 + 2).unwrap(); assert!(!seen(&engine)); + assert!(!displayed(&engine)); } } -- cgit v1.2.3