//! Balance measurements: a whole raid on flat land, flown headlessly against a cluster of systems. //! The `#[ignore]`d tests print tables (`cargo test --release balance -- --ignored --nocapture`); //! the others assert the cheap invariants. The tables are what the air defence's tuning is judged by. use super::*; /// The ten armed kinds in the order a cluster takes them (long-range heavy ones spread evenly). const MIX: [SamKind; 10] = [ SamKind::Patriot, SamKind::IrisT, SamKind::S400, SamKind::Pantsir, SamKind::Nasams, SamKind::Tor, SamKind::S500, SamKind::Buk, SamKind::Gepard, SamKind::P1sun, ]; /// What one drone went through, for saying why it got through. #[derive(Default, Clone)] struct Hist { kind: Option, /// Ticks any system held a track of it. seen: u32, /// Ticks some loaded launcher with the class in its roles had it in range (`cover`), and held its track too (`reach`). cover: u32, reach: u32, /// Distinct interceptors sent at it. shots: std::collections::HashSet, dead: bool, last: (DVec3, DVec3), /// Ticks to the aim when first covered, first tracked by a covering launcher, first shot at. ttg: (f64, f64, f64), } #[derive(Default, Debug, Clone)] pub(super) struct Result { pub launched: u32, pub kills: u32, pub leaked: u32, pub missiles: u32, pub ticks: u64, /// Leaks by cause. pub unseen: u32, pub no_cover: u32, pub untracked: u32, pub hits: u32, pub crashes: u32, /// Interceptors lost against the land, and ones that blew up short of a ridge. pub blocked: u32, pub scrubbed: u32, pub unshot: u32, pub missed: u32, /// Missiles fired by the heavy systems (S-400, S-500, Patriot). pub heavy: u32, pub by_kind: Vec<(DroneKind, u32, u32)>, pub tally: String, /// Drones that vanished short of their aim without being shot down: kind, where, how far from the aim. pub crashed: Vec<(DroneKind, DVec3, f64)>, /// Chunk tickets the sim asked for. pub tickets: usize, pub scrubs: Vec<(MissileKind, u32, f64, f64, DVec3, f64, f64)>, } pub(super) struct Setup { pub systems: usize, pub smart: bool, pub seed: u64, pub size: u32, pub radars: usize, pub objects: usize, /// Called on the sim before the raid (a test sets its switches). pub tweak: fn(&mut Sim), /// Hilly land instead of flat (`hills`). pub hilly: bool, /// Only a disc of this radius round the player (the origin) is loaded, as in the game; beyond it the land is not /// there (`Void`) unless a chunk ticket was asked for (loaded after a delay, for a while). pub disc: Option, /// Mountains on top of the hills (ridges of up to 150 blocks, 600-900 blocks apart). pub mountains: bool, /// How far out the raid launches (the order's distance). pub dist: f64, /// Systems and objects sunk this far into the land (a player who stood inside it). pub bury: f64, /// How steep the hills are (1 is the usual), and whether a forest's canopy stands on them (the game's height map shows the leaves). pub steep: f64, pub trees: bool, } /// Rolling hills: ridges of 20-35 blocks over a plain at 64, wavelengths of 90-250 blocks. pub(super) fn hills(x: i32, z: i32) -> i32 { hills_k(x, z, 1.0) } /// `hills` with its ridges `k` times as steep (higher and closer together). pub(super) fn hills_k(x: i32, z: i32, k: f64) -> i32 { let (x, z) = (f64::from(x) * k, f64::from(z) * k); let n = (x / 53.0 + (z / 91.0).sin() * 1.7).sin() * 0.5 + (z / 41.0 - (x / 77.0).cos()).sin() * 0.35 + ((x + z) / 130.0).sin() * 0.4 + ((x - 2.0 * z) / 29.0).sin() * 0.12; 64 + (n * 28.0 * k + 14.0).max(-4.0) as i32 } /// Mountains: broad ridges over the hills, nothing within about 300 blocks of the origin. pub(super) fn mountains(x: i32, z: i32) -> i32 { let (x, z) = (f64::from(x), f64::from(z)); let r = (x * x + z * z).sqrt(); let ridge = ((x / 410.0 + 0.7).sin() * (z / 530.0 - 0.4).sin() + (x / 700.0 - z / 610.0).sin() * 0.6).max(0.0); (ridge * 150.0 * ((r - 300.0) / 400.0).clamp(0.0, 1.0)) as i32 } /// The game's view of the land: all of it, or a disc round the player and the chunks asked for. pub(super) struct Loader { pub hilly: bool, pub mountains: bool, pub disc: Option, steep: f64, trees: bool, asked: std::cell::RefCell>, pub tickets: usize, } const TICKET_DELAY: u64 = 40; const TICKET_LIFE: u64 = 400; impl Loader { pub fn new(s: &Setup) -> Self { Loader { hilly: s.hilly, mountains: s.mountains, disc: s.disc, steep: s.steep, trees: s.trees, asked: Default::default(), tickets: 0 } } /// The land without the forest on it. pub fn bare(&self, x: i32, z: i32) -> f64 { f64::from(hills_k(x, z, self.steep) + if self.mountains { mountains(x, z) } else { 0 }) } pub fn height(&self, x: i32, z: i32) -> i32 { if !self.hilly { return 64; } // A forest: a quarter of the 3x3 patches carry a canopy 8 blocks over the ground (the game's height map shows the leaves). let canopy = if self.trees && ((x.div_euclid(3).wrapping_mul(73856093) ^ z.div_euclid(3).wrapping_mul(19349663)) as u32 >> 3) % 4 == 0 { 8 } else { 0 }; hills_k(x, z, self.steep) + canopy + if self.mountains { mountains(x, z) } else { 0 } } fn loaded(&self, c: (i32, i32), now: u64) -> Option { let Some(r) = self.disc else { return Some(true) }; let (x, z) = (f64::from(c.0 * 16 + 8), f64::from(c.1 * 16 + 8)); if (x * x + z * z).sqrt() <= r { return Some(true); } match self.asked.borrow().get(&c) { Some(&(since, until)) if now < until => Some(now >= since + TICKET_DELAY), _ => None, } } pub fn serve(&mut self, ground: &mut Ground, now: u64) { for c in ground.take_chunks() { self.tickets += 1; if std::env::var("NOTICKETS").is_err() { let mut asked = self.asked.borrow_mut(); let e = asked.entry(c).or_insert((now, now)); if now >= e.1 { *e = (now, now); } e.1 = now + TICKET_DELAY + TICKET_LIFE; } } let cols = ground .take_wanted() .into_iter() .map(|(x, z)| { let y = match self.loaded((x >> 4, z >> 4), now) { Some(true) => self.height(x, z), Some(false) => PENDING, None => -64, }; (x, z, y) }) .collect(); ground.answer(cols, now); let secs = ground.world.take_wanted(now); let view = Seen { l: self, now }; ground.world.serve(&view, secs, now); } } struct Seen<'a> { l: &'a Loader, now: u64, } impl world::WorldSource for Seen<'_> { fn solid(&self, x: i32, y: i32, z: i32) -> Option { self.l.loaded((x >> 4, z >> 4), self.now).filter(|&l| l).map(|_| y < self.l.height(x, z)) } } impl Setup { pub fn new(systems: usize, smart: bool, seed: u64) -> Self { Setup { systems, smart, seed, size: 36, radars: (systems / 10).max(1), objects: 36, tweak: |_| {}, hilly: false, bury: 0.0, disc: None, mountains: false, dist: 500.0, steep: std::env::var("STEEP").ok().and_then(|v| v.parse().ok()).unwrap_or(1.0), trees: std::env::var("TREES").is_ok() } } } /// The cluster: `objects` in a grid, systems of the mix scattered round them, radars among them. pub(super) fn cluster(sim: &mut Sim, s: &Setup) { let l = Loader::new(s); let y = |x: f64, z: f64| if s.hilly { l.bare(x as i32, z as i32) - s.bury } else { 70.0 }; let mut rng = Rng::new(s.seed.wrapping_mul(977) + 5); let side = (s.objects as f64).sqrt().ceil() as usize; for k in 0..s.objects { let (px, pz) = (((k % side) as f64 - side as f64 / 2.0) * 70.0, ((k / side) as f64 - side as f64 / 2.0) * 70.0); let p = DVec3::new(px, if s.hilly { y(px, pz) + 6.0 + s.bury } else { 70.0 }, pz); sim.targets.push(p); sim.target_health.push(100.0); } let objects = sim.targets.clone(); for i in 0..s.systems { let at = objects[(rng.f() * objects.len() as f64) as usize % objects.len()]; let a = rng.f() * TAU; let r = 30.0 + rng.f() * 110.0; let id = sim.id(); let (px, pz) = (at.x + a.sin() * r, at.z + a.cos() * r); sim.sams.push(Sam::new(id, MIX[i % MIX.len()], DVec3::new(px, y(px, pz), pz), rng.f() * TAU)); } for _ in 0..s.radars { let a = rng.f() * TAU; let r = rng.f() * 220.0; let id = sim.id(); let (px, pz) = (a.sin() * r, a.cos() * r); sim.sams.push(Sam::new(id, SamKind::Radar, DVec3::new(px, if s.hilly { y(px, pz) } else { 70.0 }, pz), 0.0)); } } /// Flies one raid to its end and counts. pub(super) fn fly(s: &Setup) -> Result { let mut sim = Sim::new(s.seed); cluster(&mut sim, s); (s.tweak)(&mut sim); let mut ground = Ground::default(); let mut land = Loader::new(s); let mut out = Out::default(); for _ in 0..200 { land.serve(&mut ground, sim.tick); sim.step(&mut ground, None, &mut out); } let mut order = crate::wave::WaveConfig::mix(s.size, s.dist); // The Oreshnik's blocks are a raid of their own; this measures the ordinary mass raid. order.kinds.retain(|k| k.0 != DroneKind::Oreshnik); if s.smart { order.aim = raid::Aim::Smart; order.approach = raid::Approach::AllSides; } let targets = sim.targets.clone(); let fired0: u32 = sim.sams.iter().map(|m| m.fired).sum(); sim.order_raid(&order, &targets, &mut out); let mut hist: HashMap = HashMap::new(); let mut result = Result::default(); let start = sim.tick; let mut gone_unkilled: Vec = Vec::new(); for _ in 0..9000 { land.serve(&mut ground, sim.tick); out = Out::default(); sim.step(&mut ground, None, &mut out); let now = sim.tick; for d in &sim.drones { let h = hist.entry(d.id).or_default(); h.kind = Some(d.kind); h.last = (d.pos, d.target); if !d.alive() { h.dead = true; continue; } if sim.sams.iter().any(|m| m.seen.contains(&d.id)) { h.seen += 1; } let class = d.kind.spec().class.bit(); let covers = |m: &&Sam| { m.alive() && m.roles & class != 0 && m.missiles > 0 && m.kind.spec().missile.as_ref().is_some_and(|l| (l.min..l.max).contains(&m.pos.distance(d.pos))) }; let ttg = why::arrival(d); if h.cover == 0 && sim.sams.iter().filter(covers).next().is_some() { h.ttg.0 = ttg; } if h.reach == 0 && sim.sams.iter().filter(covers).any(|m| m.seen.contains(&d.id)) { h.ttg.1 = ttg; } h.cover += u32::from(sim.sams.iter().filter(covers).next().is_some()); h.reach += u32::from(sim.sams.iter().filter(covers).any(|m| m.seen.contains(&d.id))); } for m in &sim.missiles { if let Some(h) = hist.get_mut(&m.target) { if h.shots.is_empty() { h.ttg.2 = h.last.0.distance(h.last.1) / h.kind.map_or(1.0, |k| k.spec().speed); } h.shots.insert(m.id); } } // Drones gone from the sky that were never brought down arrived (or crashed). let here: std::collections::HashSet = sim.drones.iter().map(|d| d.id).collect(); let queued: std::collections::HashSet = sim.queue.iter().map(|q| q.1.id).collect(); let vanished: Vec = hist.iter().filter(|(id, h)| !here.contains(id) && !queued.contains(id) && !h.dead && h.kind.is_some()).map(|(id, _)| *id).collect(); for id in vanished { if let Some(h) = hist.remove(&id) { // Gone from the sky near its aim: arrived. (A kill that detonates or breaks up removes the drone too.) let k = h.kind.unwrap(); if !k.spec().decoy && h.last.0.distance(h.last.1) < 2.0 * k.spec().speed + 12.0 { if std::env::var("LEAKS").is_ok() { eprintln!(" leak {k:?} seen {} cover {} reach {} shots {} ttg cover/track/shot {:.0}/{:.0}/{:.0}", h.seen, h.cover, h.reach, h.shots.len(), h.ttg.0, h.ttg.1, h.ttg.2); } gone_unkilled.push(h); } } } // Dead ones that have landed are forgotten. hist.retain(|id, h| !(h.dead && !here.contains(id))); if now - start > 300 && sim.queue.is_empty() && !sim.planning() && !sim.drones.iter().any(|d| d.alive()) && sim.missiles.is_empty() { break; } } result.tickets = land.tickets; result.scrubs = sim.scrubs.clone(); result.crashed = sim.crashed.iter().map(|c| (c.0, c.1, c.1.distance(c.2))).collect(); result.ticks = sim.tick - start; result.launched = sim.launched; result.kills = sim.kills; result.missiles = sim.sams.iter().map(|m| m.fired).sum::() - fired0; result.heavy = sim.sams.iter().filter(|m| matches!(m.kind, SamKind::S400 | SamKind::S500 | SamKind::Patriot)).map(|m| m.fired).sum(); result.hits = sim.hits; result.crashes = sim.crashes; result.blocked = sim.tally.blocked; result.scrubbed = sim.tally.scrubbed; result.leaked = sim.hits; let mut kinds: HashMap = HashMap::new(); for h in &gone_unkilled { let k = h.kind.unwrap(); kinds.entry(k).or_default().0 += 1; if h.seen == 0 { result.unseen += 1; } else if h.cover == 0 { result.no_cover += 1; } else if h.reach == 0 { result.untracked += 1; } else if h.shots.is_empty() { result.unshot += 1; } else { result.missed += 1; } } result.by_kind = kinds.into_iter().map(|(k, v)| (k, v.0, v.1)).collect(); result.by_kind.sort_by_key(|k| k.0 as u8); result.tally = sim.tally.text(); result } pub(super) fn table(title: &str, smart: bool, size: u32, systems: &[usize], seeds: &[u64], tweak: fn(&mut Sim)) -> Vec<(usize, f64)> { table_on(title, smart, size, systems, seeds, tweak, std::env::var("HILLS").is_ok()) } pub(super) fn table_on(title: &str, smart: bool, size: u32, systems: &[usize], seeds: &[u64], tweak: fn(&mut Sim), hilly: bool) -> Vec<(usize, f64)> { let mut means = Vec::new(); eprintln!("== {title} ({} raid{})", if smart { "smart" } else { "mix" }, if hilly { ", hills" } else { "" }); eprintln!("{:>4} {:>4} {:>5} {:>5} {:>5} {:>6} {:>6} | leaks: unseen nocover untrk unshot missed hits crash blkd scrub", "sams", "seed", "sent", "kills", "leak", "missil", "heavy"); for &n in systems { let (mut total, mut missiles, mut heavy, mut clean, mut crashes, mut blocked) = (0, 0, 0, 0, 0, 0); for &seed in seeds { let mut s = Setup::new(n, smart, seed); s.tweak = tweak; s.size = size; s.hilly = hilly; let r = fly(&s); total += r.leaked; clean += u32::from(r.leaked == 0); crashes += r.crashes; blocked += r.blocked + r.scrubbed; missiles += r.missiles; heavy += r.heavy; if std::env::var("ROWS").is_ok() { eprintln!("{n:>4} {seed:>4} {:>5} {:>5} {:>5} {:>6} {:>6} | {:>12} {:>7} {:>5} {:>6} {:>6} {:>4} {:>5} {:>4} {:>5} {:?}", r.launched, r.kills, r.leaked, r.missiles, r.heavy, r.unseen, r.no_cover, r.untracked, r.unshot, r.missed, r.hits, r.crashes, r.blocked, r.scrubbed, r.by_kind.iter().map(|k| format!("{:?}:{}", k.0, k.1)).collect::>()); } if seed == seeds[0] && std::env::var("ROWS").is_ok() { eprintln!(" tally: {}", r.tally); } } let mean = total as f64 / seeds.len() as f64; let k = seeds.len() as f64; eprintln!("{n:>4} mean leaks {mean:.1} (clean seeds {clean}/{}), missiles {:.0}, of them heavy {:.0}, drone crashes {:.1}, interceptors lost to land {:.1}", seeds.len(), missiles as f64 / k, heavy as f64 / k, crashes as f64 / k, blocked as f64 / k); means.push((n, mean)); } means } #[test] #[ignore] fn buried_table() { for n in [20usize, 150] { for seed in 1..=4u64 { let mut s = Setup::new(n, false, seed); s.hilly = true; s.bury = 7.0; let r = fly(&s); eprintln!("buried {n} seed {seed}: sent {} kills {} leaked {} crashes {} blocked {} missiles {}", r.launched, r.kills, r.leaked, r.crashes, r.blocked, r.missiles); } } } #[test] #[ignore] fn hills_table() { table_on("hills", false, 36, &[20, 50, 150], &[1, 2, 3, 4, 5, 6], |_| {}, true); } #[test] #[ignore] fn raid_table() { for smart in [false, true] { table("current", smart, 36, &[20, 50, 150], &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |_| {}); } } /// A raid of 80 (about 100 drones): 20 systems clearly lose, 150 hold (a loose bound, one seed). #[test] fn a_small_cluster_loses_and_a_big_one_holds() { let mut s = Setup::new(20, false, 2); s.size = 80; let small = fly(&s); let mut s = Setup::new(150, false, 2); s.size = 80; let big = fly(&s); eprintln!("20 systems: {small:?}\n150 systems: {big:?}"); assert!(small.launched >= 80, "the raid was not launched: {small:?}"); assert!(small.leaked >= 10, "20 systems should clearly lose, leaked {}", small.leaked); assert!(big.leaked <= 5, "150 systems leaked {}", big.leaked); } /// The same on rolling hills: 150 systems still hold about as on flat land, drones do not fly into the land on /// their own, and the interceptors do not routinely strike it (a few that blow up short of a ridge are allowed). #[test] fn on_hills_a_big_cluster_holds_and_nothing_routinely_hits_the_land() { let mut s = Setup::new(150, false, 2); s.size = 80; s.hilly = true; let big = fly(&s); let mut s = Setup::new(20, false, 2); s.size = 80; s.hilly = true; let small = fly(&s); eprintln!("hills, 20 systems: {small:?}\nhills, 150 systems: {big:?}"); assert!(small.leaked >= 10, "20 systems should clearly lose on hills too, leaked {}", small.leaked); assert!(big.leaked <= 6, "150 systems on hills leaked {}", big.leaked); assert!(big.crashes <= 2, "drones flew into the hills: {}", big.crashes); assert!(big.blocked * 20 <= big.missiles, "interceptors struck the land: {} of {}", big.blocked, big.missiles); } /// A raid from 2500 blocks over mountains, with only a disc round the player known (and chunks that load on request): /// the flights do not fly into the land by themselves (a rare one that cannot climb a ridge in time is allowed). #[test] fn far_raids_over_mountains_do_not_crash_into_them() { let (mut flown, mut crashed) = (0, 0); for seed in [3u64, 4, 8] { for smart in [false, true] { let mut s = Setup::new(50, smart, seed); s.hilly = true; s.mountains = true; s.dist = 2500.0; s.disc = Some(300.0); let r = fly(&s); eprintln!("seed {seed} smart {smart}: {} crashed of {}: {:?}", r.crashed.len(), r.launched, r.crashed.iter().map(|c| c.0).collect::>()); flown += r.launched; crashed += r.crashed.len() as u32; } } assert!(crashed * 40 <= flown, "{crashed} of {flown} flew into the land"); } /// Steep forested hills: an interceptor that is fired is one the land lets through. The launch check asks the same /// of a shot as the flight does (the margin over the land, the loft), so few blow up short of a ridge. #[test] fn on_steep_forested_hills_few_interceptors_are_scrubbed() { let (mut fired, mut scrubbed) = (0, 0); for seed in 1..=3u64 { let mut s = Setup::new(15, false, seed); s.radars = 2; s.objects = 4; s.hilly = true; s.steep = 1.6; s.trees = true; let r = fly(&s); fired += r.missiles; scrubbed += r.scrubbed; } eprintln!("scrubbed {scrubbed} of {fired}"); assert!(scrubbed * 100 <= fired * 10, "{scrubbed} of {fired} interceptors blew up short of a ridge"); } /// What each of the new rules adds, one at a time (the raid of 36 and a heavier one of 80). #[test] #[ignore] fn rules_one_by_one() { for size in [36, 80] { for smart in [false, true] { table(&format!("size {size}: roles only"), smart, size, &[20, 50, 150], &[1, 2, 3, 4, 5, 6], |s| { s.last_chance = false; s.far_shots = false; }); table(&format!("size {size}: + last chance"), smart, size, &[20, 50, 150], &[1, 2, 3, 4, 5, 6], |s| s.far_shots = false); table(&format!("size {size}: + last chance + long shots"), smart, size, &[20, 50, 150], &[1, 2, 3, 4, 5, 6], |_| {}); } } } /// One raid in detail, for hunting a leak: `LEAKS=1 cargo test --release one_raid -- --ignored --nocapture`. #[test] #[ignore] fn one_raid() { let n: usize = std::env::var("SAMS").ok().and_then(|v| v.parse().ok()).unwrap_or(150); let smart = std::env::var("SMART").is_ok(); let size: u32 = std::env::var("SIZE").ok().and_then(|v| v.parse().ok()).unwrap_or(36); for seed in 1..=12 { let mut s = Setup::new(n, smart, seed); s.size = size; let r = fly(&s); eprintln!("seed {seed}: sent {} kills {} leaked {} missiles {} ticks {}", r.launched, r.kills, r.leaked, r.missiles, r.ticks); } } /// Far launches over mountains where only a disc round the player is known: who crashes, where and how often. /// `cargo test --release far_table -- --ignored --nocapture` (DISC=300 sets the radius, 0 for all known). #[test] #[ignore] fn far_table() { let disc: f64 = std::env::var("DISC").ok().and_then(|v| v.parse().ok()).unwrap_or(300.0); let dist: f64 = std::env::var("DIST").ok().and_then(|v| v.parse().ok()).unwrap_or(2500.0); let mut by_kind: HashMap = HashMap::new(); let (mut launched, mut kills, mut leaked, mut tickets) = (0, 0, 0, 0); for seed in std::env::var("SEED").ok().and_then(|v| v.parse::().ok()).map_or(1..=8u64, |v| v..=v) { let mut s = Setup::new(50, std::env::var("SMART").is_ok(), seed); s.hilly = true; s.mountains = true; s.dist = dist; s.disc = (disc > 0.0).then_some(disc); let r = fly(&s); launched += r.launched; kills += r.kills; leaked += r.leaked; tickets += r.tickets; for c in &r.crashed { by_kind.entry(c.0).or_default().0 += 1; if std::env::var("ROWS").is_ok() { let (x, z) = (c.1.x as i32, c.1.z as i32); eprintln!(" seed {seed} {:?} crashed at {:.0},{:.0},{:.0} ({:.0} from origin, land {}), {:.0} from its aim", c.0, c.1.x, c.1.y, c.1.z, c.1.x.hypot(c.1.z), Loader::new(&s).height(x, z), c.2); } } } let mut v: Vec<_> = by_kind.into_iter().collect(); v.sort_by_key(|k| k.0 as u8); eprintln!("disc {disc} dist {dist}: launched {launched} kills {kills} leaked {leaked} chunk tickets {tickets}; crashed by kind {:?}", v.iter().map(|k| (k.0.clone(), k.1.0)).collect::>()); } /// Interceptors that blew up short of a ridge (`STEEP=1.6`, `SAMS=15`, `HILLS=0` for flat): by kind, and what the target was doing. #[test] #[ignore] fn scrub_table() { let n: usize = std::env::var("SAMS").ok().and_then(|v| v.parse().ok()).unwrap_or(15); let hilly = std::env::var("HILLS").map_or(true, |v| v != "0"); let (mut fired, mut scrubbed, mut kills, mut launched, mut leaked) = (0, 0, 0, 0, 0); let mut by: HashMap = HashMap::new(); let (mut near, mut low) = (0, 0); for seed in 1..=8u64 { let mut s = Setup::new(n, false, seed); s.radars = 2; s.objects = 4; s.hilly = hilly; let r = fly(&s); fired += r.missiles; scrubbed += r.scrubbed; kills += r.kills; launched += r.launched; leaked += r.leaked; for c in &r.scrubs { by.entry(format!("{:?}", c.0)).or_default().0 += 1; near += u32::from(c.2 < 150.0); low += u32::from(c.3 < 30.0); } } let mut v: Vec<_> = by.into_iter().collect(); v.sort(); eprintln!("sams {n} hilly {hilly}: launched {launched} kills {kills} leaked {leaked} missiles {fired} scrubbed {scrubbed} (target within 150: {near}, target under 30 over land: {low}) by kind {:?}", v.iter().map(|k| (k.0.clone(), k.1.0)).collect::>()); }