Skip to content

Commit 2507f15

Browse files
committed
Refactor platform bot and scatterbug to properly count and detect shinies in chain attacks.
1 parent 383caff commit 2507f15

20 files changed

+948
-197
lines changed

SerialPrograms/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,8 @@ file(GLOB MAIN_SOURCES
15431543
Source/PokemonSV/Inference/Battles/PokemonSV_PostCatchDetector.h
15441544
Source/PokemonSV/Inference/Battles/PokemonSV_ShinySoundDetector.cpp
15451545
Source/PokemonSV/Inference/Battles/PokemonSV_ShinySoundDetector.h
1546+
Source/PokemonSV/Inference/Battles/PokemonSV_StartBattleYellowBar.cpp
1547+
Source/PokemonSV/Inference/Battles/PokemonSV_StartBattleYellowBar.h
15461548
Source/PokemonSV/Inference/Battles/PokemonSV_TeraBattleMenus.cpp
15471549
Source/PokemonSV/Inference/Battles/PokemonSV_TeraBattleMenus.h
15481550
Source/PokemonSV/Inference/Boxes/PokemonSV_BoxDetection.cpp
@@ -1597,6 +1599,8 @@ file(GLOB MAIN_SOURCES
15971599
Source/PokemonSV/Inference/Overworld/PokemonSV_OliveDetector.h
15981600
Source/PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.cpp
15991601
Source/PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.h
1602+
Source/PokemonSV/Inference/Overworld/PokemonSV_OverworldSensors.cpp
1603+
Source/PokemonSV/Inference/Overworld/PokemonSV_OverworldSensors.h
16001604
Source/PokemonSV/Inference/Overworld/PokemonSV_StationaryOverworldWatcher.cpp
16011605
Source/PokemonSV/Inference/Overworld/PokemonSV_StationaryOverworldWatcher.h
16021606
Source/PokemonSV/Inference/Picnics/PokemonSV_PicnicDetector.cpp

SerialPrograms/Source/CommonTools/Audio/AudioPerSpectrumDetectorBase.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ AudioPerSpectrumDetectorBase::AudioPerSpectrumDetectorBase(
3838
, m_start_timestamp(current_time())
3939
, m_spectrums_processed(0)
4040
{}
41+
void AudioPerSpectrumDetectorBase::set_detected_callback(DetectedCallback detected_callback){
42+
m_detected_callback = std::move(detected_callback);
43+
}
4144
AudioPerSpectrumDetectorBase::~AudioPerSpectrumDetectorBase(){
4245
try{
4346
log_results();

SerialPrograms/Source/CommonTools/Audio/AudioPerSpectrumDetectorBase.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class SpectrogramMatcher;
4747
// - std::unique_ptr<SpectrogramMatcher> build_spectrogram_matcher(size_t sample_rate)
4848
class AudioPerSpectrumDetectorBase : public AudioInferenceCallback{
4949
public:
50+
virtual ~AudioPerSpectrumDetectorBase();
51+
5052
using DetectedCallback = std::function<bool(float error_coefficient)>;
5153
// label: a name for this detector. Used for logging and profiling.
5254
// audio_name: the name of the audio to be detected (shiny sound etc). Capitalize first letter.
@@ -61,10 +63,10 @@ class AudioPerSpectrumDetectorBase : public AudioInferenceCallback{
6163
std::string label,
6264
std::string audio_name,
6365
Color detection_color,
64-
DetectedCallback detected_callback
66+
DetectedCallback detected_callback = nullptr
6567
);
6668

67-
virtual ~AudioPerSpectrumDetectorBase();
69+
void set_detected_callback(DetectedCallback detected_callback);
6870

6971
WallClock last_detection() const{
7072
return m_last_timestamp;

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
#include "NintendoSwitch/Inference/NintendoSwitch2_BinarySliderDetector.h"
134134
#include "PokemonSwSh/Programs/PokemonSwSh_GameEntry.h"
135135
#include "PokemonSwSh/PokemonSwSh_Settings.h"
136+
#include "PokemonSV/Inference/Battles/PokemonSV_StartBattleYellowBar.h"
136137

137138
#include <QPixmap>
138139
#include <QVideoFrame>
@@ -286,9 +287,25 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
286287
VideoOverlaySet overlays(overlay);
287288

288289

290+
auto screenshot = feed.snapshot();
289291

290292

291-
#if 1
293+
294+
StartBattleYellowBarDetector detector(COLOR_RED);
295+
cout << detector.detect(screenshot) << endl;
296+
297+
298+
299+
300+
// cout << "asdf" << endl;
301+
// cout << (int)settings_detect_console_type(console, context) << endl;
302+
303+
304+
305+
// DateReader_Switch2_US reader(COLOR_RED);
306+
// reader.read_date(logger, screenshot);
307+
308+
#if 0
292309
HomeMenuDetector detector0(console);
293310
StartGameUserSelectDetector detector1(console);
294311
UpdatePopupDetector detector2(console);

SerialPrograms/Source/PokemonSV/Inference/Battles/PokemonSV_EncounterWatcher.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ namespace PokemonAutomation{
1818
namespace NintendoSwitch{
1919
namespace PokemonSV{
2020

21-
21+
//
22+
// This class is now deprecated.
23+
//
2224
class EncounterWatcher : public VisualInferenceCallback, public AudioInferenceCallback{
2325
public:
2426
EncounterWatcher(VideoStream& stream, Color color = COLOR_RED);
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* Start Battle Yellow Bar
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
8+
#include "CommonTools/Images/SolidColorTest.h"
9+
#include "PokemonSV_StartBattleYellowBar.h"
10+
11+
//#include <iostream>
12+
//using std::cout;
13+
//using std::endl;
14+
15+
namespace PokemonAutomation{
16+
namespace NintendoSwitch{
17+
namespace PokemonSV{
18+
19+
20+
21+
StartBattleYellowBarDetector::StartBattleYellowBarDetector(Color color)
22+
: m_color(color)
23+
, m_top(0.100000, 0.001000, 0.800000, 0.004000)
24+
, m_bot(0.100000, 0.995000, 0.800000, 0.004000)
25+
, m_mid(0.1, 0.1, 0.8, 0.8)
26+
{}
27+
void StartBattleYellowBarDetector::make_overlays(VideoOverlaySet& items) const{
28+
items.add(m_color, m_top);
29+
items.add(m_color, m_bot);
30+
}
31+
32+
bool StartBattleYellowBarDetector::detect(const ImageViewRGB32& screen){
33+
ImageStats top = image_stats(extract_box_reference(screen, m_top));
34+
ImageStats bot = image_stats(extract_box_reference(screen, m_bot));
35+
// cout << "top = " << top.average << top.stddev << endl;
36+
// cout << "bot = " << bot.average << top.stddev << endl;
37+
if (!is_solid(top, {0.554825, 0.445175, 0.})){
38+
return false;
39+
}
40+
if (!is_solid(bot, {0.554825, 0.445175, 0.})){
41+
return false;
42+
}
43+
44+
if (euclidean_distance(top.average, bot.average) > 10){
45+
return false;
46+
}
47+
48+
ImageStats mid = image_stats(extract_box_reference(screen, m_mid));
49+
// cout << "mid = " << mid.average << mid.stddev << endl;
50+
if (mid.stddev.sum() < 10){
51+
return false;
52+
}
53+
54+
return true;
55+
}
56+
57+
58+
59+
60+
void StartBattleYellowBarWatcher::make_overlays(VideoOverlaySet& items) const{
61+
StartBattleYellowBarDetector::make_overlays(items);
62+
}
63+
bool StartBattleYellowBarWatcher::process_frame(const ImageViewRGB32& frame, WallClock timestamp){
64+
if (!detect(frame)){
65+
m_start_detection = WallClock::min();
66+
return false;
67+
}
68+
69+
// Not long enough.
70+
if (m_start_detection + std::chrono::milliseconds(100) > timestamp){
71+
return false;
72+
}
73+
74+
m_last_detected = timestamp;
75+
// cout << "Detected yellow bar." << endl;
76+
return false;
77+
}
78+
79+
80+
81+
82+
}
83+
}
84+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* Start Battle Yellow Bar
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_PokemonSV_StartBattleYellowBar_H
8+
#define PokemonAutomation_PokemonSV_StartBattleYellowBar_H
9+
10+
#include <atomic>
11+
#include "Common/Cpp/Color.h"
12+
#include "CommonFramework/ImageTools/ImageBoxes.h"
13+
#include "CommonTools/VisualDetector.h"
14+
15+
namespace PokemonAutomation{
16+
namespace NintendoSwitch{
17+
namespace PokemonSV{
18+
19+
20+
21+
class StartBattleYellowBarDetector : public StaticScreenDetector{
22+
public:
23+
StartBattleYellowBarDetector(Color color);
24+
virtual void make_overlays(VideoOverlaySet& items) const override;
25+
26+
virtual bool detect(const ImageViewRGB32& screen) override;
27+
28+
private:
29+
Color m_color;
30+
ImageFloatBox m_top;
31+
ImageFloatBox m_bot;
32+
ImageFloatBox m_mid;
33+
};
34+
class StartBattleYellowBarWatcher : public StartBattleYellowBarDetector, public VisualInferenceCallback{
35+
public:
36+
StartBattleYellowBarWatcher(Color color)
37+
: StartBattleYellowBarDetector(color)
38+
, VisualInferenceCallback("StartBattleYellowBarWatcher")
39+
, m_start_detection(WallClock::min())
40+
, m_last_detected(WallClock::min())
41+
{}
42+
43+
WallClock last_detected() const{
44+
return m_last_detected.load(std::memory_order_relaxed);
45+
}
46+
47+
virtual void make_overlays(VideoOverlaySet& items) const override;
48+
virtual bool process_frame(const ImageViewRGB32& frame, WallClock timestamp) override;
49+
50+
private:
51+
WallClock m_start_detection;
52+
std::atomic<WallClock> m_last_detected;
53+
};
54+
55+
56+
57+
58+
}
59+
}
60+
}
61+
#endif

SerialPrograms/Source/PokemonSV/Inference/Overworld/PokemonSV_LetsGoKillDetector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class LetsGoKillWatcher : public DetectorToFinder<LetsGoKillDetector>{
6262

6363
class LetsGoKillSoundDetector : public AudioPerSpectrumDetectorBase{
6464
public:
65-
LetsGoKillSoundDetector(Logger& logger, DetectedCallback detected_callback);
65+
LetsGoKillSoundDetector(Logger& logger, DetectedCallback detected_callback = nullptr);
6666

6767
virtual float get_score_threshold() const override;
6868

0 commit comments

Comments
 (0)