Skip to content
Open
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,126 @@
/* Dialog Text Reader
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <ctype.h>
#include "Common/Cpp/Color.h"
#include "Common/Cpp/Logging/AbstractLogger.h"
#include "CommonFramework/GlobalSettingsPanel.h"
#include "CommonFramework/ImageTools/ImageBoxes.h"
#include "CommonFramework/ImageTypes/ImageRGB32.h"
#include "CommonFramework/ImageTypes/ImageViewRGB32.h"
#include "CommonTools/Images/ImageFilter.h"
#include "CommonTools/OCR/OCR_Routines.h"
#include "CommonTools/OCR/OCR_StringNormalization.h"
#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h"
#include "PokemonFRLG_DialogTextReader.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonFRLG{


namespace{

// Both dialog boxes (overworld and battle) put their two lines here.
const ImageFloatBox DIALOG_LINE_BOXES[2] = {
{0.055, 0.755, 0.885, 0.088},
{0.055, 0.850, 0.885, 0.088},
};

// The text of one line of a text window: `box` in the game screen, with the
// text's colors between `min_color` and `max_color`. Line breaks become
// spaces and the ends are trimmed. (The OCR reads a single line.)
std::string read_text_line(
Logger& logger, Language language, const ImageViewRGB32& frame,
const ImageFloatBox& box, uint32_t min_color, uint32_t max_color,
const std::string& what, const char* debug_image
){
ImageViewRGB32 game_screen = extract_box_reference(frame, GameSettings::instance().GAME_BOX);
ImageViewRGB32 image = extract_box_reference(game_screen, box);

// The text becomes black on white.
ImageRGB32 bw = to_blackwhite_rgb32_range(image, true, min_color, max_color);
std::string text = OCR::ocr_read(language, bw);
for (char& ch : text){
if (ch == '\n' || ch == '\r'){
ch = ' ';
}
}
size_t start = 0;
while (start < text.size() && text[start] == ' '){
start++;
}
size_t end = text.size();
while (end > start && text[end - 1] == ' '){
end--;
}
text = text.substr(start, end - start);

logger.log(what + ": \"" + text + "\"");
if (text.empty() && GlobalSettings::instance().SAVE_DEBUG_IMAGES){
image.save(debug_image);
}
return text;
}

}


std::string read_dialog_line(
Logger& logger, Language language, const ImageViewRGB32& frame,
DialogStyle style, int line
){
const ImageFloatBox& box = DIALOG_LINE_BOXES[line == 0 ? 0 : 1];
std::string what = "Dialog line " + std::to_string(line == 0 ? 1 : 2);
if (style == DialogStyle::BATTLE){
return read_text_line(
logger, language, frame, box,
combine_rgb(200, 200, 200), combine_rgb(255, 255, 255),
what, "DebugDumps/ocr_dialog_line.png"
);
}else{
return read_text_line(
logger, language, frame, box,
combine_rgb(0, 0, 0), combine_rgb(150, 150, 255),
what, "DebugDumps/ocr_dialog_line.png"
);
}
}

bool text_contains_any(const std::string& text, const std::vector<std::string>& words){
std::u32string normalized = OCR::normalize_utf32(text);
for (const std::string& word : words){
std::u32string normalized_word = OCR::normalize_utf32(word);
if (!normalized_word.empty() && normalized.find(normalized_word) != std::u32string::npos){
return true;
}
}
return false;
}

int last_number_in(const std::string& text){
size_t end = text.size();
while (end > 0 && !isdigit((unsigned char)text[end - 1])){
end--;
}
if (end == 0){
return -1;
}
size_t start = end;
while (start > 0 && isdigit((unsigned char)text[start - 1])){
start--;
}
if (end - start > 4){
return -1;
}
return std::stoi(text.substr(start, end - start));
}



}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* Dialog Text Reader
*
* From: https://github.com/PokemonAutomation/
*
* Read the text of dialog boxes and other text windows, for telling apart
* boxes that look the same (e.g. a VS Seeker message vs. a trainer's line).
*
* The readers return the raw OCR text, trimmed, with line breaks as spaces.
* To look for words in it, use text_contains_any(), which compares
* normalized text (case, accents and punctuation don't matter), with the
* words of the game's language.
*
*/

#ifndef PokemonAutomation_PokemonFRLG_DialogTextReader_H
#define PokemonAutomation_PokemonFRLG_DialogTextReader_H

#include <string>
#include <vector>
#include "CommonFramework/Language.h"

namespace PokemonAutomation{
class Logger;
class ImageViewRGB32;
namespace NintendoSwitch{
namespace PokemonFRLG{


enum class DialogStyle{
OVERWORLD, // Dark gray (or blue) text on the white overworld box.
BATTLE, // White text on the dark teal battle box.
};

// One line (0 = top, 1 = bottom) of the dialog box at the bottom of the
// screen. Empty if nothing could be read.
std::string read_dialog_line(
Logger& logger, Language language, const ImageViewRGB32& frame,
DialogStyle style, int line
);


// Whether `text` contains any of the words, comparing normalized text (see
// OCR::normalize_utf32()), so "POKéMON" matches "pokemon".
bool text_contains_any(const std::string& text, const std::vector<std::string>& words);

// The last number in `text`, e.g. 250 in "...for 250 steps.". -1 if there
// is none (or it is longer than 4 digits).
int last_number_in(const std::string& text);



}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* Overworld Geometry
*
* From: https://github.com/PokemonAutomation/
*
* Where things are on the overworld screen, in GBA pixels and tiles.
*
* The GBA screen is 240x160 pixels, made of 16x16 tiles. The camera keeps
* the player on the same screen tile: the one whose top-left corner is at
* (112, 72). A TileOffset is a tile's position relative to the player's
* (right and down positive).
*
*/

#ifndef PokemonAutomation_PokemonFRLG_OverworldGeometry_H
#define PokemonAutomation_PokemonFRLG_OverworldGeometry_H

#include <stddef.h>
#include <stdint.h>
#include "CommonFramework/ImageTypes/ImageViewRGB32.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonFRLG{


constexpr int GBA_SCREEN_WIDTH = 240;
constexpr int GBA_SCREEN_HEIGHT = 160;
constexpr int OVERWORLD_TILE_SIZE = 16;
constexpr int PLAYER_TILE_LEFT = 112;
constexpr int PLAYER_TILE_TOP = 72;


struct TileOffset{
int dx = 0;
int dy = 0;
bool operator==(const TileOffset& x) const{
return dx == x.dx && dy == x.dy;
}
};


// The pixel at the center of GBA pixel (x, y) of `game_screen`, which is the
// game's area of the video frame (see GameSettings::GAME_BOX) at any size.
inline uint32_t gba_pixel(const ImageViewRGB32& game_screen, int x, int y){
size_t px = (size_t)((x + 0.5) * (double)game_screen.width() / GBA_SCREEN_WIDTH);
size_t py = (size_t)((y + 0.5) * (double)game_screen.height() / GBA_SCREEN_HEIGHT);
return game_screen.pixel(px, py);
}



}
}
}
#endif
Loading
Loading