Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2addd8f
FRLG RNG: check the party before the program starts
dayfine Oct 29, 2026
9f36e0d
FRLG RNG: keep the party check with the RNG program base; sort its in…
dayfine Oct 30, 2026
f194f48
Merge remote-tracking branch 'origin/main' into frlg/party-room-check
dayfine Oct 30, 2026
5490a0d
Merge origin/main into frlg/party-room-check
dayfine Oct 30, 2026
180a870
Merge origin/main into frlg/party-room-check
dayfine Oct 31, 2026
bdbeb41
FRLG RNG: unit test the party check
dayfine Oct 31, 2026
fda0033
FRLG RNG: test each program's party requirement from its settings
dayfine Oct 31, 2026
85c066e
FRLG RNG: JPN_Party.png has four Pokemon
dayfine Oct 31, 2026
24113fb
FRLG RNG: check the number of free party slots each program needs
dayfine Oct 31, 2026
df6210a
Merge origin/main into frlg/party-room-check
dayfine Nov 2, 2026
a5e50d1
FRLG: list the party slots on one line to keep PokemonFRLG_Navigation…
dayfine Nov 2, 2026
3618ff3
Merge origin/main into frlg/party-room-check
dayfine Nov 3, 2026
1dd5900
Merge origin/main into frlg/party-room-check
dayfine Nov 4, 2026
51ae1a6
Merge origin/main into frlg/party-room-check
dayfine Nov 6, 2026
13ce3e5
Merge origin/main into frlg/party-room-check
dayfine Nov 7, 2026
64af545
Merge remote-tracking branch 'upstream/main' into frlg/party-room-check
dayfine Nov 10, 2026
53d5718
FRLG RNG party check: review fixes
dayfine Nov 10, 2026
b240e06
Merge remote-tracking branch 'origin/main' into frlg/party-room-check
dayfine Nov 10, 2026
5e3f8b3
FRLG RNG party check: meet the code-health gate
dayfine Nov 10, 2026
3a152c4
Merge remote-tracking branch 'origin/main' into frlg/party-room-check
dayfine Nov 10, 2026
8490e0c
FRLG RNG party check: std::format for the message; note why StarterRn…
dayfine Nov 10, 2026
a48b54d
FRLG RNG party check: no check in RngHelper, the manual tool
dayfine Nov 10, 2026
fa6ff20
FRLG RNG party check tests: std::format for the failure messages
dayfine Nov 10, 2026
d3df2bc
Merge origin/main into frlg/party-room-check
dayfine Nov 10, 2026
eb3fa61
Merge origin/main into frlg/party-room-check
dayfine Nov 11, 2026
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 @@ -69,7 +69,7 @@
void SingleSwitchProgramInstance::program(GameConsole::ConsoleProgramEnvironment& env, CancellableScope& scope){
program(static_cast<SingleSwitchProgramEnvironment&>(env), scope);
}
void SingleSwitchProgramInstance::program(SingleSwitchProgramEnvironment& env, CancellableScope& scope){

Check warning on line 72 in SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::SingleSwitchProgramInstance::program( SingleSwitchProgramEnvironment & env , CancellableScope & scope): function_lines=27 (advisory above 25, limit 50)
ProControllerContext context(scope, env.console.controller<ProController>());
if (!context->is_ready()){
throw UserSetupError(context->logger(), "Controller is not ready.");
Expand All @@ -86,6 +86,7 @@
};

try{
start_program_game_checks(env, context);
program(env, context);
}catch (FatalProgramException&){
record_debug_video();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ class SingleSwitchProgramInstance : public GameConsole::ConsoleProgramInstance{
FeedbackType feedback_type
);

// Called with the controller ready, just before program(env, context).
// Override to check that the game is set up the way the program needs
// (e.g. a free party slot) and throw UserSetupError if not, so a setup
// mistake stops the program before it starts its work.
virtual void start_program_game_checks(
SingleSwitchProgramEnvironment& env,
ProControllerContext& context
){}

private:
virtual void program(GameConsole::ConsoleProgramEnvironment& env, CancellableScope& scope) override;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -839,18 +839,15 @@ void open_party_menu_from_overworld(ConsoleHandle& console, ProControllerContext
}

PartySlot detect_last_occupied_party_slot(ConsoleHandle& console){
const auto snapshot = console.video().snapshot();
constexpr std::array slots{
PartySlot::SIX,
PartySlot::FIVE,
PartySlot::FOUR,
PartySlot::THREE,
PartySlot::TWO,
};
return detect_last_occupied_party_slot(console.video().snapshot());
}

PartySlot detect_last_occupied_party_slot(const ImageViewRGB32& screen){
constexpr std::array slots{PartySlot::SIX, PartySlot::FIVE, PartySlot::FOUR, PartySlot::THREE, PartySlot::TWO};

for (PartySlot slot : slots){
PartyEmptySlotDetector empty_slot_detector(COLOR_RED, slot);
if (!empty_slot_detector.detect(snapshot)){
if (!empty_slot_detector.detect(screen)){
return slot;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define PokemonAutomation_PokemonFRLG_Navigation_H

#include "Common/Cpp/Logging/AbstractLogger.h"
#include "CommonFramework/ImageTypes/ImageViewRGB32.h"
#include "CommonFramework/Language.h"
#include "CommonFramework/Tools/VideoStream.h"
#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h"
Expand Down Expand Up @@ -136,6 +137,8 @@ void open_party_menu_from_overworld(ConsoleHandle& console, ProControllerContext

// Starting from the party menu, detect the last occupied party slot
PartySlot detect_last_occupied_party_slot(ConsoleHandle& console);
// Detect the last occupied party slot on a screenshot of the party menu
PartySlot detect_last_occupied_party_slot(const ImageViewRGB32& screen);

// Starting from the start menu, a sub-screen of the start menu, or the overworld, navigate to the bag
void open_bag_from_overworld(ConsoleHandle& console, ProControllerContext& context, StartMenuContext menu_context = StartMenuContext::STANDARD);
Expand Down
291 changes: 291 additions & 0 deletions SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
/* Pokemon FRLG Tests

Check warning on line 1 in SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=637 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <fstream>
#include <functional>
#include <map>
#include <memory>
#include <string_view>
#include "Common/Cpp/Exceptions.h"
#include "Common/Cpp/Filesystem/Filesystem.h"
#include "Common/Cpp/Json/JsonObject.h"
#include "Common/Cpp/Json/JsonValue.h"
#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
#include "CommonFramework/StaticGlobals.h"
#include "Inference/Box/PokemonFRLG_BoxCellOccupiedDetector.h"
Expand All @@ -28,7 +36,16 @@
#include "Inference/PokemonFRLG_TrainerIdReader.h"
#include "Inference/PokemonFRLG_WildEncounterReader.h"
#include "Programs/BoxOrganization/PokemonFRLG_BoxSorter_Tests.h"
#include "Programs/RngManipulation/PokemonFRLG_EggRng.h"
#include "Programs/RngManipulation/PokemonFRLG_GiftRng.h"
#include "Programs/RngManipulation/PokemonFRLG_RngHelper.h"
#include "Programs/RngManipulation/PokemonFRLG_RngProgram.h"
#include "Programs/RngManipulation/PokemonFRLG_RoamingLegendaryRng.h"
#include "Programs/RngManipulation/PokemonFRLG_StarterRng.h"
#include "Programs/RngManipulation/PokemonFRLG_StaticRng.h"
#include "Programs/RngManipulation/PokemonFRLG_WildRng.h"
#include "PokemonFRLG_Navigation.h"
#include "Tests/ImageUnitTest.h"
#include "Tests/TestUtils.h"

#include "PokemonFRLG_Tests.h"
Expand Down Expand Up @@ -261,10 +278,281 @@
}
}


std::string slot_name(PartySlot slot){
return std::to_string(static_cast<unsigned>(slot) + 1);
}

// Reads the last occupied slot from a party menu screenshot, as the RNG programs' party check does.
class Test_LastOccupiedPartySlot : public ImageUnitTest{
public:
Test_LastOccupiedPartySlot(std::string_view image, PartySlot expected)
: ImageUnitTest("PokemonFRLG::LastOccupiedPartySlot - " + std::string(image), {std::string(image)})

Check warning on line 290 in SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.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")
, m_expected(expected)
{}

virtual UnitTestResult run_on_images(
Logger&, CancellableScope&,
const std::vector<ImageRGB32>& images
) const override{
const PartySlot slot = detect_last_occupied_party_slot(images[0]);
if (slot != m_expected){
return std::format("Expected last occupied slot {}, got {}", slot_name(m_expected), slot_name(slot));
}
return true;
}

private:
const PartySlot m_expected;
};

void add_tests_LastOccupiedPartySlot(UnitTestDatabase& database){
for (unsigned occupied = 1; occupied <= 6; occupied++){
database.add<Test_LastOccupiedPartySlot>(
"PokemonFRLG/Party/Party_" + std::to_string(occupied) + ".png",

Check warning on line 312 in SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.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")
static_cast<PartySlot>(occupied - 1)
);
}
database.add<Test_LastOccupiedPartySlot>("PokemonFRLG/Party/Party_6_With_Held_Items.png", PartySlot::SIX);
database.add<Test_LastOccupiedPartySlot>("PokemonFRLG/Party/JPN_HeldItem.png", PartySlot::SIX);
database.add<Test_LastOccupiedPartySlot>("PokemonFRLG/Party/JPN_Party.png", PartySlot::FOUR);
database.add<Test_LastOccupiedPartySlot>("PokemonFRLG/Party/SPA_HeldItem.png", PartySlot::SIX);
}

// Steps that return scripted reads, and record what check_party_free_slots() asked for.
class ScriptedPartyCheck : public PartyCheckActions{
public:
bool menu_opens = true;
PartySlot last_occupied = PartySlot::ONE;
std::string steps;

virtual bool open_party_menu() override{
steps += steps.empty() ? "open" : " open";
return menu_opens;
}
virtual PartySlot read_last_occupied_slot() override{
steps += steps.empty() ? "read" : " read";
return last_occupied;
}
};

struct PartyCheckInput{
std::string name;
size_t free_slots_needed = 1;
bool menu_opens = true;
PartySlot last_occupied = PartySlot::ONE;
};

struct PartyCheckExpectation{
std::string steps = "";
// Start of the UserSetupError message; empty if the check passes.
std::string error = "";
};

struct PartyCheckCase{
PartyCheckInput input;
PartyCheckExpectation expectation;
};

class Test_PartyCheck : public UnitTest{
public:
Test_PartyCheck(PartyCheckCase test_case)
: UnitTest("PokemonFRLG::RngPartyCheck - " + test_case.input.name)

Check warning on line 360 in SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.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")
, m_case(std::move(test_case))
{}

virtual UnitTestResult run(Logger&, CancellableScope&) const override{
const PartyCheckInput& input = m_case.input;
const PartyCheckExpectation& expected = m_case.expectation;
ScriptedPartyCheck actions;
actions.menu_opens = input.menu_opens;
actions.last_occupied = input.last_occupied;

NullLogger logger;
std::string error;
try{
check_party_free_slots(logger, input.free_slots_needed, actions);
}catch (UserSetupError& e){
error = e.message();
}
const bool error_matches = expected.error.empty()
? error.empty()
: error.rfind(expected.error, 0) == 0;
if (!error_matches){
return std::format("Expected error: \"{}\"\nActual error: \"{}\"", expected.error, error);
}
if (actions.steps != expected.steps){
return std::format("Expected steps: {}\nActual steps: {}", expected.steps, actions.steps);
}
return true;
}

private:
const PartyCheckCase m_case;
};

void add_tests_PartyCheck(UnitTestDatabase& database){

Check warning on line 394 in SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::add_tests_PartyCheck( UnitTestDatabase & database): function_lines=37 (advisory above 25, limit 50)
const PartyCheckCase cases[] = {
{
// Programs that don't add a Pokemon to the party don't open the menu.
.input = {.name = "no free slot needed, full party", .free_slots_needed = 0, .last_occupied = PartySlot::SIX},
.expectation = {},
},
{
.input = {.name = "one free slot needed, one Pokemon"},
.expectation = {.steps = "open read"},
},
{
.input = {.name = "one free slot needed, five Pokemon", .last_occupied = PartySlot::FIVE},
.expectation = {.steps = "open read"},
},
{
.input = {.name = "one free slot needed, full party", .last_occupied = PartySlot::SIX},
.expectation = {.steps = "open read", .error = "Your party is full. Leave a party slot free"},
},
{
.input = {.name = "three free slots needed, three Pokemon", .free_slots_needed = 3, .last_occupied = PartySlot::THREE},
.expectation = {.steps = "open read"},
},
{
.input = {.name = "three free slots needed, four Pokemon", .free_slots_needed = 3, .last_occupied = PartySlot::FOUR},
.expectation = {.steps = "open read", .error = "Your party has 4 "},
},
{
// A menu that can't be opened skips the check rather than stopping the program.
.input = {.name = "party menu not opened", .menu_opens = false, .last_occupied = PartySlot::SIX},
.expectation = {.steps = "open"},
},
};
for (const PartyCheckCase& test_case : cases){
database.add<Test_PartyCheck>(test_case);
}
}


std::string menu_context_name(StartMenuContext context){
switch (context){
case StartMenuContext::STANDARD: return "STANDARD";
case StartMenuContext::SAFARI_ZONE: return "SAFARI_ZONE";
case StartMenuContext::NO_DEX: return "NO_DEX";
}
return "?";
}

template <typename Program>
std::unique_ptr<RngProgramInstance> make_rng_program(){
return std::make_unique<Program>();
}

struct RngProgramPartyInput{
std::string name;
std::function<std::unique_ptr<RngProgramInstance>()> make_program;
// Program settings by option name, as in the saved settings JSON.
std::map<std::string, std::string> settings = {};
};

struct RngProgramPartyExpectation{
size_t free_slots;
StartMenuContext menu_context = StartMenuContext::STANDARD;
};

struct RngProgramPartyCase{
RngProgramPartyInput input;
RngProgramPartyExpectation expectation;
};

// Builds the program with the given settings and checks what its start check asks for. The
// check itself runs for every RNG program: RngProgramInstance's start hook is final.
class Test_RngProgramPartyRequirement : public UnitTest{
public:
Test_RngProgramPartyRequirement(RngProgramPartyCase test_case)
: UnitTest("PokemonFRLG::RngProgramPartyRequirement - " + test_case.input.name)

Check warning on line 469 in SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.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")
, m_case(std::move(test_case))
{}

virtual UnitTestResult run(Logger&, CancellableScope&) const override{
const RngProgramPartyInput& input = m_case.input;
const RngProgramPartyExpectation& expected = m_case.expectation;
std::unique_ptr<RngProgramInstance> program = input.make_program();
JsonObject settings;
for (const auto& [option, value] : input.settings){
settings[option] = value;
}
program->load_json(JsonValue(std::move(settings)));

const size_t free_slots = program->free_party_slots_needed();
if (free_slots != expected.free_slots){
return std::format("Expected {} free party slots needed, got {}", expected.free_slots, free_slots);
}
const StartMenuContext menu_context = program->start_menu_context();
if (menu_context != expected.menu_context){
return std::format(
"Expected start menu {}, got {}", menu_context_name(expected.menu_context), menu_context_name(menu_context)
);
}
return true;
}

private:
const RngProgramPartyCase m_case;
};

const RngProgramPartyCase RNG_PROGRAM_PARTY_CASES[] = {
{
.input = {.name = "StarterRng", .make_program = make_rng_program<StarterRng>},
.expectation = {.free_slots = 0},
},
{
// The egg and the two wild Pokemon caught to confirm the seed.
.input = {.name = "EggRng", .make_program = make_rng_program<EggRng>},
.expectation = {.free_slots = 3},
},
{
.input = {.name = "GiftRng", .make_program = make_rng_program<GiftRng>},
.expectation = {.free_slots = 1},
},
{
.input = {.name = "StaticRng", .make_program = make_rng_program<StaticRng>},
.expectation = {.free_slots = 1},
},
{
.input = {.name = "RoamingLegendaryRng", .make_program = make_rng_program<RoamingLegendaryRng>},
.expectation = {.free_slots = 1},
},
{
.input = {
.name = "WildRng on Route 1",
.make_program = make_rng_program<WildRng>,
.settings = {{"GAME_LOCATION", "route_1"}},
},
.expectation = {.free_slots = 1},
},
{
// The Safari Zone start menu has no POKEMON entry in the usual place.
.input = {
.name = "WildRng in the Safari Zone",
.make_program = make_rng_program<WildRng>,
.settings = {{"GAME_LOCATION", "safari_zone_entrance"}},
},
.expectation = {.free_slots = 1, .menu_context = StartMenuContext::SAFARI_ZONE},
},
// RngHelper is the manual tool: whatever the target, it trusts the user's party.
{
.input = {.name = "RngHelper lapras", .make_program = make_rng_program<RngHelper>, .settings = {{"TARGET", "lapras"}}},
.expectation = {.free_slots = 0},
},
};

void add_tests_RngProgramPartyRequirement(UnitTestDatabase& database){
for (const RngProgramPartyCase& test_case : RNG_PROGRAM_PARTY_CASES){
database.add<Test_RngProgramPartyRequirement>(test_case);
}
}

}


void add_tests(UnitTestDatabase& database){

Check warning on line 555 in SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::add_tests( UnitTestDatabase & database): function_lines=30 (advisory above 25, limit 50)
add_tests_AdvanceWhiteDialogDetector(database);
add_tests_AdvanceBattleDialogDetector(database);
add_tests_BagDetector(database);
Expand All @@ -279,12 +567,15 @@
add_tests_EliteFourRoomDetector(database);
add_tests_ExitWildBattle(database);
add_tests_HallOfFameSavingDetector(database);
add_tests_LastOccupiedPartySlot(database);
add_tests_PartyCheck(database);
add_tests_PartyEmptySlotDetector(database);
add_tests_PartyHeldItemDetector(database);
add_tests_PartyLevelUpReader(database);
add_tests_PartyMenuDetector(database);
add_tests_PrizeSelectDetector(database);
add_tests_PartySlotDetector(database);
add_tests_RngProgramPartyRequirement(database);
add_tests_StatsReader(database);
add_tests_SelectionDialogDetector(database);
add_tests_ShinySymbolDetector(database);
Expand Down
Loading
Loading