From 36325d82ddbe0b1b82f42f5ab38b3779df94c5ca Mon Sep 17 00:00:00 2001 From: acamilo Date: Tue, 29 Sep 2026 13:55:23 +0000 Subject: [PATCH] palette: a counter the fly walked away from is not walked back to for the window Row 62 re-deals GO SHOP/GO HEAL whenever the service is needed, with no window. Live on v0.6.5 from the rung-8 archive that was a ring: GO SHOP to the Viridian clerk, GO OUT/GO OBJECTIVE back out without TALK, GO SHOP again (33 in 35 minutes). A completed GO SHOP/GO HEAL facing the counter person now records TargetKey::Counter on the building's map; the service walk toward it is withheld for the reached window, from the street and inside. TALK at the counter is unchanged. ROM row66 test (FLY_ROW66_MART_CHECKPOINT): base 31 street GO SHOPs in 10,000 frames, branch 0. --- .../src/pokemon_red/macros/cartridge.rs | 12 ++ .../src/pokemon_red/macros/driver.rs | 6 +- .../src/pokemon_red/macros/executor.rs | 20 +++ .../src/pokemon_red/macros/palette.rs | 31 ++++- .../src/pokemon_red/macros/tests.rs | 40 ++++++ .../flybrain-gb/src/pokemon_red/state.rs | 5 + .../crates/flysim/tests/rom_macros_mode.rs | 123 ++++++++++++++++++ 7 files changed, 234 insertions(+), 3 deletions(-) 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 dac0e49..1f98833 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 @@ -312,6 +312,11 @@ pub enum TargetKey { /// the box belongs to is whatever the fly is standing in front of, and the box is the only /// thing on screen while it is open. Answer { at: Tile, yes: bool }, + /// A mart's or a Pokémon Center's counter, walked to by `GO SHOP` or `GO HEAL` and faced + /// (row 66). Keyed by the building's map and nothing else, because the question it answers is + /// asked from *outside* the building, where no sprite slot of it means anything: has the fly + /// just stood at this counter and walked away from it ([`MacroState::counter_walked_to`]). + Counter, } /// Which list the shared cursor belongs to right now. @@ -617,6 +622,13 @@ pub trait MacroState: GameState { false } + /// Whether a `GO SHOP` or `GO HEAL` has walked the fly to the counter of the building on + /// `map` and faced it, inside the reached window (row 66). Asked of any map, not only the + /// loaded one: it is what the service walk from the street outside reads. + fn counter_walked_to(&mut self, _map: u8) -> bool { + false + } + /// Where the ladder's next unreached rung is, when the adapter's rung catalog knows. /// /// `None` leaves `GO OBJECTIVE` unbound, which is section 9's own fall-through: the plan's diff --git a/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/driver.rs b/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/driver.rs index 23f9fa6..8760063 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/driver.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/driver.rs @@ -19,7 +19,8 @@ use crate::macros::{ use super::super::mapgrid::MapGrids; use super::super::state::PokeState; use super::cartridge::{ - Areas, Frontiers, LAST_MAP, MacroState, Pushed, Stood, Talked, Targets, Tile, outdoors, + Areas, Frontiers, LAST_MAP, MacroState, Pushed, Stood, Talked, TargetKey, Targets, Tile, + outdoors, }; use super::geography; use super::executor::{MacroAbort, MacroMachine, Refusal}; @@ -271,6 +272,9 @@ impl PokemonPalette { if let Some((map, target)) = self.machine.take_reached() { self.targets.record_reached(map, target); } + if let Some(map) = self.machine.take_counter() { + self.targets.record_reached(map, TargetKey::Counter); + } // A tile the cartridge drove the fly off: no window, because the map is like that until // the event that unlocks it, and nothing here knows which event that is (row 37). while let Some((map, tile)) = self.machine.take_pushed() { 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 783c48b..11d6cff 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 @@ -648,6 +648,9 @@ pub struct MacroMachine { /// that finds no route to *any* of its goals has failed at all of them, and each is a key. blocked: Vec<(u8, TargetKey)>, reached: Option<(u8, TargetKey)>, + /// The building whose counter a completed `GO SHOP` or `GO HEAL` has just faced, for the + /// reached ledger under [`TargetKey::Counter`] (row 66). + counter: Option, /// A map whose frontier a `GO FRONTIER` has just proved unreachable, waiting to be taken /// into the session's ledger ([`super::cartridge::Frontiers`], section 12.14). /// @@ -754,6 +757,7 @@ impl MacroMachine { outcome: None, blocked: Vec::new(), reached: None, + counter: None, exhausted: None, pushed_tile: Vec::new(), refused_at: None, @@ -1000,6 +1004,11 @@ impl MacroMachine { self.reached.take() } + /// The building whose counter a completed `GO SHOP` or `GO HEAL` faced, taken (row 66). + pub fn take_counter(&mut self) -> Option { + self.counter.take() + } + /// The map a `no route` from `GO FRONTIER` earned, taken rather than read (section 12.14). pub fn take_exhausted(&mut self) -> Option { self.exhausted.take() @@ -1040,6 +1049,7 @@ impl MacroMachine { // that so a cancelled queue cannot leak into the next macro's finish. self.blocked.clear(); self.reached = None; + self.counter = None; // A rollback is not the map pushing the fly anywhere, and it is not the frontier being // out of reach either: the fly is about to be somewhere else entirely. self.pushed_tile.clear(); @@ -1411,6 +1421,16 @@ impl MacroMachine { && let Some(entry) = active.target { self.reached = Some(entry); + // **The counter itself, by the building's map** (row 66): a service walk + // that ended facing the clerk or the nurse. From the street the question + // is "has the fly just stood at this counter", and outside the building + // no sprite slot of it can be named. + if matches!(active.kind, MacroKind::GoShop | MacroKind::GoHeal) + && let (map, TargetKey::Thing(_)) = entry + && super::geography::amenity_at(map).is_some() + { + self.counter = Some(map); + } // And a completed `HEAL` has *had* the conversation: the box was opened, // answered and closed by this macro's own presses, so the nurse is talked // to and `TALK` has nothing left to open (section 12.12). Without it the diff --git a/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/palette.rs b/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/palette.rs index 317dbf4..327d188 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/palette.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/palette.rs @@ -999,7 +999,9 @@ pub fn amenity_goals(state: &mut dyn MacroState, kind: Amenity) -> Vec { // reached and talked ledgers answer "is this job done", and for a service the answer is // the party or the bag, not the ledger: a nurse reached an hour ago with a party that has // since been beaten is a job to do again. - if service_needed(state, kind) { + // ...unless this counter is the one the fly has just walked to and walked away from + // ([`counter_walked_away`], row 66): then the ledgers answer, as they do for any person. + if service_needed(state, kind) && !state.counter_walked_to(here) { return service_aims(state, kind); } return counter_aims(state, counter_sprite(kind)); @@ -1095,7 +1097,32 @@ pub fn amenity_wanted(state: &mut dyn MacroState, kind: Amenity) -> Option { return None; } let area = area_here(state)?; - amenity_in_reach(state, area, kind) + let map = amenity_in_reach(state, area, kind)?; + if counter_walked_away(state, map) { + return None; + } + Some(map) +} + +/// Whether the fly has just stood at the counter of the building on `map`, facing it, and is now +/// outside that building: what takes the service walk toward it off the pad for the reached window +/// (row 66). +/// +/// **A counter the fly walked away from is not walked back to at once.** Row 62 made a service +/// dealt again whenever the cartridge says it is needed, whatever the ledgers say, and that is a +/// ring by construction when the fly does not use the counter: live on v0.6.5 from the rung-8 +/// archive, `GO SHOP` 33 times in 35 minutes, each one a walk in from the street and up to the +/// Viridian clerk, then `GO OUT` or `GO OBJECTIVE` back out of the door without a `TALK`, and +/// outside `GO SHOP` on the pad again because the bag still had no ball. Reproduced from a +/// post-parcel checkpoint of the Viridian mart with the connectome: three laps in eighteen brain +/// seconds. It is section 12.1's ledger doing what it does for every other walk -- a target +/// arrived at and faced is not re-walked for the window -- applied to the one walk row 62 took it +/// away from. The fly standing at the counter still has `TALK` (its precondition reads the +/// service, not this), the nurse's `HEAL` still walks from anywhere in the centre, and when the +/// window closes the counter is a service again. +fn counter_walked_away(state: &mut dyn MacroState, map: u8) -> bool { + let here = state.player().map(|player| player.map); + here != Some(map) && state.counter_walked_to(map) } /// `area`'s building of `kind`, when the route to it from the piece of ground the fly stands on 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 98b96e5..8bc6737 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 @@ -875,6 +875,10 @@ impl MacroState for World { self.targets.reached(self.map, target) } + fn counter_walked_to(&mut self, map: u8) -> bool { + self.targets.reached(map, TargetKey::Counter) + } + fn refused_here(&mut self, slot: u8) -> bool { self.targets.refused(self.map, slot, self.player) } @@ -5805,6 +5809,42 @@ fn go_shop_comes_back_while_the_bag_has_no_ball_and_the_money_covers_one() { assert!(!on_the_pad(&mut world, MacroKind::GoShop), "the parcel's text table, no counter"); } +#[test] +fn a_counter_the_fly_walked_away_from_is_not_walked_back_to_for_the_window() { + // Row 66. Live on v0.6.5 from the rung-8 archive: `GO SHOP` 33 times in 35 minutes, in from + // the street to the Viridian clerk and back out of the door without a `TALK`, and `GO SHOP` on + // the street's pad again because the bag still had no ball -- row 62's "dealt again while + // needed", with no window at all. + let mut world = viridian(); + world.areas.insert((Amenity::Mart, maps::VIRIDIAN_CITY)); + world.seen_maps.insert(maps::VIRIDIAN_MART); + assert!(on_the_pad(&mut world, MacroKind::GoShop), "no ball, and the money for one"); + + // A `GO SHOP` has just stood the fly at that counter; it walked back out. + world.targets.record_reached(maps::VIRIDIAN_MART, TargetKey::Counter); + assert!(!on_the_pad(&mut world, MacroKind::GoShop), "not straight back in: {:?}", pad_of(&mut world)); + assert_eq!(amenity_wanted(&mut world, Amenity::Mart), None); + + // The window closes and the counter is a service again. + world.targets.clock(BLOCKED_MINUTES_DEFAULT * 60_000.0 + 1.0); + assert!(on_the_pad(&mut world, MacroKind::GoShop), "after the window"); + + // Inside, inside the window: no walk back to the counter, and at the counter its press. + let mut mart = World::mart(); + mart.targets.record_reached(mart.map, TargetKey::Counter); + mart.targets.record_reached(mart.map, TargetKey::Thing(TalkTarget::Sprite(1))); + assert!(service_needed(&mut mart, Amenity::Mart)); + assert!(!on_the_pad(&mut mart, MacroKind::GoShop), "{:?}", pad_of(&mut mart)); + mart.player = Tile::new(2, 5); + mart.facing = Facing::Left; + assert!(on_the_pad(&mut mart, MacroKind::Talk), "the counter still opens where the fly stands"); + + // Only the counter: another person reached in the building is not the counter. + let mut mart = World::mart(); + mart.targets.record_reached(mart.map, TargetKey::Thing(TalkTarget::Sprite(1))); + assert!(on_the_pad(&mut mart, MacroKind::GoShop), "row 62's rule stands without a counter visit"); +} + #[test] fn heal_is_dealt_facing_the_nurse_and_after_go_heal_has_reached_her() { // Row 62. `HEAL`'s goals were `counter_aims`, which drops the tile the fly already faces her 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 4b42bcb..366207a 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/state.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/state.rs @@ -2334,6 +2334,11 @@ impl MacroState for PokeState<'_> { self.targets.reached(map, target) } + /// Whether the counter of the building on `map` was walked to inside the window (row 66). + fn counter_walked_to(&mut self, map: u8) -> bool { + self.targets.reached(map, TargetKey::Counter) + } + /// Where the ladder's next unreached rung is (`GO OBJECTIVE`). /// /// The adapter's [`crate::adapter::MapPlace`] in the executor's own types, which is the only diff --git a/services/flysim/crates/flysim/tests/rom_macros_mode.rs b/services/flysim/crates/flysim/tests/rom_macros_mode.rs index 7940255..e356b45 100644 --- a/services/flysim/crates/flysim/tests/rom_macros_mode.rs +++ b/services/flysim/crates/flysim/tests/rom_macros_mode.rs @@ -4554,3 +4554,126 @@ fn row65_the_fly_leaves_the_cerulean_badge_houses_back_yard() { assert!(worst_empty < 600, "an empty pad in the yard for {worst_empty} frames: {:?}", run.started); assert!(left.is_some(), "the fly never left the yard: {:?} {:?}", run.route, run.started); } + +fn row66_mart_checkpoint() -> Option { + std::env::var_os("FLY_ROW66_MART_CHECKPOINT").map(|path| { + flysim::store::load(std::path::Path::new(&path)) + .expect("the checkpoint should be a FLYSIM01 envelope") + }) +} + +/// Row 66: a fly that walks up to the Viridian clerk and away again without a `TALK` is not +/// walked straight back in by `GO SHOP`. +/// +/// **What was live** (v0.6.5, restored from the rung-8 archive): `GO SHOP` done 33 times in 35 +/// minutes, `TALK` 5, `BUY BALL` never dealt -- in from the street to the counter, `GO OUT` or +/// `GO OBJECTIVE` back out of the door, and `GO SHOP` on the street's pad again because the bag +/// still held no ball (row 62's "dealt again while the service is needed", with no window). +/// Reproduced with the connectome from this checkpoint: three laps in eighteen brain seconds. +/// +/// The drive declines the counter on purpose: `GO SHOP` whenever it is dealt, then the ways out, +/// never `TALK`. It says nothing about which button the fly would press; the claim is that the pad +/// does not deal the walk back to a counter the fly has just walked away from (section 12.1's +/// reached window, applied to the service walk). +/// +/// ```sh +/// FLY_ROM=/path/to/pokemon-red.gb \ +/// FLY_ROW66_MART_CHECKPOINT=.local/checkpoints/survey-rank8-row66-viridian-mart.checkpoint \ +/// cargo test --release -p flysim --test rom_macros_mode -- --nocapture row66 +/// ``` +#[test] +fn row66_a_counter_walked_away_from_is_not_walked_back_to_by_go_shop() { + use flybrain_gb::pokemon_red::macros::PokemonPalette; + use flybrain_gb::{MacroPalette, Outcome, Started}; + let rom = skip_without_rom!(); + let Some(checkpoint) = row66_mart_checkpoint() else { + eprintln!("skipped: no FLY_ROW66_MART_CHECKPOINT"); + return; + }; + let mut run = Run::resume(&rom, MacroMode::Macros, &checkpoint); + assert_eq!(run.bag_count(POKE_BALL), 0, "no ball at the checkpoint"); + assert!(run.money() >= 200, "and the money for one: {}", run.money()); + + let order = ["GO SHOP", "GO OUT", "GO OBJECTIVE"]; + let hold_frames = 48u32; + let mut palette = PokemonPalette::new(SEED); + let mut running: Option<&'static str> = None; + let mut since_decision = hold_frames; + let mut ms = run.ms; + // `GO SHOP` walks that finished facing the counter, `TALK` dealt there, and `GO SHOP` walks + // started from the street outside after the first of those. + let mut at_the_counter = 0u32; + let mut talk_dealt_at_the_counter = false; + let mut from_the_street_after = 0u32; + let mut laps = 0u32; + let mut last_map = run.map(); + for _ in 0..10_000u32 { + palette.clock(ms); + let observed = { + let ledger = AdapterLedger(&run.adapter); + palette.observe(&mut run.gb, &ledger) + }; + let mut mask = 0u8; + { + let ledger = AdapterLedger(&run.adapter); + if running.is_some() { + match palette.step(&mut run.gb, &ledger) { + Some(held) => mask = held, + None => running = None, + } + } else if since_decision >= hold_frames && !observed.bindings.is_empty() { + since_decision = 0; + if at_the_counter > 0 + && run.map() == VIRIDIAN_MART + && observed.bindings.iter().any(|binding| binding.name == "TALK") + { + talk_dealt_at_the_counter = true; + } + let binding = order + .iter() + .find_map(|want| observed.bindings.iter().find(|binding| binding.name == *want)) + .or_else(|| observed.bindings.iter().find(|binding| binding.name != "TALK")) + .unwrap_or(&observed.bindings[0]); + if binding.name == "GO SHOP" && run.map() != VIRIDIAN_MART && at_the_counter > 0 { + from_the_street_after += 1; + } + if let Started::Running(name) = palette.start(binding.slot, &mut run.gb, &ledger) { + running = Some(name); + match palette.step(&mut run.gb, &ledger) { + Some(held) => mask = held, + None => running = None, + } + } + } + } + if let Some((name, outcome)) = palette.take_finished() + && name == "GO SHOP" + && outcome == Outcome::Done + && run.map() == VIRIDIAN_MART + { + at_the_counter += 1; + } + since_decision += 1; + run.gb.set_buttons(mask); + run.gb.run_frame().expect("a frame should complete"); + ms += MS_PER_FRAME; + run.adapter.sample(&mut run.gb, ms); + let map = run.map(); + if map == VIRIDIAN_MART && last_map == VIRIDIAN_CITY { + laps += 1; + } + last_map = map; + } + eprintln!( + "GO SHOP done in the mart {at_the_counter}, TALK dealt at the counter \ + {talk_dealt_at_the_counter}, GO SHOP from the street after that {from_the_street_after}, \ + walks back into the mart {laps}, balls {}", + run.bag_count(POKE_BALL) + ); + assert!(at_the_counter >= 1, "the first GO SHOP walks the fly to the clerk"); + assert!(talk_dealt_at_the_counter, "and the press that opens the counter is on the pad there"); + assert_eq!( + from_the_street_after, 0, + "GO SHOP dealt on the street again after the fly walked away from the counter" + ); +}