diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-render/src/cockpit_radar.rs | 81 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/radar.rs | 32 |
2 files changed, 105 insertions, 8 deletions
diff --git a/crates/fparkan-render/src/cockpit_radar.rs b/crates/fparkan-render/src/cockpit_radar.rs index f14fe3c6..e9043362 100644 --- a/crates/fparkan-render/src/cockpit_radar.rs +++ b/crates/fparkan-render/src/cockpit_radar.rs @@ -3,6 +3,59 @@ use crate::cockpit_course::CENTER; use std::f64::consts::FRAC_PI_2; +pub const SWEEP_EDGES: usize = 20; + +/// Clock state held by the cockpit radar panel. +#[derive(Default)] +pub struct RadarSweep { + start_ms: Option<u32>, +} + +impl RadarSweep { + /// Returns the native design radius and whether this paint starts a new sweep. + /// `radar_period_ms` is the positive period supplied by Control. + pub fn sample(&mut self, now_ms: u32, radar_period_ms: f32) -> (u32, bool) { + let duration = f64::from(radar_period_ms * 0.002); + if let Some(start_ms) = self.start_ms { + let elapsed = f64::from(now_ms.wrapping_sub(start_ms)) * f64::from(0.001_f32); + if elapsed <= duration { + let radius = (60.0 / duration * f64::from(elapsed as f32)).round_ties_even(); + return (radius as u32, false); + } + } + self.start_ms = Some(now_ms); + (0, true) + } +} + +/// The 20 native line segments, expanded to a one-pixel stroke in screen space. +/// The caller omits the degenerate outline at radius zero. +pub fn sweep_outline(radius: u32, viewport: [f32; 2]) -> [[[f32; 2]; 2]; SWEEP_EDGES] { + let [sx, sy] = [viewport[0] / 640.0, viewport[1] / 480.0]; + let points: [[f32; 2]; SWEEP_EDGES] = std::array::from_fn(|index| { + let angle = index as f32 * (std::f32::consts::TAU / SWEEP_EDGES as f32); + let (sin, cos) = angle.sin_cos(); + let r = radius as f32; + [(CENTER[0] + r * cos) * sx, (CENTER[1] + r * sin) * sy] + }); + let normal = |[ax, ay]: [f32; 2], [bx, by]: [f32; 2]| { + let [dx, dy] = [bx - ax, by - ay]; + let length = dx.hypot(dy); + [dy / length, -dx / length] + }; + std::array::from_fn(|i| { + let point = points[i]; + let [nx, ny] = normal(points[(i + SWEEP_EDGES - 1) % SWEEP_EDGES], point); + let [mx, my] = normal(point, points[(i + 1) % SWEEP_EDGES]); + let scale = 0.5 / (1.0 + nx * mx + ny * my); + let [dx, dy] = [(nx + mx) * scale, (ny + my) * scale]; + [ + [point[0] + dx, point[1] + dy], + [point[0] - dx, point[1] - dy], + ] + }) +} + #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum ContactMark { Building, @@ -83,6 +136,34 @@ mod tests { use super::*; #[test] + fn sweep_restarts_only_after_the_native_strict_boundary() { + let mut sweep = RadarSweep::default(); + assert_eq!(sweep.sample(0, 750.0), (0, true)); + assert_eq!(sweep.sample(775, 750.0), (31, false)); + assert_eq!(sweep.sample(1500, 750.0), (60, false)); + assert_eq!(sweep.sample(1501, 750.0), (0, true)); + assert_eq!(sweep.sample(1501, 750.0), (0, false)); + assert_eq!(sweep.sample(5100, 750.0), (0, true)); + } + + #[test] + fn sweep_polygon_matches_native_800_by_600_vertices() { + let outline = sweep_outline(31, [800.0, 600.0]); + for (index, native) in [ + (0, [438.75, 495.0]), + (1, [436.85345, 506.97446]), + (5, [400.0, 533.75]), + (13, [377.22327, 463.65067]), + ] { + let [outer, inner] = outline[index]; + for axis in 0..2 { + let center = (outer[axis] + inner[axis]) * 0.5; + assert!((center - native[axis]).abs() < 0.001); + } + } + } + + #[test] fn cockpit_radar_rotates_with_view_and_maps_range_to_sixty_design_pixels() { let viewport = [800.0, 600.0]; assert_eq!( diff --git a/crates/fparkan-runtime/src/radar.rs b/crates/fparkan-runtime/src/radar.rs index a6cb9f78..677c3348 100644 --- a/crates/fparkan-runtime/src/radar.rs +++ b/crates/fparkan-runtime/src/radar.rs @@ -41,14 +41,30 @@ pub struct CockpitContact { /// The range printed on the cockpit dial, even when the antenna is unpowered. #[must_use] pub fn cockpit_range_readout(engine: &Engine, cockpit: StartingCockpit) -> i32 { - let range = engine.loaded.as_ref().and_then(|mission| { - let table = mission - .control_device_tables - .iter() - .find(|table| table.handle == cockpit.handle)?; - Some(f32::from_bits(radar_device(table)?.coefficient_bits?[3])) - }); - range_readout(range) + range_readout(cockpit_radar_coefficient(engine, cockpit, 3)) +} + +/// The installed radar's period for the HUD sweep, from Control selector 0xa9. +#[must_use] +pub fn cockpit_radar_period(engine: &Engine, cockpit: StartingCockpit) -> Option<f32> { + cockpit_radar_coefficient(engine, cockpit, 4) + .filter(|period| period.is_finite() && *period > 0.0) +} + +fn cockpit_radar_coefficient( + engine: &Engine, + cockpit: StartingCockpit, + index: usize, +) -> Option<f32> { + let table = engine + .loaded + .as_ref()? + .control_device_tables + .iter() + .find(|table| table.handle == cockpit.handle)?; + Some(f32::from_bits( + radar_device(table)?.coefficient_bits?[index], + )) } fn range_readout(range: Option<f32>) -> i32 { |
