Skip to content
Closed
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
Expand Up @@ -131,7 +131,7 @@
}


BBQOption::BBQOption(OCR::LanguageOCROption* language_option)

Check failure on line 134 in SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

PokemonAutomation::NintendoSwitch::PokemonSV::BBQOption::BBQOption( OCR :: LanguageOCROption * language_option): function_lines=88, limit=50, allowed 81
: GroupOption("Blueberry Quests", LockMode::UNLOCK_WHILE_RUNNING)
, m_language_owner(language_option == nullptr
? new OCR::LanguageOCROption(
Expand Down Expand Up @@ -194,6 +194,12 @@
"<b>Fix Time when Done:</b><br>Fix the time after the program finishes.",
LockMode::UNLOCK_WHILE_RUNNING, false
)
, SAVE_TARGET_DIAGNOSTICS(
"<b>Save Target Diagnostics:</b><br>For catch, Sneak Up and photo quests, save a frame at each step of reaching the target to DebugDumps/BBQ/targets/. "
"When the target may not have been reached, also save the stream history there, if it is enabled in the global settings. "
"For finding out why these quests fail. The log records each attempt either way.",
LockMode::UNLOCK_WHILE_RUNNING, false
)
{
if (m_language_owner){
PA_ADD_OPTION(LANGUAGE);
Expand All @@ -211,6 +217,7 @@
PA_ADD_OPTION(CATCH_ON_WIN);
PA_ADD_OPTION(NUM_RETRIES);
PA_ADD_OPTION(FIX_TIME_WHEN_DONE);
PA_ADD_OPTION(SAVE_TARGET_DIAGNOSTICS);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class BBQOption : public GroupOption{

BooleanCheckBoxOption FIX_TIME_WHEN_DONE;

BooleanCheckBoxOption SAVE_TARGET_DIAGNOSTICS;

private:
BBQOption(OCR::LanguageOCROption* language_option);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/* BBQ Target Diagnostics
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <iomanip>
#include <sstream>
#include "Common/Cpp/PrettyPrint.h"
#include "Common/Cpp/Filesystem/Filesystem.h"
#include "CommonFramework/GlobalAutoPaths.h"
#include "PokemonSV_BBQTargetDiagnostics.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonSV{


namespace{

std::string quest_slug(BBQuests quest){
const EnumEntry* entry = BBQuests_database().find(quest);
return entry == nullptr ? "unknown" : entry->slug;
}

std::string seconds_to_string(std::chrono::milliseconds elapsed){
std::ostringstream out;
out << std::fixed << std::setprecision(1) << elapsed.count() / 1000.0 << "s";
return out.str();
}

std::string target_diagnostics_directory(){
return DEBUG_PATH() + "BBQ/targets/";
}

}

Check failure on line 36 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTargetDiagnostics.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

namespace-close: close this namespace with // namespace


TargetAttemptOutcome classify_target_attempt(const TargetAttemptSignals& signals){
switch (signals.kind){
case TargetAttemptKind::battle:
if (signals.encounter_detected){
return TargetAttemptOutcome::battle_entered;
}
return signals.battle_menu_seen
? TargetAttemptOutcome::battle_menu_without_encounter
: TargetAttemptOutcome::no_battle;
case TargetAttemptKind::photo:
if (signals.encounter_detected){
return TargetAttemptOutcome::battle_during_photo;
}
return signals.photo_prompt_seen
? TargetAttemptOutcome::photo_prompt_seen
: TargetAttemptOutcome::photo_prompt_timeout;
}
return TargetAttemptOutcome::no_battle;
}

const char* target_attempt_outcome_to_string(TargetAttemptOutcome outcome){
switch (outcome){
case TargetAttemptOutcome::battle_entered: return "battle-entered";
case TargetAttemptOutcome::no_battle: return "no-battle";
case TargetAttemptOutcome::battle_menu_without_encounter: return "battle-menu-without-encounter";
case TargetAttemptOutcome::photo_prompt_seen: return "photo-prompt-seen";
case TargetAttemptOutcome::photo_prompt_timeout: return "photo-prompt-timeout";
case TargetAttemptOutcome::battle_during_photo: return "battle-during-photo";
}
return "unknown";
}

bool target_attempt_needs_history(TargetAttemptOutcome outcome){
switch (outcome){
case TargetAttemptOutcome::battle_entered:
case TargetAttemptOutcome::photo_prompt_seen:
return false;
case TargetAttemptOutcome::no_battle:
case TargetAttemptOutcome::battle_menu_without_encounter:
case TargetAttemptOutcome::photo_prompt_timeout:
case TargetAttemptOutcome::battle_during_photo:
return true;
}
return true;
}

std::string target_attempt_summary(
BBQuests quest,
const TargetAttemptSignals& signals,
std::chrono::milliseconds elapsed
){
std::ostringstream out;
out << "BBQ event=target-attempt quest=" << quest_slug(quest)
<< " outcome=" << target_attempt_outcome_to_string(classify_target_attempt(signals))
<< " encounter=" << signals.encounter_detected;
switch (signals.kind){
case TargetAttemptKind::battle:
out << " battle_menu=" << signals.battle_menu_seen;
break;
case TargetAttemptKind::photo:
out << " photo_prompt=" << signals.photo_prompt_seen;
break;
}
out << " elapsed=" << seconds_to_string(elapsed);
return out.str();
}


TargetAttemptRecorder::TargetAttemptRecorder(VideoStream& stream, BBQuests quest, bool save_files)
: m_stream(stream)
, m_quest(quest)
, m_save_files(save_files)
, m_start(current_time())
, m_file_prefix(target_diagnostics_directory() + now_to_filestring() + "-" + quest_slug(quest))
{}

void TargetAttemptRecorder::checkpoint(const std::string& name){
const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(current_time() - m_start);
m_stream.log(
"BBQ event=target-checkpoint quest=" + quest_slug(m_quest)
+ " checkpoint=" + name + " elapsed=" + seconds_to_string(elapsed)
);
if (m_save_files){
m_frames.emplace_back(name, m_stream.video().snapshot());
}
}

TargetAttemptOutcome TargetAttemptRecorder::finish(const TargetAttemptSignals& signals){

Check warning on line 126 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTargetDiagnostics.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSV::TargetAttemptRecorder::finish( const TargetAttemptSignals & signals): function_lines=29 (advisory above 25, limit 50)
const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(current_time() - m_start);
m_stream.log(target_attempt_summary(m_quest, signals, elapsed));

const TargetAttemptOutcome outcome = classify_target_attempt(signals);
if (!m_save_files){
return outcome;
}

try{
Filesystem::create_directories(Filesystem::Path(target_diagnostics_directory()));
}catch (...){}

for (const auto& [name, frame] : m_frames){
const std::string path = m_file_prefix + "-" + name + ".png";
const bool saved = frame && frame->save(path);
m_stream.log("BBQ event=target-file saved=" + std::to_string(saved) + " path=" + path);
}
m_frames.clear();

// The stream history covers the approach that led here, if the user has
// turned it on in the global settings.
if (target_attempt_needs_history(outcome)){
const std::string path = m_file_prefix + "-approach.mp4";
const bool saved = m_stream.save_stream_history(path);
m_stream.log("BBQ event=target-file saved=" + std::to_string(saved) + " path=" + path);
}
return outcome;
}



}

Check failure on line 158 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTargetDiagnostics.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

namespace-close: close this namespace with // namespace PokemonSV
}

Check failure on line 159 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTargetDiagnostics.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

namespace-close: close this namespace with // namespace NintendoSwitch
}

Check failure on line 160 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTargetDiagnostics.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

namespace-close: close this namespace with // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* BBQ Target Diagnostics
*
* From: https://github.com/PokemonAutomation/
*
* Records how each attempt to reach a catch, Sneak Up or photo target went,
* to find out why so many attempts never find their target. It only observes:
* it sends no controller commands and changes no decision.
*
*/

#ifndef PokemonAutomation_PokemonSV_BBQTargetDiagnostics_H
#define PokemonAutomation_PokemonSV_BBQTargetDiagnostics_H

#include <chrono>
#include <string>
#include <utility>
#include <vector>
#include "Common/Cpp/Time.h"
#include "CommonFramework/Tools/VideoStream.h"
#include "CommonFramework/VideoPipeline/VideoFeed.h"
#include "VideoGames/PokemonSV/Options/PokemonSV_BBQOption.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonSV{


enum class TargetAttemptKind{

Check failure on line 28 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTargetDiagnostics.h

View workflow job for this annotation

GitHub Actions / code-health

Code health

header-comment: document each namespace-scope header symbol
battle, // Catch and Sneak Up: lock on to the target to start a battle.
photo, // Photo: frame the target and take a picture.
};

enum class TargetAttemptOutcome{

Check failure on line 33 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTargetDiagnostics.h

View workflow job for this annotation

GitHub Actions / code-health

Code health

header-comment: document each namespace-scope header symbol
battle_entered,
no_battle,
// The battle menu showed up, but the encounter watcher did not fire, so
// the route treats the attempt as if no battle started.
battle_menu_without_encounter,
photo_prompt_seen,
// Not proof the photo failed: some photo quests still complete.
photo_prompt_timeout,
battle_during_photo,
};

// What the route's detectors reported for one attempt.
struct TargetAttemptSignals{
TargetAttemptKind kind = TargetAttemptKind::battle;
bool encounter_detected = false;
bool battle_menu_seen = false; // Battle routes only.
bool photo_prompt_seen = false; // Photo routes only.
};

TargetAttemptOutcome classify_target_attempt(const TargetAttemptSignals& signals);

Check failure on line 53 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTargetDiagnostics.h

View workflow job for this annotation

GitHub Actions / code-health

Code health

header-comment: document each namespace-scope header symbol
const char* target_attempt_outcome_to_string(TargetAttemptOutcome outcome);

Check failure on line 54 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTargetDiagnostics.h

View workflow job for this annotation

GitHub Actions / code-health

Code health

header-comment: document each namespace-scope header symbol

// Whether the stream history before this outcome is worth keeping: it is,
// whenever the target may not have been reached.
bool target_attempt_needs_history(TargetAttemptOutcome outcome);

// The log line for an attempt, in the same "BBQ event=..." form as the other
// BBQ logs so run audits can grep for it.
std::string target_attempt_summary(
BBQuests quest,
const TargetAttemptSignals& signals,
std::chrono::milliseconds elapsed
);


// Records one attempt: logs each checkpoint and the outcome. With
// "save_files", it also saves the frame at each checkpoint, and the stream
// history when the target may not have been reached, under
// DebugDumps/BBQ/targets/. Files are written in finish(), after the attempt,
// so saving them never delays a controller command during the approach.
class TargetAttemptRecorder{
public:
TargetAttemptRecorder(VideoStream& stream, BBQuests quest, bool save_files);

// Logs a checkpoint and keeps the current frame. Call it only where the
// route already waits for its commands to finish, so the frame shows the
// result of the commands sent so far.
void checkpoint(const std::string& name);

TargetAttemptOutcome finish(const TargetAttemptSignals& signals);

private:
VideoStream& m_stream;
const BBQuests m_quest;
const bool m_save_files;
const WallClock m_start;
const std::string m_file_prefix;
std::vector<std::pair<std::string, VideoSnapshot>> m_frames;
};



}

Check failure on line 96 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTargetDiagnostics.h

View workflow job for this annotation

GitHub Actions / code-health

Code health

namespace-close: close this namespace with // namespace PokemonSV
}
}
#endif
Loading
Loading