Skip to content

Commit 34fb511

Browse files
committed
donut berries option
1 parent aa574e4 commit 34fb511

File tree

5 files changed

+286
-0
lines changed

5 files changed

+286
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* Donut Berries Option
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include "CommonFramework/Logging/Logger.h"
8+
#include "PokemonLZA/Resources/PokemonLZA_DonutBerries.h"
9+
#include "PokemonLZA_DonutBerriesOption.h"
10+
11+
namespace PokemonAutomation{
12+
namespace NintendoSwitch{
13+
namespace PokemonLZA{
14+
15+
StringSelectDatabase make_donut_berries_database(){
16+
StringSelectDatabase ret;
17+
for (const auto& slug : DONUT_BERRIES_SLUGS()){
18+
const DonutBerries& data = get_berry_name(slug);
19+
const SpriteDatabase::Sprite* sprite = DONUT_BERRIES_DATABASE().get_nothrow(slug);
20+
if (sprite == nullptr){
21+
ret.add_entry(StringSelectEntry(slug, data.display_name()));
22+
global_logger_tagged().log("Missing sprite for: " + slug, COLOR_RED);
23+
}else{
24+
ret.add_entry(StringSelectEntry(slug, data.display_name(), sprite->icon));
25+
}
26+
}
27+
return ret;
28+
}
29+
const StringSelectDatabase& DONUT_BERRY_DATABASE(){
30+
static StringSelectDatabase database = make_donut_berries_database();
31+
return database;
32+
}
33+
34+
35+
DonutBerriesTableCell::DonutBerriesTableCell(
36+
const std::string& default_slug
37+
)
38+
: StringSelectCell(
39+
DONUT_BERRY_DATABASE(),
40+
LockMode::LOCK_WHILE_RUNNING,
41+
default_slug
42+
)
43+
{}
44+
45+
DonutBerriesTableRow::DonutBerriesTableRow(EditableTableOption& parent_table)
46+
: EditableTableRow(parent_table)
47+
, berry("hyper-cheri-berry")
48+
{
49+
PA_ADD_OPTION(berry);
50+
}
51+
std::unique_ptr<EditableTableRow> DonutBerriesTableRow::clone() const{
52+
std::unique_ptr<DonutBerriesTableRow> ret(new DonutBerriesTableRow(parent()));
53+
ret->berry.set_by_index(berry.index());
54+
return ret;
55+
}
56+
57+
DonutBerriesTable::DonutBerriesTable(std::string label)
58+
: EditableTableOption_t<DonutBerriesTableRow>(
59+
std::move(label),
60+
LockMode::LOCK_WHILE_RUNNING,
61+
make_defaults()
62+
)
63+
{}
64+
65+
66+
std::vector<std::string> DonutBerriesTable::make_header() const{
67+
return std::vector<std::string>{
68+
"Berry",
69+
};
70+
}
71+
72+
std::vector<std::unique_ptr<EditableTableRow>> DonutBerriesTable::make_defaults(){
73+
std::vector<std::unique_ptr<EditableTableRow>> ret;
74+
ret.emplace_back(std::make_unique<DonutBerriesTableRow>(*this));
75+
return ret;
76+
}
77+
78+
79+
80+
}
81+
}
82+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* Donut Berries Option
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_PokemonLZA_DonutBerriesOption_H
8+
#define PokemonAutomation_PokemonLZA_DonutBerriesOption_H
9+
10+
#include "CommonTools/Options/StringSelectOption.h"
11+
#include "Common/Cpp/Options/EditableTableOption.h"
12+
13+
namespace PokemonAutomation{
14+
namespace NintendoSwitch{
15+
namespace PokemonLZA{
16+
17+
18+
class DonutBerriesTableCell : public StringSelectCell{
19+
public:
20+
DonutBerriesTableCell(const std::string& default_slug);
21+
};
22+
23+
class DonutBerriesTableRow : public EditableTableRow{
24+
public:
25+
DonutBerriesTableRow(EditableTableOption& parent_table);
26+
virtual std::unique_ptr<EditableTableRow> clone() const override;
27+
28+
public:
29+
DonutBerriesTableCell berry;
30+
};
31+
32+
33+
class DonutBerriesTable : public EditableTableOption_t<DonutBerriesTableRow>{
34+
public:
35+
DonutBerriesTable(std::string label);
36+
37+
virtual std::vector<std::string> make_header() const override;
38+
39+
std::vector<std::unique_ptr<EditableTableRow>> make_defaults();
40+
};
41+
42+
43+
44+
}
45+
}
46+
}
47+
#endif
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/* Donut Berries
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include "Common/Cpp/Json/JsonValue.h"
8+
#include "Common/Cpp/Json/JsonArray.h"
9+
#include "Common/Cpp/Json/JsonObject.h"
10+
#include "CommonFramework/Globals.h"
11+
#include "PokemonLZA_DonutBerries.h"
12+
13+
namespace PokemonAutomation{
14+
namespace NintendoSwitch{
15+
namespace PokemonLZA{
16+
17+
18+
19+
std::vector<std::string> make_DONUT_BERRIES_SLUGS(){
20+
std::vector<std::string> ret;
21+
for (const auto& item : DONUT_BERRIES_DATABASE()){
22+
ret.emplace_back(item.first);
23+
}
24+
return ret;
25+
}
26+
const std::vector<std::string>& DONUT_BERRIES_SLUGS(){
27+
static std::vector<std::string> database = make_DONUT_BERRIES_SLUGS();
28+
return database;
29+
}
30+
31+
32+
33+
struct BerryNameDatabase{
34+
BerryNameDatabase();
35+
36+
static const BerryNameDatabase& instance(){
37+
static BerryNameDatabase database;
38+
return database;
39+
}
40+
41+
std::map<std::string, DonutBerries> database;
42+
std::map<std::string, std::string> m_display_name_to_slug;
43+
};
44+
BerryNameDatabase::BerryNameDatabase(){
45+
{
46+
std::string path = RESOURCE_PATH() + "PokemonLZA/Donuts/donut_berry_ocr.json";
47+
JsonValue json = load_json_file(path);
48+
JsonObject& object = json.to_object_throw(path);
49+
for (const auto& language_block : object){
50+
Language language = language_code_to_enum(language_block.first);
51+
const JsonObject& per_language = language_block.second.to_object_throw(path);
52+
for (const auto& slug : per_language){
53+
const JsonArray& names = slug.second.to_array_throw(path);
54+
if (names.empty()){
55+
throw JsonParseException(path, "Expected at least one name for: " + language_block.first + " : " + slug.first);
56+
}
57+
database[slug.first].m_display_names[language] = names[0].to_string_throw();
58+
}
59+
}
60+
for (auto& item : database){
61+
auto iter = item.second.m_display_names.find(Language::English);
62+
if (iter == item.second.m_display_names.end()){
63+
throw JsonParseException(path, "English not found for: " + item.first);
64+
}
65+
item.second.m_display_name = iter->second;
66+
m_display_name_to_slug[iter->second] = item.first;
67+
}
68+
}
69+
}
70+
71+
const DonutBerries& get_berry_name(const std::string& slug){
72+
const std::map<std::string, DonutBerries>& database = BerryNameDatabase::instance().database;
73+
auto iter = database.find(slug);
74+
if (iter == database.end()){
75+
throw InternalProgramError(
76+
nullptr, PA_CURRENT_FUNCTION,
77+
"Berry slug not found in database: " + slug
78+
);
79+
}
80+
return iter->second;
81+
}
82+
83+
const std::string& parse_berry_name(const std::string& display_name){
84+
const std::map<std::string, std::string>& database = BerryNameDatabase::instance().m_display_name_to_slug;
85+
auto iter = database.find(display_name);
86+
if (iter == database.end()){
87+
throw InternalProgramError(
88+
nullptr, PA_CURRENT_FUNCTION,
89+
"Berry name not found in database: " + display_name
90+
);
91+
}
92+
return iter->second;
93+
}
94+
95+
96+
97+
98+
99+
100+
const SpriteDatabase& DONUT_BERRIES_DATABASE(){
101+
static const SpriteDatabase database("PokemonLZA/Donuts/donut_berry_sheetSpritesheet.png", "PokemonLZA/Donuts/donut_berry_sheetSpritesheetData.json");
102+
return database;
103+
}
104+
105+
106+
107+
}
108+
}
109+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* Donut Berries
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_PokemonLZA_Berries_H
8+
#define PokemonAutomation_PokemonLZA_Berries_H
9+
10+
#include <vector>
11+
#include <map>
12+
#include "CommonFramework/Language.h"
13+
#include "CommonTools/Resources/SpriteDatabase.h"
14+
15+
namespace PokemonAutomation{
16+
namespace NintendoSwitch{
17+
namespace PokemonLZA{
18+
19+
20+
const std::vector<std::string>& DONUT_BERRIES_SLUGS();
21+
22+
class DonutBerries{
23+
public:
24+
const std::string& display_name() const{ return m_display_name; }
25+
26+
private:
27+
friend struct BerryNameDatabase;
28+
29+
std::string m_display_name;
30+
std::map<Language, std::string> m_display_names;
31+
};
32+
33+
const DonutBerries& get_berry_name(const std::string& slug);
34+
const std::string& parse_berry_name(const std::string& display_name);
35+
36+
37+
38+
const SpriteDatabase& DONUT_BERRIES_DATABASE();
39+
40+
41+
}
42+
}
43+
}
44+
#endif

SerialPrograms/SourceFiles.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,8 @@ file(GLOB LIBRARY_SOURCES
16251625
Source/PokemonLZA/InferenceTraining/PokemonLZA_GenerateLocationNameOCR.h
16261626
Source/PokemonLZA/Options/PokemonLZA_BattleAIOption.cpp
16271627
Source/PokemonLZA/Options/PokemonLZA_BattleAIOption.h
1628+
Source/PokemonLZA/Options/PokemonLZA_DonutBerriesOption.cpp
1629+
Source/PokemonLZA/Options/PokemonLZA_DonutBerriesOption.h
16281630
Source/PokemonLZA/Options/PokemonLZA_HyperspaceRewardOption.cpp
16291631
Source/PokemonLZA/Options/PokemonLZA_HyperspaceRewardOption.h
16301632
Source/PokemonLZA/Options/PokemonLZA_HyperspaceRewardTable.cpp
@@ -1702,6 +1704,8 @@ file(GLOB LIBRARY_SOURCES
17021704
Source/PokemonLZA/Programs/Trading/PokemonLZA_TradeRoutines.h
17031705
Source/PokemonLZA/Resources/PokemonLZA_AvailablePokemon.cpp
17041706
Source/PokemonLZA/Resources/PokemonLZA_AvailablePokemon.h
1707+
Source/PokemonLZA/Resources/PokemonLZA_DonutBerries.cpp
1708+
Source/PokemonLZA/Resources/PokemonLZA_DonutBerries.h
17051709
Source/PokemonLZA/Resources/PokemonLZA_HyperspaceRewardNames.cpp
17061710
Source/PokemonLZA/Resources/PokemonLZA_HyperspaceRewardNames.h
17071711
Source/PokemonRSE/Inference/Dialogs/PokemonRSE_DialogDetector.cpp

0 commit comments

Comments
 (0)