dc36d67eb7
build / mod (push) Failing after 21s
step_supply sent every free truck into the return branch whenever any S-400 had a pending ground_return, even when that return could not proceed (the system moving, damaged, reloading or preparing, its round already claimed by another truck, the route marked unreachable, or no depot with room). The branch then found no target, cleared the job and stopped the truck, so independent typed supply requests starved. The return now keeps its priority only while it is executable: a carrier holding a reserved round looks for a depot with room in reach, an empty one for a system ready to hand a round over. Otherwise the claim stays untouched and only real RM48U demand (a requesting system with depot stock, or ground rounds aboard for one) pulls the truck into the typed supply branch; without such demand it holds its watch exactly as before. Redirecting a reserved round to a waiting request clears return_cargo once the last one leaves the truck, and a stale return_cargo with an empty hold no longer sends the truck to a depot handover it cannot complete. Regressions: three sim tests (moving claimant, pickup claimed by another truck with damaged/save-restore phases, unreachable route) and one full-drive lib test with a mid-flight state round trip; the rounds are conserved in every phase and the actual return completes once the claimant is back. All three sim tests fail on the old code. Checks: core release 200 passed/8 ignored + 8 scenarios; adapter 67 unit + 1 boundary + 5 scenarios; real WASM test-wasm 30 steps.
601 lines
36 KiB
Rust
601 lines
36 KiB
Rust
//! Logistics: once a depot stands, magazines are no longer refilled from
|
||
//! nowhere. A system out of missiles waits; one within reach of a depot
|
||
//! reloads from its stock; the reload trucks fill up at a depot and drive
|
||
//! reloads to the systems that wait, nearest first, and back.
|
||
use super::*;
|
||
|
||
impl Sim {
|
||
/// Whether ammunition is counted: a depot was set up (without one, magazines refill as
|
||
/// before). A destroyed depot still counts: losing the last one leaves the systems short,
|
||
/// not supplied from nowhere again.
|
||
pub fn logistics(&self) -> bool {
|
||
self.sams.iter().any(|s| s.kind == SamKind::Depot)
|
||
}
|
||
|
||
/// 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("Нет такого комплекса")?;
|
||
if s.kind != SamKind::S400 { return Err("Для этого комплекса нет игрового профиля RM48U".into()); }
|
||
if !s.alive() || s.moving() || s.preparing.is_some() || s.reloading > 0 || s.part(Part::Launcher) <= 0.0 {
|
||
return Err("Комплекс должен быть исправен и развёрнут".into());
|
||
}
|
||
if s.ground_return { return Err("Сначала отмените заявку на вывоз RM48U".into()); }
|
||
if s.ground_rounds >= s.cells() { return Err("Нет свободной ячейки для RM48U".into()); }
|
||
let depot = self.sams.iter().enumerate().filter(|(_, d)| d.kind == SamKind::Depot && d.alive() && d.ground_stock > 0 && d.pos.distance(s.pos) < DEPOT_REACH)
|
||
.min_by(|(_, a), (_, b)| a.pos.distance(s.pos).total_cmp(&b.pos.distance(s.pos)).then(a.id.cmp(&b.id)))
|
||
.map(|(j, _)| j);
|
||
let Some(depot) = depot else {
|
||
if s.ground_request { return Err("Заявка RM48U уже ожидает ТЗМ".into()); }
|
||
if !self.sams.iter().any(|d| d.alive() && ((d.kind == SamKind::Depot && d.ground_stock > 0) || (d.kind == SamKind::Tzm && d.ground_stock > 0))) {
|
||
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());
|
||
}
|
||
if !self.sams.iter().any(|t| t.kind == SamKind::Tzm && t.alive()) { return Err("Для доставки RM48U нужна исправная ТЗМ".into()); }
|
||
self.sams[i].ground_request = true;
|
||
return Ok(format!("С-400 №{}: заявка RM48U ожидает доставки ТЗМ", i + 1));
|
||
};
|
||
let displaced = u32::from(self.sams[i].missiles + self.sams[i].ground_rounds >= self.sams[i].cells());
|
||
let air_48n6_stock = self.sams[depot].air_48n6_stock.checked_add(displaced).ok_or("Запас склада переполнен")?;
|
||
self.sams[i].missiles -= displaced;
|
||
self.sams[i].ground_rounds += 1;
|
||
self.sams[i].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;
|
||
Ok(format!("С-400 №{}: загружена RM48U (игровой профиль), ударных {}, ПВО {}; на складе RM48U {}, снятых ракет ПВО {}", i + 1, self.sams[i].ground_rounds, self.sams[i].missiles, self.sams[depot].ground_stock, self.sams[depot].air_48n6_stock))
|
||
}
|
||
|
||
/// Return a loaded ground round to a nearby depot; restore a previously removed air round if available.
|
||
pub fn unload_ground_round(&mut self, i: usize) -> Result<String, String> {
|
||
let s = self.sams.get(i).ok_or("Нет такого комплекса")?;
|
||
if s.kind != SamKind::S400 || !s.alive() || s.moving() || s.preparing.is_some() || s.reloading > 0 || s.part(Part::Launcher) <= 0.0 || s.ground_rounds == 0 {
|
||
return Err("Нужен исправный развёрнутый С-400 с загруженной RM48U".into());
|
||
}
|
||
if s.ground_return { return Err("Заявка на вывоз RM48U уже ожидает ТЗМ".into()); }
|
||
let depot = self.sams.iter().enumerate().filter(|(_, d)| d.kind == SamKind::Depot && d.alive() && d.pos.distance(s.pos) < DEPOT_REACH)
|
||
.min_by(|(_, a), (_, b)| a.pos.distance(s.pos).total_cmp(&b.pos.distance(s.pos)).then(a.id.cmp(&b.id)))
|
||
.map(|(j, _)| j);
|
||
let Some(depot) = depot else {
|
||
if s.ground_request { return Err("Сначала отмените заявку на доставку RM48U".into()); }
|
||
if !self.sams.iter().any(|d| d.kind == SamKind::Depot && d.alive() && d.ground_stock < STOCK_LIMIT) { return Err("Нет доступного склада для возврата RM48U".into()); }
|
||
if !self.sams.iter().any(|t| t.kind == SamKind::Tzm && t.alive() && t.drive_speed() > 0.0 && t.ground_stock.saturating_add(t.air_48n6_stock) < TRUCK_LOAD) { return Err("Нет исправной ТЗМ со свободным местом для RM48U".into()); }
|
||
self.sams[i].ground_return = true;
|
||
return Ok(format!("С-400 №{}: RM48U зарезервирована, ожидается вывоз ТЗМ", i + 1));
|
||
};
|
||
let stock = self.sams[depot].ground_stock.checked_add(1).filter(|n| *n <= STOCK_LIMIT).ok_or("Запас склада переполнен")?;
|
||
self.sams[i].ground_rounds -= 1;
|
||
self.sams[depot].ground_stock = stock;
|
||
if self.sams[depot].air_48n6_stock > 0 {
|
||
self.sams[depot].air_48n6_stock -= 1;
|
||
self.sams[i].missiles += 1;
|
||
}
|
||
Ok(format!("С-400 №{}: выгружена RM48U; ударных {}, ПВО {}", i + 1, self.sams[i].ground_rounds, self.sams[i].missiles))
|
||
}
|
||
|
||
pub fn cancel_ground_request(&mut self, i: usize) -> Result<String, String> {
|
||
let s = self.sams.get_mut(i).ok_or("Нет такого комплекса")?;
|
||
if s.kind != SamKind::S400 || (!s.ground_request && !s.ground_return) { return Err("Нет ожидающей заявки RM48U".into()); }
|
||
s.ground_request = false;
|
||
s.ground_return = false;
|
||
Ok(format!("С-400 №{}: заявка RM48U отменена", i + 1))
|
||
}
|
||
|
||
/// Reloads from depots near the systems, and the trucks' rounds.
|
||
pub(super) fn step_supply(&mut self, out: &mut Out) {
|
||
if !self.logistics() {
|
||
for s in &mut self.sams {
|
||
s.empty = false;
|
||
}
|
||
return;
|
||
}
|
||
let now = self.tick;
|
||
// Systems by a depot reload from it.
|
||
for i in 0..self.sams.len() {
|
||
if !self.sams[i].empty || !self.sams[i].alive() {
|
||
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)
|
||
{
|
||
self.sams[d].cargo -= 1;
|
||
self.reload_now(i, out);
|
||
}
|
||
}
|
||
// A destroyed truck drops its job, for another to take.
|
||
for s in self.sams.iter_mut().filter(|s| s.kind == SamKind::Tzm && !s.alive()) {
|
||
s.job = None;
|
||
}
|
||
// The trucks: a job each, when idle.
|
||
for t in 0..self.sams.len() {
|
||
let truck = &self.sams[t];
|
||
if truck.kind != SamKind::Tzm || !truck.alive() || truck.moving() || truck.drive_speed() <= 0.0 || now % 10 != t as u64 % 10 {
|
||
continue;
|
||
}
|
||
let at = truck.pos;
|
||
let truck_id = truck.id;
|
||
// A return is picked up physically; its cargo remains marked until a depot accepts it.
|
||
// The return keeps its priority only while it can actually proceed (#128): a carrier
|
||
// holding a reserved round needs a depot with room in reach, an empty one a system
|
||
// ready to hand a round over (whole, deployed, not claimed by another truck, a
|
||
// reachable route). A return that is merely claimed but impossible used to clear the
|
||
// job and stop the truck, starving independent supply requests; the claim now stays
|
||
// untouched and only real RM48U demand pulls the truck away from its watch.
|
||
let return_cargo = truck.return_cargo && truck.ground_stock > 0;
|
||
let can_pick = truck.ground_stock == 0 && truck.air_48n6_stock < TRUCK_LOAD;
|
||
let claim_pending = return_cargo || (can_pick && self.sams.iter().any(|s| s.kind == SamKind::S400 && s.alive() && s.ground_return));
|
||
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 target = if return_cargo {
|
||
self.sams.iter().filter(|s| s.kind == SamKind::Depot && s.alive() && s.ground_stock < STOCK_LIMIT && !self.routes.out_of_reach(truck_id, s.id, now)).min_by(|a,b| a.pos.distance(at).total_cmp(&b.pos.distance(at)))
|
||
} else if can_pick {
|
||
self.sams.iter().filter(|s| s.kind == SamKind::S400 && s.alive() && s.ground_return && s.ground_rounds > 0 && !s.moving() && s.preparing.is_none() && s.reloading == 0 && s.part(Part::Launcher) > 0.0 && !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)))
|
||
} else {
|
||
None
|
||
}.map(|s| (s.id, s.pos));
|
||
if let Some((id, to)) = target {
|
||
self.sams[t].job = Some(id);
|
||
if at.distance(to) > HANDOVER {
|
||
let side = horizontal(at - to).normalize_or(DVec3::X) * (HANDOVER * 0.6);
|
||
self.order_move_near(t, to + side, 3.5);
|
||
} else if let Some(j) = self.sams.iter().position(|s| s.id == id) {
|
||
if self.sams[t].return_cargo {
|
||
self.sams[j].ground_stock += 1;
|
||
self.sams[t].ground_stock -= 1;
|
||
self.sams[t].return_cargo = false;
|
||
} else {
|
||
self.sams[j].ground_rounds -= 1;
|
||
self.sams[j].ground_return = false;
|
||
self.sams[t].ground_stock += 1;
|
||
self.sams[t].return_cargo = true;
|
||
if self.sams[t].air_48n6_stock > 0 && self.sams[j].missiles + self.sams[j].ground_rounds < self.sams[j].cells() {
|
||
self.sams[t].air_48n6_stock -= 1;
|
||
self.sams[j].missiles += 1;
|
||
}
|
||
}
|
||
self.sams[t].job = None;
|
||
out.sound("minecraft:block.piston.extend", at, 1.5, 0.8);
|
||
}
|
||
continue;
|
||
}
|
||
if claim_pending {
|
||
// The impossible return still outranks a mere stock run: without RM48U demand
|
||
// the truck holds its watch (and drops a stale job) exactly as before.
|
||
let supply_demand = (self.sams[t].ground_stock > 0 && self.sams.iter().any(|s| s.alive() && s.ground_request))
|
||
|| (self.sams.iter().any(|s| s.kind == SamKind::S400 && s.alive() && s.ground_request)
|
||
&& self.sams.iter().any(|s| s.kind == SamKind::Depot && s.alive() && s.ground_stock > 0));
|
||
if !supply_demand {
|
||
self.sams[t].job = None;
|
||
continue;
|
||
}
|
||
}
|
||
// Typed strike cargo takes precedence over generic reloads. Jobs are reconstructed
|
||
// after loading a save; the request and physical cargo survive independently.
|
||
let typed_work = truck.ground_stock > 0 || truck.air_48n6_stock > 0 ||
|
||
(self.sams.iter().any(|s| s.kind == SamKind::S400 && s.alive() && s.ground_request)
|
||
&& self.sams.iter().any(|s| s.kind == SamKind::Depot && s.alive() && s.ground_stock > 0));
|
||
if typed_work {
|
||
if let Some(id) = self.sams[t].job {
|
||
let valid = self.sams.iter().find(|s| s.id == id).is_some_and(|s| s.alive() && !self.routes.out_of_reach(truck_id, s.id, now) && (s.kind == SamKind::Depot || (self.sams[t].ground_stock > 0 && s.kind == SamKind::S400 && s.ground_request)));
|
||
if !valid { self.sams[t].job = None; }
|
||
}
|
||
if let Some(id) = self.sams[t].job {
|
||
if let Some(j) = self.sams.iter().position(|s| s.id == id && s.alive()) {
|
||
if self.sams[j].pos.distance(at) <= HANDOVER {
|
||
if self.sams[j].kind == SamKind::Depot {
|
||
if let Some(n) = self.sams[j].air_48n6_stock.checked_add(self.sams[t].air_48n6_stock) {
|
||
self.sams[j].air_48n6_stock = n;
|
||
self.sams[t].air_48n6_stock = 0;
|
||
}
|
||
if self.sams[t].ground_stock > 0 && !self.sams.iter().any(|s| s.alive() && s.ground_request) && self.sams[j].ground_stock < STOCK_LIMIT {
|
||
let take = self.sams[t].ground_stock.min(STOCK_LIMIT - self.sams[j].ground_stock);
|
||
self.sams[j].ground_stock += take;
|
||
self.sams[t].ground_stock -= take;
|
||
// The last reserved round left the truck: nothing marks as return cargo any more.
|
||
if self.sams[t].ground_stock == 0 { self.sams[t].return_cargo = false; }
|
||
}
|
||
if self.sams[t].ground_stock == 0 && self.sams[j].ground_stock > 0 && self.sams[t].air_48n6_stock == 0 && self.sams.iter().any(|s| s.kind == SamKind::S400 && s.alive() && s.ground_request) {
|
||
self.sams[j].ground_stock -= 1;
|
||
self.sams[t].ground_stock += 1;
|
||
}
|
||
} else if self.sams[j].kind == SamKind::S400 && self.sams[j].ground_request && !self.sams[j].moving() && self.sams[j].preparing.is_none() && self.sams[j].reloading == 0 && self.sams[j].part(Part::Launcher) > 0.0 && self.sams[j].ground_rounds < self.sams[j].cells() && self.sams[t].ground_stock > 0 {
|
||
let displaced = u32::from(self.sams[j].missiles + self.sams[j].ground_rounds >= self.sams[j].cells());
|
||
self.sams[j].missiles -= displaced;
|
||
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[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).
|
||
if self.sams[t].ground_stock == 0 { self.sams[t].return_cargo = false; }
|
||
self.sams[t].air_48n6_stock += displaced;
|
||
out.sound("minecraft:block.piston.extend", at, 1.5, 0.8);
|
||
}
|
||
self.sams[t].job = None;
|
||
} else {
|
||
let to = self.sams[j].pos;
|
||
let side = horizontal(at - to).normalize_or(DVec3::X) * (HANDOVER * 0.6);
|
||
self.order_move_near(t, to + side, 3.5);
|
||
continue;
|
||
}
|
||
} else { self.sams[t].job = None; }
|
||
}
|
||
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 next = if self.sams[t].ground_stock > 0 && self.sams.iter().any(|s| s.alive() && s.ground_request) {
|
||
self.sams.iter().filter(|s| s.kind == SamKind::S400 && s.alive() && s.ground_request && !s.moving() && s.preparing.is_none() && s.reloading == 0 && s.part(Part::Launcher) > 0.0 && s.ground_rounds < s.cells() && !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)))
|
||
} else {
|
||
self.sams.iter().filter(|s| s.kind == SamKind::Depot && s.alive() && (self.sams[t].air_48n6_stock > 0 || (self.sams[t].ground_stock > 0 && s.ground_stock < STOCK_LIMIT) || (self.sams[t].ground_stock == 0 && s.ground_stock > 0)) && !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));
|
||
if let Some((id, to)) = next {
|
||
self.sams[t].job = Some(id);
|
||
let side = horizontal(at - to).normalize_or(DVec3::X) * (HANDOVER * 0.6);
|
||
if to.distance(at) > HANDOVER { self.order_move_near(t, to + side, 3.5); }
|
||
}
|
||
continue;
|
||
}
|
||
// Where it was going: hand over, or load up.
|
||
if let Some(job) = truck.job
|
||
&& let Some(j) = self.sams.iter().position(|s| s.id == job)
|
||
{
|
||
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);
|
||
}
|
||
self.sams[t].job = None;
|
||
} else if self.sams[j].alive() && (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;
|
||
let side = horizontal(at - to).normalize_or(DVec3::X) * (HANDOVER * 0.6);
|
||
self.order_move_near(t, to + side, 3.5);
|
||
continue;
|
||
} else {
|
||
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
|
||
};
|
||
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()
|
||
});
|
||
if let Some((id, to)) = next {
|
||
self.sams[t].job = Some(id);
|
||
// Stop beside it, not on it.
|
||
let side = horizontal(at - to).normalize_or(DVec3::X) * (HANDOVER * 0.6);
|
||
if to.distance(at) > HANDOVER {
|
||
self.order_move_near(t, to + side, 3.5);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// System `i` gets a reload: its magazine fills after its reload time.
|
||
fn reload_now(&mut self, i: usize, out: &mut Out) {
|
||
let sam = &mut self.sams[i];
|
||
sam.empty = false;
|
||
sam.reloading = sam.reload_time();
|
||
out.sound("minecraft:block.piston.extend", sam.pos, 2.0, 0.6);
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
#[test]
|
||
fn an_empty_nearby_depot_explains_the_missing_typed_stock() {
|
||
let mut sim = Sim::new(1);
|
||
for (kind, x) in [(SamKind::Depot, 0.0), (SamKind::S400, 1.0)] {
|
||
let id = sim.id();
|
||
sim.sams.push(Sam::new(id, kind, DVec3::new(x, 70.0, 0.0), 0.0));
|
||
}
|
||
let error = sim.load_ground_round(1).unwrap_err();
|
||
assert!(error.contains("RM48U: 0"));
|
||
assert!(error.contains("/airdef ground-stock 1"));
|
||
assert_eq!((sim.sams[0].ground_stock, sim.sams[1].ground_rounds, sim.sams[1].missiles), (0, 0, 4));
|
||
}
|
||
|
||
#[test]
|
||
fn return_waits_for_capacity_and_ready_carrier_and_can_be_cancelled() {
|
||
let mut sim = Sim::new(3);
|
||
for (kind, x) in [(SamKind::Depot, 0.0), (SamKind::S400, 160.0), (SamKind::Tzm, 160.0)] {
|
||
let id = sim.id(); sim.sams.push(Sam::new(id, kind, DVec3::new(x, 70.0, 0.0), 0.0));
|
||
}
|
||
sim.sams[1].ground_rounds = 1;
|
||
sim.sams[1].missiles = 3;
|
||
sim.sams[2].air_48n6_stock = 2;
|
||
assert!(sim.unload_ground_round(1).is_err());
|
||
sim.sams[2].air_48n6_stock = 1;
|
||
sim.unload_ground_round(1).unwrap();
|
||
sim.cancel_ground_request(1).unwrap();
|
||
assert!(!sim.sams[1].ground_return);
|
||
sim.unload_ground_round(1).unwrap();
|
||
sim.tick = 2;
|
||
sim.sams[1].reloading = 1;
|
||
sim.step_supply(&mut Out::default());
|
||
assert_eq!(sim.sams[1].ground_rounds, 1);
|
||
sim.sams[1].reloading = 0;
|
||
sim.step_supply(&mut Out::default());
|
||
assert_eq!((sim.sams[1].ground_rounds, sim.sams[1].missiles), (0, 4));
|
||
assert_eq!((sim.sams[2].ground_stock, sim.sams[2].air_48n6_stock), (1, 0));
|
||
assert!(sim.sams[2].return_cargo);
|
||
sim.direct_hit(sim.sams[2].pos + DVec3::Y, DVec3::ZERO, 1, &mut Out::default());
|
||
sim.repair();
|
||
assert_eq!((sim.sams[0].ground_stock, sim.sams[1].ground_rounds, sim.sams[2].ground_stock), (0, 0, 0));
|
||
assert!(!sim.sams[2].return_cargo);
|
||
}
|
||
|
||
#[test]
|
||
fn return_waits_for_moving_or_damaged_units() {
|
||
let mut sim = Sim::new(4);
|
||
for (kind, x) in [(SamKind::Depot, 0.0), (SamKind::S400, 160.0), (SamKind::Tzm, 160.0)] {
|
||
let id = sim.id(); sim.sams.push(Sam::new(id, kind, DVec3::new(x, 70.0, 0.0), 0.0));
|
||
}
|
||
sim.sams[1].ground_rounds = 1;
|
||
sim.sams[1].missiles = 3;
|
||
sim.unload_ground_round(1).unwrap();
|
||
sim.tick = 2;
|
||
sim.sams[1].comps.0[Part::Launcher as usize] = 0.0;
|
||
sim.step_supply(&mut Out::default());
|
||
assert_eq!(sim.sams[1].ground_rounds, 1);
|
||
sim.sams[1].comps.0[Part::Launcher as usize] = 1.0;
|
||
sim.sams[2].comps.0[Part::Tracks as usize] = 0.0;
|
||
sim.step_supply(&mut Out::default());
|
||
assert_eq!(sim.sams[1].ground_rounds, 1);
|
||
sim.sams[2].comps.0[Part::Tracks as usize] = 1.0;
|
||
assert!(sim.order_move(1, DVec3::new(220.0, 70.0, 0.0)));
|
||
sim.step_supply(&mut Out::default());
|
||
assert_eq!(sim.sams[1].ground_rounds, 1);
|
||
sim.sams[1].state = SamState::Search;
|
||
sim.step_supply(&mut Out::default());
|
||
assert_eq!((sim.sams[1].ground_rounds, sim.sams[2].ground_stock), (0, 1));
|
||
}
|
||
|
||
#[test]
|
||
fn typed_delivery_uses_stock_and_shared_cells_and_survives_interruption() {
|
||
let mut sim = Sim::new(1);
|
||
for (kind, x) in [(SamKind::Depot, 0.0), (SamKind::S400, 200.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));
|
||
}
|
||
assert!(sim.load_ground_round(1).is_err());
|
||
sim.sams[0].ground_stock = 1;
|
||
assert!(sim.load_ground_round(0).is_err());
|
||
sim.load_ground_round(1).unwrap();
|
||
sim.tick = 2;
|
||
sim.step_supply(&mut Out::default()); // assign depot
|
||
sim.step_supply(&mut Out::default()); // physically take one
|
||
assert_eq!((sim.sams[0].ground_stock, sim.sams[2].ground_stock), (0, 1));
|
||
assert_eq!(sim.sams[1].ground_rounds, 0);
|
||
// Interrupted truck retains cargo and request; a restored job is not required.
|
||
sim.sams[2].job = None;
|
||
sim.sams[2].state = SamState::Search;
|
||
sim.sams[2].pos = sim.sams[1].pos;
|
||
sim.step_supply(&mut Out::default());
|
||
sim.step_supply(&mut Out::default());
|
||
assert_eq!((sim.sams[1].missiles, sim.sams[1].ground_rounds), (3, 1));
|
||
assert_eq!((sim.sams[2].ground_stock, sim.sams[2].air_48n6_stock), (0, 1));
|
||
assert!(!sim.sams[1].ground_request);
|
||
sim.sams[2].job = None;
|
||
sim.sams[2].state = SamState::Search;
|
||
sim.sams[2].pos = sim.sams[0].pos;
|
||
sim.step_supply(&mut Out::default());
|
||
sim.step_supply(&mut Out::default());
|
||
assert_eq!((sim.sams[0].air_48n6_stock, sim.sams[2].air_48n6_stock), (1, 0));
|
||
assert!(sim.load_ground_round(1).is_err());
|
||
}
|
||
#[test]
|
||
fn pending_delivery_waits_for_compatible_type_and_cell_after_unload() {
|
||
let mut sim = Sim::new(2);
|
||
for kind in [SamKind::Depot, SamKind::S400, SamKind::Tzm] {
|
||
let id = sim.id(); sim.sams.push(Sam::new(id, kind, DVec3::ZERO, 0.0));
|
||
}
|
||
sim.sams[1].ground_request = true;
|
||
sim.sams[2].ground_stock = 1;
|
||
sim.sams[1].kind = SamKind::Patriot;
|
||
sim.tick = 2;
|
||
for _ in 0..4 { sim.step_supply(&mut Out::default()); }
|
||
assert_eq!(sim.sams[2].ground_stock, 1);
|
||
assert_eq!(sim.sams[1].ground_rounds, 0);
|
||
sim.sams[1].kind = SamKind::S400;
|
||
sim.sams[1].missiles = 0;
|
||
sim.sams[1].ground_rounds = 4;
|
||
for _ in 0..4 { sim.step_supply(&mut Out::default()); }
|
||
assert_eq!(sim.sams[2].ground_stock, 1);
|
||
sim.unload_ground_round(1).unwrap();
|
||
for _ in 0..4 { sim.step_supply(&mut Out::default()); }
|
||
assert_eq!((sim.sams[1].ground_rounds, sim.sams[2].ground_stock, sim.sams[0].ground_stock), (4, 0, 1));
|
||
assert!(!sim.sams[1].ground_request);
|
||
}
|
||
|
||
/// Runs `step_supply`, jumping the truck beside its current job each tick so handovers
|
||
/// resolve without a full drive (movement itself is covered by the lib round-trip tests).
|
||
/// Fails after 200 ticks if `done` never holds.
|
||
fn serve_until(sim: &mut Sim, truck: usize, mut done: impl FnMut(&Sim) -> bool) {
|
||
let mut ok = false;
|
||
for _ in 0..200 {
|
||
if done(sim) { ok = true; break; }
|
||
if let Some(id) = sim.sams[truck].job()
|
||
&& let Some(j) = sim.sams.iter().position(|s| s.id == id)
|
||
{
|
||
sim.sams[truck].pos = sim.sams[j].pos;
|
||
}
|
||
sim.sams[truck].state = SamState::Search;
|
||
sim.tick += 1;
|
||
sim.step_supply(&mut Out::default());
|
||
}
|
||
assert!(ok, "condition not reached within 200 step_supply calls");
|
||
}
|
||
|
||
/// #128: a return that is claimed but cannot proceed (here: the returning system is on the
|
||
/// move) must not stop the free truck with job=None; an independent typed request is served.
|
||
/// The claim stays, the rounds are conserved, and once the system stops the return completes.
|
||
#[test]
|
||
fn a_moving_return_claim_does_not_starve_an_independent_supply_request() {
|
||
let mut sim = Sim::new(5);
|
||
for (kind, x) in [(SamKind::Depot, 0.0), (SamKind::S400, 40.0), (SamKind::S400, 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));
|
||
}
|
||
let (depot, a, b, truck) = (0usize, 1usize, 2usize, 3usize);
|
||
let b_id = sim.sams[b].id;
|
||
sim.sams[depot].ground_stock = 2;
|
||
sim.sams[a].ground_rounds = 1;
|
||
sim.sams[a].missiles = 3;
|
||
sim.sams[a].ground_return = true;
|
||
sim.sams[b].ground_request = true;
|
||
assert!(sim.order_move(a, DVec3::new(400.0, 70.0, 0.0)));
|
||
// The free truck loads one RM48U at the depot and starts driving to B within 100 ticks;
|
||
// before the fix it entered the return branch, found the moving A impossible and idled.
|
||
let mut started = false;
|
||
for _ in 0..100 {
|
||
sim.tick += 1;
|
||
sim.step_supply(&mut Out::default());
|
||
if sim.sams[truck].job() == Some(b_id) { started = true; break; }
|
||
}
|
||
assert!(started, "truck never started the delivery to B");
|
||
// The claim and the rounds are untouched by the fallback.
|
||
assert!(sim.sams[a].ground_return);
|
||
assert_eq!(sim.sams[a].ground_rounds, 1);
|
||
assert_eq!((sim.sams[depot].ground_stock, sim.sams[truck].ground_stock), (1, 1));
|
||
assert_eq!(sim.sams[depot].ground_stock + sim.sams[truck].ground_stock + sim.sams[a].ground_rounds + sim.sams[b].ground_rounds, 3);
|
||
// Hand the round over to B; the full drive is exercised in the lib round-trip test.
|
||
serve_until(&mut sim, truck, |s| s.sams[b].ground_rounds == 1);
|
||
assert!(!sim.sams[b].ground_request);
|
||
assert_eq!((sim.sams[b].missiles, sim.sams[truck].air_48n6_stock), (3, 1));
|
||
assert_eq!(sim.sams[depot].ground_stock + sim.sams[truck].ground_stock + sim.sams[a].ground_rounds + sim.sams[b].ground_rounds, 3);
|
||
// No request is left, so the truck holds its watch (a stock run aside): the pending
|
||
// return still outranks it and the claim survives.
|
||
for _ in 0..20 { sim.tick += 1; sim.step_supply(&mut Out::default()); }
|
||
assert!(sim.sams[a].ground_return);
|
||
assert_eq!(sim.sams[a].ground_rounds, 1);
|
||
// A stops: the actual return is picked up and driven back to the depot.
|
||
sim.sams[a].state = SamState::Search;
|
||
sim.sams[a].dest = None;
|
||
serve_until(&mut sim, truck, |s| s.sams[truck].return_cargo);
|
||
assert!(!sim.sams[a].ground_return);
|
||
assert_eq!((sim.sams[a].ground_rounds, sim.sams[a].missiles), (0, 4));
|
||
assert_eq!((sim.sams[truck].ground_stock, sim.sams[truck].air_48n6_stock), (1, 0));
|
||
serve_until(&mut sim, truck, |s| s.sams[depot].ground_stock == 2);
|
||
assert!(!sim.sams[truck].return_cargo);
|
||
assert_eq!((sim.sams[depot].ground_stock, sim.sams[truck].ground_stock, sim.sams[b].ground_rounds), (2, 0, 1));
|
||
assert_eq!(sim.sams[depot].ground_stock + sim.sams[truck].ground_stock + sim.sams[a].ground_rounds + sim.sams[b].ground_rounds, 3);
|
||
}
|
||
|
||
/// #128 variant: the pickup is already claimed by another truck, so for this one the return
|
||
/// is impossible; it must turn to the waiting request instead of idling with job=None. A
|
||
/// dropped job (as after loading a save) is rebuilt, damaged trucks wait, and the repaired
|
||
/// claiming truck keeps the priority of the now executable return over the request.
|
||
#[test]
|
||
fn a_return_claimed_by_another_truck_leaves_the_supply_to_the_second_one() {
|
||
let mut sim = Sim::new(7);
|
||
for (kind, x) in [(SamKind::Depot, 0.0), (SamKind::S400, 160.0), (SamKind::S400, 200.0), (SamKind::Tzm, 0.0), (SamKind::Tzm, 150.0)] {
|
||
let id = sim.id(); sim.sams.push(Sam::new(id, kind, DVec3::new(x, 70.0, 0.0), 0.0));
|
||
}
|
||
let (depot, a, b, t2, t1) = (0usize, 1usize, 2usize, 3usize, 4usize);
|
||
let (a_id, b_id) = (sim.sams[a].id, sim.sams[b].id);
|
||
sim.sams[depot].ground_stock = 2;
|
||
sim.sams[a].ground_rounds = 1;
|
||
sim.sams[a].missiles = 3;
|
||
sim.sams[a].ground_return = true;
|
||
sim.sams[b].ground_request = true;
|
||
// The first truck claimed A's pickup earlier; both trucks start damaged: nothing
|
||
// moves, no round is lost, the claim waits.
|
||
sim.sams[t1].job = Some(a_id);
|
||
sim.sams[t1].comps.0[Part::Tracks as usize] = 0.0;
|
||
sim.sams[t2].comps.0[Part::Tracks as usize] = 0.0;
|
||
for _ in 0..30 { sim.tick += 1; sim.step_supply(&mut Out::default()); }
|
||
assert_eq!(sim.sams[t2].job(), None);
|
||
assert_eq!((sim.sams[depot].ground_stock, sim.sams[b].ground_rounds), (2, 0));
|
||
assert!(sim.sams[a].ground_return);
|
||
assert_eq!(sim.sams[depot].ground_stock + sim.sams[t1].ground_stock + sim.sams[t2].ground_stock + sim.sams[a].ground_rounds + sim.sams[b].ground_rounds, 3);
|
||
// Repaired, the second truck must not idle on the claimed return: depot first, then B.
|
||
sim.sams[t2].comps.0[Part::Tracks as usize] = 1.0;
|
||
let mut started = false;
|
||
for _ in 0..100 {
|
||
sim.tick += 1;
|
||
sim.step_supply(&mut Out::default());
|
||
if sim.sams[t2].job() == Some(b_id) { started = true; break; }
|
||
}
|
||
assert!(started, "second truck never started the delivery to B");
|
||
assert_eq!((sim.sams[depot].ground_stock, sim.sams[t2].ground_stock), (1, 1));
|
||
assert!(sim.sams[a].ground_return, "claimed return must survive the fallback");
|
||
// A save/load drops the job; the request and the cargo survive and the job is rebuilt.
|
||
sim.sams[t2].job = None;
|
||
serve_until(&mut sim, t2, |s| s.sams[t2].job() == Some(b_id));
|
||
// The claiming truck is repaired: the executable return has priority over the request,
|
||
// and the still-hauling second truck is not diverted from B.
|
||
sim.sams[t1].comps.0[Part::Tracks as usize] = 1.0;
|
||
serve_until(&mut sim, t1, |s| s.sams[t1].return_cargo);
|
||
assert!(!sim.sams[a].ground_return);
|
||
assert_eq!(sim.sams[a].ground_rounds, 0);
|
||
assert_eq!(sim.sams[t2].job(), Some(b_id), "hauling truck must keep its supply job");
|
||
// B gets its round; the displaced air round rides along.
|
||
serve_until(&mut sim, t2, |s| s.sams[b].ground_rounds == 1);
|
||
assert_eq!((sim.sams[b].missiles, sim.sams[t2].air_48n6_stock), (3, 1));
|
||
assert_eq!(sim.sams[depot].ground_stock + sim.sams[t1].ground_stock + sim.sams[t2].ground_stock + sim.sams[a].ground_rounds + sim.sams[b].ground_rounds, 3);
|
||
// And the picked-up return reaches the depot: everything is back except B's new round.
|
||
serve_until(&mut sim, t1, |s| s.sams[depot].ground_stock == 2);
|
||
assert!(!sim.sams[t1].return_cargo);
|
||
assert_eq!((sim.sams[depot].ground_stock, sim.sams[t1].ground_stock, sim.sams[b].ground_rounds), (2, 0, 1));
|
||
}
|
||
|
||
/// #128 variant: the route to the returning system is marked unreachable, so the return is
|
||
/// impossible for now; the truck serves the request, and the return completes once the
|
||
/// route is passable again and the system is still waiting with its claim.
|
||
#[test]
|
||
fn an_unreachable_route_falls_back_to_supply_until_it_expires() {
|
||
let mut sim = Sim::new(9);
|
||
for (kind, x) in [(SamKind::Depot, 0.0), (SamKind::S400, 40.0), (SamKind::S400, 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));
|
||
}
|
||
let (depot, a, b, truck) = (0usize, 1usize, 2usize, 3usize);
|
||
let (a_id, b_id) = (sim.sams[a].id, sim.sams[b].id);
|
||
sim.sams[depot].ground_stock = 2;
|
||
sim.sams[a].ground_rounds = 1;
|
||
sim.sams[a].missiles = 3;
|
||
sim.sams[a].ground_return = true;
|
||
sim.sams[b].ground_request = true;
|
||
sim.routes.unreachable.insert((sim.sams[truck].id, a_id), u64::MAX);
|
||
let mut started = false;
|
||
for _ in 0..100 {
|
||
sim.tick += 1;
|
||
sim.step_supply(&mut Out::default());
|
||
if sim.sams[truck].job() == Some(b_id) { started = true; break; }
|
||
}
|
||
assert!(started, "truck never started the delivery to B");
|
||
assert!(sim.sams[a].ground_return, "claim must survive the blocked route");
|
||
serve_until(&mut sim, truck, |s| s.sams[b].ground_rounds == 1);
|
||
// The route is passable again: the waiting claim is picked up and returned.
|
||
sim.routes.unreachable.clear();
|
||
serve_until(&mut sim, truck, |s| s.sams[truck].return_cargo);
|
||
assert!(!sim.sams[a].ground_return);
|
||
serve_until(&mut sim, truck, |s| s.sams[depot].ground_stock == 2);
|
||
assert!(!sim.sams[truck].return_cargo);
|
||
assert_eq!(sim.sams[depot].ground_stock + sim.sams[truck].ground_stock + sim.sams[a].ground_rounds + sim.sams[b].ground_rounds, 3);
|
||
}
|
||
|
||
}
|