From 6eeff4a73574e734f8d7853b6d71a414deafc3f6 Mon Sep 17 00:00:00 2001 From: acamilo Date: Wed, 30 Sep 2026 19:15:28 +0000 Subject: [PATCH] macros: the SHIFT offer's party list is the fly's turn, not a forced switch (row 70) EnemySendOut opens the party list with BATTLE_PARTY_MENU when the fly answers YES to "Will change POKeMON?", the value ChooseNextMon uses after a faint. Read as a forced switch its pad was SWITCH, NEXT with no B: live in Mt. Moon after the first catch, NEXT on the Pokemon already out for 58 brain minutes. A forced switch now also needs the active battler at 0 HP; the offer's pad is the party list's SWITCH and BACK. Unit tests for both lists; ROM tests from the live trap (FLY_ROW70_CHECKPOINT) and from a real forced switch after a faint (FLY_ROW70_FORCED_CHECKPOINT); macros.md 12.33, macros-traps row 70. --- docs/design/macros.md | 35 +++ infra/docs/macros-traps.md | 37 ++++ .../flybrain-gb/src/pokemon_red/fake_wram.rs | 15 +- .../src/pokemon_red/scene/tests.rs | 8 + .../flybrain-gb/src/pokemon_red/state.rs | 25 ++- .../src/pokemon_red/state/tests.rs | 27 +++ .../crates/flysim/tests/rom_macros_mode.rs | 204 ++++++++++++++++++ 7 files changed, 344 insertions(+), 7 deletions(-) diff --git a/docs/design/macros.md b/docs/design/macros.md index 280e77a..6179bad 100644 --- a/docs/design/macros.md +++ b/docs/design/macros.md @@ -2079,6 +2079,41 @@ Nothing presses for the fly on the keyboard but the bound's one button, dealt as chosen by the fly's own macro group. The decoder, the reward catalog, the adapter version, the roles and the compatibility string are untouched. +### 12.33 The SHIFT offer's party list is the fly's turn, not a forced switch (2026-09-30, row 70) + +Live on v0.7.0, rung 12, Mt. Moon 1F: the fly caught its first Pokémon (a Zubat). Minutes later, a +Lass's battle. Wartortle fainted to her Oddish, the forced switch sent out the Zubat, and the Zubat +knocked the Oddish out. With two Pokémon in the party, the cartridge's SHIFT battle style asks +"Will ZUBAT change POKéMON?" before the trainer's next one (`EnemySendOut`, +`engine/battle/core.asm`). The fly answered YES, and the game opened the party list. From then on, +the event log was `NEXT start/done` every 0.8 brain seconds for 58 brain minutes, and nothing +else happened until the ladder reset the run to rung 11. + +- **The misread.** `EnemySendOut` opens that list with `wPartyMenuTypeOrMessageID = + BATTLE_PARTY_MENU`, the same value `ChooseNextMon` uses after a faint. `state::battle` took + BATTLE_PARTY_MENU alone as the forced switch, so the pad was the forced switch's `SWITCH, NEXT`. + `SWITCH` had no one to switch to, because the Wartortle had fainted. `NEXT`'s A landed on the + cursor, which the game parks on the Pokémon already out. The game printed "ZUBAT is already out!" + and reopened the list. The one press that leaves this list is B, which declines the offer: + `DisplayPartyMenu` returns carry and the trainer sends the next one out. No button on the pad + sent B. +- **The rule.** A battle party list is a forced switch only when BATTLE_PARTY_MENU is set **and + the active battler's HP is 0**. Both callers of `ChooseNextMon` (`HandlePlayerMonFainted`, and + the enemy's faint after a double knock-out) reach it only with `wBattleMonHP` at 0, so the faint + is what the forced switch has and the offer does not. On the offer's list it is the fly's turn, + and its pad is the party list's own answers, `SWITCH` and `BACK` (12.9). `SWITCH` is dealt only + when another Pokémon can go out. Nothing is chosen for the fly. It can still take the offer with + `SWITCH`, and `BACK` is the way out that the pad was missing. +- **Why it waited for a catch.** The offer needs a second Pokémon (`wPartyCount - 1 != 0`), so + with a party of one it never appeared. Every catch from now on makes it likelier. + +Tests: `the_shift_offers_party_list_is_the_flys_turn_and_its_way_out_is_back`, the second half of +`the_party_list_a_fainted_mon_forces_is_a_forced_switch`, and two ROM tests. +`row70_the_shift_offers_party_list_is_declined_and_the_battle_goes_on` uses +`FLY_ROW70_CHECKPOINT`, the live trap. `row70_the_list_after_a_faint_is_still_a_forced_switch_and_is_answered` +uses `FLY_ROW70_FORCED_CHECKPOINT`, the rung-12 archive carried forward by the stub to the same +Lass with a Zubat caught. + ## 13. Shops and Pokémon Centers (the operator, 2026-09-17: "refactor the shop macros. make it a ## priority to visit the shop at least once per area; make shop macros item purchases. same ## for the Pokécenter. heal should be a macro.") diff --git a/infra/docs/macros-traps.md b/infra/docs/macros-traps.md index b3de9dc..15fa6a7 100644 --- a/infra/docs/macros-traps.md +++ b/infra/docs/macros-traps.md @@ -3592,3 +3592,40 @@ survey cannot say which. **Status: shipped as the operator decided, not a fix.** The trap is still possible under it: one of three `v8` starts never won. Row 67's watchdog check 10 (`unwon-battles`, on its branch) is the detector, and the pad rule the operator declined is the lever that still exists. + +## 2026-09-30, row 70: Mt. Moon, the SHIFT offer's party list, and the ladder that took the catch + +### What was live + +On v0.7.0 with adapter v8, the ladder reset the run to rung 11 at 16:54Z. The rung-11 archive +replayed deterministically: Route 3's trainers, Mt. Moon (rung 12) at 17:40, a Poké Ball bought, +thrown, and a Zubat caught at 17:47 (`OWNED #41`, `CAUGHT #107`). At 17:52 a Lass's battle on 1F +started. From then on the log was `NEXT start/done` every 0.8 brain seconds and nothing else: no +reward and no other macro for 58 brain minutes. Check 10 flagged `dominant`/`sequence` (`NEXT` +100%). At 18:50 the ladder, at level 2 since the 16:54 reset, reset the run to rung 11 again and +erased the catch. The replay after that reset matched the one before it event for event (the same +`brainMs`), so it was heading into the same trap at about 19:48. + +### Reproduction (durable generation 250877, pulled from the reset's archive) + +`wIsInBattle` 2 and the Lass (`wTrainerClass` 3), with Oddish beaten and Bellsprout L11 next. +Wartortle L19 was at 0/58, Zubat L8 at 24/26 and out. The party list was open with +`wPartyMenuTypeOrMessageID` = BATTLE_PARTY_MENU and the cursor on the Zubat. The screen read +"ZUBAT is already out!". The seam read `forced_switch`, so the pad was `SWITCH, NEXT`, and `SWITCH` +had no healthy Pokémon to go to. Driven from the checkpoint on v0.7.0, the pad was `NEXT` on +6,000 of 6,000 frames: 125 `NEXT` starts, every one "already out" and the list again. + +| # | trap | trigger | test | fix, or why it is left | +| --- | --- | --- | --- | --- | +| 70 | the SHIFT style's "Will change POKéMON?" offer opens the party list with BATTLE_PARTY_MENU, which the seam read as `ChooseNextMon`'s forced switch. Its pad `SWITCH, NEXT` had no B; `NEXT` on the Pokémon already out rings forever | any trainer battle with two or more Pokémon in the party, the fly answering YES (the first catch made it possible) | `the_shift_offers_party_list_is_the_flys_turn_and_its_way_out_is_back`, `row70_the_shift_offers_party_list_is_declined_and_the_battle_goes_on` (`FLY_ROW70_CHECKPOINT`), `row70_the_list_after_a_faint_is_still_a_forced_switch_and_is_answered` (`FLY_ROW70_FORCED_CHECKPOINT`) | **fixed**: forced switch = BATTLE_PARTY_MENU **and** the active battler at 0 HP (`docs/design/macros.md` 12.33). The offer is the fly's turn, and its pad is `SWITCH` (when someone can go out) and `BACK` | + +ROM tests on the build box (rom-env sourced): + +| test | v0.7.0 | branch | +| --- | --- | --- | +| `row70_the_shift_offers_party_list_is_declined_and_the_battle_goes_on` | 6,000 frames in the list, `NEXT` on all of them, `BACK` on none, never left. **Fails** | `BACK` from f3. "LASS sent out BELLSPROUT!", then the fly's turn at f194. Passes | +| `row70_the_list_after_a_faint_is_still_a_forced_switch_and_is_answered` | passes | 881 forced frames, all read as forced, `SWITCH` on all of them, `BACK` on none. The Zubat goes out, and the fly's turn comes at f1,102. Passes | + +The stub reaches row 70 by itself. From the live rung-12 archive (Mt. Moon's mouth, one ball), +three uniform-stub seeds each catch a Zubat within 4,000 frames and meet the same Lass. There +Wartortle faints at f27,361, f34,656 and f35,284. diff --git a/services/flysim/crates/flybrain-gb/src/pokemon_red/fake_wram.rs b/services/flysim/crates/flybrain-gb/src/pokemon_red/fake_wram.rs index 1a01f6c..c7951be 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/fake_wram.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/fake_wram.rs @@ -337,10 +337,14 @@ impl Wram { .screen_tile(poke::MOVE_LIST_JOIN, top, poke::frame::BOTTOM_RIGHT) } - /// The party list. `forced` is the state `ChooseNextMon` leaves: A only, no way out. + /// The party list. `forced` is the state `ChooseNextMon` leaves: A only, no way out, and the + /// battler that fainted at 0 HP (the only way the game reaches that list). pub fn party_list(&mut self, current: u8, forced: bool) -> &mut Self { let count = self.peek(ram::wPartyCount).max(1); let keys = if forced { poke::pad::A } else { poke::pad::A | poke::pad::B }; + if forced { + self.set(ram::wBattleMonHP, 0).set(ram::wBattleMonHP + 1, 0); + } self.set( ram::wPartyMenuTypeOrMessageID, if forced { poke::BATTLE_PARTY_MENU } else { 0 }, @@ -349,6 +353,15 @@ impl Wram { .cursor(1, 0, current, count - 1, keys) } + /// The party list the SHIFT style's "Will change POKéMON?" opens on YES (row 70): + /// BATTLE_PARTY_MENU like `ChooseNextMon`'s, but with the battler standing, and B declines it. + pub fn shift_offer_list(&mut self, current: u8) -> &mut Self { + let count = self.peek(ram::wPartyCount).max(1); + self.set(ram::wPartyMenuTypeOrMessageID, poke::BATTLE_PARTY_MENU) + .set(ram::wFontLoaded, poke::BIT_FONT_LOADED) + .cursor(1, 0, current, count - 1, poke::pad::A | poke::pad::B) + } + /// Money, as three bytes of big-endian BCD. pub fn money(&mut self, amount: u32) -> &mut Self { let digits = amount.min(999_999); diff --git a/services/flysim/crates/flybrain-gb/src/pokemon_red/scene/tests.rs b/services/flysim/crates/flybrain-gb/src/pokemon_red/scene/tests.rs index 90dd9f0..2b43636 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/scene/tests.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/scene/tests.rs @@ -185,6 +185,14 @@ fn the_party_list_a_fainted_mon_forces_is_a_forced_switch() { wram.party_list(0, true); assert_eq!(detect(&mut wram), Scene::Battle { own_turn: false, forced_switch: true }); + // Row 70: the same BATTLE_PARTY_MENU list with the active Pokémon standing is the SHIFT + // style's offer before a trainer's next Pokémon ("Will change POKéMON?", YES). B + // declines it, so it is the fly's turn, not a forced switch. + let mut wram = battle_frame(2); + wram.party_mon(1, 16, 8, 24, 24, 0, &[(33, 35)]); + wram.shift_offer_list(0); + assert_eq!(detect(&mut wram), Scene::Battle { own_turn: true, forced_switch: false }); + // The same list opened from the menu's PKMN entry is not forced: it sets NORMAL_PARTY_MENU // and it watches B, so the player can back out — and it is an ordinary part of the fly's turn, // because the game is waiting for it to choose. diff --git a/services/flysim/crates/flybrain-gb/src/pokemon_red/state.rs b/services/flysim/crates/flybrain-gb/src/pokemon_red/state.rs index c39eb72..860b7b8 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/state.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/state.rs @@ -693,13 +693,26 @@ pub fn battle(memory: &mut dyn MemoryReader) -> Option { BattleMenu::None }; - // A forced switch is the party list that `ChooseNextMon` opens: it is the only battle path - // that sets BATTLE_PARTY_MENU, where choosing PKMN from the menu above sets - // NORMAL_PARTY_MENU. - let forced_switch = matches!(menu, BattleMenu::Party { .. }) - && read(memory, ram::wPartyMenuTypeOrMessageID) == poke::BATTLE_PARTY_MENU; - + // A forced switch is the party list that `ChooseNextMon` opens after the active Pokémon + // fainted; choosing PKMN from the menu above sets NORMAL_PARTY_MENU instead. + // + // **BATTLE_PARTY_MENU alone is not enough** (row 70). `EnemySendOut` sets it too: in a trainer + // battle on the SHIFT style, with a second Pokémon in the party, the game asks "Will + // change POKéMON?" before the trainer's next one comes out, and YES opens this same list. + // That list is an offer, not a faint: B declines it (`DisplayPartyMenu` returns carry and the + // battle goes on), and the Pokémon already out cannot be chosen (" is already out!", + // then the list again). Read as a forced switch, its pad was `SWITCH` and `NEXT`; with the + // rest of the party fainted `SWITCH` had no one to switch to, and `NEXT`'s A on the cursor, + // which the game parks on the Pokémon already out, printed "already out" and reopened the + // list: live on rung 12 in Mt. Moon, 58 brain minutes of `NEXT` and nothing else. What tells + // the two apart is the one thing a faint changes: the active battler's HP is 0 on + // `ChooseNextMon`'s list, and not on the offer's. let own = own_mon(memory); + let active_alive = own.as_ref().is_some_and(|mon| mon.hp > 0); + let forced_switch = matches!(menu, BattleMenu::Party { .. }) + && read(memory, ram::wPartyMenuTypeOrMessageID) == poke::BATTLE_PARTY_MENU + && !active_alive; + // **The fly's turn is any menu of the battle that is accepting input**, not only the top-level // one (2026-09-17, live: an hour and forty-one minutes in Viridian Forest). // diff --git a/services/flysim/crates/flybrain-gb/src/pokemon_red/state/tests.rs b/services/flysim/crates/flybrain-gb/src/pokemon_red/state/tests.rs index b08bfdc..6ec67ad 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/state/tests.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/state/tests.rs @@ -338,6 +338,33 @@ fn a_forced_switch_is_the_party_list_that_cannot_be_cancelled() { assert!(cursor(&mut wram).cancellable()); } +/// Row 70: `EnemySendOut`'s SHIFT-style offer ("Will ZUBAT change POKéMON?", YES) opens the party +/// list with BATTLE_PARTY_MENU, the value `ChooseNextMon` uses. Read as a forced switch, its pad was +/// `SWITCH, NEXT`; with the only other Pokémon fainted `SWITCH` had no one to switch to, and +/// `NEXT`'s A on the cursor (parked on the Pokémon already out) printed "ZUBAT is already out!" and +/// reopened the list: 58 brain minutes live in Mt. Moon. The offer is told apart by the one thing a +/// faint changes, the active battler's HP, and its pad is the party list's own answers. +#[test] +fn the_shift_offers_party_list_is_the_flys_turn_and_its_way_out_is_back() { + use crate::pokemon_red::macros::palette::{MacroKind, scene_set}; + let mut wram = Wram::overworld(); + wram.party_mon(0, 179, 19, 0, 58, 0, &[(33, 35), (39, 14), (145, 26), (55, 25)]); + wram.party_mon(1, 107, 8, 24, 26, 0, &[(141, 13)]); + wram.battle_mon(1, 107, 8, 24, 26, 0, &[(141, 13)]) + .enemy_mon(188, 11, 33, 33) + .battle(2) + .shift_offer_list(1); + + let fight = battle(&mut wram).unwrap(); + assert_eq!(fight.menu, BattleMenu::Party { cursor: 1 }); + assert!(!fight.forced_switch, "the offer is not a faint"); + assert!(fight.own_turn); + let scene = crate::pokemon_red::scene::detect(&mut wram); + let dealt = scene_set(scene, &mut PokeState::new(&mut wram)); + assert!(dealt.contains(&MacroKind::Back), "B declines the offer: {dealt:?}"); + assert!(!dealt.contains(&MacroKind::Next), "NEXT's A on the Pokémon already out is the ring: {dealt:?}"); +} + /// Section 12.10: **a battle frame with a cursor accepting input is the fly's turn.** /// /// The four menus a battle waits on are the top-level one, the move list, the party list and the diff --git a/services/flysim/crates/flysim/tests/rom_macros_mode.rs b/services/flysim/crates/flysim/tests/rom_macros_mode.rs index 980ad75..f63dcf4 100644 --- a/services/flysim/crates/flysim/tests/rom_macros_mode.rs +++ b/services/flysim/crates/flysim/tests/rom_macros_mode.rs @@ -4933,3 +4933,207 @@ fn row67_the_gym_trainer_is_beaten_from_what_the_pad_deals() { 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:?}"); } + +/// A live row-70 checkpoint named by `var`, or `None` to skip. +/// +/// `FLY_ROW70_CHECKPOINT`: v0.7.0, rung 12, durable generation 250877, pulled from inside the +/// trap. Mt. Moon 1F, a Lass's battle (Oddish beaten, Bellsprout L11 next), Wartortle L19 fainted, +/// the Zubat L8 the fly had caught minutes before standing at 24/26, and the party list open under +/// "ZUBAT is already out!" -- the list the SHIFT style's "Will ZUBAT change POKéMON?" opens on YES. +/// +/// `FLY_ROW70_FORCED_CHECKPOINT`: the live rung-12 milestone archive carried forward by the stub +/// (uniform choice, seed 1, 27,361 frames): a Zubat caught, then the same Lass, and Wartortle +/// fainted to her Oddish -- `ChooseNextMon`'s list, the forced switch this row must not disturb. +fn row70_checkpoint(var: &str) -> Option { + std::env::var_os(var).map(|path| { + flysim::store::load(std::path::Path::new(&path)) + .expect("the checkpoint should be a FLYSIM01 envelope") + }) +} + +/// The real palette driven by a uniform choice per hold (a harness choice, not the fly's), one +/// frame at a time, as row 67's test drives it. +struct UniformDrive { + palette: flybrain_gb::pokemon_red::macros::PokemonPalette, + rng: u32, + running: bool, + since_decision: u32, + ms: f64, + starts: std::collections::BTreeMap<&'static str, u32>, +} + +impl UniformDrive { + const HOLD_FRAMES: u32 = 48; + + fn new(run: &Run) -> Self { + Self { + palette: flybrain_gb::pokemon_red::macros::PokemonPalette::new(SEED), + rng: 1, + running: false, + since_decision: Self::HOLD_FRAMES, + ms: run.ms, + starts: std::collections::BTreeMap::new(), + } + } + + /// Observes, decides, runs one frame; returns the names the pad dealt before the frame. + fn frame(&mut self, run: &mut Run) -> Vec<&'static str> { + use flybrain_gb::{MacroPalette, Started}; + self.palette.clock(self.ms); + let ledger = AdapterLedger(&run.adapter); + let observed = self.palette.observe(&mut run.gb, &ledger); + let names = observed.bindings.iter().map(|binding| binding.name).collect(); + let mut mask = 0u8; + if self.running { + match self.palette.step(&mut run.gb, &ledger) { + Some(held) => mask = held, + None => self.running = false, + } + } else if self.since_decision >= Self::HOLD_FRAMES && !observed.bindings.is_empty() { + self.since_decision = 0; + self.rng ^= self.rng << 13; + self.rng ^= self.rng >> 17; + self.rng ^= self.rng << 5; + let binding = &observed.bindings[(self.rng >> 8) as usize % observed.bindings.len()]; + if let Started::Running(_) = self.palette.start(binding.slot, &mut run.gb, &ledger) { + *self.starts.entry(binding.name).or_default() += 1; + self.running = true; + match self.palette.step(&mut run.gb, &ledger) { + Some(held) => mask = held, + None => self.running = false, + } + } + } + self.since_decision += 1; + run.gb.set_buttons(mask); + run.gb.run_frame().expect("a frame should complete"); + self.ms += MS_PER_FRAME; + run.adapter.sample(&mut run.gb, self.ms); + names + } +} + +/// Which battle party list is open: `Some(true)` for `ChooseNextMon`'s after a faint (the battler +/// at 0 HP), `Some(false)` for the SHIFT style's offer (BATTLE_PARTY_MENU, the battler standing), +/// `None` for anything else. Read from the cartridge's bytes, not through the seam under test. +fn row70_list(gb: &mut Emulator) -> Option { + use flybrain_gb::MemoryReader; + use flybrain_gb::pokemon_red::macros::state::BattleMenu; + use flybrain_gb::pokemon_red::state; + use flybrain_gb::pokemon_red::symbols::ram; + let hp = u16::from(gb.read8(ram::wBattleMonHP)) << 8 | u16::from(gb.read8(ram::wBattleMonHP + 1)); + let open = gb.read8(ram::wIsInBattle) != 0 + && gb.read8(ram::wPartyMenuTypeOrMessageID) == 2 + && state::battle(gb).is_some_and(|battle| matches!(battle.menu, BattleMenu::Party { .. })); + open.then_some(hp == 0) +} + +/// Whether the seam reads the fly's own turn at the top-level battle menu. +fn row70_main_turn(gb: &mut Emulator) -> bool { + use flybrain_gb::pokemon_red::macros::state::BattleMenu; + flybrain_gb::pokemon_red::state::battle(gb) + .is_some_and(|battle| battle.own_turn && matches!(battle.menu, BattleMenu::Main { .. })) +} + +/// Row 70: the SHIFT style's party list is an offer the fly can decline, not a forced switch. +/// +/// **What was live** (2026-09-30, v0.7.0): after the fly's first catch, a trainer battle in +/// Mt. Moon asked "Will ZUBAT change POKéMON?" before the trainer's next Pokémon. The fly said +/// YES, and `EnemySendOut` opened the party list with BATTLE_PARTY_MENU, the value +/// `ChooseNextMon` uses after a faint. The seam read a forced switch; its pad was `SWITCH, NEXT`, +/// `SWITCH` had no one to switch to, and `NEXT`'s A on the Pokémon already out printed "already +/// out" and reopened the list: `NEXT` and nothing else for 58 brain minutes. +/// +/// From the live checkpoint: `NEXT` is on no frame of the offer's list, `BACK` is, the list is +/// left, and the fly gets its turn against the trainer's next Pokémon. (On v0.7.0: 6,000 frames +/// in the list, `NEXT` on every one, 125 `NEXT` starts.) +#[test] +fn row70_the_shift_offers_party_list_is_declined_and_the_battle_goes_on() { + use flybrain_gb::MemoryReader; + use flybrain_gb::pokemon_red::symbols::ram; + let rom = skip_without_rom!(); + let Some(checkpoint) = row70_checkpoint("FLY_ROW70_CHECKPOINT") else { + eprintln!("skipped: no FLY_ROW70_CHECKPOINT"); + return; + }; + let mut run = Run::resume(&rom, MacroMode::Macros, &checkpoint); + assert_eq!(row70_list(&mut run.gb), Some(false), "the checkpoint is inside the offer's party list"); + + let mut drive = UniformDrive::new(&run); + let (mut offer_frames, mut next_on_offer, mut back_on_offer) = (0u32, 0u32, 0u32); + let mut turn_after = None; + for frame in 0..6_000u32 { + let offer = row70_list(&mut run.gb) == Some(false); + let names = drive.frame(&mut run); + if offer { + offer_frames += 1; + next_on_offer += u32::from(names.contains(&"NEXT")); + back_on_offer += u32::from(names.contains(&"BACK")); + } else if row70_main_turn(&mut run.gb) { + turn_after = Some(frame); + break; + } + } + eprintln!( + "offer frames {offer_frames}, NEXT dealt on {next_on_offer}, BACK on {back_on_offer}; the \ + fly's turn against the next Pokémon at {turn_after:?} (enemy {:#04x}); starts {:?}", + run.gb.read8(ram::wEnemyMonSpecies), + drive.starts, + ); + assert_eq!(next_on_offer, 0, "NEXT's A on the Pokémon already out is the ring"); + assert!(back_on_offer > 0, "B declines the offer, so BACK is on its pad"); + assert!(turn_after.is_some(), "the offer was never left: {:?}", drive.starts); + assert_eq!(row70_list(&mut run.gb), None, "the list is closed"); +} + +/// Row 70's other side: the party list `ChooseNextMon` opens after a faint is still a forced +/// switch. Its pad is `SWITCH` (and the `NEXT` that confirms the cursor), never a `BACK` the game +/// would ignore; the fly sends out the Pokémon still standing and gets its turn with it. +#[test] +fn row70_the_list_after_a_faint_is_still_a_forced_switch_and_is_answered() { + use flybrain_gb::MemoryReader; + use flybrain_gb::pokemon_red::macros::state::GameState; + use flybrain_gb::pokemon_red::symbols::ram; + let rom = skip_without_rom!(); + let Some(checkpoint) = row70_checkpoint("FLY_ROW70_FORCED_CHECKPOINT") else { + eprintln!("skipped: no FLY_ROW70_FORCED_CHECKPOINT"); + return; + }; + let mut run = Run::resume(&rom, MacroMode::Macros, &checkpoint); + assert_eq!(row70_list(&mut run.gb), Some(true), "the checkpoint is inside ChooseNextMon's list"); + + let mut drive = UniformDrive::new(&run); + let (mut forced_frames, mut read_forced) = (0u32, 0u32); + let (mut switch_on_forced, mut back_on_forced) = (0u32, 0u32); + let mut turn_after = None; + for frame in 0..6_000u32 { + let forced = row70_list(&mut run.gb) == Some(true); + if forced { + let ledger = AdapterLedger(&run.adapter); + let mut state = + flybrain_gb::pokemon_red::state::PokeState::with_ledger(&mut run.gb, &ledger); + read_forced += u32::from(state.battle().is_some_and(|battle| battle.forced_switch)); + } + let names = drive.frame(&mut run); + if forced { + forced_frames += 1; + switch_on_forced += u32::from(names.contains(&"SWITCH")); + back_on_forced += u32::from(names.contains(&"BACK")); + } else if row70_main_turn(&mut run.gb) { + turn_after = Some(frame); + break; + } + } + let out = run.gb.read8(ram::wPlayerMonNumber); + eprintln!( + "forced frames {forced_frames} (read forced on {read_forced}), SWITCH dealt on \ + {switch_on_forced}, BACK on {back_on_forced}; the fly's turn at {turn_after:?} with party \ + slot {out} out; starts {:?}", + drive.starts, + ); + assert_eq!(read_forced, forced_frames, "every frame of ChooseNextMon's list reads as a forced switch"); + assert!(switch_on_forced > 0, "SWITCH is on the forced switch's pad"); + assert_eq!(back_on_forced, 0, "the game ignores B on this list, so BACK is not dealt"); + assert!(turn_after.is_some(), "the forced switch was never answered: {:?}", drive.starts); + assert_eq!(out, 1, "the Pokémon still standing (the Zubat, slot 1) was sent out"); +}