Skip to content

Commit 996abec

Browse files
committed
clamp vertical padding and cutoffs
1 parent ea3ec1c commit 996abec

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

SerialPrograms/Source/PokemonSV/Programs/Farming/PokemonSV_AuctionFarmer.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "PokemonSwSh/Commands/PokemonSwSh_Commands_DateSpam.h"
3333
#include "PokemonSV_AuctionFarmer.h"
3434

35+
#include <algorithm>
36+
3537
#include "Common/Cpp/PrettyPrint.h"
3638

3739

@@ -377,7 +379,7 @@ uint64_t read_next_bid(VideoStream& stream, ProControllerContext& context, Langu
377379
// How much to cut off from the bid text in order to not read currencies and exclamation marks as numbers.
378380
// Values are pixels for a 1920x1080 screen, negative values are padding
379381
static const std::map<Language, std::pair<int8_t, int8_t>> cutoffs = {
380-
{ Language::English, {22, 7} },
382+
{ Language::English, {22, 6} },
381383
{ Language::Japanese, {-5, 54} },
382384
{ Language::Spanish, {6, 32} },
383385
{ Language::French, {-5, 45} },
@@ -387,6 +389,7 @@ uint64_t read_next_bid(VideoStream& stream, ProControllerContext& context, Langu
387389
{ Language::ChineseSimplified, {22, 7} },
388390
{ Language::ChineseTraditional, {22,7} }
389391
};
392+
390393

391394
static const std::map<Language, float> high_x = {
392395
{ Language::English, 0.75f },
@@ -412,7 +415,6 @@ uint64_t read_next_bid(VideoStream& stream, ProControllerContext& context, Langu
412415
{ Language::ChineseTraditional, 0.75f }
413416
};
414417

415-
416418
float box_y = high ? 0.42f : 0.493f;
417419
float box_x = high ? high_x.at(language) : low_x.at(language);
418420
float width = 0.9f - box_x; // max_x is always the same for all languages
@@ -427,8 +429,6 @@ uint64_t read_next_bid(VideoStream& stream, ProControllerContext& context, Langu
427429
VideoSnapshot screen = stream.video().snapshot();
428430
double screen_scale = (double)screen->width() / 1920.0;
429431
double vertical_padding = 5.0; // small amount of pixels so numbers do not touch the edge of the view when reading them
430-
float left_cutoff = cutoffs.at(language).first;
431-
float right_cutoff = cutoffs.at(language).second;
432432

433433
ImageViewRGB32 raw_bid_image = extract_box_reference(screen, box);
434434
ImagePixelBox bid_bounding_box = ImageMatch::enclosing_rectangle_with_pixel_filter(
@@ -437,13 +437,19 @@ uint64_t read_next_bid(VideoStream& stream, ProControllerContext& context, Langu
437437
return (uint32_t)pixel.red() + pixel.green() + pixel.blue() < 250;
438438
});
439439

440+
int32_t max_width = static_cast<int32_t>(raw_bid_image.width() - 1);
441+
int32_t max_height = static_cast<int32_t>(raw_bid_image.height() - 1);
442+
int32_t scaled_vertical_padding = static_cast<int32_t>(vertical_padding * screen_scale);
443+
int32_t left_cutoff = static_cast<int32_t>(cutoffs.at(language).first * screen_scale);
444+
int32_t right_cutoff = static_cast<int32_t>(cutoffs.at(language).second * screen_scale);
445+
440446
ImagePixelBox cut_bid_bounding_box(
441-
bid_bounding_box.min_x + (size_t)(left_cutoff * screen_scale),
442-
bid_bounding_box.min_y - (size_t)(vertical_padding * screen_scale),
443-
bid_bounding_box.max_x - (size_t)(right_cutoff * screen_scale),
444-
bid_bounding_box.max_y + (size_t)(vertical_padding * screen_scale)
445-
);
446-
447+
std::max(0, std::min(max_width, static_cast<int32_t>(bid_bounding_box.min_x) + left_cutoff)),
448+
std::max(0, std::min(max_height, static_cast<int32_t>(bid_bounding_box.min_y) - scaled_vertical_padding)),
449+
std::max(0, std::min(max_width, static_cast<int32_t>(bid_bounding_box.max_x) - right_cutoff)),
450+
std::max(0, std::min(max_height, static_cast<int32_t>(bid_bounding_box.max_y) + scaled_vertical_padding))
451+
);
452+
447453
uint64_t read_bid = OCR::read_number(stream.logger(), extract_box_reference(raw_bid_image, cut_bid_bounding_box));
448454

449455
if (read_bids.find(read_bid) == read_bids.end()){

0 commit comments

Comments
 (0)