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
@@ -0,0 +1,56 @@
/* Place Name Banner Reader
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "CommonFramework/ImageTypes/ImageRGB32.h"
#include "CommonTools/Images/ImageFilter.h"
#include "CommonTools/OCR/OCR_Routines.h"
#include "PokemonSV_PlaceNameBannerReader.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonSV{


// The banner is centered horizontally. On a lab screenshot (2026-10-22,
// Chinese (Traditional), landing at Central Plaza) the name 中心廣場 spans
// x 0.438-0.562, y 0.167-0.222, with the blue line under it at y 0.233 and
// the smaller "Terarium" subtitle below that. The read area stays above the
// line and is wider than the name, so that longer names fit.
PlaceNameBannerReader::PlaceNameBannerReader()
: m_box(0.300, 0.155, 0.400, 0.070)
{}

void PlaceNameBannerReader::make_overlays(VideoOverlaySet& items) const{
items.add(COLOR_BLUE, m_box);
}

std::vector<std::string> PlaceNameBannerReader::read(Logger& logger, Language language, const ImageViewRGB32& screen) const{
// The name is white, drawn straight over the scene. Keep only the
// brightest pixels; the scene behind it changes with the time of day,
// so try a few thresholds.
static const uint32_t MINS[] = {0xffe0e0e0, 0xffc0c0c0};

const ImageViewRGB32 cropped = extract_box_reference(screen, m_box);
std::vector<std::string> ret;
for (uint32_t mins : MINS){
ImageRGB32 filtered = to_blackwhite_rgb32_range(cropped, true, mins, 0xffffffff);
std::string text;
for (char ch : OCR::ocr_read(language, filtered)){
if ((unsigned char)ch < 32){
continue;
}
text += ch;
}
logger.log("Place name banner: \"" + text + "\"");
ret.emplace_back(std::move(text));
}
return ret;
}


}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* Place Name Banner Reader
*
* From: https://github.com/PokemonAutomation/
*
* Read the place name the game shows near the top of the screen after
* landing from a fly, e.g. "Central Plaza" over a blue line.
*
*/

#ifndef PokemonAutomation_PokemonSV_PlaceNameBannerReader_H
#define PokemonAutomation_PokemonSV_PlaceNameBannerReader_H

#include <string>
#include <vector>
#include "Common/Cpp/Logging/AbstractLogger.h"
#include "CommonFramework/Language.h"
#include "CommonFramework/ImageTools/ImageBoxes.h"
#include "CommonFramework/ImageTypes/ImageViewRGB32.h"
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonSV{


class PlaceNameBannerReader{
public:
PlaceNameBannerReader();

void make_overlays(VideoOverlaySet& items) const;

// OCR the banner's white text, once per brightness threshold. Returns
// the raw OCR text of each read, which is empty (or noise) when there
// is no banner.
std::vector<std::string> read(Logger& logger, Language language, const ImageViewRGB32& screen) const;

private:
ImageFloatBox m_box;
};


}
}
}
#endif
252 changes: 252 additions & 0 deletions SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "Inference/Picnics/PokemonSV_SandwichHandDetector.h"
#include "Options/PokemonSV_BBQuests.h"
#include "Programs/Farming/PokemonSV_BlueberryQuests.h"
#include "Programs/PokemonSV_TerariumPlazaReturn.h"
#include "PokemonSV_Tests.h"
#include "CommonFramework/ImageTypes/ImageRGB32.h"
#include "Tests/ImageUnitTest.h"
Expand Down Expand Up @@ -148,7 +149,255 @@

}

