Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* Money Box Reader
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "CommonFramework/ImageTools/ImageBoxes.h"
#include "CommonTools/OCR/OCR_NumberReader.h"
#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h"
#include "PokemonFRLG_MoneyBoxReader.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonFRLG{


namespace{

// The amount's digits in the game's 240 x 160 screen, from right of the
// POKEDOLLAR sign to the box's right edge: x 44.5 to 75, y 22 to 33
// (measured on a Fuchsia Mart BUY list capture, "MONEY / P 10586":
// digits at x 47..71, y 24..31).
const ImageFloatBox MONEY_DIGITS(44.5 / 240, 22.0 / 160, 30.5 / 240, 11.0 / 160);
// The most the game holds (money.c MAX_MONEY).
constexpr int MAX_MONEY = 999999;

} // namespace


std::optional<size_t> read_money_box(Logger& logger, const ImageViewRGB32& screen){
if (screen.width() == 0 || screen.height() == 0){
return std::nullopt;
}
const ImageViewRGB32 game = extract_box_reference(screen, GameSettings::instance().GAME_BOX);
const int money = OCR::read_number(logger, extract_box_reference(game, MONEY_DIGITS));
if (money < 0 || money > MAX_MONEY){
return std::nullopt;
}
return (size_t)money;
}


} // namespace PokemonFRLG
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* Money Box Reader
*
* From: https://github.com/PokemonAutomation/
*
* Reads the money box a Mart's BUY menu shows on the top left: "MONEY",
* and below it the amount after the POKEDOLLAR sign, right-aligned
* (pokefirered money.c PrintMoneyAmountInMoneyBox).
*
*/

#ifndef PokemonAutomation_PokemonFRLG_MoneyBoxReader_H
#define PokemonAutomation_PokemonFRLG_MoneyBoxReader_H

#include <cstddef>
#include <optional>
#include "Common/Cpp/Logging/AbstractLogger.h"
#include "CommonFramework/ImageTypes/ImageViewRGB32.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonFRLG{


// The money the box on `screen` (the whole frame) shows; nothing if it
// can't be read.
std::optional<size_t> read_money_box(Logger& logger, const ImageViewRGB32& screen);


} // namespace PokemonFRLG
} // namespace NintendoSwitch
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*
*/

