diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_FlyMap.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_FlyMap.cpp new file mode 100644 index 0000000000..56a16709a7 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_FlyMap.cpp @@ -0,0 +1,47 @@ +/* Fly Map + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "PokemonFRLG_FlyMap.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +const std::vector& kanto_fly_towns(){ + static const std::vector TOWNS{ + {"PalletTown", {4, 11}, {"PalletTown", {6, 8}}}, + {"ViridianCity", {4, 8}, {"ViridianCity", {26, 27}}}, + {"PewterCity", {4, 4}, {"PewterCity", {17, 26}}}, + {"Route4", {8, 3}, {"Route4", {12, 6}}}, + {"CeruleanCity", {14, 3}, {"CeruleanCity", {22, 20}}}, + {"VermilionCity", {14, 9}, {"VermilionCity", {15, 7}}}, + {"Route10", {18, 3}, {"Route10", {13, 21}}}, + {"LavenderTown", {18, 6}, {"LavenderTown", {6, 6}}}, + {"CeladonCity", {11, 6}, {"CeladonCity", {48, 12}}}, + {"SaffronCity", {14, 6}, {"SaffronCity", {24, 39}}}, + {"FuchsiaCity", {12, 12}, {"FuchsiaCity", {25, 32}}}, + {"CinnabarIsland", {4, 14}, {"CinnabarIsland", {14, 12}}}, + {"IndigoPlateau", {2, 3}, {"IndigoPlateau_Exterior", {11, 7}}}, + }; + return TOWNS; +} + +std::optional fly_town_index(std::string_view name){ + const std::vector& towns = kanto_fly_towns(); + for (size_t c = 0; c < towns.size(); c++){ + if (towns[c].name == name){ + return c; + } + } + return std::nullopt; +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_FlyMap.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_FlyMap.h new file mode 100644 index 0000000000..3748436f1e --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_FlyMap.h @@ -0,0 +1,57 @@ +/* Fly Map + * + * From: https://github.com/PokemonAutomation/ + * + * The towns Fly takes the player to in Kanto: each one's cell on the fly + * map and the tile the player lands on. Cells are pokefirered's + * sRegionMapSections_Kanto (src/data/region_map/region_map_layout_kanto.h, + * 22 x 15, the cursor's x and y); landing tiles its heal locations + * (src/data/heal_locations.json), which Fly warps to (SetFlyWarpDestination() + * in src/region_map.c). + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_FlyMap_H +#define PokemonAutomation_PokemonFRLG_FlyMap_H + +#include +#include +#include +#include +#include +#include "PokemonFRLG_OverworldRouteSteps.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// A place on the fly map Fly can take the player to. +struct FlyTown{ + // The map's name, as a step names the town ("CeladonCity", "Route4"). + std::string name; + // The town's cell on the fly map: the presses right and down from its + // top-left corner, where a hold up-left leaves the cursor + // (HandleRegionMapInput() stops it at 0). + TilePos cell; + // The tile Fly lands the player on: below the Pokemon Center's door, + // or Pallet Town's player's house. + MapPosition landing; +}; + +// Kanto's Fly destinations, Pallet Town first. +const std::vector& kanto_fly_towns(); +// The index of the town named `name` in kanto_fly_towns(), if it is one. +std::optional fly_town_index(std::string_view name); + +// The fly map's width in cells (MAP_WIDTH in region_map.c). +constexpr int FLY_MAP_COLUMNS = 22; +// The fly map's height in cells (MAP_HEIGHT in region_map.c). +constexpr int FLY_MAP_ROWS = 15; + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp index 880e331e4b..017373fef8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp @@ -168,6 +168,7 @@ void OverworldSim::update_view(){ m_state.black_below = map().connections.empty() && map().tiles.height <= SMALL_ROOM_ROWS && m_player.position.y >= map().tiles.height - BLACK_BELOW_ROWS; m_state.dark = m_script.dark_maps.contains(map().name); + m_state.surfing = m_player.surfing; } void OverworldSim::schedule(Milliseconds at, SimScreen screen, bool unfreeze){ @@ -286,7 +287,7 @@ std::optional OverworldSim::cross_edge(Direction direction) case Direction::EAST: point.x = 0; break; } const StepKind kind = classify_step_onto(map(), m_player, direction, to, point); - if (kind != StepKind::WALK){ + if (kind != StepKind::WALK && kind != StepKind::STOP_SURFING){ return std::nullopt; } return Step{destination, after_step_onto(map(), m_player, to, point, kind), false, false, false}; @@ -313,7 +314,7 @@ std::optional OverworldSim::press(Direction direction) const return Step{m_map, PlayerState{landed, elevation_at(map(), landed), false}, false, false, true}; } const StepKind kind = classify_step(map(), m_player, direction); - if (kind != StepKind::WALK){ + if (kind != StepKind::WALK && kind != StepKind::STOP_SURFING){ return std::nullopt; } Step step{m_map, after_step_onto(map(), m_player, map(), ahead, kind), false, false, false}; @@ -369,10 +370,7 @@ bool OverworldSim::after_step(const Step& step, Milliseconds pressed){ update_view(); if (step.warped){ m_state.warps++; - for (auto& [index, tree] : m_cut_trees){ - m_maps[index].objects.push_back(tree); - } - m_cut_trees.clear(); + regrow_cut_trees(); if (m_state.on_bike){ // A door takes the player off (the user's; not measured). m_state.on_bike = false; @@ -841,8 +839,8 @@ std::optional OverworldSimWalker::walk_step(const RouteStep& step, sim_turn_to(m_sim, step.direction, m_rules.confirm_turn); return std::nullopt; } - if (step.action != RouteAction::WALK){ - fail("Sim: Surf isn't modelled."); + if (step.action == RouteAction::SURF){ + return surf(step); } if (turns_first(step, m_speed, facing)){ sim_turn_to(m_sim, step.direction, m_rules.confirm_turn); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h index da4d5b84f2..e8d9f62a33 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h @@ -20,7 +20,8 @@ * A step onto a connected map waits a tile first, as the console does * while the game loads the map. * - * Not modelled: Surf, running, HP and status. Wandering people stand on + * Not modelled: running, HP and status, and wild battles on the water. + * Surf and Fly are (SimField::SURF, SimField::FLY). Wandering people stand on * their tile. The Bicycle is: its pace, its taps (each a tile, some * dropped when they come too fast), SELECT and doors taking the player * off it. @@ -164,6 +165,10 @@ class OverworldSim{ bool cut_tree(); bool choose_lift_floor(size_t floor); bool push_poster_switch(); + bool start_surf(); + bool fly_to_town(size_t town); + // The trees cut come back, as after a warp. + void regrow_cut_trees(); bool after_step(const Step& step, Milliseconds pressed); bool wild_battle(Milliseconds pressed); std::optional next_encounter(); @@ -173,8 +178,11 @@ class OverworldSim{ // Whether the Bicycle tap starting at `start` is dropped, too soon // after the last one. bool drops_bike_tap(Milliseconds start); - // How long a tile takes: on the Bicycle or on foot. + // How long a tile takes: on the water, on the Bicycle or on foot. Milliseconds tile_time() const{ + if (m_player.surfing){ + return m_script.timing.surf_tile; + } return m_state.on_bike ? m_script.timing.bike_tile : m_script.timing.tile; } bool trainer_sees(Milliseconds pressed); @@ -397,6 +405,10 @@ class OverworldSimWalker{ return stop_tiles(step, timed, m_leg_dark); } std::optional walk_step(const RouteStep& step, std::optional facing); + // start_surf(): turned to the water, A, YES, and the jump onto it. + // Nothing: a Surf is never stopped part-way. + // (PokemonFRLG_OverworldSimField.cpp) + std::optional surf(const RouteStep& step); bool after_warp(const RouteStep& step, int warps_before, Milliseconds watched_from); bool fade_over_since(Milliseconds from) const; // after_warp() on a fade seen with no warp: fails, or with diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimField.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimField.cpp index 05dfa2e40f..4b22e93852 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimField.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimField.cpp @@ -3,13 +3,15 @@ * From: https://github.com/PokemonAutomation/ * * What A does on the sim's overworld, facing something: a tree to cut, - * the Game Corner's poster, the lift's panel (OverworldSim::use_field()). + * the Game Corner's poster, the lift's panel, the water to Surf onto; and + * Fly (OverworldSim::use_field()). * */ #include #include #include "PokemonFRLG_CodedRooms.h" +#include "PokemonFRLG_FlyMap.h" #include "PokemonFRLG_OverworldSim.h" #include "PokemonFRLG_OverworldSimField.h" #include "PokemonFRLG_BikeSpinMechanics.h" @@ -38,6 +40,10 @@ bool OverworldSim::use_field(SimField field, size_t floor){ return choose_lift_floor(floor); case SimField::BIKE_SELECT: return press_select(); + case SimField::SURF: + return start_surf(); + case SimField::FLY: + return fly_to_town(floor); } return false; } @@ -117,6 +123,62 @@ bool OverworldSim::choose_lift_floor(size_t floor){ return true; } +bool OverworldSim::start_surf(){ + const bool water = classify_step(map(), m_player, m_state.facing) == StepKind::START_SURF; + if (!water || m_state.screen != SimScreen::OVERWORLD || m_frozen){ + violation("no water to Surf onto " + std::string(direction_name(m_state.facing)) + " of " + to_string(m_state.position) + "."); + return false; + } + advance_to(m_now + m_script.timing.surf_start); + m_player = PokemonFRLG::after_step(map(), m_player, m_state.facing, StepKind::START_SURF); + m_state.on_bike = false; + update_view(); + note("surfed onto " + to_string(m_state.position) + "."); + return true; +} + +bool OverworldSim::fly_to_town(size_t town){ + const std::vector& towns = kanto_fly_towns(); + const MapPosition* landing = town < towns.size() ? &towns[town].landing : nullptr; + const auto to = std::find_if(m_maps.begin(), m_maps.end(), [&](const MapData& map){ + return landing != nullptr && map.name == landing->map; + }); + if (to == m_maps.end() || m_state.screen != SimScreen::OVERWORLD || m_frozen){ + violation(std::format("no Fly to town {} from {}.", town, to_string(m_state.position))); + return false; + } + m_state.warped_from.push_back(map().name); + m_state.warps++; + regrow_cut_trees(); + m_map = (int)(to - m_maps.begin()); + m_player = PlayerState{landing->point, elevation_at(*to, landing->point), false}; + m_state.facing = Direction::SOUTH; + m_state.on_bike = false; + update_view(); + m_frozen = true; + schedule(m_now + m_script.timing.message, SimScreen::FADE, false); + schedule(m_now + m_script.timing.fly, SimScreen::OVERWORLD, true); + note("flew to " + to_string(m_state.position) + "."); + return true; +} + +std::optional OverworldSimWalker::surf(const RouteStep& step){ + if (m_sim.state().facing != step.direction){ + sim_turn_to(m_sim, step.direction, m_rules.confirm_turn); + } + if (!m_sim.use_field(SimField::SURF)){ + fail("walk_overworld_route(): no Surf prompt facing the water."); + } + return std::nullopt; +} + +void OverworldSim::regrow_cut_trees(){ + for (auto& [index, tree] : m_cut_trees){ + m_maps[index].objects.push_back(tree); + } + m_cut_trees.clear(); +} + bool OverworldSim::push_poster_switch(){ std::vector& objects = m_maps[m_map].objects; const TilePos ahead = m_player.position.next(m_state.facing); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimField.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimField.h index 4dd405a0f6..b4993fa1e8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimField.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimField.h @@ -36,6 +36,15 @@ enum class SimField{ // running is allowed, or off it, in about // MovementMechanics::BIKE_SELECT_TIME. (PokemonFRLG_OverworldSimTaps.cpp) BIKE_SELECT, + // Surf onto the water the player faces, from ground beside it: the + // question, " used SURF!" and the jump (SimTiming::surf_start). + // Each step on the water then takes SimTiming::surf_tile, and a step + // onto land gets the player off. + SURF, + // Not A: Fly from the party menu to the town numbered `floor` in + // kanto_fly_towns(), outdoors: the fade, then the player on its landing + // tile facing south, still for SimTiming::fly. + FLY, }; // Register the tests of the Bicycle's taps and SELECT on a made-up map. diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimScreens.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimScreens.h index b2407335b8..5f0be8df11 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimScreens.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimScreens.h @@ -62,6 +62,15 @@ struct SimTiming{ Milliseconds tile = MovementMechanics::TILE_TIME; // A tile on the Bicycle. Milliseconds bike_tile = MovementMechanics::BIKE_TILE_TIME; + // A tile on the water. + Milliseconds surf_tile = MovementMechanics::SURF_TILE_TIME; + // From the A facing the water to the player on it, able to move + // (OverworldWalkSteps::start_surf()'s presses and wait). Not measured + // on the console. + Milliseconds surf_start{4000}; + // From Fly's town picked to the player standing in it, able to move: + // the fade, the flight and the landing. Not measured on the console. + Milliseconds fly{5000}; Milliseconds flash{300}; Milliseconds battle_screen{3700}; // A wild battle's text box, before the HP boxes (battle_screen). Not diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimState.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimState.h index 36d32a60ad..b32f169d85 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimState.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimState.h @@ -133,6 +133,8 @@ struct SimState{ int turn_taps_dropped = 0; // On the Bicycle (SimScript::on_bike). bool on_bike = false; + // On the water (SimField::SURF). + bool surfing = false; // Bicycle taps the game dropped, too soon after the last one. int bike_taps_dropped = 0; // Step taps the game ignored, inside the lockout after the last one diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoveSteps.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoveSteps.cpp new file mode 100644 index 0000000000..4f62918727 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoveSteps.cpp @@ -0,0 +1,50 @@ +/* AutoStory Field Move Steps + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_FlyMap.h" +#include "PokemonFRLG_AutoStoryFieldMoves.h" +#include "PokemonFRLG_AutoStoryFieldMoveSteps.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +using Pokemon::Direction; +using Pokemon::StoryStep; + + +StoryStep fly_step(std::string name, size_t slot, std::string_view town){ + StoryStep step; + step.kind = Pokemon::StoryStepKind::GAME; + step.name = std::move(name); + step.action = FLY_VARIANT; + step.args = {{"town", std::string(town)}, {"slot", (int64_t)slot}}; + if (const std::optional index = fly_town_index(town)){ + const MapPosition& landing = kanto_fly_towns()[*index].landing; + step.pose_after = Pokemon::TileDeclared{landing.map, {landing.point}, Direction::SOUTH}; + } + return step; +} + +StoryStep surf_step(std::string name, Pokemon::GridPose water){ + StoryStep step; + step.kind = Pokemon::StoryStepKind::GAME; + step.name = std::move(name); + step.action = SURF_VARIANT; + if (water.facing){ + step.args = {{"direction", *water.facing}}; + } + step.pose_after = Pokemon::TileDeclared{std::move(water.map), std::move(water.tiles), water.facing}; + return step; +} + + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoveSteps.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoveSteps.h new file mode 100644 index 0000000000..31440a57a0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoveSteps.h @@ -0,0 +1,39 @@ +/* AutoStory Field Move Steps + * + * From: https://github.com/PokemonAutomation/ + * + * The field move GAME steps (PokemonFRLG_AutoStoryFieldMoves.h) as a + * checkpoint's JSON gives them, for tests. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStoryFieldMoveSteps_H +#define PokemonAutomation_PokemonFRLG_AutoStoryFieldMoveSteps_H + +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// A fly step to `town` with the Fly user in `slot` (from 1), ending on its +// landing tile facing south. +Pokemon::StoryStep fly_step(std::string name, size_t slot, std::string_view town); +// A surf step onto `water`'s tile, `water.facing` the way to it. +Pokemon::StoryStep surf_step(std::string name, Pokemon::GridPose water); + + +// Registers the field moves' tests. +void add_tests_AutoStoryFieldMoves(UnitTestDatabase& database); + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoves.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoves.cpp new file mode 100644 index 0000000000..f9ea5c4df6 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoves.cpp @@ -0,0 +1,194 @@ +/* AutoStory Field Moves + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "Pokemon/AutoStory/Pokemon_StoryInterrupts.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h" +#include "VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h" +#include "VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_PartyDialogs.h" +#include "VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_FlyMap.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_MovementMechanics.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h" +#include "PokemonFRLG_AutoStoryFieldMoves.h" +#include "PokemonFRLG_AutoStoryGameArgs.h" +#include "PokemonFRLG_AutoStoryQuietOverworld.h" +#include "PokemonFRLG_AutoStoryStepInterrupts.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +using namespace std::chrono_literals; +using Pokemon::Direction; +using Pokemon::StoryStep; + + +namespace{ + +// The party has six slots; a Pokemon's menu at most SUMMARY, four field +// moves, SWITCH, ITEM and CANCEL. +constexpr size_t PARTY_SLOTS = 6; +constexpr size_t MON_MENU_ROWS = 8; + +} // namespace + +std::optional parse_fly_step(const StoryStep& step){ + if (step.action != FLY_VARIANT){ + return std::nullopt; + } + const std::string* town = game_arg(step.args, "town"); + const std::optional index = town == nullptr ? std::nullopt : fly_town_index(*town); + const std::optional slot = counted_arg(step.args, "slot", PARTY_SLOTS); + const std::optional menu_row = step.args.contains("menu_row") + ? counted_arg(step.args, "menu_row", MON_MENU_ROWS) + : std::optional(FlyPlan{}.menu_row); + if (!index || !slot || !menu_row){ + return std::nullopt; + } + return FlyPlan{*slot, *menu_row, *index}; +} + +std::optional parse_surf_step(const StoryStep& step){ + const Direction* direction = step.action == SURF_VARIANT ? game_arg(step.args, "direction") : nullptr; + if (direction == nullptr || step.args.size() != 1){ + return std::nullopt; + } + return *direction; +} + +bool is_field_move_step(const StoryStep& step){ + return parse_fly_step(step).has_value() || parse_surf_step(step).has_value(); +} + +std::vector field_move_specs(){ + using Pokemon::StoryArgType; + using Pokemon::StoryStepKind; + return { + {.kind = StoryStepKind::GAME, .name = FLY_VARIANT, .args = { + required_arg("town", StoryArgType::WORD, "the town's map, as kanto_fly_towns() names it (CeladonCity, Route4)"), + required_arg("slot", StoryArgType::INTEGER, "the Fly user's party slot, from 1"), + optional_arg("menu_row", StoryArgType::INTEGER, int64_t(2), "FLY's row on its menu, from 1: SUMMARY, then field moves in move order"), + }}, + {.kind = StoryStepKind::GAME, .name = SURF_VARIANT, .args = { + required_arg("direction", StoryArgType::DIRECTION, "the way to the water, from ground beside it"), + }}, + }; +} + + +namespace{ + +// Not measured on the console: the party menu's and the fly map's cursor +// taps, as the lift panel's (PokemonFRLG_AutoStoryHideout.cpp). A fly map +// cursor step takes 4 frames (HandleRegionMapInput(): 2 pixels a frame, +// 8 a cell); the release leaves it time to stop. +constexpr Milliseconds MENU_TAP = 80ms; +constexpr Milliseconds MENU_TAP_RELEASE = 240ms; +constexpr Milliseconds CURSOR_TAP = 80ms; +constexpr Milliseconds CURSOR_TAP_RELEASE = 170ms; +// Up-left to the map's corner: 21 cells at 4 frames each is 1.4 s. +constexpr Milliseconds CURSOR_TO_CORNER = 3000ms; +// The Pokemon's menu, after its A. +constexpr Milliseconds MON_MENU_TIMEOUT = 5000ms; +// The fly map, after FLY's A, and before its cursor takes the D-pad +// (open_fly_map_from_overworld()'s wait). +constexpr Milliseconds FLY_MAP_TIMEOUT = 20000ms; +constexpr Milliseconds FLY_MAP_SETTLE = 1000ms; +// The fade out after the town's A: none, the town wasn't one Fly goes to. +constexpr Milliseconds TAKEOFF_TIMEOUT = 10000ms; +// The flight and the landing, to the overworld at rest. +constexpr Milliseconds LANDING_TIMEOUT = 30000ms; + +void tap_dpad(ProControllerContext& context, DpadPosition dpad, size_t count, Milliseconds tap, Milliseconds release){ + for (size_t c = 0; c < count; c++){ + pbf_press_dpad(context, dpad, tap, release); + } +} + +// From the overworld to the fly map: the party, the Fly user, FLY. +// Returns the failure, or empty. +std::string open_fly_map(ConsoleHandle& console, ProControllerContext& context, const FlyPlan& plan){ + open_party_menu_from_overworld(console, context); + tap_dpad(context, DPAD_DOWN, plan.slot, MENU_TAP, MENU_TAP_RELEASE); + PartySelectionWatcher mon_menu(COLOR_RED); + context.wait_for_all_requests(); + pbf_press_button(context, BUTTON_A, 160ms, 0ms); + if (wait_until(console, context, MON_MENU_TIMEOUT, {mon_menu}) < 0){ + return "fly_no_menu"; + } + tap_dpad(context, DPAD_DOWN, plan.menu_row, MENU_TAP, MENU_TAP_RELEASE); + KantoMapWatcher fly_map(COLOR_RED); + context.wait_for_all_requests(); + pbf_press_button(context, BUTTON_A, 160ms, 0ms); + if (wait_until(console, context, FLY_MAP_TIMEOUT, {fly_map}) < 0){ + return "fly_no_map"; + } + pbf_wait(context, FLY_MAP_SETTLE); + return ""; +} + +// On the fly map: the cursor to the corner, then to the town, and A. +// Returns the failure, or empty once the overworld is at rest after the +// landing. +std::string fly_to_town(ConsoleHandle& console, ProControllerContext& context, const FlyTown& town){ + pbf_press_dpad(context, DPAD_UP_LEFT, CURSOR_TO_CORNER, CURSOR_TAP_RELEASE); + tap_dpad(context, DPAD_RIGHT, (size_t)town.cell.x, CURSOR_TAP, CURSOR_TAP_RELEASE); + tap_dpad(context, DPAD_DOWN, (size_t)town.cell.y, CURSOR_TAP, CURSOR_TAP_RELEASE); + BlackScreenWatcher takeoff(COLOR_RED); + context.wait_for_all_requests(); + pbf_press_button(context, BUTTON_A, 160ms, 0ms); + if (wait_until(console, context, TAKEOFF_TIMEOUT, {takeoff}) < 0){ + return "fly_no_takeoff"; + } + context.wait_for_all_requests(); + ConsoleScreenLook eyes(console, context); + const Pokemon::ScreenSettle settle = wait_for_quiet_overworld( + [&eyes](bool fresh){ return eyes.look(fresh); }, + MovementMechanics::AFTER_BATTLE_QUIET, LANDING_TIMEOUT + ); + console.log(std::format("Story: landed in {}, {}", town.name, Pokemon::describe(settle))); + return quiet_overworld(settle, MovementMechanics::AFTER_BATTLE_QUIET) ? "" : "fly_no_landing"; +} + +} // namespace + +std::optional run_field_move_step( + ConsoleHandle& console, ProControllerContext& context, const StoryStep& step +){ + const auto done = [&step]{ + return step.pose_after ? Pokemon::outcome_ok(Pokemon::pose_of(*step.pose_after)) : Pokemon::outcome_ok(); + }; + if (const std::optional water = parse_surf_step(step)){ + console.log(std::format("Story: Surf {}.", Pokemon::direction_name(*water))); + OverworldWalkSteps::start_surf(console, context, *water, false); + return done(); + } + const std::optional plan = parse_fly_step(step); + if (!plan){ + return std::nullopt; + } + const FlyTown& town = kanto_fly_towns()[plan->town]; + console.log(std::format( + "Story: Fly with slot {}, menu row {}, to {} (cell {}, {}).", + plan->slot + 1, plan->menu_row + 1, town.name, town.cell.x, town.cell.y + )); + std::string failure = open_fly_map(console, context, *plan); + if (failure.empty()){ + failure = fly_to_town(console, context, town); + } + return failure.empty() ? done() : Pokemon::outcome_failed(failure, town.name); +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoves.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoves.h new file mode 100644 index 0000000000..e04e257b1d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoves.h @@ -0,0 +1,71 @@ +/* AutoStory Field Moves + * + * From: https://github.com/PokemonAutomation/ + * + * The HMs the story uses away from what the player faces: Fly to a town, + * and Surf onto the water. Both are GAME steps named by their variant. + * The legs and routes after a Surf start on the water and are walked + * surfing (fixed_route(), plan_overworld_route()), until a step onto + * land gets the player off. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStoryFieldMoves_H +#define PokemonAutomation_PokemonFRLG_AutoStoryFieldMoves_H + +#include +#include +#include +#include +#include +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "Pokemon/AutoStory/Pokemon_StoryEvents.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The GAME step that flies to a town, outdoors: START, POKEMON, the Fly +// user (`slot`, from 1), FLY on its menu (`menu_row`, from 1: SUMMARY, +// then its field moves in move order, so 2 when Fly is its only one; +// pokefirered SetPartyMonFieldSelectionActions()), and `town` on the fly +// map (kanto_fly_towns()). It ends on the town's landing tile. +inline const std::string FLY_VARIANT = "fly"; +// The GAME step that surfs onto the water `direction` of the player, on +// ground beside it: A facing it, YES to "Would you like to SURF?", and +// the jump onto it. It ends on that water tile. +inline const std::string SURF_VARIANT = "surf"; + +// A fly step's choices, counted from 0. +struct FlyPlan{ + size_t slot = 0; + size_t menu_row = 1; + // In kanto_fly_towns(). + size_t town = 0; +}; + +// The fly step's choices, or nothing if `step` isn't one or names no town +// Fly goes to. +std::optional parse_fly_step(const Pokemon::StoryStep& step); +// The way a surf step faces the water, or nothing if it isn't one. +std::optional parse_surf_step(const Pokemon::StoryStep& step); +// Whether `step` is one of this file's steps. +bool is_field_move_step(const Pokemon::StoryStep& step); +// This file's GAME actions and their args (field_action_specs()). +std::vector field_move_specs(); + +// On the console, a step of this file's; nothing for another step. The +// step ends on its pose_after, if it has one. +std::optional run_field_move_step( + ConsoleHandle& console, ProControllerContext& context, const Pokemon::StoryStep& step +); + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoves_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoves_Tests.cpp new file mode 100644 index 0000000000..cae15e22a7 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoves_Tests.cpp @@ -0,0 +1,290 @@ +/* AutoStory Field Moves Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_FlyMap.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldTileRules.h" +#include "PokemonFRLG_AutoStoryEvents.h" +#include "PokemonFRLG_AutoStoryFieldMoves.h" +#include "PokemonFRLG_AutoStoryFieldMoveSteps.h" +#include "PokemonFRLG_AutoStoryHideout.h" +#include "PokemonFRLG_AutoStoryLegs.h" +#include "PokemonFRLG_AutoStoryPacedRoute.h" +#include "PokemonFRLG_AutoStoryPoses.h" +#include "PokemonFRLG_AutoStoryRowSim.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +using Pokemon::StoryLeg; +using Pokemon::StoryStep; + +struct FieldMovesInput{ + std::string name; + UnitTestResult (*body)(); +}; +struct FieldMovesExpectation{ + std::string_view description; +}; +struct FieldMovesCase{ + FieldMovesInput input; + FieldMovesExpectation expectation; +}; + +class Test_AutoStoryFieldMoves : public UnitTest{ +public: + explicit Test_AutoStoryFieldMoves(FieldMovesCase test_case) + : UnitTest("PokemonFRLG::AutoStoryFieldMoves - " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + try{ + return m_case.input.body(); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + FieldMovesCase m_case; +}; + +bool built_in(const std::string& map){ + const std::vector>& maps = overworld_map_json(); + return std::any_of(maps.begin(), maps.end(), [&](const auto& entry){ return entry.first == map; }); +} + +// Each town on its own cell of the fly map; on the maps built in, its +// landing tile below a door (a Center's, or the player's house). +UnitTestResult test_fly_map(){ + std::set> cells; + std::set names; + for (const FlyTown& town : kanto_fly_towns()){ + if (town.cell.x < 0 || town.cell.x >= FLY_MAP_COLUMNS || town.cell.y < 0 || town.cell.y >= FLY_MAP_ROWS + || !cells.emplace(town.cell.x, town.cell.y).second || !names.insert(town.name).second + ){ + return town.name + ": its cell is off the fly map or another town's, or its name is taken."; + } + if (!built_in(town.landing.map)){ + continue; + } + const MapData map = load_map(town.landing.map); + const TilePos door = town.landing.point.next(Direction::NORTH); + if (is_wall(map, town.landing.point) || !is_door(map, door)){ + return std::format("{}: no door north of the landing tile {}.", town.name, to_string(town.landing)); + } + } + if (kanto_fly_towns().size() != 13 || fly_town_index("CeladonCity") != 8 || fly_town_index("Celadon")){ + return "Expected Kanto's 13 Fly destinations, Celadon City 9th."; + } + return true; +} + +// The steps' args: a fly step needs a town Fly goes to and a party slot; +// its menu row is SUMMARY's next unless it says. A surf step needs only +// its way to the water. +UnitTestResult test_args(){ + StoryStep fly = fly_step("fly_celadon", 2, "CeladonCity"); + std::optional plan = parse_fly_step(fly); + if (!plan || plan->slot != 1 || plan->menu_row != 1 || plan->town != 8){ + return "Expected slot 2, menu row 2, Celadon City."; + } + const Pokemon::DeclaredPose landed = Pokemon::TileDeclared{"CeladonCity", {{48, 12}}, Direction::SOUTH}; + if (!(fly.pose_after == landed)){ + return "Expected the fly step to end below Celadon's Center door, (48, 12)."; + } + fly.args["menu_row"] = int64_t(3); + plan = parse_fly_step(fly); + if (!plan || plan->menu_row != 2){ + return "Expected menu row 3."; + } + fly.args["slot"] = int64_t(7); + if (parse_fly_step(fly) || parse_fly_step(fly_step("fly", 1, "SeafoamIslands"))){ + return "Expected no plan for slot 7 or a town Fly doesn't go to."; + } + StoryStep surf = surf_step("surf", Pokemon::GridPose{"PalletTown", {{7, 17}}, Direction::SOUTH}); + if (parse_surf_step(surf) != Direction::SOUTH || !is_field_move_step(surf)){ + return "Expected a surf step south."; + } + surf.args["town"] = std::string("PalletTown"); + if (parse_surf_step(surf) || is_field_move_step(surf)){ + return "Expected no surf step with an arg it doesn't take."; + } + const std::vector specs = field_action_specs(); + const auto has = [&](const std::string& name){ + return std::any_of(specs.begin(), specs.end(), [&](const Pokemon::StoryActionSpec& spec){ return spec.name == name; }); + }; + if (!has(FLY_VARIANT) || !has(SURF_VARIANT)){ + return "Expected fly and surf among the field actions."; + } + return true; +} + +// Fly from Lavender Town to Celadon City on the sim, as a row runs it: +// the player stands on Celadon's landing tile, off the Bicycle. +UnitTestResult test_fly_on_sim(){ + const MapPosition lavender{"LavenderTown", {6, 6}}; + SimScript script; + script.on_bike = true; + OverworldSim sim(story_maps(), lavender, Direction::SOUTH, script); + StoryPose at{lavender.map, lavender.point, Direction::SOUTH}; + const std::string failure = row_step_on_sim(sim, at, fly_step("fly_celadon", 1, "CeladonCity"), "11.01"); + if (!failure.empty()){ + return failure; + } + const MapPosition celadon{"CeladonCity", {48, 12}}; + if (!(sim.state().position == celadon) || !(at.map == celadon.map && at.tile == celadon.point) || sim.state().on_bike){ + return "After Fly, on " + to_string(sim.state().position) + ", not on foot on Celadon's (48, 12)."; + } + if (!sim.state().violations.empty()){ + return "Fly broke the game's rules: " + sim.state().violations.front(); + } + return true; +} + +StoryStep legs_step(std::string name, std::vector legs){ + StoryStep step; + step.kind = Pokemon::StoryStepKind::LEGS; + step.name = std::move(name); + step.args = {{"legs", std::move(legs)}}; + return step; +} + +// Viridian City's pond, x 11 to 16 and y 26 to 30, ground west and east +// of it. (Pallet Town's is in its Fat Man's wander box, so no leg crosses +// it.) +const MapPosition POND_WEST_SHORE{"ViridianCity", {10, 27}}; +const MapPosition POND_EAST_SHORE{"ViridianCity", {17, 27}}; + +// Legs from the water: surfing, until a one-tile leg onto land. Legs +// onto the water from land, or off it with more tiles after, aren't +// legs. +UnitTestResult test_legs_from_water(){ + const std::vector& maps = story_maps(); + const MapPosition pond{"ViridianCity", {11, 27}}; + const std::optional> route = fixed_route(maps, pond, {{Direction::EAST, 5}, {Direction::EAST, 1}}); + if (!route || route->size() != 2 || !(*route)[0].surfing || !(*route)[0].wild || !(*route)[1].surfing + || !((*route)[1].end == POND_EAST_SHORE) + ){ + return "Expected east 5 surfing, then east 1 onto Viridian City's (17, 27)."; + } + if (fixed_route(maps, MapPosition{"ViridianCity", {16, 27}}, {{Direction::EAST, 2}}) + || fixed_route(maps, POND_WEST_SHORE, {{Direction::EAST, 1}}) + ){ + return "Expected no legs off the water with a tile after, or onto it without Surf."; + } + return true; +} + +// Viridian City's pond on the sim, as a row runs it: Surf east from its +// west shore, the legs across it surfing, and off it onto the east shore. +UnitTestResult test_surf_legs_on_sim(){ + const MapPosition shore = POND_WEST_SHORE; + OverworldSim sim(story_maps(), shore, Direction::NORTH); + StoryPose at{shore.map, shore.point, Direction::NORTH}; + std::string failure = row_step_on_sim(sim, at, surf_step("surf", Pokemon::GridPose{"ViridianCity", {{11, 27}}, Direction::EAST}), "13.01"); + if (!failure.empty() || !sim.state().surfing){ + return "Surf: " + (failure.empty() ? std::string("not on the water.") : failure); + } + failure = row_step_on_sim(sim, at, legs_step("across", {{Direction::EAST, 5}, {Direction::EAST, 1}}), "13.01"); + if (!failure.empty()){ + return failure; + } + if (!(sim.state().position == POND_EAST_SHORE) || sim.state().surfing){ + return "After the legs, on " + to_string(sim.state().position) + ", not on land at (17, 27)."; + } + if (!sim.state().violations.empty()){ + return "Surf broke the game's rules: " + sim.state().violations.front(); + } + return true; +} + +// Routes planned on and onto the water, walked by the sim's walker: from +// the water the story's routes surf to land (Surf itself is off for +// them: RouteCost::SURF); with Surf on, a route surfs onto the pond. +UnitTestResult test_routes_on_water(){ + const std::vector& maps = story_maps(); + OverworldSim sim(maps, POND_WEST_SHORE, Direction::EAST); + if (!sim.use_field(SimField::SURF)){ + return "No Surf east of Viridian City's (10, 27)."; + } + const MapPosition water = sim.state().position; + const MapPosition land{"ViridianCity", {20, 28}}; + const std::optional> off = plan_overworld_route(maps, water, RouteGoal{land, std::nullopt}, story_route_costs()); + if (!off){ + return "No story route off the pond to (20, 28)."; + } + std::string failure = OverworldSimWalker(sim).run(*off, RoutePlanning{.maps = &maps, .costs = story_route_costs(), .start = water, .label = "Sim"}); + if (!failure.empty() || !(sim.state().position == land) || sim.state().surfing){ + return "Off the pond, on " + to_string(sim.state().position) + ": " + failure; + } + const MapPosition pond{"ViridianCity", {13, 28}}; + const std::optional> onto = plan_overworld_route(maps, land, RouteGoal{pond, std::nullopt}); + const bool surfs = onto && std::any_of(onto->begin(), onto->end(), [](const RouteStep& step){ return step.action == RouteAction::SURF; }); + if (!surfs){ + return "Expected a route that surfs onto the pond."; + } + failure = OverworldSimWalker(sim).run(*onto, RoutePlanning{.maps = &maps, .start = land, .label = "Sim"}); + if (!failure.empty() || !(sim.state().position == pond) || !sim.state().surfing){ + return "Onto the pond, on " + to_string(sim.state().position) + ": " + failure; + } + return true; +} + +const FieldMovesCase CASES[] = { + { + .input = {.name = "fly map", .body = test_fly_map}, + .expectation = {.description = "each town on its own cell, landing below a door"}, + }, + { + .input = {.name = "step args", .body = test_args}, + .expectation = {.description = "fly and surf steps read their args, the specs list them"}, + }, + { + .input = {.name = "fly on the sim", .body = test_fly_on_sim}, + .expectation = {.description = "Lavender Town to Celadon City's landing tile"}, + }, + { + .input = {.name = "legs from the water", .body = test_legs_from_water}, + .expectation = {.description = "surfing legs, and a one-tile leg off the water"}, + }, + { + .input = {.name = "surf and legs on the sim", .body = test_surf_legs_on_sim}, + .expectation = {.description = "Viridian City's pond: Surf, across, and off"}, + }, + { + .input = {.name = "routes on the water on the sim", .body = test_routes_on_water}, + .expectation = {.description = "planned routes off and onto the pond, walked"}, + }, +}; + +} // namespace + + +void add_tests_AutoStoryFieldMoves(UnitTestDatabase& database){ + for (const FieldMovesCase& test_case : CASES){ + database.add(test_case); + } +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp index 9046387f0f..5cee9a7e65 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp @@ -11,6 +11,7 @@ #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldBike.h" #include "PokemonFRLG_AutoStoryActions.h" +#include "PokemonFRLG_AutoStoryFieldMoves.h" #include "PokemonFRLG_AutoStoryHideout.h" #include "PokemonFRLG_AutoStoryHideoutLift.h" #include "PokemonFRLG_AutoStoryItemUse.h" @@ -101,6 +102,9 @@ std::vector field_action_specs(){ for (const std::string& center : {CENTER_ENTER_VARIANT, CENTER_EXIT_VARIANT}){ specs.push_back({.kind = StoryStepKind::GAME, .name = center, .args = {}}); } + for (Pokemon::StoryActionSpec& move : field_move_specs()){ + specs.push_back(std::move(move)); + } return specs; } @@ -165,6 +169,9 @@ std::optional run_field_game_step( ride_lift(console, context, *ride, player); return done(); } + if (std::optional moved = run_field_move_step(console, context, step)){ + return moved; + } if (is_center_variant(step.action)){ // Both start facing north: at the door, or at the nurse. walk_room_legs(console, context, center_legs(step.action), Pokemon::Direction::NORTH, player); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.h index e6a2cde118..0b45c27e55 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.h @@ -59,7 +59,8 @@ std::vector field_action_specs(); void push_poster_switch(ConsoleHandle& console, ProControllerContext& context); // On the console, a GAME step of this file's, a Pokemon Center's -// (is_center_variant()) or an item from the bag (is_item_use_step()); +// (is_center_variant()), a field move (is_field_move_step()) or an item +// from the bag (is_item_use_step()); // nothing for another GAME step. The step ends on its pose_after, if it // has one (an Escape Rope's, a lift ride's, a Center's). std::optional run_field_game_step( diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLegs.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLegs.cpp index ee0e282b4c..139fbdcc61 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLegs.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLegs.cpp @@ -46,20 +46,25 @@ std::optional> fixed_route( return std::nullopt; } std::vector route; - PlayerState player{start.point, elevation_at(*map, start.point), false}; + // On the water, after a Surf: surfing until a step onto land. + PlayerState player{start.point, elevation_at(*map, start.point), is_water(*map, start.point)}; for (const StoryLeg& leg : legs){ RouteStep step; step.direction = leg.direction; step.tiles = leg.tiles; step.facing_after = leg.direction; step.walk_only = !map->allow_running; + step.surfing = player.surfing; for (int c = 0; c < leg.tiles; c++){ const StepKind kind = classify_step(*map, player, leg.direction); const TilePos next = player.position.next(leg.direction); - if (kind != StepKind::WALK || !in_bounds(*map, next) || warps_on_step(*map, next)){ + // The step off the water is a leg of its own, as the planner + // makes it: the player walks at half the speed from there. + const bool lands = kind == StepKind::STOP_SURFING && leg.tiles == 1; + if ((kind != StepKind::WALK && !lands) || !in_bounds(*map, next) || warps_on_step(*map, next)){ return std::nullopt; } - step.wild |= is_tall_grass(*map, next); + step.wild |= is_tall_grass(*map, next) || player.surfing; player = after_step(*map, player, leg.direction, kind); if (spin_direction(*map, player.position) && !land_on_spinner(*map, player, c + 1 == leg.tiles, step)){ return std::nullopt; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.cpp index ef58e7f860..e5cdb2ed97 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.cpp @@ -19,6 +19,7 @@ #include "PokemonFRLG_AutoStoryBattleDecisions.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h" +#include "PokemonFRLG_AutoStoryFieldMoves.h" #include "PokemonFRLG_AutoStoryHideout.h" #include "PokemonFRLG_AutoStoryHideoutLift.h" #include "PokemonFRLG_AutoStoryRooms.h" @@ -26,6 +27,7 @@ #include "PokemonFRLG_AutoStoryRowSim.h" #include "PokemonFRLG_AutoStoryViridianMart.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimField.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_FlyMap.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -602,11 +604,42 @@ bool center_walkable(const StoryStep& step){ return after && std::any_of(maps.begin(), maps.end(), [&](const auto& map){ return map.first == after->map; }); } +// Whether the sim can play a field move: Surf, or Fly to a town whose +// map is built into the program. +bool field_move_simulated(const StoryStep& step){ + if (parse_surf_step(step)){ + return true; + } + const std::optional fly = parse_fly_step(step); + const std::vector>& maps = overworld_map_json(); + return fly && std::any_of(maps.begin(), maps.end(), [&](const auto& map){ + return map.first == kanto_fly_towns()[fly->town].landing.map; + }); +} + +// A field move on the sim: turned to the water and Surf, or Fly and its +// landing waited out, as on the console. The failure, or empty. +std::string sim_field_move(const StoryStep& step, OverworldSim& sim){ + if (const std::optional water = parse_surf_step(step)){ + if (sim.state().facing != *water){ + sim_turn_to(sim, *water); + } + return sim.use_field(SimField::SURF) ? "" : "no water to Surf onto."; + } + const std::optional fly = parse_fly_step(step); + if (!fly || !sim.use_field(SimField::FLY, fly->town)){ + return "no Fly from " + to_string(sim.state().position) + "."; + } + sim_wait_out_battle_exit(sim); + return ""; +} + } // namespace // A scripted GAME step on the sim: the poster's switch, the lift's ride -// (its room coded, SimScript::lift_floors, rows as on the panel), or a -// Pokemon Center's way in or out (center_walkable()); any other GAME step +// (its room coded, SimScript::lift_floors, rows as on the panel), a +// Pokemon Center's way in or out (center_walkable()), or a field move +// (field_move_simulated()); any other GAME step // is stubbed. Pokemon::StoryAction OverworldStepSim::hideout_or_stub(){ Pokemon::StoryAction stub = m_stubs.stub(); @@ -615,7 +648,7 @@ Pokemon::StoryAction OverworldStepSim::hideout_or_stub(){ return failure.empty() ? Pokemon::outcome_ok() : Pokemon::outcome_failed("sim_failure", failure); }); sim.act = [stub, played_act = sim.act](const StoryStep& step, RunState& state){ - return is_hideout_step(step) || center_walkable(step) + return is_hideout_step(step) || center_walkable(step) || field_move_simulated(step) ? played_act(step, state) : stub.act(step, state); }; @@ -631,6 +664,10 @@ std::string OverworldStepSim::hideout(const StoryStep& step){ const std::string failure = sim_walk_room_legs(m_sim, center_legs(step.action)); return failure.empty() ? ended_on(step) : failure; } + if (is_field_move_step(step)){ + const std::string failure = sim_field_move(step, m_sim); + return failure.empty() ? ended_on(step) : failure; + } const std::optional ride = parse_lift_floor_step(step); if (!ride){ return "no lift ride \"" + step.action + "\"."; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable_Tests.cpp index f1ebde9f09..926b48d2c4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable_Tests.cpp @@ -19,6 +19,7 @@ #include "PokemonFRLG_AutoStoryStepInterrupts.h" #include "PokemonFRLG_AutoStoryStepTable.h" #include "PokemonFRLG_AutoStoryEvents.h" +#include "PokemonFRLG_AutoStoryFieldMoveSteps.h" #include "PokemonFRLG_AutoStoryHideout.h" #include "PokemonFRLG_AutoStoryRooms.h" #include "PokemonFRLG_AutoStoryItemUse.h" @@ -254,6 +255,7 @@ void add_tests_AutoStoryStepTable(UnitTestDatabase& database){ add_tests_AutoStoryStepInterrupts(database); // The GAME variants with logic of their own. add_tests_AutoStoryHideout(database); + add_tests_AutoStoryFieldMoves(database); add_tests_AutoStoryRooms(database); add_tests_AutoStoryItemUse(database); } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 426cb0ad26..db81aed2f1 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2007,6 +2007,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_BikeSpinPresses.h Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_CodedRooms.cpp Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_CodedRooms.h + Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_FlyMap.cpp + Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_FlyMap.h Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_HideoutLiftRoom.h Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_MovementMechanics.h Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_MovementStepPresses.h @@ -2112,6 +2114,11 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFrameReplay_Tests.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLegs.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLegs.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoveSteps.cpp + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoveSteps.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoves.cpp + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoves.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldMoves_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldSteps.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFieldSteps.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFossil.h