//! Verification harness, active only with command-line flags.
//!
//! - `--scenario` lays out a fixed network with trains, selects a front
//! sector and runs the clock at 4x.
//! - `--record
` saves a screenshot of the primary window every 0.1 real
//! seconds to `/frame_{:05}.png`.
//! - `--quit-after ` exits the app after that much real time.
//! - `--stress` (with `--scenario`) pushes North Sector's front line back and
//! starts in summer, to reach evacuations and seasons quickly.
//! - `--demo` runs the self-playing narrated demo (`demo.rs`).
//!
//! Without flags none of this is added to the app.
use std::path::PathBuf;
use bevy::app::AppExit;
use bevy::prelude::*;
use bevy::render::view::window::screenshot::{Screenshot, save_to_disk};
use bevy::time::{Real, Time};
use crate::model::{
CarKind, LocoKind, Network, NodeId, Selected, SimClock, Stop, TrackSeg, Train,
};
/// Seconds between saved frames.
const RECORD_INTERVAL: f32 = 0.1;
/// Command-line options. Unknown arguments are ignored.
#[derive(Default)]
pub struct Cli {
pub scenario: bool,
pub stress: bool,
pub play: bool,
pub demo: bool,
pub record: Option,
pub quit_after: Option,
}
impl Cli {
pub fn from_env() -> Self {
let mut cli = Cli::default();
let mut args = std::env::args().skip(1);
while let Some(arg) = args.next() {
match arg.as_str() {
"--scenario" => cli.scenario = true,
"--stress" => cli.stress = true,
"--play" => cli.play = true,
"--demo" => cli.demo = true,
"--record" => cli.record = args.next().map(PathBuf::from),
"--quit-after" => cli.quit_after = args.next().and_then(|s| s.parse().ok()),
_ => {}
}
}
cli
}
}
fn find(net: &Network, name: &str) -> NodeId {
net.nodes
.iter()
.position(|n| n.name == name)
.map(NodeId)
.unwrap_or_else(|| panic!("scenario: no node named {name}"))
}
fn stop(node: NodeId, goods: &[usize]) -> Stop {
let mut load = [false; 4];
for &g in goods {
load[g] = true;
}
Stop { node, load }
}
fn train(loco: LocoKind, cars: Vec, route: Vec) -> Train {
Train {
loco,
cars,
at: route[0].node,
route,
stop_idx: 0,
path: Vec::new(),
progress: 0.0,
cargo: [0.0; 4],
dwell: 0.0,
stalled: false,
crates: 0,
}
}
/// Lays the scenario network, spawns the trains and selects a front sector.
fn setup_scenario(
mut commands: Commands,
mut net: ResMut,
mut clock: ResMut,
mut selected: ResMut,
stress: Res,
) {
// Good indices: ammo 0, food 1, troops 2, wounded 3.
let (ammo, food, troops, wounded) = (0, 1, 2, 3);
let links = [
("Varenholm", "Lindach"),
("Lindach", "Feldau"),
("Feldau", "North Sector"),
("Feldau", "Centre Sector"),
("Vellin", "Sorvik"),
("Sorvik", "Rhunen"),
("Rhunen", "South Sector"),
("Brenhollow", "Lindach"),
];
for (a, b) in links {
let (a, b) = (find(&net, a), find(&net, b));
let length = net.nodes[a.0].pos.distance(net.nodes[b.0].pos);
net.tracks.push(TrackSeg {
a,
b,
length,
condition: 1.0,
});
}
let n = |name: &str| find(&net, name);
let trains = [
// Heavy ammo: factory to the front and back.
train(
LocoKind::Heavy,
vec![CarKind::Ammo; 4],
vec![stop(n("Ostwerk"), &[ammo]), stop(n("North Sector"), &[])],
),
train(
LocoKind::Heavy,
vec![CarKind::Ammo; 4],
vec![stop(n("Vellin"), &[ammo]), stop(n("South Sector"), &[])],
),
// Light food: farms to the centre front and to a city.
train(
LocoKind::Light,
vec![CarKind::Food; 2],
vec![stop(n("Lindach"), &[food]), stop(n("Centre Sector"), &[])],
),
train(
LocoKind::Light,
vec![CarKind::Food; 2],
vec![stop(n("Sorvik"), &[food]), stop(n("South Sector"), &[])],
),
train(
LocoKind::Light,
vec![CarKind::Food; 2],
vec![stop(n("Lindach"), &[food]), stop(n("Brenhollow"), &[])],
),
// Hospital: wounded from the north front back to the capital.
train(
LocoKind::Light,
vec![CarKind::Hospital; 3],
vec![
stop(n("North Sector"), &[wounded]),
stop(n("Varenholm"), &[]),
],
),
// Light troops from the capital to the centre front.
train(
LocoKind::Light,
vec![CarKind::Troop; 2],
vec![
stop(n("Varenholm"), &[troops]),
stop(n("Centre Sector"), &[]),
],
),
];
for t in trains {
commands.spawn(t);
}
clock.speed = 4;
selected.0 = Some(n("North Sector"));
if stress.0 {
// Summer starts at hour 14 days; the front is pushed back to threaten the north.
clock.hour = 24 * 14;
let north = n("North Sector");
if let Some(front) = net.nodes[north.0].front.as_mut() {
front.line = -0.45;
}
}
}
#[derive(Resource)]
struct Stress(bool);
/// Adds the fixed scenario network, trains and clock speed at startup.
pub struct ScenarioPlugin {
pub stress: bool,
}
impl Plugin for ScenarioPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(Stress(self.stress))
.add_systems(Startup, setup_scenario);
}
}
#[derive(Resource)]
struct Recorder {
dir: PathBuf,
/// Real seconds since the last frame was saved.
acc: f32,
frame: u32,
}
#[derive(Resource)]
struct QuitAfter(f32);
/// Screenshot recording and timed exit. Added only when a flag asks for them.
#[derive(Default)]
pub struct CapturePlugin {
pub record: Option,
pub quit_after: Option,
}
impl Plugin for CapturePlugin {
fn build(&self, app: &mut App) {
if let Some(dir) = &self.record {
std::fs::create_dir_all(dir).expect("create --record directory");
app.insert_resource(Recorder {
dir: dir.clone(),
acc: 0.0,
frame: 0,
})
.add_systems(Update, record_frames);
}
if let Some(secs) = self.quit_after {
app.insert_resource(QuitAfter(secs))
.add_systems(Update, quit_when_due);
}
}
}
fn record_frames(mut commands: Commands, time: Res