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 @@ -22,24 +22,48 @@ bool is_boss_battle(const GlobalState& state){
return state.wins == 3;
}

bool has_boss_candidate(const std::set<std::string>& candidates){
for (const std::string& slug : candidates){
if (is_boss(slug)){
return true;
}
}
return false;
}

} // namespace

bool GlobalState::in_boss_battle_with_known_boss() const{
return is_boss_battle(*this) && is_boss(boss);
}

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)){
if (opponent.empty()){
return false;
}
if (opponent.size() > 1){
// Forms sharing one OCR species name (Lycanroc, Basculin) stay
// ambiguous even after the summary read. That is fine for a rental
// battle as long as no candidate is a boss. It cannot identify the boss.
return wins < 3 && !has_boss_candidate(opponent);
}
const std::string& opponent_slug = *opponent.begin();
// Reject the OCR read if the opponent species doesn't match the current stage:
// a legendary boss read during battles 1-3, or a non-boss rental read in battle 4.
if (is_boss(opponent_slug) != is_boss_battle(*this)){
return false;
}
// Infer the boss if we don't know it.
if (is_boss_battle(*this) && boss.empty()){
if (is_boss_battle(*this) && !in_boss_battle_with_known_boss()){
boss = opponent_slug;
}
return true;
}

std::string GlobalState::boss_if_caught() const{
// The identified boss is reported whether or not it was caught.
return boss;
// A failed adventure (e.g. wins == 3, only lost to the boss) can still have
// an identified boss, but no boss catch: catching the boss requires winning
// the fourth battle (wins == 4).
return wins == 4 && is_boss(boss) ? boss : "";
}

std::string PlayerState::dump() const{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,22 @@ 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.
// True during the fourth (boss) battle when a boss has already been identified.
bool in_boss_battle_with_known_boss() const;

// Accepts one opponent matching this stage. Before the fourth battle,
// also accepts an ambiguous read with no boss candidate, preserving the
// candidate set. Boss-stage reads still need one boss; records it unless
// a boss is already known. Returns false for a read that doesn't fit:
// the caller retries it through the summary, and keeps going if it
// still doesn't fit. A misread is not a reason to reset the adventure.
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 @@ -56,6 +56,9 @@ class Test_MaxLairBossState : public UnitTest{
if (state.boss != expected.boss){
return std::format("Expected boss '{}', got '{}'", expected.boss, state.boss);
}
if (state.opponent != input.opponent){
return "Expected opponent candidates to be preserved.";
}
state.wins = input.final_wins;
const std::string caught = state.boss_if_caught();
if (caught != expected.boss_if_caught){
Expand All @@ -68,26 +71,55 @@ class Test_MaxLairBossState : public UnitTest{
BossStateTestCase m_scenario;
};

// Cases marked TODO record the current, wrong behaviour.
const BossStateTestCase CASES[] = {
{
.input = {.name = "form ambiguity is only valid during rental battles", .wins = 4, .opponent = {"lycanroc-midday", "lycanroc-midnight"}, .boss = "nihilego", .final_wins = 4},
.expectation = {.accepted = false, .boss = "nihilego", .boss_if_caught = "nihilego"},
},
{
.input = {.name = "Lycanroc forms at battle two", .wins = 1, .opponent = {"lycanroc-midday", "lycanroc-midnight"}, .boss = "nihilego", .final_wins = 1},
.expectation = {.accepted = true, .boss = "nihilego", .boss_if_caught = ""},
},
{
.input = {.name = "Basculin forms at battle three", .wins = 2, .opponent = {"basculin-blue-striped", "basculin-red-striped"}, .boss = "nihilego", .final_wins = 2},
.expectation = {.accepted = true, .boss = "nihilego", .boss_if_caught = ""},
},
{
.input = {.name = "rental forms cannot pass boss stage", .wins = 3, .opponent = {"lycanroc-midday", "lycanroc-midnight"}, .boss = "", .final_wins = 3},
.expectation = {.accepted = false, .boss = "", .boss_if_caught = ""},
},
{
.input = {.name = "two different rentals at battle two", .wins = 1, .opponent = {"lycanroc-midday", "magmar"}, .boss = "nihilego", .final_wins = 1},
.expectation = {.accepted = true, .boss = "nihilego", .boss_if_caught = ""},
},
{
.input = {.name = "mixed boss and rental cannot pass rental stage", .wins = 1, .opponent = {"lycanroc-midday", "nihilego"}, .boss = "nihilego", .final_wins = 1},
.expectation = {.accepted = false, .boss = "nihilego", .boss_if_caught = ""},
},
{
.input = {.name = "empty rental read still rejects", .wins = 1, .opponent = {}, .boss = "nihilego", .final_wins = 1},
.expectation = {.accepted = false, .boss = "nihilego", .boss_if_caught = ""},
},
{
.input = {.name = "multiple boss candidates cannot infer a boss", .wins = 3, .opponent = {"kyurem", "nihilego"}, .boss = "", .final_wins = 3},
.expectation = {.accepted = false, .boss = "", .boss_if_caught = ""},
},

{
.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"},
.input = {.name = "Mimikyu at boss stage must retry", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "", .final_wins = 3},
.expectation = {.accepted = false, .boss = "", .boss_if_caught = ""},
},
// 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"},
.expectation = {.accepted = true, .boss = "kyurem", .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 @@ -97,25 +129,21 @@ 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 = ""},
.input = {.name = "empty boss-stage read must retry", .wins = 3, .opponent = {}, .boss = "", .final_wins = 3},
.expectation = {.accepted = false, .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"},
.input = {.name = "ambiguous boss-stage read must retry", .wins = 3, .opponent = {"kyurem", "mimikyu-disguised"}, .boss = "", .final_wins = 3},
.expectation = {.accepted = false, .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"},
.input = {.name = "rejected read preserves known boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "kyurem", .final_wins = 3},
.expectation = {.accepted = false, .boss = "kyurem", .boss_if_caught = ""},
},
// 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"},
.input = {.name = "fourth win cannot legitimize rental boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "mimikyu-disguised", .final_wins = 4},
.expectation = {.accepted = false, .boss = "mimikyu-disguised", .boss_if_caught = ""},
},
};

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.accept_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.in_boss_battle_with_known_boss()){
if (!state.opponent.empty() && *state.opponent.begin() != state.boss){
stream.log(
"Inconsistent Boss: Expected " + state.boss + ", Read: " + *state.opponent.begin(),
Expand All @@ -110,10 +107,10 @@ bool read_battle_menu(
context.wait_for_all_requests();


// Probably a misread. It only costs move choice; a reset would cost the adventure.
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;
stream.log("Opponent does not match the battle stage after summary read. Continuing.", COLOR_RED);
dump_image(stream.logger(), MODULE_NAME, "UnexpectedBattleOpponent", snapshot);
}

// Read misc.
Expand Down
Loading