Add typed depot logistics visibility, auto-strike, 16-block reach, and web panel controls
build / mod (push) Failing after 15s
build / mod (push) Failing after 15s
This commit is contained in:
@@ -302,13 +302,20 @@ impl Sim {
|
||||
out.messages.push(format!("{} №{} УНИЧТОЖЕН", sam.kind.name(), i + 1));
|
||||
let (id, kind) = (sam.id, sam.kind);
|
||||
// A depot's stock (or a truck's load) goes up with it.
|
||||
let stock = if matches!(self.sams[i].kind, SamKind::Depot | SamKind::Tzm) { self.sams[i].cargo } else { 0 };
|
||||
let stock = if matches!(self.sams[i].kind, SamKind::Depot | SamKind::Tzm) {
|
||||
self.sams[i].ammo_stock.total().max(self.sams[i].cargo)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
self.sams[i].ammo_stock = AmmoStock::default();
|
||||
self.sams[i].cargo = 0;
|
||||
self.sams[i].reload_mask = 0;
|
||||
let typed = self.sams[i].ground_stock + self.sams[i].air_48n6_stock;
|
||||
self.sams[i].ground_stock = 0;
|
||||
self.sams[i].air_48n6_stock = 0;
|
||||
self.sams[i].ground_rounds = 0;
|
||||
self.sams[i].ground_request = false;
|
||||
self.sams[i].auto_ground_request = false;
|
||||
self.sams[i].ground_return = false;
|
||||
self.sams[i].return_cargo = false;
|
||||
let missiles = missiles + stock * 2 + typed;
|
||||
@@ -466,6 +473,7 @@ impl Sim {
|
||||
sam.burn = 0;
|
||||
sam.state = SamState::Search;
|
||||
sam.reloading = 0;
|
||||
sam.reload_mask = 0;
|
||||
sam.empty = false;
|
||||
// Rebuilt where it stands: a march under way is called off.
|
||||
sam.dest = None;
|
||||
|
||||
+803
-46
@@ -1,7 +1,216 @@
|
||||
//! Empty magazines are replenished from finite depot stock, either directly or via a truck.
|
||||
//! Empty magazines are replenished from finite typed depot stock, either directly or via a truck.
|
||||
use super::*;
|
||||
|
||||
pub const DEPOT_RM48U_CHANCE: f64 = 0.35;
|
||||
pub(super) const RELOAD_MISSILE: u8 = 1;
|
||||
pub(super) const RELOAD_GUN: u8 = 2;
|
||||
|
||||
/// Typed air-defence ammunition carried by depots and reload trucks, differentiated per system and weapon channel.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
|
||||
pub enum AmmoKind {
|
||||
PatriotMissile,
|
||||
GepardGun,
|
||||
PantsirMissile,
|
||||
PantsirGun,
|
||||
S400Missile,
|
||||
S500Missile,
|
||||
IrisTMissile,
|
||||
NasamsMissile,
|
||||
BukMissile,
|
||||
TorMissile,
|
||||
P1sunMissile,
|
||||
}
|
||||
|
||||
impl AmmoKind {
|
||||
pub const COUNT: usize = 11;
|
||||
pub const ALL: [AmmoKind; Self::COUNT] = [
|
||||
AmmoKind::PatriotMissile,
|
||||
AmmoKind::GepardGun,
|
||||
AmmoKind::PantsirMissile,
|
||||
AmmoKind::PantsirGun,
|
||||
AmmoKind::S400Missile,
|
||||
AmmoKind::S500Missile,
|
||||
AmmoKind::IrisTMissile,
|
||||
AmmoKind::NasamsMissile,
|
||||
AmmoKind::BukMissile,
|
||||
AmmoKind::TorMissile,
|
||||
AmmoKind::P1sunMissile,
|
||||
];
|
||||
|
||||
pub const fn index(self) -> usize {
|
||||
self as usize
|
||||
}
|
||||
|
||||
pub const fn key(self) -> &'static str {
|
||||
match self {
|
||||
AmmoKind::PatriotMissile => "patriot",
|
||||
AmmoKind::GepardGun => "gepard_gun",
|
||||
AmmoKind::PantsirMissile => "pantsir_missile",
|
||||
AmmoKind::PantsirGun => "pantsir_gun",
|
||||
AmmoKind::S400Missile => "s400",
|
||||
AmmoKind::S500Missile => "s500",
|
||||
AmmoKind::IrisTMissile => "irist",
|
||||
AmmoKind::NasamsMissile => "nasams",
|
||||
AmmoKind::BukMissile => "buk",
|
||||
AmmoKind::TorMissile => "tor",
|
||||
AmmoKind::P1sunMissile => "p1sun",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_key(key: &str) -> Option<Self> {
|
||||
Self::ALL.into_iter().find(|k| k.key() == key)
|
||||
}
|
||||
|
||||
pub const fn short_name(self) -> &'static str {
|
||||
match self {
|
||||
AmmoKind::PatriotMissile => "Patriot",
|
||||
AmmoKind::GepardGun => "Гепард пуш",
|
||||
AmmoKind::PantsirMissile => "Панцирь рак",
|
||||
AmmoKind::PantsirGun => "Панцирь пуш",
|
||||
AmmoKind::S400Missile => "С-400",
|
||||
AmmoKind::S500Missile => "С-500",
|
||||
AmmoKind::IrisTMissile => "IRIS-T",
|
||||
AmmoKind::NasamsMissile => "NASAMS",
|
||||
AmmoKind::BukMissile => "Бук",
|
||||
AmmoKind::TorMissile => "Тор",
|
||||
AmmoKind::P1sunMissile => "P1-SUN",
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn name(self) -> &'static str {
|
||||
match self {
|
||||
AmmoKind::PatriotMissile => "Patriot (PAC-3)",
|
||||
AmmoKind::GepardGun => "Гепард (35 мм)",
|
||||
AmmoKind::PantsirMissile => "Панцирь (57Э6)",
|
||||
AmmoKind::PantsirGun => "Панцирь (30 мм)",
|
||||
AmmoKind::S400Missile => "С-400 (48Н6)",
|
||||
AmmoKind::S500Missile => "С-500 (77Н6)",
|
||||
AmmoKind::IrisTMissile => "IRIS-T SLM",
|
||||
AmmoKind::NasamsMissile => "NASAMS (AMRAAM)",
|
||||
AmmoKind::BukMissile => "Бук-М2 (9М317)",
|
||||
AmmoKind::TorMissile => "Тор-М2 (9М338)",
|
||||
AmmoKind::P1sunMissile => "P1-SUN",
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn is_gun(self) -> bool {
|
||||
matches!(self, AmmoKind::GepardGun | AmmoKind::PantsirGun)
|
||||
}
|
||||
|
||||
pub const fn spawn_range(self) -> (u32, u32) {
|
||||
match self {
|
||||
AmmoKind::PatriotMissile => (1, 4),
|
||||
AmmoKind::GepardGun => (2, 6),
|
||||
AmmoKind::PantsirMissile => (1, 5),
|
||||
AmmoKind::PantsirGun => (2, 6),
|
||||
AmmoKind::S400Missile => (1, 4),
|
||||
AmmoKind::S500Missile => (1, 3),
|
||||
AmmoKind::IrisTMissile => (1, 4),
|
||||
AmmoKind::NasamsMissile => (1, 4),
|
||||
AmmoKind::BukMissile => (1, 4),
|
||||
AmmoKind::TorMissile => (2, 5),
|
||||
AmmoKind::P1sunMissile => (2, 6),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Per-type air-defence reload stock stored at a depot or carried on a TZM.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
|
||||
pub struct AmmoStock(pub [u32; AmmoKind::COUNT]);
|
||||
|
||||
impl AmmoStock {
|
||||
/// Deterministic stock used by `Sam::new(Depot)` when not deployed with a random roll.
|
||||
pub const fn new_default_depot() -> Self {
|
||||
Self([3; AmmoKind::COUNT])
|
||||
}
|
||||
|
||||
/// Distributes a legacy untyped `cargo` count across all `AmmoKind`s for backward-compatible save loading.
|
||||
pub fn from_legacy_cargo(cargo: u32) -> Self {
|
||||
let base = cargo / AmmoKind::COUNT as u32;
|
||||
let rem = cargo % AmmoKind::COUNT as u32;
|
||||
let mut counts = [base; AmmoKind::COUNT];
|
||||
for (i, c) in counts.iter_mut().enumerate() {
|
||||
if (i as u32) < rem {
|
||||
*c += 1;
|
||||
}
|
||||
}
|
||||
Self(counts)
|
||||
}
|
||||
|
||||
/// Rolls random stock for each system/weapon channel plus a probabilistic RM48U strike allocation.
|
||||
pub fn roll(rng: &mut Rng) -> (Self, u32) {
|
||||
let mut stock = Self::default();
|
||||
for kind in AmmoKind::ALL {
|
||||
let (lo, hi) = kind.spawn_range();
|
||||
let span = hi - lo + 1;
|
||||
let n = lo + ((rng.f() * span as f64) as u32).min(span - 1);
|
||||
stock.set(kind, n);
|
||||
}
|
||||
let rm48u = if rng.chance(DEPOT_RM48U_CHANCE) {
|
||||
if rng.chance(0.30) { 2 } else { 1 }
|
||||
} else {
|
||||
0
|
||||
};
|
||||
(stock, rm48u)
|
||||
}
|
||||
|
||||
pub const fn get(self, kind: AmmoKind) -> u32 {
|
||||
self.0[kind.index()]
|
||||
}
|
||||
|
||||
pub fn set(&mut self, kind: AmmoKind, n: u32) {
|
||||
self.0[kind.index()] = n.min(STOCK_LIMIT);
|
||||
}
|
||||
|
||||
pub fn add(&mut self, kind: AmmoKind, n: u32) -> Option<u32> {
|
||||
let total = self.total();
|
||||
let room = STOCK_LIMIT.saturating_sub(total);
|
||||
let added = n.min(room);
|
||||
self.0[kind.index()] = self.0[kind.index()].saturating_add(added);
|
||||
(added > 0 || n == 0).then_some(added)
|
||||
}
|
||||
|
||||
pub fn take(&mut self, kind: AmmoKind, n: u32) -> u32 {
|
||||
let taken = self.0[kind.index()].min(n);
|
||||
self.0[kind.index()] -= taken;
|
||||
taken
|
||||
}
|
||||
|
||||
pub fn total(self) -> u32 {
|
||||
self.0.iter().copied().sum()
|
||||
}
|
||||
|
||||
pub fn is_empty(self) -> bool {
|
||||
self.0.iter().all(|&n| n == 0)
|
||||
}
|
||||
|
||||
pub fn non_zero(self) -> impl Iterator<Item = (AmmoKind, u32)> {
|
||||
AmmoKind::ALL
|
||||
.into_iter()
|
||||
.filter_map(move |k| {
|
||||
let n = self.get(k);
|
||||
(n > 0).then_some((k, n))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn save_text(self) -> String {
|
||||
let parts: Vec<String> = self.non_zero().map(|(k, n)| format!("{}={n}", k.key())).collect();
|
||||
if parts.is_empty() { "-".into() } else { parts.join(" ") }
|
||||
}
|
||||
}
|
||||
|
||||
impl Sim {
|
||||
/// Rolls random per-complex ammunition and probabilistic RM48U stock when deploying a depot.
|
||||
pub fn roll_depot_stock(&mut self, sam: &mut Sam) {
|
||||
if sam.kind != SamKind::Depot {
|
||||
return;
|
||||
}
|
||||
let (stock, rm48u) = AmmoStock::roll(&mut self.rng);
|
||||
sam.ammo_stock = stock;
|
||||
sam.sync_cargo();
|
||||
sam.ground_stock = rm48u;
|
||||
}
|
||||
|
||||
/// Prepare one explicitly typed round at a nearby living depot. Any displaced air round is retained there.
|
||||
pub fn load_ground_round(&mut self, i: usize) -> Result<String, String> {
|
||||
let s = self.sams.get(i).ok_or("Нет такого комплекса")?;
|
||||
@@ -20,10 +229,11 @@ impl Sim {
|
||||
if let Some((j, _)) = self.sams.iter().enumerate().find(|(_, d)| d.kind == SamKind::Depot && d.alive() && d.pos.distance(s.pos) < DEPOT_REACH) {
|
||||
return Err(format!("Склад №{} рядом, но RM48U: 0. Задайте запас: /airdef ground-stock {} <количество>", j + 1, j + 1));
|
||||
}
|
||||
return Err("Нет живого склада с RM48U рядом (60 блоков)".into());
|
||||
return Err(format!("Нет живого склада с RM48U рядом ({:.0} блоков)", DEPOT_REACH));
|
||||
}
|
||||
if !self.sams.iter().any(|t| t.kind == SamKind::Tzm && t.alive()) { return Err("Для доставки RM48U нужна исправная ТЗМ".into()); }
|
||||
self.sams[i].ground_request = true;
|
||||
self.sams[i].auto_ground_request = false;
|
||||
return Ok(format!("С-400 №{}: заявка RM48U ожидает доставки ТЗМ", i + 1));
|
||||
};
|
||||
let displaced = u32::from(self.sams[i].missiles + self.sams[i].ground_rounds >= self.sams[i].cells());
|
||||
@@ -31,6 +241,7 @@ impl Sim {
|
||||
self.sams[i].missiles -= displaced;
|
||||
self.sams[i].ground_rounds += 1;
|
||||
self.sams[i].ground_request = false;
|
||||
self.sams[i].auto_ground_request = false;
|
||||
if self.sams[i].ground_rounds >= self.sams[i].cells() && self.sams[i].kind.spec().gun.is_none() { self.sams[i].empty = false; }
|
||||
self.sams[depot].ground_stock -= 1;
|
||||
self.sams[depot].air_48n6_stock = air_48n6_stock;
|
||||
@@ -68,6 +279,7 @@ impl Sim {
|
||||
let s = self.sams.get_mut(i).ok_or("Нет такого комплекса")?;
|
||||
if !s.kind.supports_rm48u() || (!s.ground_request && !s.ground_return) { return Err("Нет ожидающей заявки RM48U".into()); }
|
||||
s.ground_request = false;
|
||||
s.auto_ground_request = false;
|
||||
s.ground_return = false;
|
||||
Ok(format!("С-400 №{}: заявка RM48U отменена", i + 1))
|
||||
}
|
||||
@@ -75,19 +287,43 @@ impl Sim {
|
||||
/// Reloads from depots near the systems, and the trucks' rounds.
|
||||
pub(super) fn step_supply(&mut self, out: &mut Out) {
|
||||
let now = self.tick;
|
||||
// Systems by a depot reload from it.
|
||||
// Systems by a depot reload directly from matching typed stock.
|
||||
for i in 0..self.sams.len() {
|
||||
if !self.sams[i].empty || !self.sams[i].alive() {
|
||||
let s = &self.sams[i];
|
||||
if !s.empty || !s.alive() || s.moving() || s.reloading > 0 {
|
||||
continue;
|
||||
}
|
||||
let pos = self.sams[i].pos;
|
||||
if let Some(d) = self
|
||||
.sams
|
||||
.iter()
|
||||
.position(|d| d.kind == SamKind::Depot && d.alive() && d.cargo > 0 && d.pos.distance(pos) < DEPOT_REACH)
|
||||
let pos = s.pos;
|
||||
let (need_m, need_g) = s.needed_ammo();
|
||||
let mut mask = 0u8;
|
||||
if let Some(mk) = need_m
|
||||
&& let Some(d) = self
|
||||
.sams
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, d)| d.kind == SamKind::Depot && d.alive() && d.ammo_stock.get(mk) > 0 && d.pos.distance(pos) < DEPOT_REACH)
|
||||
.min_by(|(_, a), (_, b)| a.pos.distance(pos).total_cmp(&b.pos.distance(pos)).then(a.id.cmp(&b.id)))
|
||||
.map(|(j, _)| j)
|
||||
{
|
||||
self.sams[d].cargo -= 1;
|
||||
self.reload_now(i, out);
|
||||
self.sams[d].ammo_stock.take(mk, 1);
|
||||
self.sams[d].sync_cargo();
|
||||
mask |= RELOAD_MISSILE;
|
||||
}
|
||||
if let Some(gk) = need_g
|
||||
&& let Some(d) = self
|
||||
.sams
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, d)| d.kind == SamKind::Depot && d.alive() && d.ammo_stock.get(gk) > 0 && d.pos.distance(pos) < DEPOT_REACH)
|
||||
.min_by(|(_, a), (_, b)| a.pos.distance(pos).total_cmp(&b.pos.distance(pos)).then(a.id.cmp(&b.id)))
|
||||
.map(|(j, _)| j)
|
||||
{
|
||||
self.sams[d].ammo_stock.take(gk, 1);
|
||||
self.sams[d].sync_cargo();
|
||||
mask |= RELOAD_GUN;
|
||||
}
|
||||
if mask != 0 {
|
||||
self.reload_weapons(i, mask, out);
|
||||
}
|
||||
}
|
||||
// A destroyed truck drops its job, for another to take.
|
||||
@@ -190,6 +426,7 @@ impl Sim {
|
||||
self.sams[j].ground_rounds += 1;
|
||||
if self.sams[j].ground_rounds >= self.sams[j].cells() && self.sams[j].kind.spec().gun.is_none() { self.sams[j].empty = false; }
|
||||
self.sams[j].ground_request = false;
|
||||
self.sams[j].auto_ground_request = false;
|
||||
self.sams[t].ground_stock -= 1;
|
||||
// A round reserved for return redirected to a waiting request is no
|
||||
// longer return cargo once the last one leaves the truck (#128).
|
||||
@@ -217,7 +454,10 @@ impl Sim {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// Where it was going: hand over, or load up.
|
||||
// Priority 3: typed air-defence resupply.
|
||||
let taken: Vec<u32> = self.sams.iter().filter(|s| s.kind == SamKind::Tzm && s.alive() && s.id != truck_id).filter_map(|s| s.job).collect();
|
||||
let waiting = self.waiting_needs(truck_id, at, now, &taken);
|
||||
let demand = demand_totals(&waiting);
|
||||
if let Some(job) = truck.job {
|
||||
let Some(j) = self.sams.iter().position(|s| s.id == job && s.alive()) else {
|
||||
self.sams[t].job = None;
|
||||
@@ -225,45 +465,108 @@ impl Sim {
|
||||
};
|
||||
if self.sams[j].pos.distance(at) <= HANDOVER {
|
||||
if self.sams[j].kind == SamKind::Depot {
|
||||
let take = TRUCK_LOAD.saturating_sub(self.sams[t].cargo).min(self.sams[j].cargo);
|
||||
self.sams[j].cargo -= take;
|
||||
self.sams[t].cargo += take;
|
||||
out.sound("minecraft:block.piston.extend", at, 1.5, 0.8);
|
||||
} else if self.sams[j].empty && self.sams[t].cargo > 0 {
|
||||
self.sams[t].cargo -= 1;
|
||||
self.reload_now(j, out);
|
||||
let mut moved = false;
|
||||
// Unload any cargo not needed by reachable waiting systems (or exceeding total demand).
|
||||
for kind in AmmoKind::ALL {
|
||||
let have = self.sams[t].ammo_stock.get(kind);
|
||||
let excess = have.saturating_sub(demand[kind.index()]);
|
||||
if excess > 0 {
|
||||
let put = self.sams[j].ammo_stock.add(kind, excess).unwrap_or(0);
|
||||
if put > 0 {
|
||||
self.sams[t].ammo_stock.take(kind, put);
|
||||
moved = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Load needed ammunition kinds in order of nearest waiting systems up to TRUCK_LOAD.
|
||||
'fill: for (_, _, _, needs) in &waiting {
|
||||
for kind in needs.into_iter().flatten() {
|
||||
if self.sams[t].ammo_stock.total() >= TRUCK_LOAD {
|
||||
break 'fill;
|
||||
}
|
||||
let carried = self.sams[t].ammo_stock.get(*kind);
|
||||
if carried < demand[kind.index()] && self.sams[j].ammo_stock.get(*kind) > 0 {
|
||||
let taken_n = self.sams[j].ammo_stock.take(*kind, 1);
|
||||
if taken_n > 0 {
|
||||
self.sams[t].ammo_stock.add(*kind, taken_n);
|
||||
moved = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
self.sams[j].sync_cargo();
|
||||
self.sams[t].sync_cargo();
|
||||
if moved {
|
||||
out.sound("minecraft:block.piston.extend", at, 1.5, 0.8);
|
||||
}
|
||||
} else if self.sams[j].empty && self.sams[j].alive() && !self.sams[j].moving() && self.sams[j].reloading == 0 {
|
||||
let (need_m, need_g) = self.sams[j].needed_ammo();
|
||||
let mut mask = 0u8;
|
||||
if let Some(mk) = need_m && self.sams[t].ammo_stock.take(mk, 1) > 0 {
|
||||
mask |= RELOAD_MISSILE;
|
||||
}
|
||||
if let Some(gk) = need_g && self.sams[t].ammo_stock.take(gk, 1) > 0 {
|
||||
mask |= RELOAD_GUN;
|
||||
}
|
||||
self.sams[t].sync_cargo();
|
||||
if mask != 0 {
|
||||
self.reload_weapons(j, mask, out);
|
||||
}
|
||||
}
|
||||
self.sams[t].job = None;
|
||||
} else if self.sams[j].kind == SamKind::Depot {
|
||||
let depot = &self.sams[j];
|
||||
let can_fetch = self.sams[t].ammo_stock.total() < TRUCK_LOAD
|
||||
&& AmmoKind::ALL.into_iter().any(|k| demand[k.index()] > self.sams[t].ammo_stock.get(k) && depot.ammo_stock.get(k) > 0);
|
||||
let can_drop = depot.ammo_stock.total() < STOCK_LIMIT
|
||||
&& AmmoKind::ALL.into_iter().any(|k| self.sams[t].ammo_stock.get(k) > demand[k.index()]);
|
||||
if !self.routes.out_of_reach(truck_id, depot.id, now) && (can_fetch || can_drop) {
|
||||
let to = depot.pos;
|
||||
self.order_move_near(t, to, HANDOVER - 0.75);
|
||||
continue;
|
||||
}
|
||||
self.sams[t].job = None;
|
||||
} else if self.sams[j].empty || self.sams[j].kind == SamKind::Depot {
|
||||
// Stopped short of it (it drove off meanwhile): on to where it stands now.
|
||||
let to = self.sams[j].pos;
|
||||
self.order_move_near(t, to, HANDOVER - 0.75);
|
||||
continue;
|
||||
} else {
|
||||
let target_sam = &self.sams[j];
|
||||
let (need_m, need_g) = target_sam.needed_ammo();
|
||||
let has_needed = [need_m, need_g].into_iter().flatten().any(|k| self.sams[t].ammo_stock.get(k) > 0);
|
||||
if target_sam.empty && target_sam.reloading == 0 && !target_sam.moving() && has_needed && !self.routes.out_of_reach(truck_id, target_sam.id, now) {
|
||||
// Stopped short of it (it drove off meanwhile): on to where it stands now.
|
||||
let to = target_sam.pos;
|
||||
self.order_move_near(t, to, HANDOVER - 0.75);
|
||||
continue;
|
||||
}
|
||||
self.sams[t].job = None;
|
||||
}
|
||||
}
|
||||
// A new job: the nearest waiting system no other truck is driving to, or a depot to load at.
|
||||
let taken: Vec<u32> = self.sams.iter().filter(|s| s.kind == SamKind::Tzm && s.alive()).filter_map(|s| s.job).collect();
|
||||
let next = if self.sams[t].cargo > 0 {
|
||||
self.sams
|
||||
.iter()
|
||||
.filter(|s| s.empty && s.alive() && !s.moving() && !taken.contains(&s.id) && !self.routes.out_of_reach(truck_id, s.id, now))
|
||||
.min_by(|a, b| a.pos.distance(at).total_cmp(&b.pos.distance(at)))
|
||||
.map(|s| (s.id, s.pos))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// A new job: deliver carried matching ammo first, else fetch needed ammo (or drop unneeded cargo) at a depot.
|
||||
let truck_stock = self.sams[t].ammo_stock;
|
||||
let next = waiting
|
||||
.iter()
|
||||
.find(|(_, _, _, needs)| needs.into_iter().flatten().any(|k| truck_stock.get(*k) > 0))
|
||||
.map(|&(id, pos, _, _)| (id, pos));
|
||||
let next = next.or_else(|| {
|
||||
(self.sams[t].cargo < TRUCK_LOAD)
|
||||
.then(|| {
|
||||
self.sams
|
||||
.iter()
|
||||
.filter(|d| d.kind == SamKind::Depot && d.alive() && d.cargo > 0 && !self.routes.out_of_reach(truck_id, d.id, now))
|
||||
.min_by(|a, b| a.pos.distance(at).total_cmp(&b.pos.distance(at)))
|
||||
.map(|d| (d.id, d.pos))
|
||||
})
|
||||
.flatten()
|
||||
let any_demand = demand.iter().any(|&d| d > 0);
|
||||
if truck_stock.total() < TRUCK_LOAD && any_demand {
|
||||
self.sams
|
||||
.iter()
|
||||
.filter(|d| {
|
||||
d.kind == SamKind::Depot
|
||||
&& d.alive()
|
||||
&& !self.routes.out_of_reach(truck_id, d.id, now)
|
||||
&& AmmoKind::ALL.into_iter().any(|k| demand[k.index()] > truck_stock.get(k) && d.ammo_stock.get(k) > 0)
|
||||
})
|
||||
.min_by(|a, b| a.pos.distance(at).total_cmp(&b.pos.distance(at)).then(a.id.cmp(&b.id)))
|
||||
.map(|d| (d.id, d.pos))
|
||||
} else if truck_stock.total() >= TRUCK_LOAD && AmmoKind::ALL.into_iter().any(|k| truck_stock.get(k) > demand[k.index()]) {
|
||||
self.sams
|
||||
.iter()
|
||||
.filter(|d| d.kind == SamKind::Depot && d.alive() && d.ammo_stock.total() < STOCK_LIMIT && !self.routes.out_of_reach(truck_id, d.id, now))
|
||||
.min_by(|a, b| a.pos.distance(at).total_cmp(&b.pos.distance(at)).then(a.id.cmp(&b.id)))
|
||||
.map(|d| (d.id, d.pos))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
if let Some((id, to)) = next {
|
||||
self.sams[t].job = Some(id);
|
||||
@@ -275,15 +578,321 @@ impl Sim {
|
||||
}
|
||||
}
|
||||
|
||||
/// System `i` gets a reload: its magazine fills after its reload time.
|
||||
fn reload_now(&mut self, i: usize, out: &mut Out) {
|
||||
/// Reachable waiting systems sorted by distance from `at`, with each system's needed weapon channels.
|
||||
fn waiting_needs(&self, truck_id: u32, at: DVec3, now: u64, taken: &[u32]) -> Vec<(u32, DVec3, f64, [Option<AmmoKind>; 2])> {
|
||||
let mut out: Vec<_> = self
|
||||
.sams
|
||||
.iter()
|
||||
.filter(|s| s.empty && s.alive() && !s.moving() && s.reloading == 0 && !taken.contains(&s.id) && !self.routes.out_of_reach(truck_id, s.id, now))
|
||||
.filter_map(|s| {
|
||||
let (m, g) = s.needed_ammo();
|
||||
(m.is_some() || g.is_some()).then(|| (s.id, s.pos, s.pos.distance(at), [m, g]))
|
||||
})
|
||||
.collect();
|
||||
out.sort_by(|a, b| a.2.total_cmp(&b.2).then(a.0.cmp(&b.0)));
|
||||
out
|
||||
}
|
||||
|
||||
/// System `i` begins reloading the specified weapon channels (`RELOAD_MISSILE | RELOAD_GUN`).
|
||||
fn reload_weapons(&mut self, i: usize, mask: u8, out: &mut Out) {
|
||||
let sam = &mut self.sams[i];
|
||||
sam.empty = false;
|
||||
sam.reload_mask = mask;
|
||||
sam.reloading = sam.reload_time();
|
||||
out.sound("minecraft:block.piston.extend", sam.pos, 2.0, 0.6);
|
||||
}
|
||||
}
|
||||
|
||||
/// Why an armed unit is currently waiting for ammunition or actively resupplying.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum SupplyStatus {
|
||||
Reloading { ticks_left: u32 },
|
||||
TruckEnRoute { truck_num: usize, truck_id: u32, eta_ticks: u32, rm48u: bool, returning: bool },
|
||||
TruckFetching { truck_num: usize, truck_id: u32, depot_num: usize, eta_ticks: u32, rm48u: bool },
|
||||
NoRm48uStock,
|
||||
OutOfStock { missing: [Option<AmmoKind>; 2] },
|
||||
NoTruck { rm48u: bool },
|
||||
RouteBlocked { rm48u: bool },
|
||||
WaitingForTruck { rm48u: bool, returning: bool },
|
||||
}
|
||||
|
||||
/// Aggregate ammunition & logistics state across all live depots, trucks, and batteries.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct SupplySummary {
|
||||
pub depots_alive: u32,
|
||||
pub trucks_alive: u32,
|
||||
pub trucks_busy: u32,
|
||||
pub waiting_ammo: u32,
|
||||
pub waiting_rm48u: u32,
|
||||
pub reloading: u32,
|
||||
pub rm48u_depot: u32,
|
||||
pub rm48u_truck: u32,
|
||||
pub rm48u_loaded: u32,
|
||||
pub depot_ammo: [u32; AmmoKind::COUNT],
|
||||
pub truck_ammo: [u32; AmmoKind::COUNT],
|
||||
}
|
||||
|
||||
impl SupplySummary {
|
||||
pub fn total_for(&self, kind: AmmoKind) -> u32 {
|
||||
self.depot_ammo[kind.index()] + self.truck_ammo[kind.index()]
|
||||
}
|
||||
|
||||
pub fn depot_total(&self) -> u32 {
|
||||
self.depot_ammo.iter().sum::<u32>() + self.rm48u_depot
|
||||
}
|
||||
}
|
||||
|
||||
fn truck_eta_ticks(sim: &Sim, truck: &Sam, dest: DVec3) -> u32 {
|
||||
let dist = sim.routes.remaining_distance(truck.id, truck.pos, dest);
|
||||
let speed = truck.drive_speed().max(0.05);
|
||||
let travel = (dist / speed).ceil() as u32;
|
||||
let march = if truck.state == SamState::Packing {
|
||||
truck.march
|
||||
} else if truck.state != SamState::Moving && dist > HANDOVER {
|
||||
SamKind::Tzm.march_ticks()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
march + travel
|
||||
}
|
||||
|
||||
fn fetch_eta_ticks(sim: &Sim, truck: &Sam, depot: &Sam, target: DVec3) -> u32 {
|
||||
let leg1 = sim.routes.remaining_distance(truck.id, truck.pos, depot.pos);
|
||||
let leg2 = (depot.pos.distance(target) - HANDOVER).max(0.0);
|
||||
let speed = truck.drive_speed().max(0.05);
|
||||
let march1 = if truck.state == SamState::Packing {
|
||||
truck.march
|
||||
} else if truck.state != SamState::Moving && leg1 > HANDOVER {
|
||||
SamKind::Tzm.march_ticks()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let march2 = if leg2 > 0.0 { SamKind::Tzm.march_ticks() } else { 0 };
|
||||
march1 + march2 + ((leg1 + leg2) / speed).ceil() as u32
|
||||
}
|
||||
|
||||
impl Sim {
|
||||
/// Aggregate logistics inventory and delivery queues across live units.
|
||||
pub fn supply_summary(&self) -> SupplySummary {
|
||||
let mut out = SupplySummary::default();
|
||||
for sam in &self.sams {
|
||||
if !sam.alive() {
|
||||
continue;
|
||||
}
|
||||
match sam.kind {
|
||||
SamKind::Depot => {
|
||||
out.depots_alive += 1;
|
||||
out.rm48u_depot += sam.ground_stock;
|
||||
for kind in AmmoKind::ALL {
|
||||
out.depot_ammo[kind.index()] += sam.ammo_stock.get(kind);
|
||||
}
|
||||
}
|
||||
SamKind::Tzm => {
|
||||
out.trucks_alive += 1;
|
||||
if sam.job.is_some() {
|
||||
out.trucks_busy += 1;
|
||||
}
|
||||
out.rm48u_truck += sam.ground_stock;
|
||||
for kind in AmmoKind::ALL {
|
||||
out.truck_ammo[kind.index()] += sam.ammo_stock.get(kind);
|
||||
}
|
||||
}
|
||||
kind if kind.armed() => {
|
||||
if sam.reloading > 0 {
|
||||
out.reloading += 1;
|
||||
} else if sam.empty {
|
||||
out.waiting_ammo += 1;
|
||||
}
|
||||
if sam.ground_request || sam.ground_return {
|
||||
out.waiting_rm48u += 1;
|
||||
}
|
||||
if kind.supports_rm48u() {
|
||||
out.rm48u_loaded += sam.ground_rounds;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Explains why armed unit `i` is reloading or waiting for ammunition / RM48U delivery.
|
||||
pub fn supply_status(&self, i: usize) -> Option<SupplyStatus> {
|
||||
let sam = self.sams.get(i)?;
|
||||
if !sam.alive() || !sam.kind.armed() {
|
||||
return None;
|
||||
}
|
||||
if sam.reloading > 0 {
|
||||
return Some(SupplyStatus::Reloading { ticks_left: sam.reloading });
|
||||
}
|
||||
let now = self.tick;
|
||||
let mobile_trucks = self
|
||||
.sams
|
||||
.iter()
|
||||
.filter(|t| t.kind == SamKind::Tzm && t.alive() && t.drive_speed() > 0.0)
|
||||
.count();
|
||||
|
||||
if sam.ground_return {
|
||||
if let Some((t_idx, truck)) = self
|
||||
.sams
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, t)| t.kind == SamKind::Tzm && t.alive() && t.job == Some(sam.id))
|
||||
{
|
||||
return Some(SupplyStatus::TruckEnRoute {
|
||||
truck_num: t_idx + 1,
|
||||
truck_id: truck.id,
|
||||
eta_ticks: truck_eta_ticks(self, truck, sam.pos),
|
||||
rm48u: true,
|
||||
returning: true,
|
||||
});
|
||||
}
|
||||
if mobile_trucks == 0 {
|
||||
return Some(SupplyStatus::NoTruck { rm48u: true });
|
||||
}
|
||||
let all_blocked = self
|
||||
.sams
|
||||
.iter()
|
||||
.filter(|t| t.kind == SamKind::Tzm && t.alive() && t.drive_speed() > 0.0)
|
||||
.all(|t| self.routes.out_of_reach(t.id, sam.id, now));
|
||||
return Some(if all_blocked {
|
||||
SupplyStatus::RouteBlocked { rm48u: true }
|
||||
} else {
|
||||
SupplyStatus::WaitingForTruck { rm48u: true, returning: true }
|
||||
});
|
||||
}
|
||||
|
||||
if sam.ground_request {
|
||||
if let Some((t_idx, truck)) = self
|
||||
.sams
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, t)| t.kind == SamKind::Tzm && t.alive() && t.job == Some(sam.id))
|
||||
{
|
||||
return Some(SupplyStatus::TruckEnRoute {
|
||||
truck_num: t_idx + 1,
|
||||
truck_id: truck.id,
|
||||
eta_ticks: truck_eta_ticks(self, truck, sam.pos),
|
||||
rm48u: true,
|
||||
returning: false,
|
||||
});
|
||||
}
|
||||
if let Some((t_idx, truck, d_idx, depot)) = self.sams.iter().enumerate().find_map(|(ti, t)| {
|
||||
if t.kind != SamKind::Tzm || !t.alive() || t.ground_stock > 0 {
|
||||
return None;
|
||||
}
|
||||
let job = t.job?;
|
||||
let (di, d) = self.sams.iter().enumerate().find(|(_, d)| d.id == job && d.kind == SamKind::Depot && d.alive() && d.ground_stock > 0)?;
|
||||
Some((ti, t, di, d))
|
||||
}) {
|
||||
return Some(SupplyStatus::TruckFetching {
|
||||
truck_num: t_idx + 1,
|
||||
truck_id: truck.id,
|
||||
depot_num: d_idx + 1,
|
||||
eta_ticks: fetch_eta_ticks(self, truck, depot, sam.pos),
|
||||
rm48u: true,
|
||||
});
|
||||
}
|
||||
let has_rm48u = self
|
||||
.sams
|
||||
.iter()
|
||||
.any(|s| s.alive() && matches!(s.kind, SamKind::Depot | SamKind::Tzm) && s.ground_stock > 0);
|
||||
if !has_rm48u {
|
||||
return Some(SupplyStatus::NoRm48uStock);
|
||||
}
|
||||
if mobile_trucks == 0 {
|
||||
return Some(SupplyStatus::NoTruck { rm48u: true });
|
||||
}
|
||||
let all_blocked = self
|
||||
.sams
|
||||
.iter()
|
||||
.filter(|t| t.kind == SamKind::Tzm && t.alive() && t.drive_speed() > 0.0)
|
||||
.all(|t| self.routes.out_of_reach(t.id, sam.id, now));
|
||||
return Some(if all_blocked {
|
||||
SupplyStatus::RouteBlocked { rm48u: true }
|
||||
} else {
|
||||
SupplyStatus::WaitingForTruck { rm48u: true, returning: false }
|
||||
});
|
||||
}
|
||||
|
||||
if sam.empty {
|
||||
let (need_m, need_g) = sam.needed_ammo();
|
||||
let needs = [need_m, need_g];
|
||||
if need_m.is_none() && need_g.is_none() {
|
||||
return None;
|
||||
}
|
||||
if let Some((t_idx, truck)) = self
|
||||
.sams
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, t)| t.kind == SamKind::Tzm && t.alive() && t.job == Some(sam.id))
|
||||
{
|
||||
return Some(SupplyStatus::TruckEnRoute {
|
||||
truck_num: t_idx + 1,
|
||||
truck_id: truck.id,
|
||||
eta_ticks: truck_eta_ticks(self, truck, sam.pos),
|
||||
rm48u: false,
|
||||
returning: false,
|
||||
});
|
||||
}
|
||||
if let Some((t_idx, truck, d_idx, depot)) = self.sams.iter().enumerate().find_map(|(ti, t)| {
|
||||
if t.kind != SamKind::Tzm || !t.alive() || t.ammo_stock.total() >= TRUCK_LOAD {
|
||||
return None;
|
||||
}
|
||||
let job = t.job?;
|
||||
let (di, d) = self.sams.iter().enumerate().find(|(_, d)| {
|
||||
d.id == job
|
||||
&& d.kind == SamKind::Depot
|
||||
&& d.alive()
|
||||
&& needs.into_iter().flatten().any(|k| d.ammo_stock.get(k) > 0)
|
||||
})?;
|
||||
Some((ti, t, di, d))
|
||||
}) {
|
||||
return Some(SupplyStatus::TruckFetching {
|
||||
truck_num: t_idx + 1,
|
||||
truck_id: truck.id,
|
||||
depot_num: d_idx + 1,
|
||||
eta_ticks: fetch_eta_ticks(self, truck, depot, sam.pos),
|
||||
rm48u: false,
|
||||
});
|
||||
}
|
||||
let has_stock = self.sams.iter().any(|s| {
|
||||
s.alive()
|
||||
&& matches!(s.kind, SamKind::Depot | SamKind::Tzm)
|
||||
&& needs.into_iter().flatten().any(|k| s.ammo_stock.get(k) > 0)
|
||||
});
|
||||
if !has_stock {
|
||||
return Some(SupplyStatus::OutOfStock { missing: needs });
|
||||
}
|
||||
if mobile_trucks == 0 {
|
||||
return Some(SupplyStatus::NoTruck { rm48u: false });
|
||||
}
|
||||
let all_blocked = self
|
||||
.sams
|
||||
.iter()
|
||||
.filter(|t| t.kind == SamKind::Tzm && t.alive() && t.drive_speed() > 0.0)
|
||||
.all(|t| self.routes.out_of_reach(t.id, sam.id, now));
|
||||
return Some(if all_blocked {
|
||||
SupplyStatus::RouteBlocked { rm48u: false }
|
||||
} else {
|
||||
SupplyStatus::WaitingForTruck { rm48u: false, returning: false }
|
||||
});
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn demand_totals(waiting: &[(u32, DVec3, f64, [Option<AmmoKind>; 2])]) -> [u32; AmmoKind::COUNT] {
|
||||
let mut totals = [0u32; AmmoKind::COUNT];
|
||||
for (_, _, _, needs) in waiting {
|
||||
for kind in needs.into_iter().flatten() {
|
||||
totals[kind.index()] += 1;
|
||||
}
|
||||
}
|
||||
totals
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -578,4 +1187,152 @@ mod tests {
|
||||
assert_eq!(sim.sams[depot].ground_stock + sim.sams[truck].ground_stock + sim.sams[a].ground_rounds + sim.sams[b].ground_rounds, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn depot_spawn_roll_varies_by_system_weapon_and_rolls_rm48u_with_chance() {
|
||||
let mut saw_rm48u = false;
|
||||
let mut saw_zero_rm48u = false;
|
||||
let mut saw_diff_pantsir_channels = false;
|
||||
for seed in 1..=100u64 {
|
||||
let mut sim = Sim::new(seed);
|
||||
let mut depot = Sam::new(1, SamKind::Depot, DVec3::ZERO, 0.0);
|
||||
sim.roll_depot_stock(&mut depot);
|
||||
for kind in AmmoKind::ALL {
|
||||
let (lo, hi) = kind.spawn_range();
|
||||
let n = depot.ammo_stock.get(kind);
|
||||
assert!((lo..=hi).contains(&n), "seed {seed} {kind:?}: {n} not in {lo}..={hi}");
|
||||
}
|
||||
if depot.ammo_stock.get(AmmoKind::PantsirMissile) != depot.ammo_stock.get(AmmoKind::PantsirGun) {
|
||||
saw_diff_pantsir_channels = true;
|
||||
}
|
||||
assert!(depot.ground_stock <= 2);
|
||||
if depot.ground_stock > 0 {
|
||||
saw_rm48u = true;
|
||||
} else {
|
||||
saw_zero_rm48u = true;
|
||||
}
|
||||
assert_eq!(depot.cargo, depot.ammo_stock.total());
|
||||
}
|
||||
assert!(saw_rm48u && saw_zero_rm48u, "RM48U should appear probabilistically across spawns");
|
||||
assert!(saw_diff_pantsir_channels, "Pantsir missile and gun stocks should vary independently");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn depot_only_reloads_matching_system_and_weapon_channel() {
|
||||
let mut sim = Sim::new(1);
|
||||
for (kind, x) in [(SamKind::Depot, 0.0), (SamKind::S400, 10.0), (SamKind::Pantsir, 12.0), (SamKind::Patriot, 25.0)] {
|
||||
let id = sim.id();
|
||||
sim.sams.push(Sam::new(id, kind, DVec3::new(x, 70.0, 0.0), 0.0));
|
||||
}
|
||||
sim.sams[0].clear_ammo();
|
||||
sim.sams[0].set_ammo(AmmoKind::PatriotMissile, 2);
|
||||
sim.sams[0].set_ammo(AmmoKind::PantsirMissile, 1);
|
||||
sim.sams[1].missiles = 0;
|
||||
sim.sams[1].empty = true;
|
||||
sim.sams[2].missiles = 0;
|
||||
sim.sams[2].bursts = 0;
|
||||
sim.sams[2].empty = true;
|
||||
sim.sams[3].missiles = 0;
|
||||
sim.sams[3].empty = true;
|
||||
sim.step_supply(&mut Out::default());
|
||||
assert_eq!(sim.sams[1].reloading, 0, "S-400 must not reload from Patriot stock");
|
||||
assert!(sim.sams[1].empty);
|
||||
assert_eq!(sim.sams[3].reloading, 0, "Patriot beyond DEPOT_REACH (25 > 16) must not auto-reload directly");
|
||||
assert!(sim.sams[3].empty);
|
||||
assert!(sim.sams[2].reloading > 0, "Pantsir must start reloading missiles");
|
||||
assert_eq!(sim.sams[0].ammo_stock.get(AmmoKind::PantsirMissile), 0);
|
||||
assert_eq!(sim.sams[0].ammo_stock.get(AmmoKind::PatriotMissile), 2);
|
||||
// Simulate reload completion in step_sams: only missiles are restored, guns remain 0.
|
||||
sim.sams[2].reloading = 1;
|
||||
sim.step(&mut Ground::default(), None, &mut Out::default());
|
||||
assert_eq!(sim.sams[2].missiles, 12);
|
||||
assert_eq!(sim.sams[2].bursts, 0);
|
||||
// After step_sams detects bursts == 0, Pantsir marks empty again and waits for PantsirGun.
|
||||
assert!(sim.sams[2].empty);
|
||||
sim.sams[0].set_ammo(AmmoKind::PantsirGun, 1);
|
||||
sim.step_supply(&mut Out::default());
|
||||
assert!(sim.sams[2].reloading > 0, "Pantsir must now reload gun channel");
|
||||
sim.sams[2].reloading = 1;
|
||||
sim.step(&mut Ground::default(), None, &mut Out::default());
|
||||
assert_eq!(sim.sams[2].bursts, 140);
|
||||
assert_eq!(sim.sams[0].ammo_stock.get(AmmoKind::PantsirGun), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tzm_fetches_matching_ammo_and_drops_unneeded_cargo() {
|
||||
let mut sim = Sim::new(4);
|
||||
for (kind, x) in [(SamKind::Depot, 0.0), (SamKind::Tor, 160.0), (SamKind::Tzm, 0.0)] {
|
||||
let id = sim.id();
|
||||
sim.sams.push(Sam::new(id, kind, DVec3::new(x, 70.0, 0.0), 0.0));
|
||||
}
|
||||
sim.sams[0].clear_ammo();
|
||||
sim.sams[0].set_ammo(AmmoKind::TorMissile, 1);
|
||||
sim.sams[1].missiles = 0;
|
||||
sim.sams[1].empty = true;
|
||||
// Full TZM carrying unneeded PatriotMissile must drop it off at the depot and pick up TorMissile.
|
||||
sim.sams[2].set_ammo(AmmoKind::PatriotMissile, 2);
|
||||
sim.tick = 1;
|
||||
serve_until(&mut sim, 2, |s| s.sams[1].reloading > 0);
|
||||
assert_eq!(sim.sams[0].ammo_stock.get(AmmoKind::PatriotMissile), 2);
|
||||
assert_eq!(sim.sams[0].ammo_stock.get(AmmoKind::TorMissile), 0);
|
||||
assert_eq!(sim.sams[2].ammo_stock.total(), 0);
|
||||
assert!(!sim.sams[1].empty);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn supply_summary_and_status_diagnose_all_wait_and_delivery_states() {
|
||||
let mut sim = Sim::new(1);
|
||||
for (kind, x) in [(SamKind::Depot, 0.0), (SamKind::Tor, 160.0), (SamKind::S400, 200.0), (SamKind::Tzm, 40.0)] {
|
||||
let id = sim.id();
|
||||
sim.sams.push(Sam::new(id, kind, DVec3::new(x, 70.0, 0.0), 0.0));
|
||||
}
|
||||
sim.sams[0].clear_ammo();
|
||||
sim.sams[0].ground_stock = 1;
|
||||
sim.sams[1].missiles = 0;
|
||||
sim.sams[1].empty = true;
|
||||
sim.sams[2].ground_request = true;
|
||||
|
||||
assert_eq!(sim.supply_status(0), None);
|
||||
assert_eq!(sim.supply_status(3), None);
|
||||
assert_eq!(sim.supply_status(1), Some(SupplyStatus::OutOfStock { missing: [Some(AmmoKind::TorMissile), None] }));
|
||||
|
||||
sim.sams[0].set_ammo(AmmoKind::TorMissile, 2);
|
||||
sim.sams[0].ground_stock = 0;
|
||||
assert_eq!(sim.supply_status(2), Some(SupplyStatus::NoRm48uStock));
|
||||
|
||||
sim.sams[0].ground_stock = 1;
|
||||
sim.sams[3].job = Some(sim.sams[0].id);
|
||||
match sim.supply_status(2) {
|
||||
Some(SupplyStatus::TruckFetching { truck_num: 4, depot_num: 1, eta_ticks, rm48u: true, .. }) => assert!(eta_ticks > 0),
|
||||
other => panic!("expected RM48U fetch status, got {other:?}"),
|
||||
}
|
||||
|
||||
sim.sams[3].ground_stock = 1;
|
||||
sim.sams[3].job = Some(sim.sams[2].id);
|
||||
match sim.supply_status(2) {
|
||||
Some(SupplyStatus::TruckEnRoute { truck_num: 4, eta_ticks, rm48u: true, returning: false, .. }) => assert!(eta_ticks > 0),
|
||||
other => panic!("expected RM48U delivery status, got {other:?}"),
|
||||
}
|
||||
|
||||
sim.sams[2].ground_request = false;
|
||||
sim.sams[3].job = Some(sim.sams[0].id);
|
||||
match sim.supply_status(1) {
|
||||
Some(SupplyStatus::TruckFetching { truck_num: 4, depot_num: 1, eta_ticks, rm48u: false, .. }) => assert!(eta_ticks > 0),
|
||||
other => panic!("expected Tor fetch status, got {other:?}"),
|
||||
}
|
||||
|
||||
sim.sams[3].job = None;
|
||||
assert_eq!(sim.supply_status(1), Some(SupplyStatus::WaitingForTruck { rm48u: false, returning: false }));
|
||||
sim.routes.unreachable.insert((sim.sams[3].id, sim.sams[1].id), 100);
|
||||
assert_eq!(sim.supply_status(1), Some(SupplyStatus::RouteBlocked { rm48u: false }));
|
||||
sim.sams[3].comps.0[Part::Tracks as usize] = 0.0;
|
||||
assert_eq!(sim.supply_status(1), Some(SupplyStatus::NoTruck { rm48u: false }));
|
||||
sim.sams[1].reloading = 42;
|
||||
assert_eq!(sim.supply_status(1), Some(SupplyStatus::Reloading { ticks_left: 42 }));
|
||||
|
||||
let summary = sim.supply_summary();
|
||||
assert_eq!((summary.depots_alive, summary.trucks_alive, summary.trucks_busy), (1, 1, 0));
|
||||
assert_eq!((summary.rm48u_depot, summary.rm48u_truck, summary.reloading), (1, 1, 1));
|
||||
assert_eq!(summary.total_for(AmmoKind::TorMissile), 2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -34,6 +34,9 @@ pub use observe::Observation;
|
||||
pub use recon::{Category, Mark, Quality, Source, is_drone_id, priority_parts};
|
||||
pub mod lights;
|
||||
mod logistics;
|
||||
pub use logistics::{AmmoKind, AmmoStock, DEPOT_RM48U_CHANCE, SupplyStatus, SupplySummary};
|
||||
pub const TRUCK_CAPACITY: u32 = TRUCK_LOAD;
|
||||
pub const DEPOT_REACH_BLOCKS: f64 = DEPOT_REACH;
|
||||
mod orders;
|
||||
mod route;
|
||||
mod voices;
|
||||
@@ -1254,6 +1257,12 @@ pub struct Sam {
|
||||
march: u32,
|
||||
/// A depot's reloads in stock; a reload truck's on board.
|
||||
pub cargo: u32,
|
||||
/// Typed air-defence ammunition stock per system and weapon channel (depots and TZMs).
|
||||
pub ammo_stock: AmmoStock,
|
||||
/// Pending automatic RM48U request originated by auto-strike.
|
||||
pub auto_ground_request: bool,
|
||||
/// Bitmask of weapon channels being reloaded (`RELOAD_MISSILE | RELOAD_GUN`).
|
||||
pub(super) reload_mask: u8,
|
||||
/// A reload truck's job: the system it is driving to, or a depot.
|
||||
job: Option<u32>,
|
||||
/// Out of ammunition, waiting for a resupply.
|
||||
@@ -1335,10 +1344,15 @@ impl Sam {
|
||||
dest: None,
|
||||
march: 0,
|
||||
cargo: match kind {
|
||||
SamKind::Depot => 30,
|
||||
SamKind::Tzm => 2,
|
||||
SamKind::Depot => AmmoStock::new_default_depot().total(),
|
||||
_ => 0,
|
||||
},
|
||||
ammo_stock: match kind {
|
||||
SamKind::Depot => AmmoStock::new_default_depot(),
|
||||
_ => AmmoStock::default(),
|
||||
},
|
||||
auto_ground_request: false,
|
||||
reload_mask: 0,
|
||||
job: None,
|
||||
empty: false,
|
||||
ew_on: false,
|
||||
@@ -1380,6 +1394,49 @@ impl Sam {
|
||||
pub fn preparing_cell(&self) -> Option<u32> {
|
||||
self.preparing.map(|p| p.cell)
|
||||
}
|
||||
/// Keeps legacy `cargo` synchronized with `ammo_stock.total()`.
|
||||
pub fn sync_cargo(&mut self) {
|
||||
self.cargo = self.ammo_stock.total();
|
||||
}
|
||||
/// Sets the count of a specific typed air-defence ammunition kind and updates `cargo`.
|
||||
pub fn set_ammo(&mut self, kind: AmmoKind, count: u32) {
|
||||
self.ammo_stock.set(kind, count);
|
||||
self.sync_cargo();
|
||||
}
|
||||
/// Clears all typed air-defence ammunition and sets `cargo` to 0.
|
||||
pub fn clear_ammo(&mut self) {
|
||||
self.ammo_stock = AmmoStock::default();
|
||||
self.cargo = 0;
|
||||
}
|
||||
/// Weapon channels that currently need resupply (`(missile_ammo, gun_ammo)`).
|
||||
pub fn needed_ammo(&self) -> (Option<AmmoKind>, Option<AmmoKind>) {
|
||||
let missile = self
|
||||
.kind
|
||||
.missile_ammo()
|
||||
.filter(|_| self.missiles == 0 && self.part(Part::Launcher) > 0.0 && self.cells() > self.ground_rounds);
|
||||
let gun = self.kind.gun_ammo().filter(|_| self.bursts == 0);
|
||||
(missile, gun)
|
||||
}
|
||||
/// Infers which weapon channels were reloading when loading a save without `reload_mask`.
|
||||
pub fn infer_reload_mask(&mut self) {
|
||||
if self.reloading == 0 {
|
||||
self.reload_mask = 0;
|
||||
return;
|
||||
}
|
||||
let spec = self.kind.spec();
|
||||
let mut mask = 0u8;
|
||||
if spec.missile.is_some() && self.missiles == 0 {
|
||||
mask |= logistics::RELOAD_MISSILE;
|
||||
}
|
||||
if spec.gun.is_some() && self.bursts == 0 {
|
||||
mask |= logistics::RELOAD_GUN;
|
||||
}
|
||||
self.reload_mask = if mask == 0 {
|
||||
logistics::RELOAD_MISSILE | logistics::RELOAD_GUN
|
||||
} else {
|
||||
mask
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- effects
|
||||
@@ -1450,6 +1507,8 @@ pub struct Sim {
|
||||
pub hold: bool,
|
||||
/// Last chance (`/airdef lastchance`): a system fires at a class outside its roles when it is about to hit.
|
||||
pub last_chance: bool,
|
||||
/// Automatic destruction of fixed enemy launch sites by idle S-400s (`/airdef autostrike`).
|
||||
pub auto_strike: bool,
|
||||
/// The heavy systems take long shots on the big radar's cue (a switch for the balance tests).
|
||||
pub far_shots: bool,
|
||||
/// What the land hides round each system's radar, by system id.
|
||||
@@ -1561,6 +1620,7 @@ impl Sim {
|
||||
shader: false,
|
||||
hold: false,
|
||||
last_chance: true,
|
||||
auto_strike: false,
|
||||
far_shots: true,
|
||||
horizons: HashMap::new(),
|
||||
later: Vec::new(),
|
||||
@@ -1772,6 +1832,7 @@ impl Sim {
|
||||
profile::time(profile::SAMS, || self.step_sams(ground, out));
|
||||
self.step_missiles(ground, out);
|
||||
self.step_strikes(ground, player, out);
|
||||
self.step_auto_strike(out);
|
||||
self.step_fx(ground, out);
|
||||
self.draw_targets(out);
|
||||
self.draw_sites(out);
|
||||
@@ -2453,8 +2514,18 @@ impl Sim {
|
||||
if sam.reloading > 0 {
|
||||
sam.reloading -= 1;
|
||||
if sam.reloading == 0 {
|
||||
sam.missiles = sam.cells().saturating_sub(sam.ground_rounds);
|
||||
sam.bursts = spec.gun.as_ref().map_or(0, |g| g.bursts);
|
||||
let mask = if sam.reload_mask == 0 {
|
||||
logistics::RELOAD_MISSILE | logistics::RELOAD_GUN
|
||||
} else {
|
||||
sam.reload_mask
|
||||
};
|
||||
if mask & logistics::RELOAD_MISSILE != 0 {
|
||||
sam.missiles = sam.cells().saturating_sub(sam.ground_rounds);
|
||||
}
|
||||
if mask & logistics::RELOAD_GUN != 0 {
|
||||
sam.bursts = spec.gun.as_ref().map_or(0, |g| g.bursts);
|
||||
}
|
||||
sam.reload_mask = 0;
|
||||
out.sound("minecraft:block.piston.extend", sam.pos, 2.0, 0.6);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
use super::*;
|
||||
|
||||
/// Blocks a tick a vehicle drives (about 25 km/h).
|
||||
const SPEED: f64 = 0.35;
|
||||
pub(super) const SPEED: f64 = 0.35;
|
||||
|
||||
impl SamKind {
|
||||
/// Ticks to pack up for the march, and to deploy again.
|
||||
|
||||
@@ -28,7 +28,7 @@ fn dead_truck_must_not_reserve_a_waiting_system() {
|
||||
let depot=sim.id(); sim.sams.push(Sam::new(depot,SamKind::Depot,DVec3::new(500.,70.,0.),0.));
|
||||
let target=sim.id(); let mut s=Sam::new(target,SamKind::Tor,DVec3::new(0.,70.,0.),0.);s.empty=true;s.missiles=0;sim.sams.push(s);
|
||||
let dead=sim.id();let mut s=Sam::new(dead,SamKind::Tzm,DVec3::new(100.,70.,0.),0.);s.job=Some(target);s.health=0.;sim.sams.push(s);
|
||||
let live=sim.id();sim.sams.push(Sam::new(live,SamKind::Tzm,DVec3::new(30.,70.,0.),0.));
|
||||
let live=sim.id();let mut s=Sam::new(live,SamKind::Tzm,DVec3::new(30.,70.,0.),0.);s.set_ammo(AmmoKind::TorMissile,1);sim.sams.push(s);
|
||||
sim.tick=3;sim.step_supply(&mut Out::default());
|
||||
assert_eq!(sim.sams[3].job,Some(target),"a live loaded truck must take the waiting system");
|
||||
}
|
||||
@@ -39,7 +39,7 @@ fn truck_must_follow_a_target_that_moved_away() {
|
||||
let mut sim=Sim::new(1);
|
||||
let depot=sim.id();sim.sams.push(Sam::new(depot,SamKind::Depot,DVec3::new(500.,70.,0.),0.));
|
||||
let target=sim.id();let mut s=Sam::new(target,SamKind::Tor,DVec3::new(100.,70.,0.),0.);s.empty=true;s.missiles=0;sim.sams.push(s);
|
||||
let truck=sim.id();let mut s=Sam::new(truck,SamKind::Tzm,DVec3::new(0.,70.,0.),0.);s.job=Some(target);sim.sams.push(s);
|
||||
let truck=sim.id();let mut s=Sam::new(truck,SamKind::Tzm,DVec3::new(0.,70.,0.),0.);s.job=Some(target);s.set_ammo(AmmoKind::TorMissile,1);sim.sams.push(s);
|
||||
sim.tick=2;sim.step_supply(&mut Out::default());
|
||||
assert!(sim.sams[2].moving(),"truck at obsolete rendezvous must get a new move order");
|
||||
}
|
||||
|
||||
@@ -228,6 +228,19 @@ pub(super) struct Route {
|
||||
}
|
||||
|
||||
impl Route {
|
||||
/// Horizontal path distance remaining from `pos` to the route's goal.
|
||||
pub(super) fn remaining_distance(&self, pos: DVec3) -> f64 {
|
||||
if self.next >= self.pts.len() {
|
||||
return (horizontal(self.dest - pos).length() - self.slack).max(0.0);
|
||||
}
|
||||
let mut total = 0.0;
|
||||
let mut cur = pos;
|
||||
for &pt in &self.pts[self.next..] {
|
||||
total += horizontal(pt - cur).length();
|
||||
cur = pt;
|
||||
}
|
||||
total
|
||||
}
|
||||
pub(super) fn new(pos: DVec3, dest: DVec3, slack: f64, kind: SamKind, now: u64) -> Self {
|
||||
let dist = horizontal(dest - pos).length();
|
||||
let cell = if dist <= 400.0 { 4 } else if dist <= 900.0 { 6 } else { 8 };
|
||||
@@ -739,6 +752,9 @@ impl Routes {
|
||||
pub(super) fn out_of_reach(&self, truck: u32, job: u32, now: u64) -> bool {
|
||||
self.unreachable.get(&(truck, job)).is_some_and(|&t| t > now)
|
||||
}
|
||||
pub(super) fn remaining_distance(&self, unit_id: u32, pos: DVec3, dest: DVec3) -> f64 {
|
||||
self.map.get(&unit_id).map_or_else(|| (horizontal(dest - pos).length() - HANDOVER).max(0.0), |r| r.remaining_distance(pos))
|
||||
}
|
||||
}
|
||||
|
||||
impl SamKind {
|
||||
|
||||
@@ -13,7 +13,7 @@ const GRAVITY: f64 = 0.05;
|
||||
/// Height over the target where the Oreshnik's blocks release their submunitions.
|
||||
const BLOCK_SPLIT: f64 = 300.0;
|
||||
/// How near a depot a system reloads straight from it.
|
||||
const DEPOT_REACH: f64 = 60.0;
|
||||
const DEPOT_REACH: f64 = 16.0;
|
||||
/// How near a truck must come to hand a reload over, or to load up at a depot.
|
||||
const HANDOVER: f64 = 10.0;
|
||||
/// What a truck carries.
|
||||
|
||||
@@ -736,14 +736,237 @@ impl Sim {
|
||||
.then(a.total_cmp(b)).then(self.sams[*i].id.cmp(&self.sams[*j].id))
|
||||
})
|
||||
.ok_or("Нет готового С-400 с загруженной RM48U в игровой дальности; подготовьте боезапас у склада")?;
|
||||
let sam = &mut self.sams[i];
|
||||
self.prepare_strike_from(i, id, est.pos);
|
||||
Ok(format!("С-400 №{} готовит RM48U по ПУ №{id}", i + 1))
|
||||
}
|
||||
|
||||
fn prepare_strike_from(&mut self, sam_index: usize, site_id: u32, aim: DVec2) {
|
||||
let sam = &mut self.sams[sam_index];
|
||||
sam.sync_tube_cells();
|
||||
let cell = sam.next_tube_cell();
|
||||
sam.preparing = Some(launch::Prepare { target: id, cell, start: self.tick, ground: true });
|
||||
sam.preparing = Some(launch::Prepare { target: site_id, cell, start: self.tick, ground: true });
|
||||
sam.cap_cell = cell as u8;
|
||||
sam.cap_open = 0.0;
|
||||
self.strikes.pending.push(PendingStrike { carrier: sam.id, site: id, aim: est.pos });
|
||||
Ok(format!("С-400 №{} готовит RM48U по ПУ №{id}", i + 1))
|
||||
self.strikes.pending.push(PendingStrike { carrier: sam.id, site: site_id, aim });
|
||||
}
|
||||
|
||||
/// True while a raid is active (planning/queued/drones airborne).
|
||||
pub fn raid_in_progress(&self) -> bool {
|
||||
self.wave.is_some() || !self.queue.is_empty() || self.drones.iter().any(|d| d.alive())
|
||||
}
|
||||
|
||||
fn has_foreseen_air_task(&self, sam: &Sam) -> bool {
|
||||
const AUTO_STRIKE_LOOKAHEAD_TICKS: f64 = 240.0;
|
||||
if sam.target.is_some() || sam.gun_target.is_some() || self.missiles.iter().any(|m| m.sam == sam.id) {
|
||||
return true;
|
||||
}
|
||||
let Some(load) = sam.kind.spec().missile.as_ref() else {
|
||||
return false;
|
||||
};
|
||||
let engage_reach = if self.far_shots { load.max * why::FAR_K } else { load.max };
|
||||
for tr in &sam.tracks {
|
||||
if tr.age(self.tick) > sight::MISSILE_AGE {
|
||||
continue;
|
||||
}
|
||||
let Some(d) = self.drones.iter().find(|d| d.id == tr.id && d.alive()) else {
|
||||
continue;
|
||||
};
|
||||
let in_role = tr.class.in_roles(sam.roles) && sam.doctrine.area_blocks(sam.pos, tr, self.tick) == 0;
|
||||
let urgent = self.last_chance && self.last_chance_for(sam, tr.class, d, 0);
|
||||
if !in_role && !urgent {
|
||||
continue;
|
||||
}
|
||||
let est = tr.est(self.tick);
|
||||
let p0 = est - sam.pos;
|
||||
let dist = p0.length();
|
||||
if dist <= load.max * 1.15 {
|
||||
return true;
|
||||
}
|
||||
let v2 = tr.vel.length_squared();
|
||||
let t = if v2 > 1e-6 { (-p0.dot(tr.vel) / v2).clamp(0.0, AUTO_STRIKE_LOOKAHEAD_TICKS) } else { 0.0 };
|
||||
if (p0 + tr.vel * t).length() <= load.max * 1.1 {
|
||||
return true;
|
||||
}
|
||||
if self.far_shots && dist <= engage_reach && tr.vel.dot((-p0).normalize_or_zero()) > 0.1 {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn s400_free_for_auto_strike(&self, i: usize) -> bool {
|
||||
let sam = &self.sams[i];
|
||||
sam.kind.supports_rm48u()
|
||||
&& sam.alive()
|
||||
&& !sam.moving()
|
||||
&& sam.reloading == 0
|
||||
&& sam.cooldown == 0
|
||||
&& sam.part(Part::Launcher) > 0.0
|
||||
&& sam.preparing.is_none()
|
||||
&& !sam.ground_return
|
||||
&& !self.has_foreseen_air_task(sam)
|
||||
}
|
||||
|
||||
fn cancel_auto_ground_requests(&mut self) {
|
||||
for s in &mut self.sams {
|
||||
if s.auto_ground_request {
|
||||
s.ground_request = false;
|
||||
s.auto_ground_request = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Automatic counter-battery: idle S-400s without foreseen air tasks request RM48U and strike fixed sites.
|
||||
pub(super) fn step_auto_strike(&mut self, out: &mut Out) {
|
||||
if !self.auto_strike || self.hold || !self.raid_in_progress() {
|
||||
self.cancel_auto_ground_requests();
|
||||
return;
|
||||
}
|
||||
if self.tick % 10 != 0 {
|
||||
return;
|
||||
}
|
||||
let mut uncovered: Vec<(u32, SiteClass, SiteState, Estimate)> = self
|
||||
.strikes
|
||||
.known
|
||||
.iter()
|
||||
.filter_map(|k| {
|
||||
let est = k.est.filter(|e| e.fix)?;
|
||||
if matches!(k.state, SiteState::Destroyed(_)) {
|
||||
return None;
|
||||
}
|
||||
if !self.sites.get(k.site).is_some_and(|s| s.alive) {
|
||||
return None;
|
||||
}
|
||||
if self.strikes.flights.iter().any(|f| f.site == k.site) || self.strikes.pending.iter().any(|p| p.site == k.site) {
|
||||
return None;
|
||||
}
|
||||
Some((k.site, k.class, k.state, est))
|
||||
})
|
||||
.collect();
|
||||
if uncovered.is_empty() {
|
||||
self.cancel_auto_ground_requests();
|
||||
return;
|
||||
}
|
||||
let threat_rank = |c: SiteClass| match c {
|
||||
SiteClass::Battery => 3u8,
|
||||
SiteClass::Launcher => 2,
|
||||
SiteClass::Ramp => 1,
|
||||
};
|
||||
let pick_best_site = |list: &[(u32, SiteClass, SiteState, Estimate)], sam_pos: DVec3| -> Option<usize> {
|
||||
let sam_xz = DVec2::new(sam_pos.x, sam_pos.z);
|
||||
list.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, (_, _, _, est))| (est.pos - sam_xz).length() <= TUNE.range)
|
||||
.min_by(|(_, (id_a, class_a, st_a, est_a)), (_, (id_b, class_b, st_b, est_b))| {
|
||||
let active_a = matches!(st_a, SiteState::Active);
|
||||
let active_b = matches!(st_b, SiteState::Active);
|
||||
let ok_a = est_a.radius <= TUNE.rough;
|
||||
let ok_b = est_b.radius <= TUNE.rough;
|
||||
active_b
|
||||
.cmp(&active_a)
|
||||
.then(ok_b.cmp(&ok_a))
|
||||
.then(threat_rank(*class_b).cmp(&threat_rank(*class_a)))
|
||||
.then(est_a.radius.total_cmp(&est_b.radius))
|
||||
.then((est_a.pos - sam_xz).length().total_cmp(&(est_b.pos - sam_xz).length()))
|
||||
.then(id_a.cmp(id_b))
|
||||
})
|
||||
.map(|(idx, _)| idx)
|
||||
};
|
||||
|
||||
// Phase 1: Fire already-loaded RM48U on free S-400 launchers.
|
||||
for i in 0..self.sams.len() {
|
||||
if !self.s400_free_for_auto_strike(i) {
|
||||
continue;
|
||||
}
|
||||
let s = &self.sams[i];
|
||||
if s.ground_rounds <= u32::from(s.ground_return) {
|
||||
continue;
|
||||
}
|
||||
if let Some(pos_idx) = pick_best_site(&uncovered, s.pos) {
|
||||
let (site_id, _, _, est) = uncovered.remove(pos_idx);
|
||||
self.prepare_strike_from(i, site_id, est.pos);
|
||||
out.messages.push(format!("Автоудар: С-400 №{} готовит RM48U по ПУ №{site_id}", i + 1));
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 2: Order RM48U delivery (or load directly from nearby depot and fire) for remaining uncovered sites.
|
||||
let mut pending_requests = self
|
||||
.sams
|
||||
.iter()
|
||||
.filter(|s| s.alive() && s.kind.supports_rm48u() && s.ground_request)
|
||||
.count();
|
||||
let mut available_rm48u: u32 = self
|
||||
.sams
|
||||
.iter()
|
||||
.filter(|s| s.alive() && matches!(s.kind, SamKind::Depot | SamKind::Tzm))
|
||||
.map(|s| s.ground_stock)
|
||||
.sum();
|
||||
while !uncovered.is_empty() && pending_requests < uncovered.len() && (pending_requests as u32) < available_rm48u {
|
||||
let best_sam = (0..self.sams.len())
|
||||
.filter_map(|i| {
|
||||
if !self.s400_free_for_auto_strike(i) {
|
||||
return None;
|
||||
}
|
||||
let sam = &self.sams[i];
|
||||
if sam.ground_rounds != 0 || sam.ground_request || sam.ground_return || sam.cells() == 0 {
|
||||
return None;
|
||||
}
|
||||
if sam.missiles + sam.ground_rounds >= sam.cells() && sam.doctrine.reserve > 0 && sam.missiles <= sam.doctrine.reserve {
|
||||
return None;
|
||||
}
|
||||
pick_best_site(&uncovered, sam.pos)?;
|
||||
let direct_depot = self
|
||||
.sams
|
||||
.iter()
|
||||
.any(|d| d.kind == SamKind::Depot && d.alive() && d.ground_stock > 0 && d.pos.distance(sam.pos) < DEPOT_REACH);
|
||||
if !direct_depot {
|
||||
let reachable_tzm = self.sams.iter().any(|t| {
|
||||
t.kind == SamKind::Tzm
|
||||
&& t.alive()
|
||||
&& t.drive_speed() > 0.0
|
||||
&& !self.routes.out_of_reach(t.id, sam.id, self.tick)
|
||||
});
|
||||
if !reachable_tzm {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
let min_dist = self
|
||||
.sams
|
||||
.iter()
|
||||
.filter(|d| d.alive() && matches!(d.kind, SamKind::Depot | SamKind::Tzm) && d.ground_stock > 0)
|
||||
.map(|d| d.pos.distance(sam.pos))
|
||||
.min_by(f64::total_cmp)
|
||||
.unwrap_or(f64::INFINITY);
|
||||
Some((i, direct_depot, min_dist, sam.missiles, sam.id))
|
||||
})
|
||||
.min_by(|a, b| {
|
||||
b.1.cmp(&a.1)
|
||||
.then(a.2.total_cmp(&b.2))
|
||||
.then(b.3.cmp(&a.3))
|
||||
.then(a.4.cmp(&b.4))
|
||||
})
|
||||
.map(|(i, ..)| i);
|
||||
let Some(i) = best_sam else { break };
|
||||
match self.load_ground_round(i) {
|
||||
Ok(_) => {
|
||||
if self.sams[i].ground_request {
|
||||
self.sams[i].auto_ground_request = true;
|
||||
pending_requests += 1;
|
||||
out.messages.push(format!("Автоудар: С-400 №{} заказал доставку RM48U", i + 1));
|
||||
} else if self.sams[i].ground_rounds > 0 {
|
||||
available_rm48u = available_rm48u.saturating_sub(1);
|
||||
if let Some(pos_idx) = pick_best_site(&uncovered, self.sams[i].pos) {
|
||||
let (site_id, _, _, est) = uncovered.remove(pos_idx);
|
||||
self.prepare_strike_from(i, site_id, est.pos);
|
||||
out.messages.push(format!("Автоудар: С-400 №{} загрузил RM48U и готовит пуск по ПУ №{site_id}", i + 1));
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn prepare_ground_strikes(&mut self, out: &mut Out) {
|
||||
@@ -989,6 +1212,7 @@ impl Sim {
|
||||
v.push("Известных ПУ нет: РЛС должна заметить пуск или вылетевшую цель; для удара нужно определить место пуска".into());
|
||||
}
|
||||
v.push("Удар: /airdef strike <вражеская ПУ>; RM48U: /airdef ground-stock <склад> <число>, затем ground-load <С-400>".into());
|
||||
v.push(format!("Автоудар по ПУ: {} (/airdef autostrike on|off)", if self.auto_strike { "вкл" } else { "выкл" }));
|
||||
v
|
||||
}
|
||||
}
|
||||
@@ -1869,4 +2093,220 @@ mod tests {
|
||||
assert!(rec.tracks.iter().any(|t| t.what == record::What::Strike));
|
||||
assert!(rec.events.iter().any(|e| matches!(e.kind, record::EventKind::Strike { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auto_strike_disabled_by_default_does_not_request_or_fire() {
|
||||
let mut sim = Sim::new(11);
|
||||
assert!(!sim.auto_strike);
|
||||
fixed_site(&mut sim, DVec3::new(0.0, 64.0, 400.0), 1);
|
||||
let mut s400 = Sam::new(sim.id(), SamKind::S400, DVec3::ZERO, 0.0);
|
||||
s400.cooldown = 0;
|
||||
s400.ground_rounds = 1;
|
||||
s400.missiles = 3;
|
||||
sim.sams.push(s400);
|
||||
let mut depot = Sam::new(sim.id(), SamKind::Depot, DVec3::new(200.0, 64.0, 0.0), 0.0);
|
||||
depot.ground_stock = 2;
|
||||
sim.sams.push(depot);
|
||||
sim.add_drone(DroneKind::Shahed, DVec3::new(5000.0, 100.0, 5000.0), -DVec3::Z, DVec3::new(5000.0, 64.0, 0.0));
|
||||
let mut ground = Ground::default();
|
||||
let mut out = Out::default();
|
||||
for _ in 0..40 {
|
||||
sim.step(&mut ground, None, &mut out);
|
||||
}
|
||||
assert!(sim.strikes.pending.is_empty() && sim.strikes.flights.is_empty());
|
||||
assert!(!sim.sams[0].ground_request);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auto_strike_loads_directly_from_nearby_depot_and_fires() {
|
||||
let mut sim = Sim::new(12);
|
||||
sim.auto_strike = true;
|
||||
let site = fixed_site(&mut sim, DVec3::new(0.0, 64.0, 450.0), 1);
|
||||
let mut s400 = Sam::new(sim.id(), SamKind::S400, DVec3::new(0.0, 64.0, 0.0), 0.0);
|
||||
s400.cooldown = 0;
|
||||
sim.sams.push(s400);
|
||||
let mut depot = Sam::new(sim.id(), SamKind::Depot, DVec3::new(12.0, 64.0, 0.0), 0.0);
|
||||
depot.ground_stock = 2;
|
||||
sim.sams.push(depot);
|
||||
sim.add_drone(DroneKind::Shahed, DVec3::new(5000.0, 100.0, 5000.0), -DVec3::Z, DVec3::new(5000.0, 64.0, 0.0));
|
||||
let mut ground = Ground::default();
|
||||
let mut out = Out::default();
|
||||
run(&mut sim, &mut ground, &mut out, 40);
|
||||
assert_eq!(sim.strikes.fired, 1);
|
||||
assert_eq!(sim.strikes.flights.len(), 1);
|
||||
assert_eq!(sim.strikes.flights[0].site, site);
|
||||
assert_eq!(sim.sams[1].ground_stock, 1);
|
||||
assert_eq!(sim.sams[1].air_48n6_stock, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auto_strike_orders_delivery_via_tzm_and_fires_when_s400_is_free() {
|
||||
let mut sim = Sim::new(13);
|
||||
sim.auto_strike = true;
|
||||
let site = fixed_site(&mut sim, DVec3::new(0.0, 64.0, 500.0), 1);
|
||||
let mut depot = Sam::new(sim.id(), SamKind::Depot, DVec3::new(0.0, 64.0, 0.0), 0.0);
|
||||
depot.ground_stock = 1;
|
||||
sim.sams.push(depot);
|
||||
let mut s400 = Sam::new(sim.id(), SamKind::S400, DVec3::new(120.0, 64.0, 0.0), 0.0);
|
||||
s400.cooldown = 0;
|
||||
sim.sams.push(s400);
|
||||
let tzm_id = sim.id();
|
||||
sim.sams.push(Sam::new(tzm_id, SamKind::Tzm, DVec3::new(10.0, 64.0, 0.0), 0.0));
|
||||
// Keep an active raid alive far away so raid_in_progress() stays true without threatening the S-400.
|
||||
sim.add_drone(DroneKind::Shahed, DVec3::new(6000.0, 100.0, 6000.0), -DVec3::Z, DVec3::new(6000.0, 64.0, 0.0));
|
||||
let (mut ground, land) = (Ground::default(), sites::testland::Land(|_, _| 64));
|
||||
let mut out = Out::default();
|
||||
let mut requested = false;
|
||||
for _ in 0..4000 {
|
||||
sites::testland::step(&mut sim, &mut ground, &land, &mut out);
|
||||
if sim.sams[1].auto_ground_request {
|
||||
requested = true;
|
||||
}
|
||||
if sim.strikes.fired > 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert!(requested, "idle S-400 should automatically order RM48U delivery");
|
||||
assert_eq!(sim.strikes.fired, 1);
|
||||
assert_eq!(sim.strikes.flights[0].site, site);
|
||||
assert!(!sim.sams[1].auto_ground_request);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auto_strike_waits_when_in_role_threat_is_foreseen_but_ignores_out_of_role_drone() {
|
||||
let mut sim = Sim::new(14);
|
||||
sim.auto_strike = true;
|
||||
fixed_site(&mut sim, DVec3::new(0.0, 64.0, 450.0), 1);
|
||||
let mut s400 = Sam::new(sim.id(), SamKind::S400, DVec3::ZERO, 0.0);
|
||||
s400.cooldown = 0;
|
||||
s400.ground_rounds = 1;
|
||||
s400.missiles = 3;
|
||||
let s400_id = s400.id;
|
||||
// Confirmed Drone (out of S-400's default roles) approaching from 1500 blocks out: must NOT block auto-strike.
|
||||
sim.add_drone(DroneKind::Shahed, DVec3::new(0.0, 100.0, 1500.0), -DVec3::Z * 0.6, DVec3::ZERO);
|
||||
let shahed_id = sim.drones[0].id;
|
||||
s400.tracks.push(Track {
|
||||
id: shahed_id,
|
||||
class: ClassEst::Firm(Class::Drone),
|
||||
pos: DVec3::new(0.0, 100.0, 1500.0),
|
||||
vel: DVec3::new(0.0, 0.0, -0.6),
|
||||
tick: 0,
|
||||
own_tick: 0,
|
||||
source: s400_id,
|
||||
product: battery::Measure::Fire,
|
||||
quality: 100,
|
||||
own_product: battery::Measure::Fire,
|
||||
own_quality: 100,
|
||||
ttl: 200,
|
||||
hits: 4,
|
||||
locked: false,
|
||||
});
|
||||
// Approaching Kh-101 (cruise missile, in S-400 roles) 1400 blocks out -> reaches within 240 ticks -> MUST block!
|
||||
sim.add_drone(DroneKind::Kh101, DVec3::new(0.0, 100.0, 1400.0), -DVec3::Z * 2.4, DVec3::ZERO);
|
||||
let kh_id = sim.drones[1].id;
|
||||
s400.tracks.push(Track {
|
||||
id: kh_id,
|
||||
class: ClassEst::Firm(Class::Cruise),
|
||||
pos: DVec3::new(0.0, 100.0, 1400.0),
|
||||
vel: DVec3::new(0.0, 0.0, -2.4),
|
||||
tick: 0,
|
||||
own_tick: 0,
|
||||
source: s400_id,
|
||||
product: battery::Measure::Fire,
|
||||
quality: 100,
|
||||
own_product: battery::Measure::Fire,
|
||||
own_quality: 100,
|
||||
ttl: 200,
|
||||
hits: 4,
|
||||
locked: false,
|
||||
});
|
||||
sim.sams.push(s400);
|
||||
assert!(sim.has_foreseen_air_task(&sim.sams[0]));
|
||||
sim.tick = 10;
|
||||
let mut out = Out::default();
|
||||
sim.step_auto_strike(&mut out);
|
||||
assert!(sim.strikes.pending.is_empty(), "approaching Kh-101 must hold auto-strike");
|
||||
// Remove the Kh-101 so only the out-of-role Shahed remains.
|
||||
sim.drones[1].flight = super::super::Flight::Falling;
|
||||
assert!(!sim.has_foreseen_air_task(&sim.sams[0]));
|
||||
sim.tick = 20;
|
||||
sim.step_auto_strike(&mut out);
|
||||
assert_eq!(sim.strikes.pending.len(), 1, "out-of-role drone must not block auto-strike");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auto_strike_does_not_over_order_for_single_site_and_prioritizes_higher_threat_site() {
|
||||
let mut sim = Sim::new(15);
|
||||
sim.auto_strike = true;
|
||||
let ramp = fixed_site(&mut sim, DVec3::new(0.0, 64.0, 300.0), 1);
|
||||
let bat_id = sim.sites.add(LaunchSite {
|
||||
id: 0,
|
||||
side: 0,
|
||||
sector: 0,
|
||||
bearing: 0.0,
|
||||
class: SiteClass::Battery,
|
||||
pos: DVec3::new(0.0, 64.0, 600.0),
|
||||
range: 600.0,
|
||||
water: false,
|
||||
stock: 1,
|
||||
planned: 1,
|
||||
alive: true,
|
||||
next_free: 0,
|
||||
offmap: false,
|
||||
});
|
||||
sim.strikes.known.push(Known {
|
||||
site: bat_id,
|
||||
class: SiteClass::Battery,
|
||||
obs: Vec::new(),
|
||||
est: Some(Estimate { pos: DVec2::new(0.0, 600.0), radius: 15.0, fix: true, bearing: 0.0, bearing_err: 0.01, origin: DVec2::ZERO }),
|
||||
launches: 1,
|
||||
last: 0,
|
||||
state: SiteState::Active,
|
||||
aimed: false,
|
||||
});
|
||||
let mut s1 = Sam::new(sim.id(), SamKind::S400, DVec3::ZERO, 0.0);
|
||||
s1.cooldown = 0;
|
||||
s1.ground_rounds = 1;
|
||||
s1.missiles = 3;
|
||||
sim.sams.push(s1);
|
||||
sim.add_drone(DroneKind::Shahed, DVec3::new(6000.0, 100.0, 6000.0), -DVec3::Z, DVec3::new(6000.0, 64.0, 0.0));
|
||||
sim.tick = 10;
|
||||
let mut out = Out::default();
|
||||
sim.step_auto_strike(&mut out);
|
||||
assert_eq!(sim.strikes.pending.len(), 1);
|
||||
assert_eq!(sim.strikes.pending[0].site, bat_id, "Battery must outrank Ramp ({ramp})");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auto_strike_cancels_auto_request_when_turned_off_but_keeps_manual_request() {
|
||||
let mut sim = Sim::new(16);
|
||||
sim.auto_strike = true;
|
||||
fixed_site(&mut sim, DVec3::new(0.0, 64.0, 450.0), 1);
|
||||
let mut depot = Sam::new(sim.id(), SamKind::Depot, DVec3::ZERO, 0.0);
|
||||
depot.ground_stock = 3;
|
||||
sim.sams.push(depot);
|
||||
// S-400 #1 has a manual request; S-400 #2 gets an automatic request.
|
||||
let mut s_manual = Sam::new(sim.id(), SamKind::S400, DVec3::new(150.0, 64.0, 0.0), 0.0);
|
||||
s_manual.cooldown = 0;
|
||||
s_manual.ground_request = true;
|
||||
sim.sams.push(s_manual);
|
||||
let mut s_auto = Sam::new(sim.id(), SamKind::S400, DVec3::new(160.0, 64.0, 0.0), 0.0);
|
||||
s_auto.cooldown = 0;
|
||||
sim.sams.push(s_auto);
|
||||
let tzm_id = sim.id();
|
||||
sim.sams.push(Sam::new(tzm_id, SamKind::Tzm, DVec3::new(200.0, 64.0, 0.0), 0.0));
|
||||
// Keep only 1 uncovered site + 1 extra site so s_auto can request, or add a second site.
|
||||
fixed_site(&mut sim, DVec3::new(0.0, 64.0, 550.0), 1);
|
||||
sim.add_drone(DroneKind::Shahed, DVec3::new(6000.0, 100.0, 6000.0), -DVec3::Z, DVec3::new(6000.0, 64.0, 0.0));
|
||||
sim.tick = 10;
|
||||
let mut out = Out::default();
|
||||
sim.step_auto_strike(&mut out);
|
||||
assert!(sim.sams[1].ground_request && !sim.sams[1].auto_ground_request);
|
||||
assert!(sim.sams[2].ground_request && sim.sams[2].auto_ground_request);
|
||||
sim.auto_strike = false;
|
||||
sim.tick = 11;
|
||||
sim.step_auto_strike(&mut out);
|
||||
assert!(sim.sams[1].ground_request, "manual request must stay active");
|
||||
assert!(!sim.sams[2].ground_request && !sim.sams[2].auto_ground_request, "auto request must cancel when auto_strike is off");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,6 +156,35 @@ impl SamKind {
|
||||
pub fn has_gun(self) -> bool {
|
||||
self.spec().gun.is_some()
|
||||
}
|
||||
/// Typed missile ammunition kind consumed by this unit, if it launches air-defence missiles.
|
||||
pub fn missile_ammo(self) -> Option<AmmoKind> {
|
||||
match self {
|
||||
SamKind::Patriot => Some(AmmoKind::PatriotMissile),
|
||||
SamKind::Pantsir => Some(AmmoKind::PantsirMissile),
|
||||
SamKind::S400 => Some(AmmoKind::S400Missile),
|
||||
SamKind::S500 => Some(AmmoKind::S500Missile),
|
||||
SamKind::IrisT => Some(AmmoKind::IrisTMissile),
|
||||
SamKind::Nasams => Some(AmmoKind::NasamsMissile),
|
||||
SamKind::Buk => Some(AmmoKind::BukMissile),
|
||||
SamKind::Tor => Some(AmmoKind::TorMissile),
|
||||
SamKind::P1sun => Some(AmmoKind::P1sunMissile),
|
||||
SamKind::Gepard
|
||||
| SamKind::Radar
|
||||
| SamKind::Depot
|
||||
| SamKind::Tzm
|
||||
| SamKind::Ew
|
||||
| SamKind::Light
|
||||
| SamKind::Optical => None,
|
||||
}
|
||||
}
|
||||
/// Typed gun ammunition kind consumed by this unit, if it has anti-aircraft guns.
|
||||
pub fn gun_ammo(self) -> Option<AmmoKind> {
|
||||
match self {
|
||||
SamKind::Gepard => Some(AmmoKind::GepardGun),
|
||||
SamKind::Pantsir => Some(AmmoKind::PantsirGun),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Sam {
|
||||
|
||||
@@ -1357,13 +1357,14 @@ mod tests {
|
||||
assert!(sim.sams[0].empty);
|
||||
let id = sim.id();
|
||||
let mut depot = Sam::new(id, SamKind::Depot, DVec3::new(10.0, 70.0, 0.0), 0.0);
|
||||
depot.cargo = 1;
|
||||
depot.clear_ammo();
|
||||
depot.set_ammo(AmmoKind::S400Missile, 1);
|
||||
sim.sams.push(depot);
|
||||
for _ in 0..450 {
|
||||
serve(&mut ground, &land, sim.tick);
|
||||
sim.step(&mut ground, None, &mut out);
|
||||
}
|
||||
assert_eq!((sim.sams[0].missiles, sim.sams[1].cargo), (4, 0));
|
||||
assert_eq!((sim.sams[0].missiles, sim.sams[1].ammo_stock.get(AmmoKind::S400Missile), sim.sams[1].cargo), (4, 0, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user