Files
airdef/airdef-mod/src/sim/catalog.rs
T
chexit eea1b3692e
build / mod (push) Failing after 20s
Share aircraft and item catalog with asset generation
Co-Authored-By: Codex <noreply@openai.com>
2026-11-04 15:55:45 +03:00

171 lines
6.5 KiB
Rust

//! Stable identities and availability for the existing simulation content.
//!
//! Balance and rendering remain in `DroneSpec` and `SamSpec`: the catalog
//! refers to those typed definitions instead of copying their values. The
//! first eleven drone entries retain the historical positional wave-save order.
use super::{DroneKind, SamKind};
#[derive(Clone, Copy, Debug)]
pub struct Aircraft {
pub kind: DroneKind,
/// Inventory item, when this type has a player launch item.
pub launch_item: Option<&'static str>,
/// May appear as a selectable type in the raid menu.
pub raid: bool,
}
impl Aircraft {
/// Existing save/command key; changing it requires an explicit migration.
pub fn id(self) -> &'static str { self.kind.spec().key }
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GroundStrike {
/// Game-only RM48U coordinate-strike profile for S-400. Evidence: official agency report,
/// not independent certification: https://gur.gov.ua/content/warsanctions-hur-mo-rozkryvaie-budovu-komponentnu-bazu-ta-pidpryiemstva-kooperatsii-z-vyrobnytstva-udarnykh-raket-rm48u.html
/// The range and flight are game abstractions, not real parameters.
Rm48uGame,
/// No implemented game profile; this is not a claim of physical impossibility.
Unavailable { reason: &'static str },
}
impl GroundStrike {
/// Evidence describes provenance, not independent certification.
pub fn evidence(self) -> Option<&'static str> {
match self { GroundStrike::Rm48uGame => Some("official-agency-report"), GroundStrike::Unavailable { .. } => None }
}
}
#[derive(Clone, Copy, Debug)]
pub struct Unit {
pub kind: SamKind,
pub deploy_item: &'static str,
}
impl Unit {
pub fn id(self) -> &'static str { self.kind.spec().key }
/// Whether the unit has a gun; callers need no access to the private weapon profile.
pub fn has_gun(self) -> bool { self.kind.spec().gun.is_some() }
pub fn ground_strike(self) -> GroundStrike {
if self.kind == SamKind::S400 { GroundStrike::Rm48uGame } else { GroundStrike::Unavailable { reason: "Для этой техники не задан профиль удара по координатам" } }
}
}
use DroneKind::*;
// Generated entries keep the historical raid and unit ordering.
include!("catalog_entries.rs");
/// Existing positional serialization makes the first entries' order part of the save format.
const fn raid_count() -> usize {
let mut i = 0;
let mut count = 0;
while i < AIRCRAFT.len() {
if AIRCRAFT[i].raid { count += 1; }
i += 1;
}
count
}
pub const RAID_COUNT: usize = raid_count();
const fn raid_kinds() -> [DroneKind; RAID_COUNT] {
let mut result = [Gerbera; RAID_COUNT];
let mut i = 0;
let mut out = 0;
while i < AIRCRAFT.len() {
if AIRCRAFT[i].raid { result[out] = AIRCRAFT[i].kind; out += 1; }
i += 1;
}
result
}
pub const RAID_KINDS: [DroneKind; RAID_COUNT] = raid_kinds();
pub const UNIT_COUNT: usize = UNITS.len();
const fn unit_kinds() -> [SamKind; UNIT_COUNT] {
let mut result = [SamKind::Patriot; UNIT_COUNT];
let mut i = 0;
while i < result.len() { result[i] = UNITS[i].kind; i += 1; }
result
}
pub const UNIT_KINDS: [SamKind; UNIT_COUNT] = unit_kinds();
pub fn unit(kind: SamKind) -> &'static Unit {
UNITS.iter().find(|entry| entry.kind == kind).expect("unit kind missing from catalog")
}
/// Inventory ordering is stable for existing remotes, while availability comes from the same entries as raids.
const fn inventory_count() -> usize {
let mut n = UNIT_COUNT + 4;
let mut i = 0;
while i < AIRCRAFT.len() {
if AIRCRAFT[i].launch_item.is_some() { n += 1; }
i += 1;
}
n
}
pub const INVENTORY_COUNT: usize = inventory_count();
const fn inventory_items() -> [&'static str; INVENTORY_COUNT] {
let mut out = [""; INVENTORY_COUNT];
out[0] = "airdef:designator";
let mut n = 1;
let mut i = 0;
while i < AIRCRAFT.len() {
if let Some(item) = AIRCRAFT[i].launch_item { out[n] = item; n += 1; }
i += 1;
}
i = 0;
while i < UNITS.len() { out[n] = UNITS[i].deploy_item; n += 1; i += 1; }
out[n] = "airdef:mover";
out[n + 1] = "airdef:flare_gun";
out[n + 2] = "airdef:remover";
out
}
pub const INVENTORY: [&str; INVENTORY_COUNT] = inventory_items();
pub fn launch_item(id: &str) -> Option<DroneKind> {
AIRCRAFT.iter().find(|entry| entry.launch_item == Some(id)).map(|entry| entry.kind)
}
pub fn deploy_item(id: &str) -> Option<SamKind> {
UNITS.iter().find(|entry| entry.deploy_item == id).map(|entry| entry.kind)
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashSet;
use super::super::units::Muzzle;
#[test]
fn identities_cover_the_simulation_and_raid_order_is_compatible() {
let mut ids = HashSet::new();
let mut kinds = HashSet::new();
let mut items = HashSet::new();
for (i, e) in AIRCRAFT.iter().enumerate() {
assert!(ids.insert(e.id()), "duplicate aircraft ID {}", e.id());
assert!(kinds.insert(e.kind), "duplicate aircraft kind {:?}", e.kind);
if i < 11 { assert!(e.raid); }
if let Some(item) = e.launch_item {
assert!(items.insert(item), "duplicate item {item}");
assert_eq!(item, format!("airdef:launch_{}", e.id()));
}
}
assert_eq!(kinds, DroneKind::ALL.into_iter().collect());
let mut kinds = HashSet::new();
for e in UNITS {
assert!(ids.insert(e.id()), "duplicate unit ID {}", e.id());
assert!(kinds.insert(e.kind), "duplicate unit kind {:?}", e.kind);
assert!(items.insert(e.deploy_item), "duplicate item {}", e.deploy_item);
assert_eq!(e.deploy_item, format!("airdef:deploy_{}", e.id()));
let spec = e.kind.spec();
assert!(spec.sensor.is_finite() && spec.sensor >= 0.0);
match (&spec.missile, spec.muzzle) {
(Some(_), Muzzle::None) => panic!("{} has missiles but no muzzle", e.id()),
(_, Muzzle::Grid(cols, rows, size)) => assert!(cols > 0 && rows > 0 && size.is_finite() && size > 0.0),
(_, Muzzle::Rails(xs)) => assert!(!xs.is_empty() && xs.iter().all(|v| v.is_finite())),
_ => {}
}
}
assert_eq!(kinds, SamKind::ALL.into_iter().collect());
assert_eq!(&RAID_KINDS[..11], &[Gerbera, Shahed, Geran3, Lancet, Kh101, Kalibr, Kh22, Zircon, Iskander, Kinzhal, Oreshnik]);
}
}