Skip to content

Commit d67f804

Browse files
committed
Fix incorrect end battle detection in Doubles Leveling when wild Graveler uses magnitude.
1 parent 19360c8 commit d67f804

File tree

9 files changed

+72
-44
lines changed

9 files changed

+72
-44
lines changed

SerialPrograms/Source/CommonFramework/Globals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace PokemonAutomation{
2525
const bool IS_BETA_VERSION = true;
2626
const int PROGRAM_VERSION_MAJOR = 0;
2727
const int PROGRAM_VERSION_MINOR = 50;
28-
const int PROGRAM_VERSION_PATCH = 16;
28+
const int PROGRAM_VERSION_PATCH = 17;
2929

3030
const std::string PROGRAM_VERSION_BASE =
3131
"v" + std::to_string(PROGRAM_VERSION_MAJOR) +

SerialPrograms/Source/CommonFramework/Inference/BlackScreenDetector.cpp

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,44 +57,41 @@ bool WhiteScreenDetector::detect(const ImageViewRGB32& screen) const{
5757

5858

5959

60-
BlackScreenWatcher::BlackScreenWatcher(
61-
Color color, const ImageFloatBox& box,
62-
double max_rgb_sum,
63-
double max_stddev_sum
64-
)
65-
: BlackScreenDetector(color, box, max_rgb_sum, max_stddev_sum)
66-
, VisualInferenceCallback("BlackScreenWatcher")
67-
{}
68-
void BlackScreenWatcher::make_overlays(VideoOverlaySet& items) const{
69-
BlackScreenDetector::make_overlays(items);
70-
}
71-
bool BlackScreenWatcher::process_frame(const ImageViewRGB32& frame, WallClock timestamp){
72-
return detect(frame);
73-
}
74-
7560

7661

7762

7863
BlackScreenOverWatcher::BlackScreenOverWatcher(
7964
Color color, const ImageFloatBox& box,
8065
double max_rgb_sum,
81-
double max_stddev_sum
66+
double max_stddev_sum,
67+
std::chrono::milliseconds hold_duration,
68+
std::chrono::milliseconds release_duration
8269
)
8370
: VisualInferenceCallback("BlackScreenOverWatcher")
84-
, m_detector(color, box, max_rgb_sum, max_stddev_sum)
71+
, m_on(color, box, max_rgb_sum, max_stddev_sum, BlackScreenWatcher::FinderType::PRESENT, hold_duration)
72+
, m_off(color, box, max_rgb_sum, max_stddev_sum, BlackScreenWatcher::FinderType::GONE, release_duration)
8573
{}
8674
void BlackScreenOverWatcher::make_overlays(VideoOverlaySet& items) const{
87-
m_detector.make_overlays(items);
75+
m_on.make_overlays(items);
8876
}
8977
bool BlackScreenOverWatcher::process_frame(const ImageViewRGB32& frame, WallClock timestamp){
90-
return black_is_over(frame);
91-
}
92-
bool BlackScreenOverWatcher::black_is_over(const ImageViewRGB32& frame){
93-
if (m_detector.detect(frame)){
94-
m_has_been_black = true;
78+
if (m_black_is_over.load(std::memory_order_acquire)){
79+
return true;
80+
}
81+
if (!m_has_been_black){
82+
m_has_been_black = m_on.process_frame(frame, timestamp);
9583
return false;
9684
}
97-
return m_has_been_black;
85+
86+
bool is_over = m_off.process_frame(frame, timestamp);
87+
if (!is_over){
88+
return false;
89+
}
90+
m_black_is_over.store(true, std::memory_order_release);
91+
return true;
92+
}
93+
bool BlackScreenOverWatcher::black_is_over(const ImageViewRGB32& frame){
94+
return m_black_is_over.load(std::memory_order_acquire);
9895
}
9996

10097

SerialPrograms/Source/CommonFramework/Inference/BlackScreenDetector.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,18 @@ class WhiteScreenDetector : public StaticScreenDetector{
5757
};
5858

5959

60-
class BlackScreenWatcher : public BlackScreenDetector, public VisualInferenceCallback{
60+
class BlackScreenWatcher : public DetectorToFinder<BlackScreenDetector>{
6161
public:
6262
BlackScreenWatcher(
6363
Color color = COLOR_RED,
6464
const ImageFloatBox& box = {0.1, 0.1, 0.8, 0.8},
6565
double max_rgb_sum = 100,
66-
double max_stddev_sum = 10
67-
);
68-
69-
virtual void make_overlays(VideoOverlaySet& items) const override;
70-
virtual bool process_frame(const ImageViewRGB32& frame, WallClock timestamp) override;
66+
double max_stddev_sum = 10,
67+
FinderType finder_type = FinderType::PRESENT,
68+
std::chrono::milliseconds duration = std::chrono::milliseconds(250)
69+
)
70+
: DetectorToFinder("BlackScreenWatcher", finder_type, duration, color, box, max_rgb_sum, max_stddev_sum)
71+
{}
7172
};
7273

7374
// Detect when a period of black screen is over
@@ -77,7 +78,9 @@ class BlackScreenOverWatcher : public VisualInferenceCallback{
7778
Color color = COLOR_RED,
7879
const ImageFloatBox& box = {0.1, 0.1, 0.8, 0.8},
7980
double max_rgb_sum = 100,
80-
double max_stddev_sum = 10
81+
double max_stddev_sum = 10,
82+
std::chrono::milliseconds hold_duration = std::chrono::milliseconds(250),
83+
std::chrono::milliseconds release_duration = std::chrono::milliseconds(250)
8184
);
8285

8386
bool black_is_over(const ImageViewRGB32& frame);
@@ -87,8 +90,10 @@ class BlackScreenOverWatcher : public VisualInferenceCallback{
8790
virtual bool process_frame(const ImageViewRGB32& frame, WallClock timestamp) override;
8891

8992
private:
90-
BlackScreenDetector m_detector;
93+
BlackScreenWatcher m_on;
94+
BlackScreenWatcher m_off;
9195
bool m_has_been_black = false;
96+
std::atomic<bool> m_black_is_over = false;
9297
};
9398

9499

SerialPrograms/Source/CommonFramework/Tools/ErrorDumper.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ std::string dump_image_alone(
4343
void dump_image(
4444
Logger& logger,
4545
const ProgramInfo& program_info, const std::string& label,
46-
const ImageViewRGB32& image
46+
const ImageViewRGB32& image,
47+
ConsoleHandle* console
4748
){
4849
report_error(
4950
&logger,
5051
program_info,
5152
label,
5253
{},
53-
image
54+
image,
55+
console
5456
);
5557
}
5658
void dump_image(

SerialPrograms/Source/CommonFramework/Tools/ErrorDumper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ std::string dump_image_alone(
3232
void dump_image(
3333
Logger& logger,
3434
const ProgramInfo& program_info, const std::string& label,
35-
const ImageViewRGB32& image
35+
const ImageViewRGB32& image,
36+
ConsoleHandle* console = nullptr
3637
);
3738
void dump_image(
3839
const ProgramInfo& program_info,

SerialPrograms/Source/PokemonBDSP/Inference/Battles/PokemonBDSP_EndBattleDetector.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace PokemonBDSP{
2121

2222

2323

24-
24+
#if 0
2525
EndBattleWatcher::EndBattleWatcher(const ImageFloatBox& box, Color color)
2626
: VisualInferenceCallback("EndBattleWatcher")
2727
, m_color(color)
@@ -50,6 +50,7 @@ bool EndBattleWatcher::battle_is_over(const ImageViewRGB32& frame){
5050
}
5151
return true;
5252
}
53+
#endif
5354

5455

5556

SerialPrograms/Source/PokemonBDSP/Inference/Battles/PokemonBDSP_EndBattleDetector.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99

1010
#include "Common/Cpp/Color.h"
1111
#include "CommonFramework/ImageTools/ImageBoxes.h"
12-
#include "CommonFramework/Inference/VisualDetector.h"
13-
#include "CommonFramework/InferenceInfra/VisualInferenceCallback.h"
12+
#include "CommonFramework/Inference/BlackScreenDetector.h"
1413

1514
namespace PokemonAutomation{
1615
namespace NintendoSwitch{
1716
namespace PokemonBDSP{
1817

1918

20-
19+
#if 0
2120
class EndBattleWatcher : public VisualInferenceCallback{
2221
public:
2322
EndBattleWatcher(
@@ -36,6 +35,21 @@ class EndBattleWatcher : public VisualInferenceCallback{
3635
ImageFloatBox m_box;
3736
bool m_has_been_black = false;
3837
};
38+
#endif
39+
40+
41+
class EndBattleWatcher : public BlackScreenOverWatcher{
42+
public:
43+
EndBattleWatcher(
44+
const ImageFloatBox& box = {0.1, 0.1, 0.8, 0.8},
45+
Color color = COLOR_RED
46+
)
47+
: BlackScreenOverWatcher(
48+
color, box, 100, 10, std::chrono::milliseconds(1000)
49+
)
50+
{}
51+
52+
};
3953

4054

4155

SerialPrograms/Source/PokemonBDSP/Programs/Farming/PokemonBDSP_DoublesLeveling.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "CommonFramework/Exceptions/OperationFailedException.h"
88
#include "CommonFramework/Notifications/ProgramNotifications.h"
99
#include "CommonFramework/InferenceInfra/InferenceRoutines.h"
10+
#include "CommonFramework/Inference/FrozenImageDetector.h"
1011
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h"
1112
#include "PokemonSwSh/ShinyHuntTracker.h"
1213
#include "PokemonBDSP/Inference/PokemonBDSP_SelectionArrow.h"
@@ -102,15 +103,17 @@ bool DoublesLeveling::battle(SingleSwitchProgramEnvironment& env, BotBaseContext
102103
BattleMenuWatcher battle_menu(BattleType::STANDARD);
103104
EndBattleWatcher end_battle;
104105
SelectionArrowFinder learn_move(env.console, {0.50, 0.62, 0.40, 0.18}, COLOR_YELLOW);
106+
FrozenImageDetector overworld(std::chrono::seconds(5), 10);
105107
int ret = run_until(
106108
env.console, context,
107109
[](BotBaseContext& context){
108110
pbf_mash_button(context, BUTTON_B, 120 * TICKS_PER_SECOND);
109111
},
110112
{
111-
{battle_menu},
112-
{end_battle},
113-
{learn_move},
113+
battle_menu,
114+
end_battle,
115+
learn_move,
116+
overworld,
114117
}
115118
);
116119
switch (ret){
@@ -131,6 +134,10 @@ bool DoublesLeveling::battle(SingleSwitchProgramEnvironment& env, BotBaseContext
131134
break;
132135
}
133136
return true;
137+
case 3:
138+
env.log("Detected possible overworld!", COLOR_BLUE);
139+
pbf_mash_button(context, BUTTON_B, 250);
140+
return false;
134141
default:
135142
env.log("Timed out.", COLOR_RED);
136143
stats.add_error();

SerialPrograms/Source/PokemonBDSP/Programs/PokemonBDSP_EncounterDetection.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ std::set<std::string> StandardEncounterDetection::read_name(const ImageViewRGB32
130130
dump_image(
131131
m_console, ProgramInfo(),
132132
"StandardEncounterDetection-NameOCR-" + language_data(m_language).code,
133-
screen
133+
screen,
134+
&m_console
134135
);
135136
}else{
136137
for (const auto& item : result.results){

0 commit comments

Comments
 (0)