diff --git a/docs/design/macros.md b/docs/design/macros.md index 76ed048..5e00cc6 100644 --- a/docs/design/macros.md +++ b/docs/design/macros.md @@ -2209,6 +2209,22 @@ What changed, all of it knowledge inside macros: so the walk's one aim is the tile on that side and only facing it from there is the arrival and `TALK`'s precondition. An argument that is an item or a text id and happens to equal a facing value only narrows the approach. +- **An errand is recoverable inside one session (row 73 review).** The talked ledger is the + session's and never forgot, but the cartridge re-shows people: Route 25's script resets Bill's + event once the fly has left, and a Bill whose bag was full never handed over the ticket. A + conversation had on the last visit then retired this one's and the rung was unreachable until a + restart. The palette now notes the room while the errand is the objective and, on the first + frame the fly stands anywhere else with that errand still the objective, forgets that room's + talked entries (`Talked::forget_map`). Other rooms' entries and a room that is not the errand + keep theirs. The bag-full case needs no extra rule: nothing is left to aim at, the exits return, + and the fly leaves and comes back. +- **A one-sided target nobody can stand in front of is not waited for.** If every tile a hidden + event can be pressed from is one the cartridge pushes the fly off (the pushed ledger, row 37), + the walk had no aim, so no walk ended `blocked` and the ways out stayed withheld for good. Such + a target leaves the errand's list at once (`palette::servable`), which returns the exits. +- **A hidden-event entry off its own map is skipped.** The Safari Zone gate lists a nugget + "inaccessible" at (10, 1) on an 8 x 6 map, and the rest houses a copied Pokecenter PC; one such + entry no longer voids the rest of the map's list. The fly still chooses every press: `GO OBJECTIVE` to walk, `TALK` to press A, `YES`, `NO` or `NEXT` in Bill's boxes. Nothing presses for it, no button is added to any pad, and the readout, the diff --git a/services/flysim/crates/flybrain-gb/src/pokemon_red/fake_wram.rs b/services/flysim/crates/flybrain-gb/src/pokemon_red/fake_wram.rs index c7951be..6fe8414 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/fake_wram.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/fake_wram.rs @@ -460,6 +460,29 @@ impl Wram { self } + /// The cartridge's hidden-event table with one map in it: `HiddenEventMaps` listing `map`, + /// `HiddenEventPointers` pointing at a list of `(y, x, argument)` entries (six bytes each, the + /// routine's `dba` a bank-$11 address). + pub fn hidden_table(&mut self, map: u8, entries: &[(u8, u8, u8)]) -> &mut Self { + use super::state::poke::hidden::{ENTRY_BYTES, MAPS_ADDRESS, POINTERS_ADDRESS, TABLE_BANK}; + let list = 0x7000u16; + let mut put = |address: u16, byte: u8| { + self.rom.insert((TABLE_BANK, address), byte); + }; + put(MAPS_ADDRESS, map); + put(MAPS_ADDRESS + 1, 0xff); + put(POINTERS_ADDRESS, (list & 0xff) as u8); + put(POINTERS_ADDRESS + 1, (list >> 8) as u8); + for (index, (y, x, argument)) in entries.iter().enumerate() { + let base = list + index as u16 * ENTRY_BYTES; + for (offset, byte) in [*y, *x, *argument, TABLE_BANK, 0x00, 0x60].into_iter().enumerate() { + put(base + offset as u16, byte); + } + } + put(list + entries.len() as u16 * ENTRY_BYTES, 0xff); + self + } + /// The cartridge's `ItemUsePtrTable`, one pointer per item id, grouped the way the pinned /// commit groups them: the balls share one routine, the potions, drinks, status heals and /// revives another, the four X stat items another, the four PP restores another, and every 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 2e90aca..d871a20 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 @@ -691,6 +691,13 @@ impl Talked { self.0.insert((map, target)); } + /// Forget everything talked to on `map` (row 73's errand: the cartridge shows the room's + /// people again after the fly leaves, and a conversation had on the last visit is not this + /// one's). + pub fn forget_map(&mut self, map: u8) { + self.0.retain(|(at, _)| *at != map); + } + /// How many things this session has talked to, for a log line and the tests. pub fn len(&self) -> usize { self.0.len() 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 733a84b..f3c7db3 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 @@ -107,6 +107,11 @@ pub struct PokemonPalette { nearest: Option<(u8, u32)>, /// Whether the last `observe` was the frame that number fell on. nearer: bool, + /// The map the fly was last seen standing in while the ladder's objective was an errand in + /// it (row 73). On leaving it with the errand still the objective, the talked entries of that + /// room are forgotten: Route 25 re-shows Bill, and a Bill whose bag was full never handed + /// over the ticket, so a conversation had on the last visit must not retire this one's. + errand_seen: Option, /// The map of the last frame that was not a warp's tear, and how many tear frames have run /// since (row 58, [`PokemonPalette::tear`], [`TEAR_FRAMES`]). /// @@ -150,6 +155,7 @@ impl PokemonPalette { grids: MapGrids::default(), nearest: None, nearer: false, + errand_seen: None, settled: None, tear_frames: 0, naming_since: None, @@ -316,7 +322,7 @@ impl MacroPalette for PokemonPalette { // name gets the whole bound and a restore starts it again. self.naming_since = naming.map(|_| self.naming_since.unwrap_or(self.now_ms)); let naming_elapsed = self.naming_since.map_or(0.0, |since| (self.now_ms - since).max(0.0)); - let (scene, bindings, raw, standing, stepping, approach) = { + let (scene, bindings, raw, standing, stepping, approach, errand) = { let Self { machine, mode, @@ -372,12 +378,28 @@ impl MacroPalette for PokemonPalette { let hops = geography::hops(palette::region_here(&mut state)?, objective.map)?; Some((objective.map, hops)) }); + // Row 73: whether the objective is an errand, and in which room. + let errand = standing.and_then(|_| { + let objective = palette::objective_place(&mut state)?; + (objective.target == Some(crate::adapter::PlaceKind::Errand)).then_some(objective.map) + }); *cached = Some(palette); - (scene, bindings, raw, standing, stepping, approach) + (scene, bindings, raw, standing, stepping, approach, errand) }; // Section 12.7: the macro layer's own answer to "has the run stood here", because the // adapter's reward ledger cannot record a doormat. if let Some(player) = standing { + if let Some(room) = self.errand_seen + && player.map != room + { + if errand == Some(room) { + self.talked.forget_map(room); + } + self.errand_seen = None; + } + if errand == Some(player.map) { + self.errand_seen = Some(player.map); + } // New ground under the fly is the one thing that can change which tiles of this map // it can reach, so it is what clears the map's frontier mark (section 12.14). A tile // the ledger already had changes nothing and clears nothing. @@ -594,7 +616,7 @@ mod tests { use crate::pokemon_red::fake_wram::{self, REDS_HOUSE_1F, WALL_TILE, Wram}; use crate::pokemon_red::macros::geography::Amenity; use crate::pokemon_red::maps; - use crate::pokemon_red::macros::cartridge::{Edge, ExitId, MacroState}; + use crate::pokemon_red::macros::cartridge::{Edge, ExitId, MacroState, TalkLedger, TalkTarget}; /// A ledger with one exit in it, for the wiring test below. struct OneVisited(MapExit); @@ -784,6 +806,54 @@ mod tests { assert!(palette.nearer_the_objective(), "out of the front door is nearer still"); } + /// A ledger whose objective is the errand in one room (row 73). + struct ErrandIn(u8); + + impl RunLedger for ErrandIn { + fn exit_visited(&self, _exit: MapExit) -> bool { + false + } + + fn objective(&self) -> Option { + Some(crate::adapter::MapPlace::errand(self.0)) + } + } + + #[test] + fn an_errand_rooms_talked_entries_are_forgotten_when_the_fly_leaves_it() { + // Row 73 review N3. The talked ledger is the session's and never forgot, but the + // cartridge re-shows Bill after the fly leaves (Route 25's script resets his event), and a + // Bill whose bag was full never gave the ticket: a conversation had on the last visit + // retired this one's, and rung 16 was unreachable until a restart. + let mut wram = Wram::new(); + wram.started().map(maps::BILLS_HOUSE, 8, 8, 2, 6).facing(0).house_collision(); + let mut palette = PokemonPalette::new(7); + let ledger = ErrandIn(maps::BILLS_HOUSE); + palette.observe(&mut wram, &ledger); + palette.talked.record(maps::BILLS_HOUSE, TalkTarget::Sprite(1)); + // A conversation elsewhere is not the errand's and stays. + palette.talked.record(maps::VIRIDIAN_CITY, TalkTarget::Sprite(2)); + palette.observe(&mut wram, &ledger); + assert_eq!(palette.talked(), 2, "still in the room: nothing is forgotten"); + + wram.map(maps::ROUTE_25, 40, 10, 45, 4); + palette.observe(&mut wram, &ledger); + palette.observe(&mut wram, &ledger); + assert!(!palette.talked.talked(maps::BILLS_HOUSE, TalkTarget::Sprite(1)), "left: asked again"); + assert!(palette.talked.talked(maps::VIRIDIAN_CITY, TalkTarget::Sprite(2)), "other rooms keep theirs"); + + // And a room that is not the objective's errand keeps its entries on leaving. + let mut wram = Wram::new(); + wram.started().map(maps::BILLS_HOUSE, 8, 8, 2, 6).facing(0).house_collision(); + let mut palette = PokemonPalette::new(7); + let elsewhere = Bound(maps::PEWTER_GYM); + palette.observe(&mut wram, &elsewhere); + palette.talked.record(maps::BILLS_HOUSE, TalkTarget::Sprite(1)); + wram.map(maps::ROUTE_25, 40, 10, 45, 4); + palette.observe(&mut wram, &elsewhere); + assert_eq!(palette.talked(), 1); + } + #[test] fn standing_on_new_ground_is_what_clears_a_maps_frontier_mark() { // Section 12.14's other half, wired: the mark is written by a `GO FRONTIER` that could 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 a039154..76ccaae 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 @@ -2282,12 +2282,27 @@ pub fn objective_targets(state: &mut dyn MacroState) -> Vec<(Tile, TalkTarget)> fn open_targets(state: &mut dyn MacroState, targets: Vec<(Tile, TalkTarget)>) -> Vec<(Tile, TalkTarget)> { targets .into_iter() - .filter(|(_, target)| { - !state.talked(*target) && !state.blocked(TargetKey::Thing(*target)) + .filter(|(tile, target)| { + !state.talked(*target) + && !state.blocked(TargetKey::Thing(*target)) + && servable(state, *tile, *target) }) .collect() } +/// Whether some tile the objective's `target` can be pressed from is not one a script pushes the +/// fly off (row 37). Only a one-sided hidden event can fail this: its single stand tile is the +/// only door in, and with that tile pushed the walk has no aim, so it never ends in a refusal +/// that earns a blocked entry -- and while a target waits the exits are withheld, so the room +/// became a trap. A target nobody can stand in front of is not one the rung is waiting on, and +/// it leaves the list at once: the exits are back the frame the pushed tile is learned. +fn servable(state: &mut dyn MacroState, tile: Tile, target: TalkTarget) -> bool { + let Some(need) = required_facing(state, target) else { return true }; + FACINGS.into_iter().filter(|facing| need == opposite(*facing)).any(|facing| { + tile.step(facing).is_some_and(|stand| !state.pushed_tile(stand.x, stand.y)) + }) +} + /// The one side the objective's `target` is pressed from, when the cartridge names one: a hidden /// event whose routine checks the player's facing (row 73, Bill's PC). `None` for every sprite and /// sign, which answer from any side. 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 ca89a17..7aaa237 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 @@ -6706,6 +6706,27 @@ fn a_hidden_event_is_the_errands_next_step_once_no_person_is_left_and_it_is_face assert_eq!(super::palette::objective_targets(&mut below), vec![(Tile::new(4, 4), TalkTarget::Sprite(2))]); } +#[test] +fn a_one_sided_target_whose_only_stand_tile_is_pushed_is_not_waited_for() { + // Row 73 review N4. The PC answers only from below; if the cartridge pushes the fly off + // that tile the walk has no aim, so no walk ever ends `blocked`, and while a target waited the + // exits were withheld for good. The target leaves the list and the way out is back. + let mut world = bills_house().at(3, 5); + world.npcs.clear(); + world.talked.insert(TalkTarget::Sprite(1)); + assert_eq!(super::palette::objective_targets(&mut world).len(), 1); + assert!(!on_the_pad(&mut world, MacroKind::GoOut)); + world.pushes.insert(Tile::new(1, 5)); + assert!(super::palette::objective_targets(&mut world).is_empty(), "nobody can stand in front of it"); + assert!(on_the_pad(&mut world, MacroKind::GoOut), "so the room is not a trap"); + // A pushed tile somewhere else changes nothing. + let mut other = bills_house().at(3, 5); + other.npcs.clear(); + other.talked.insert(TalkTarget::Sprite(1)); + other.pushes.insert(Tile::new(2, 5)); + assert_eq!(super::palette::objective_targets(&mut other).len(), 1); +} + #[test] fn a_hidden_event_is_nobody_elses_target() { // A hidden event is every PC, poster and trash can in Kanto. Outside the errand that names it 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 c99aaa8..5361fd8 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/state.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/state.rs @@ -348,8 +348,6 @@ pub mod poke { pub const TYPE_ELECTRIC: u8 = 0x17; } - /// The cartridge's item-use dispatch and the ids that name its routines (row 63, - /// `docs/design/macros.md` 12.27). /// The cartridge's hidden-event tables (row 73, `docs/design/macros.md` 12.35). /// /// `engine/overworld/hidden_events.asm`: `CheckForHiddenEvent` walks `HiddenEventMaps`, one @@ -375,6 +373,8 @@ pub mod poke { pub const FACINGS: [u8; 4] = [0x00, 0x04, 0x08, 0x0c]; } + /// The cartridge's item-use dispatch and the ids that name its routines (row 63, + /// `docs/design/macros.md` 12.27). pub mod items { /// `engine/items/item_effects.asm`: `UseItem_` jumps through `ItemUsePtrTable`, one /// little-endian pointer per item id from `MASTER_BALL` (1) to `MAX_ELIXER` (`$53`), in @@ -2084,7 +2084,7 @@ pub fn signs(memory: &mut dyn MemoryReader) -> Vec { /// /// Empty on a map the table does not list, when the seam has no cartridge image behind it, and /// when the bytes are not the shape the disassembly describes -- a map list with no end, a pointer -/// outside the bank's window, a list longer than any map's, a tile off the loaded map. A table +/// outside the bank's window, a list longer than any map's. An entry whose tile is off the loaded map is skipped, not fatal. A table /// this module cannot read is not one it reports, so a wrong address narrows (`GO OBJECTIVE` has /// one less thing to aim at) and never invents a tile. pub fn hidden_events(memory: &mut dyn MemoryReader) -> Vec { @@ -2125,9 +2125,15 @@ fn hidden_events_inner(memory: &mut dyn MemoryReader) -> Option let argument = memory.read_rom(TABLE_BANK, base + 2)?; let routine = u16::from_le_bytes([memory.read_rom(TABLE_BANK, base + 4)?, memory.read_rom(TABLE_BANK, base + 5)?]); - if x >= size.width || y >= size.height || routine >= 0x8000 { + if routine >= 0x8000 { return None; } + // The cartridge has entries off their own map (the Safari Zone gate's inaccessible + // nugget, the rest houses' copied Pokecenter PC): the map's other entries are still + // good, so only the bad one is skipped. The entry index stays the list position. + if x >= size.width || y >= size.height { + continue; + } let facing = FACINGS.contains(&argument).then(|| facing_from(argument)); out.push(HiddenEvent { x, y, index: entry as u8, facing }); } diff --git a/services/flysim/crates/flybrain-gb/src/pokemon_red/state/tests.rs b/services/flysim/crates/flybrain-gb/src/pokemon_red/state/tests.rs index ea8183c..2585e36 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/state/tests.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/state/tests.rs @@ -1462,3 +1462,23 @@ fn the_naming_screen_reads_unknown_in_a_battle_and_title_before_the_game() { wram.naming_screen(0, 7); assert_eq!(crate::pokemon_red::scene::detect(&mut wram), Scene::Title); } + +#[test] +fn an_off_map_hidden_event_is_skipped_and_the_maps_other_entries_are_kept() { + // Row 73 review N1. The Safari Zone gate lists a nugget at (10, 1) on an 8 x 6 map, and the + // rest houses a copied Pokecenter PC at (13, 3) on 8 x 8 maps: one entry off its own map used + // to void the list. + let mut wram = Wram::new(); + // Two blocks by two is a 4 x 4 tile map. + wram.map(0x5d, 2, 2, 1, 1).hidden_table(0x5d, &[(1, 10, 0x00), (2, 1, 0x04), (9, 1, 0x08), (0, 3, 0x17)]); + let events = hidden_events(&mut wram); + assert_eq!( + events.iter().map(|event| (event.x, event.y, event.index)).collect::>(), + vec![(1, 2, 1), (3, 0, 3)], + "the entries on the map, under their own list positions" + ); + // A list with nothing wrong in it is what it was. + let mut wram = Wram::new(); + wram.map(0x5d, 2, 2, 1, 1).hidden_table(0x5d, &[(2, 1, 0x04)]); + assert_eq!(hidden_events(&mut wram).len(), 1); +} diff --git a/services/flysim/crates/flysim/tests/rom_macros_mode.rs b/services/flysim/crates/flysim/tests/rom_macros_mode.rs index 6a68c3e..da0e2bf 100644 --- a/services/flysim/crates/flysim/tests/rom_macros_mode.rs +++ b/services/flysim/crates/flysim/tests/rom_macros_mode.rs @@ -5276,6 +5276,60 @@ fn row73_the_cell_separator_pc_is_pressed_and_bill_hands_over_the_ticket() { row73_drive_to_the_ticket(&mut run, 36_000); } +/// Row 73 review N3: the errand is recoverable inside one session. The talked ledger is the +/// session's and the cartridge re-shows Bill once the fly has left (Route 25's script resets his +/// event), so a conversation had on the last visit must not retire this one's: the room's talked +/// entries are forgotten when the fly leaves it with the errand still the objective. +/// +/// From the door checkpoint, over the stub rotation: wait for the first `TALK` in the house, then +/// walk the fly out by hand (the macros withhold the exits while the errand waits, which is the +/// point), and the stub earns the ticket from there, in the same session. +#[test] +fn row73_the_errand_survives_leaving_the_house_mid_conversation() { + let rom = skip_without_rom!(); + let Some(checkpoint) = row73_checkpoint("FLY_ROW73_DOOR_CHECKPOINT") else { + eprintln!("skipped: no FLY_ROW73_DOOR_CHECKPOINT"); + return; + }; + let mut run = Run::resume(&rom, MacroMode::Macros, &checkpoint); + assert_eq!(run.map(), BILLS_HOUSE, "the checkpoint is inside Bill's house"); + let mut talked = false; + for _ in 0..24_000 { + run.frame(); + if run.talk_starts_by_map.get(&BILLS_HOUSE).copied().unwrap_or(0) > 0 { + talked = true; + break; + } + } + assert!(talked, "no TALK in the house: {:?}", run.talk_starts_by_map); + assert!(!run.event(EVENT_GOT_SS_TICKET), "the ticket is not earned by the first conversation"); + // Out by hand: along the room to the doormat's columns (2 and 3) and down, pressing B between + // the cartridge's own text. + let mut out = false; + let mut last = None; + for frame in 0..4_800u32 { + let here = flybrain_gb::pokemon_red::state::player(&mut run.gb); + last = here.map(|player| (player.map, player.x, player.y)); + let heading = match here { + Some(player) if player.x < 2 => flybrain_gb::buttons::RIGHT, + Some(player) if player.x > 3 => flybrain_gb::buttons::LEFT, + _ => flybrain_gb::buttons::DOWN, + }; + run.gb.set_buttons(if frame % 16 < 12 { heading } else { flybrain_gb::buttons::B }); + run.gb.run_frame().expect("a frame should complete"); + run.ms += MS_PER_FRAME; + run.adapter.sample(&mut run.gb, run.ms); + if run.map() != BILLS_HOUSE && run.map() != u32::MAX { + out = true; + break; + } + } + assert!(out, "the fly could not be walked out of the house, last at {last:?}"); + run.gb.set_buttons(0); + // Back to the macros, in the same session: the errand is Bill's again, and the ticket is earned. + row73_drive_to_the_ticket(&mut run, 120_000); +} + /// Drive the stub rotation until the ticket's flag is set, counting the house's doors on the way: /// `(into, out)`. fn row73_drive_to_the_ticket(run: &mut Run, budget: u32) -> (u32, u32) {