From 9803ace69504e2363f46393f53bf2e02edd916fa Mon Sep 17 00:00:00 2001 From: dayfine Date: Mon, 2 Nov 2026 15:00:16 -0800 Subject: [PATCH] FRLG: search static/gift/starter RNG targets in the program Mirrors the Ten Lines Searcher + Initial Seed tabs for FRLG on Switch: lists the Method 1 results that pass nature/IV/gender/ability/shiny filters, each with the initial seed (from the RngSeeds tables, as the RNG programs pick it) that reaches it in the fewest advances. Shiny is checked against a set of weighted SID candidates, and targets can be ranked by expected wait per shiny. Unit tests reproduce 50 Ten Lines results for LeafGreen (English). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../PokemonFRLG/PokemonFRLG_Tests.cpp | 2 + .../PokemonFRLG_RngTargetSearch.cpp | 417 ++++++++++++++++ .../PokemonFRLG_RngTargetSearch.h | 150 ++++++ .../PokemonFRLG_RngTargetSearch_Tests.cpp | 446 ++++++++++++++++++ .../PokemonFRLG_RngTargetSearch_Tests.h | 23 + .../PokemonFRLG_SeedsDatabase.h | 13 +- SerialPrograms/cmake/SourceFiles.cmake | 4 + 7 files changed, 1051 insertions(+), 4 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch_Tests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch_Tests.h diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp index d734e275f7..43487142ae 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp @@ -28,6 +28,7 @@ #include "Inference/PokemonFRLG_TrainerIdReader.h" #include "Inference/PokemonFRLG_WildEncounterReader.h" #include "PokemonFRLG_Navigation.h" +#include "Programs/RngManipulation/PokemonFRLG_RngTargetSearch_Tests.h" #include "Tests/TestUtils.h" #include "PokemonFRLG_Tests.h" @@ -288,6 +289,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_BattleMoveSelectionArrow(database); add_tests_BagDetector(database); add_tests_ExitWildBattle(database); + add_tests_RngTargetSearch(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch.cpp new file mode 100644 index 0000000000..699e4c051e --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch.cpp @@ -0,0 +1,417 @@ +/* RNG Target Search + * + * From: https://github.com/PokemonAutomation/ + * + * Ten Lines finds targets in two steps, and this does the same in one: + * 1. Searcher (src/wasm/src/searcher.cpp, search_seeds_static()): PokeFinder's + * StaticSearcher3 lists every RNG state whose Method 1 Pokemon passes the filters. + * 2. Initial Seed (src/wasm/src/initial_seed.cpp, frlg_seeds()): for a target state, + * the initial seeds from the seed table, ordered by the advances from the seed to + * the target. That is the LCRNG distance, since the game starts the RNG at the seed. + * + * The code here is written from that description, not copied from either project. + * + */ + +#include +#include +#include "PokemonFRLG_RngTargetSearch.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +using namespace Pokemon; + + +namespace{ + +const uint32_t LCRNG_MULT = 0x41c64e6d; +const uint32_t LCRNG_ADD = 0x6073; +const uint32_t LCRNG_MULT_INVERSE = 0xeeb9eb65; +const uint32_t LCRNG_ADD_INVERSE = 0x0a3561a1; + +// The GBA frame rate, as Ten Lines uses it (SYSTEM_TIMING_DATA in src/tenLines/index.ts). +const double GBA_FRAMES_PER_SECOND = 16777216.0 / 280896.0; + +} + + +uint32_t lcrng_next(uint32_t state){ + return state * LCRNG_MULT + LCRNG_ADD; +} +uint32_t lcrng_previous(uint32_t state){ + return state * LCRNG_MULT_INVERSE + LCRNG_ADD_INVERSE; +} + +// Bit by bit: once the low k bits of "from" and "to" agree, only a jump of 2^k steps +// keeps them agreeing, and it flips bit k. So bit k of the distance is whether bit k +// still differs. +uint32_t lcrng_distance(uint32_t from, uint32_t to){ + uint32_t mult = LCRNG_MULT; + uint32_t add = LCRNG_ADD; + uint32_t distance = 0; + for (uint32_t bit = 1; from != to; bit <<= 1){ + if ((from ^ to) & bit){ + from = from * mult + add; + distance |= bit; + } + add *= mult + 1; + mult *= mult; + } + return distance; +} + + +AdvPokemonResult method1_pokemon(uint32_t state){ + uint32_t pid_low = lcrng_next(state); + uint32_t pid_high = lcrng_next(pid_low); + uint32_t iv_first = lcrng_next(pid_high); + uint32_t iv_second = lcrng_next(iv_first); + + uint32_t pid = (pid_high & 0xffff0000) | (pid_low >> 16); + iv_first >>= 16; + iv_second >>= 16; + + AdvIVs ivs; + ivs.hp = iv_first & 0x1f; + ivs.attack = (iv_first >> 5) & 0x1f; + ivs.defense = (iv_first >> 10) & 0x1f; + ivs.speed = iv_second & 0x1f; + ivs.spatk = (iv_second >> 5) & 0x1f; + ivs.spdef = (iv_second >> 10) & 0x1f; + + return {pid, uint8_t(pid & 0xff), AdvNature(pid % 25), AdvAbility(pid & 1), ivs}; +} + +AdvShinyType shiny_type(uint32_t pid, uint16_t tid, uint16_t sid){ + uint16_t value = uint16_t(pid >> 16) ^ uint16_t(pid) ^ tid ^ sid; + if (value == 0){ + return AdvShinyType::Square; + } + return value < 8 ? AdvShinyType::Star : AdvShinyType::Normal; +} + +HiddenPower hidden_power(const AdvIVs& ivs){ + static const char* TYPES[] = { + "Fighting", "Flying", "Poison", "Ground", "Rock", "Bug", "Ghost", "Steel", + "Fire", "Water", "Grass", "Electric", "Psychic", "Ice", "Dragon", "Dark", + }; + // Bit 0 of each IV picks the type, bit 1 the power, in the order HP, Atk, Def, Spe, + // SpA, SpD. + const uint8_t order[] = {ivs.hp, ivs.attack, ivs.defense, ivs.speed, ivs.spatk, ivs.spdef}; + int type_bits = 0; + int power_bits = 0; + for (int c = 0; c < 6; c++){ + type_bits |= (order[c] & 1) << c; + power_bits |= ((order[c] >> 1) & 1) << c; + } + return {TYPES[type_bits * 15 / 63], uint8_t(power_bits * 40 / 63 + 30)}; +} + + +std::vector initial_seed_entries( + const SeedsDatabase& database, + SoundSetting sound, + const std::string& button_mode, + bool allow_blackout +){ + // Like SeedsDatabase::find_seed(), the first entry found wins a tie. + std::map earliest; + auto add = [&](const InitialSeedEntry& entry){ + auto iter = earliest.find(entry.seed); + if (iter == earliest.end() || entry.seed_time_ms < iter->second.seed_time_ms){ + earliest[entry.seed] = entry; + } + }; + + const std::vector& delays = database.delays(); + for (const SeedsDatabase::SeedColumn& column : database.columns()){ + if (column.sound != sound || column.button_mode != button_mode){ + continue; + } + for (size_t c = 0; c < column.seeds.size() && c < delays.size(); c++){ + uint16_t seed = column.seeds[c]; + add({seed, delays[c], column.seed_button, BlackoutButton::None}); + if (allow_blackout){ + add({uint16_t(seed - BLACKOUT_SEED_OFFSET), delays[c], column.seed_button, BlackoutButton::L}); + } + } + } + + std::vector entries; + entries.reserve(earliest.size()); + for (const auto& item : earliest){ + entries.emplace_back(item.second); + } + return entries; +} + + +namespace{ + +// Every 16-bit x times the LCRNG multiplier, sorted. A state with known top 16 bits +// "high" is (high << 16) + x, and its next state is lcrng_next(high << 16) + x * mult, +// so the states whose next state is in a range are a range of this table. +class LowBitsTable{ +public: + LowBitsTable(){ + m_products.reserve(0x10000); + for (uint32_t x = 0; x < 0x10000; x++){ + m_products.emplace_back(x * LCRNG_MULT, uint16_t(x)); + } + std::sort(m_products.begin(), m_products.end()); + } + + // Calls "found" with each state whose top 16 bits are "high" and whose next state + // is in [low, low + width). + template + void find_states(uint16_t high, uint32_t low, uint32_t width, Callback&& found) const{ + uint32_t top = uint32_t(high) << 16; + uint32_t start = low - lcrng_next(top); + uint64_t end = uint64_t(start) + width; + const uint64_t WRAP = uint64_t(1) << 32; + visit(top, start, std::min(end, WRAP), found); + if (end > WRAP){ + visit(top, 0, end - WRAP, found); + } + } + +private: + template + void visit(uint32_t top, uint64_t start, uint64_t end, Callback& found) const{ + auto iter = std::lower_bound( + m_products.begin(), m_products.end(), start, + [](const std::pair& item, uint64_t value){ return item.first < value; } + ); + for (; iter != m_products.end() && iter->first < end; ++iter){ + found(top | iter->second); + } + } + + std::vector> m_products; +}; + + +// Keeps the states whose Pokemon pass the filters, each with its nearest initial seed. +class TargetCollector{ +public: + TargetCollector( + const std::vector& seeds, + uint16_t tid, + const std::vector& candidates, + const RngTargetFilters& filters + ) + : m_tid(tid) + , m_candidates(candidates) + , m_filters(filters) + { + for (const InitialSeedEntry& entry : seeds){ + if (seed_time_allowed(entry.seed_time_ms)){ + m_seeds.push_back({lcrng_distance(0, entry.seed), entry}); + } + } + std::sort(m_seeds.begin(), m_seeds.end(), [](const SeedPosition& x, const SeedPosition& y){ + return x.distance < y.distance; + }); + for (const SidCandidate& candidate : candidates){ + m_total_weight += candidate.weight; + } + } + + void consider(uint32_t state){ + AdvPokemonResult pokemon = method1_pokemon(state); + if (!passes(pokemon)){ + return; + } + RngTarget target{state, {}, 0, pokemon, {}, 0}; + double weight = 0; + for (size_t c = 0; c < m_candidates.size(); c++){ + AdvShinyType type = shiny_type(pokemon.pid, m_tid, m_candidates[c].sid); + if (type != AdvShinyType::Normal){ + target.shiny_for.push_back({c, type}); + weight += m_candidates[c].weight; + } + } + if (m_filters.shiny_required && target.shiny_for.empty()){ + return; + } + if (!nearest_seed(state, target)){ + return; + } + target.shiny_probability = m_total_weight > 0 ? weight / m_total_weight : 0; + m_targets.emplace_back(std::move(target)); + } + + std::vector results(){ + std::sort(m_targets.begin(), m_targets.end(), [](const RngTarget& x, const RngTarget& y){ + return x.advances != y.advances ? x.advances < y.advances : x.state < y.state; + }); + return std::move(m_targets); + } + +private: + struct SeedPosition{ + uint32_t distance; + InitialSeedEntry entry; + }; + + bool seed_time_allowed(uint64_t time) const{ + if (time < m_filters.min_seed_time_ms){ + return false; + } + uint64_t gap = time > m_filters.avoided_seed_time_ms + ? time - m_filters.avoided_seed_time_ms + : m_filters.avoided_seed_time_ms - time; + return m_filters.avoided_seed_time_radius_ms == 0 || gap > m_filters.avoided_seed_time_radius_ms; + } + + bool passes(const AdvPokemonResult& pokemon) const{ + const std::vector& natures = m_filters.natures; + if (!natures.empty() && std::find(natures.begin(), natures.end(), pokemon.nature) == natures.end()){ + return false; + } + if (m_filters.ability != AdvAbility::Any && pokemon.ability != m_filters.ability){ + return false; + } + if (m_filters.gender != AdvGender::Any + && gender_from_gender_value(pokemon.gender, m_filters.gender_threshold) != m_filters.gender + ){ + return false; + } + const AdvIVs& ivs = pokemon.ivs; + const AdvIVs& min = m_filters.min_ivs; + return ivs.hp >= min.hp && ivs.attack >= min.attack && ivs.defense >= min.defense + && ivs.spatk >= min.spatk && ivs.spdef >= min.spdef && ivs.speed >= min.speed; + } + + // The seed with the fewest advances to "state" that are still at least the minimum: + // the one with the largest distance from 0 that is at most "state"'s, less the + // minimum, wrapping around the cycle. + bool nearest_seed(uint32_t state, RngTarget& target) const{ + if (m_seeds.empty()){ + return false; + } + uint32_t distance = lcrng_distance(0, state); + uint32_t latest = distance - uint32_t(m_filters.min_advances); + auto iter = std::upper_bound( + m_seeds.begin(), m_seeds.end(), latest, + [](uint32_t value, const SeedPosition& seed){ return value < seed.distance; } + ); + const SeedPosition& seed = iter == m_seeds.begin() ? m_seeds.back() : *(iter - 1); + uint64_t advances = uint32_t(distance - seed.distance); + if (advances < m_filters.min_advances || advances > m_filters.max_advances){ + return false; + } + target.seed = seed.entry; + target.advances = advances; + return true; + } + + const uint16_t m_tid; + const std::vector& m_candidates; + const RngTargetFilters& m_filters; + double m_total_weight = 0; + std::vector m_seeds; + std::vector m_targets; +}; + + +// Shiny Pokemon only: for each low PID half, the high halves that are shiny for a +// candidate are 8 aligned values, so each takes one lookup. +void search_shiny_states( + const LowBitsTable& table, + uint16_t tid, + const std::vector& candidates, + TargetCollector& collector +){ + std::vector shiny_values; + for (const SidCandidate& candidate : candidates){ + shiny_values.push_back((tid ^ candidate.sid) & 0xfff8); + } + std::sort(shiny_values.begin(), shiny_values.end()); + shiny_values.erase(std::unique(shiny_values.begin(), shiny_values.end()), shiny_values.end()); + + auto found = [&](uint32_t pid_low_state){ + collector.consider(lcrng_previous(pid_low_state)); + }; + for (uint32_t pid_low = 0; pid_low < 0x10000; pid_low++){ + for (uint16_t value : shiny_values){ + uint32_t pid_high = (pid_low & 0xfff8) ^ value; + table.find_states(uint16_t(pid_low), pid_high << 16, 8 << 16, found); + } + } +} + +// Any Pokemon, like PokeFinder's StaticSearcher3: every IV combination at or above the +// minimums gives the top 15 bits of the two IV calls. +void search_iv_states(const LowBitsTable& table, const AdvIVs& min_ivs, TargetCollector& collector){ + auto found = [&](uint32_t iv_first_state){ + collector.consider(lcrng_previous(lcrng_previous(lcrng_previous(iv_first_state)))); + }; + // HP, Atk, Def, Spe, SpA, SpD: the order the IVs sit in the two calls. + const uint8_t mins[6] = {min_ivs.hp, min_ivs.attack, min_ivs.defense, min_ivs.speed, min_ivs.spatk, min_ivs.spdef}; + uint8_t ivs[6]; + std::copy(mins, mins + 6, ivs); + while (true){ + uint16_t first = uint16_t(ivs[0] | (ivs[1] << 5) | (ivs[2] << 10)); + uint16_t second = uint16_t(ivs[3] | (ivs[4] << 5) | (ivs[5] << 10)); + for (uint16_t first_top : {0x0000, 0x8000}){ + for (uint32_t second_top : {0x0000, 0x8000}){ + table.find_states(first | first_top, (second | second_top) << 16, 0x10000, found); + } + } + int c = 0; + while (c < 6 && ivs[c] == 31){ + ivs[c] = mins[c]; + c++; + } + if (c == 6){ + return; + } + ivs[c]++; + } +} + +} + + +std::vector search_rng_targets( + const std::vector& seeds, + uint16_t tid, + const std::vector& candidates, + const RngTargetFilters& filters +){ + TargetCollector collector(seeds, tid, candidates, filters); + LowBitsTable table; + if (filters.shiny_required){ + search_shiny_states(table, tid, candidates, collector); + }else{ + search_iv_states(table, filters.min_ivs, collector); + } + return collector.results(); +} + + +void rank_rng_targets(std::vector& targets){ + auto time_ms = [](const RngTarget& target){ + return double(target.seed.seed_time_ms) + double(target.advances) * 1000.0 / GBA_FRAMES_PER_SECOND; + }; + std::stable_sort(targets.begin(), targets.end(), [&](const RngTarget& x, const RngTarget& y){ + bool x_shiny = x.shiny_probability > 0; + bool y_shiny = y.shiny_probability > 0; + if (x_shiny != y_shiny){ + return x_shiny; + } + if (!x_shiny){ + return time_ms(x) < time_ms(y); + } + return time_ms(x) / x.shiny_probability < time_ms(y) / y.shiny_probability; + }); +} + + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch.h new file mode 100644 index 0000000000..bfe0c90a68 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch.h @@ -0,0 +1,150 @@ +/* RNG Target Search + * + * From: https://github.com/PokemonAutomation/ + * + * Finds static, gift and starter RNG targets (a Target Seed plus Advances, as + * Static/Gift/Starter RNG take them) for a trainer, the way the "Searcher" and + * "Initial Seed" tabs of Ten Lines do for FRLG on Switch: + * https://lincoln-lm.github.io/ten-lines + * https://github.com/Lincoln-LM/ten-lines + * + * How a program can use it to pick its own target when the SID is not known: + * 1. SID Helper reads the TID and narrows the SID down to a few weighted candidates. + * 2. search_rng_targets() with those candidates, then rank_rng_targets(). + * 3. Run Static/Gift/Starter RNG with the top target's seed and advances. + * 4. Read the result: + * - Shiny: the SID is one of the candidates the target is shiny for. If that is + * one candidate, the SID is confirmed. + * - Nature and IVs match, but not shiny: the target was hit, so every candidate it + * is shiny for is wrong. Drop them and search again. + * - Nature or IVs differ: the target was missed. Retry it. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_RngTargetSearch_H +#define PokemonAutomation_PokemonFRLG_RngTargetSearch_H + +#include +#include +#include +#include "Pokemon/Pokemon_AdvRng.h" +#include "PokemonFRLG_BlindNavigation.h" +#include "PokemonFRLG_SeedsDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The Gen 3 LCRNG: state * 0x41C64E6D + 0x6073. +uint32_t lcrng_next(uint32_t state); +uint32_t lcrng_previous(uint32_t state); +// How many lcrng_next() calls take "from" to "to". The LCRNG has a single cycle of +// 2^32 states, so every pair has exactly one distance. +uint32_t lcrng_distance(uint32_t from, uint32_t to); + +// The static, gift or starter Pokemon (Method 1) generated from "state", the RNG state +// just before the PID is rolled: the next two calls give the low and high PID halves, +// the two after that the IVs (HP/Atk/Def, then Spe/SpA/SpD). +Pokemon::AdvPokemonResult method1_pokemon(uint32_t state); + +Pokemon::AdvShinyType shiny_type(uint32_t pid, uint16_t tid, uint16_t sid); + +struct HiddenPower{ + std::string type; + uint8_t power; +}; +HiddenPower hidden_power(const Pokemon::AdvIVs& ivs); + + +// One way to reach an initial seed from the title screen. +struct InitialSeedEntry{ + // The seed reached, after the blackout offset. + uint16_t seed; + uint64_t seed_time_ms; + SeedButton seed_button; + BlackoutButton blackout_button; +}; + +// The initial seeds reachable with one sound setting and button mode, each by its +// earliest entry in the seed table. That is the entry Static/Gift/Starter RNG use for +// a Target Seed (SeedsDatabase::find_seed()). +// +// Holding a blackout button lowers the seed by BLACKOUT_SEED_OFFSET. Ten Lines lists +// that only for the English Switch tables in Help mode (HELD_BUTTON_OFFSETS["lg_nx"] +// and ["fr_nx"] in src/wasm/src/initial_seed.hpp), so pass "allow_blackout" only then. +std::vector initial_seed_entries( + const SeedsDatabase& database, + SoundSetting sound, + const std::string& button_mode, + bool allow_blackout +); + + +struct SidCandidate{ + uint16_t sid; + // Relative likelihood. The weights need not add up to 1. + double weight; +}; + +struct RngTargetFilters{ + // Empty: any nature. + std::vector natures; + Pokemon::AdvIVs min_ivs; + Pokemon::AdvGender gender = Pokemon::AdvGender::Any; + int16_t gender_threshold = 126; + Pokemon::AdvAbility ability = Pokemon::AdvAbility::Any; + // Shiny for at least one SID candidate. + bool shiny_required = true; + + // Total advances from the initial seed: the "Advances" of Ten Lines and of the RNG + // programs. They include the title and continue screens, so fewer of them are + // spent in game. + uint64_t min_advances = 0; + uint64_t max_advances = 1000000; + + uint64_t min_seed_time_ms = 0; + // Skip seed times within "avoided_seed_time_radius_ms" of this. + uint64_t avoided_seed_time_ms = 0; + uint64_t avoided_seed_time_radius_ms = 0; +}; + +struct ShinyCandidate{ + // Index into the SID candidates. + size_t candidate; + Pokemon::AdvShinyType type; +}; + +struct RngTarget{ + // The RNG state before the PID is rolled: the "Target" column of Ten Lines. + uint32_t state; + InitialSeedEntry seed; + uint64_t advances; + Pokemon::AdvPokemonResult pokemon; + // The candidates this target is shiny for. + std::vector shiny_for; + // Their share of the total candidate weight. + double shiny_probability; +}; + +// Every Method 1 result that passes "filters", each with the initial seed that reaches +// it in the fewest advances, sorted by advances. +std::vector search_rng_targets( + const std::vector& seeds, + uint16_t tid, + const std::vector& candidates, + const RngTargetFilters& filters +); + +// Sorts targets by expected wait per shiny: the time to the target (seed time plus +// advances at the GBA frame rate) divided by shiny_probability. A target shiny for +// every candidate beats one shiny for half of them unless it takes twice as long. +// Targets with no shiny candidate go last, by time. +void rank_rng_targets(std::vector& targets); + + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch_Tests.cpp new file mode 100644 index 0000000000..71dec39025 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch_Tests.cpp @@ -0,0 +1,446 @@ +/* RNG Target Search Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The expected targets were found with Ten Lines (https://lincoln-lm.github.io/ten-lines) + * for LeafGreen (English) on Switch, TID 43771, SID 64694, Mono sound, Help button mode, + * Static 1: the lowest advances per target with total advances of at least 3,000 and + * seed times more than 3 seconds from 30,500 ms. + * + */ + +#include +#include +#include "CommonFramework/Language.h" +#include "PokemonFRLG_RngTargetSearch.h" +#include "PokemonFRLG_RngTargetSearch_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +using namespace Pokemon; + + +namespace{ + +const uint16_t TID = 43771; +const uint16_t SID = 64694; + +// One Ten Lines result: Target, PID, Shiny, Ability, IVs (HP/Atk/Def/SpA/SpD/Spe), +// Hidden Power, Initial Seed, Advances, Seed Time (ms), Buttons. +struct TenLinesRow{ + uint32_t state; + uint32_t pid; + std::string shiny; + int ability; + std::string ivs; + std::string hidden_power; + uint16_t seed; + uint64_t advances; + uint64_t seed_time_ms; + std::string buttons; +}; + +struct TenLinesGroup{ + std::string name; + std::vector natures; + AdvIVs min_ivs; + std::string rows; +}; + +// Ten Lines results, one per line, sorted by advances. +const char MODEST_ROWS[] = + "9435B76A,68F03EBD,Square,1,31/14/7/29/1/27,Dragon 39,2EF6,29364,52641,A/None\n" + "2E37261A,DB7C8D33,Star,1,28/16/6/30/11/29,Water 63,6E9A,50048,54054,A/Blackout R\n" + "C408B1DF,192D4F60,Square,0,25/8/10/29/0/28,Rock 32,02A8,53397,55259,Start/Blackout R\n" + "814DD9D3,84FBD2B6,Square,0,26/19/17/30/10/30,Flying 67,8EB6,58835,48990,Start/Blackout R\n" + "0D815228,8C45DA08,Square,0,29/5/29/31/22/25,Steel 60,A356,59258,64959,Start/Blackout R\n" + "016CB6DF,9C35CA7C,Star,0,29/11/29/30/20/27,Ground 46,A626,69439,48471,A/None\n" + "5DFAB16B,9FC1C98E,Star,0,26/1/0/30/27/28,Fire 61,2B48,77713,41805,A/None\n" + "0E710A6F,8E60D829,Star,1,26/1/9/31/10/25,Steel 61,47CD,89010,50903,A/None\n" + "74224D48,A59DF3D7,Star,1,26/28/22/31/10/30,Ground 68,2CE5,95029,51237,Start/Blackout R\n" + "18699F2D,4B0B1D46,Square,0,28/23/23/29/17/25,Dragon 33,A4C2,101793,47657,Start/Blackout R\n" + "19D01211,FB76AD3F,Star,1,31/16/12/30/22/30,Fighting 66,7DC6,103697,68622,A/Blackout R\n" + "BCDA4E7A,CF729938,Star,0,30/31/30/31/18/26,Rock 70,99D2,107880,36842,Start/None\n" + "1A5A9954,E80BBE43,Star,1,29/24/22/29/4/29,Bug 32,C1C1,107989,35490,A/None\n" + "5A6B83CC,ABF8FDB7,Star,1,27/30/3/31/17/30,Psychic 49,811D,119809,35822,A/Blackout R\n" + "50F06E45,D01B8656,Square,0,26/8/29/31/4/26,Rock 45,4C09,125356,67439,A/None\n" + "9F536DFD,C6FA90B7,Square,1,28/13/28/29/28/25,Ghost 30,1548,129923,66441,Start/None\n" + "2F82C0CA,21ED77A6,Star,0,25/16/4/29/1/31,Ice 35,4FDC,150758,39071,Start/None\n" + "4C7401EB,5AF00CBC,Star,0,30/12/0/30/21/26,Steel 45,45DC,155101,55440,Start/None\n" + "BE31CFCC,FA05AC4E,Star,0,30/22/21/30/3/29,Grass 62,D17C,158288,38405,Start/Blackout R\n" + "3F487560,7B922DDD,Star,1,30/21/5/31/21/27,Dragon 45,FF1C,171172,58240,A/None\n"; + +const char ADAMANT_ROWS[] = + "537A2752,A823FE6A,Star,0,25/29/15/17/31/27,Dark 57,B897,4437,35972,A/Blackout R\n" + "D9FDB041,B9B4EFFE,Star,0,27/31/19/11/5/31,Dark 49,D356,22897,60771,A/None\n" + "E9F88FDD,EAD6BC98,Star,0,30/29/1/29/23/31,Dragon 56,CE81,26060,33587,Start/None\n" + "C3B70E12,D96E8F26,Star,0,29/31/31/9/4/31,Steel 38,8AA1,30571,57386,Start/None\n" + "37BB3B92,BD83EBCB,Star,1,27/30/19/8/26/25,Ground 54,F054,33142,46390,Start/None\n" + "E8D67E1F,BEF4E8BB,Star,1,26/30/2/25/21/26,Electric 39,A9D1,39694,61024,A/Blackout R\n" + "DE925A82,083B5E70,Star,0,28/30/22/26/3/27,Water 69,4665,42695,49155,Start/Blackout R\n" + "D8713EE8,6E9838D2,Star,0,29/31/14/6/26/27,Poison 69,1233,48679,63991,Start/None\n" + "B50AB28B,EC3FBA76,Star,0,30/31/22/25/27/27,Ice 59,704A,61591,65705,A/None\n" + "0A550D9C,25487304,Star,0,30/29/14/5/27/31,Ice 58,B845,68937,53954,A/None\n" + "119C3777,633C3574,Star,0,26/29/4/26/18/30,Fighting 66,A39D,85194,58722,Start/Blackout R\n" + "315D5C57,AF63F92D,Star,1,30/29/23/13/21/29,Dragon 33,DE4C,95017,36725,A/None\n" + "66A23785,8397D5DA,Square,0,25/30/7/24/27/31,Grass 59,9DAC,95463,59324,A/None\n" + "A31E8DB4,462C1067,Star,1,31/31/17/9/22/28,Bug 52,0D4C,99560,36085,A/None\n" + "6109BF46,A9DAFF92,Star,0,26/31/30/6/2/30,Fighting 70,3136,103312,63359,A/None\n" + "3F5603C4,8C11DA5D,Star,1,25/29/6/6/24/29,Poison 42,D22E,111518,54657,Start/None\n"; + +const char NEUTRAL_ROWS[] = + "130195A8,AD7BFB36,Square,0,31/27/31/31/27/26,Ice 70,4381,17225,61908,Start/None\n" + "BE0D7E8F,6FA739E8,Star,0,31/21/25/29/26/25,Steel 50,C845,21690,41371,Start/None\n" + "1798BCE3,066A5027,Square,1,28/23/25/30/31/28,Water 61,3DDC,26549,44721,A/None\n" + "0602C959,EA14BC5F,Star,1,27/21/27/30/27/25,Electric 63,DA81,31480,46973,Start/None\n" + "5B31FD89,A635F07F,Star,1,31/22/19/31/22/31,Ghost 70,B8BB,48990,35972,A/None\n" + "0BA51887,D464822E,Star,0,25/16/17/31/28/28,Bug 40,3C95,59906,38258,A/Blackout R\n" + "61407261,751D2350,Square,0,30/18/16/31/30/28,Ground 62,B83C,61923,51625,Start/Blackout R\n" + "BBB6D222,9F67C92A,Square,0,30/28/25/30/24/31,Poison 45,1439,76611,62542,A/Blackout R\n" + "2C224D48,1D9D4BD7,Star,1,26/28/28/31/24/30,Ground 45,0210,83512,56705,Start/None\n" + "AC47BD6D,33D86591,Star,1,31/19/19/31/31/27,Dark 70,F483,101418,57476,A/Blackout R\n" + "0937DE0D,AB36FD7F,Star,1,27/18/18/30/19/29,Water 64,E588,103955,53106,A/None\n" + "30339DE2,E8A8BEE1,Star,1,27/17/25/29/19/29,Dark 50,47A0,126746,69310,A/Blackout R\n" + "E78F978E,C86B9E25,Star,1,25/20/31/29/23/29,Dragon 52,5A24,136290,33839,A/None\n" + "4A07D3EB,2CE57AA9,Star,1,25/26/24/30/27/25,Water 61,9754,145893,38338,Start/None\n"; + +std::vector groups(){ + AdvIVs modest; + modest.hp = 25; + modest.spatk = 29; + modest.speed = 25; + AdvIVs adamant; + adamant.hp = 25; + adamant.attack = 29; + adamant.speed = 25; + AdvIVs neutral = modest; + neutral.attack = neutral.defense = neutral.spdef = 16; + + using enum AdvNature; + return { + {"Modest", {Modest}, modest, MODEST_ROWS}, + {"Adamant", {Adamant}, adamant, ADAMANT_ROWS}, + {"Neutral", {Hardy, Docile, Serious, Bashful, Quirky}, neutral, NEUTRAL_ROWS}, + }; +} + +std::vector parse_rows(const std::string& text){ + std::vector rows; + std::istringstream lines(text); + std::string line; + while (std::getline(lines, line)){ + std::vector cells; + std::istringstream fields(line); + std::string cell; + while (std::getline(fields, cell, ',')){ + cells.push_back(cell); + } + rows.push_back({ + uint32_t(std::stoul(cells[0], nullptr, 16)), + uint32_t(std::stoul(cells[1], nullptr, 16)), + cells[2], + std::stoi(cells[3]), + cells[4], + cells[5], + uint16_t(std::stoul(cells[6], nullptr, 16)), + std::stoull(cells[7]), + std::stoull(cells[8]), + cells[9], + }); + } + return rows; +} + +std::string hex(uint32_t value){ + std::ostringstream out; + out << std::hex << std::uppercase << value; + return out.str(); +} + +std::string ivs_text(const AdvIVs& ivs){ + return std::to_string(ivs.hp) + "/" + std::to_string(ivs.attack) + "/" + std::to_string(ivs.defense) + + "/" + std::to_string(ivs.spatk) + "/" + std::to_string(ivs.spdef) + "/" + std::to_string(ivs.speed); +} + +std::string shiny_text(AdvShinyType type){ + switch (type){ + case AdvShinyType::Square: return "Square"; + case AdvShinyType::Star: return "Star"; + default: return "No"; + } +} + +// Ten Lines writes "Blackout R"; its Switch offsets for R and L are the same, and the +// RNG programs hold L. +std::string buttons_text(const InitialSeedEntry& entry){ + std::string seed_button = entry.seed_button == SeedButton::Start ? "Start" + : entry.seed_button == SeedButton::L ? "L" : "A"; + return seed_button + (entry.blackout_button == BlackoutButton::None ? "/None" : "/Blackout R"); +} + +std::string row_name(const TenLinesRow& row){ + return "Target " + hex(row.state) + ": "; +} + +uint32_t advance(uint32_t state, uint64_t advances){ + for (uint64_t c = 0; c < advances; c++){ + state = lcrng_next(state); + } + return state; +} + + +std::string check_pokemon(const TenLinesRow& row, const TenLinesGroup& group){ + AdvPokemonResult pokemon = method1_pokemon(row.state); + HiddenPower power = hidden_power(pokemon.ivs); + std::string actual = hex(pokemon.pid) + " " + shiny_text(shiny_type(pokemon.pid, TID, SID)) + + " " + std::to_string(int(pokemon.ability)) + " " + ivs_text(pokemon.ivs) + + " " + power.type + " " + std::to_string(power.power); + std::string expected = hex(row.pid) + " " + row.shiny + " " + std::to_string(row.ability) + + " " + row.ivs + " " + row.hidden_power; + if (actual != expected){ + return row_name(row) + "expected " + expected + ", got " + actual; + } + const std::vector& natures = group.natures; + if (std::find(natures.begin(), natures.end(), pokemon.nature) == natures.end()){ + return row_name(row) + "nature " + nature_to_string(pokemon.nature) + " is not in the group"; + } + return ""; +} + +std::string check_advances(const TenLinesRow& row){ + if (advance(row.seed, row.advances) != row.state){ + return row_name(row) + "seed " + hex(row.seed) + " + " + std::to_string(row.advances) + + " advances does not reach it"; + } + if (lcrng_distance(row.seed, row.state) != row.advances){ + return row_name(row) + "wrong distance from seed " + hex(row.seed); + } + // The RNG programs count advances the same way. + AdvRngSearcher program(row.seed, row.advances, AdvRngMethod::Method1); + if (program.generate_pokemon().pid != row.pid){ + return row_name(row) + "AdvRngSearcher gives PID " + hex(program.generate_pokemon().pid); + } + return ""; +} + +class Test_RngTargetMethod1 : public UnitTest{ +public: + Test_RngTargetMethod1(TenLinesGroup group) + : UnitTest("PokemonFRLG::RngTargetSearch - Method 1 - " + group.name) + , m_group(std::move(group)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + for (const TenLinesRow& row : parse_rows(m_group.rows)){ + std::string error = check_pokemon(row, m_group); + if (error.empty()){ + error = check_advances(row); + } + if (!error.empty()){ + return error; + } + } + return true; + } + +private: + const TenLinesGroup m_group; +}; + + +RngTargetFilters ten_lines_filters(const TenLinesGroup& group){ + RngTargetFilters filters; + filters.natures = group.natures; + filters.min_ivs = group.min_ivs; + filters.min_advances = 3000; + filters.avoided_seed_time_ms = 30500; + filters.avoided_seed_time_radius_ms = 3000; + return filters; +} + +std::vector leafgreen_mono_help_seeds(){ + SeedsDatabase database(seeds_json_path(false, Language::English)); + return initial_seed_entries(database, SoundSetting::Mono, "Help", true); +} + +std::string compare_seed_entry(const TenLinesRow& row, const InitialSeedEntry& entry){ + std::string expected = std::to_string(row.seed_time_ms) + " " + row.buttons; + std::string actual = std::to_string(entry.seed_time_ms) + " " + buttons_text(entry); + // Ten Lines lists every way to reach a seed, and the RNG programs use the earliest + // (SeedsDatabase::find_seed()): 3136 by Start at 58991 ms, not by A at 63359 ms. + if (buttons_text(entry) != row.buttons){ + return entry.seed_time_ms < row.seed_time_ms ? "" : "Expected " + expected + ", got " + actual; + } + // Ten Lines rounds the seed time down from 1/16 frames; the seed table here is 1 ms + // lower on about 1 row in 17 (61023 instead of 61024 for row 1833). + if (entry.seed_time_ms > row.seed_time_ms || entry.seed_time_ms + 1 < row.seed_time_ms){ + return "Expected " + expected + ", got " + actual; + } + return ""; +} + +std::string compare_target(const TenLinesRow& row, const RngTarget& target){ + std::string expected = hex(row.state) + " " + hex(row.seed) + " " + std::to_string(row.advances); + std::string actual = hex(target.state) + " " + hex(target.seed.seed) + " " + std::to_string(target.advances); + if (actual != expected){ + return "Expected " + expected + ", got " + actual; + } + std::string error = compare_seed_entry(row, target.seed); + if (!error.empty()){ + return row_name(row) + error; + } + if (target.shiny_for.size() != 1 || shiny_text(target.shiny_for[0].type) != row.shiny || target.shiny_probability != 1){ + return row_name(row) + "expected shiny (" + row.shiny + ") for the one SID"; + } + return ""; +} + +class Test_RngTargetSearch : public UnitTest{ +public: + Test_RngTargetSearch(TenLinesGroup group) + : UnitTest("PokemonFRLG::RngTargetSearch - Search - " + group.name) + , m_group(std::move(group)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::vector targets = search_rng_targets( + leafgreen_mono_help_seeds(), TID, {{SID, 1}}, ten_lines_filters(m_group) + ); + std::vector rows = parse_rows(m_group.rows); + if (targets.size() < rows.size()){ + return "Expected at least " + std::to_string(rows.size()) + " targets, got " + std::to_string(targets.size()); + } + for (size_t c = 0; c < rows.size(); c++){ + std::string error = compare_target(rows[c], targets[c]); + if (!error.empty()){ + return "Target #" + std::to_string(c + 1) + ": " + error; + } + } + return true; + } + +private: + const TenLinesGroup m_group; +}; + + +// Searching every IV combination finds the same shiny targets as searching shiny PIDs. +class Test_RngTargetIvSearch : public UnitTest{ +public: + Test_RngTargetIvSearch() + : UnitTest("PokemonFRLG::RngTargetSearch - IV search") + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const TenLinesGroup group = groups()[2]; + std::vector seeds = leafgreen_mono_help_seeds(); + RngTargetFilters filters = ten_lines_filters(group); + std::vector shiny = search_rng_targets(seeds, TID, {{SID, 1}}, filters); + + filters.shiny_required = false; + std::vector any = search_rng_targets(seeds, TID, {{SID, 1}}, filters); + std::vector any_shiny; + for (const RngTarget& target : any){ + if (!target.shiny_for.empty()){ + any_shiny.push_back(target); + } + } + if (any.size() <= any_shiny.size()){ + return "Expected targets that are not shiny"; + } + if (any_shiny.size() != shiny.size()){ + return "Expected " + std::to_string(shiny.size()) + " shiny targets, got " + std::to_string(any_shiny.size()); + } + for (size_t c = 0; c < shiny.size(); c++){ + if (any_shiny[c].state != shiny[c].state || any_shiny[c].advances != shiny[c].advances){ + return "Shiny target #" + std::to_string(c + 1) + " differs: " + hex(any_shiny[c].state); + } + } + return true; + } +}; + + +// A second SID candidate: each target is shiny for one of the two, by weight. +class Test_RngTargetCandidates : public UnitTest{ +public: + Test_RngTargetCandidates() + : UnitTest("PokemonFRLG::RngTargetSearch - SID candidates") + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const TenLinesGroup group = groups()[0]; + const uint16_t other_sid = SID ^ 0x0100; + std::vector targets = search_rng_targets( + leafgreen_mono_help_seeds(), TID, {{SID, 3}, {other_sid, 1}}, ten_lines_filters(group) + ); + size_t found = 0; + for (const RngTarget& target : targets){ + if (target.shiny_for.size() != 1){ + return "Target " + hex(target.state) + " is shiny for " + std::to_string(target.shiny_for.size()) + " SIDs"; + } + double expected = target.shiny_for[0].candidate == 0 ? 0.75 : 0.25; + if (target.shiny_probability != expected){ + return "Target " + hex(target.state) + " has shiny probability " + std::to_string(target.shiny_probability); + } + found += target.shiny_for[0].candidate == 0 && target.advances <= 171172; + } + if (found != 20){ + return "Expected the 20 Ten Lines targets for SID " + std::to_string(SID) + ", got " + std::to_string(found); + } + return true; + } +}; + + +class Test_RngTargetRanking : public UnitTest{ +public: + Test_RngTargetRanking() + : UnitTest("PokemonFRLG::RngTargetSearch - Ranking") + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + auto target = [](uint32_t state, uint64_t advances, double probability){ + RngTarget ret{state, {0, 40000, SeedButton::A, BlackoutButton::None}, advances, {}, {}, probability}; + return ret; + }; + // About 60 advances per second, after a 40 s seed time: A takes 207 s and B 249 s, + // but A is shiny only half the time. D takes 57 s, but is shiny one time in 10. + std::vector targets{ + target(0xA, 10000, 0.5), + target(0xB, 12500, 1), + target(0xC, 100, 0), + target(0xD, 1000, 0.1), + }; + rank_rng_targets(targets); + std::string order; + for (const RngTarget& item : targets){ + order += hex(item.state); + } + if (order != "BADC"){ + return "Expected order BADC, got " + order; + } + return true; + } +}; + +} + + +void add_tests_RngTargetSearch(UnitTestDatabase& database){ + for (const TenLinesGroup& group : groups()){ + database.add(group); + database.add(group); + } + database.add(); + database.add(); + database.add(); +} + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch_Tests.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch_Tests.h new file mode 100644 index 0000000000..0dc22e8b00 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch_Tests.h @@ -0,0 +1,23 @@ +/* RNG Target Search Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_RngTargetSearch_Tests_H +#define PokemonAutomation_PokemonFRLG_RngTargetSearch_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +void add_tests_RngTargetSearch(UnitTestDatabase& database); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_SeedsDatabase.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_SeedsDatabase.h index 12c1b2a77e..a619054ca0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_SeedsDatabase.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_SeedsDatabase.h @@ -44,10 +44,6 @@ struct SeedMatch{ class SeedsDatabase{ public: - SeedsDatabase(const std::string& json_path); - SeedMatch find_seed(uint16_t target_seed, SoundSetting sound, int radius) const; - -private: struct SeedColumn{ std::string name; SoundSetting sound; @@ -56,6 +52,15 @@ class SeedsDatabase{ std::vector seeds; }; +public: + SeedsDatabase(const std::string& json_path); + SeedMatch find_seed(uint16_t target_seed, SoundSetting sound, int radius) const; + + // The delay in milliseconds before each row of the columns. + const std::vector& delays() const{ return m_delays; } + const std::vector& columns() const{ return m_columns; } + +private: std::vector m_delays; std::vector m_columns; }; diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index dad3af3e6b..84ac888f12 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1805,6 +1805,10 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngNavigation.h Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngStatsDatabase.cpp Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngStatsDatabase.h + Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch.cpp + Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch.h + Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch_Tests.cpp + Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngTargetSearch_Tests.h Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RoamingLegendaryRng.cpp Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RoamingLegendaryRng.h Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_SeedsDatabase.cpp