e3148bf458
build / mod (push) Successful in 3m22s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
123 lines
5.3 KiB
Rust
123 lines
5.3 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::*;
|
|
|
|
/// How near a depot a system reloads straight from it.
|
|
const DEPOT_REACH: f64 = 60.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.
|
|
const TRUCK_LOAD: u32 = 2;
|
|
|
|
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)
|
|
}
|
|
|
|
/// 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;
|
|
// 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(t, to + side);
|
|
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))
|
|
.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)
|
|
.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(t, to + side);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 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);
|
|
}
|
|
}
|