Skip to content
Draft
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 @@ -29,6 +29,7 @@
#include "Inference/PokemonFRLG_WildEncounterReader.h"
#include "Programs/BoxOrganization/PokemonFRLG_BoxSorter_Tests.h"
#include "PokemonFRLG_Navigation.h"
#include "Programs/RngManipulation/PokemonFRLG_GiftRngAttempts_Tests.h"
#include "Tests/TestUtils.h"

#include "PokemonFRLG_Tests.h"
Expand Down Expand Up @@ -264,7 +265,7 @@
}


void add_tests(UnitTestDatabase& database){

Check warning on line 268 in SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::add_tests( UnitTestDatabase & database): function_lines=28 (advisory above 25, limit 50)
add_tests_AdvanceWhiteDialogDetector(database);
add_tests_AdvanceBattleDialogDetector(database);
add_tests_BagDetector(database);
Expand All @@ -290,6 +291,7 @@
add_tests_ShinySymbolDetector(database);
add_tests_TrainerIdReader(database);
add_tests_WildEncounterReader(database);
add_tests_GiftRngAttempts(database);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,6 @@
)
{}

struct GiftRng_Descriptor::Stats : public StatsTracker{
Stats()
: resets(m_stats["Resets"])
, shinies(m_stats["Shinies"])
, nonshiny(m_stats["Non-Shiny Hits"])
, errors(m_stats["Errors"])
{
m_display_order.emplace_back("Resets");
m_display_order.emplace_back("Shinies");
m_display_order.emplace_back("Non-Shiny Hits", HIDDEN_IF_ZERO);
m_display_order.emplace_back("Errors", HIDDEN_IF_ZERO);
}
std::atomic<uint64_t>& resets;
std::atomic<uint64_t>& shinies;
std::atomic<uint64_t>& nonshiny;
std::atomic<uint64_t>& errors;
};
std::unique_ptr<StatsTracker> GiftRng_Descriptor::make_stats() const{
return std::unique_ptr<StatsTracker>(new Stats());
}
Expand Down Expand Up @@ -232,7 +215,7 @@
* Settings: Text Speed fast
*/

GiftRng_Descriptor::Stats& stats = env.current_stats<GiftRng_Descriptor::Stats>();
GiftRngStats& stats = env.current_stats<GiftRng_Descriptor::Stats>();

home_black_border_check(env.console, context);

