Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/* Selection Arrow Detector
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "Common/Cpp/Exceptions.h"
#include "CommonTools/ImageMatch/WaterfillTemplateMatcher.h"
#include "CommonTools/Images/WaterfillUtilities.h"
#include "PokemonFRLG_SelectionArrowDetector.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonFRLG{

ImageFloatBox SelectionArrowDetector::arrow_box_for_position(SelectionArrowPositionStartMenu position){
switch (position){
case SelectionArrowPositionStartMenu::POKEDEX:
return ImageFloatBox(0.685, 0.055, 0.03, 0.075);
case SelectionArrowPositionStartMenu::POKEMON:
return ImageFloatBox(0.685, 0.145, 0.03, 0.075);
case SelectionArrowPositionStartMenu::BAG:
return ImageFloatBox(0.685, 0.235, 0.03, 0.075);
case SelectionArrowPositionStartMenu::TRAINER:
return ImageFloatBox(0.685, 0.330, 0.03, 0.075);
case SelectionArrowPositionStartMenu::SAVE:
return ImageFloatBox(0.685, 0.415, 0.03, 0.075);
case SelectionArrowPositionStartMenu::OPTION:
return ImageFloatBox(0.685, 0.510, 0.03, 0.075);
case SelectionArrowPositionStartMenu::EXIT:
return ImageFloatBox(0.685, 0.6, 0.03, 0.075);
default:
break;
}
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid FRLG Selection Arrow Position");
}

ImageFloatBox SelectionArrowDetector::arrow_box_for_position(SelectionArrowPositionConfirmationMenu position) {
switch (position) {
case SelectionArrowPositionConfirmationMenu::YES:
return ImageFloatBox(0.660, 0.450, 0.03, 0.075);
case SelectionArrowPositionConfirmationMenu::NO:
return ImageFloatBox(0.660, 0.535, 0.03, 0.075);
default:
break;
}
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid FRLG Selection Arrow Position");
}

class SelectionArrowMatcher : public ImageMatch::WaterfillTemplateMatcher{
public:
SelectionArrowMatcher(const char* path)
: WaterfillTemplateMatcher(
path,
Color(50, 50, 50), Color(115, 115, 115), 70
)
{
m_aspect_ratio_lower = 0.8;
m_aspect_ratio_upper = 1.2;
m_area_ratio_lower = 0.8;
m_area_ratio_upper = 1.2;
}

static const SelectionArrowMatcher& matcher(){
static SelectionArrowMatcher matcher("PokemonFRLG/SelectionArrow.png");
return matcher;
}
};

SelectionArrowDetector::SelectionArrowDetector(
Color color,
VideoOverlay* overlay,
const ImageFloatBox& box
)
: m_color(color)
, m_overlay(overlay)
, m_arrow_box(box)
{}
SelectionArrowDetector::SelectionArrowDetector(
Color color,
VideoOverlay* overlay,
SelectionArrowPositionStartMenu position
)
: m_color(color)
, m_overlay(overlay)
, m_arrow_box(arrow_box_for_position(position))
{}
SelectionArrowDetector::SelectionArrowDetector(
Color color,
VideoOverlay* overlay,
SelectionArrowPositionConfirmationMenu position
)
: m_color(color)
, m_overlay(overlay)
, m_arrow_box(arrow_box_for_position(position))
{
}
void SelectionArrowDetector::make_overlays(VideoOverlaySet& items) const{
items.add(m_color, m_arrow_box);
}
bool SelectionArrowDetector::detect(const ImageViewRGB32& screen){
double screen_rel_size = (screen.height() / 1080.0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need use GameSettings::instance().GAME_BOX. Take a look at the other FRLG programs. (https://github.com/PokemonAutomation/Arduino-Source/blob/main/SerialPrograms/Source/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.cpp#L40)

GAME_BOX gives the location of the actual game screen within the entire screen. Then you want all the FRLG inference boxes to be relative to the game box instead of the entire screen.

The idea is that if the game box moves, we only change one setting and everything will work again.

If you want to convert your existing boxes (which are relative to the entire screen) to being relative to the game box, apply this operation:
(x - {0.09375, 0.00462963, 0, 0})/{0.8125, 0.962963, 0.8125, 0.962963}

So taking your first example:

x = {0.685, 0.055, 0.03, 0.075}
(x - {0.09375, 0.00462963, 0, 0})/{0.8125, 0.962963, 0.8125, 0.962963} =
{0.727692, 0.0523077, 0.0369231, 0.0778846}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, to derive box coordinates relative to the game box, Box Draw supports that now.

Set the "Content Box" to {0.8125, 0.962963, 0.8125, 0.962963} (which is the current coordinates of the FRLG game box). Then when you draw the box, it will give you coordinates relative to that.

double screen_rel_size_2 = screen_rel_size * screen_rel_size;

double min_area_1080p = 700;
double rmsd_threshold = 80;
size_t min_area = size_t(screen_rel_size_2 * min_area_1080p);

const std::vector<std::pair<uint32_t, uint32_t>> FILTERS = {
{0xff464646, 0xff787878}
};

bool found = match_template_by_waterfill(
screen.size(),
extract_box_reference(screen, m_arrow_box),
SelectionArrowMatcher::matcher(),
FILTERS,
{min_area, SIZE_MAX},
rmsd_threshold,
[&](Kernels::Waterfill::WaterfillObject& object) -> bool {
m_last_detected = translate_to_parent(screen, m_arrow_box, object);
return true;
}
);

if (m_overlay){
if (found){
m_last_detected_box.emplace(*m_overlay, m_last_detected, COLOR_GREEN);
}else{
m_last_detected_box.reset();
}
}

return found;
}


}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* Selection Arrow Detector
*
* From: https://github.com/PokemonAutomation/
*
*/

