diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-02 00:04:24 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-02 00:04:24 +0300 |
| commit | 752d34af83acf9a818e22c621fb034913079acff (patch) | |
| tree | 65b0f47859be8f8a33b8e37fecad81cfd660739c /crates | |
| parent | 029297446ec0b235602b4f4d9197786a3466a6fe (diff) | |
| download | fparkan-752d34af83acf9a818e22c621fb034913079acff.tar.xz fparkan-752d34af83acf9a818e22c621fb034913079acff.zip | |
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.
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-render/src/cockpit_course.rs | 2 | ||||
| -rw-r--r-- | crates/fparkan-render/src/cockpit_radar.rs | 120 | ||||
| -rw-r--r-- | crates/fparkan-render/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 2 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/radar.rs | 107 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/satellite_contacts.rs | 4 |
6 files changed, 231 insertions, 5 deletions
diff --git a/crates/fparkan-render/src/cockpit_course.rs b/crates/fparkan-render/src/cockpit_course.rs index 974249b0..713c9ca6 100644 --- a/crates/fparkan-render/src/cockpit_course.rs +++ b/crates/fparkan-render/src/cockpit_course.rs @@ -2,7 +2,7 @@ use std::f32::consts::{FRAC_PI_2, PI}; -const CENTER: [f32; 2] = [320.0, 396.0]; +pub(crate) const CENTER: [f32; 2] = [320.0, 396.0]; const HALF_MARKER_ANGLE: f32 = 0.08726647; /// Front and rear marker corners in the original triangle-strip order. diff --git a/crates/fparkan-render/src/cockpit_radar.rs b/crates/fparkan-render/src/cockpit_radar.rs new file mode 100644 index 00000000..f14fe3c6 --- /dev/null +++ b/crates/fparkan-render/src/cockpit_radar.rs @@ -0,0 +1,120 @@ +//! Native cockpit radar projection and screen-pixel marks. + +use crate::cockpit_course::CENTER; +use std::f64::consts::FRAC_PI_2; + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum ContactMark { + Building, + Unit, + Cross, +} + +/// Project a horizontal world offset into the cockpit radar's screen pixels. +#[must_use] +pub fn contact_pixel( + [x, y]: [f32; 2], + [forward_x, forward_y]: [f32; 2], + range: f32, + viewport: [f32; 2], +) -> [f32; 2] { + let [x, y] = [x, y].map(f64::from); + let bearing = y.atan2(x) - f64::from(forward_y).atan2(f64::from(forward_x)) + FRAC_PI_2; + let (sin, cos) = bearing.sin_cos(); + let radius = f64::from(x.hypot(y) as f32) * f64::from(range.recip()) * 60.0; + let [sx, sy] = [viewport[0] / 640.0, viewport[1] / 480.0].map(f64::from); + [ + ((f64::from(CENTER[0]) + cos * radius) * sx) as f32, + ((f64::from(CENTER[1]) - sin * radius) * sy) as f32, + ] +} + +/// Emit the filled rectangles that make up a contact and its optional outline. +pub fn mark_rectangles( + mark: ContactMark, + [x, y]: [f32; 2], + [width, height]: [f32; 2], + selected: bool, + mut paint: impl FnMut([i32; 4]), +) { + let design_size = match mark { + ContactMark::Building => 5.0, + ContactMark::Unit => 3.0, + ContactMark::Cross => 4.0, + }; + let round = |value: f32| value.round_ties_even() as i32; + let w = round(width / 640.0 * design_size); + let h = round(height / 480.0 * design_size); + let [left, top, right, bottom] = if mark == ContactMark::Cross { + let [cx, cy] = [round(x), round(y)]; + let [dx, dy] = [w / 2, h / 2]; + let bounds = [ + cx.saturating_sub(dx), + cy.saturating_sub(dy), + cx.saturating_add(dx), + cy.saturating_add(dy), + ]; + paint([bounds[0], cy, bounds[2], cy.saturating_add(1)]); + paint([cx, bounds[1], cx.saturating_add(1), bounds[3]]); + bounds + } else { + let left = round(x - (w / 2) as f32); + let top = round(y - (h / 2) as f32); + let bounds = [left, top, left.saturating_add(w), top.saturating_add(h)]; + paint(bounds); + bounds + }; + if selected { + let [l, t, r, b] = [ + left.saturating_sub(2), + top.saturating_sub(2), + right.saturating_add(if mark == ContactMark::Cross { 2 } else { 1 }), + bottom.saturating_add(if mark == ContactMark::Cross { 2 } else { 1 }), + ]; + paint([l, t, r, t.saturating_add(1)]); + paint([l, b.saturating_sub(1), r, b]); + paint([l, t, l.saturating_add(1), b]); + paint([r.saturating_sub(1), t, r, b]); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn cockpit_radar_rotates_with_view_and_maps_range_to_sixty_design_pixels() { + let viewport = [800.0, 600.0]; + assert_eq!( + contact_pixel([300.0, 0.0], [1.0, 0.0], 300.0, viewport), + [400.0, 420.0] + ); + assert_eq!( + contact_pixel([0.0, -300.0], [1.0, 0.0], 300.0, viewport), + [475.0, 495.0] + ); + assert_eq!( + contact_pixel([300.0, 0.0], [0.0, 1.0], 300.0, viewport), + [475.0, 495.0] + ); + } + + #[test] + fn projected_contact_reproduces_native_800_by_600_draw_coordinates() { + // Native callbacks produced these bounds for a point 110 units east and 80 north. + let viewport = [800.0, 600.0]; + let point = contact_pixel([110.0, 80.0], [0.7673624, -0.64121366], 400.0, viewport); + for (mark, expected) in [ + (ContactMark::Building, vec![[372, 486, 378, 492]]), + (ContactMark::Unit, vec![[373, 487, 377, 491]]), + ( + ContactMark::Cross, + vec![[373, 489, 377, 490], [375, 487, 376, 491]], + ), + ] { + let mut actual = Vec::new(); + mark_rectangles(mark, point, viewport, false, |rect| actual.push(rect)); + assert_eq!(actual, expected); + } + } +} diff --git a/crates/fparkan-render/src/lib.rs b/crates/fparkan-render/src/lib.rs index b684b787..645cb25d 100644 --- a/crates/fparkan-render/src/lib.rs +++ b/crates/fparkan-render/src/lib.rs @@ -2,6 +2,7 @@ //! Camera mathematics and graphics pipeline state. pub mod cockpit_course; +pub mod cockpit_radar; pub mod water; /// A 64-byte transform block returned by the original Terrain camera ABI. diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 2a5497f1..7905a75b 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -132,7 +132,7 @@ mod projectile_damage; mod projectile_flight; mod projectile_parts; mod projectile_shield; -mod radar; +pub mod radar; mod weapon_deployment; mod weapon_resupply; pub use projectile_shield::{ 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<ObjectHandle>, +) -> Option<(f32, impl Iterator<Item = CockpitContact> + '_)> { + 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)); } } diff --git a/crates/fparkan-runtime/src/satellite_contacts.rs b/crates/fparkan-runtime/src/satellite_contacts.rs index 12b45037..68957250 100644 --- a/crates/fparkan-runtime/src/satellite_contacts.rs +++ b/crates/fparkan-runtime/src/satellite_contacts.rs @@ -7,7 +7,7 @@ use fparkan_world::{ TransformState, }; -const OWN_TINT: [u8; 3] = [128, 128, 255]; +pub(super) const OWN_TINT: [u8; 3] = [128, 128, 255]; /// The original icon crop and position within the 256-pixel satellite image. #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -202,7 +202,7 @@ fn unit_class(class: u32) -> bool { class & crate::autodemo::MOBILE_UNIT != 0 || class == crate::projectile::ANIMAL_UNIT_CLASS } -fn clan_tint(mode: u32, relation: Option<i32>) -> [u8; 3] { +pub(super) fn clan_tint(mode: u32, relation: Option<i32>) -> [u8; 3] { // iron3d+0x658a0 selects the observed clan's relation to the viewer. match (mode, relation) { (0, _) => [255, 255, 0], |
