Skip to content

Commit 922f6a7

Browse files
committed
Improve reliability of LGPE battle arrow detector.
1 parent c4baf9c commit 922f6a7

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

SerialPrograms/Source/CommonTools/StartupChecks/StartProgramChecks.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ void check_performance_class_wired_or_wireless(AbstractController& controller){
5959
throw UserSetupError(
6060
controller.logger(),
6161
"Incompatible Controller:\n\n"
62-
"This program requires a controller with performance class \"Wired\" or \"Wireless\"."
62+
"This program requires a controller with performance class \"Wired\" or \"Wireless\".\n\n"
63+
"If you are using sys-botbase 2, please upgrade to sys-botbase 3."
6364
);
6465
}
6566
}

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
#include "PokemonSwSh/Inference/PokemonSwSh_DialogBoxDetector.h"
144144
#include "CommonTools/Images/SolidColorTest.h"
145145
#include "CommonTools/Async/InterruptableCommands.h"
146+
#include "PokemonLGPE/Inference/Battles/PokemonLGPE_BattleArrowDetector.h"
146147

147148

148149
#include <QPixmap>
@@ -176,7 +177,6 @@ TestProgram_Descriptor::TestProgram_Descriptor()
176177
ProgramControllerClass::StandardController_NoRestrictions,
177178
FeedbackType::OPTIONAL_,
178179
AllowCommandsWhenRunning::ENABLE_COMMANDS,
179-
{},
180180
1, 4, 1
181181
)
182182
{}
@@ -264,6 +264,23 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
264264
ProControllerContext context(scope, console.controller<ProController>());
265265
VideoOverlaySet overlays(overlay);
266266

267+
268+
269+
270+
// auto screenshot = feed.snapshot();
271+
272+
PokemonLGPE::BattleArrowWatcher detector(COLOR_RED, {0.004251, 0.638941, 0.062699, 0.115312});
273+
detector.make_overlays(overlays);
274+
275+
while (true){
276+
cout << detector.process_frame(feed.snapshot()) << endl;
277+
scope.wait_for(100ms);
278+
}
279+
280+
281+
282+
283+
#if 0
267284
AsyncCommandSession<ProController> session(
268285
scope, logger, env.realtime_dispatcher(),
269286
console.controller<ProController>()
@@ -289,7 +306,7 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
289306

290307
delay += 1ms;
291308
}
292-
309+
#endif
293310

294311

295312

SerialPrograms/Source/PokemonLGPE/Inference/Battles/PokemonLGPE_BattleArrowDetector.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ bool BattleArrowDetector::detect(const ImageViewRGB32& screen){
5353
ImageViewRGB32 cropped = extract_box_reference(region, object);
5454
//cropped.save("test-object-" + std::to_string(c++) + ".png");
5555
double rmsd = matcher.rmsd(cropped);
56-
//cout << rmsd << endl;
56+
// cout << rmsd << endl;
5757
if (rmsd < 116){
5858
return true;
5959
}
@@ -63,6 +63,25 @@ bool BattleArrowDetector::detect(const ImageViewRGB32& screen){
6363
}
6464

6565

66+
bool BattleArrowWatcher::process_frame(const ImageViewRGB32& frame, WallClock timestamp){
67+
if (!detect(frame)){
68+
return false;
69+
}
70+
m_detections.emplace_back(timestamp);
71+
72+
WallClock threshold = timestamp - std::chrono::seconds(5);
73+
while (!m_detections.empty() && m_detections.front() < threshold){
74+
m_detections.pop_front();
75+
}
76+
77+
// cout << "m_detections = " << m_detections.size() << endl;
78+
79+
return m_detections.size() >= 3;
80+
}
81+
82+
83+
84+
6685

6786
}
6887
}

SerialPrograms/Source/PokemonLGPE/Inference/Battles/PokemonLGPE_BattleArrowDetector.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#define PokemonAutomation_PokemonLGPE_BattleArrowDetector_H
99

1010
#include <chrono>
11-
#include <atomic>
11+
//#include <atomic>
12+
#include <deque>
1213
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
1314
#include "Common/Cpp/Color.h"
1415
#include "CommonFramework/ImageTools/ImageBoxes.h"
@@ -37,11 +38,28 @@ class BattleArrowDetector : public StaticScreenDetector{
3738
Color m_color;
3839
ImageFloatBox m_box;
3940
};
40-
class BattleArrowWatcher : public DetectorToFinder<BattleArrowDetector>{
41+
42+
43+
//
44+
// We use a custom watcher because the arrow wiggles and thus is less likely to
45+
// maintain consecutive detections.
46+
//
47+
class BattleArrowWatcher : public BattleArrowDetector, public VisualInferenceCallback{
4148
public:
4249
BattleArrowWatcher(Color color = COLOR_RED, ImageFloatBox box = {0.546, 0.863, 0.045, 0.068})
43-
: DetectorToFinder("BattleArrowWatcher", std::chrono::milliseconds(100), color, box)
50+
: BattleArrowDetector(color, box)
51+
, VisualInferenceCallback("BattleArrowWatcher")
4452
{}
53+
54+
virtual void make_overlays(VideoOverlaySet& items) const override{
55+
BattleArrowDetector::make_overlays(items);
56+
}
57+
58+
using VisualInferenceCallback::process_frame;
59+
virtual bool process_frame(const ImageViewRGB32& frame, WallClock timestamp) override;
60+
61+
private:
62+
std::deque<WallClock> m_detections;
4563
};
4664

4765

SerialPrograms/Source/PokemonLGPE/Programs/ShinyHunting/PokemonLGPE_LegendaryReset.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ void LegendaryReset::program(SingleSwitchProgramEnvironment& env, CancellableSco
206206
}
207207
//Shiny found, complete the battle
208208
WallClock start = current_time();
209-
BattleArrowWatcher catching_started(COLOR_RED, {0.005, 0.662, 0.049, 0.069});
209+
BattleArrowWatcher catching_started(COLOR_RED, {0.004251, 0.638941, 0.062699, 0.115312});
210210
int res = run_until<JoyconContext>(
211211
env.console, context,
212212
[&](JoyconContext& context) {

0 commit comments

Comments
 (0)