executor: a purchase answers the price box the cartridge drew, and is done when paid

After the quantity the mart prints "That will be" and waits for a press
(a cont), then prints the price and only then draws its YES/NO box. The
script's one blind A after a settle was that press, so BUY BALL reported
done before the box existed: 55 starts, 55 done, the wallet unchanged,
from the rung-11 Pewter checkpoint. Step::Prompt pulses A until the box
is drawn (yes_no_prompt, row 56's reading), Step::Paid waits for the
wallet to drop; either running out is blocked. The fake mart now prints
its price, draws the box and charges for a YES (loop review row 62).
This commit is contained in:
acamilo 2026-09-24 02:42:30 +00:00
parent 019bd02dfe
commit 9ad2feff5c
2 changed files with 140 additions and 8 deletions

View file

@ -120,6 +120,16 @@ const SETTLE_FRAMES: u32 = 20;
/// because a battle spends whole seconds animating, and a wait presses nothing. /// because a battle spends whole seconds animating, and a wait presses nothing.
const CURSOR_WAIT: u32 = 180; const CURSOR_WAIT: u32 = 180;
/// Frames a purchase waits for the mart's YES/NO box before it gives up ([`Step::Prompt`]).
///
/// The price line is two dozen characters either side of one press, at the cartridge's text
/// speed; six brain seconds is twice what the Pewter counter took.
const PROMPT_WAIT: u32 = 360;
/// Frames between the presses [`Step::Prompt`] makes while the price line is up: one
/// [`PRESS_HOLD`] of A, then released, so the press is a new one each time.
const PROMPT_PULSE: u32 = 40;
/// Extra presses a cursor navigation may spend beyond twice the length of its list, to cover a /// Extra presses a cursor navigation may spend beyond twice the length of its list, to cover a
/// press the game swallows while a menu is still drawing. /// press the game swallows while a menu is still drawing.
const CURSOR_SLACK: u8 = 6; const CURSOR_SLACK: u8 = 6;
@ -343,6 +353,23 @@ enum Step {
/// the presses happened and the conversation may simply have been about something else -- so /// the presses happened and the conversation may simply have been about something else -- so
/// it reports `Next` and the close press still happens. /// it reports `Next` and the close press still happens.
Rested { waited: u32 }, Rested { waited: u32 },
/// Advance the price line until the mart's YES/NO box is drawn ([`MacroState::yes_no_prompt`]),
/// then move on to the press that answers it; `Blocked` if the box never comes.
///
/// A purchase's last answer (row 62). After the quantity the mart prints "POKe BALL? / That
/// will be" and **waits for a press** -- the line is a `cont`, with the blinking arrow -- and
/// only after it prints "200. OK?" and draws the box. The script's one blind A after a settle
/// was that press, so the box came up after the macro had reported `done`: 55 `BUY BALL`
/// starts on the cartridge, 55 `done`, the wallet at 1,606 throughout, and the box left to
/// whatever the pad dealt next. So this pulses A every [`PROMPT_PULSE`] frames while no box
/// is drawn -- a press on a line still printing only prints it faster -- and reads the box
/// the way `HEAL` reads the party. `before` is the wallet when the purchase began: if it has
/// dropped, a pulse already answered the box and the purchase is rung up.
Prompt { waited: u32, before: u32 },
/// Press nothing until the wallet reads less than `before`, which is the purchase rung up;
/// `Blocked` if it never does. The cartridge adds the item and takes the money a few frames
/// after the YES, so a purchase is `done` when the wallet says so and not when the press is.
Paid { waited: u32, before: u32 },
} }
/// Progress through one A* walk. /// Progress through one A* walk.
@ -1462,6 +1489,32 @@ fn advance(step: &mut Step, state: &mut dyn MacroState) -> Progress {
Progress::Hold(buttons::NONE) Progress::Hold(buttons::NONE)
} }
} }
Step::Paid { waited, before } => {
if state.money() < *before {
Progress::Next
} else if *waited >= PROMPT_WAIT {
Progress::Blocked
} else {
*waited += 1;
Progress::Hold(buttons::NONE)
}
}
Step::Prompt { waited, before } => {
if state.money() < *before {
Progress::Finished
} else if state.yes_no_prompt() {
Progress::Next
} else if *waited >= PROMPT_WAIT {
Progress::Blocked
} else {
*waited += 1;
if *waited % PROMPT_PULSE < PRESS_HOLD {
Progress::Hold(buttons::A)
} else {
Progress::Hold(buttons::NONE)
}
}
}
} }
} }
@ -2284,6 +2337,7 @@ fn approach(state: &mut dyn MacroState, targets: &[(Tile, TalkTarget)]) -> Vec<G
/// script works both from a freshly opened counter and from the buy list. /// script works both from a freshly opened counter and from the buy list.
fn shop_plan(state: &mut dyn MacroState, want: u8) -> Option<Vec<Step>> { fn shop_plan(state: &mut dyn MacroState, want: u8) -> Option<Vec<Step>> {
let index = stock_index(state, want)?; let index = stock_index(state, want)?;
let before = state.money();
let mut steps = Vec::new(); let mut steps = Vec::new();
if shop_screen(state)? == ShopScreen::BuySellQuit { if shop_screen(state)? == ShopScreen::BuySellQuit {
// BUY is the counter menu's first entry. // BUY is the counter menu's first entry.
@ -2292,10 +2346,12 @@ fn shop_plan(state: &mut dyn MacroState, want: u8) -> Option<Vec<Step>> {
} }
steps.push(cursor(index, true)); steps.push(cursor(index, true));
steps.push(settle()); steps.push(settle());
// The quantity prompt opens on one, and the price confirmation opens on YES. // The quantity prompt opens on one, and the price confirmation opens on YES -- once it is
// drawn, which is after the price has finished printing (row 62).
steps.push(press(buttons::A)); steps.push(press(buttons::A));
steps.push(settle()); steps.push(Step::Prompt { waited: 0, before });
steps.push(press(buttons::A)); steps.push(press(buttons::A));
steps.push(Step::Paid { waited: 0, before });
Some(steps) Some(steps)
} }

View file

@ -19,7 +19,7 @@ use crate::emulator::buttons;
use super::cartridge::{ use super::cartridge::{
BLOCKED_MINUTES_DEFAULT, CHEAPEST_PURCHASE, Edge, ExitId, FACINGS, LAST_MAP, MacroState, Objective, BLOCKED_MINUTES_DEFAULT, CHEAPEST_PURCHASE, Edge, ExitId, FACINGS, LAST_MAP, MacroState, Objective,
TalkTarget, TargetKey, TargetLedger, Targets, Tile, battle_entry, button, item, price, PURCHASES, TalkTarget, TargetKey, TargetLedger, Targets, Tile, battle_entry, button, item, price,
}; };
use super::geography::Amenity; use super::geography::Amenity;
use super::executor::{ use super::executor::{
@ -186,6 +186,14 @@ struct World {
/// beside a cursor the game never clears, and what the palette asks is only "is a choice /// beside a cursor the game never clears, and what the palette asks is only "is a choice
/// open" (section 12.12). /// open" (section 12.12).
prompt: bool, prompt: bool,
/// A mart's price confirmation (row 62): after this many more A presses in the shop the price
/// line starts printing, and the YES/NO box is drawn this many frames later. `None`: the fake
/// draws no price box at all, which is what it did before row 62.
price_after: Option<(u8, u32)>,
/// The frame the price box is drawn on, once the presses above have been made.
price_at: Option<u32>,
/// Purchases the fake mart has rung up: an A on its drawn price box.
bought: u32,
/// Whether the cartridge is driving the player right now ([`MacroState::scripted`]). /// Whether the cartridge is driving the player right now ([`MacroState::scripted`]).
scripted: bool, scripted: bool,
/// A frame at which the cartridge takes the joypad, which is what the Viridian gate does. /// A frame at which the cartridge takes the joypad, which is what the Viridian gate does.
@ -264,6 +272,9 @@ impl World {
heal_at: None, heal_at: None,
box_open: false, box_open: false,
prompt: false, prompt: false,
price_after: None,
price_at: None,
bought: 0,
scripted: false, scripted: false,
scripted_at: None, scripted_at: None,
pulses: Vec::new(), pulses: Vec::new(),
@ -422,6 +433,12 @@ impl World {
} }
self.heal_at = None; self.heal_at = None;
} }
if let Some(at) = self.price_at
&& self.frames >= at
{
self.prompt = true;
self.price_at = None;
}
if mask == buttons::NONE && self.previous != buttons::NONE { if mask == buttons::NONE && self.previous != buttons::NONE {
self.pulses.push(self.previous); self.pulses.push(self.previous);
self.on_pulse(self.previous); self.on_pulse(self.previous);
@ -468,6 +485,24 @@ impl World {
self.list = List::None; self.list = List::None;
} }
} }
if mask == buttons::A && self.scene == Scene::Shop {
if self.prompt {
// YES on the drawn price box: the one press that rings a purchase up, and the
// wallet pays for the item the list's cursor is on.
self.prompt = false;
self.bought += 1;
let item = self.stock.get(usize::from(self.cursor)).copied();
let price = PURCHASES.iter().find(|(id, _)| Some(*id) == item).map_or(0, |(_, p)| *p);
self.money = self.money.saturating_sub(price);
} else if let Some((left, print)) = self.price_after {
if left <= 1 {
self.price_after = None;
self.price_at = Some(self.frames + print);
} else {
self.price_after = Some((left - 1, print));
}
}
}
if mask == buttons::A if mask == buttons::A
&& let Some(next) = self.opens.pop_front() && let Some(next) = self.opens.pop_front()
{ {
@ -696,7 +731,7 @@ impl MacroState for World {
/// A drawn box is what the reading rests on, so a prompt cannot be open with no box open: /// A drawn box is what the reading rests on, so a prompt cannot be open with no box open:
/// `pokemon_red::state::yes_no_prompt` gates on `wFontLoaded` before it looks at the tiles. /// `pokemon_red::state::yes_no_prompt` gates on `wFontLoaded` before it looks at the tiles.
fn yes_no_prompt(&mut self) -> bool { fn yes_no_prompt(&mut self) -> bool {
self.prompt && self.scene == Scene::Dialog self.prompt && matches!(self.scene, Scene::Dialog | Scene::Shop)
} }
fn move_without_effect(&mut self, id: u8) -> bool { fn move_without_effect(&mut self, id: u8) -> bool {
@ -2317,6 +2352,8 @@ fn buy_potion_takes_buy_then_the_item_then_confirms_twice() {
world.list = List::Shop(ShopScreen::BuySellQuit); world.list = List::Shop(ShopScreen::BuySellQuit);
world.cursor_max = 2; world.cursor_max = 2;
world.stock = vec![item::POKE_BALL, item::POTION]; world.stock = vec![item::POKE_BALL, item::POTION];
// BUY, the item, the quantity, then the press the price line's `cont` waits for (row 62).
world.price_after = Some((4, 40));
world.money = 1000; world.money = 1000;
world.b_to_close = u8::MAX; world.b_to_close = u8::MAX;
// A on BUY opens the priced stock list. // A on BUY opens the priced stock list.
@ -2330,10 +2367,11 @@ fn buy_potion_takes_buy_then_the_item_then_confirms_twice() {
assert_eq!(world.cursor, 1, "the potion is the second thing the mart stocks"); assert_eq!(world.cursor, 1, "the potion is the second thing the mart stocks");
assert_eq!( assert_eq!(
world.pulses.iter().filter(|mask| **mask == buttons::A).count(), world.pulses.iter().filter(|mask| **mask == buttons::A).count(),
4, 5,
"BUY, the item, the quantity and the price: {:?}", "BUY, the item, the quantity, the price line and the YES: {:?}",
world.pulses world.pulses
); );
assert_eq!(world.bought, 1, "and the price box was answered once it was drawn");
} }
#[test] #[test]
@ -2345,12 +2383,14 @@ fn buy_ball_skips_the_counter_menu_when_the_buy_list_is_already_up() {
world.stock = vec![item::POKE_BALL, item::POTION]; world.stock = vec![item::POKE_BALL, item::POTION];
world.money = 1000; world.money = 1000;
world.b_to_close = u8::MAX; world.b_to_close = u8::MAX;
world.price_after = Some((3, 40));
assert_eq!(run(&mut world, MacroKind::BuyBall).unwrap(), MacroAbort::Done); assert_eq!(run(&mut world, MacroKind::BuyBall).unwrap(), MacroAbort::Done);
assert_eq!(world.bought, 1);
assert_eq!(world.cursor, 0); assert_eq!(world.cursor, 0);
assert_eq!( assert_eq!(
world.pulses.iter().filter(|mask| **mask == buttons::A).count(), world.pulses.iter().filter(|mask| **mask == buttons::A).count(),
3, 4,
"the item, the quantity and the price: {:?}", "the item, the quantity, the price line and the YES: {:?}",
world.pulses world.pulses
); );
} }
@ -4414,6 +4454,7 @@ fn a_purchase_navigates_by_the_items_place_in_the_stock_list() {
world.cursor_max = 3; world.cursor_max = 3;
world.stock = vec![item::POKE_BALL, item::ANTIDOTE, 15, 12]; world.stock = vec![item::POKE_BALL, item::ANTIDOTE, 15, 12];
world.money = 3_000; world.money = 3_000;
world.price_after = Some((3, 40));
assert_eq!(run(&mut world, MacroKind::BuyAntidote).unwrap(), MacroAbort::Done); assert_eq!(run(&mut world, MacroKind::BuyAntidote).unwrap(), MacroAbort::Done);
assert_eq!(world.cursor, 1, "the Antidote's own index"); assert_eq!(world.cursor, 1, "the Antidote's own index");
assert_eq!(world.pulses.last(), Some(&buttons::A), "the price confirmation"); assert_eq!(world.pulses.last(), Some(&buttons::A), "the price confirmation");
@ -5487,3 +5528,38 @@ fn a_trainer_walking_up_teaches_the_ledgers_nothing() {
assert!(!world.targets.blocked(world.map, north), "a challenge is not the road refusing"); assert!(!world.targets.blocked(world.map, north), "a challenge is not the road refusing");
assert!(world.pushes.is_empty(), "and the ground is as walkable as it was"); assert!(world.pushes.is_empty(), "and the ground is as walkable as it was");
} }
#[test]
fn a_purchase_answers_the_price_box_once_the_cartridge_has_drawn_it() {
// Row 62, from the rung-11 checkpoint in the Pewter mart: the price line "POKe BALL? That will
// be 200. OK?" prints at text speed after the quantity is chosen, and only then is the YES/NO
// box drawn. The script's last A came one settle after the quantity's and landed on the text
// while it was still printing, so `BUY BALL` reported `done` with nothing bought -- 55 starts,
// 55 `done`, the wallet at 1,606 throughout -- and the box it had not answered was left to
// whatever the pad dealt next.
let mut world = World::room();
world.scene = Scene::Shop;
world.list = List::Shop(ShopScreen::Buying);
world.cursor_max = 3;
world.stock = vec![item::POKE_BALL, item::POTION, 29, item::ANTIDOTE];
world.money = 1_606;
world.b_to_close = u8::MAX;
// Measured: the line waits for a press after "That will be", and the box is drawn after
// the rest of it prints. The fake draws it three presses after the item's: the item, the
// quantity, and the press that the `cont` waits for.
world.price_after = Some((3, 30));
assert_eq!(run(&mut world, MacroKind::BuyBall).unwrap(), MacroAbort::Done);
assert_eq!(world.bought, 1, "YES on the drawn box: {:?}", world.pulses);
assert!(!world.prompt, "and the box is answered, not left up");
// A box that never comes is not a purchase: `blocked`, and nothing answered.
let mut world = World::room();
world.scene = Scene::Shop;
world.list = List::Shop(ShopScreen::Buying);
world.cursor_max = 3;
world.stock = vec![item::POKE_BALL];
world.money = 1_606;
world.b_to_close = u8::MAX;
assert_eq!(run(&mut world, MacroKind::BuyBall).unwrap(), MacroAbort::Blocked);
assert_eq!(world.bought, 0);
}