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
46 changes: 45 additions & 1 deletion SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,10 @@
: m_landings(std::move(landings))
{}

virtual void fly_to_plaza(bool overshoot) override{
virtual bool fly_to_plaza(bool overshoot) override{
const size_t flight = overshoots.size();
overshoots.push_back(overshoot);
return flight >= fly_results.size() || fly_results[flight];
}
virtual std::string read_landing_banner(const std::function<bool(const std::string& text)>& matches) override{
const size_t landing = banner_reads++;
Expand All @@ -291,6 +293,8 @@
}

public:
// Whether each fly happened; flies past the end do.
std::vector<bool> fly_results;
std::vector<bool> overshoots;
size_t banner_reads = 0;

Expand All @@ -308,6 +312,7 @@
std::string name;
std::string expected_banner;
std::vector<std::vector<std::string>> landings = {};
std::vector<bool> fly_results = {};
PlazaReturnResult result;
std::vector<bool> overshoots;
size_t banner_reads;
Expand All @@ -323,6 +328,7 @@
virtual UnitTestResult run(Logger&, CancellableScope&) const override{
NullLogger logger;
ScriptedPlazaReturnActions actions(m_case.landings);
actions.fly_results = m_case.fly_results;
const PlazaReturnResult result = return_to_plaza_checked(logger, actions, m_case.expected_banner, 3);
if (result != m_case.result){
return "Expected result " + plaza_return_result_to_string(m_case.result)
Expand All @@ -343,7 +349,7 @@
const PlazaReturnCase m_case;
};

void add_tests_BBQPlazaReturn(UnitTestDatabase& database){

Check failure on line 352 in SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

PokemonAutomation::NintendoSwitch::PokemonSV::add_tests_BBQPlazaReturn( UnitTestDatabase & database): function_lines=87, limit=50, allowed 50
const PlazaReturnCase cases[] = {
{
.name = "arrives first time",
Expand Down Expand Up @@ -387,6 +393,44 @@
.overshoots = {false},
.banner_reads = 0,
},
{
// No Pokecenter to fly to, or a battle on the way: no landing to
// read, and the next attempt overshoots.
.name = "a fly that didn't happen uses an attempt, then arrives",
.expected_banner = "中心廣場",
.landings = {{"中心廣場"}},
.fly_results = {false},
.result = PlazaReturnResult::ARRIVED,
.overshoots = {false, true},
.banner_reads = 1,
},
{
// bbq-m5 11:20: the fly kept failing and the return looped for
// minutes. Now it fails after 3 flies.
.name = "flies that never happen fail the return",
.expected_banner = "中心廣場",
.fly_results = {false, false, false},
.result = PlazaReturnResult::FAILED,
.overshoots = {false, true, true},
.banner_reads = 0,
},
{
.name = "a failed fly and a wrong landing both count",
.expected_banner = "Central Plaza",
.landings = {{"Coastal Plaza"}},
.fly_results = {false, true, false},
.result = PlazaReturnResult::FAILED,
.overshoots = {false, true, true},
.banner_reads = 1,
},
{
.name = "nothing to check retries a fly that didn't happen",
.expected_banner = "",
.fly_results = {false},
.result = PlazaReturnResult::NOT_CHECKED,
.overshoots = {false, true},
.banner_reads = 0,
},
};
for (const PlazaReturnCase& test_case : cases){
database.add<Test_BBQPlazaReturn>(test_case);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@

namespace{

// Flies to the closest Pokecenter on the map. Nothing here checks where the
// player lands: the closest-Pokecenter search can fall back to any fly point.
void fly_toward_plaza(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, bool laggy){
// Flies to the closest Pokecenter on the map, once. Nothing here checks where
// the player lands: the closest-Pokecenter search can fall back to any fly
// point. Returns false if the fly failed or a battle interrupted it; the
// caller decides whether to try again, so one return can't loop for minutes.
bool fly_toward_plaza(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, bool laggy){

Check failure on line 36 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

PokemonAutomation::NintendoSwitch::PokemonSV::fly_toward_plaza( const ProgramInfo & info , VideoStream & stream , ProControllerContext & context , bool laggy): function_lines=90, limit=50, allowed 87
stream.log("Attempting to return to Central Plaza.");
//Modified version of handle_battles_and_back_to_pokecenter()
bool returned_to_pokecenter = false;

while(!returned_to_pokecenter){
{
EncounterWatcher encounter_watcher(stream, COLOR_RED);
int ret = run_until<ProControllerContext>(
stream, context,
Expand Down Expand Up @@ -74,13 +76,15 @@
pbf_press_button(context, BUTTON_ZR, 320ms, 800ms);

try{
//The only pokecenter on the map is Central Plaza
fly_to_closest_pokecenter_on_map(info, stream, context);
//The only pokecenter on the map is Central Plaza. Each failed search
//costs ~13 s, and repeating the same one rarely helps (bbq-m5 11:20:
//17 identical "no Pokecenter" tries), so give up after 3.
fly_to_closest_pokecenter_on_map(info, stream, context, 3);
context.wait_for_all_requests();
returned_to_pokecenter = true;
}catch (...){
stream.log("Failed to return to Pokecenter. Closing map and retrying.");
laggy = true;
//The next try mashes B back to the overworld first.
stream.log("Failed to return to Pokecenter.", COLOR_RED);
}
context.wait_for_all_requests();
},
Expand Down Expand Up @@ -117,6 +121,7 @@
}
}
context.wait_for_all_requests();
return returned_to_pokecenter;
}

class PlazaReturnConsoleActions : public PlazaReturnActions{
Expand All @@ -131,8 +136,8 @@
, m_language(language)
{}

virtual void fly_to_plaza(bool overshoot) override{
fly_toward_plaza(m_info, m_stream, m_context, overshoot);
virtual bool fly_to_plaza(bool overshoot) override{
return fly_toward_plaza(m_info, m_stream, m_context, overshoot);
}

// The banner fades in shortly after the overworld is back and stays for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
return "?";
}

PlazaReturnResult return_to_plaza_checked(

Check warning on line 83 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumPlazaReturn.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSV::return_to_plaza_checked( Logger & logger , PlazaReturnActions & actions , const std :: string & expected_banner , size_t max_attempts): function_lines=42 (advisory above 25, limit 50)
Logger& logger, PlazaReturnActions& actions,
const std::string& expected_banner,
size_t max_attempts
Expand All @@ -91,7 +91,10 @@

// A wrong landing usually means the cursor stopped short of Central
// Plaza, so retries overshoot from the start.
actions.fly_to_plaza(attempt > 1);
if (!actions.fly_to_plaza(attempt > 1)){
logger.log(prefix + "the fly didn't happen.", COLOR_RED);
continue;
}

if (expected_banner.empty()){
logger.log(prefix + "landed. Not checking where: no place name to check in this language.", COLOR_ORANGE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ class PlazaReturnActions{
// Fly toward Central Plaza the way return_to_plaza() always has: map
// cursor to the top-left corner, back toward the center, then the
// closest Pokecenter. "overshoot" moves the cursor back further, which
// helps when the game lags.
virtual void fly_to_plaza(bool overshoot) = 0;
// helps when the game lags. Tries once: returns false if it didn't fly
// (no Pokecenter to fly to, or a battle interrupted it).
virtual bool fly_to_plaza(bool overshoot) = 0;

// Watch the place-name banner shown after landing. Returns the first
// text read that `matches` accepts, else the last non-empty text read
Expand All @@ -57,7 +58,8 @@ enum class PlazaReturnResult{
std::string plaza_return_result_to_string(PlazaReturnResult result);

// Fly toward Central Plaza and check the landing banner, up to
// `max_attempts` times. `expected_banner` comes from
// `max_attempts` times. A fly that didn't happen uses up an attempt too, so
// the return is bounded. `expected_banner` comes from
// central_plaza_banner_to_check(); when it is empty the first landing is
// accepted unchecked.
PlazaReturnResult return_to_plaza_checked(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* PokemonSV World Navigation

Check failure on line 1 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

<file>: file_lines=1069, limit=1000, allowed 1067
*
*/

#include <algorithm>
#include <array>
#include <cmath>
#include <sstream>
Expand Down Expand Up @@ -532,8 +533,9 @@
}


void fly_to_closest_pokecenter_on_map(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){
void fly_to_closest_pokecenter_on_map(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, int max_tries){

Check failure on line 536 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

PokemonAutomation::NintendoSwitch::PokemonSV::fly_to_closest_pokecenter_on_map( const ProgramInfo & info , VideoStream & stream , ProControllerContext & context , int max_tries): function_lines=92, limit=50, allowed 91
const int MAX_TRY_COUNT = 17;
const int try_limit = std::clamp(max_tries, 1, MAX_TRY_COUNT);
int try_count = 0;
// Part 1: Tries to detect a pokecenter that is very close to the player
// Zoom in one level onto the map.
Expand All @@ -557,7 +559,7 @@
break;
}catch (OperationFailedException&){ // pokecenter was detected, but failed to fly there
try_count++;
if (try_count >= MAX_TRY_COUNT){
if (try_count >= try_limit){
OperationFailedExceptionWithScreenshot::fire(
ErrorReportMode::SEND_ERROR_REPORT,
"fly_to_closest_pokecenter_on_map(): At min warpable map level, pokecenter was detected, but failed to fly there.",
Expand Down Expand Up @@ -609,7 +611,7 @@
}
}catch (OperationFailedException&){
try_count++;
if (try_count >= MAX_TRY_COUNT){
if (try_count >= try_limit){
// either:
// - pokecenter was detected, but failed to fly there.
// - could not find pokecenter icon.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ bool fly_to_visible_closest_flypoint_cur_zoom_level(
// While on map (default zoom), move to the closest PokeCenter and fly there.
// The PokeCenter must be already visited before (so having the little wing icon with it) and not occluded
// by other map icons on the most zoomed-in level of the map.
void fly_to_closest_pokecenter_on_map(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context);
// Each of its two searches gives up after `max_tries` failed flies (at most 17).
void fly_to_closest_pokecenter_on_map(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, int max_tries = 17);

// Attempt to escape being stuck on a wall.
// Repeatedly center camera and try to backwards jump off.
Expand Down
Loading