//! A picture of every flight in a stretch of play: for designing paths. Plays //! a movie up to frame FROM on the unmodified board, lays a path mod over it //! there (the power-on test is long over, so its checksums do not matter), keeps //! playing the movie's inputs to frame TO, and draws each enemy's whole //! flight path over the last frame. The real board flies the paths, so the //! picture is exact, not a simulation. Usage: //! galaga-pathshot MOVIE.json FROM TO OUT.png [--mod mods/NAME.paths] [--idle] [--rom DIR] //! //! --idle: from FROM on the ship does nothing (no moving, no shooting), so //! every flight is drawn whole: the stage as a script. It prints difficulty //! measures over the flights: frames on screen (fewer is harder) and speed. //! //! Stage starts in movies/tas-seed1.json are in its note; start FROM a //! hundred frames before one to see that stage's entrances. use std::path::PathBuf; use emu_galaga::galaga::movie::Movie; use emu_galaga::galaga::objects::objects; use emu_galaga::galaga::paths::PathMod; use emu_galaga::galaga::roms::load_directory; use emu_galaga::galaga::video::{Video, SCREEN_HEIGHT, SCREEN_WIDTH}; use emu_galaga::galaga::Machine; const SCALE: usize = 3; fn main() { let args: Vec = std::env::args().skip(1).collect(); if args.len() < 4 { eprintln!("usage: galaga-pathshot MOVIE.json FROM TO OUT.png [--mod FILE] [--rom DIR]"); std::process::exit(2); } let movie = Movie::from_json(&std::fs::read_to_string(&args[0]).unwrap()).unwrap(); let from: u64 = args[1].parse().expect("FROM"); let to: u64 = args[2].parse().expect("TO"); let out = PathBuf::from(&args[3]); let mut path_mod = None; let mut idle = false; let mut rom_dir = PathBuf::from("rom/galaga"); let mut rest = args[4..].iter(); while let Some(a) = rest.next() { match a.as_str() { "--mod" => { path_mod = Some(PathMod::load(std::path::Path::new(rest.next().unwrap())).unwrap()) } "--rom" => rom_dir = PathBuf::from(rest.next().unwrap()), "--idle" => idle = true, _ => panic!("unknown argument {a}"), } } let roms = load_directory(&rom_dir).unwrap(); let mut machine = Machine::new(&roms); movie.setup(&mut machine).unwrap(); let mut video = Video::new(&roms); if let Some(m) = movie.path_mod().unwrap() { m.apply_video(&mut video); } if let Some(m) = &path_mod { m.apply_video(&mut video); } let mut trails: Vec>> = vec![Vec::new(); 64]; let mut flying = [false; 64]; for (frame, c) in movie.inputs().into_iter().enumerate().take(to as usize) { if frame as u64 == from { if let Some(m) = &path_mod { m.check(&roms.sub).unwrap(); m.apply_over(&mut machine, &roms).unwrap(); } } machine.set_controls(if idle && frame as u64 >= from { [0, 0] } else { c }); machine.run_frame(); if (frame as u64) < from { continue; } for o in objects(&machine.shared.borrow()).iter().take(0x30) { let n = usize::from(o.slot); let now = matches!(o.state, 0x03 | 0x07 | 0x09); if now && !flying[n] { trails[n].push(Vec::new()); } if now { trails[n].last_mut().unwrap().push(o.center()); } flying[n] = now; } } video.render(&machine.shared.borrow(), machine.videolatch_at_vblank_end); let mut rgba = vec![0u8; SCREEN_WIDTH * SCREEN_HEIGHT * 4]; video.screen_rgba(&mut rgba); // Dim the picture so the paths stand out; paint the off-screen margin. let margin = 40; let (w, h) = ( (SCREEN_WIDTH + 2 * margin) * SCALE, (SCREEN_HEIGHT + 2 * margin) * SCALE, ); let mut img = vec![18u8; w * h * 3]; for y in 0..SCREEN_HEIGHT * SCALE { for x in 0..SCREEN_WIDTH * SCALE { let s = ((y / SCALE) * SCREEN_WIDTH + x / SCALE) * 4; let d = ((y + margin * SCALE) * w + x + margin * SCALE) * 3; for c in 0..3 { img[d + c] = rgba[s + c] / 3; } } } let colors: [[u8; 3]; 6] = [ [64, 190, 255], [255, 77, 90], [77, 255, 128], [255, 90, 255], [255, 210, 60], [180, 140, 255], ]; let mut flights = 0; for (n, list) in trails.iter().enumerate() { for (k, trail) in list.iter().enumerate() { flights += 1; let color = colors[(n + k) % colors.len()]; for pair in trail.windows(2) { let (a, b) = (pair[0], pair[1]); // Skip the wrap of the 9-bit coordinates. if (a.0 - b.0).abs() > 40 || (a.1 - b.1).abs() > 40 { continue; } let (x0, y0) = ( (a.0 + margin as i32) * SCALE as i32, (a.1 + margin as i32) * SCALE as i32, ); let (x1, y1) = ( (b.0 + margin as i32) * SCALE as i32, (b.1 + margin as i32) * SCALE as i32, ); let steps = (x1 - x0).abs().max((y1 - y0).abs()).max(1); for i in 0..=steps { let x = x0 + (x1 - x0) * i / steps; let y = y0 + (y1 - y0) * i / steps; if x >= 0 && y >= 0 && (x as usize) < w && (y as usize) < h { let d = (y as usize * w + x as usize) * 3; img[d..d + 3].copy_from_slice(&color); } } } if let Some(&(x, y)) = trail.first() { // A dot where the flight starts. for dy in -3..=3i32 { for dx in -3..=3i32 { let (px, py) = ( (x + margin as i32) * SCALE as i32 + dx, (y + margin as i32) * SCALE as i32 + dy, ); if px >= 0 && py >= 0 && (px as usize) < w && (py as usize) < h { let d = (py as usize * w + px as usize) * 3; img[d..d + 3].copy_from_slice(&[255, 255, 255]); } } } } } } // Screen border. for x in margin * SCALE..(margin + SCREEN_WIDTH) * SCALE { for &y in &[margin * SCALE, (margin + SCREEN_HEIGHT) * SCALE - 1] { let d = (y * w + x) * 3; img[d..d + 3].copy_from_slice(&[90, 90, 90]); } } for y in margin * SCALE..(margin + SCREEN_HEIGHT) * SCALE { for &x in &[margin * SCALE, (margin + SCREEN_WIDTH) * SCALE - 1] { let d = (y * w + x) * 3; img[d..d + 3].copy_from_slice(&[90, 90, 90]); } } let mut ppm = format!("P6 {w} {h} 255\n").into_bytes(); ppm.extend_from_slice(&img); let tmp = out.with_extension("ppm"); std::fs::write(&tmp, ppm).unwrap(); let ok = std::process::Command::new("ffmpeg") .args(["-loglevel", "error", "-y", "-i"]) .arg(&tmp) .arg(&out) .status() .map(|s| s.success()) .unwrap_or(false); if ok { let _ = std::fs::remove_file(&tmp); } // Difficulty measures over the flights: frames on screen and speed. let (mut on_screen, mut moved, mut steps) = (0usize, 0f64, 0usize); let mut shortest = usize::MAX; let mut shortest_at = (0, 0, 0); // A flight of a frame or two at the parked position is an object being // set up, not an enemy: leave those out. let mut counted = 0; for trail in trails.iter().flatten().filter(|t| t.len() >= 3) { counted += 1; let visible = trail .iter() .filter(|&&(x, y)| (0..SCREEN_WIDTH as i32).contains(&x) && (16..272).contains(&y)) .count(); on_screen += visible; if visible < shortest { shortest = visible; let (x, y) = trail[0]; shortest_at = (x, y, trail.len()); } for pair in trail.windows(2) { let d = (((pair[0].0 - pair[1].0).pow(2) + (pair[0].1 - pair[1].1).pow(2)) as f64).sqrt(); if d < 20.0 { moved += d; steps += 1; } } } if flights > 0 { println!( "on screen {:.0} frames per flight on average (shortest {shortest}, a flight of {} frames from ({},{})); speed {:.2} px/frame", on_screen as f64 / counted.max(1) as f64, shortest_at.2, shortest_at.0, shortest_at.1, moved / steps.max(1) as f64 ); } println!( "{flights} flights, frames {from}..{to} -> {}", if ok { out.display().to_string() } else { tmp.display().to_string() } ); }