-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathPokemonSwSh_MaxLairDatabase.cpp
More file actions
291 lines (247 loc) · 10.3 KB
/
PokemonSwSh_MaxLairDatabase.cpp
File metadata and controls
291 lines (247 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* Pokemon Sword/Shield Max Lair Rentals
*
* From: https://github.com/PokemonAutomation/
*
*/
#include <algorithm>
#include <map>
#include "Common/Cpp/Exceptions.h"
#include "Common/Cpp/Json/JsonValue.h"
#include "Common/Cpp/Json/JsonArray.h"
#include "Common/Cpp/Json/JsonObject.h"
#include "CommonFramework/Globals.h"
#include "Pokemon/Resources/Pokemon_PokemonSlugs.h"
#include "Pokemon/Resources/Pokemon_PokemonNames.h"
#include "PokemonSwSh_MaxLairDatabase.h"
namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonSwSh{
namespace MaxLairInternal{
struct MaxLairSlugsDatabase{
std::map<std::string, MaxLairSlugs> m_slugs;
static MaxLairSlugsDatabase& instance(){
static MaxLairSlugsDatabase data;
return data;
}
MaxLairSlugsDatabase(){
std::string path = RESOURCE_PATH() + "PokemonSwSh/MaxLairSlugMap.json";
JsonValue json = load_json_file(path);
JsonObject& root = json.to_object_throw(path);
for (auto& item0 : root){
const std::string& maxlair_slug = item0.first;
JsonObject& obj = item0.second.to_object_throw(path);
MaxLairSlugs slugs;
for (auto& item1 : obj.get_array_throw("OCR", path)){
std::string& slug = item1.to_string_throw(path);
if (!slugs.name_slug.empty()){
throw FileException(nullptr, PA_CURRENT_FUNCTION, "Multiple names specified for MaxLair slug.", std::move(path));
}
slugs.name_slug = std::move(slug);
}
for (auto& item1 : obj.get_array_throw("Sprite", path)){
std::string& slug = item1.to_string_throw(path);
slugs.sprite_slugs.insert(std::move(slug));
}
m_slugs[maxlair_slug] = std::move(slugs);
}
}
};
const std::map<std::string, MaxLairSlugs>& maxlair_slugs(){
return MaxLairSlugsDatabase::instance().m_slugs;
}
const MaxLairSlugs& get_maxlair_slugs(const std::string& slug){
const MaxLairSlugsDatabase& database = MaxLairSlugsDatabase::instance();
auto iter = database.m_slugs.find(slug);
if (iter == database.m_slugs.end()){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid Max Lair slug: " + slug);
}
return iter->second;
}
MoveCategory parse_category_slug(const std::string& slug){
static const std::map<std::string, MoveCategory> database{
{"status", MoveCategory::STATUS},
{"physical", MoveCategory::PHYSICAL},
{"special", MoveCategory::SPECIAL},
};
auto iter = database.find(slug);
if (iter == database.end()){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid Move Category: " + slug);
}
return iter->second;
}
PokemonType parse_type_slug(const std::string& slug){
static const std::map<std::string, PokemonType> database{
{"normal", PokemonType::NORMAL},
{"fire", PokemonType::FIRE},
{"fighting", PokemonType::FIGHTING},
{"water", PokemonType::WATER},
{"flying", PokemonType::FLYING},
{"grass", PokemonType::GRASS},
{"poison", PokemonType::POISON},
{"electric", PokemonType::ELECTRIC},
{"ground", PokemonType::GROUND},
{"psychic", PokemonType::PSYCHIC},
{"rock", PokemonType::ROCK},
{"ice", PokemonType::ICE},
{"bug", PokemonType::BUG,},
{"dragon", PokemonType::DRAGON},
{"ghost", PokemonType::GHOST},
{"dark", PokemonType::DARK},
{"steel", PokemonType::STEEL},
{"fairy", PokemonType::FAIRY},
};
auto iter = database.find(slug);
if (iter == database.end()){
return PokemonType::NONE;
}
return iter->second;
}
MaxLairMove parse_move(JsonObject&& obj, const std::string& path){
MaxLairMove move;
move.slug = obj.get_string_throw("move", path);
move.category = parse_category_slug(obj.get_string_throw("category", path));
move.type = parse_type_slug(obj.get_string_throw("type", path));
move.base_power = (uint8_t)obj.get_integer_throw("base_power", path);
move.accuracy = obj.get_double_throw("accuracy", path);
move.PP = (uint8_t)obj.get_integer_throw("PP", path);
move.spread = obj.get_boolean_throw("spread", path);
move.correction_factor = obj.get_double_throw("correction_factor", path);
move.effective_power = obj.get_double_throw("effective_power", path);
return move;
}
std::map<std::string, MaxLairMon> build_maxlair_mon_database(const std::string& path){
std::string filepath = RESOURCE_PATH() + path;
JsonValue json = load_json_file(filepath);
JsonObject& root = json.to_object_throw(filepath);
std::map<std::string, MaxLairMon> database;
for (auto& item : root){
const std::string& slug = item.first;
JsonObject& obj = item.second.to_object_throw(filepath);
MaxLairMon& mon = database[slug];
mon.species = slug;
mon.type[0] = PokemonType::NONE;
mon.type[1] = PokemonType::NONE;
{
JsonArray& array = obj.get_array_throw("type", filepath);
if (array.size() >= 1){
std::string& str = array[0].to_string_throw(filepath);
mon.type[0] = parse_type_slug(str);
}
if (array.size() >= 2){
std::string& str = array[1].to_string_throw(filepath);
mon.type[1] = parse_type_slug(str);
}
}
{
std::string& str = obj.get_string_throw("ability", filepath);
mon.ability = std::move(str);
}
{
JsonArray& array = obj.get_array_throw("base_stats", filepath);
if (array.size() != 6){
throw FileException(nullptr, PA_CURRENT_FUNCTION, "Base stats should contain 6 elements: " + slug, std::move(filepath));
}
for (int c = 0; c < 6; c++){
mon.base_stats[c] = (uint8_t)array[c].to_integer_throw(filepath);
}
}
{
JsonArray& array = obj.get_array_throw("moves", filepath);
size_t stop = std::min<size_t>(5, array.size());
for (size_t c = 0; c < stop; c++){
JsonObject& move = array[c].to_object_throw(filepath);
mon.moves[c] = parse_move(std::move(move), filepath);
}
}
{
JsonArray& array = obj.get_array_throw("max_moves", filepath);
size_t stop = std::min<size_t>(5, array.size());
for (size_t c = 0; c < stop; c++){
JsonObject& move = array[c].to_object_throw(filepath);
mon.max_moves[c] = parse_move(std::move(move), filepath);
}
}
}
return database;
}
struct MaxLairDatabase{
std::map<std::string, MaxLairMon> m_rentals;
std::map<std::string, MaxLairMon> m_bosses;
std::map<size_t, std::string> m_bosses_by_dex;
std::multimap<size_t, std::string> m_rentals_by_dex;
static MaxLairDatabase& instance(){
static MaxLairDatabase data;
return data;
}
MaxLairDatabase()
: m_rentals(build_maxlair_mon_database("PokemonSwSh/MaxLairRentals.json"))
, m_bosses(build_maxlair_mon_database("PokemonSwSh/MaxLairBosses.json"))
{
const std::map<std::string, size_t>& national_dex = SLUGS_TO_NATIONAL_DEX();
for (const auto& item : m_bosses){
const MaxLairSlugs& slugs = get_maxlair_slugs(item.first);
auto iter = national_dex.find(slugs.name_slug);
if (iter == national_dex.end()){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Slug not found in national dex: " + slugs.name_slug);
}
m_bosses_by_dex[iter->second] = item.first;
}
for (const auto& item : m_rentals){
const MaxLairSlugs& slugs = get_maxlair_slugs(item.first);
auto iter = national_dex.find(slugs.name_slug);
if (iter == national_dex.end()){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Rental slug not found in national dex: " + slugs.name_slug);
}
m_rentals_by_dex.insert({iter->second, item.first});
}
#if 0
for (const auto& item : m_bosses_by_dex){
const MaxLairSlugs& slugs = get_maxlair_slugs(item.second);
cout << item.first << " : "
<< item.second << " : "
<< get_pokemon_name(slugs.name_slug).display_name().toStdString() << endl;
}
#endif
}
};
const std::map<size_t, std::string>& all_bosses_by_dex(){
const MaxLairDatabase& database = MaxLairDatabase::instance();
return database.m_bosses_by_dex;
}
const std::multimap<size_t, std::string>& all_rentals_by_dex(){
const MaxLairDatabase& database = MaxLairDatabase::instance();
return database.m_rentals_by_dex;
}
bool is_boss(const std::string& slug){
const MaxLairDatabase& database = MaxLairDatabase::instance();
auto iter = database.m_bosses.find(slug);
return iter != database.m_bosses.end();
}
const MaxLairMon& get_maxlair_mon(const std::string& slug){
const MaxLairDatabase& database = MaxLairDatabase::instance();
auto iter0 = database.m_rentals.find(slug);
if (iter0 != database.m_rentals.end()){
return iter0->second;
}
auto iter1 = database.m_bosses.find(slug);
if (iter1 != database.m_bosses.end()){
return iter1->second;
}
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid Species Slug: " + slug);
}
const MaxLairMon* get_maxlair_mon_nothrow(const std::string& slug){
const MaxLairDatabase& database = MaxLairDatabase::instance();
auto iter0 = database.m_rentals.find(slug);
if (iter0 != database.m_rentals.end()){
return &iter0->second;
}
auto iter1 = database.m_bosses.find(slug);
if (iter1 != database.m_bosses.end()){
return &iter1->second;
}
return nullptr;
}
}
}
}
}