#include <cctype>
#include <optional>
#include <string_view>
#include <format>
#include "Common/Cpp/Color.h"
Expand All @@ -25,6 +27,75 @@ namespace PokemonFRLG{

namespace{

// A place the save card can name: the pokefirered map name its maps start
// with ("Route4" for "Route4" and "Route4_PokemonCenter_1F"), the slug
// read_save_card_location() returns, and the card's text in English (the
// region map section's name in pokefirered's region_map_sections.json).
struct SaveCardPlace{
std::string_view map_prefix;
std::string_view slug;
std::string_view card_name;
};

// Every Kanto place: the save card's names and the maps they are on come
// from this one table, so a place the story saves in is one the reader
// knows (19.04 saved in Victory Road, which the reader didn't have:
// console B, 2026-11-15).
constexpr SaveCardPlace SAVE_CARD_PLACES[] = {
{"PalletTown", "pallet-town", "Pallet Town"},
{"ViridianCity", "viridian-city", "Viridian City"},
{"PewterCity", "pewter-city", "Pewter City"},
{"CeruleanCity", "cerulean-city", "Cerulean City"},
{"LavenderTown", "lavender-town", "Lavender Town"},
{"VermilionCity", "vermilion-city", "Vermilion City"},
{"CeladonCity", "celadon-city", "Celadon City"},
{"FuchsiaCity", "fuchsia-city", "Fuchsia City"},
{"CinnabarIsland", "cinnabar-island", "Cinnabar Island"},
{"IndigoPlateau", "indigo-plateau", "Indigo Plateau"},
{"SaffronCity", "saffron-city", "Saffron City"},
{"Route1", "route-1", "Route 1"},
{"Route2", "route-2", "Route 2"},
{"Route3", "route-3", "Route 3"},
{"Route4", "route-4", "Route 4"},
{"Route5", "route-5", "Route 5"},
{"Route6", "route-6", "Route 6"},
{"Route7", "route-7", "Route 7"},
{"Route8", "route-8", "Route 8"},
{"Route9", "route-9", "Route 9"},
{"Route10", "route-10", "Route 10"},
{"Route11", "route-11", "Route 11"},
{"Route12", "route-12", "Route 12"},
{"Route13", "route-13", "Route 13"},
{"Route14", "route-14", "Route 14"},
{"Route15", "route-15", "Route 15"},
{"Route16", "route-16", "Route 16"},
{"Route17", "route-17", "Route 17"},
{"Route18", "route-18", "Route 18"},
{"Route19", "route-19", "Route 19"},
{"Route20", "route-20", "Route 20"},
{"Route21", "route-21", "Route 21"},
{"Route22", "route-22", "Route 22"},
{"Route23", "route-23", "Route 23"},
{"Route24", "route-24", "Route 24"},
{"Route25", "route-25", "Route 25"},
{"ViridianForest", "viridian-forest", "Viridian Forest"},
{"MtMoon", "mt-moon", "Mt. Moon"},
{"SSAnne", "ss-anne", "S.S. Anne"},
{"UndergroundPath", "underground-path", "Underground Path"},
{"DiglettsCave", "digletts-cave", "Diglett's Cave"},
{"VictoryRoad", "victory-road", "Victory Road"},
{"RocketHideout", "rocket-hideout", "Rocket Hideout"},
{"SilphCo", "silph-co", "Silph Co."},
{"PokemonMansion", "pokemon-mansion", "Pokemon Mansion"},
{"SafariZone", "safari-zone", "Safari Zone"},
{"PokemonLeague", "pokemon-league", "Pokemon League"},
{"RockTunnel", "rock-tunnel", "Rock Tunnel"},
{"SeafoamIslands", "seafoam-islands", "Seafoam Islands"},
{"PokemonTower", "pokemon-tower", "Pokemon Tower"},
{"CeruleanCave", "cerulean-cave", "Cerulean Cave"},
{"PowerPlant", "power-plant", "Power Plant"},
};

// The title's blue, about (30, 80, 225) on the capture.
const std::vector<OCR::TextColorRange>& BLUE_TEXT_FILTERS(){
static const std::vector<OCR::TextColorRange> filters{
Expand Down Expand Up @@ -63,37 +134,10 @@ class PlaceNameMatcher : public OCR::SmallDictionaryMatcher{

static JsonObject dictionary(){
JsonObject english;
for (const auto& [slug, name] : {
std::pair<std::string_view, std::string_view>{"pallet-town", "Pallet Town"},
{"viridian-city", "Viridian City"},
{"viridian-forest", "Viridian Forest"},
{"pewter-city", "Pewter City"},
{"route-1", "Route 1"},
{"route-2", "Route 2"},
{"route-3", "Route 3"},
{"route-4", "Route 4"},
{"cerulean-city", "Cerulean City"},
{"route-21", "Route 21"},
{"route-22", "Route 22"},
{"route-23", "Route 23"},
{"route-24", "Route 24"},
{"route-25", "Route 25"},
{"route-10", "Route 10"},
{"lavender-town", "Lavender Town"},
{"vermilion-city", "Vermilion City"},
{"celadon-city", "Celadon City"},
{"rocket-hideout", "Rocket Hideout"},
{"pokemon-tower", "Pokemon Tower"},
{"route-16", "Route 16"},
{"fuchsia-city", "Fuchsia City"},
{"saffron-city", "Saffron City"},
{"cinnabar-island", "Cinnabar Island"},
{"silph-co", "Silph Co"},
{"pokemon-mansion", "Pokemon Mansion"},
}){
for (const SaveCardPlace& place : SAVE_CARD_PLACES){
JsonArray names;
names.push_back(std::string(name));
english[std::string(slug)] = std::move(names);
names.push_back(std::string(place.card_name));
english[std::string(place.slug)] = std::move(names);
}
JsonObject json;
json["eng"] = std::move(english);
Expand All @@ -108,6 +152,17 @@ ImageFloatBox save_card_title_box(){
return ImageFloatBox(8.0 / 240, 9.0 / 160, 114.0 / 240, 13.0 / 160);
}

std::optional<std::string> save_card_place(std::string_view map){
for (const SaveCardPlace& place : SAVE_CARD_PLACES){
if (map.starts_with(place.map_prefix)
&& (map.size() == place.map_prefix.size() || map[place.map_prefix.size()] == '_')
){
return std::string(place.slug);
}
}
return std::nullopt;
}

std::string read_save_card_location(Logger& logger, Language language, const ImageViewRGB32& screen){
const ImageViewRGB32 game = extract_box_reference(screen, GameSettings::instance().GAME_BOX);
return PlaceNameMatcher::instance().read(logger, language, extract_box_reference(game, save_card_title_box()));
Expand Down Expand Up @@ -145,9 +200,60 @@ class Test_SaveCard : public ImageUnitTest{
}
};

// OCR text off the card, matched against the places' names.
struct SaveCardTextCase{
std::string_view name;
struct Input{
std::string_view text;
} input;
struct Expectation{
std::string_view slug;
} expectation;
};

const SaveCardTextCase SAVE_CARD_TEXT_CASES[] = {
// 19.04's card in Victory Road (console B, 2026-11-15).
{.name = "a misread D", .input = {.text = "VICTORY ROAO"}, .expectation = {.slug = "victory-road"}},
{.name = "a one-digit route among two-digit ones", .input = {.text = "ROUTE 1"}, .expectation = {.slug = "route-1"}},
{.name = "a two-digit route", .input = {.text = "ROUTE 22"}, .expectation = {.slug = "route-22"}},
};

class Test_SaveCardText : public UnitTest{
public:
Test_SaveCardText()
: UnitTest("PokemonFRLG::SaveCardReader - text")
{}

virtual UnitTestResult run(Logger&, CancellableScope&) const override{
auto best = [](std::string_view text) -> std::string{
const OCR::StringMatchResult result = PlaceNameMatcher::instance().match_substring(Language::English, std::string(text));
return result.results.empty() ? "" : result.results.begin()->second.token;
};
for (const SaveCardTextCase& c : SAVE_CARD_TEXT_CASES){
const std::string read = best(c.input.text);
if (read != c.expectation.slug){
return std::format("{}: \"{}\" read as \"{}\", expected {}.", c.name, c.input.text, read, c.expectation.slug);
}
}
// Every place a save can be in is one the card's text is read as.
for (const SaveCardPlace& place : SAVE_CARD_PLACES){
std::string upper(place.card_name);
for (char& ch : upper){
ch = (char)std::toupper((unsigned char)ch);
}
const std::string read = best(upper);
if (read != place.slug){
return std::format("\"{}\" read as \"{}\", expected {}.", upper, read, place.slug);
}
}
return true;
}
};

} // namespace

void add_tests_SaveCardReader(UnitTestDatabase& database){
database.add<Test_SaveCardText>();
// FireRed on console B, 2026-11-02. The card covers the same corner of
// the screen everywhere; the places behind it differ.
database.add<Test_SaveCard>("PokemonFRLG/SaveCardReader/Bedroom_eng_pallet-town.png");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
*
* Reads where the game would be saved: the place name in blue at the top
* of the card the START menu's SAVE shows ("PALLET TOWN" over PLAYER,
* BADGES and TIME). It knows the places from a new game to Route 4,
* Cerulean City, Route 24 and Route 25 (Bill's Sea Cottage is on it), in
* English only so far: in another language nothing is read. Route 3,
* Route 4, Cerulean City, Route 24 and Route 25 have no test capture yet.
* BADGES and TIME). It knows every Kanto place, in English only so far:
* in another language nothing is read. Only Pallet Town, Viridian Forest
* and Pewter City have a test capture yet.
*
*/

#ifndef PokemonAutomation_PokemonFRLG_SaveCardReader_H
#define PokemonAutomation_PokemonFRLG_SaveCardReader_H

#include <optional>
#include <string>
#include <string_view>
#include "Common/Cpp/Logging/AbstractLogger.h"
#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
#include "CommonFramework/ImageTools/ImageBoxes.h"
Expand All @@ -31,13 +32,17 @@ namespace PokemonFRLG{
// spans x 31 to 96, y 11 to 19 of the 240x160 screen, centered.
ImageFloatBox save_card_title_box();

// The place on the save card: "pallet-town", "viridian-city",
// "viridian-forest", "pewter-city", "route-1" to "route-4", "cerulean-city",
// "route-10", "route-24", "route-25", "vermilion-city",
// "lavender-town"; empty if it can't be read, as
// in a language the reader has no names for.
// The place on the save card, as a slug ("pallet-town", "route-22",
// "victory-road"); empty if it can't be read, as in a language the reader
// has no names for.
std::string read_save_card_location(Logger& logger, Language language, const ImageViewRGB32& screen);

// The slug read_save_card_location() returns for a save on `map`, a
// pokefirered map name: a place's map or a building in it
// ("Route4_PokemonCenter_1F" is on Route 4; "Route24" isn't on Route 2).
// Nullopt for a map outside Kanto.
std::optional<std::string> save_card_place(std::string_view map);


// Registers save card reader unit tests.
void add_tests_SaveCardReader(UnitTestDatabase& database);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using Pokemon::Outcome;
namespace{

Outcome mart(Logger& logger, const MartPlan& plan, StoryAbilities& abilities){
const MartResult result = run_mart(logger, abilities.mart, plan);
const MartResult result = run_mart(logger, abilities.mart, abilities.mart_sell, plan);
const std::string message = mart_message(plan, result);
logger.log(message, result.end == MartEnd::DONE ? COLOR_BLACK : COLOR_RED);
return result.end == MartEnd::DONE ? Pokemon::outcome_ok() : Pokemon::outcome_failed(std::format("mart_{}", mart_end_name(result.end)), message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace PokemonFRLG{
struct StoryAbilities{
MemberReader members;
MartActions& mart;
MartSellActions& mart_sell;
TeachActions& teach;
GrindActions& grind;
ChoiceActions& choice;
Expand Down
Loading
Loading