Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,12 @@
// 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<Direction> read_facing(ConsoleHandle& console, ProControllerContext& context){
std::optional<Direction> read_facing(ConsoleHandle& console, ProControllerContext& context, PlayerGender player){
context.wait_for_all_requests();
VideoSnapshot screen = console.video().snapshot();
std::optional<Direction> 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;
Expand Down Expand Up @@ -388,15 +388,16 @@
}


void walk_overworld_route(

Check warning on line 391 in SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::walk_overworld_route( ConsoleHandle & console , ProControllerContext & context , const std :: vector<RouteStep> & route , std :: optional<Direction> facing , const OverworldInterruptSet & interrupts , MoveSpeed speed , PlayerGender player): function_lines=50 (advisory above 25, limit 50)
ConsoleHandle& console, ProControllerContext& context,
const std::vector<RouteStep>& route,
std::optional<Direction> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -36,15 +37,17 @@ 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(
ConsoleHandle& console, ProControllerContext& context,
const std::vector<RouteStep>& route,
std::optional<Direction> facing = std::nullopt,
const OverworldInterruptSet& interrupts = default_overworld_interrupts(),
MoveSpeed speed = MoveSpeed::WALK
MoveSpeed speed = MoveSpeed::WALK,
PlayerGender player = PlayerGender::BOY
);


Expand Down Expand Up @@ -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<Direction> read_facing(ConsoleHandle& console, ProControllerContext& context);
// The way `player` faces, read from the screen; nothing if it can't be read.
std::optional<Direction> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@

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;

Expand Down Expand Up @@ -83,18 +86,31 @@
} // 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<Direction> read_player_facing(const ImageViewRGB32& screen){
std::optional<Direction> 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<Direction> match_player_facing(const ImageViewRGB32& game, const PlayerSprites& sprites){
Expand Down Expand Up @@ -156,7 +172,7 @@

std::optional<Direction> 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());
Expand Down Expand Up @@ -244,10 +260,65 @@
}
};

// 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());

Check warning on line 280 in SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}
return true;
}

private:
static std::string check_gender(PlayerGender gender){

Check warning on line 286 in SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::Test_PlayerFacingGenders::check_gender( PlayerGender gender): function_lines=29 (advisory above 25, limit 50)
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<Direction> 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<Test_PlayerFacingFixture>();
database.add<Test_PlayerFacingGenders>();
// LeafGreen on the Switch, captured with a 1080p card.
database.add<Test_PlayerFacingReader>("PokemonFRLG/PlayerFacingReader/Route24_South.png");
database.add<Test_PlayerFacingReader>("PokemonFRLG/PlayerFacingReader/Route24_North.png");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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{
Expand All @@ -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<Direction> 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<Direction> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

} // namespace

AutoStory::AutoStory()

Check warning on line 70 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::AutoStory::AutoStory(): function_lines=30 (advisory above 25, limit 50)
: RUN(checkpoint_database(), Pokemon::FireRedLeafGreenLanguages_Database())
, NEW_GAME_FROM_TITLE(
"<b>New game from the title screen:</b><br>The game is on its title screen with no save data. "
Expand All @@ -75,6 +75,16 @@
LockMode::LOCK_WHILE_RUNNING,
true
)
, PLAYER_GENDER(
"<b>Player:</b><br>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,
Expand All @@ -83,6 +93,7 @@
{
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);
}
Expand Down Expand Up @@ -205,7 +216,7 @@

} // namespace

void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){

Check warning on line 219 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::AutoStory::program( SingleSwitchProgramEnvironment & env , ProControllerContext & context): function_lines=49 (advisory above 25, limit 50)
if (const std::optional<Pokemon::VerifyPolicy> verify = RUN.VERIFY.policy()){
verify_story_checkpoint(env, context, RUN, *verify, NOTIFICATION_PROGRAM_FINISH);
return;
Expand All @@ -221,6 +232,7 @@
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class AutoStory : public SingleSwitchProgramInstance{
// every game's AutoStory.
Pokemon::StoryRunOptions RUN;
BooleanCheckBoxOption NEW_GAME_FROM_TITLE;
EnumDropdownOption<PlayerGender> PLAYER_GENDER;
// The save loaded was made by hand: keep the screen as its start frame.
BooleanCheckBoxOption KEEP_START_FRAME;
EventNotificationsOption NOTIFICATIONS;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* AutoStory Actions

Check warning on line 1 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryActions.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=878 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -161,7 +162,8 @@


// 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
Expand All @@ -177,9 +179,10 @@
// 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(){
Expand Down Expand Up @@ -228,7 +231,7 @@
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;
Expand All @@ -247,6 +250,33 @@
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<int> 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;
Expand Down Expand Up @@ -331,7 +361,9 @@
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;
};
Expand Down Expand Up @@ -834,9 +866,9 @@
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();
}


Expand Down
Loading
Loading