From a6632b4acf289cd17c14e7c0ec60ff95985d7f01 Mon Sep 17 00:00:00 2001 From: acamilo Date: Thu, 1 Oct 2026 13:10:05 +0000 Subject: [PATCH] macros: the mart's list cursor is the entry too; the catch test pins its first-window ball (row 71) state::shop reports the buy and sell lists' cursor as row plus wListScrollOffset, only while the list is on screen. MART_CURSOR_ROWS stays. The catch ROM test now asserts its ball sits in the bag window's first rows, so it covers the unscrolled throw beside row 71's scrolled one. --- docs/design/macros-wram.md | 6 ++-- docs/design/macros.md | 11 +++++-- .../src/pokemon_red/macros/cartridge.rs | 5 +++ .../flybrain-gb/src/pokemon_red/state.rs | 32 +++++++++++++------ .../src/pokemon_red/state/tests.rs | 11 +++++++ .../flysim/crates/flysim/tests/rom_catch.rs | 7 ++++ 6 files changed, 58 insertions(+), 14 deletions(-) diff --git a/docs/design/macros-wram.md b/docs/design/macros-wram.md index 9aea241..6ea508b 100644 --- a/docs/design/macros-wram.md +++ b/docs/design/macros-wram.md @@ -1072,6 +1072,8 @@ the cartridge instead. **`BattleMenu::Bag`'s `cursor`** is now `wCurrentMenuItem + wListScrollOffset`, the bag entry under the arrow, which is the sum the cartridge makes on A. In the live trap the arrow was on row 1 -with an offset of 3, so it was on entry 4, the Poké Ball. The mart's buy list -(`MART_CURSOR_ROWS`, section 7) still uses the first three rows only. +with an offset of 3, so it was on entry 4, the Poké Ball. `state::shop`'s buy and sell lists +report their cursor (and its `max`) as the entry the same way. The offset is added only while a +list is on screen, because the byte outlives every list. Purchases still use only the first three +rows (`MART_CURSOR_ROWS`, section 7). diff --git a/docs/design/macros.md b/docs/design/macros.md index 448cf8f..e53b6a5 100644 --- a/docs/design/macros.md +++ b/docs/design/macros.md @@ -2142,11 +2142,16 @@ restored the same battle with the same bag open, so the trap came straight back. the moves, `SWITCH`, `RUN` and the bag's `BACK` at low HP, so the dead button became the only one. -The mart's buy list scrolls the same way, and `MART_CURSOR_ROWS` (row 55) still limits purchases -to the first three rows. Lifting that limit with the pinned offset is a separate change. +Other lists, checked: the mart's buy and sell lists are `DisplayListMenuID` lists too, and +`state::shop` now reports their cursor as the entry in the same way. The battle menu, the move +list, the party list and the start menu do not scroll. No macro navigates the PC's lists. +`MART_CURSOR_ROWS` (row 55) still limits purchases to the first three rows, because lifting it puts +new buttons on the pad and needs its own survey on the cartridge. Tests: `a_battle_frame_with_a_cursor_accepting_input_is_the_flys_turn` (state, a bag scrolled by -three), +three), `a_mart_reports_which_of_its_screens_is_up` (a scrolled buy list, and a stale offset on the +counter menu), the catch test's check that its ball is in the window's first rows (it still +throws), `row71_a_ball_past_the_bags_window_is_thrown` (`FLY_ROW71_CHECKPOINT`, the live trap) and `row71_the_list_scroll_offset_is_where_the_cartridge_reads_it` (FLY_ROM). diff --git a/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/cartridge.rs b/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/cartridge.rs index 5eb6046..80ee959 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/cartridge.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/cartridge.rs @@ -118,6 +118,11 @@ pub const CHEAPEST_PURCHASE: u32 = { /// can name, and a purchase aimed at one is a button whose script gives up before it presses /// anything. [`super::palette::stock_index`] is where the rule is applied, once, for both the pad /// and the plan. +/// +/// Row 71 pinned `wListScrollOffset` from the cartridge, and `state::shop` now reports the buy +/// list's cursor as the entry (row plus offset), so the reading is right however the list is +/// scrolled. The limit stays until a purchase past the third row has been surveyed on the +/// cartridge: lifting it would put new buttons on the pad, which is its own change. pub const MART_CURSOR_ROWS: usize = 3; /// Cursor indices of the battle menu, as `state::BattleMenu::Main` documents them: "0 FIGHT, 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 a8b660d..69f1c35 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/state.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/state.rs @@ -1370,19 +1370,33 @@ pub fn shop(memory: &mut dyn MemoryReader) -> Option { if text_box(memory).waiting { return Some(Shop { screen: ShopScreen::Talking, cursor }); } - let screen = if mart_item_window_drawn(memory) { - ShopScreen::Buying - } else { - ShopScreen::BuySellQuit - }; - return Some(Shop { screen, cursor }); + if mart_item_window_drawn(memory) { + return Some(Shop { screen: ShopScreen::Buying, cursor: list_entry(memory, cursor) }); + } + return Some(Shop { screen: ShopScreen::BuySellQuit, cursor }); } if read(memory, ram::wTextBoxID) != poke::BUY_SELL_QUIT_MENU { return None; } - let screen = - if list == poke::ITEM_LIST_MENU { ShopScreen::Selling } else { ShopScreen::BuySellQuit }; - Some(Shop { screen, cursor }) + if list == poke::ITEM_LIST_MENU { + return Some(Shop { screen: ShopScreen::Selling, cursor: list_entry(memory, cursor) }); + } + Some(Shop { screen: ShopScreen::BuySellQuit, cursor }) +} + +/// A `DisplayListMenuID` cursor as the entry it is on rather than the window's row (row 71). +/// +/// The arrow walks the window's rows and the list scrolls under it, so the entry is the row plus +/// [`poke::LIST_SCROLL_OFFSET`], and the last entry the window can reach is its last row plus the +/// same. Only for a list that is on screen: the offset is one shared byte that outlives every list, +/// so a menu that is not a list menu keeps its own cursor. +fn list_entry(memory: &mut dyn MemoryReader, cursor: Cursor) -> Cursor { + let offset = read(memory, poke::LIST_SCROLL_OFFSET); + Cursor { + current: cursor.current.saturating_add(offset), + max: cursor.max.saturating_add(offset), + ..cursor + } } /// Whether the mart's priced item window is the thing drawn over the counter menu. 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 bb7af83..ea8183c 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 @@ -586,6 +586,17 @@ fn a_mart_reports_which_of_its_screens_is_up() { .screen_tile(poke::MART_NAME_COLUMN, poke::MART_NAME_ROW, poke::CHAR_UPPER_A); assert_eq!(shop(&mut wram).map(|shop| shop.screen), Some(ShopScreen::Buying)); + // Row 71: the buy list's cursor is the entry, the window's row plus the scroll offset, and so + // is the last entry the window reaches. + wram.cursor(4, 5, 1, 2, poke::pad::A | poke::pad::B).set(poke::LIST_SCROLL_OFFSET, 2); + let buying = shop(&mut wram).unwrap(); + assert_eq!((buying.cursor.current, buying.cursor.max), (3, 4)); + // The counter menu is not a list menu: a stale offset leaves its cursor alone. + wram.screen_tile(poke::MART_NAME_COLUMN, poke::MART_NAME_ROW, 0x7f); + let counter = shop(&mut wram).unwrap(); + assert_eq!(counter.screen, ShopScreen::BuySellQuit); + assert_eq!((counter.cursor.current, counter.cursor.max), (1, 2)); + // The bag list on its own is the start menu's, not a mart's. let mut wram = Wram::overworld(); wram.set(ram::wFontLoaded, poke::BIT_FONT_LOADED) diff --git a/services/flysim/crates/flysim/tests/rom_catch.rs b/services/flysim/crates/flysim/tests/rom_catch.rs index 1003225..6b8b544 100644 --- a/services/flysim/crates/flysim/tests/rom_catch.rs +++ b/services/flysim/crates/flysim/tests/rom_catch.rs @@ -210,6 +210,13 @@ fn a_catch_on_the_cartridge_pays_the_catch_rule_once_with_the_species_in_its_lab return; } eprintln!("bag holds {balls} balls"); + // Row 71's other half: this checkpoint's ball is in the bag window's first rows, unscrolled, + // so the catch below is the throw that worked before the scroll offset was read and must + // still work after it. + let ball_entry = state::bag(&mut Run::resume(&rom, &checkpoint).gb) + .iter() + .position(|item| (0x01..=0x04).contains(&item.id) && item.count > 0); + assert!(ball_entry.is_some_and(|entry| entry < 3), "ball at bag entry {ball_entry:?}"); // 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