Skip to content

Commit a2dcaa8

Browse files
committed
PLA pokeballs sprites for Pokemon Home.
1 parent c26783f commit a2dcaa8

File tree

7 files changed

+145
-15
lines changed

7 files changed

+145
-15
lines changed

SerialPrograms/Source/Pokemon/Resources/Pokemon_PokeballNames.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,17 @@ PokeballNameDatabase::PokeballNameDatabase(){
4242
std::string& slug = item.to_string_throw(path_slugs);
4343
ordered_list.emplace_back(slug);
4444

45-
JsonObject& languages = item_disp.get_object_throw(slug, path_disp);
46-
std::string& display_name = languages.get_string_throw("eng", path_disp);
47-
48-
database[slug].m_display_name = display_name;
49-
reverse_lookup[std::move(display_name)] = std::move(slug);
45+
JsonObject* languages = item_disp.get_object(slug);
46+
std::string* display_name;
47+
if (languages != nullptr){
48+
// TODO: REMOVE: Display names for PLA balls.
49+
display_name = &languages->get_string_throw("eng", path_disp);
50+
}else{
51+
display_name = &slug;
52+
}
53+
54+
database[slug].m_display_name = *display_name;
55+
reverse_lookup[*display_name] = slug;
5056
}
5157
}
5258

