Skip to content

Commit

Permalink
Refactor out the raid filter from Tera Roller.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mysticial committed Sep 15, 2023
1 parent d91ff34 commit 617451c
Show file tree
Hide file tree
Showing 12 changed files with 335 additions and 179 deletions.
2 changes: 2 additions & 0 deletions SerialPrograms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,8 @@ file(GLOB MAIN_SOURCES
Source/PokemonSV/Options/PokemonSV_TeraAIOption.h
Source/PokemonSV/Options/PokemonSV_TeraMoveTable.cpp
Source/PokemonSV/Options/PokemonSV_TeraMoveTable.h
Source/PokemonSV/Options/PokemonSV_TeraRollFilter.cpp
Source/PokemonSV/Options/PokemonSV_TeraRollFilter.h
Source/PokemonSV/Options/PokemonSV_TournamentPrizeSelectOption.cpp
Source/PokemonSV/Options/PokemonSV_TournamentPrizeSelectOption.h
Source/PokemonSV/Options/PokemonSV_TournamentPrizeTable.cpp
Expand Down
4 changes: 4 additions & 0 deletions SerialPrograms/SerialPrograms.pro
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ SOURCES += \
Source/PokemonSV/Options/PokemonSV_SandwichMakerOption.cpp \
Source/PokemonSV/Options/PokemonSV_TeraAIOption.cpp \
Source/PokemonSV/Options/PokemonSV_TeraMoveTable.cpp \
Source/PokemonSV/Options/PokemonSV_TeraRollFilter.cpp \
Source/PokemonSV/Options/PokemonSV_TournamentPrizeSelectOption.cpp \
Source/PokemonSV/Options/PokemonSV_TournamentPrizeTable.cpp \
Source/PokemonSV/PokemonSV_Panels.cpp \
Expand All @@ -701,6 +702,7 @@ SOURCES += \
Source/PokemonSV/Programs/General/PokemonSV_MassRelease.cpp \
Source/PokemonSV/Programs/General/PokemonSV_SizeChecker.cpp \
Source/PokemonSV/Programs/General/PokemonSV_StatsReset.cpp \
Source/PokemonSV/Programs/General/PokemonSV_StatsResetBloodmoon.cpp \
Source/PokemonSV/Programs/General/PokemonSV_TournamentFarmer.cpp \
Source/PokemonSV/Programs/Glitches/PokemonSV_CloneItems-1.0.1.cpp \
Source/PokemonSV/Programs/Glitches/PokemonSV_RideCloner-1.0.1.cpp \
Expand Down Expand Up @@ -1714,6 +1716,7 @@ HEADERS += \
Source/PokemonSV/Options/PokemonSV_SandwichMakerOption.h \
Source/PokemonSV/Options/PokemonSV_TeraAIOption.h \
Source/PokemonSV/Options/PokemonSV_TeraMoveTable.h \
Source/PokemonSV/Options/PokemonSV_TeraRollFilter.h \
Source/PokemonSV/Options/PokemonSV_TournamentPrizeSelectOption.h \
Source/PokemonSV/Options/PokemonSV_TournamentPrizeTable.h \
Source/PokemonSV/PokemonSV_Panels.h \
Expand All @@ -1738,6 +1741,7 @@ HEADERS += \
Source/PokemonSV/Programs/General/PokemonSV_MassRelease.h \
Source/PokemonSV/Programs/General/PokemonSV_SizeChecker.h \
Source/PokemonSV/Programs/General/PokemonSV_StatsReset.h \
Source/PokemonSV/Programs/General/PokemonSV_StatsResetBloodmoon.h \
Source/PokemonSV/Programs/General/PokemonSV_TournamentFarmer.h \
Source/PokemonSV/Programs/Glitches/PokemonSV_CloneItems-1.0.1.h \
Source/PokemonSV/Programs/Glitches/PokemonSV_RideCloner-1.0.1.h \
Expand Down
2 changes: 1 addition & 1 deletion SerialPrograms/Source/CommonFramework/Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace PokemonAutomation{
const bool IS_BETA_VERSION = true;
const int PROGRAM_VERSION_MAJOR = 0;
const int PROGRAM_VERSION_MINOR = 39;
const int PROGRAM_VERSION_PATCH = 3;
const int PROGRAM_VERSION_PATCH = 5;

const std::string PROGRAM_VERSION_BASE =
"v" + std::to_string(PROGRAM_VERSION_MAJOR) +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#include <array>
#include <map>
#include "Common/Cpp/PrettyPrint.h"
#include "Common/Qt/StringToolsQt.h"
//#include "Kernels/Waterfill/Kernels_Waterfill.h"
#include "Kernels/Waterfill/Kernels_Waterfill_Session.h"
#include "CommonFramework/GlobalSettingsPanel.h"
#include "CommonFramework/ImageTypes/ImageRGB32.h"
#include "CommonFramework/ImageTypes/BinaryImage.h"
#include "CommonFramework/ImageTools/SolidColorTest.h"
Expand All @@ -20,7 +20,8 @@
//#include "CommonFramework/ImageTools/ImageFilter.h"
#include "CommonFramework/OCR/OCR_RawOCR.h"
//#include "CommonFramework/OCR/OCR_NumberReader.h"
//#include "CommonFramework/Tools/ErrorDumper.h"
#include "CommonFramework/Tools/DebugDumper.h"
#include "CommonFramework/Tools/ErrorDumper.h"
//#include "CommonFramework/OCR/OCR_Routines.h"
#include "PokemonSV/Inference/Dialogs/PokemonSV_GradientArrowDetector.h"
#include "PokemonSV_TeraCodeReader.h"
Expand All @@ -47,6 +48,8 @@ TeraCardReader::TeraCardReader(Color color)
, m_label(CARD_LABEL_BOX())
, m_cursor(0.135, 0.25, 0.05, 0.25)
, m_stars(0.500, 0.555, 0.310, 0.070)
, m_tera_type(color)
, m_silhouette(color)
{}
void TeraCardReader::make_overlays(VideoOverlaySet& items) const{
items.add(m_color, m_top);
Expand All @@ -55,6 +58,8 @@ void TeraCardReader::make_overlays(VideoOverlaySet& items) const{
items.add(m_color, m_label);
items.add(m_color, m_cursor);
items.add(m_color, m_stars);
m_tera_type.make_overlays(items);
m_silhouette.make_overlays(items);
}

const ImageFloatBox& TeraCardReader::CARD_LABEL_BOX(){
Expand Down Expand Up @@ -143,6 +148,40 @@ size_t TeraCardReader::stars(const ImageViewRGB32& screen) const{

return 0;
}
std::string TeraCardReader::tera_type(
Logger& logger, const ProgramInfo& info, const ImageViewRGB32& screen
) const{
ImageMatch::ImageMatchResult type = m_tera_type.read(screen);
type.log(logger, 100);
std::string best_type;
if (!type.results.empty()){
best_type = type.results.begin()->second;
}
if (best_type.empty()){
dump_image(logger, info, "ReadTypeFailed", screen);
}else if (PreloadSettings::debug().IMAGE_TEMPLATE_MATCHING){
dump_debug_image(logger, "PokemonSV/TeraRoller/" + best_type, "", screen);
}

return best_type;
}
std::string TeraCardReader::pokemon_slug(
Logger& logger, const ProgramInfo& info, const ImageViewRGB32& screen
) const{
ImageMatch::ImageMatchResult silhouette = m_silhouette.read(screen);
silhouette.log(logger, 100);
std::string best_silhouette;
if (!silhouette.results.empty()){
best_silhouette = silhouette.results.begin()->second;
}
if (silhouette.results.empty()){
dump_image(logger, info, "ReadSilhouetteFailed", screen);
}else if (PreloadSettings::debug().IMAGE_TEMPLATE_MATCHING){
dump_debug_image(logger, "PokemonSV/TeraRoller/" + best_silhouette, "", screen);
}

return best_silhouette;
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
#include "Common/Cpp/AbstractLogger.h"
#include "CommonFramework/ImageTools/ImageBoxes.h"
#include "CommonFramework/Inference/VisualDetector.h"
#include "CommonFramework/InferenceInfra/VisualInferenceCallback.h"
//#include "CommonFramework/InferenceInfra/VisualInferenceCallback.h"
#include "PokemonSV/Options/PokemonSV_PlayerList.h"
#include "PokemonSV_TeraTypeReader.h"
#include "PokemonSV_TeraSilhouetteReader.h"

namespace PokemonAutomation{
class AsyncDispatcher;
Expand All @@ -35,6 +37,12 @@ class TeraCardReader : public StaticScreenDetector{
virtual bool detect(const ImageViewRGB32& screen) const override;

size_t stars(const ImageViewRGB32& screen) const;
std::string tera_type(
Logger& logger, const ProgramInfo& info, const ImageViewRGB32& screen
) const;
std::string pokemon_slug(
Logger& logger, const ProgramInfo& info, const ImageViewRGB32& screen
) const;

static const ImageFloatBox& CARD_LABEL_BOX();
static bool is_card_label(const ImageViewRGB32& screen);
Expand All @@ -46,7 +54,10 @@ class TeraCardReader : public StaticScreenDetector{
ImageFloatBox m_bottom_right;
ImageFloatBox m_label;
ImageFloatBox m_cursor;

ImageFloatBox m_stars;
TeraTypeReader m_tera_type;
TeraSilhouetteReader m_silhouette;
};
class TeraCardWatcher : public DetectorToFinder<TeraCardReader>{
public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
#ifndef PokemonAutomation_PokemonSV_TeraMoveTable_H
#define PokemonAutomation_PokemonSV_TeraMoveTable_H

//#include "Common/Cpp/Options/GroupOption.h"
//#include "Common/Cpp/Options/StaticTextOption.h"
//#include "Common/Cpp/Options/BooleanCheckBoxOption.h"
#include "Common/Cpp/Options/SimpleIntegerOption.h"
#include "Common/Cpp/Options/StringOption.h"
#include "Common/Cpp/Options/EnumDropdownOption.h"
Expand Down
171 changes: 171 additions & 0 deletions SerialPrograms/Source/PokemonSV/Options/PokemonSV_TeraRollFilter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/* Tera Roll Filter
*
* From: https://github.com/PokemonAutomation/Arduino-Source
*
*/

#include "Common/Cpp/Exceptions.h"
#include "ClientSource/Connection/BotBase.h"
#include "CommonFramework/VideoPipeline/VideoFeed.h"
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
#include "CommonFramework/Tools/ConsoleHandle.h"
//#include "CommonFramework/Tools/DebugDumper.h"
#include "CommonFramework/Tools/ErrorDumper.h"
#include "Pokemon/Pokemon_Strings.h"
#include "PokemonSV/Inference/Tera/PokemonSV_TeraCardDetector.h"
#include "PokemonSV/Programs/TeraRaids/PokemonSV_TeraRoutines.h"
#include "PokemonSV_TeraRollFilter.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonSV{


TeraRollFilter::TeraRollFilter()
: GroupOption("Opponent Filter", LockWhileRunning::UNLOCKED)
, EVENT_CHECK_MODE(
"<b>Event Tera Raid Action:</b><br>Choose how the program interacts with event/non-event raids."
"<br>Check only non-event can be further sped up if you exclude 6 star from your filters.",
{
{EventCheckMode::CHECK_ALL, "check_all", "Check everything. Don't filter by event."},
{EventCheckMode::CHECK_ONLY_EVENT, "check_event", "Check only event raids."},
{EventCheckMode::CHECK_ONLY_NONEVENT, "check_nonevent", "Check only non-event raids."},
},
LockWhileRunning::UNLOCKED,
EventCheckMode::CHECK_ALL
)
, MIN_STARS(
"<b>Min Stars:</b><br>Skip raids with less than this many stars.",
LockWhileRunning::UNLOCKED,
1, 1, 7
)
, MAX_STARS(
"<b>Max Stars:</b><br>Skip raids with more than this many stars.",
LockWhileRunning::UNLOCKED,
4, 1, 7
)
{
PA_ADD_OPTION(EVENT_CHECK_MODE);
PA_ADD_OPTION(MIN_STARS);
PA_ADD_OPTION(MAX_STARS);
}

void TeraRollFilter::start_program_check(Logger& logger) const{
if (MIN_STARS > MAX_STARS){
throw UserSetupError(logger, "Error in the settings, \"Min Stars\" is bigger than \"Max Stars\".");
}
}

TeraRollFilter::FilterResult TeraRollFilter::run_filter(
const ProgramInfo& info, ConsoleHandle& console, BotBaseContext& context,
std::string* pokemon_slug, size_t* stars
){
if (pokemon_slug != nullptr){
pokemon_slug->clear();
}
if (stars != nullptr){
*stars = 0;
}

uint8_t min_stars = MIN_STARS;
uint8_t max_stars = MAX_STARS;
EventCheckMode event_check_mode = EVENT_CHECK_MODE;

bool sparkling_raid = false;

switch (event_check_mode){
case EventCheckMode::CHECK_ALL:
break;
case EventCheckMode::CHECK_ONLY_EVENT:
// this makes sure that we only check sparkling raids
// and that includes event & 6 star raids
// a later star check will be performed to exclude 6 star raids
if (!is_sparkling_raid(console, context)){
console.log("No sparkling raid detected, skipping...", COLOR_ORANGE);
return FilterResult::NO_RAID;
}
break;
case EventCheckMode::CHECK_ONLY_NONEVENT:
if (is_sparkling_raid(console, context)){
// if the user excluded 6 star raids, skip sparkling raids
if (min_stars > 6 || max_stars < 6){
console.log("Sparkling raid detected, skipping...", COLOR_ORANGE);
return FilterResult::FAILED;
}
// if the user included 6 star raids, defer skip decision
sparkling_raid = true;
}
break;
}

if (!open_raid(console, context)){
return FilterResult::NO_RAID;
}
context.wait_for(std::chrono::milliseconds(500));

VideoSnapshot screen = console.video().snapshot();
TeraCardReader reader(COLOR_RED);
size_t read_stars = reader.stars(screen);
if (stars != nullptr){
*stars = read_stars;
}
if (read_stars == 0){
dump_image(console, info, "ReadStarsFailed", *screen.frame);
}

switch (event_check_mode){
case EventCheckMode::CHECK_ALL:
break;
case EventCheckMode::CHECK_ONLY_EVENT:
// only sparkling raids at this point
// skip 6 star raids
if (read_stars == 6){
console.log("Detected non-event 6 star raid, skipping...", COLOR_ORANGE);
close_raid(info, console, context);
return FilterResult::NO_RAID;
}
break;
case EventCheckMode::CHECK_ONLY_NONEVENT:
// skip sparkling raids unless 6 stars
if (sparkling_raid && read_stars != 6){
console.log("Detected event raid, skipping...", COLOR_ORANGE);
close_raid(info, console, context);
return FilterResult::NO_RAID;
}
break;
}
context.wait_for_all_requests();


VideoOverlaySet overlay_set(console);

std::string silhouette = reader.pokemon_slug(console, info, screen);
if (silhouette.empty()){
silhouette = "unknown " + Pokemon::STRING_POKEMON;
}

std::string tera_type = reader.tera_type(console, info, screen);
if (tera_type.empty()){
tera_type = "unknown tera type";
}

std::string log = "Detected a " + std::to_string(read_stars) + "* " + tera_type + " " + silhouette;
console.overlay().add_log(log, COLOR_GREEN);
console.log(log);

if (read_stars < min_stars || read_stars > max_stars){
close_raid(info, console, context);
return FilterResult::FAILED;
}

// TODO: Add species filter

return FilterResult::PASSED;
}




}
}
}
Loading

0 comments on commit 617451c

Please sign in to comment.