namespace{

std::string join_bools(const std::vector<bool>& items){
std::string ret;
for (bool item : items){
if (!ret.empty()){
ret += ", ";
}
ret += item ? "true" : "false";
}
return "{" + ret + "}";
}


struct PlazaBannerToCheckCase{
std::string name;
Language language;
bool ocr_available;
std::string expected;
};

class Test_BBQPlazaBannerToCheck : public UnitTest{
public:
Test_BBQPlazaBannerToCheck(PlazaBannerToCheckCase test_case)
: UnitTest("PokemonSV::BBQPlazaBannerToCheck - " + test_case.name)
, m_case(std::move(test_case))
{}

virtual UnitTestResult run(Logger&, CancellableScope&) const override{
const std::string banner = central_plaza_banner_to_check(m_case.language, m_case.ocr_available);
if (banner != m_case.expected){
return "Expected \"" + m_case.expected + "\", got \"" + banner + "\"";
}
return true;
}

private:
const PlazaBannerToCheckCase m_case;
};

void add_tests_BBQPlazaBannerToCheck(UnitTestDatabase& database){
const PlazaBannerToCheckCase cases[] = {
{.name = "english", .language = Language::English, .ocr_available = true, .expected = "Central Plaza"},
{.name = "chinese traditional", .language = Language::ChineseTraditional, .ocr_available = true, .expected = "中心廣場"},
{.name = "no OCR data", .language = Language::ChineseTraditional, .ocr_available = false, .expected = ""},
{.name = "name not known yet", .language = Language::Japanese, .ocr_available = true, .expected = ""},
{.name = "no language", .language = Language::None, .ocr_available = true, .expected = ""},
};
for (const PlazaBannerToCheckCase& test_case : cases){
database.add<Test_BBQPlazaBannerToCheck>(test_case);
}
}


struct PlazaBannerMatchCase{
std::string name;
std::string expected;
std::string ocr_text;
bool matches;
};

class Test_BBQPlazaBannerMatch : public UnitTest{
public:
Test_BBQPlazaBannerMatch(PlazaBannerMatchCase test_case)
: UnitTest("PokemonSV::BBQPlazaBannerMatch - " + test_case.name)
, m_case(std::move(test_case))
{}

virtual UnitTestResult run(Logger&, CancellableScope&) const override{
const bool matches = place_name_banner_matches(m_case.expected, m_case.ocr_text);
if (matches != m_case.matches){
return "Banner \"" + m_case.ocr_text + "\" against \"" + m_case.expected + "\": expected "
+ (m_case.matches ? "a match" : "no match") + ", got "
+ (matches ? "a match" : "no match");
}
return true;
}

private:
const PlazaBannerMatchCase m_case;
};

void add_tests_BBQPlazaBannerMatch(UnitTestDatabase& database){

Check warning on line 234 in SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSV::add_tests_BBQPlazaBannerMatch( UnitTestDatabase & database): function_lines=28 (advisory above 25, limit 50)
const PlazaBannerMatchCase cases[] = {
{.name = "chinese as seen in the lab", .expected = "中心廣場", .ocr_text = "中心廣場", .matches = true},
{.name = "chinese with spaces", .expected = "中心廣場", .ocr_text = "中 心\xE3\x80\x80廣 場", .matches = true},
{.name = "chinese with noise around it", .expected = "中心廣場", .ocr_text = "|一中心廣場。", .matches = true},
{.name = "chinese, one character misread", .expected = "中心廣場", .ocr_text = "中心廣塲", .matches = true},
{.name = "chinese, one character missing", .expected = "中心廣場", .ocr_text = "中心場", .matches = true},
{.name = "chinese, coastal plaza", .expected = "中心廣場", .ocr_text = "海岸廣場", .matches = false},
{.name = "chinese, savanna plaza", .expected = "中心廣場", .ocr_text = "草原廣場", .matches = false},
{.name = "chinese, canyon plaza", .expected = "中心廣場", .ocr_text = "峽谷廣場", .matches = false},
{.name = "chinese, polar plaza", .expected = "中心廣場", .ocr_text = "極地廣場", .matches = false},
{.name = "chinese, only half the name", .expected = "中心廣場", .ocr_text = "廣場", .matches = false},
{.name = "english", .expected = "Central Plaza", .ocr_text = "Central Plaza", .matches = true},
{.name = "english, case and punctuation", .expected = "Central Plaza", .ocr_text = " CENTRAL plaza.", .matches = true},
{.name = "english, two characters misread", .expected = "Central Plaza", .ocr_text = "Centra1 P1aza", .matches = true},
{.name = "english, coastal plaza", .expected = "Central Plaza", .ocr_text = "Coastal Plaza", .matches = false},
{.name = "english, canyon plaza", .expected = "Central Plaza", .ocr_text = "Canyon Plaza", .matches = false},
{.name = "english, savanna plaza", .expected = "Central Plaza", .ocr_text = "Savanna Plaza", .matches = false},
{.name = "english, polar plaza", .expected = "Central Plaza", .ocr_text = "Polar Plaza", .matches = false},
{.name = "english, chargestone cavern", .expected = "Central Plaza", .ocr_text = "Chargestone Cavern", .matches = false},
{.name = "nothing read", .expected = "中心廣場", .ocr_text = "", .matches = false},
{.name = "only punctuation read", .expected = "Central Plaza", .ocr_text = " .|- ", .matches = false},
{.name = "nothing expected", .expected = "", .ocr_text = "Central Plaza", .matches = false},
};
for (const PlazaBannerMatchCase& test_case : cases){
database.add<Test_BBQPlazaBannerMatch>(test_case);
}
}


// Plays back the banner texts read after each landing, in order. Each
// landing is a list of reads (frames x thresholds), handed to `matches` the
// way the console reads them.
class ScriptedPlazaReturnActions : public PlazaReturnActions{
public:
ScriptedPlazaReturnActions(std::vector<std::vector<std::string>> landings)
: m_landings(std::move(landings))
{}

virtual void fly_to_plaza(bool overshoot) override{
overshoots.push_back(overshoot);
}
virtual std::string read_landing_banner(const std::function<bool(const std::string& text)>& matches) override{
const size_t landing = banner_reads++;
if (landing >= m_landings.size()){
return "";
}
std::string last;
for (const std::string& text : m_landings[landing]){
if (matches(text)){
return text;
}
if (!text.empty()){
last = text;
}
}
return last;
}

public:
std::vector<bool> overshoots;
size_t banner_reads = 0;

private:
const std::vector<std::vector<std::string>> m_landings;
};

class NullLogger : public Logger{
public:
virtual void log(const std::string&, Color) override{}
};


struct PlazaReturnCase{
std::string name;
std::string expected_banner;
std::vector<std::vector<std::string>> landings = {};
PlazaReturnResult result;
std::vector<bool> overshoots;
size_t banner_reads;
};

class Test_BBQPlazaReturn : public UnitTest{
public:
Test_BBQPlazaReturn(PlazaReturnCase test_case)
: UnitTest("PokemonSV::BBQPlazaReturn - " + test_case.name)
, m_case(std::move(test_case))
{}

virtual UnitTestResult run(Logger&, CancellableScope&) const override{
NullLogger logger;
ScriptedPlazaReturnActions actions(m_case.landings);
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)
+ ", got " + plaza_return_result_to_string(result);
}
if (actions.overshoots != m_case.overshoots){
return "Expected flights with overshoot " + join_bools(m_case.overshoots)
+ ", got " + join_bools(actions.overshoots);
}
if (actions.banner_reads != m_case.banner_reads){
return "Expected " + std::to_string(m_case.banner_reads) + " banner reads, got "
+ std::to_string(actions.banner_reads);
}
return true;
}

