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 ce75a01..4b42bcb 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/state.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/state.rs @@ -119,6 +119,10 @@ pub mod poke { /// four tall, then writes a horizontal run over its top-left corner and a `┘` over (10, 12). /// Values rather than symbols, like `YES_NO_BOX`: this is a figure on screen, not a byte. pub const MOVE_LIST_BOX: (u16, u16, u16, u16) = (4, 12, 19, 17); + /// The bag's own box in a battle (row 66): `DisplayListMenuID` draws a `TextBoxBorder` at + /// (4, 2), nine rows by fourteen, so its corners are (4, 2) and (19, 12). Surveyed on the + /// cartridge: whole on every frame the list is up, gone on the frame it closes. + pub const ITEM_LIST_BOX: (u16, u16, u16, u16) = (4, 2, 19, 12); pub const MOVE_LIST_JOIN: u16 = 10; /// Where `MoveSelectionMenu` parks the shared cursor: row 12, column 5. pub const MOVE_LIST_CURSOR_Y: u8 = 12; @@ -631,12 +635,19 @@ pub fn battle(memory: &mut dyn MemoryReader) -> Option { BattleMenu::Moves { cursor: slot, count } } else if party_list(memory) { BattleMenu::Party { cursor: cursor.current } - } else if read(memory, ram::wListMenuID) == poke::ITEM_LIST_MENU { + } else if read(memory, ram::wListMenuID) == poke::ITEM_LIST_MENU && item_list_drawn(memory) { // The bag, opened from the battle menu's ITEM entry. `DisplayListMenuID` keeps its // position in the same shared cursor every other menu uses, and the entry count is the // bag's own, so the scripts that reach into it (`ITEM`, `THROW BALL`) navigate by reading // rather than by counting presses -- which is what section 4 requires of them and what // they could not do while this read as no list at all. + // + // **And only while its box is on screen** (row 66), row 50's rule for the move list: + // `wListMenuID` is written when the list opens and never cleared, so after a ball was + // thrown every frame to the end of the battle -- "All right! WEEDLE was caught!", the + // Pokédex page, the nickname offer and the naming screen -- read as an open bag, the pad + // was `BACK` and `THROW BALL`, and the ball's A presses typed the nickname: 6,900 frames + // from the catch to the end of the battle, under a `THROW BALL` that threw nothing. let count = bag(memory).len().min(usize::from(poke::BAG_CAPACITY)); BattleMenu::Bag { cursor: cursor.current, count: u8::try_from(count).unwrap_or(0) } } else { @@ -1083,6 +1094,12 @@ fn two_option_box_drawn(memory: &mut dyn MemoryReader, cursor_x: u8, cursor_y: u /// becomes a horizontal run and (10, 12) becomes the `┘` junction with the PP box above. The /// mimic and relearn menus draw at row 7 and never reach a battle's own turn. Read whole, like /// every other box in this module, because a single tile id is an ordinary character. +/// Whether the battle bag's list box is whole on screen ([`poke::ITEM_LIST_BOX`], row 66). +fn item_list_drawn(memory: &mut dyn MemoryReader) -> bool { + let (left, top, right, bottom) = poke::ITEM_LIST_BOX; + border_drawn(memory, left, top, right, bottom) +} + fn move_list_drawn(memory: &mut dyn MemoryReader) -> bool { let (left, top, right, bottom) = poke::MOVE_LIST_BOX; if screen_tile(memory, left, top) != poke::frame::HORIZONTAL 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 5aa3af4..a461e0b 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 @@ -382,6 +382,13 @@ fn a_battle_frame_with_a_cursor_accepting_input_is_the_flys_turn() { let mut wram = Wram::overworld(); battler(&mut wram); wram.bag(&[(crate::pokemon_red::macros::cartridge::item::POTION, 2)]).set(ram::wListMenuID, poke::ITEM_LIST_MENU); + // Row 66: the list byte alone is not the bag. It is never cleared, so it outlives the list + // into the rest of the battle -- after a ball, the catch, the Pokédex page, the nickname. + let fight = battle(&mut wram).unwrap(); + assert_eq!(fight.menu, BattleMenu::None, "the list byte without the list's box on screen"); + assert!(!fight.own_turn, "text after the bag closed is nobody's turn"); + let (left, top, right, bottom) = poke::ITEM_LIST_BOX; + wram.draw_box(left, top, right, bottom); let fight = battle(&mut wram).unwrap(); assert_eq!(fight.menu, BattleMenu::Bag { cursor: 0, count: 1 }); assert!(fight.own_turn, "the battle bag is a cursor accepting input"); diff --git a/services/flysim/crates/flysim/tests/rom_catch.rs b/services/flysim/crates/flysim/tests/rom_catch.rs index f731ebd..5f6feea 100644 --- a/services/flysim/crates/flysim/tests/rom_catch.rs +++ b/services/flysim/crates/flysim/tests/rom_catch.rs @@ -54,7 +54,15 @@ const SEED: u32 = 20_260_922; const HOT: f64 = 16.0; const REST: f64 = 10.0; /// `THROW BALL`'s channel (`pokemon_red::macros::palette`). +/// +/// Row 66's note: this is the button's *tag*; its channel is `macro_throw_ball` +/// ([`THROW_BALL_CHANNEL`]), so the lean below sets a rate the decoder does not read and the drive +/// in a wild battle is the calibrated tie. It is kept as it is because that drive is the one that +/// catches from the forest checkpoint (with the real channel hot the checkpoint's one ball misses +/// on this frame timing), and the row 66 test reuses it up to the catch. const BALL: &str = "MB·BALL"; +/// `THROW BALL`'s channel as the macro layer binds it. +const THROW_BALL_CHANNEL: &str = "macro_throw_ball"; /// Frames the stub leans on one channel before the rotation moves on, the shape of the real /// group's hysteresis-then-fatigue rotation. const BURST_FRAMES: u32 = 24; @@ -190,9 +198,9 @@ fn a_catch_on_the_cartridge_pays_the_catch_rule_once_with_the_species_in_its_lab eprintln!("skipped: no FLY_CATCH_CHECKPOINT"); return; }; - let mut run = Run::resume(&rom, &checkpoint); - let balls = run.balls(); + let balls = Run::resume(&rom, &checkpoint).balls(); if balls == 0 { + let run = Run::resume(&rom, &checkpoint); eprintln!( "skipped: the checkpoint's bag holds no ball (map {:#04x}). `BUY BALL` can buy one \ inside a mart; point FLY_CATCH_CHECKPOINT at a state that already has one.", @@ -200,24 +208,47 @@ fn a_catch_on_the_cartridge_pays_the_catch_rule_once_with_the_species_in_its_lab ); return; } - eprintln!("bag holds {balls} balls; map {:#04x}", run.adapter.map_id().unwrap_or(u32::MAX)); + eprintln!("bag holds {balls} balls"); - // Twenty brain minutes is generous for a forest checkpoint: the live run threw 28 balls in - // its first Viridian Forest session (`pokemon_red::macros::palette`). - let budget = 20 * 60 * 60; - let mut battles = 0u32; - let mut was_in_battle = false; - for _ in 0..budget { - run.step(); - let now = run.in_wild_battle(); - if now && !was_in_battle { - battles += 1; + // Five brain minutes per attempt is generous for a forest checkpoint: the live run threw 28 + // balls in its first Viridian Forest session (`pokemon_red::macros::palette`). An attempt + // starts after `5 * attempt` frames with nothing pressed, so each one meets the wild battle, + // and the ball meets the random number, on a different frame (row 66): one ball misses about + // two throws in three, and which throw a checkpoint's one ball is depends on frame timing + // that any change to the pad moves. + let budget = 5 * 60 * 60; + let mut battles; + let mut attempt = 0u32; + let mut run = loop { + let mut run = Run::resume(&rom, &checkpoint); + for _ in 0..attempt * 5 { + run.ms += MS_PER_FRAME; + run.frame += 1; + let evaluated = run + .legacy + .stub_advance(Some(&mut run.layer), &mut run.gb, &mut run.adapter, run.ms) + .expect("a frame should complete"); + run.payouts.extend(evaluated.rewards); } - was_in_battle = now; - if !run.catches().is_empty() { - break; + battles = 0; + let mut was_in_battle = false; + for _ in 0..budget { + run.step(); + let now = run.in_wild_battle(); + if now && !was_in_battle { + battles += 1; + } + was_in_battle = now; + if !run.catches().is_empty() { + break; + } } - } + if !run.catches().is_empty() || attempt == 23 { + break run; + } + eprintln!("attempt {attempt}: no catch ({battles} wild battles, {} balls left)", run.balls()); + attempt += 1; + }; let catches = run.catches(); assert!( @@ -255,3 +286,136 @@ fn a_catch_on_the_cartridge_pays_the_catch_rule_once_with_the_species_in_its_lab // sets the bit the `species` rule reads, whether or not it was new to this run. assert!(run.balls() < balls, "a ball was spent"); } + +/// What one drive from the checkpoint saw after its catch. +struct AfterCatch { + caught_at: u32, + ended_at: u32, + /// Frames between the catch and the battle's end on which `THROW BALL` was on the pad. + ball_dealt: u32, + /// Every pad the fly could decide on in that stretch. + pads: std::collections::BTreeSet>, +} + +/// One drive: `idle` frames with nothing pressed first (so each attempt reaches the wild battle +/// on a different frame, and the ball meets a different random number), then the first test's +/// drive up to the catch, then the pad's own buttons in turn. `None` when no ball kept anything +/// within the budget: one ball in the bag misses about two throws in three. +fn drive_past_a_catch(rom: &[u8], checkpoint: &flysim::store::Checkpoint, idle: u32) -> Option { + let mut run = Run::resume(rom, checkpoint); + for _ in 0..idle { + run.ms += MS_PER_FRAME; + run.frame += 1; + let evaluated = run + .legacy + .stub_advance(Some(&mut run.layer), &mut run.gb, &mut run.adapter, run.ms) + .expect("a frame should complete"); + run.payouts.extend(evaluated.rewards); + } + let budget = 5 * 60 * 60; + let mut caught_at: Option = None; + let mut ball_dealt = 0u32; + let mut pads = std::collections::BTreeSet::new(); + let mut hold = 0usize; + for frame in 0..budget { + let bound = run.layer.bound_channels(); + let hot = if caught_at.is_none() { + if run.in_wild_battle() { + Some(BALL.to_string()) + } else { + let slot = (run.frame / BURST_FRAMES) as usize % run.channels.len(); + Some(run.channels[slot].to_string()) + } + } else if bound.is_empty() { + None + } else { + if frame % BURST_FRAMES == 0 { + hold += 1; + } + Some(bound[hold % bound.len()].clone()) + }; + if caught_at.is_some() { + if bound.iter().any(|channel| channel == THROW_BALL_CHANNEL) { + ball_dealt += 1; + } + if run.layer.running().is_none() && !bound.is_empty() { + pads.insert(bound.clone()); + } + } + let active = run.decoder.decode_bound(&rates(hot.as_deref()), run.ms, false, None, Some(&bound)); + run.legacy.execute(Some(&mut run.layer), &active, 0, run.ms, &mut run.gb, &run.adapter); + run.ms += MS_PER_FRAME; + run.frame += 1; + let evaluated = run + .legacy + .stub_advance(Some(&mut run.layer), &mut run.gb, &mut run.adapter, run.ms) + .expect("a frame should complete"); + run.payouts.extend(evaluated.rewards); + if caught_at.is_none() && run.byte(ram::wCapturedMonSpecies) != 0 { + caught_at = Some(frame); + } + if let Some(caught_at) = caught_at + && run.byte(ram::wIsInBattle) == 0 + { + assert!(!run.catches().is_empty(), "the catch pays at the battle's end"); + return Some(AfterCatch { caught_at, ended_at: frame, ball_dealt, pads }); + } + } + assert!(caught_at.is_none(), "a battle still running {budget} frames after its catch"); + None +} + +/// Row 66: once a ball has kept a Pokémon, nothing to the end of the battle is the bag. +/// +/// After the throw `wListMenuID` still says `ITEMLISTMENU` -- it is written when a list opens and +/// never cleared -- so on v0.6.5 every frame from "All right! … was caught!" through the Pokédex +/// page, the nickname offer and the naming screen read as an open battle bag. The pad was `BACK` +/// and `THROW BALL`, a `THROW BALL` that threw nothing and whose A presses typed the nickname; +/// driven from the rung-8 survey checkpoint the battle took 6,900 frames to end after the catch. +/// +/// The drive after the catch leans on the pad's own buttons in turn (not the whole channel +/// list), so what it measures is what the pad offers, not how often a rotation over every channel +/// lands on it. +#[test] +fn row66_after_a_catch_the_pad_is_not_the_bag_and_the_battle_ends() { + let Some(rom) = rom() else { + eprintln!("skipped: FLY_ROM is not set"); + return; + }; + let Some(checkpoint) = checkpoint() else { + eprintln!("skipped: no FLY_CATCH_CHECKPOINT"); + return; + }; + if Run::resume(&rom, &checkpoint).balls() == 0 { + eprintln!("skipped: the checkpoint's bag holds no ball"); + return; + } + let after = (0..24) + .find_map(|attempt| { + let after = drive_past_a_catch(&rom, &checkpoint, attempt * 5); + if after.is_none() { + eprintln!("attempt {attempt}: the ball missed"); + } + after + }) + .expect("one of 24 attempts should keep a Pokémon"); + eprintln!( + "caught at f{}, battle over at f{} ({} frames); THROW BALL dealt on {} of them; pads {:?}", + after.caught_at, + after.ended_at, + after.ended_at - after.caught_at, + after.ball_dealt, + after.pads + ); + assert_eq!(after.ball_dealt, 0, "THROW BALL on the pad after the catch: {:?}", after.pads); + assert!( + after.pads.iter().all(|pad| !pad.iter().any(|channel| channel == "macro_back")), + "BACK on the pad after the catch, with no list up: {:?}", + after.pads + ); + assert!( + after.ended_at - after.caught_at < 3_000, + "the battle took {} frames to end after the catch", + after.ended_at - after.caught_at + ); +}