From 61caf325b14e219224c20080f397f72774375745 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 20:43:14 +0400 Subject: feat(ui): select targets through the cockpit radar --- crates/fparkan-runtime/src/radar.rs | 204 +++++++++++++++++++++++++++++++++--- 1 file changed, 192 insertions(+), 12 deletions(-) (limited to 'crates/fparkan-runtime/src') diff --git a/crates/fparkan-runtime/src/radar.rs b/crates/fparkan-runtime/src/radar.rs index 46eb8576..9bb0dfde 100644 --- a/crates/fparkan-runtime/src/radar.rs +++ b/crates/fparkan-runtime/src/radar.rs @@ -36,6 +36,179 @@ pub struct CockpitContact { pub mark: ContactMark, pub tint: [u8; 3], pub selected: bool, + handle: ObjectHandle, + distance_squared: f32, + affiliation: Affiliation, +} + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +enum Affiliation { + Enemy, + Friendly, + Other, +} + +/// The target-selection commands in `ui_other.man`. +#[derive(Clone, Copy)] +pub enum CockpitTargetCommand { + Next, + Enemy, + Friend, +} + +/// Select from the same live scan that supplies the contacts on the radar. +pub fn select_cockpit_target( + engine: &Engine, + cockpit: StartingCockpit, + selected: Option, + command: CockpitTargetCommand, +) -> Option { + let (_, contacts) = cockpit_contacts(engine, cockpit, selected)?; + select_contact(contacts, command) +} + +/// Keep a live target in range or choose one when the cockpit has no target. +pub fn refresh_cockpit_target( + engine: &Engine, + cockpit: StartingCockpit, + selected: Option, +) -> Option { + let (range, contacts) = cockpit_contacts(engine, cockpit, selected)?; + let mut contacts = contacts.peekable(); + contacts.peek()?; + let world = &engine.world; + let mission = engine.loaded.as_ref()?; + let active = selected.filter(|&target| { + if target == cockpit.handle + || registration_sequence(world, target) + .ok() + .flatten() + .is_none() + || mission + .boarding + .is_some_and(|boarding| boarding.hero.handle == target) + || !mission + .control_device_tables + .iter() + .any(|table| table.handle == target && !table.node_provider_depletion_marked) + { + return false; + } + let (Ok(observer), Ok(target)) = ( + transform_state(world, cockpit.handle), + transform_state(world, target), + ) else { + return false; + }; + let [x, y] = horizontal_offset(observer.position, target.position); + x * x + y * y <= range * range + }); + // iron3d+0x90e00 tries enemies first, then its own clan, then any contact. + active.or_else(|| { + contacts + .min_by(|a, b| { + a.affiliation + .cmp(&b.affiliation) + .then_with(|| a.distance_squared.total_cmp(&b.distance_squared)) + }) + .map(|contact| contact.handle) + }) +} + +fn select_contact( + contacts: impl Iterator, + command: CockpitTargetCommand, +) -> Option { + let affiliation = match command { + CockpitTargetCommand::Next => None, + CockpitTargetCommand::Enemy => Some(Affiliation::Enemy), + CockpitTargetCommand::Friend => Some(Affiliation::Friendly), + }; + let mut eligible = + contacts.filter(|contact| affiliation.is_none_or(|team| team == contact.affiliation)); + let first = eligible.next()?; + let mut wrap = first.selected; + let mut nearest = (first.handle, first.distance_squared); + for contact in eligible { + if wrap { + return Some(contact.handle); + } + wrap = contact.selected; + if contact.distance_squared < nearest.1 { + nearest = (contact.handle, contact.distance_squared); + } + } + // With a matching selection, the original searches forward and then wraps. + Some(if wrap || affiliation.is_none() { + first.handle + } else { + nearest.0 + }) +} + +#[cfg(test)] +#[test] +fn contact_selection_wraps_and_filters_by_affiliation() { + let first = ObjectHandle { + slot: 0, + generation: 1, + }; + let second = ObjectHandle { + slot: 1, + generation: 1, + }; + let third = ObjectHandle { + slot: 2, + generation: 1, + }; + let fourth = ObjectHandle { + slot: 3, + generation: 1, + }; + let select = |selected, command| { + let contacts = [ + (first, Affiliation::Enemy, 100.0), + (second, Affiliation::Other, 1.0), + (third, Affiliation::Enemy, 25.0), + (fourth, Affiliation::Friendly, 36.0), + ]; + select_contact( + contacts + .into_iter() + .map(|(handle, affiliation, distance_squared)| CockpitContact { + offset: [0.0; 2], + mark: ContactMark::Unit, + tint: [0; 3], + selected: selected == Some(handle), + handle, + distance_squared, + affiliation, + }), + command, + ) + }; + assert_eq!(select(None, CockpitTargetCommand::Next), Some(first)); + assert_eq!( + select(Some(first), CockpitTargetCommand::Next), + Some(second) + ); + assert_eq!( + select(Some(fourth), CockpitTargetCommand::Next), + Some(first) + ); + assert_eq!( + select(Some(first), CockpitTargetCommand::Enemy), + Some(third) + ); + assert_eq!(select(None, CockpitTargetCommand::Enemy), Some(third)); + assert_eq!( + select(Some(third), CockpitTargetCommand::Enemy), + Some(first) + ); + assert_eq!( + select(Some(first), CockpitTargetCommand::Friend), + Some(fourth) + ); } /// The range printed on the cockpit dial, even when the antenna is unpowered. @@ -84,10 +257,7 @@ pub fn cockpit_contacts( 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 origin = transform_state(world, cockpit.handle).ok()?.position; let scan = mission.radar.scans.get(&cockpit.handle)?; let local_table = mission .control_device_tables @@ -124,8 +294,8 @@ pub fn cockpit_contacts( } else { ContactMark::Unit }; - let tint = if clan == observer { - OWN_TINT + let (tint, affiliation) = if clan == observer { + (OWN_TINT, Affiliation::Friendly) } else { let observer = mission.mission.clans.get(usize::from(observer.0))?; let target = mission.mission.clans.get(usize::from(clan.0))?; @@ -144,22 +314,32 @@ pub fn cockpit_contacts( { return None; } - clan_tint(target.mode, relation) + let affiliation = if target.mode != 0 && relation == Some(0) { + Affiliation::Enemy + } else { + Affiliation::Other + }; + (clan_tint(target.mode, relation), affiliation) }; - let [x, y, _] = transform_state(world, handle) - .ok()? - .position - .map(f32::from_bits); + let position = transform_state(world, handle).ok()?.position; + let [x, y] = horizontal_offset(origin, position); Some(CockpitContact { - offset: [x - origin[0], y - origin[1]], + offset: [x, y], mark, tint, selected: selected == Some(handle), + handle, + distance_squared: x * x + y * y, + affiliation, }) }); Some((range, contacts)) } +fn horizontal_offset(origin: [u32; 3], target: [u32; 3]) -> [f32; 2] { + std::array::from_fn(|axis| f32::from_bits(target[axis]) - f32::from_bits(origin[axis])) +} + struct Actor<'a> { table: &'a MissionControlDeviceTableState, owner: OwnerId, -- cgit v1.2.3