//! Rewind's snapshot is complete: a run restored to a snapshot continues as //! the run that was not interrupted, frame for frame (picture, sound, RAM, //! counters), with controls changing on the way. Needs the ROM set at //! rom/galaxian/; skips without it. use std::fs; use std::path::PathBuf; use z80_wasm::galaxian::{roms, Galaxian}; #[test] fn restore_continues_as_the_uninterrupted_run() { let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("rom/galaxian"); if !dir.exists() { eprintln!("rom/galaxian/ not present; skipping"); return; } let roms = roms::load(|name| fs::read(dir.join(name)).ok()).unwrap(); let mut game = Galaxian::new(&roms, true).unwrap(); // The controls a frame holds: a coin, start, then moving and firing. let controls = |frame: u32| -> Vec<&'static str> { match frame { 400..=405 => vec!["coin_1"], 500..=505 => vec!["start_1"], f if f > 700 => { let mut held = vec![if (f / 50) % 2 == 0 { "player_1_left" } else { "player_1_right" }]; if f % 8 < 2 { held.push("player_1_fire"); } held } _ => vec![], } }; const ALL: [&str; 5] = [ "coin_1", "start_1", "player_1_left", "player_1_right", "player_1_fire", ]; let step = |game: &mut Galaxian, frame: u32| { let held = controls(frame); for name in ALL { game.machine .board_mut() .set_input(name, held.contains(&name)) .unwrap(); } game.run_frame().unwrap(); let mut rgba = vec![0; 224 * 256 * 4]; game.frame.portrait_rgba(&mut rgba); ( rgba, game.pcm, game.machine.board().work_ram, game.machine.vblank_count, ) }; for frame in 0..1000 { step(&mut game, frame); } let snapshot = game.snapshot(); let straight: Vec<_> = (1000..1600).map(|f| step(&mut game, f)).collect(); // Somewhere else entirely, then back. for frame in 2000..2300 { step(&mut game, frame); } game.restore(&snapshot); let again: Vec<_> = (1000..1600).map(|f| step(&mut game, f)).collect(); let first_difference = straight.iter().zip(&again).position(|(a, b)| a != b); assert_eq!(first_difference, None, "frames after the restore differ"); }