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 6704438..2881433 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 @@ -56,6 +56,10 @@ pub mod item { pub const POTION: u8 = 20; /// `$1e`. pub const REPEL: u8 = 30; + /// `$46`. The Viridian clerk's parcel for Oak: while the bag holds it, that counter has + /// nothing to sell (`scripts/ViridianMart.asm` keeps the parcel's text table until + /// `EVENT_OAK_GOT_PARCEL`). + pub const OAKS_PARCEL: u8 = 0x46; } /// Mart prices at the pinned pokered commit, for section 3's "if money allows". 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 819c371..3e2b751 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 @@ -11,7 +11,7 @@ use super::cartridge::{ CHEAPEST_PURCHASE, FACINGS, MART_CURSOR_ROWS, opposite, ExitId, ListKind, Listing, MacroState, Objective, PARTY_CAPACITY, PURCHASES, TalkTarget, - TargetKey, Tile, item, outdoors, + TargetKey, Tile, item, outdoors, price, }; use crate::adapter::PlaceKind; @@ -627,7 +627,13 @@ pub fn precondition(kind: MacroKind, state: &mut dyn MacroState) -> bool { // object with a purpose, not a person to chat with: with the party already full her whole // conversation is forty-six text frames that end where they began, which is section // 12.2's trap at conversation scale (section 12.12). - MacroKind::Talk => facing_untalked(state) && !rested_nurse(state), + // ...and, since row 62, at a counter the fly has a use for however many times this + // session has already talked to it ([`facing_service`]): a clerk or a nurse is a service, + // and the talked ledger answers "is there anything left to say", which for a service is + // the party's HP or the bag's balls rather than the last conversation. + MacroKind::Talk => { + (facing_untalked(state) || facing_service(state)) && !rested_nurse(state) + } // The start menu opens from anywhere, and that is exactly why `MENU` is on no pad: // "the precondition is satisfied wherever the fly stands" is section 12.2's trap, and a // macro whose whole effect is a screen its own scene's `BACK` closes again is 12.10's @@ -973,9 +979,16 @@ pub fn stock_index(state: &mut dyn MacroState, item: u8) -> Option { pub fn amenity_goals(state: &mut dyn MacroState, kind: Amenity) -> Vec { let Some(here) = state.player().map(|player| player.map) else { return Vec::new() }; if geography::amenity_at(here) == Some(kind) { + // **A counter the fly has a use for is not retired by the last visit** (row 62). The + // 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) { + return service_aims(state, kind); + } return counter_aims(state, counter_sprite(kind)); } - match errand(state, kind) { + match amenity_wanted(state, kind) { Some(map) => { let here = state.player().map(|player| Tile::new(player.x, player.y)); goals_toward(state, map) @@ -1003,7 +1016,97 @@ pub fn heal_goals(state: &mut dyn MacroState) -> Vec { if geography::amenity_at(here) != Some(Amenity::Center) { return Vec::new(); } - counter_aims(state, poke_sprite::NURSE) + // **Neither the reached nor the talked ledger, and the counter the fly already faces is + // somewhere to heal from** (row 62). `HEAL` is only dealt while the party needs it, and that + // is its whole satiation: a completed heal leaves a party that reads full. Through + // [`counter_aims`] the button was off the pad on the one tile it is for -- facing the nurse, + // which is where `GO HEAL` leaves the fly, and for ten brain minutes after `GO HEAL` had + // reached her. Only the blocked window is kept: a counter no walk can reach is still that. + person_aims(state, poke_sprite::NURSE, Ledgers::BlockedOnly, true) +} + +/// Whether the building of `kind` has something to do for the fly right now: the party needs the +/// nurse, or the bag has no ball the wallet could buy one of (row 62). +/// +/// The errand (section 13) is "visit each area's building once per run", and it is paid on +/// entering -- by a visit on which the counter could do nothing, too: the Viridian mart is entered +/// first for Oak's parcel, when its clerk sells nothing, and a centre entered with a full party +/// heals nothing. Once paid it was never offered again, whatever the party or the bag read. This +/// is the other half of the same question, read from the cartridge each time it is asked, and +/// **it satisfies itself**: a heal leaves the party full and a purchase leaves a ball in the bag, +/// so the button leaves the pad the moment the job is done. +/// +/// - **Centre**: [`party_needs_rest`], the same byte `HEAL` and the nurse's prompt read. +/// - **Mart**: no ball in the bag, and the money for one. A ball because `THROW BALL` is the one +/// button in the vocabulary that a purchase has to make possible -- with no ball it is never +/// dealt (section 12.9) -- and every mart in Kanto stocks it as its first row +/// (`data/items/marts.asm`), inside the cursor's reach ([`stock_index`]). And not while the bag +/// holds Oak's parcel: until it is delivered the Viridian clerk's text table is the parcel's, +/// and his counter sells nothing (`scripts/ViridianMart.asm`). +pub fn service_needed(state: &mut dyn MacroState, kind: Amenity) -> bool { + match kind { + Amenity::Center => party_needs_rest(state), + Amenity::Mart => { + let bag = state.bag(); + state.money() >= price::POKE_BALL + && !bag.iter().any(|stack| item::BALLS.contains(&stack.id) && stack.count > 0) + && !bag.iter().any(|stack| stack.id == item::OAKS_PARCEL) + } + } +} + +/// The map `GO SHOP` or `GO HEAL` walks toward from elsewhere in the area, or `None`. +/// +/// The errand while it stands ([`errand`]), and otherwise the area's building of that kind while +/// it has something to do ([`service_needed`]). Nothing here ranks the building above anything +/// else on the pad: it is what the button aims at, and the fly chooses the button. +pub fn amenity_wanted(state: &mut dyn MacroState, kind: Amenity) -> Option { + if let Some(map) = errand(state, kind) { + return Some(map); + } + if !service_needed(state, kind) { + return None; + } + geography::amenity_of(area_here(state)?, kind) +} + +/// Inside a building with a job to do: the tiles to face its counter person from, whatever the +/// ledgers say, and nothing once the fly already faces them -- that is `TALK`'s tile, and a walk +/// that settles where it started is section 12.2's trap. +fn service_aims(state: &mut dyn MacroState, kind: Amenity) -> Vec { + if facing_counter_person(state, kind) { + return Vec::new(); + } + person_aims(state, counter_sprite(kind), Ledgers::BlockedOnly, false) +} + +/// Whether the fly stands facing the clerk or the nurse of the building of `kind` it is in. +fn facing_counter_person(state: &mut dyn MacroState, kind: Amenity) -> bool { + let inside = state.player().is_some_and(|player| geography::amenity_at(player.map) == Some(kind)); + let Some(TalkTarget::Sprite(slot)) = facing_target(state) else { return false }; + inside + && state + .npcs() + .iter() + .any(|npc| npc.slot == slot && npc.picture == counter_sprite(kind)) +} + +/// Whether the fly faces a counter whose service it has a use for: `TALK`'s second reason (row +/// 62). The press that opens a mart's counter or a nurse's conversation, dealt whatever the talked +/// ledger says, while [`service_needed`] holds. +pub fn facing_service(state: &mut dyn MacroState) -> bool { + Amenity::ALL + .into_iter() + .any(|kind| facing_counter_person(state, kind) && service_needed(state, kind)) +} + +/// Which of the target ledgers [`person_aims`] respects. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Ledgers { + /// Talked, reached and blocked: a person whose conversation is a job done once. + All, + /// Blocked alone: a service, whose job is done when the cartridge says so. + BlockedOnly, } /// The picture id of the person who stands behind `kind`'s counter. @@ -1039,10 +1142,29 @@ pub mod poke_sprite { /// Nearest first by Manhattan distance, ties by tile, which is `approach`'s own rule. Whether any /// of the tiles is *reachable* is the route search's answer, not this one's. pub fn counter_aims(state: &mut dyn MacroState, picture: u8) -> Vec { + person_aims(state, picture, Ledgers::All, false) +} + +/// [`counter_aims`] with the ledgers it respects, and whether the tile the fly already faces +/// the person from stays in the list (`HEAL`'s walk may be no steps at all). +fn person_aims( + state: &mut dyn MacroState, + picture: u8, + ledgers: Ledgers, + keep_facing: bool, +) -> Vec { let Some(player) = state.player() else { return Vec::new() }; let here = Tile::new(player.x, player.y); - let mut people: Vec<(u32, Tile, u8)> = state - .npcs() + // **Drawn or not** (row 62). A centre is entered at (3, 7) and its nurse stands at (3, 1), six + // rows up and off the top of the screen, so from the doormat the drawn sprites held nobody + // behind the counter: `HEAL` was off the pad, `GO HEAL` had nobody to walk to and the + // counter's suppression was released, all on the frame the fly walked in -- measured in the + // Pewter centre, where the pad on arrival was the way out. Row 58's reading of the sprites the + // cartridge keeps off the screen is the same table, so the counter person is found from the + // door. + let mut everyone = state.npcs(); + everyone.extend(state.offscreen_npcs()); + let mut people: Vec<(u32, Tile, u8)> = everyone .iter() .filter(|npc| npc.picture == picture) .map(|npc| (Tile::new(npc.x, npc.y).distance(here), Tile::new(npc.x, npc.y), npc.slot)) @@ -1055,10 +1177,11 @@ pub fn counter_aims(state: &mut dyn MacroState, picture: u8) -> Vec { // already been stood in front of, or already talked to, is a job done. Without them // `amenity_goals` never empties, `GO SHOP` never leaves the pad inside the building, and the // suppression in [`ways`] would keep the fly in there for ever. - if state.talked(target) - || state.reached(TargetKey::Thing(target)) - || state.blocked(TargetKey::Thing(target)) - { + let retired = match ledgers { + Ledgers::All => state.talked(target) || state.reached(TargetKey::Thing(target)), + Ledgers::BlockedOnly => false, + }; + if retired || state.blocked(TargetKey::Thing(target)) { return Vec::new(); } let key = Some(TargetKey::Thing(target)); @@ -1096,7 +1219,9 @@ pub fn counter_aims(state: &mut dyn MacroState, picture: u8) -> Vec { // The tile the fly is already standing on facing the right way is not somewhere to walk to: // that is `TALK`'s state, and the same exclusion `untalked` makes (row 4). let ahead = Tile::new(player.x, player.y).step(player.facing); - aims.retain(|aim| aim.tile != here || Some(tile) != ahead); + if !keep_facing { + aims.retain(|aim| aim.tile != here || Some(tile) != ahead); + } aims } 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 c4169cb..8032cc4 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 @@ -21,14 +21,14 @@ use super::cartridge::{ BLOCKED_MINUTES_DEFAULT, CHEAPEST_PURCHASE, Edge, ExitId, FACINGS, LAST_MAP, MacroState, Objective, PURCHASES, TalkTarget, TargetKey, TargetLedger, Targets, Tile, battle_entry, button, item, price, }; -use super::geography::Amenity; +use super::geography::{self, Amenity}; use super::executor::{ FRAME_CAP, FRAMES_PER_PLANNED_TILE, MacroAbort, MacroMachine, MacroRefused, Refusal, WALK_FRAME_CEILING, walk_budget, }; use super::palette::{ MacroId, MacroKind, Palette, SLOTS, amenity_goals, answer_key, errand, facing_nurse, - healthiest_other, heal_goals, nurse_prompt, rested_nurse, + healthiest_other, heal_goals, inside_center, nurse_prompt, rested_nurse, service_needed, listing, losing, move_slot_bound, objective_goals, party_needs_rest, party_rested, poke_sprite, precondition, throw_slot, untalked_objects, untalked_people, ways, }; @@ -4186,9 +4186,11 @@ fn go_shop_and_go_heal_are_on_the_pad_while_their_errand_stands() { assert!(on_the_pad(&mut world, MacroKind::GoHeal)); // Paid, and the button leaves the pad -- which is section 12's "a precondition failure means - // the button is not on the pad", and what makes the errand impossible to loop on. + // the button is not on the pad", and what makes the errand impossible to loop on. Since row + // 62 that holds while neither building has a job to do: a ball in the bag and a full party. world.areas.insert((Amenity::Mart, maps::VIRIDIAN_CITY)); world.areas.insert((Amenity::Center, maps::VIRIDIAN_CITY)); + world.bag = vec![(item::POKE_BALL, 3)]; assert!(!on_the_pad(&mut world, MacroKind::GoShop)); assert!(!on_the_pad(&mut world, MacroKind::GoHeal)); @@ -5349,11 +5351,15 @@ fn a_declined_heal_writes_the_nurse_into_the_talked_ledger() { assert_eq!(run(&mut center, MacroKind::No).unwrap(), MacroAbort::Done); assert!(center.talked.contains(&TalkTarget::Sprite(1)), "the nurse: {:?}", center.talked); - // So `TALK` is off the pad there even if the party is hurt later: the ledger is the record - // that this run has had her conversation. + // So `TALK` is off the pad there while the party stays full: the ledger is the record that + // this run has had her conversation, and a full party has nothing to ask her for. center.scene = Scene::Overworld; - center.mons[0].hp = 4; assert!(!precondition(MacroKind::Talk, &mut center)); + + // Row 62: hurt again later, and she is a service with a job to do. The talked ledger said + // "never again" for the session, which was the whole Mt. Moon run's one nurse. + center.mons[0].hp = 4; + assert!(precondition(MacroKind::Talk, &mut center), "a hurt party has a use for her"); } #[test] @@ -5529,6 +5535,162 @@ fn a_trainer_walking_up_teaches_the_ledgers_nothing() { assert!(world.pushes.is_empty(), "and the ground is as walkable as it was"); } +// --------------------------------------------------------------------------------------------- +// Row 62: a service the fly has a use for is not withheld by the ledger that paid its errand +// --------------------------------------------------------------------------------------------- + +#[test] +fn go_heal_comes_back_for_a_hurt_party_after_the_errand_is_paid() { + // The live run of 2026-09-24: the fly entered the Viridian centre once with a full party, + // which paid the errand for the run, and was never offered `GO HEAL` in Viridian again -- + // eleven hours, no heal anywhere, and every whiteout sent it back to Pallet Town. + let mut world = viridian(); + world.areas.insert((Amenity::Center, maps::VIRIDIAN_CITY)); + world.seen_maps.insert(maps::VIRIDIAN_POKECENTER); + assert_eq!(errand(&mut world, Amenity::Center), None, "the errand is paid"); + assert!(!on_the_pad(&mut world, MacroKind::GoHeal), "full party: nothing to go for"); + + world.mons[0].hp = 7; + assert!(on_the_pad(&mut world, MacroKind::GoHeal), "hurt: the centre has a job to do"); + assert_eq!( + amenity_goals(&mut world, Amenity::Center).iter().map(|aim| aim.tile).collect::>(), + vec![Tile::new(6, 1)], + "the centre's door" + ); + + // A status counts as the nurse's job too, and a route with no centre has none to go to. + world.mons[0].hp = 20; + world.mons[0].status = Status::Poison; + assert!(on_the_pad(&mut world, MacroKind::GoHeal)); + world.map = maps::ROUTE_1; + assert!(!on_the_pad(&mut world, MacroKind::GoHeal), "no centre in this area"); +} + +#[test] +fn go_shop_comes_back_while_the_bag_has_no_ball_and_the_money_covers_one() { + // The Viridian mart is entered first for Oak's parcel, when its clerk sells nothing, and that + // visit paid the errand for the run: `GO SHOP` was never dealt in Viridian again, `BUY BALL` + // never, `THROW BALL` never (live: GO SHOP 2 starts in eleven hours, both on the parcel). + let mut world = viridian(); + world.areas.insert((Amenity::Mart, maps::VIRIDIAN_CITY)); + world.seen_maps.insert(maps::VIRIDIAN_MART); + assert_eq!(errand(&mut world, Amenity::Mart), None, "the errand is paid"); + assert!(on_the_pad(&mut world, MacroKind::GoShop), "no ball and 3000 to buy one with"); + + // A ball in the bag: nothing to go for. + world.bag = vec![(item::POTION, 1), (item::POKE_BALL, 1)]; + assert!(!on_the_pad(&mut world, MacroKind::GoShop)); + // Thrown, and it is back. + world.bag = vec![(item::POTION, 1)]; + assert!(on_the_pad(&mut world, MacroKind::GoShop)); + // Under a ball's price: nothing it could buy the ball with. + world.money = price::POKE_BALL - 1; + assert!(!on_the_pad(&mut world, MacroKind::GoShop)); + world.money = price::POKE_BALL; + assert!(on_the_pad(&mut world, MacroKind::GoShop)); + // Carrying Oak's parcel the Viridian counter sells nothing (`scripts/ViridianMart.asm`). + world.bag = vec![(item::OAKS_PARCEL, 1)]; + assert!(!on_the_pad(&mut world, MacroKind::GoShop), "the parcel's text table, no counter"); +} + +#[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 + // from and retires a nurse `GO HEAL` has reached for ten brain minutes: so `HEAL` was off the + // pad on the one tile it is for, and for ten minutes after the walk that put the fly there. + let mut center = World::center(); + center.player = Tile::new(3, 3); + center.facing = Facing::Up; + center.mons[0].hp = 5; + assert!(facing_nurse(&mut center)); + assert!(on_the_pad(&mut center, MacroKind::Heal), "facing her with a hurt party"); + + let nurse = TargetKey::Thing(TalkTarget::Sprite(1)); + center.targets.record_reached(center.map, nurse); + center.talked.insert(TalkTarget::Sprite(1)); + assert!(on_the_pad(&mut center, MacroKind::Heal), "reached and talked: still her job"); + assert!(on_the_pad(&mut center, MacroKind::Talk), "and the press that opens her box"); + + // It runs from where it stands: no walk, the talk, the YES, and a party that reads full. + center.switch = Some((40, Scene::Dialog)); + center.heal_at = Some(80); + assert_eq!(run(&mut center, MacroKind::Heal).unwrap(), MacroAbort::Done); + assert_eq!(center.player, Tile::new(3, 3)); + assert!(party_rested(&mut center)); + center.scene = Scene::Overworld; + assert!(!on_the_pad(&mut center, MacroKind::Heal), "full: the button leaves the pad"); + assert!(!on_the_pad(&mut center, MacroKind::Talk), "and so does the nurse's conversation"); + + // The blocked window is kept: a counter no walk reaches is still that. + center.mons[0].hp = 5; + center.targets.record_blocked(center.map, nurse); + assert!(!on_the_pad(&mut center, MacroKind::Heal)); +} + +#[test] +fn a_counter_the_fly_has_a_use_for_opens_again_after_this_session_has_talked_to_it() { + // The clerk talked once this session (the parcel, or a visit that bought nothing) was out of + // `TALK`'s reach for the rest of it, and `GO SHOP` inside had nobody to walk to: the counter + // could not be opened again, so `BUY BALL` could not be dealt again. + let mut mart = World::mart(); + mart.talked.insert(TalkTarget::Sprite(1)); + 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), "walk to the clerk: {:?}", pad_of(&mut mart)); + + // At the counter, facing him: the walk is done and the press that opens it is dealt. + mart.player = Tile::new(2, 5); + mart.facing = Facing::Left; + assert!(!on_the_pad(&mut mart, MacroKind::GoShop), "already there"); + assert!(on_the_pad(&mut mart, MacroKind::Talk), "open the counter again"); + + // With a ball in the bag the ledgers answer as they always did. + mart.bag = vec![(item::POKE_BALL, 2)]; + assert!(!on_the_pad(&mut mart, MacroKind::Talk)); + mart.player = Tile::new(3, 6); + assert!(!on_the_pad(&mut mart, MacroKind::GoShop)); +} + +#[test] +fn the_mt_moon_pokecenter_is_a_centre_and_route_4s() { + // Route 4's centre, the last before Mt. Moon, was on no table: `HEAL` was never dealt in it and + // the nurse's prompt was nobody's. Live: the fly stood in it at 10/70 (the row 59 checkpoint) + // and walked back out. + assert_eq!(geography::amenity_at(maps::MT_MOON_POKECENTER), Some(Amenity::Center)); + assert_eq!(geography::area_of(maps::MT_MOON_POKECENTER), Some(maps::ROUTE_4)); + assert_eq!(geography::amenity_of(maps::ROUTE_4, Amenity::Center), Some(maps::MT_MOON_POKECENTER)); + assert_eq!(geography::amenity_of(maps::ROUTE_4, Amenity::Mart), None, "Route 4 has no mart"); + // The cave's first floor opens onto Route 4, so a hurt fly inside it has a centre to go to. + assert_eq!(geography::area_of(maps::MT_MOON_1F), Some(maps::ROUTE_4)); + + let mut center = World::center(); + center.map = maps::MT_MOON_POKECENTER; + center.player = Tile::new(3, 3); + center.facing = Facing::Up; + center.mons[0].hp = 10; + assert!(inside_center(&mut center)); + assert!(on_the_pad(&mut center, MacroKind::Heal)); +} + +#[test] +fn the_nurse_is_found_from_the_door_when_the_screen_does_not_draw_her() { + // Row 62, measured in the Pewter centre: the fly arrives at (3, 7) and the nurse at (3, 1) is + // six rows up, off the top of the screen, so the drawn sprites hold nobody behind the + // counter. The pad on arrival was the way out: no `HEAL`, no `GO HEAL`, and the counter's + // suppression released. + let mut center = World::center(); + center.player = Tile::new(3, 7); + center.mons[0].hp = 6; + center.offscreen = std::mem::take(&mut center.npcs); + assert!( + heal_goals(&mut center).iter().any(|aim| aim.tile == Tile::new(3, 3)), + "over the counter: {:?}", + heal_goals(&mut center) + ); + assert!(on_the_pad(&mut center, MacroKind::Heal)); + assert!(!on_the_pad(&mut center, MacroKind::GoOut), "the counter is still unfaced"); +} + #[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