#ifndef PokemonAutomation_PokemonFRLG_SelectionArrowDetector_H
#define PokemonAutomation_PokemonFRLG_SelectionArrowDetector_H

#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
#include "CommonTools/VisualDetector.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonFRLG{

const int START_MENU_OPTION_COUNT = 7;
// The order of these enums should be the same as the order of options in the game menu, from top to bottom, for ease of use with loops.
enum class SelectionArrowPositionStartMenu{
POKEDEX,
POKEMON,
BAG,
TRAINER,
SAVE,
OPTION,
EXIT
};

enum class SelectionArrowPositionConfirmationMenu {
YES,
NO
};

class SelectionArrowDetector : public StaticScreenDetector{
public:
SelectionArrowDetector(
Color color,
VideoOverlay* overlay,
const ImageFloatBox& box
);

SelectionArrowDetector(
Color color,
VideoOverlay* overlay,
SelectionArrowPositionStartMenu position
);

SelectionArrowDetector(
Color color,
VideoOverlay* overlay,
SelectionArrowPositionConfirmationMenu position
);

static ImageFloatBox arrow_box_for_position(SelectionArrowPositionStartMenu position);

static ImageFloatBox arrow_box_for_position(SelectionArrowPositionConfirmationMenu position);

const ImageFloatBox& last_detected() const { return m_last_detected; }

virtual void make_overlays(VideoOverlaySet& items) const override;

// This is not const so that detectors can save/cache state.
virtual bool detect(const ImageViewRGB32& screen) override;

private:
friend class SelectionArrowWatcher;

const Color m_color;
VideoOverlay* m_overlay;
const ImageFloatBox m_arrow_box;

ImageFloatBox m_last_detected;
std::optional<OverlayBoxScope> m_last_detected_box;
};
class SelectionArrowWatcher : public DetectorToFinder<SelectionArrowDetector>{
public:
SelectionArrowWatcher(
Color color,
VideoOverlay* overlay,
const ImageFloatBox& box,
std::chrono::milliseconds hold_duration = std::chrono::milliseconds(250)
)
: DetectorToFinder("SelectionArrowWatcher", hold_duration, color, overlay, box)
{}
SelectionArrowWatcher(
Color color,
VideoOverlay* overlay,
SelectionArrowPositionStartMenu position,
std::chrono::milliseconds hold_duration = std::chrono::milliseconds(250)
)
: DetectorToFinder("SelectionArrowWatcher", hold_duration, color, overlay, arrow_box_for_position(position))
{
}
SelectionArrowWatcher(
Color color,
VideoOverlay* overlay,
SelectionArrowPositionConfirmationMenu position,
std::chrono::milliseconds hold_duration = std::chrono::milliseconds(250)
)
: DetectorToFinder("SelectionArrowWatcher", hold_duration, color, overlay, arrow_box_for_position(position))
{
}
};


}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "PokemonFRLG/Inference/Menus/PokemonFRLG_StartMenuDetector.h"
#include "PokemonFRLG/PokemonFRLG_Navigation.h"
#include "PokemonFRLG_NuggetBridgeFarmer.h"
#include "PokemonFRLG/Inference/PokemonFRLG_SelectionArrowDetector.h"
#include <PokemonFRLG/Programs/PokemonFRLG_StartMenuNavigation.h>

