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 a5e2862..469c682 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 @@ -444,6 +444,14 @@ struct Walk { /// So a walk that starts on its own step-fired warp takes it the way the cartridge does: one /// step onto free ground beside it, and the step back. step_off: Option, + /// Whether the walk settled on a way out and the map did not change: the exit did not take it. + /// + /// The general half of row 64 (section 12.2's rule, held by the executor instead of by each + /// macro): a way out's whole promise is a different map, so a `Settle` arrival on one that + /// leaves the fly on this map has not arrived anywhere. It ends `Blocked` -- the exit rests in + /// the window -- and, when the fly never left the tile it pressed from, + /// [`MacroMachine::unfired_here`] keeps the button off this tile for the window too. + unfired: bool, } /// Progress through one cursor navigation. @@ -909,6 +917,9 @@ impl MacroMachine { Decided::End(outcome) => { let pushed = state.scripted(); let at = pushed.then(|| state.player()).flatten(); + if outcome == MacroAbort::Blocked { + self.unfired_here(state); + } return self.finish(outcome, true, pushed, at); } Decided::Pop => { @@ -1021,6 +1032,26 @@ impl MacroMachine { self.pending_push_calm = 0; } + /// A way out that did not fire, pressed from the tile the fly is still standing on: the button + /// is not dealt again from here for the window (row 57's ledger, [`Walk::unfired`]). + /// + /// The blocked entry alone rests the exit for every list but a last resort, and a last resort + /// ignores the blocked ledger by design -- so on a map with nothing else on it the same button + /// would be dealt at the same dead exit once per hold, which is the ring this is here to end. + /// A pad with nothing left that can run is empty and the fly waits, section 13.1's answer. + fn unfired_here(&mut self, state: &mut dyn MacroState) { + let Some(active) = self.active.as_ref() else { return }; + let Some(Step::Walk(walk)) = active.plan.front() else { return }; + if !walk.unfired { + return; + } + let Some(player) = state.player() else { return }; + let here = Tile::new(player.x, player.y); + if player.map == walk.map && active.from == Some(here) { + self.refused_at = Some((player.map, active.kind.slot(), here)); + } + } + /// Whether the running walk is mid-hop: it pressed into a ledge ([`Walk::hop`]), the /// cartridge has the joypad with no text up, and the fly is on the hop's own line -- the tile /// it pressed from, the ledge, or the landing -- on the walk's own map. A wild battle on the @@ -1554,6 +1585,11 @@ fn walk_frame(walk: &mut Walk, state: &mut dyn MacroState) -> Progress { walk.held += 1; if walk.held <= SETTLE_FRAMES { Progress::Hold(buttons::NONE) + } else if settled_on_a_way_out(walk) { + // Still on this map (the top of this function ends the walk on any other): + // the way out did not take the fly anywhere ([`Walk::unfired`]). + walk.unfired = true; + Progress::Blocked } else { arrived(walk.continues) } @@ -1687,6 +1723,16 @@ fn walk_frame(walk: &mut Walk, state: &mut dyn MacroState) -> Progress { Progress::Hold(button(first)) } +/// Whether the goal the walk is settling on is a way out: an exit key, whose promise is a map +/// change. A tile the objective names is not one -- standing on it is the point. +fn settled_on_a_way_out(walk: &Walk) -> bool { + walk.arrived.is_some_and(|tile| { + walk.goals + .iter() + .any(|goal| goal.tile == tile && matches!(goal.key, Some(TargetKey::Exit(_)))) + }) +} + /// The tiles one step from `here` that are not themselves warps: where a walk standing on a warp /// it has to fire steps to first ([`Walk::step_off`]). Stepping onto another warp would fire that /// one instead. Which of them is ground is the route search's answer, as it is for every goal. @@ -2248,6 +2294,7 @@ fn walk_then( stalled: false, hop: None, step_off, + unfired: false, }, target, )) 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 0e811d2..cc5d41f 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 @@ -195,6 +195,10 @@ struct World { /// landing. `HandleLedges` takes the joypad for the length of it, so the scene reads /// `Unknown` and the world is scripted. hop: Option<(u32, Tile, Tile)>, + /// Warps a step onto does not fire: the "turns out to need some other press" of + /// [`path::exits`]'s `outward`, for the executor's rule about a way out that did not take the + /// fly anywhere. + duds: BTreeSet, /// A frame at which the cartridge takes the joypad, which is what the Viridian gate does. scripted_at: Option, /// Every completed pulse, in order. @@ -274,6 +278,7 @@ impl World { scripted: false, ledges: Vec::new(), hop: None, + duds: BTreeSet::new(), scripted_at: None, pulses: Vec::new(), held: 0, @@ -581,6 +586,9 @@ impl World { else { return; }; + if self.duds.contains(&next) { + return; + } // An interior warp fires on the step onto it; a doormat only when the step onto it is the // same direction the step off the map would be. match outward(next, self.size.height) { @@ -902,6 +910,10 @@ fn settle(machine: &mut MacroMachine, world: &mut World) { assert_eq!(map, world.map); world.pushes.insert(tile); } + // `PokemonPalette::record_talk` drains this after every finish, not only after a refusal. + if let Some((map, slot, tile)) = machine.take_refused() { + world.targets.record_refused(map, slot, tile); + } if let Some((map, target)) = machine.take_talked() { assert_eq!(map, world.map); world.talked.insert(target); @@ -5612,3 +5624,48 @@ fn a_ledge_the_walk_presses_into_is_hopped_and_is_not_a_push_back() { assert!(run(&mut below, MacroKind::GoObjective).is_err(), "a ledge is not climbed"); assert_eq!(below.player, Tile::new(3, 6)); } + +#[test] +fn a_way_out_that_does_not_take_the_fly_anywhere_is_not_done() { + // The general half of row 64 (section 12.2's rule, held by the executor): a way out's promise + // is another map, so settling on one that leaves the fly on this map is not an arrival. Live, + // `GO OBJECTIVE` and `GO WARP` reported that `done` ~750 times per ten brain minutes on one + // tile for two days. Here the warp does not fire at all, whatever the walk does. + let mut world = World::room().at(1, 1); + world.warps = vec![Warp { x: 5, y: 5, destination_warp: 0, destination_map: 0x26 }]; + world.duds = BTreeSet::from([Tile::new(5, 5)]); + let mut machine = MacroMachine::new(0x1234_5678); + assert_eq!(run_with(&mut machine, &mut world, MacroKind::GoWarp), Ok(MacroAbort::Blocked)); + assert_eq!((world.map, world.player), (0x25, Tile::new(5, 5)), "walked to it; it did not fire"); + let exit = TargetKey::Exit(ExitId::Warp(0)); + assert!(world.targets.blocked(world.map, exit), "the dead exit rests in the window"); + // Walked to from elsewhere, so the button is not held against the tile it now stands on: + // a different tile is a different press. + assert!(!world.targets.refused(world.map, MacroKind::GoWarp.slot(), world.player)); +} + +#[test] +fn a_way_out_pressed_from_where_it_does_not_fire_is_not_dealt_again_from_there() { + // Standing on it: step off, step back on, nothing. The blocked entry rests the exit for every + // list but a last resort, and the last resort ignores the blocked ledger by design, so on a + // map with nothing else on it the same button would be dealt at the same dead exit once per + // hold. Row 57's ledger ends that: not from this tile, for the window. + let mut world = World::room().at(3, 3); + world.warps = vec![Warp { x: 3, y: 3, destination_warp: 0, destination_map: 0x26 }]; + world.duds = BTreeSet::from([Tile::new(3, 3)]); + world.seen_maps = BTreeSet::from([0x26]); + world.stood = (0..8).flat_map(|y| (0..8).map(move |x| Tile::new(x, y))).collect(); + let mut machine = MacroMachine::new(0x1234_5678); + // Nothing else on this map: the last resort is what deals `GO WARP` here. + assert!(pick_opt(&mut world, MacroKind::GoWarp), "the last resort deals the only way out"); + assert_eq!(run_with(&mut machine, &mut world, MacroKind::GoWarp), Ok(MacroAbort::Blocked)); + assert_eq!((world.map, world.player), (0x25, Tile::new(3, 3))); + assert!(world.targets.refused(world.map, MacroKind::GoWarp.slot(), world.player)); + assert!(!pick_opt(&mut world, MacroKind::GoWarp), "not dealt again from the tile it failed on"); +} + +/// Whether `kind` is on the pad the world's scene deals now. +fn pick_opt(world: &mut World, kind: MacroKind) -> bool { + let scene = world.scene(); + Palette::for_scene(scene, world).slots.iter().any(|slot| slot.is_some_and(|spec| spec.kind == kind)) +}