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"
Comment thread
dayfine marked this conversation as resolved.
Resolved
#include "PokemonSwSh_MaxLair_State.h"

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


namespace{

// wins is incremented after each battle is won: battles 1-3 (wins < 3) are
// against rental Pokemon, and battle 4 (wins == 3) is against the legendary boss.
bool is_boss_battle(const GlobalState& state){
return state.wins == 3;
}

} // namespace

bool GlobalState::accept_opponent(){
const std::string opponent_slug = opponent.empty() ? "" : *opponent.begin();
// A boss before the end means something is out-of-sync.
if (!is_boss_battle(*this) && is_boss(opponent_slug)){
return false;
}
// Infer the boss if we don't know it.
if (is_boss_battle(*this) && boss.empty()){
boss = opponent_slug;
}
return true;
}
Comment thread
dayfine marked this conversation as resolved.
Resolved

std::string GlobalState::boss_if_caught() const{
// The identified boss is reported whether or not it was caught.
return boss;
}

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,12 +140,16 @@
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)
// Timestamp when this state was issued.
WallClock timestamp = WallClock::min();

bool adventure_started = false;

// The adventure's boss as known from the start: the den's boss sprite,
// voted across consoles. Used all adventure long (path and move choice,
// the boss ball, the fourth battle's check, Save Path) and kept after a
// lost adventure.
std::string boss;

PathMap path;
Expand All @@ -168,6 +172,19 @@
size_t find_player_index(size_t console_id) const;
void clear_battle_state();

// Returns false when the opponent read doesn't fit this battle stage (a
// boss before the fourth battle), so the caller retries the read. On an
// accepted read at the fourth battle, records the boss if none is known.
bool accept_opponent();

// The same boss, but only if the adventure caught it, else empty: for the
// end-of-adventure decision (end_adventure_action()), which must not apply
// the boss-caught rules after a loss. Decided from the battles won (the
// boss is the fourth battle), not from how many Pokemon were caught, so it
// stays right if non-boss catches are ever skipped.
// For now this returns boss whatever the battles won.
std::string boss_if_caught() const;

