diff --git a/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/geography.rs b/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/geography.rs index 8779a9e..fffb3cc 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/geography.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/geography.rs @@ -665,6 +665,32 @@ pub fn amenity_of(area: u8, kind: Amenity) -> Option { .map(|(_, _, map)| *map) } +/// Whether the route [`next_step`] walks from `from` to the map `to` stays inside `area` on every +/// hop, which is what makes `to` *this area's* building from where the fly stands. +/// +/// An area is counted by map id ([`area_of`]), and a split map's pieces can be in one area while +/// the ground between them is not: Route 4's east side, and Mt. Moon B1F's exit chamber, are "in +/// Route 4" and its centre is on the west side, through B2F, which is in no area at all (row 62, +/// review round 2). A walk that has to leave the area to reach the building is not the area's +/// errand from here -- it is the mountain crossed backwards, and the first hop off the area drops +/// the button that started it. `true` when `from` is already on `to`; `false` when no route is +/// known. +pub fn route_within(from: Region, to: u8, area: u8) -> bool { + let mut here = from; + // A shortest route never revisits a piece, so it is at most as long as the table is big. + for _ in 0..=LINKS.len() + CONNECTIONS.len() { + if here.map == to { + return true; + } + let Some(hop) = next_step(here, to) else { return false }; + if hop.map != to && area_of(hop.map) != Some(area) { + return false; + } + here = hop; + } + false +} + /// Which kind of amenity `map` *is*, when it is one. /// /// The reverse lookup, and what tells the fly it is standing in a mart rather than in a house: the @@ -1101,6 +1127,32 @@ mod tests { assert_eq!(amenity_at(maps::VIRIDIAN_CITY), None); } + #[test] + fn route_4s_centre_is_the_areas_only_from_the_mountains_west_side() { + // Row 62, review round 2: Route 4's east side and B1F's exit chamber are in Route 4's + // area, and the only road from them to its centre is through B2F, which is in none. + let centre = maps::MT_MOON_POKECENTER; + let area = maps::ROUTE_4; + assert_eq!(next_step(Region::piece(maps::ROUTE_4, EAST_SIDE), centre).map(|hop| hop.map), Some(maps::MT_MOON_B1F)); + assert_eq!(next_step(Region::piece(maps::MT_MOON_B1F, B1F_EXIT), centre).map(|hop| hop.map), Some(maps::MT_MOON_B2F)); + assert_eq!(area_of(maps::MT_MOON_B2F), None, "the fossil floor opens on no outdoor map"); + assert!(!route_within(Region::piece(maps::ROUTE_4, EAST_SIDE), centre, area)); + assert!(!route_within(Region::piece(maps::MT_MOON_B1F, B1F_EXIT), centre, area)); + // The west side, the first floor and the three chambers a first-floor ladder reaches. + assert!(route_within(Region::piece(maps::ROUTE_4, WEST_SIDE), centre, area)); + assert!(route_within(Region::whole(maps::MT_MOON_1F), centre, area)); + for chamber in [B1F_WEST, B1F_MIDDLE, B1F_SOUTH] { + assert!(route_within(Region::piece(maps::MT_MOON_B1F, chamber), centre, area), "{chamber}"); + } + assert!(route_within(Region::whole(centre), centre, area), "already there"); + // A town is one piece, so every building off it is within: nothing moved for them. + for (town, kind) in [(maps::VIRIDIAN_CITY, Amenity::Mart), (maps::PEWTER_CITY, Amenity::Center)] { + let building = amenity_of(town, kind).unwrap(); + assert!(route_within(Region::whole(town), building, town)); + assert!(route_within(Region::whole(maps::PEWTER_GYM), maps::PEWTER_POKECENTER, maps::PEWTER_CITY)); + } + } + #[test] fn every_amenity_is_on_the_map_graph() { // A building with no `LINKS` row is one `GO OBJECTIVE` and `GO SHOP` could never route to, 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 3e2b751..87f8a63 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 @@ -877,7 +877,7 @@ pub fn errand(state: &mut dyn MacroState, kind: Amenity) -> Option { if kind == Amenity::Mart && state.money() < CHEAPEST_PURCHASE { return None; } - let map = geography::amenity_of(area, kind)?; + let map = amenity_in_reach(state, area, kind)?; // **A building this run has already been inside is an errand already discharged.** The session // ledger above is the errand's own record and it is the one that can be missing: it is written // from the frame the fly stands on the building's map, so a restore starts with it empty and @@ -1067,7 +1067,33 @@ pub fn amenity_wanted(state: &mut dyn MacroState, kind: Amenity) -> Option { if !service_needed(state, kind) { return None; } - geography::amenity_of(area_here(state)?, kind) + // **The room the rung is in is not left for a service** (row 62, review round 2). The same + // rule row 29 gave the ways out ([`exit_tiers`]) and row 58 measured in the Pewter Gym: while + // the thing that earns the rung stands on this map untalked and unexcluded, nothing walks the + // fly out of the room. A hurt party in a gym is the common case -- its trainers are why -- and + // `GO HEAL` / `GO SHOP` from the gym's floor were four of its six doormat bounces. The errand + // above needs no such line: while it stands it *is* the objective, so the rung's target is not + // on this map. The escape hatch is the rule's own: a target no walk reaches is excluded for + // the window, `objective_targets` empties, and the service is wanted again. + if !objective_targets(state).is_empty() { + return None; + } + let area = area_here(state)?; + amenity_in_reach(state, area, kind) +} + +/// `area`'s building of `kind`, when the route to it from the piece of ground the fly stands on +/// stays inside the area ([`geography::route_within`]). +/// +/// Row 62's review, round 2: Route 4's centre is on the mountain's west side, and Route 4's east +/// side and Mt. Moon B1F's exit chamber are in Route 4's area too -- but the only road from them +/// to that door runs through B2F, which is in no area, so the button that started the walk left +/// the pad one hop in, opposite `GO OBJECTIVE`'s road to Cerulean. From there the centre is not +/// this area's building; Cerulean's is one edge away, and it is its own area's. +fn amenity_in_reach(state: &mut dyn MacroState, area: u8, kind: Amenity) -> Option { + let map = geography::amenity_of(area, kind)?; + let from = region_here(state)?; + geography::route_within(from, map, area).then_some(map) } /// Inside a building with a job to do: the tiles to face its counter person from, whatever the 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 8032cc4..72e304a 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 @@ -27,7 +27,7 @@ use super::executor::{ WALK_FRAME_CEILING, walk_budget, }; use super::palette::{ - MacroId, MacroKind, Palette, SLOTS, amenity_goals, answer_key, errand, facing_nurse, + MacroId, MacroKind, Palette, SLOTS, amenity_goals, amenity_wanted, answer_key, errand, facing_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, @@ -5725,3 +5725,86 @@ fn a_purchase_answers_the_price_box_once_the_cartridge_has_drawn_it() { assert_eq!(run(&mut world, MacroKind::BuyBall).unwrap(), MacroAbort::Blocked); assert_eq!(world.bought, 0); } + +// --------------------------------------------------------------------------------------------- +// Row 62, review round 2: a service is not dealt off the area's ground, nor out of the rung's room +// --------------------------------------------------------------------------------------------- + +/// A hurt fly with no ball and money for one, on `map` at `(x, y)`, the rung in Cerulean. +fn hurt_on(map: u8, x: u8, y: u8) -> World { + let mut world = World::room().at(x, y); + world.map = map; + world.size = MapSize { width: 40, height: 36 }; + world.warps = Vec::new(); + world.mons[0].hp = 7; + world.money = 1_000; + world.objective = Some(Objective { + map: maps::CERULEAN_CITY, + tile: None, + warp: None, + edge: None, + target: None, + }); + world +} + +#[test] +fn east_of_mt_moon_route_4s_centre_is_not_the_areas_errand_or_service() { + // B1: Route 4's east side (where the fly comes out of the mountain, hurt) and B1F's exit + // chamber are "in Route 4", and the centre is on the west side through B2F, which is in no + // area. The pad dealt `GO HEAL` there, its walk took it into B2F, and it left the pad one hop + // in, opposite `GO OBJECTIVE`'s road back up: a two-map ring by construction. + for (map, x, y, place) in [ + (maps::ROUTE_4, 30, 10, "Route 4 east"), + (maps::MT_MOON_B1F, 26, 4, "B1F exit chamber"), + ] { + let mut world = hurt_on(map, x, y); + assert!(party_needs_rest(&mut world)); + assert_eq!(errand(&mut world, Amenity::Center), None, "{place}: not the area's errand from here"); + assert_eq!(amenity_wanted(&mut world, Amenity::Center), None, "{place}: nor its service"); + assert!(amenity_goals(&mut world, Amenity::Center).is_empty(), "{place}"); + assert!(!on_the_pad(&mut world, MacroKind::GoHeal), "{place}: GO HEAL is off the pad"); + assert_eq!( + super::palette::objective_place(&mut world).map(|place| place.map), + Some(maps::CERULEAN_CITY), + "{place}: GO OBJECTIVE is the rung, not the centre behind the mountain" + ); + } + + // West of the mountain, and on the first floor, it is the area's centre as it was. + let mut world = hurt_on(maps::ROUTE_4, 12, 8); + world.warps = vec![Warp { x: 11, y: 5, destination_warp: 0, destination_map: maps::MT_MOON_POKECENTER }]; + assert_eq!(amenity_wanted(&mut world, Amenity::Center), Some(maps::MT_MOON_POKECENTER)); + assert!(on_the_pad(&mut world, MacroKind::GoHeal), "the door is a few tiles away"); + let mut world = hurt_on(maps::MT_MOON_1F, 14, 20); + assert_eq!(amenity_wanted(&mut world, Amenity::Center), Some(maps::MT_MOON_POKECENTER)); +} + +#[test] +fn a_service_does_not_walk_the_fly_out_of_the_rungs_room() { + // B2, row 58's ROM test on the branch: in the Pewter Gym with BROCK up the room, a hurt party + // and an empty bag put `GO HEAL` and `GO SHOP` on the pad, and both walked out of the door the + // fly had just come in by -- four of six arrivals bounced. Row 29's rule is the ways out's and + // is now the services' too. + let mut world = pewter_gym_doormat(); + world.mons[0].hp = 7; + world.money = 1_000; + world.bag = Vec::new(); + assert!(service_needed(&mut world, Amenity::Center)); + assert!(service_needed(&mut world, Amenity::Mart)); + assert!(!super::palette::objective_targets(&mut world).is_empty(), "BROCK is in the room"); + assert_eq!(amenity_wanted(&mut world, Amenity::Center), None); + assert_eq!(amenity_wanted(&mut world, Amenity::Mart), None); + assert!(!on_the_pad(&mut world, MacroKind::GoHeal)); + assert!(!on_the_pad(&mut world, MacroKind::GoShop)); + assert!(on_the_pad(&mut world, MacroKind::GoObjective), "the walk to him is still there"); + + // The rule's own escape hatch: with the room's people talked to (or excluded), the services + // are wanted again and the centre is Pewter's. + for slot in [1, 2] { + world.talked.insert(TalkTarget::Sprite(slot)); + } + assert!(super::palette::objective_targets(&mut world).is_empty()); + assert_eq!(amenity_wanted(&mut world, Amenity::Center), Some(maps::PEWTER_POKECENTER)); + assert_eq!(amenity_wanted(&mut world, Amenity::Mart), Some(maps::PEWTER_MART)); +}