Skip to content
Merged
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 @@ -24,24 +24,33 @@ bool is_boss_battle(const GlobalState& state){

} // namespace

bool GlobalState::accept_opponent(){
bool GlobalState::opponent_fits_stage() const{
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;
if (!is_boss_battle(*this)){
return !is_boss(opponent_slug);
}
if (!boss.empty()){
return opponent.size() == 1 && opponent_slug == boss;
}
// A rental read here is a misread (Kyurem read as Mimikyu, 2026-11-02).
return opponent.size() == 1 && is_boss(opponent_slug);
}

bool GlobalState::accept_opponent(){
const bool fits = opponent_fits_stage();
if (!is_boss_battle(*this)){
// A boss before the end means something is out-of-sync.
return fits;
}
// Infer the boss if we don't know it. Only a single boss species counts:
// a rental read here is a misread (Kyurem read as Mimikyu, 2026-11-02)
// and must not become the boss. A later turn's read can still record it.
if (is_boss_battle(*this) && boss.empty() && opponent.size() == 1 && is_boss(opponent_slug)){
boss = opponent_slug;
if (boss.empty() && fits){
boss = *opponent.begin();
}
return true;
}

std::string GlobalState::boss_if_caught() const{
// The identified boss is reported whether or not it was caught.
return boss;
// The boss is caught only if the fourth battle was won.
return wins == 4 ? boss : "";
}

std::string PlayerState::dump() const{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,21 @@ struct GlobalState{
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
// and the read is a single boss species.
// Whether the opponent read fits this battle stage: before the fourth
// battle, any non-boss; at the fourth battle, exactly the known boss, or a
// single boss species when no boss is known yet.
bool opponent_fits_stage() const;

// Returns false when the read doesn't fit a battle before the fourth (a
// boss there), so the caller retries the read. At the fourth battle,
// records the boss if none is known and the read fits.
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
Expand Up @@ -68,7 +68,6 @@ class Test_MaxLairBossState : public UnitTest{
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},
Expand All @@ -86,12 +85,11 @@ const BossStateTestCase CASES[] = {
// is already known is kept.
{
.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"},
.expectation = {.accepted = true, .boss = "mimikyu-disguised", .boss_if_caught = ""},
},
// 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"},
.expectation = {.accepted = true, .boss = "kyurem", .boss_if_caught = ""},
},
{
.input = {.name = "identified boss caught", .wins = 3, .opponent = {"kyurem"}, .boss = "", .final_wins = 4},
Expand All @@ -101,7 +99,6 @@ const BossStateTestCase CASES[] = {
.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 = ""},
Expand All @@ -110,24 +107,100 @@ const BossStateTestCase CASES[] = {
.input = {.name = "ambiguous boss-stage read is not the boss", .wins = 3, .opponent = {"kyurem", "mimikyu-disguised"}, .boss = "", .final_wins = 3},
.expectation = {.accepted = true, .boss = "", .boss_if_caught = ""},
},
// 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"},
.expectation = {.accepted = true, .boss = "kyurem", .boss_if_caught = ""},
},
};

struct FitsStageTestCaseInput{
std::string name;
uint8_t wins;
std::set<std::string> opponent;
std::string boss;
};

struct FitsStageTestCaseExpectation{
bool fits;
};

struct FitsStageTestCase{
FitsStageTestCaseInput input;
FitsStageTestCaseExpectation expectation;
};

class Test_MaxLairFitsStage : public UnitTest{
public:
explicit Test_MaxLairFitsStage(FitsStageTestCase scenario)
: UnitTest(std::format("PokemonSwSh::MaxLairFitsStage - {}", scenario.input.name))
, m_scenario(std::move(scenario))
{}

virtual UnitTestResult run(Logger&, CancellableScope&) const override{
const FitsStageTestCaseInput& input = m_scenario.input;
const FitsStageTestCaseExpectation& expected = m_scenario.expectation;
GlobalState state;
state.wins = input.wins;
state.opponent = input.opponent;
state.boss = input.boss;
const bool fits = state.opponent_fits_stage();
if (fits != expected.fits){
return std::format("Expected fits={}, got {}", expected.fits, fits);
}
return true;
}

private:
FitsStageTestCase m_scenario;
};

const FitsStageTestCase FITS_STAGE_CASES[] = {
{
.input = {.name = "rental before three wins", .wins = 2, .opponent = {"mimikyu-disguised"}, .boss = ""},
.expectation = {.fits = true},
},
{
.input = {.name = "boss before three wins", .wins = 2, .opponent = {"kyurem"}, .boss = ""},
.expectation = {.fits = false},
},
{
.input = {.name = "rental at boss stage with unknown boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = ""},
.expectation = {.fits = false},
},
// TODO: a rental boss recorded before this fix is still reported as caught.
{
.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"},
.input = {.name = "empty read at boss stage with unknown boss", .wins = 3, .opponent = {}, .boss = ""},
.expectation = {.fits = false},
},
{
.input = {.name = "ambiguous read at boss stage with unknown boss", .wins = 3, .opponent = {"kyurem", "mimikyu-disguised"}, .boss = ""},
.expectation = {.fits = false},
},
{
.input = {.name = "boss at boss stage", .wins = 3, .opponent = {"kyurem"}, .boss = ""},
.expectation = {.fits = true},
},
{
.input = {.name = "rental at boss stage with known boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "kyurem"},
.expectation = {.fits = false},
},
{
.input = {.name = "known boss at boss stage", .wins = 3, .opponent = {"kyurem"}, .boss = "kyurem"},
.expectation = {.fits = true},
},
{
.input = {.name = "other boss at boss stage with known boss", .wins = 3, .opponent = {"zekrom"}, .boss = "kyurem"},
.expectation = {.fits = false},
},
};

}

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

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bool process_header_read(
){
if (mon.size() == 1){
state.opponent = std::move(mon);
return true;
return state.opponent_fits_stage();
}
if (mon.size() > 1){
logger.log("Ambiguous Read Result: " + set_to_str(mon), COLOR_PURPLE);
Expand All @@ -41,15 +41,15 @@ bool process_header_read(
"Using previous known value to disambiguate: " + set_to_str(mon),
COLOR_PURPLE
);
return true;
return state.opponent_fits_stage();
}
}
if (state.opponent.size() == 1){
logger.log(
"Failed to read opponent from battle. Using previously known value: " + set_to_str(state.opponent),
COLOR_ORANGE
);
return true;
return state.opponent_fits_stage();
}

return false;
Expand All @@ -75,14 +75,6 @@ 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)){
retry = true;
}

VideoSnapshot snapshot;
if (retry){
Expand Down Expand Up @@ -363,14 +355,14 @@ StateMachineAction throw_balls(
state.players[2].health.value.dead = 0;
state.players[3].health.value.dead = 0;


GlobalState inferred = state_tracker.synchronize(stream.logger(), console_index);


std::string ball;

bool boss = inferred.wins == 4;
if (boss){
// This will crash if `inferred.boss` is still empty after battle-screen
// and the fallback summary read (OCR misses).
ball = decider.boss_ball(console_index, inferred.boss);
}else{
ball = decider.normal_ball(console_index);
Expand Down
Loading