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 9c0c36a..f4fe575 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 @@ -25,7 +25,7 @@ use crate::adapter::MemoryReader; use crate::emulator::buttons; use super::cartridge::{ - FACINGS, ListKind, MacroState, TalkTarget, TargetKey, Tile, battle_entry, button, item, + ExitId, FACINGS, ListKind, MacroState, TalkTarget, TargetKey, Tile, battle_entry, button, item, opposite, }; use super::geography::Amenity; @@ -422,6 +422,18 @@ struct Walk { /// without this the same tile is aimed at once per hold for ever, which is what a ledge, a /// tile-pair rule or a person on the far side of it looks like. stalled: bool, + /// The warp this walk began standing on, while it has not yet stepped off it. + /// + /// Row 64, Mt. Moon, two days. A ladder, a cave mouth, a staircase -- every warp whose exit + /// has no press -- fires at the end of a *step onto it* (`CheckWarpsNoCollision`, run after a + /// step completes), and nothing else fires it: a press into the wall beside it warps only at + /// a map's edge (`ExtraWarpCheck`, `IsPlayerFacingEdgeOfMap` off the overworld). A fly that + /// arrived on B2F's exit ladder by coming down it is standing on the one way on, and + /// `GO OBJECTIVE` and `GO WARP` both aimed at the tile underfoot, settled on it, and reported + /// `done` without a press -- section 12.2's trap, with no ledger in it for a restart to clear. + /// 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, } /// Progress through one cursor navigation. @@ -1588,8 +1600,15 @@ fn walk_frame(walk: &mut Walk, state: &mut dyn MacroState) -> Progress { }; } + // Off the warp it began on: from here the way back onto it is an ordinary step, and the + // step is what fires it ([`Walk::step_off`]). + if walk.step_off.is_some_and(|tile| tile != here) { + walk.step_off = None; + } // Plan, or re-plan. - if let Some(goal) = walk.goals.iter().find(|goal| goal.tile == here) { + if walk.step_off.is_none() + && let Some(goal) = walk.goals.iter().find(|goal| goal.tile == here) + { walk.arrival = Some(goal.arrival); walk.arrived = Some(here); walk.plan.clear(); @@ -1604,7 +1623,11 @@ fn walk_frame(walk: &mut Walk, state: &mut dyn MacroState) -> Progress { // walkable window that has moved with the player, which is the oscillation [`Walk::plan`] // documents; the plan is only remade when the ground has said something new. if walk.plan.is_empty() || walk.expect != Some(here) { - let tiles: Vec = walk.goals.iter().map(|goal| goal.tile).collect(); + let tiles: Vec = if walk.step_off.is_some() { + beside(state, here) + } else { + walk.goals.iter().map(|goal| goal.tile).collect() + }; let Some(Route { steps, .. }) = path::route_avoiding(state, &tiles, &walk.refused) else { return Progress::Blocked; }; @@ -1622,6 +1645,18 @@ fn walk_frame(walk: &mut Walk, state: &mut dyn MacroState) -> Progress { Progress::Hold(button(first)) } +/// 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. +fn beside(state: &mut dyn MacroState, here: Tile) -> Vec { + let warps: Vec = state.warps().iter().map(|warp| Tile::new(warp.x, warp.y)).collect(); + FACINGS + .iter() + .filter_map(|facing| here.step(*facing)) + .filter(|tile| !warps.contains(tile)) + .collect() +} + /// The extra scene classes a macro is allowed to run through ([`Active::spans`]). /// /// One row, and the empty slice everywhere else. `HEAL` walks in the overworld and finishes in a @@ -2138,6 +2173,18 @@ fn walk_then( .or(nearest); let here = Tile::new(player.x, player.y); let budget = walk_budget(route.steps.len()); + // Standing on the goal already, and the goal is a warp that fires by itself: the arrival + // would settle where the warp has plainly not fired ([`Walk::step_off`]). + let step_off = route + .goal + .and_then(|index| goals.get(index)) + .filter(|goal| { + route.steps.is_empty() + && goal.tile == here + && goal.arrival == Arrival::Settle + && matches!(goal.key, Some(TargetKey::Exit(ExitId::Warp(_)))) + }) + .map(|goal| goal.tile); Some(( Walk { goals, @@ -2157,6 +2204,7 @@ fn walk_then( best_distance: distance, continues, stalled: false, + step_off, }, 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 26ed293..f1da422 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 @@ -5487,3 +5487,39 @@ fn a_trainer_walking_up_teaches_the_ledgers_nothing() { assert!(!world.targets.blocked(world.map, north), "a challenge is not the road refusing"); assert!(world.pushes.is_empty(), "and the ground is as walkable as it was"); } + +#[test] +fn a_walk_that_starts_on_the_ladder_it_wants_steps_off_and_back_on_to_take_it() { + // Row 64, Mt. Moon B2F (5, 7): the fly came down the ladder and is standing on it, and the + // ladder is the one way on. The cartridge fires a ladder at the end of a step onto it and at + // no other time, so a walk that "arrives" on the tile it is already standing on and settles + // there presses nothing, reports `done` and changes nothing -- `GO OBJECTIVE` and `GO WARP`, + // 27 frames each, for two days. The walk takes it the way the cartridge does: a step onto + // free ground beside it and the step back. + for kind in [MacroKind::GoWarp, MacroKind::GoObjective] { + let mut world = World::room().at(3, 3); + world.warps = vec![Warp { x: 3, y: 3, destination_warp: 0, destination_map: 0x26 }]; + if kind == MacroKind::GoObjective { + world.objective = Some(Objective { map: 0x26, tile: None, warp: None, edge: None, target: None }); + } + assert_eq!(run(&mut world, kind), Ok(MacroAbort::Done), "{}", kind.name()); + assert_eq!(world.map, 0x26, "{} took the ladder it was standing on", kind.name()); + assert!(!world.pulses.is_empty(), "{} pressed something to take it", kind.name()); + } +} + +#[test] +fn a_walk_off_its_ladder_does_not_step_onto_another_warp() { + // Stepping onto a warp is what fires it, so the step off the one underfoot goes to plain + // ground. With the only free side another ladder there is nowhere to step off to, and the + // walk ends `blocked` -- its exit rests in the window -- rather than taking the other ladder. + let mut world = World::room().at(3, 3).wall(&[(3, 2), (2, 3), (3, 4)]); + world.warps = vec![ + Warp { x: 3, y: 3, destination_warp: 0, destination_map: 0x26 }, + Warp { x: 4, y: 3, destination_warp: 0, destination_map: 0x27 }, + ]; + world.seen_maps = BTreeSet::from([0x27]); + // Only one side is free and it is the other ladder: nowhere to step off to without firing it. + assert_eq!(run(&mut world, MacroKind::GoWarp), Ok(MacroAbort::Blocked)); + assert_eq!(world.map, 0x25, "the other ladder was not taken for this one"); +}