namespace PokemonAutomation{
namespace NintendoSwitch{
Expand Down Expand Up @@ -55,17 +57,17 @@ NuggetBridgeFarmer::NuggetBridgeFarmer()
"<b>Number of Nuggets:</b><br>"
"Zero will run until 'Stop after Current Nugget' is pressed or the program is manually stopped.",
LockMode::UNLOCK_WHILE_RUNNING,
120, // About 2 hours of farming.
100, // About 2 hours of farming.
0
)
, GO_HOME_WHEN_DONE(false)
/*, PERIODIC_SAVE(
, PERIODIC_SAVE(
"<b>Periodically Save:</b><br>"
"Save the game every this many nuggets. This reduces the loss to game crashes. Set to zero to disable.",
LockMode::UNLOCK_WHILE_RUNNING,
10,
50,
0
)*/
)
, NOTIFICATION_STATUS_UPDATE("Status Update", true, false, std::chrono::seconds(3600))
, NOTIFICATIONS({
&NOTIFICATION_STATUS_UPDATE,
Expand All @@ -77,7 +79,7 @@ NuggetBridgeFarmer::NuggetBridgeFarmer()

PA_ADD_OPTION(NUM_NUGGETS);
PA_ADD_OPTION(GO_HOME_WHEN_DONE);
//PA_ADD_OPTION(PERIODIC_SAVE);
PA_ADD_OPTION(PERIODIC_SAVE);

PA_ADD_OPTION(NOTIFICATIONS);
}
Expand All @@ -89,13 +91,19 @@ void NuggetBridgeFarmer::program(SingleSwitchProgramEnvironment& env, ProControl
NuggetBridgeFarmer_Descriptor::Stats& stats = env.current_stats<NuggetBridgeFarmer_Descriptor::Stats>();
DeferredStopButtonOption::ResetOnExit reset_on_exit(STOP_AFTER_CURRENT);

//for (uint32_t nuggets_since_last_save = 0;; nuggets_since_last_save++) {
while (true) {
for (uint32_t nuggets_since_last_save = 0;; nuggets_since_last_save++) {
send_program_status_notification(env, NOTIFICATION_STATUS_UPDATE);
if (NUM_NUGGETS != 0 && stats.nuggets >= NUM_NUGGETS) {
break;
}

if (PERIODIC_SAVE != 0 && nuggets_since_last_save >= PERIODIC_SAVE) {
env.console.log("Saving game...");

save_game_to_overworld(env.console, context);
nuggets_since_last_save = 0;
}

env.console.log("Exiting Pokemon Center...");
while (true){
BlackScreenWatcher pokemon_ceter_exit(COLOR_RED);
Expand Down Expand Up @@ -210,41 +218,6 @@ void NuggetBridgeFarmer::program(SingleSwitchProgramEnvironment& env, ProControl
}
}

//TODO: Implement periodic saving. The Save Menu keeps the last cursor position. Need to implement arrow dectection on the correct option
/*if (PERIODIC_SAVE != 0 && nuggets_since_last_save >= PERIODIC_SAVE) {
StartMenuWatcher start_menu = StartMenuWatcher(COLOR_RED);

env.console.log("Saving game...");

while (true)
{
int ret = run_until<ProControllerContext>(
env.console, context,
[](ProControllerContext& context) {
pbf_press_button(context, BUTTON_PLUS, 320ms, 640ms);
pbf_wait(context, 100ms);
context.wait_for_all_requests();
},
{ start_menu }
);

if (ret == 0) {
break;
}
}

pbf_press_dpad(context, DPAD_DOWN, 320ms, 320ms);
pbf_press_dpad(context, DPAD_DOWN, 320ms, 320ms);
pbf_press_dpad(context, DPAD_DOWN, 320ms, 320ms);
pbf_press_dpad(context, DPAD_DOWN, 320ms, 320ms);

pbf_press_button(context, BUTTON_A, 320ms, 320ms);
pbf_press_button(context, BUTTON_A, 320ms, 320ms);

pbf_mash_button(context, BUTTON_B, 2000ms);
nuggets_since_last_save = 0;
}*/

stats.nuggets++;
env.update_stats();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class NuggetBridgeFarmer : public SingleSwitchProgramInstance {
SimpleIntegerOption<uint32_t> NUM_NUGGETS;
GoHomeWhenDoneOption GO_HOME_WHEN_DONE;

//SimpleIntegerOption<uint32_t> PERIODIC_SAVE;
SimpleIntegerOption<uint32_t> PERIODIC_SAVE;

EventNotificationOption NOTIFICATION_STATUS_UPDATE;
EventNotificationsOption NOTIFICATIONS;
Expand Down
Loading