Skip to content

Commit 7c56d5a

Browse files
author
Gin
committed
fix LZA box sorter
1 parent 3bea882 commit 7c56d5a

File tree

5 files changed

+71
-12
lines changed

5 files changed

+71
-12
lines changed

SerialPrograms/Source/CommonTools/OCR/OCR_NumberReader.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ std::string read_number_waterfill_no_normalization(
173173
std::string ocr_text;
174174
for (const auto& item : map){
175175
const WaterfillObject& object = item.second;
176-
ImageRGB32 cropped = extract_box_reference(filtered, object).copy();
176+
ImageRGB32 cropped = extract_box_reference(filtered, object).copy();
177177
PackedBinaryMatrix tmp(object.packed_matrix());
178178
filter_by_mask(tmp, cropped, Color(0xffffffff), true);
179179

@@ -190,11 +190,9 @@ std::string read_number_waterfill_no_normalization(
190190
// std::cout << ocr[0] << std::endl;
191191
if (!ocr.empty()){
192192
ocr_text += ocr[0];
193-
}else{
194-
if (check_empty_string){
195-
logger.log("OCR fail: one of characters read as empty string.", COLOR_RED);
196-
return "";
197-
}
193+
}else if (check_empty_string){
194+
logger.log("OCR fail: one of characters read as empty string.", COLOR_RED);
195+
return "";
198196
}
199197
}
200198

@@ -228,13 +226,14 @@ int read_number_waterfill_multifilter(
228226

229227
uint32_t rgb32_min = filter.first;
230228
uint32_t rgb32_max = filter.second;
229+
bool check_empty_string = false;
231230
std::string ocr_text = read_number_waterfill_no_normalization(
232231
logger,
233232
image,
234233
rgb32_min, rgb32_max,
235234
text_inside_range,
236235
width_max,
237-
true
236+
check_empty_string
238237
);
239238

240239
std::string normalized = run_number_normalization(ocr_text);

SerialPrograms/Source/Pokemon/Options/Pokemon_BoxSortingTable.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ const EnumDropdownDatabase<SortingRuleType>& SortingRuleType_Database(){
2626

2727
BoxSortingRow::BoxSortingRow(EditableTableOption& parent_table)
2828
: EditableTableRow(parent_table)
29-
, sort_type(SortingRuleType_Database(), LockMode::LOCK_WHILE_RUNNING, SortingRuleType::DexNo)
29+
, sort_type(
30+
static_cast<BoxSortingTable&>(parent_table).database(),
31+
LockMode::LOCK_WHILE_RUNNING,
32+
SortingRuleType::DexNo
33+
)
3034
, reverse(LockMode::LOCK_WHILE_RUNNING, false)
3135
{
3236
PA_ADD_OPTION(sort_type);
@@ -41,7 +45,30 @@ std::unique_ptr<EditableTableRow> BoxSortingRow::clone() const{
4145

4246

4347
BoxSortingTable::BoxSortingTable(std::string label)
44-
: EditableTableOption_t<BoxSortingRow>(
48+
: BoxSortingTableDatabaseHelper(nullptr)
49+
, EditableTableOption_t<BoxSortingRow>(
50+
std::move(label),
51+
LockMode::LOCK_WHILE_RUNNING,
52+
make_defaults()
53+
)
54+
{}
55+
56+
BoxSortingTable::BoxSortingTable(std::string label, const std::vector<SortingRuleType>& allowed_rules)
57+
: BoxSortingTableDatabaseHelper([&allowed_rules]() {
58+
// Build custom database from allowed rules
59+
const EnumDropdownDatabase<SortingRuleType>& full_db = SortingRuleType_Database();
60+
auto custom_db = std::make_shared<EnumDropdownDatabase<SortingRuleType>>();
61+
62+
for (SortingRuleType rule : allowed_rules) {
63+
const EnumEntry* entry = full_db.find(rule);
64+
if (entry != nullptr) {
65+
custom_db->add(rule, entry->slug, entry->display, entry->enabled);
66+
}
67+
}
68+
69+
return custom_db;
70+
}())
71+
, EditableTableOption_t<BoxSortingRow>(
4572
std::move(label),
4673
LockMode::LOCK_WHILE_RUNNING,
4774
make_defaults()
@@ -77,6 +104,13 @@ std::vector<std::unique_ptr<EditableTableRow>> BoxSortingTable::make_defaults(){
77104
return ret;
78105
}
79106

107+
const EnumDropdownDatabase<SortingRuleType>& BoxSortingTable::database() const{
108+
if (m_database) {
109+
return *m_database;
110+
}
111+
return SortingRuleType_Database();
112+
}
113+
80114

81115

82116

SerialPrograms/Source/Pokemon/Options/Pokemon_BoxSortingTable.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,29 @@ class BoxSortingRow : public EditableTableRow{
4242
};
4343

4444

45+
// Helper base class to ensure database is initialized before table construction
46+
class BoxSortingTableDatabaseHelper{
47+
protected:
48+
BoxSortingTableDatabaseHelper(std::shared_ptr<const EnumDropdownDatabase<SortingRuleType>> db = nullptr)
49+
: m_database(std::move(db))
50+
{}
51+
52+
std::shared_ptr<const EnumDropdownDatabase<SortingRuleType>> m_database;
53+
};
54+
4555

46-
class BoxSortingTable : public EditableTableOption_t<BoxSortingRow>{
56+
class BoxSortingTable : private BoxSortingTableDatabaseHelper, public EditableTableOption_t<BoxSortingRow>{
4757
public:
4858
BoxSortingTable(std::string label);
59+
BoxSortingTable(std::string label, const std::vector<SortingRuleType>& allowed_rules);
4960

5061
std::vector<SortingRule> preferences() const;
5162

5263
virtual std::vector<std::string> make_header() const;
5364

5465
std::vector<std::unique_ptr<EditableTableRow>> make_defaults();
66+
67+
const EnumDropdownDatabase<SortingRuleType>& database() const;
5568
};
5669

5770

SerialPrograms/Source/PokemonLZA/Inference/Boxes/PokemonLZA_BoxInfoDetector.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,19 @@ void BoxDexNumberDetector::make_overlays(VideoOverlaySet& items) const{
205205
bool BoxDexNumberDetector::detect(const ImageViewRGB32& screen){
206206
const size_t max_dex_number = std::max(LUMIOSE_DEX_SLUGS().size(), HYPERSPACE_DEX_SLUGS().size());
207207

208-
const int dex_number = OCR::read_number_waterfill(m_logger, extract_box_reference(screen, m_dex_number_box), 0xff808080, 0xffffffff, false);
208+
const ImageViewRGB32 dex_image_crop = extract_box_reference(screen, m_dex_number_box);
209+
// const bool in_range_black = false;
210+
// const ImageRGB32 black_white_dex_image_crop = to_blackwhite_rgb32_range(dex_image_crop, in_range_black, 0xff808080, 0xffffffff);
211+
// black_white_dex_image_crop.save("blackwhite_number.png");
212+
// const int dex_number = OCR::read_number(m_logger, black_white_dex_image_crop);
213+
// const int dex_number = OCR::read_number_waterfill(m_logger, dex_image_crop, 0xff808080, 0xffffffff, false);
214+
const int dex_number = OCR::read_number_waterfill_multifilter(m_logger, dex_image_crop, {
215+
{0x0, 0xff202020},
216+
{0x0, 0xff404040},
217+
{0x0, 0xff606060},
218+
{0x0, 0xff808080},
219+
{0x0, 0xffA0A0A0},
220+
});
209221
if (dex_number <= 0 || dex_number > static_cast<int>(max_dex_number)) {
210222
m_dex_number = 0;
211223
m_dex_number_when_error = dex_number;

SerialPrograms/Source/PokemonLZA/Programs/PokemonLZA_BoxSorter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ BoxSorter::BoxSorter()
8282
1, 1, MAX_BOXES
8383
)
8484
, SORT_TABLE(
85-
"<b>Sort Order Rules:</b><br>Sort order rules will be applied top to bottom."
85+
"<b>Sort Order Rules:</b><br>Sort order rules will be applied top to bottom.",
86+
{SortingRuleType::DexNo, SortingRuleType::Shiny, SortingRuleType::Alpha}
8687
)
8788
, OUTPUT_FILE(
8889
false,

0 commit comments

Comments
 (0)