SerialPrograms/Source/PokemonBDSP/Inference/PokemonBDSP_PokeballSpriteMatcher.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ PokeballSpriteMatcher::PokeballSpriteMatcher(double min_euclidean_distance)
2222
add(item.first, item.second.sprite);
2323
}
2424
}
25-
26-
2725
auto PokeballSpriteMatcher::get_crop_candidates(const ImageViewRGB32& image) const -> std::vector<ImageViewRGB32>{
2826
ImageStats border = image_border_stats(image);
2927
ImagePixelBox box = ImageMatch::enclosing_rectangle_with_pixel_filter(

SerialPrograms/Source/PokemonHome/Inference/PokemonHome_BallReader.cpp

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,86 @@
44
*
55
*/
66

7+
#include "Kernels/Waterfill/Kernels_Waterfill_Session.h"
78
#include "CommonFramework/ImageTypes/ImageViewRGB32.h"
89
#include "CommonFramework/Notifications/ProgramInfo.h"
910
#include "CommonFramework/Tools/ErrorDumper.h"
10-
#include "PokemonSwSh/Resources/PokemonSwSh_PokeballSprites.h"
11-
#include "PokemonBDSP/Inference/PokemonBDSP_PokeballSpriteMatcher.h"
11+
#include "CommonTools/Images/BinaryImage_FilterRgb32.h"
12+
#include "CommonTools/ImageMatch/ImageCropper.h"
13+
#include "PokemonHome/Resources/PokemonHome_PokeballSprites.h"
14+
//#include "PokemonSwSh/Resources/PokemonSwSh_PokeballSprites.h"
15+
//#include "PokemonBDSP/Inference/PokemonBDSP_PokeballSpriteMatcher.h"
1216
#include "PokemonHome_BallReader.h"
1317

1418
namespace PokemonAutomation{
1519
namespace NintendoSwitch{
1620
namespace PokemonHome{
1721

1822

23+
24+
25+
PokeballSpriteMatcher::PokeballSpriteMatcher(double min_euclidean_distance)
26+
: CroppedImageDictionaryMatcher({1, 128})
27+
, m_min_euclidean_distance_squared(min_euclidean_distance * min_euclidean_distance)
28+
{
29+
for (const auto& item : PokemonHome::ALL_POKEBALL_SPRITES()){
30+
add(item.first, remove_white_border(item.second.sprite));
31+
}
32+
}
33+
ImageRGB32 PokeballSpriteMatcher::remove_white_border(const ImageViewRGB32& image){
34+
using namespace Kernels::Waterfill;
35+
36+
ImageRGB32 ret = image.copy();
37+
{
38+
auto matrix = compress_rgb32_to_binary_range(ret, 0x00000000, 0x7f000000);
39+
auto session = make_WaterfillSession(matrix);
40+
auto iter = session->make_iterator(200);
41+
WaterfillObject object;
42+
if (!iter->find_next(object, true)){
43+
return ret;
44+
}
45+
filter_by_mask(object.packed_matrix(), ret, Color(0xffffffff), false);
46+
}
47+
{
48+
auto matrix = compress_rgb32_to_binary_range(ret, 0x00808080, 0xffffffff);
49+
auto session = make_WaterfillSession(matrix);
50+
auto iter = session->make_iterator(200);
51+
WaterfillObject object;
52+
if (!iter->find_next(object, true)){
53+
return ret;
54+
}
55+
filter_by_mask(object.packed_matrix(), ret, Color(0x00000000), false);
56+
}
57+
return ret;
58+
}
59+
auto PokeballSpriteMatcher::get_crop_candidates(const ImageViewRGB32& image) const -> std::vector<ImageViewRGB32>{
60+
ImageStats border = image_border_stats(image);
61+
ImagePixelBox box = ImageMatch::enclosing_rectangle_with_pixel_filter(
62+
image,
63+
[&](Color pixel){
64+
double r = (double)pixel.red() - border.average.r;
65+
double g = (double)pixel.green() - border.average.g;
66+
double b = (double)pixel.blue() - border.average.b;
67+
bool stop = r*r + g*g + b*b >= m_min_euclidean_distance_squared;
68+
return stop;
69+
}
70+
);
71+
std::vector<ImageViewRGB32> ret;
72+
ret.emplace_back(extract_box_reference(image, box));
73+
return ret;
74+
}
75+
76+
77+
78+
79+
80+
1981
const double BallReader::MAX_ALPHA = 0.40;
2082
const double BallReader::ALPHA_SPREAD = 0.02;
2183

2284

23-
const PokemonBDSP::PokeballSpriteMatcher& BALL_SPRITE_MATCHER(){
24-
static PokemonBDSP::PokeballSpriteMatcher matcher;
85+
const PokeballSpriteMatcher& BALL_SPRITE_MATCHER(){
86+
static PokeballSpriteMatcher matcher;
2587
return matcher;
2688
}
2789

SerialPrograms/Source/PokemonHome/Inference/PokemonHome_BallReader.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define PokemonAutomation_PokemonHome_BallReader_H
99

1010
#include <string>
11+
#include "CommonFramework/ImageTypes/ImageRGB32.h"
1112
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
1213
#include "CommonFramework/Tools/VideoStream.h"
1314
#include "CommonTools/ImageMatch/CroppedImageDictionaryMatcher.h"
@@ -17,6 +18,22 @@ namespace NintendoSwitch{
1718
namespace PokemonHome{
1819

1920

21+
22+
class PokeballSpriteMatcher : public ImageMatch::CroppedImageDictionaryMatcher{
23+
public:
24+
PokeballSpriteMatcher(double min_euclidean_distance = 100);
25+
26+
private:
27+
static ImageRGB32 remove_white_border(const ImageViewRGB32& image);
28+
virtual std::vector<ImageViewRGB32> get_crop_candidates(const ImageViewRGB32& image) const override;
29+
30+
private:
31+
double m_min_euclidean_distance_squared;
32+
};
33+
34+
35+
36+
2037
class BallReader{
2138
static const double MAX_ALPHA;
2239
static const double ALPHA_SPREAD;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* Pokemon Home Pokeball Sprites
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include "PokemonHome_PokeballSprites.h"
8+
9+
namespace PokemonAutomation{
10+
namespace NintendoSwitch{
11+
namespace PokemonHome{
12+
13+
14+
const SpriteDatabase& ALL_POKEBALL_SPRITES(){
15+
static const SpriteDatabase database("PokemonHome/PokeballSprites.png", "PokemonHome/PokeballSprites.json");
16+
return database;
17+
}
18+
19+
20+
21+
}
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* Pokemon s Pokeball Sprites
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_PokemonHome_PokeballSprites_H
8+
#define PokemonAutomation_PokemonHome_PokeballSprites_H
9+
10+
#include "CommonTools/Resources/SpriteDatabase.h"
11+
12+
namespace PokemonAutomation{
13+
namespace NintendoSwitch{
14+
namespace PokemonHome{
15+
16+
17+
const SpriteDatabase& ALL_POKEBALL_SPRITES();
18+
19+
20+
21+
}
22+
}
23+
}
24+
#endif

SerialPrograms/Source/PokemonSwSh/Options/PokemonSwSh_BallSelectOption.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ StringSelectDatabase make_all_balls_database(){
2626
// cout << "slug = " << slug << endl;
2727
const PokeballNames& data = get_pokeball_name(slug);
2828
const SpriteDatabase::Sprite* sprite = ALL_POKEBALL_SPRITES().get_nothrow(slug);
29-
if (sprite == nullptr){
30-
ret.add_entry(StringSelectEntry(slug, data.display_name()));
31-
global_logger_tagged().log("Missing sprite for: " + slug, COLOR_RED);
32-
}else{
29+
if (sprite != nullptr){
3330
ret.add_entry(StringSelectEntry(slug, data.display_name(), sprite->icon));
31+
}else{
32+
// ret.add_entry(StringSelectEntry(slug, data.display_name()));
33+
// global_logger_tagged().log("Missing sprite for: " + slug, COLOR_RED);
3434
}
3535
}
3636
return ret;

0 commit comments

Comments
 (0)