private:
const PlazaReturnCase m_case;
};

void add_tests_BBQPlazaReturn(UnitTestDatabase& database){

Check warning on line 346 in SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSV::add_tests_BBQPlazaReturn( UnitTestDatabase & database): function_lines=49 (advisory above 25, limit 50)
const PlazaReturnCase cases[] = {
{
.name = "arrives first time",
.expected_banner = "中心廣場",
.landings = {{"", "中心廣場"}},
.result = PlazaReturnResult::ARRIVED,
.overshoots = {false},
.banner_reads = 1,
},
{
// As in the lab on 2026-10-22, when the closest-Pokecenter
// fallback flew to another fly point.
.name = "lands elsewhere, then arrives with overshoot",
.expected_banner = "中心廣場",
.landings = {{"海岸廣場"}, {"中心廣場"}},
.result = PlazaReturnResult::ARRIVED,
.overshoots = {false, true},
.banner_reads = 2,
},
{
.name = "banner not seen, then arrives",
.expected_banner = "Central Plaza",
.landings = {{"", ""}, {"", ""}, {"Central Plaza"}},
.result = PlazaReturnResult::ARRIVED,
.overshoots = {false, true, true},
.banner_reads = 3,
},
{
.name = "never arrives",
.expected_banner = "Central Plaza",
.landings = {{"Coastal Plaza"}, {"Canyon Plaza"}, {""}},
.result = PlazaReturnResult::FAILED,
.overshoots = {false, true, true},
.banner_reads = 3,
},
{
.name = "nothing to check",
.expected_banner = "",
.landings = {{"Coastal Plaza"}},
.result = PlazaReturnResult::NOT_CHECKED,
.overshoots = {false},
.banner_reads = 0,
},
};
for (const PlazaReturnCase& test_case : cases){
database.add<Test_BBQPlazaReturn>(test_case);
}
}

}



void add_tests(UnitTestDatabase& database){

Check warning on line 400 in SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSV::add_tests( UnitTestDatabase & database): function_lines=36 (advisory above 25, limit 50)
database.add<Test_BBQuestsSlugs>();

// Full-frame references, in the CommandLineTests repo: they exercise the production
Expand Down Expand Up @@ -179,6 +428,9 @@
add_tests_TeraTypeReader(database);
add_tests_NormalBattleMenus(database);
add_tests_LetsGoKillDetector(database);
add_tests_BBQPlazaBannerToCheck(database);
add_tests_BBQPlazaBannerMatch(database);
add_tests_BBQPlazaReturn(database);
add_tests_TeraEncounterAction(database);
}

Expand Down
Loading
Loading