Skip to content
Closed
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 @@ -5,6 +5,7 @@
*/
#include "Common/Cpp/PrettyPrint.h"
#include "Pokemon/Pokemon_Types.h"
#include "VideoGames/PokemonSwSh/Resources/PokemonSwSh_MaxLairDatabase.h"
#include "PokemonSwSh_MaxLair_State.h"

namespace PokemonAutomation{
Expand All @@ -13,6 +14,24 @@ namespace PokemonSwSh{
namespace MaxLairInternal{


bool GlobalState::infer_boss_from_opponent(){
if (opponent.size() != 1){
return false;
}
const std::string& slug = *opponent.begin();
if (is_boss(slug) != (wins == 3)){
return false;
}
if (wins == 3 && !is_boss(boss)){
boss = slug;
}
return true;
}
std::string GlobalState::caught_boss(size_t caught_count) const{
// A failed adventure can still have an identified boss, but no boss catch.
return caught_count == 4 && is_boss(boss) ? boss : std::string();
}

std::string PlayerState::dump() const{
std::string str;
str += console_id < 0 ? "NPC" : "Switch " + std::to_string(console_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
std::string dump_path(const std::vector<PathNode>& path);


struct GlobalState{

Check warning on line 143 in SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSwSh::MaxLairInternal::GlobalState: public_methods=6 (advisory above 5, limit 7)

Check warning on line 143 in SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSwSh::MaxLairInternal::GlobalState: public_methods=6 (advisory above 5, limit 7)
// Timestamp when this state was issued.
WallClock timestamp = WallClock::min();

Expand Down Expand Up @@ -168,6 +168,13 @@
size_t find_player_index(size_t console_id) const;
void clear_battle_state();

// Accept a single opponent for the current battle stage and infer the boss.
// Returns false when the read needs to be retried; rejected reads do not change the boss.
bool infer_boss_from_opponent();

// Return the caught boss slug, or an empty string if no boss was caught.
std::string caught_boss(size_t caught_count) const;

std::string dump() const;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class EndBattleDecider{
virtual const std::string& boss_ball(
size_t console_index, const std::string& boss_slug
) const = 0;
// boss_slug is empty when the adventure ended without catching the boss.
virtual CaughtScreenAction end_adventure_action(
size_t console_index, const std::string& boss_slug,
const PathStats& path_stats,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,9 @@ bool read_battle_menu(
std::set<std::string> mon = reader.read_opponent(stream.logger(), context, stream.video());

bool retry = !process_header_read(stream.logger(), state, std::move(mon));
std::string opponent = state.opponent.empty()
? ""
: *state.opponent.begin();

// If we see a boss before the end. Something is wrong. Retry.
if (state.wins != 3 && is_boss(opponent)){
// The fourth battle must be a boss; earlier battles must be rentals.
if (!state.infer_boss_from_opponent()){
retry = true;
}

Expand All @@ -97,7 +94,7 @@ bool read_battle_menu(
pbf_mash_button(context, BUTTON_B, 3000ms);
state.opponent = std::move(mon);

if (state.wins == 3 && !state.boss.empty()){
if (state.wins == 3 && is_boss(state.boss)){
if (!state.opponent.empty() && *state.opponent.begin() != state.boss){
stream.log(
"Inconsistent Boss: Expected " + state.boss + ", Read: " + *state.opponent.begin(),
Expand All @@ -106,25 +103,16 @@ bool read_battle_menu(
}
state.opponent = {state.boss};
}

opponent = state.opponent.empty()
? ""
: *state.opponent.begin();
}
context.wait_for_all_requests();


if (state.wins != 3 && is_boss(opponent)){
stream.log("Boss found before 3 wins. Something is seriously out-of-sync.", COLOR_RED);
dump_image(stream.logger(), MODULE_NAME, "BossBeforeEnd", snapshot);
if (!state.infer_boss_from_opponent()){
stream.log("Opponent does not match the battle stage after summary read.", COLOR_RED);
dump_image(stream.logger(), MODULE_NAME, "UnexpectedBattleOpponent", snapshot);
return false;
}

// Infer the boss if we don't know it.
if (state.boss.empty() && state.wins == 3){
state.boss = opponent;
}

// Read misc.
VideoSnapshot screen = stream.video().snapshot();
state.opponent_hp = reader.read_opponent_hp(stream.logger(), screen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ StateMachineAction run_caught_screen(
}


const std::string& boss = state_tracker[console_index].boss;
const std::string boss = state_tracker[console_index].caught_boss(tracker.total());
CaughtScreenAction action =
decider.end_adventure_action(
console_index, boss,
Expand Down
76 changes: 76 additions & 0 deletions SerialPrograms/Source/VideoGames/PokemonSwSh/PokemonSwSh_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
*
*/

#include <format>
#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
#include "VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h"
#include "VideoGames/PokemonSwSh/Inference/PokemonSwSh_DialogBoxDetector.h"
#include "VideoGames/PokemonSwSh/Inference/PokemonSwSh_DialogTriangleDetector.h"
#include "VideoGames/PokemonSwSh/Inference/PokemonSwSh_BoxGenderDetector.h"
Expand All @@ -21,7 +23,81 @@



namespace{
using MaxLairInternal::GlobalState;

struct BossStateCase{
std::string name;
uint8_t wins;
std::set<std::string> opponent;
std::string boss;
size_t caught_count;
bool expected_accepted;
std::string expected_boss;
std::string expected_caught_boss;
};

class Test_MaxLairBossState : public UnitTest{
public:
explicit Test_MaxLairBossState(BossStateCase scenario)
: UnitTest("PokemonSwSh::MaxLairBossState - " + scenario.name)
, m_scenario(std::move(scenario))
{}

virtual UnitTestResult run(Logger&, CancellableScope&) const override{
GlobalState state;
state.wins = m_scenario.wins;
state.opponent = m_scenario.opponent;
state.boss = m_scenario.boss;
const bool accepted = state.infer_boss_from_opponent();
if (accepted != m_scenario.expected_accepted){
return std::format("Expected opponent accepted={}, got {}", m_scenario.expected_accepted, accepted);
}
if (state.boss != m_scenario.expected_boss){
return std::format("Expected boss '{}', got '{}'", m_scenario.expected_boss, state.boss);
}
const std::string caught = state.caught_boss(m_scenario.caught_count);
if (caught != m_scenario.expected_caught_boss){
return std::format("Expected caught boss '{}', got '{}'", m_scenario.expected_caught_boss, caught);
}
return true;
}

private:
BossStateCase m_scenario;
};

void add_tests_MaxLairBossState(UnitTestDatabase& database){

Check warning on line 70 in SerialPrograms/Source/VideoGames/PokemonSwSh/PokemonSwSh_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSwSh::add_tests_MaxLairBossState( UnitTestDatabase & database): function_lines=27 (advisory above 25, limit 50)

Check warning on line 70 in SerialPrograms/Source/VideoGames/PokemonSwSh/PokemonSwSh_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSwSh::add_tests_MaxLairBossState( UnitTestDatabase & database): function_lines=27 (advisory above 25, limit 50)
const BossStateCase cases[] = {
{.name = "rental before three wins", .wins = 2, .opponent = {"mimikyu-disguised"}, .boss = "", .caught_count = 3,
.expected_accepted = true, .expected_boss = "", .expected_caught_boss = ""},
{.name = "Mimikyu at boss stage must retry", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "", .caught_count = 3,
.expected_accepted = false, .expected_boss = "", .expected_caught_boss = ""},
{.name = "Kyurem after Mimikyu misidentification", .wins = 3, .opponent = {"kyurem"}, .boss = "mimikyu-disguised", .caught_count = 3,
.expected_accepted = true, .expected_boss = "kyurem", .expected_caught_boss = ""},
{.name = "identified boss lost", .wins = 3, .opponent = {"kyurem"}, .boss = "kyurem", .caught_count = 3,
.expected_accepted = true, .expected_boss = "kyurem", .expected_caught_boss = ""},
{.name = "identified boss caught", .wins = 3, .opponent = {"kyurem"}, .boss = "", .caught_count = 4,
.expected_accepted = true, .expected_boss = "kyurem", .expected_caught_boss = "kyurem"},
{.name = "boss before three wins must retry", .wins = 2, .opponent = {"kyurem"}, .boss = "", .caught_count = 2,
.expected_accepted = false, .expected_boss = "", .expected_caught_boss = ""},
{.name = "empty boss-stage read must retry", .wins = 3, .opponent = {}, .boss = "", .caught_count = 3,
.expected_accepted = false, .expected_boss = "", .expected_caught_boss = ""},
{.name = "ambiguous boss-stage read must retry", .wins = 3, .opponent = {"kyurem", "mimikyu-disguised"}, .boss = "", .caught_count = 3,
.expected_accepted = false, .expected_boss = "", .expected_caught_boss = ""},
{.name = "rejected read preserves known boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "kyurem", .caught_count = 3,
.expected_accepted = false, .expected_boss = "kyurem", .expected_caught_boss = ""},
{.name = "four catches cannot legitimize rental boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "mimikyu-disguised", .caught_count = 4,
.expected_accepted = false, .expected_boss = "mimikyu-disguised", .expected_caught_boss = ""},
};
for (const BossStateCase& scenario : cases){
database.add<Test_MaxLairBossState>(scenario);
}
}
}

void add_tests(UnitTestDatabase& database){
add_tests_MaxLairBossState(database);
NintendoSwitch::PokemonSwSh::add_tests_DialogBoxDetector(database);
NintendoSwitch::PokemonSwSh::add_tests_DialogTriangleDetector(database);
NintendoSwitch::PokemonSwSh::add_tests_BoxGenderDetector(database);
Expand Down
Loading