diff --git a/services/flysim/crates/flysim/tests/rom_macros_mode.rs b/services/flysim/crates/flysim/tests/rom_macros_mode.rs index 7940255..78ae774 100644 --- a/services/flysim/crates/flysim/tests/rom_macros_mode.rs +++ b/services/flysim/crates/flysim/tests/rom_macros_mode.rs @@ -4554,3 +4554,133 @@ fn row65_the_fly_leaves_the_cerulean_badge_houses_back_yard() { assert!(worst_empty < 600, "an empty pad in the yard for {worst_empty} frames: {:?}", run.started); assert!(left.is_some(), "the fly never left the yard: {:?} {:?}", run.route, run.started); } + +/// The live row-67 checkpoint, or `None` to skip. +/// +/// v0.6.5, rung 10, hot generation 230739, pulled while the fly walked back to the Pewter Gym +/// from its last whiteout: Pewter City (18, 13), Squirtle L11 (TACKLE, TAIL WHIP, BUBBLE, all +/// full), money 0, the gym's Jr. Trainer (Diglett L11, Sandshrew L11) not yet beaten. +fn row67_checkpoint() -> Option { + std::env::var_os("FLY_ROW67_CHECKPOINT").map(|path| { + flysim::store::load(std::path::Path::new(&path)) + .expect("the checkpoint should be a FLYSIM01 envelope") + }) +} + +/// Row 67: the Pewter Gym trainer lost thirty times in a row is the readout's, not the pad's. +/// +/// **What was live** (2026-09-29, v0.6.5): an hour and a half in the gym, `NEXT` 1,332, `MOVE 2` +/// 83, `MOVE 3` 13, `MOVE 1` 0 in 3,000 events, no battle won. Per battle the readout chose TAIL +/// WHIP six turns in seven, Squirtle fainted, the fly whited out to Pewter and walked straight +/// back in (`infra/docs/macros-traps.md` row 67, `docs/design/macros.md` 12.31). +/// +/// The claims, from the live checkpoint with the real palette and a uniform choice per hold (a +/// harness choice, not the fly's): +/// +/// - **on every frame of the fly's own turn the pad deals a `MOVE n` for every move that has PP +/// and that the cartridge would not answer with nothing** -- BUBBLE is always there to be +/// pressed, so the pad is not the trap; +/// - **the trainer is beaten** inside the budget: a readout with no favourite wins this battle +/// with what the pad offers. The live readout's measured favourite (TAIL WHIP 87%) lost 17 of +/// 17 over the same budget, two seeds (`infra/docs/macros-traps.md` row 67). +#[test] +fn row67_the_gym_trainer_is_beaten_from_what_the_pad_deals() { + use flybrain_gb::pokemon_red::macros::PokemonPalette; + use flybrain_gb::pokemon_red::state; + use flybrain_gb::pokemon_red::symbols::{events, ram}; + use flybrain_gb::{MacroPalette, MemoryReader, Started}; + let rom = skip_without_rom!(); + let Some(checkpoint) = row67_checkpoint() else { + eprintln!("skipped: no FLY_ROW67_CHECKPOINT"); + return; + }; + let mut run = Run::resume(&rom, MacroMode::Macros, &checkpoint); + assert_eq!(run.map(), PEWTER_CITY, "the checkpoint is in Pewter City, on the way back"); + assert!(!run.event(events::EVENT_BEAT_PEWTER_GYM_TRAINER_0), "the trainer is not yet beaten"); + + let budget = 60_000u32; + let hold_frames = 48u32; + let mut palette = PokemonPalette::new(SEED); + let mut rng = 1u32; + let mut running = false; + let mut since_decision = hold_frames; + let mut ms = run.ms; + let mut own_turn_frames = 0u32; + let mut useful_move_missing = 0u32; + let mut starts = std::collections::BTreeMap::<&'static str, u32>::new(); + let mut battles: Vec<(u32, bool)> = Vec::new(); + let mut fighting = run.gb.read8(ram::wIsInBattle); + let mut beaten_at = None; + for frame in 0..budget { + palette.clock(ms); + let observed = { + let ledger = AdapterLedger(&run.adapter); + palette.observe(&mut run.gb, &ledger) + }; + let names: Vec<&str> = observed.bindings.iter().map(|binding| binding.name).collect(); + if let Some(battle) = state::battle(&mut run.gb) + && battle.own_turn + && matches!(battle.menu, flybrain_gb::pokemon_red::macros::state::BattleMenu::Main { .. }) + && !state::a_latched(&mut run.gb) + && let Some(own) = battle.own + { + own_turn_frames += 1; + for (slot, name) in ["MOVE 1", "MOVE 2", "MOVE 3", "MOVE 4"].iter().enumerate() { + if let Some(entry) = own.moves[slot].filter(|entry| entry.id != 0 && entry.pp > 0) + && state::move_without_effect(&mut run.gb, entry.id) != Some(true) + && !names.contains(name) + { + useful_move_missing += 1; + } + } + } + let mut mask = 0u8; + { + let ledger = AdapterLedger(&run.adapter); + if running { + match palette.step(&mut run.gb, &ledger) { + Some(held) => mask = held, + None => running = false, + } + } else if since_decision >= hold_frames && !observed.bindings.is_empty() { + since_decision = 0; + rng ^= rng << 13; + rng ^= rng >> 17; + rng ^= rng << 5; + let binding = &observed.bindings[(rng >> 8) as usize % observed.bindings.len()]; + if let Started::Running(_) = palette.start(binding.slot, &mut run.gb, &ledger) { + *starts.entry(binding.name).or_default() += 1; + running = true; + match palette.step(&mut run.gb, &ledger) { + Some(held) => mask = held, + None => running = false, + } + } + } + } + since_decision += 1; + run.gb.set_buttons(mask); + run.gb.run_frame().expect("a frame should complete"); + ms += MS_PER_FRAME; + run.adapter.sample(&mut run.gb, ms); + let now = run.gb.read8(ram::wIsInBattle); + if fighting == 2 && now != 2 { + // `$ff` is the frame a battle is lost; anything else after a trainer battle is a win. + battles.push((frame, now != 0xff)); + } + fighting = now; + if run.event(events::EVENT_BEAT_PEWTER_GYM_TRAINER_0) { + beaten_at = Some(frame); + break; + } + } + eprintln!( + "{:.1} brain minutes: trainer beaten at {beaten_at:?}; trainer battles (frame, won) \ + {battles:?}; own-turn frames {own_turn_frames}, a useful move missing from the pad on \ + {useful_move_missing}; starts {starts:?}", + (ms - run.ms) / 60_000.0, + ); + assert!(own_turn_frames > 0, "the run never reached the trainer's battle"); + assert_eq!(useful_move_missing, 0, "a move with PP and an effect was not on the pad"); + assert!(beaten_at.is_some(), "the trainer was not beaten in {budget} frames: {battles:?}"); +}