std::string dump() const;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* Max Lair State Tests
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <utility>
#include "Common/Cpp/TestRunners/UnitTest.h"
#include "VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h"
#include "PokemonSwSh_MaxLair_State_Tests.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonSwSh{
namespace MaxLairInternal{
namespace{

struct BossStateTestCaseInput{
std::string name;
uint8_t wins;
std::set<std::string> opponent;
std::string boss;
uint8_t final_wins; // wins on the caught screen
};

struct BossStateTestCaseExpectation{
bool accepted;
std::string boss;
std::string boss_if_caught;
};

struct BossStateTestCase{
BossStateTestCaseInput input;
BossStateTestCaseExpectation expectation;
};

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

virtual UnitTestResult run(Logger&, CancellableScope&) const override{
const BossStateTestCaseInput& input = m_scenario.input;
const BossStateTestCaseExpectation& expected = m_scenario.expectation;
GlobalState state;
state.wins = input.wins;
state.opponent = input.opponent;
state.boss = input.boss;
const bool accepted = state.accept_opponent();
if (accepted != expected.accepted){
return std::format("Expected opponent accepted={}, got {}", expected.accepted, accepted);
}
if (state.boss != expected.boss){
return std::format("Expected boss '{}', got '{}'", expected.boss, state.boss);
}
state.wins = input.final_wins;
const std::string caught = state.boss_if_caught();
if (caught != expected.boss_if_caught){
return std::format("Expected boss_if_caught '{}', got '{}'", expected.boss_if_caught, caught);
}
return true;
}

private:
BossStateTestCase m_scenario;
};

// Cases marked TODO record the current, wrong behaviour.
const BossStateTestCase CASES[] = {
{
.input = {.name = "rental before three wins", .wins = 2, .opponent = {"mimikyu-disguised"}, .boss = "", .final_wins = 3},
.expectation = {.accepted = true, .boss = "", .boss_if_caught = ""},
},
// TODO: to be fixed: a rental at the boss stage is accepted as the boss and reported caught.
{
.input = {.name = "Mimikyu at boss stage", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "", .final_wins = 3},
.expectation = {.accepted = true, .boss = "mimikyu-disguised", .boss_if_caught = "mimikyu-disguised"},
},
// TODO: to be fixed: a rental misread as the boss is kept after the real boss is read.
{
.input = {.name = "Kyurem after Mimikyu misidentification", .wins = 3, .opponent = {"kyurem"}, .boss = "mimikyu-disguised", .final_wins = 3},
.expectation = {.accepted = true, .boss = "mimikyu-disguised", .boss_if_caught = "mimikyu-disguised"},
},
// TODO: to be fixed: the boss is reported caught without a fourth win.
{
.input = {.name = "identified boss lost", .wins = 3, .opponent = {"kyurem"}, .boss = "kyurem", .final_wins = 3},
.expectation = {.accepted = true, .boss = "kyurem", .boss_if_caught = "kyurem"},
},
{
.input = {.name = "identified boss caught", .wins = 3, .opponent = {"kyurem"}, .boss = "", .final_wins = 4},
.expectation = {.accepted = true, .boss = "kyurem", .boss_if_caught = "kyurem"},
},
{
.input = {.name = "boss before three wins must retry", .wins = 2, .opponent = {"kyurem"}, .boss = "", .final_wins = 2},
.expectation = {.accepted = false, .boss = "", .boss_if_caught = ""},
},
// TODO: to be fixed: a boss stage with no opponent read is accepted.
{
.input = {.name = "empty boss-stage read", .wins = 3, .opponent = {}, .boss = "", .final_wins = 3},
.expectation = {.accepted = true, .boss = "", .boss_if_caught = ""},
},
// TODO: to be fixed: a two-candidate read is accepted and the first is taken as the boss.
{
.input = {.name = "ambiguous boss-stage read", .wins = 3, .opponent = {"kyurem", "mimikyu-disguised"}, .boss = "", .final_wins = 3},
.expectation = {.accepted = true, .boss = "kyurem", .boss_if_caught = "kyurem"},
},
// TODO: to be fixed: a rental read at the boss stage is accepted, and the boss is reported caught without a fourth win.
{
.input = {.name = "rental read after known boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "kyurem", .final_wins = 3},
.expectation = {.accepted = true, .boss = "kyurem", .boss_if_caught = "kyurem"},
},
// TODO: to be fixed: a rental read at the boss stage is accepted and the rental is reported as the caught boss.
{
.input = {.name = "fourth win after rental boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "mimikyu-disguised", .final_wins = 4},
.expectation = {.accepted = true, .boss = "mimikyu-disguised", .boss_if_caught = "mimikyu-disguised"},
},
};

}

void add_tests_MaxLairBossState(UnitTestDatabase& database){
for (const BossStateTestCase& scenario : CASES){
database.add<Test_MaxLairBossState>(scenario);
}
}

}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Max Lair State Tests
*
* From: https://github.com/PokemonAutomation/
*
*/

#ifndef PokemonAutomation_PokemonSwSh_MaxLair_State_Tests_H
#define PokemonAutomation_PokemonSwSh_MaxLair_State_Tests_H

#include "Common/Cpp/TestRunners/UnitTestDatabase.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonSwSh{
namespace MaxLairInternal{


void add_tests_MaxLairBossState(UnitTestDatabase& database);


}
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,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)){
if (!state.accept_opponent()){
stream.log("Boss found before 3 wins. Something is seriously out-of-sync.", COLOR_RED);
dump_image(stream.logger(), MODULE_NAME, "BossBeforeEnd", 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].boss_if_caught();
CaughtScreenAction action =
decider.end_adventure_action(
console_index, boss,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "VideoGames/PokemonSwSh/Inference/PokemonSwSh_MainMenuDetector.h"
#include "VideoGames/PokemonSwSh/Inference/PokemonSwSh_YCommDetector.h"
#include "VideoGames/PokemonSwSh/Inference/PokemonSwSh_SelectionArrowFinder.h"
#include "VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.h"
#include "VideoGames/PokemonSwSh/MaxLair/Inference/PokemonSwSh_MaxLair_Detect_BattleMenu.h"
#include "PokemonSwSh_Tests.h"

Expand All @@ -30,6 +31,7 @@ void add_tests(UnitTestDatabase& database){
NintendoSwitch::PokemonSwSh::add_tests_YCommDetector(database);
NintendoSwitch::PokemonSwSh::add_tests_SelectionArrowFinder(database);
NintendoSwitch::PokemonSwSh::MaxLairInternal::add_tests_MaxLairBattleMenuDetector(database);
NintendoSwitch::PokemonSwSh::MaxLairInternal::add_tests_MaxLairBossState(database);
}


Expand Down
2 changes: 2 additions & 0 deletions SerialPrograms/cmake/SourceFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2876,6 +2876,8 @@ file(GLOB LIBRARY_SOURCES
Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_Notifications.h
Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.cpp
Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h
Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.cpp
Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.h
Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_StateMachine.cpp
Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_StateMachine.h
Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_StateTracker.cpp
Expand Down
Loading