From 86149dca9b87d2224ee02d67f21071d2472ec22f Mon Sep 17 00:00:00 2001 From: acamilo Date: Thu, 24 Sep 2026 04:06:50 +0000 Subject: [PATCH] executor: a confirming A waits for the cartridge to have let go of the last one The Potion's party list reads open before the game asks the joypad again, and until it asks the A that opened it is still in hJoyLast: an A pressed there is no edge. ITEM confirmed on that frame, reported done over a list still waiting, and the fly backed out with BACK, BACK. --- .../src/pokemon_red/macros/executor.rs | 16 +++++ .../src/pokemon_red/macros/tests.rs | 67 +++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/executor.rs b/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/executor.rs index 9c0c36a..d1cc28b 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/executor.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/executor.rs @@ -1725,6 +1725,22 @@ fn cursor_frame(cursor: &mut Cursor, state: &mut dyn MacroState) -> Progress { if !cursor.confirm { return Progress::Next; } + // **The confirming A waits for the cartridge to have seen A let go** (row 63). A list + // opened by an A reads open from its cursor bytes before the game has asked the joypad + // again, and until it asks, the A that opened it is still down in `hJoyLast`: an A + // pressed into that window is no edge and the cartridge never sees it. The Potion's party + // list is the case that found it -- one Pokémon, so the cursor is on its target on the + // first frame and the confirm went straight into the window, `ITEM` reported done over a + // list that was still waiting, and the fly backed out of it and the bag with two `BACK`s. + // A wait presses nothing, and it is the same budget as a list that is not up at all. + if state.a_latched() { + cursor.waited += 1; + return if cursor.waited > CURSOR_WAIT { + Progress::Blocked + } else { + Progress::Hold(buttons::NONE) + }; + } cursor.confirmed = true; cursor.phase = 0; return pulse(buttons::A, &mut cursor.phase, PRESS_HOLD, PRESS_GAP); diff --git a/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/tests.rs b/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/tests.rs index 26ed293..32a0a6e 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/tests.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/tests.rs @@ -168,6 +168,14 @@ struct World { opens_draw_in: u32, /// The list an A press opened and the frame it starts accepting input on. pending: Option<(u32, Opens)>, + /// Frames the cartridge keeps the A that opened a list as its last joypad reading before it + /// asks again (row 63): an A pressed inside them is no edge and changes nothing, while the + /// list already reads open. Zero everywhere but the tests this is for. + a_latch: u32, + /// The frame the cartridge next asks the joypad, while an A is latched. + latched_until: u32, + /// Item ids the cartridge would refuse from the battle bag on this frame (row 63). + refused_items: BTreeSet, /// A scene the world switches to at this frame, for the abort rule. switch: Option<(u32, Scene)>, /// A frame at which the cartridge heals the party, which is what a Pokémon Center does while @@ -260,6 +268,9 @@ impl World { opens: VecDeque::new(), opens_draw_in: 0, pending: None, + a_latch: 0, + latched_until: 0, + refused_items: BTreeSet::new(), switch: None, heal_at: None, box_open: false, @@ -468,9 +479,14 @@ impl World { self.list = List::None; } } + if mask == buttons::A && self.frames < self.latched_until { + // The cartridge has not asked since the A that opened this list: this A is no edge. + return; + } if mask == buttons::A && let Some(next) = self.opens.pop_front() { + self.latched_until = self.frames + self.a_latch; if self.opens_draw_in > 0 { self.pending = Some((self.frames + self.opens_draw_in, next)); } else { @@ -703,6 +719,14 @@ impl MacroState for World { self.no_effect.contains(&id) } + fn item_refused(&mut self, id: u8) -> bool { + self.refused_items.contains(&id) + } + + fn a_latched(&mut self) -> bool { + self.frames < self.latched_until + } + fn shop_stock(&mut self) -> Vec { self.stock.clone() } @@ -2310,6 +2334,49 @@ fn item_reaches_the_potion_by_reading_the_bags_cursor() { ); } +#[test] +fn items_last_press_waits_for_the_cartridge_to_let_go_of_the_potions_a() { + // Row 63, Mt. Moon B2F: Wartortle out at 8 of 86, one Pokémon, a Potion in the bag. The A + // that chose the Potion opened the party list, whose cursor bytes read open at once and whose + // one entry is already under the cursor -- so `ITEM` confirmed on the first frame, while the + // cartridge still held that A as its last reading. No edge, no press: `ITEM` reported done over + // a list still waiting for its answer, and the fly left it and the bag with `BACK`, `BACK`. + let mut world = World::battle(); + world.mons = vec![mon(0, 8, 86, &[(44, 0), (55, 25)])]; + world.bag = vec![(item::ANTIDOTE, 1), (item::POTION, 1), (0xea, 1)]; + world.list = List::BattleBag; + world.grid = false; + world.cursor = 0; + world.cursor_max = 2; + // Measured: the list reads open at once, and an A is lost until about 30 frames after the + // Potion's A was let go. This fake takes a press on its release, eight frames after the edge + // the cartridge would see, so its window is those 30 and the 8 and a little: 40. + world.a_latch = 40; + world.opens.push_back(Opens { list: List::BattleParty, cursor: 0, max: 0, grid: false }); + // What the honoured A does: the Potion is used, and the screen is battle text. + world.opens.push_back(Opens { list: List::None, cursor: 0, max: 0, grid: false }); + assert_eq!(run(&mut world, MacroKind::Item).unwrap(), MacroAbort::Done); + assert_eq!(world.list, List::None, "the party list was answered, not left waiting"); + assert!(world.opens.is_empty(), "both A presses were taken: {:?}", world.pulses); +} + +#[test] +fn a_confirm_is_not_held_back_when_nothing_is_latched() { + // The wait costs nothing where there is nothing to wait for: a list that reads open with the + // cartridge's last reading clear is confirmed on the frame it is reached, as before. + let mut world = World::battle(); + world.mons = vec![mon(0, 8, 86, &[(44, 0), (55, 25)])]; + world.bag = vec![(item::POTION, 1)]; + world.list = List::BattleBag; + world.grid = false; + world.cursor_max = 0; + world.opens.push_back(Opens { list: List::BattleParty, cursor: 0, max: 0, grid: false }); + world.opens.push_back(Opens { list: List::None, cursor: 0, max: 0, grid: false }); + assert_eq!(run(&mut world, MacroKind::Item).unwrap(), MacroAbort::Done); + assert_eq!(world.list, List::None); + assert!(world.frames < 120, "no wait: {} frames", world.frames); +} + #[test] fn buy_potion_takes_buy_then_the_item_then_confirms_twice() { let mut world = World::room();