Separate search and engagement roles, support launcher-only SAMs, animate individual tube lids and delay ground strike spending until ejection. Preserve battery configuration and launch poses in saves and replay. Co-authored-by: Codex <codex@openai.com>
This commit is contained in:
@@ -0,0 +1,257 @@
|
||||
//! Game-only configuration for a launcher using shared radar tracks.
|
||||
use super::*;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum SamMode {
|
||||
#[default]
|
||||
Integrated,
|
||||
LauncherOnly,
|
||||
}
|
||||
|
||||
impl SamMode {
|
||||
pub fn key(self) -> &'static str {
|
||||
match self { Self::Integrated => "integrated", Self::LauncherOnly => "launcher" }
|
||||
}
|
||||
pub fn from_key(key: &str) -> Option<Self> {
|
||||
match key { "integrated" => Some(Self::Integrated), "launcher" => Some(Self::LauncherOnly), _ => None }
|
||||
}
|
||||
}
|
||||
|
||||
/// Game roles for the generic shared radar; all parameters are fictional.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum RadarRole {
|
||||
#[default]
|
||||
Multifunction,
|
||||
Search,
|
||||
Engagement,
|
||||
Height,
|
||||
}
|
||||
impl RadarRole {
|
||||
pub fn key(self) -> &'static str {
|
||||
match self { Self::Multifunction => "multi", Self::Search => "search", Self::Engagement => "engagement", Self::Height => "height" }
|
||||
}
|
||||
pub fn from_key(key: &str) -> Option<Self> {
|
||||
match key { "multi" => Some(Self::Multifunction), "search" => Some(Self::Search), "engagement" => Some(Self::Engagement), "height" => Some(Self::Height), _ => None }
|
||||
}
|
||||
pub fn name(self) -> &'static str {
|
||||
match self { Self::Multifunction => "многофункциональная", Self::Search => "обзорная", Self::Engagement => "сопровождения", Self::Height => "мачтовая" }
|
||||
}
|
||||
pub fn authorizes(self) -> bool { matches!(self, Self::Multifunction | Self::Engagement) }
|
||||
pub(super) fn reach_factor(self) -> f64 { match self { Self::Multifunction => 1.0, Self::Search => 1.15, Self::Engagement => 0.8, Self::Height => 1.05 } }
|
||||
}
|
||||
|
||||
impl Sam {
|
||||
pub fn set_radar_role(&mut self, role: RadarRole) -> bool {
|
||||
if self.kind != SamKind::Radar { return false; }
|
||||
if self.radar_role != role {
|
||||
self.radar_role = role;
|
||||
self.tracks.clear();
|
||||
self.seen.clear();
|
||||
self.own.clear();
|
||||
}
|
||||
true
|
||||
}
|
||||
/// Change the physical configuration. Invalid combinations leave the unit untouched.
|
||||
pub fn set_mode(&mut self, mode: SamMode) -> bool {
|
||||
if mode == SamMode::LauncherOnly && !matches!(self.kind, SamKind::S400 | SamKind::S500) { return false; }
|
||||
if self.mode != mode {
|
||||
self.mode = mode;
|
||||
self.tracks.clear();
|
||||
self.seen.clear();
|
||||
self.own.clear();
|
||||
self.target = None;
|
||||
self.gun_target = None;
|
||||
self.terrain.clear();
|
||||
self.waits.clear();
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl Sim {
|
||||
/// Current configurations and whether a launcher has an operational radar link.
|
||||
pub fn battery_status(&self) -> Vec<String> {
|
||||
self.sams.iter().enumerate().filter_map(|(i, sam)| {
|
||||
if sam.kind == SamKind::Radar {
|
||||
Some(format!("№{} РЛС: {}, {}", i + 1, sam.radar_role.name(), if sam.radar_up() && sam.link_range() > 0.0 { "работает" } else { "нет связи или РЛС не работает" }))
|
||||
} else if matches!(sam.kind, SamKind::S400 | SamKind::S500) {
|
||||
let linked = self.sams.iter().any(|r| r.kind == SamKind::Radar && r.radar_role.authorizes() && r.radar_up() && r.link_range() > 0.0 && sam.link_range() > 0.0 && r.pos.distance(sam.pos) <= r.link_range().min(sam.link_range()));
|
||||
Some(format!("№{} {}: {}{}", i + 1, sam.kind.name(), sam.mode.key(), if sam.mode == SamMode::LauncherOnly { if linked { ", РЛС связана" } else { ", нет связанной РЛС сопровождения" } } else { "" }))
|
||||
} else { None }
|
||||
}).collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use super::super::{sight, why};
|
||||
|
||||
fn track(now: u64) -> Track {
|
||||
Track { id: 42, kind: DroneKind::Kh101, pos: DVec3::new(0.0, 100.0, -200.0), vel: DVec3::ZERO, tick: now, own_tick: now, ttl: 400, hits: 3, locked: false }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_radar_serves_multiple_launchers_and_failure_blocks_cue() {
|
||||
let mut radar = Sam::new(1, SamKind::Radar, DVec3::ZERO, 0.0);
|
||||
let mut a = Sam::new(2, SamKind::S400, DVec3::new(10.0, 0.0, 0.0), 0.0);
|
||||
let mut b = Sam::new(3, SamKind::S500, DVec3::new(20.0, 0.0, 0.0), 0.0);
|
||||
assert!(a.set_mode(SamMode::LauncherOnly));
|
||||
assert!(b.set_mode(SamMode::LauncherOnly));
|
||||
assert!(!a.radar_up() && !b.radar_up());
|
||||
radar.tracks.push(track(10));
|
||||
radar.set_radar_role(RadarRole::Search);
|
||||
radar.tracks.push(track(10));
|
||||
assert!(!why::radar_cue(&a, &[&radar], 42, 10));
|
||||
radar.set_radar_role(RadarRole::Height);
|
||||
radar.tracks.push(track(10));
|
||||
assert!(!why::radar_cue(&a, &[&radar], 42, 10));
|
||||
radar.set_radar_role(RadarRole::Engagement);
|
||||
radar.tracks.push(track(10));
|
||||
assert!(why::radar_cue(&a, &[&radar], 42, 10));
|
||||
assert!(why::radar_cue(&b, &[&radar], 42, 10));
|
||||
assert!(!why::radar_cue(&a, &[&radar], 42, 10 + why::FAR_RADAR_AGE + 1));
|
||||
radar.comps.0[Part::Comms as usize] = 0.0;
|
||||
assert!(!why::radar_cue(&a, &[&radar], 42, 10));
|
||||
radar.comps.0[Part::Comms as usize] = 1.0;
|
||||
a.comps.0[Part::Comms as usize] = 0.0;
|
||||
assert!(!why::radar_cue(&a, &[&radar], 42, 10));
|
||||
a.comps.0[Part::Comms as usize] = 1.0;
|
||||
a.pos.x = sight::LINK + 1.0;
|
||||
assert!(!why::radar_cue(&a, &[&radar], 42, 10));
|
||||
a.pos.x = 10.0;
|
||||
radar.health = 0.0;
|
||||
assert!(!why::radar_cue(&a, &[&radar], 42, 10));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn network_shares_a_measurement_to_both_launchers_without_fabricating_own_tracks() {
|
||||
let mut sim = Sim::new(7);
|
||||
sim.tick = 10;
|
||||
let id = sim.id();
|
||||
sim.sams.push(Sam::new(id, SamKind::Radar, DVec3::ZERO, 0.0));
|
||||
for kind in [SamKind::S400, SamKind::S500] {
|
||||
let id = sim.id();
|
||||
let mut launcher = Sam::new(id, kind, DVec3::new(10.0, 0.0, 0.0), 0.0);
|
||||
launcher.set_mode(SamMode::LauncherOnly);
|
||||
launcher.roles = 0xff;
|
||||
sim.sams.push(launcher);
|
||||
}
|
||||
sim.add_drone(DroneKind::Kh101, DVec3::new(0.0, 100.0, -200.0), DVec3::Z, DVec3::ZERO);
|
||||
let id = sim.drones[0].id;
|
||||
let mut measurement = track(10);
|
||||
measurement.id = id;
|
||||
sim.sams[0].tracks.push(measurement);
|
||||
sim.update_sight(&mut Ground::default());
|
||||
for launcher in &sim.sams[1..] {
|
||||
let received = launcher.track(id).expect("shared measurement");
|
||||
assert_eq!(received.tick, 10);
|
||||
assert_eq!(received.own_tick, 0);
|
||||
assert!(why::radar_cue(launcher, &[&sim.sams[0]], id, 10));
|
||||
let load = launcher.kind.spec().missile.as_ref().unwrap();
|
||||
let opts = why::Opts { last_chance: false, far_shots: false };
|
||||
assert!(why::basis(launcher, load, received, &sim.drones[0], 10, 0, 1.0, opts, true, false).is_some());
|
||||
assert!(why::basis(launcher, load, received, &sim.drones[0], 10, 0, 1.0, opts, false, false).is_none());
|
||||
}
|
||||
sim.sams[0].health = 0.0;
|
||||
for launcher in &sim.sams[1..] {
|
||||
assert!(launcher.track(id).is_some());
|
||||
assert!(!why::radar_cue(launcher, &[&sim.sams[0]], id, 10));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn search_cues_tracking_radar_but_only_its_own_measurement_authorizes_launch() {
|
||||
for in_range in [true, false] {
|
||||
let mut sim = Sim::new(71);
|
||||
sim.tick = 10;
|
||||
let mut search = Sam::new(1, SamKind::Radar, DVec3::ZERO, 0.0);
|
||||
search.set_radar_role(RadarRole::Search);
|
||||
let mut fire_control = Sam::new(2, SamKind::Radar, DVec3::ZERO, 0.0);
|
||||
fire_control.set_radar_role(RadarRole::Engagement);
|
||||
let mut launcher = Sam::new(3, SamKind::S400, DVec3::ZERO, 0.0);
|
||||
launcher.set_mode(SamMode::LauncherOnly);
|
||||
launcher.roles = 0xff;
|
||||
sim.sams.extend([search, fire_control, launcher]);
|
||||
let distance = if in_range { 100.0 } else { 10000.0 };
|
||||
sim.add_drone(DroneKind::Kh101, DVec3::new(0.0, 70.0, -distance), DVec3::Z, DVec3::ZERO);
|
||||
let id = sim.drones[0].id;
|
||||
let mut measurement = track(10);
|
||||
measurement.id = id;
|
||||
measurement.pos = sim.drones[0].pos;
|
||||
sim.sams[0].tracks.push(measurement);
|
||||
sim.sams[0].beam = std::f64::consts::PI;
|
||||
sim.sams[1].beam = std::f64::consts::PI;
|
||||
let mut ground = Ground::default();
|
||||
sim.update_sight(&mut ground);
|
||||
assert_eq!(sim.sams[1].track(id).unwrap().own_tick, 0, "cue is not a measurement");
|
||||
assert!(!why::radar_cue(&sim.sams[2], &[&sim.sams[1]], id, sim.tick));
|
||||
for now in 11..40 {
|
||||
sim.tick = now;
|
||||
// Bearing is north (zero); force the ordinary beam away each tick.
|
||||
sim.sams[1].beam = std::f64::consts::PI;
|
||||
sim.update_sight(&mut ground);
|
||||
if sim.sams[1].track(id).is_some_and(|t| t.own_tick > 0) { break; }
|
||||
}
|
||||
let got_own = sim.sams[1].track(id).is_some_and(|t| t.own_tick > 0);
|
||||
assert_eq!(got_own, in_range);
|
||||
assert_eq!(why::radar_cue(&sim.sams[2], &[&sim.sams[1]], id, sim.tick), in_range);
|
||||
if in_range {
|
||||
let tr = sim.sams[2].track(id).unwrap();
|
||||
let load = sim.sams[2].kind.spec().missile.as_ref().unwrap();
|
||||
let opts = why::Opts { last_chance: false, far_shots: false };
|
||||
assert!(why::basis(&sim.sams[2], load, tr, &sim.drones[0], sim.tick, 0, 1.0, opts, true, false).is_some());
|
||||
let mut out = Out::default();
|
||||
for _ in 0..100 {
|
||||
sim.step(&mut ground, None, &mut out);
|
||||
if sim.sams[2].fired > 0 { break; }
|
||||
}
|
||||
assert!(sim.sams[2].fired > 0, "launcher never fired after its tracking radar measured the cue");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fictional_roles_change_scan_and_height_and_coverage() {
|
||||
let mut r = Sam::new(1, SamKind::Radar, DVec3::ZERO, 0.0);
|
||||
let base_mast = r.sensor_mast();
|
||||
let base_scan = r.scan_ticks();
|
||||
let base_reach = sight::reach(&r, DroneKind::Kh101, 5000.0);
|
||||
r.set_radar_role(RadarRole::Height);
|
||||
assert!(r.sensor_mast() > base_mast);
|
||||
r.set_radar_role(RadarRole::Engagement);
|
||||
assert!(r.scan_ticks() < base_scan && r.scan_profile().agile);
|
||||
assert!(sight::reach(&r, DroneKind::Kh101, 5000.0) < base_reach);
|
||||
r.set_radar_role(RadarRole::Search);
|
||||
assert!(sight::reach(&r, DroneKind::Kh101, 5000.0) > base_reach);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn launcher_has_no_own_map_sensor_and_mode_switch_drops_stale_tracks() {
|
||||
let mut sim = Sim::new(3);
|
||||
sim.sams.push(Sam::new(1, SamKind::S400, DVec3::ZERO, 0.0));
|
||||
assert!(sim.low_sensor(SamKind::S400, DVec3::ZERO, Some(1)).is_some());
|
||||
sim.sams[0].tracks.push(track(10));
|
||||
sim.sams[0].terrain.insert(42, (10, 1));
|
||||
assert!(sim.sams[0].set_mode(SamMode::LauncherOnly));
|
||||
assert!(sim.low_sensor(SamKind::S400, DVec3::ZERO, Some(1)).is_none());
|
||||
assert!(sim.low_sensors().is_empty());
|
||||
assert!(sim.sams[0].track(42).is_none());
|
||||
assert!(sim.sams[0].terrain.is_empty());
|
||||
sim.sams[0].set_mode(SamMode::Integrated);
|
||||
assert!(sim.low_sensor(SamKind::S400, DVec3::ZERO, Some(1)).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn configuration_clears_old_tracks_and_rejects_other_kinds() {
|
||||
let mut sam = Sam::new(1, SamKind::S400, DVec3::ZERO, 0.0);
|
||||
sam.tracks.push(track(10));
|
||||
assert!(sam.set_mode(SamMode::LauncherOnly));
|
||||
assert!(sam.tracks.is_empty());
|
||||
assert!(sam.set_mode(SamMode::Integrated));
|
||||
assert!(sam.radar_up());
|
||||
let mut other = Sam::new(2, SamKind::Patriot, DVec3::ZERO, 0.0);
|
||||
assert!(!other.set_mode(SamMode::LauncherOnly));
|
||||
assert_eq!(other.mode, SamMode::Integrated);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user