Expand All @@ -245,242 +228,41 @@
if (!seed_match.found){
throw UserSetupError(env.console, "The target Seed was not found in the seed database for this game version, language, and sound setting.");
}
const std::vector<uint16_t> SEED_VALUES = seed_match.seed_values;
const int16_t SEED_POSITION = seed_match.seed_position;
const uint64_t SEED_DELAY = seed_match.seed_delay;
const SeedButton SEED_BUTTON = seed_match.seed_button;
const BlackoutButton EXTRA_BUTTON = seed_match.extra_button;
env.log("Seed delay: " + std::to_string(SEED_DELAY) + "ms, button mode: " + seed_match.button_mode
env.log("Seed delay: " + std::to_string(seed_match.seed_delay) + "ms, button mode: " + seed_match.button_mode

Check warning on line 231 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_GiftRng.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
+ ", column: " + seed_match.column_name);

BaseStats BASE_STATS;
int16_t GENDER_THRESHOLD = -1;
uint8_t LEVEL = 0;
switch (TARGET){
case PokemonFRLG_RngTarget::magikarp:
BASE_STATS = { 20, 10, 55, 15, 20, 80 };
GENDER_THRESHOLD = 126;
LEVEL = 5;
break;
case PokemonFRLG_RngTarget::hitmonchan:
BASE_STATS = { 50, 105, 79, 35, 110, 76 };
GENDER_THRESHOLD = -1;
LEVEL = 25;
break;
case PokemonFRLG_RngTarget::hitmonlee:
BASE_STATS = { 50, 120, 53, 35, 110, 87 };
GENDER_THRESHOLD = -1;
LEVEL = 25;
break;
case PokemonFRLG_RngTarget::eevee:
BASE_STATS = { 55, 55, 50, 45, 65, 55 };
GENDER_THRESHOLD = 30;
LEVEL = 25;
break;
case PokemonFRLG_RngTarget::lapras:
BASE_STATS = { 130, 85, 80, 85, 95, 60 };
GENDER_THRESHOLD = 126;
LEVEL = 25;
break;
case PokemonFRLG_RngTarget::omanyte:
BASE_STATS = { 35, 40, 100, 90, 55, 35 };
GENDER_THRESHOLD = 30;
LEVEL = 5;
break;
case PokemonFRLG_RngTarget::kabuto:
BASE_STATS = { 30, 80, 90, 55, 45, 55 };
GENDER_THRESHOLD = 30;
LEVEL = 5;
break;
case PokemonFRLG_RngTarget::aerodactyl:
BASE_STATS = { 80, 105, 65, 60, 75, 130 };
GENDER_THRESHOLD = 30;
LEVEL = 5;
break;
case PokemonFRLG_RngTarget::gamecornerabra:
BASE_STATS = { 25, 20, 15, 105, 55, 90 };
GENDER_THRESHOLD = 63;
break;
case PokemonFRLG_RngTarget::gamecornerclefairy:
BASE_STATS = { 70, 45, 48, 60, 65, 35 };
GENDER_THRESHOLD = 190;
break;
case PokemonFRLG_RngTarget::gamecornerdratinifr:
case PokemonFRLG_RngTarget::gamecornerdratinilg:
BASE_STATS = { 41, 64, 45, 50, 50, 50 };
GENDER_THRESHOLD = 126;
break;
case PokemonFRLG_RngTarget::gamecornerscyther:
BASE_STATS = { 70, 110, 80, 55, 80, 105 };
GENDER_THRESHOLD = 126;
LEVEL = 25;
break;
case PokemonFRLG_RngTarget::gamecornerpinsir:
BASE_STATS = { 65, 125, 100, 55, 70, 85 };
GENDER_THRESHOLD = 126;
LEVEL = 18;
break;
case PokemonFRLG_RngTarget::gamecornerporygon:
BASE_STATS = { 65, 60, 70, 85, 75, 40 };
GENDER_THRESHOLD = -1;
break;
case PokemonFRLG_RngTarget::togepi:
case PokemonFRLG_RngTarget::togepifast:
BASE_STATS = { 35, 20, 65, 40, 65, 20 };
GENDER_THRESHOLD = 30;
LEVEL = 5;
break;
default:
break;
}

static const int64_t FIXED_SEED_OFFSET = -845; // milliseconds, approximate

static const uint64_t CONTINUE_SCREEN_FRAMES = 200;

const uint64_t INITIAL_ADVANCES_RADIUS = USE_TEACHY_TV ? 4096 : 1024;

const uint8_t MAX_HISTORY_LENGTH = USE_TEACHY_TV ? 2 : 10;

static const std::set<std::string> SPECIES_LIST = {
"magikarp", "hitmonchan", "hitmonlee", "eevee", "lapras",
"omanyte", "kabuto", "aerodactyl",
"abra", "clefairy", "dratini", "scyther", "pinsir", "porygon",
"togepi"
};

env.log("RNG Target: " + std::to_string(TARGET.current_value()));
env.log("Target Seed: " + to_hex_string(TARGET_SEED));
env.log("Target Advances: " + std::to_string(ADVANCES));

AdvRngSearcher searcher(TARGET_SEED, ADVANCES, AdvRngMethod::Method1);
AdvPokemonResult target_result = searcher.generate_pokemon();
RNG_TARGET.set_target(target_result, GENDER_THRESHOLD);
log_target_pokemon(env.console, target_result);

RngCalibrations calibrations = {
RNG_CALIBRATION.seed_calibration / FRLG_FRAME_DURATION,
RNG_CALIBRATION.csf_calibration,
RNG_CALIBRATION.advances_calibration
};
log_calibrations(env.console, calibrations, true);

Milliseconds launch_delay = INITIAL_LAUNCH_DELAY;

RngUncertainHistory uncertain_history;
RngCalibrationHistory calibration_history;

uint16_t failed_searches = 0;

while (true){
if (calibration_history.results.size() > 0){
env.log("Checking for nonshiny target hit...");
if (have_hit_target(TARGET_SEED, ADVANCES, calibration_history.results.back())){
env.log("Target Hit!");
stats.nonshiny++;
break;
}
env.log("Missed target.");
}

if (failed_searches >= 5){
env.log("Failed to find any matches 5 times in a row");
OperationFailedExceptionWithScreenshot::fire(
ErrorReportMode::NO_ERROR_REPORT,
"Failed to find any matches 5 times in a row. Check your seed and advances settings.",
env.console
);
break;
}

if (stats.resets > MAX_RESETS){
env.log("Max resets reached.");
break;
}

send_program_status_notification(
env, NOTIFICATION_STATUS_UPDATE,
"Calibrating."
);
env.update_stats();


uint64_t advances_radius = get_advances_radius(env.console, calibration_history, INITIAL_ADVANCES_RADIUS);

if (calibration_history.results.size() > 0){
calibrations = get_calibrations(env.console, calibration_history, SEED_VALUES, SEED_POSITION, ADVANCES);
}

// if previous resets had uncertain advances, slightly modify the seed delay to try to hit a different target
apply_seed_bump(calibrations, uncertain_history);

uint64_t ingame_advances = ADVANCES - CONTINUE_SCREEN_FRAMES;

RngTimings timings = prepare_timings(
env.console, TARGET,
SEED_DELAY, CONTINUE_SCREEN_FRAMES, ingame_advances,
USE_TEACHY_TV, calibrations,
FIXED_SEED_OFFSET, LANGUAGE
);

env.log("Resetting Game...");
reset_and_perform_blind_sequence(
env.console, context, LANGUAGE, TARGET,
SEED_BUTTON, EXTRA_BUTTON, timings,
launch_delay, false, PROFILE
);
stats.resets++;

RNG_FILTERS.reset();
RNG_CALIBRATION.set_calibrations(calibrations);
RNG_CALIBRATION.reset_hits();

bool shiny_found = check_for_shiny(env.console, context, TARGET);

if (shiny_found){
env.log("Shiny found!");
stats.shinies++;
RNG_CALIBRATION.hits.set("Shiny!");
send_program_notification(
env,
NOTIFICATION_SHINY,
COLOR_YELLOW,
"Shiny found!",
{}, "",
env.console.video().snapshot(),
true
);
if (TAKE_VIDEO){
pbf_press_button(context, BUTTON_CAPTURE, 2000ms, 0ms);
}
break;
}

AdvObservedPokemon pokemon = read_summary(env.console, context, LANGUAGE, SPECIES_LIST);
if (LEVEL > 0){
pokemon.level[0] = LEVEL;
}
AdvRngFilters filters = observation_to_filters(pokemon, BASE_STATS);
RNG_FILTERS.set(filters);

std::vector<AdvRngState> search_hits = refine_calibration_with_rare_candy(
env, context, LANGUAGE, pokemon, filters, BASE_STATS,
uncertain_history, calibration_history, calibrations,
MAX_HISTORY_LENGTH, MAX_RARE_CANDIES, AdvRngMethod::Method1, false,
stats.errors, NOTIFICATION_ERROR_RECOVERABLE,
[&](AdvRngFilters& f){
return get_search_results(env.console, searcher, f, SEED_VALUES, ADVANCES, advances_radius, GENDER_THRESHOLD);
},
[&](const std::vector<AdvRngState>& h){ RNG_CALIBRATION.set_hits(h); },
[&](const AdvRngFilters& f){ RNG_FILTERS.set(f); },
[&](const std::vector<AdvRngState>& h){
return all_equal(h) || all_indistinguishable(h, searcher, GENDER_THRESHOLD);
}
);

env.log("RNG search finished.");
update_failed_searches(failed_searches, search_hits);

GiftRngAttempts attempts(
GiftRngTarget{
.target = TARGET,
.seed = TARGET_SEED,
.advances = ADVANCES,
.seed_match = seed_match,
},
GiftRngSettings{
.language = LANGUAGE,
.profile = PROFILE,
.use_teachy_tv = USE_TEACHY_TV,
.take_video = TAKE_VIDEO,
.identify_shiny = false,
.max_resets = [this]{ return uint64_t(MAX_RESETS); },
.max_rare_candies = [this]{ return uint64_t(MAX_RARE_CANDIES); },
.on_reset = nullptr,
},
GiftRngDisplays{.target = RNG_TARGET, .filters = RNG_FILTERS, .calibration = RNG_CALIBRATION},
GiftRngNotifications{
.shiny = NOTIFICATION_SHINY,
.status_update = NOTIFICATION_STATUS_UPDATE,
.error_recoverable = NOTIFICATION_ERROR_RECOVERABLE,
},
stats
);
GiftRngResult result = attempts.run(env, context);
if (result.outcome == GiftRngOutcome::no_matches){
OperationFailedExceptionWithScreenshot::fire(
ErrorReportMode::NO_ERROR_REPORT,
"Failed to find any matches 5 times in a row. Check your seed and advances settings.",
env.console
);
}

if (GO_HOME_WHEN_DONE){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h"
#include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h"
#include "PokemonFRLG_BlindNavigation.h"
#include "PokemonFRLG_GiftRngAttempts.h"
#include "PokemonFRLG_RngDisplays.h"
#include "PokemonFRLG_SeedsDatabase.h"

Expand All @@ -25,7 +26,7 @@ namespace PokemonFRLG{
class GiftRng_Descriptor : public SingleSwitchProgramDescriptor{
public:
GiftRng_Descriptor();
struct Stats;
using Stats = GiftRngStats;
virtual std::unique_ptr<StatsTracker> make_stats() const override;
};

Expand Down
Loading
Loading