diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.cpp index 003ed1fb8d..86b4cbdfc2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.cpp @@ -284,12 +284,12 @@ void check_warp_was_no_battle(ConsoleHandle& console, ProControllerContext& cont // Each leg starts with a turn tap unless it goes the way the player faces. // A tap while already facing that way takes a step, so a facing not given // is read from the screen. If that fails too it taps anyway. -std::optional read_facing(ConsoleHandle& console, ProControllerContext& context){ +std::optional read_facing(ConsoleHandle& console, ProControllerContext& context, PlayerGender player){ context.wait_for_all_requests(); VideoSnapshot screen = console.video().snapshot(); std::optional facing; if (screen){ - facing = read_player_facing(screen); + facing = read_player_facing(screen, player); } console.log(std::string("Route: the player faces ") + (facing ? Pokemon::direction_name(*facing) : "an unknown way") + "."); return facing; @@ -393,10 +393,11 @@ void walk_overworld_route( const std::vector& route, std::optional facing, const OverworldInterruptSet& interrupts, - MoveSpeed speed + MoveSpeed speed, + PlayerGender player ){ if (!facing){ - facing = read_facing(console, context); + facing = read_facing(console, context, player); } for (const RouteStep& step : route){ console.log("Route: " + to_string(step)); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h index f8a7b0d71f..0ccb146c1c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h @@ -27,6 +27,7 @@ #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" #include "PokemonFRLG_OverworldRoute.h" #include "PokemonFRLG_OverworldWalkMoves.h" +#include "PokemonFRLG_PlayerFacingReader.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -36,7 +37,8 @@ namespace PokemonFRLG{ // Walks `route` from where it was planned, at `speed`. The player must be // standing still on the overworld and not on the bike. `facing` is the way -// the player faces at the start; if not given, it is read from the screen. +// the player faces at the start; if not given, it is read from the screen +// with `player`'s sprites. // Throws OperationFailedException if a warp's fade or the Surf prompt // doesn't show, or a leg is interrupted too many times. void walk_overworld_route( @@ -44,7 +46,8 @@ void walk_overworld_route( const std::vector& route, std::optional facing = std::nullopt, const OverworldInterruptSet& interrupts = default_overworld_interrupts(), - MoveSpeed speed = MoveSpeed::WALK + MoveSpeed speed = MoveSpeed::WALK, + PlayerGender player = PlayerGender::BOY ); @@ -72,8 +75,8 @@ void clear_open_message( // wild battle's. void check_warp_was_no_battle(ConsoleHandle& console, ProControllerContext& context, const RouteStep& step); -// The way the player faces, read from the screen; nothing if it can't be read. -std::optional read_facing(ConsoleHandle& console, ProControllerContext& context); +// The way `player` faces, read from the screen; nothing if it can't be read. +std::optional read_facing(ConsoleHandle& console, ProControllerContext& context, PlayerGender player); // Facing `direction` (turning first unless `facing_water`), starts Surf. void start_surf(ConsoleHandle& console, ProControllerContext& context, Direction direction, bool facing_water); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.cpp index c93346f9e8..46a97b8321 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.cpp @@ -28,9 +28,12 @@ namespace PokemonFRLG{ namespace{ -// The sprite sheet's frames are 16x32, and the standing ones are 0 (south), -// 1 (north) and 2 (west). Rows 12 to 30 are drawn; the rest is empty. -constexpr std::string_view PLAYER_SPRITE_SHEET = "PokemonFRLG/PlayerSprites/red_normal.png"; +// The sprite sheets' frames are 16x32, and the standing ones are 0 (south), +// 1 (north) and 2 (west). Rows 12 to 30 are drawn; the rest is empty. The +// boy and the girl share the layout (pokefirered field_player_avatar.c: +// OBJ_EVENT_GFX_RED_NORMAL and OBJ_EVENT_GFX_GREEN_NORMAL). +constexpr std::string_view BOY_SPRITE_SHEET = "PokemonFRLG/PlayerSprites/red_normal.png"; +constexpr std::string_view GIRL_SPRITE_SHEET = "PokemonFRLG/PlayerSprites/green_normal.png"; constexpr int SHEET_FRAME_WIDTH = 16; constexpr int SHEET_FIRST_ROW = 12; @@ -83,18 +86,31 @@ double difference(const ImageViewRGB32& game, const ImageViewRGB32& sprite, bool } // namespace -const PlayerSprites& player_sprites(){ - static const PlayerSprites SPRITES = []{ - const ImageRGB32 sheet(std::format("{}{}", RESOURCE_PATH(), PLAYER_SPRITE_SHEET)); - return PlayerSprites{sheet_frame(sheet, 0), sheet_frame(sheet, 1), sheet_frame(sheet, 2)}; - }(); - return SPRITES; +namespace{ + +PlayerSprites load_player_sprites(std::string_view sheet_path){ + const ImageRGB32 sheet(std::format("{}{}", RESOURCE_PATH(), sheet_path)); + return PlayerSprites{sheet_frame(sheet, 0), sheet_frame(sheet, 1), sheet_frame(sheet, 2)}; } +} // namespace + +const PlayerSprites& player_sprites(PlayerGender gender){ + switch (gender){ + case PlayerGender::GIRL:{ + static const PlayerSprites GIRL = load_player_sprites(GIRL_SPRITE_SHEET); + return GIRL; + } + case PlayerGender::BOY: + break; + } + static const PlayerSprites BOY = load_player_sprites(BOY_SPRITE_SHEET); + return BOY; +} -std::optional read_player_facing(const ImageViewRGB32& screen){ +std::optional read_player_facing(const ImageViewRGB32& screen, PlayerGender gender){ const ImageViewRGB32 game = extract_box_reference(screen, GameSettings::instance().GAME_BOX); - return match_player_facing(game, player_sprites()); + return match_player_facing(game, player_sprites(gender)); } std::optional match_player_facing(const ImageViewRGB32& game, const PlayerSprites& sprites){ @@ -156,7 +172,7 @@ class Test_PlayerFacingReader : public UnitTest{ std::optional facing; try{ - facing = read_player_facing(ImageRGB32(m_image)); + facing = read_player_facing(ImageRGB32(m_image), PlayerGender::BOY); }catch (FileException& e){ // The screenshots and the sprite sheet aren't in CI's resources yet. return UnitTestResult(UnitTestResult::SKIPPED, "File not found: " + e.message()); @@ -244,10 +260,65 @@ class Test_PlayerFacingFixture : public UnitTest{ } }; +// The real sheets, the boy's then the girl's, drawn on a grey screen: each +// reads with its own sheet, and neither reads with the other's. +class Test_PlayerFacingGenders : public UnitTest{ +public: + Test_PlayerFacingGenders() + : UnitTest("PokemonFRLG::PlayerFacingReader - boy and girl sheets") + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + try{ + for (PlayerGender gender : {PlayerGender::BOY, PlayerGender::GIRL}){ + const std::string error = check_gender(gender); + if (!error.empty()){ + return error; + } + } + }catch (FileException& e){ + return UnitTestResult(UnitTestResult::SKIPPED, "File not found: " + e.message()); + } + return true; + } + +private: + static std::string check_gender(PlayerGender gender){ + const std::string_view name = gender == PlayerGender::BOY ? "boy" : "girl"; + const PlayerSprites& sprites = player_sprites(gender); + struct Case{ + const ImageViewRGB32* sprite; + bool mirror; + Direction expected; + }; + const Case cases[] = { + {&sprites.south, false, Direction::SOUTH}, + {&sprites.north, false, Direction::NORTH}, + {&sprites.west, false, Direction::WEST}, + {&sprites.west, true, Direction::EAST}, + }; + for (const Case& test : cases){ + const ImageRGB32 screen = fixture_screen(test.sprite, test.mirror); + const std::optional own = match_player_facing(screen, sprites); + if (own != test.expected){ + return std::format("The {} facing {}: read {} with its own sheet.", + name, direction_name(test.expected), own ? direction_name(*own) : "nothing" + ); + } + const PlayerGender other = gender == PlayerGender::BOY ? PlayerGender::GIRL : PlayerGender::BOY; + if (match_player_facing(screen, player_sprites(other)) == test.expected){ + return std::format("The {} facing {} read with the other player's sheet.", name, direction_name(test.expected)); + } + } + return ""; + } +}; + } // namespace void add_tests_PlayerFacingReader(UnitTestDatabase& database){ database.add(); + database.add(); // LeafGreen on the Switch, captured with a 1080p card. database.add("PokemonFRLG/PlayerFacingReader/Route24_South.png"); database.add("PokemonFRLG/PlayerFacingReader/Route24_North.png"); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.h index bbb12c7d11..93e29a992b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.h @@ -6,8 +6,9 @@ * player on the same spot of the screen, so the reader compares that spot * with the player's standing sprites from pret/pokefirered, on the sprite's * own pixels only, so the ground behind doesn't matter. The sprites come - * from Packages: Resources/PokemonFRLG/PlayerSprites/red_normal.png is the - * decompilation's graphics/object_events/pics/people/red_normal.png. + * from Packages: Resources/PokemonFRLG/PlayerSprites/red_normal.png (the + * boy) and green_normal.png (the girl) are the decompilation's + * graphics/object_events/pics/people/ files of those names. * * Only the walking player is known: surfing and biking use other sprites. * @@ -27,6 +28,12 @@ namespace NintendoSwitch{ namespace PokemonFRLG{ +// Who the player is, picked at "Are you a boy or a girl?" in the intro. +enum class PlayerGender{ + BOY, + GIRL, +}; + // The standing player's frames, as the game draws them, with transparent // pixels at alpha 0. Facing east is facing west mirrored. struct PlayerSprites{ @@ -35,13 +42,15 @@ struct PlayerSprites{ ImageRGB32 west; }; -// Loads the frames once. Throws FileException if the file is missing. -const PlayerSprites& player_sprites(); +// Loads `gender`'s frames once. Throws FileException if the file is +// missing. +const PlayerSprites& player_sprites(PlayerGender gender); // The way the standing player faces, or nothing if no standing sprite -// matches clearly (surfing, on the bike, in a menu, mid-step). -std::optional read_player_facing(const ImageViewRGB32& screen); +// matches clearly (surfing, on the bike, in a menu, mid-step). Only +// `gender`'s sheet is loaded and matched. +std::optional read_player_facing(const ImageViewRGB32& screen, PlayerGender gender); // The same on `game`, the GBA's 240x160 screen at any scale, already cut // out of the console's. diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory.cpp index 65d0de1d1a..99fa97c948 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory.cpp @@ -75,6 +75,16 @@ AutoStory::AutoStory() LockMode::LOCK_WHILE_RUNNING, true ) + , PLAYER_GENDER( + "Player:
Who a new game plays as, picked at \"Are you a boy or a girl?\". " + "The player's sprite is read as this one's, so a save must be of the same player.", + { + {PlayerGender::BOY, "boy", "Boy"}, + {PlayerGender::GIRL, "girl", "Girl"}, + }, + LockMode::LOCK_WHILE_RUNNING, + PlayerGender::BOY + ) , KEEP_START_FRAME(KEEP_START_FRAME_TEXT, LockMode::LOCK_WHILE_RUNNING, false) , NOTIFICATIONS({ &NOTIFICATION_PROGRAM_FINISH, @@ -83,6 +93,7 @@ AutoStory::AutoStory() { PA_ADD_OPTION(RUN); PA_ADD_OPTION(NEW_GAME_FROM_TITLE); + PA_ADD_OPTION(PLAYER_GENDER); PA_ADD_OPTION(KEEP_START_FRAME); PA_ADD_OPTION(NOTIFICATIONS); } @@ -221,6 +232,7 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex } StoryEnv story = story_env(env, context, checkpoints[first], RUN.check_level(), KEEP_START_FRAME); + story.player_gender = PLAYER_GENDER; if (!(first == 0 && NEW_GAME_FROM_TITLE)){ env.console.log("Loading the save for " + checkpoints[first].id + "."); soft_reset(env.console, context); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory.h index 285a2c16b8..10710593aa 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory.h @@ -47,6 +47,7 @@ class AutoStory : public SingleSwitchProgramInstance{ // every game's AutoStory. Pokemon::StoryRunOptions RUN; BooleanCheckBoxOption NEW_GAME_FROM_TITLE; + EnumDropdownOption PLAYER_GENDER; // The save loaded was made by hand: keep the screen as its start frame. BooleanCheckBoxOption KEEP_START_FRAME; EventNotificationsOption NOTIFICATIONS; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryActions.cpp index 74d8f117b1..87b47ed2fd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryActions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryActions.cpp @@ -28,6 +28,7 @@ #include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" #include "VideoGames/PokemonFRLG/Programs/PokemonFRLG_StartMenuNavigation.h" #include "PokemonFRLG_AutoStoryActions.h" +#include "PokemonFRLG_AutoStoryGenderMenu.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -161,7 +162,8 @@ void open_start_menu_row(ConsoleHandle& console, ProControllerContext& context, // The new game's intro, from the title screen. Everything but the names -// is advanced with A, which never hurts there (BOY is the default, YES to +// is advanced with A, which never hurts there (BOY is the default, the +// girl is picked on the BOY/GIRL menu (gender_menu_cursor()), YES to // "So your name is ...?" and "Er, was it GREEN?"); B is never pressed // outside the keyboard, since in the intro it goes back to the controls // pages (console B, 2026-11-01). The controls pages read as a message box @@ -177,9 +179,10 @@ void open_start_menu_row(ConsoleHandle& console, ProControllerContext& context, // there instead, which also names the rival GREEN. class NewGameIntro{ public: - NewGameIntro(ConsoleHandle& console, ProControllerContext& context) + NewGameIntro(ConsoleHandle& console, ProControllerContext& context, PlayerGender gender) : m_console(console) , m_context(context) + , m_gender(gender) {} void run(){ @@ -228,7 +231,7 @@ class NewGameIntro{ pbf_wait(m_context, POLL); return false; } - if (names(screen)){ + if (gender_menu(screen) || names(screen)){ m_last_dialog = current_time(); m_last_press = m_last_dialog; return false; @@ -247,6 +250,33 @@ class NewGameIntro{ return quiet(screen, now); } + // True if the BOY/GIRL menu showed and was handled. It comes before the + // player's keyboard and is answered once. + bool gender_menu(const ImageViewRGB32& screen){ + if (m_gender_picked || m_keyboards_seen > 0 || m_gender == PlayerGender::BOY){ + return false; + } + const std::optional row = gender_menu_cursor(screen); + switch (gender_menu_step(row, m_gender)){ + case GenderMenuStep::DOWN: + m_console.log("New game: the BOY/GIRL menu, moving to GIRL."); + tap(m_context, DPAD_DOWN, 300ms); + break; + case GenderMenuStep::UP: + tap(m_context, DPAD_UP, 300ms); + break; + case GenderMenuStep::CONFIRM: + m_console.log("New game: the BOY/GIRL menu, picking GIRL."); + press(m_context, BUTTON_A, 2000ms); + m_gender_picked = true; + break; + case GenderMenuStep::NONE: + return false; + } + m_context.wait_for_all_requests(); + return true; + } + // True if a keyboard or the rival's name list showed and was handled. bool names(const ImageViewRGB32& screen){ IntroNameScreen shown = IntroNameScreen::OTHER; @@ -331,7 +361,9 @@ class NewGameIntro{ ProControllerContext& m_context; WallClock m_last_dialog; WallClock m_last_press; + PlayerGender m_gender; bool m_dialog_seen = false; + bool m_gender_picked = false; size_t m_keyboards_seen = 0; size_t m_presets_picked = 0; }; @@ -834,9 +866,9 @@ void buy_items( mash_until_overworld(console, context, 20000ms); } -void play_new_game_intro(ConsoleHandle& console, ProControllerContext& context){ - console.log("New game: from the title screen."); - NewGameIntro(console, context).run(); +void play_new_game_intro(ConsoleHandle& console, ProControllerContext& context, PlayerGender gender){ + console.log(gender == PlayerGender::GIRL ? "New game: from the title screen, as the girl." : "New game: from the title screen, as the boy."); + NewGameIntro(console, context, gender).run(); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp index 29c518b22f..b6de9476d3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp @@ -119,7 +119,7 @@ Outcome walk_route(StoryEnv& env, RunState& state, const StoryStep& step){ return Pokemon::outcome_failed("no_route", to_string(start) + " to " + to_string(goal.position)); } const OverworldInterruptSet interrupts = story_interrupts(env.supplies, arg(step, "fight_wild"), env.checks, env.language); - walk_overworld_route(env.console, env.context, *route, facing, interrupts, move_speed(step)); + walk_overworld_route(env.console, env.context, *route, facing, interrupts, move_speed(step), env.player_gender); log_arrival(env, step.name, target->map, target->tile); const std::optional end_facing = route->empty() ? facing : std::optional(route->back().facing_after); return Pokemon::outcome_ok(to_pose(StoryPose{target->map, target->tile, end_facing})); @@ -140,7 +140,7 @@ Outcome walk_legs(StoryEnv& env, RunState& state, const StoryStep& step){ return Pokemon::outcome_failed("bad_legs", to_string(start)); } const OverworldInterruptSet interrupts = story_interrupts(env.supplies, arg(step, "fight_wild"), env.checks, env.language); - walk_overworld_route(env.console, env.context, *route, facing, interrupts, move_speed(step)); + walk_overworld_route(env.console, env.context, *route, facing, interrupts, move_speed(step), env.player_gender); log_arrival(env, step.name, route->back().end.map, route->back().end.point); return Pokemon::outcome_ok(to_pose(StoryPose{route->back().end.map, route->back().end.point, route->back().facing_after})); } @@ -398,7 +398,7 @@ ConsoleAction console_action_of(StoryActionId id){ switch (id){ case StoryActionId::NEW_GAME: return [](StoryEnv& env, RunState&, const StoryStep&){ - play_new_game_intro(env.console, env.context); + play_new_game_intro(env.console, env.context, env.player_gender); return Pokemon::outcome_ok(); }; case StoryActionId::ROUTE: diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.h index 418c6f2c31..7c92161042 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.h @@ -33,6 +33,7 @@ #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMap.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.h" #include "PokemonFRLG_AutoStoryActions.h" #include "PokemonFRLG_AutoStoryBattle.h" #include "PokemonFRLG_AutoStoryStartFrames.h" @@ -70,6 +71,9 @@ struct StoryEnv{ // The next start check keeps the screen as its checkpoint's start // frame and passes: the user saved there by hand. bool keep_start_frame = false; + // The player: the one a new game picks, and whose sprites the facing + // reader matches. A save must be of this player. + PlayerGender player_gender = PlayerGender::BOY; }; // After a save is loaded for a checkpoint that starts on `start_map`: the diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryGenderMenu.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryGenderMenu.cpp new file mode 100644 index 0000000000..5628511473 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryGenderMenu.cpp @@ -0,0 +1,55 @@ +/* AutoStory Gender Menu + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/Images/SolidColorTest.h" +#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_SelectionArrowDetector.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_AutoStoryGenderMenu.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +std::optional gender_menu_cursor(const ImageViewRGB32& screen){ + // On the 240x160 screen: the menu's window spans x 144-216, y 72-104, + // and this box (x 190-212, y 76-100) is white there, right of BOY and + // GIRL. The intro's message box is lower down. + const ImageViewRGB32 game = extract_box_reference(screen, GameSettings::instance().GAME_BOX); + if (!is_white(extract_box_reference(game, ImageFloatBox(190.0 / 240, 76.0 / 160, 22.0 / 240, 24.0 / 160)))){ + return std::nullopt; + } + for (int row = 0; row < 2; row++){ + // rival_name_list_cursor()'s arrow box, moved to the menu's cursor + // at x 144, y 73 + 16 * row. + const ImageFloatBox box(142.6 / 240, (73.4 + 16 * row) / 160, 0.0369231, 0.0778846); + if (SelectionArrowDetector(COLOR_RED, nullptr, box).detect(screen)){ + return row; + } + } + return std::nullopt; +} + +GenderMenuStep gender_menu_step(std::optional cursor_row, PlayerGender wanted){ + if (!cursor_row || wanted == PlayerGender::BOY){ + return GenderMenuStep::NONE; + } + const int target = 1; + if (*cursor_row < target){ + return GenderMenuStep::DOWN; + } + if (*cursor_row > target){ + return GenderMenuStep::UP; + } + return GenderMenuStep::CONFIRM; +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryGenderMenu.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryGenderMenu.h new file mode 100644 index 0000000000..945a973cc4 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryGenderMenu.h @@ -0,0 +1,44 @@ +/* AutoStory Gender Menu + * + * From: https://github.com/PokemonAutomation/ + * + * The new game intro's "Are you a boy or a girl?" menu. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStoryGenderMenu_H +#define PokemonAutomation_PokemonFRLG_AutoStoryGenderMenu_H + +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The arrow's row on the BOY/GIRL menu (0 BOY, 1 GIRL), if it shows. In +// pokefirered's oak_speech.c the menu is a 9x4-tile window at tile +// (18, 9), rows 16 px apart, the cursor starting on BOY. +std::optional gender_menu_cursor(const ImageViewRGB32& screen); + +// What the intro does on the BOY/GIRL menu. The boy is left to the A every +// 1.5 s that advances the intro, as before; the girl is moved to and +// picked. +enum class GenderMenuStep{ + NONE, + DOWN, + UP, + CONFIRM, +}; +// The step for the arrow on row `cursor_row` (0 BOY, 1 GIRL), or none if +// the menu doesn't show. +GenderMenuStep gender_menu_step(std::optional cursor_row, PlayerGender wanted); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryIntro.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryIntro.h index 735cc17aa6..b6ed80fa69 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryIntro.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryIntro.h @@ -22,6 +22,7 @@ #include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.h" #include "Pokemon/Navigation/Pokemon_Grid.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.h" #include "PokemonFRLG_AutoStoryPresses.h" namespace PokemonAutomation{ @@ -58,10 +59,10 @@ constexpr size_t RIVAL_PRESET_TRIES = 3; // Determines the next input step in character/rival naming sequence. IntroNameStep intro_name_step(IntroNameScreen screen, size_t keyboards_seen, size_t presets_picked); // From the title screen of a game with no save, to the bedroom: the -// controls pages, Oak's speech, BOY, the default name for the player (an -// empty name confirmed) and the rival's first preset name. Returns on the -// overworld. -void play_new_game_intro(ConsoleHandle& console, ProControllerContext& context); +// controls pages, Oak's speech, `gender` on the BOY/GIRL menu, the default +// name for the player (an empty name confirmed) and the rival's first +// preset name. Returns on the overworld. +void play_new_game_intro(ConsoleHandle& console, ProControllerContext& context, PlayerGender gender); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp index 5768edbd81..f10b77a4a9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp @@ -21,6 +21,7 @@ #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.h" #include "PokemonFRLG_AutoStoryActions.h" +#include "PokemonFRLG_AutoStoryGenderMenu.h" #include "PokemonFRLG_AutoStoryMoveData.h" #include "PokemonFRLG_AutoStoryStepTable.h" #include "PokemonFRLG_AutoStoryTable.h" @@ -574,6 +575,32 @@ UnitTestResult test_intro_names(){ return true; } +// The BOY/GIRL menu, the boy then the girl: the boy is left to the A that +// advances the intro, as before; the girl moves down from BOY and is +// picked on GIRL. +UnitTestResult test_intro_gender(){ + struct Case{ + std::string_view what; + std::optional cursor_row; + PlayerGender wanted; + GenderMenuStep expected; + }; + const std::vector cases{ + {.what = "boy, no menu", .cursor_row = std::nullopt, .wanted = PlayerGender::BOY, .expected = GenderMenuStep::NONE}, + {.what = "boy, cursor on BOY", .cursor_row = 0, .wanted = PlayerGender::BOY, .expected = GenderMenuStep::NONE}, + {.what = "girl, no menu", .cursor_row = std::nullopt, .wanted = PlayerGender::GIRL, .expected = GenderMenuStep::NONE}, + {.what = "girl, cursor on BOY", .cursor_row = 0, .wanted = PlayerGender::GIRL, .expected = GenderMenuStep::DOWN}, + {.what = "girl, cursor on GIRL", .cursor_row = 1, .wanted = PlayerGender::GIRL, .expected = GenderMenuStep::CONFIRM}, + }; + for (const Case& c : cases){ + const GenderMenuStep step = gender_menu_step(c.cursor_row, c.wanted); + if (step != c.expected){ + return std::format("{}: expected step {}, got {}.", c.what, static_cast(c.expected), static_cast(step)); + } + } + return true; +} + // set_options()'s RIGHT presses from each value an old save can leave: // on console B a save with FAST and SET became SLOW and SHIFT when the // presses assumed MID and SHIFT. @@ -659,6 +686,10 @@ const AutoStoryTestCase AUTO_STORY_CASES[] = { .input = {.name = "intro: names, the rival's from the list", .body = test_intro_names}, .expectation = {.description = "verifies character and rival naming inputs"}, }, + { + .input = {.name = "intro: the boy, then the girl, on the BOY/GIRL menu", .body = test_intro_gender}, + .expectation = {.description = "leaves the boy to A; moves to GIRL and picks it"}, + }, { .input = {.name = "options: RIGHT presses from the shown value", .body = test_option_presses}, .expectation = {.description = "calculates Dpad RIGHT inputs for setting game options"}, diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 647ca0f7e2..e3d9f14d66 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2052,6 +2052,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCheckpoints.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryGenderMenu.cpp + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryGenderMenu.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryIntro.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemsPocket.h