diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp index eff0ef6726..da65ade93f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp @@ -29,7 +29,10 @@ #include "Inference/PokemonFRLG_WildEncounterReader.h" #include "Programs/BoxOrganization/PokemonFRLG_BoxSorter_Tests.h" #include "PokemonFRLG_Navigation.h" +#include "Programs/RngManipulation/PokemonFRLG_RngPlanInputs_Tests.h" +#include "Programs/RngManipulation/PokemonFRLG_RngPlanLog_Tests.h" #include "Programs/RngManipulation/PokemonFRLG_RngPlanTracker_Tests.h" +#include "Programs/RngManipulation/PokemonFRLG_RngPlan_Tests.h" #include "Programs/RngManipulation/PokemonFRLG_RngTargetSearch_Tests.h" #include "Tests/TestUtils.h" @@ -293,7 +296,10 @@ void add_tests(UnitTestDatabase& database){ add_tests_TrainerIdReader(database); add_tests_WildEncounterReader(database); add_tests_RngTargetSearch(database); + add_tests_RngPlanInputs(database); + add_tests_RngPlan(database); add_tests_RngPlanTracker(database); + add_tests_RngPlanLog(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanInputs.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanInputs.cpp new file mode 100644 index 0000000000..9e0d1a087f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanInputs.cpp @@ -0,0 +1,106 @@ +/* RNG Plan Inputs + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "PokemonFRLG_RngPlanInputs.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +using namespace Pokemon; + + +namespace{ + +std::string trim(const std::string& text){ + const size_t first = text.find_first_not_of(' '); + if (first == std::string::npos){ + return ""; + } + const size_t last = text.find_last_not_of(' '); + return text.substr(first, last - first + 1); +} + +std::vector split(const std::string& text, char separator){ + std::vector parts; + size_t start = 0; + while (true){ + const size_t next = text.find(separator, start); + parts.push_back(trim(text.substr(start, next == std::string::npos ? std::string::npos : next - start))); + if (next == std::string::npos){ + return parts; + } + start = next + 1; + } +} + +} + + +std::optional parse_trainer_id(const std::string& text){ + const std::string digits = trim(text); + if (digits.empty() || digits.size() > 5 || !std::all_of(digits.begin(), digits.end(), [](char ch){ return ch >= '0' && ch <= '9'; })){ + return std::nullopt; + } + const int value = std::stoi(digits); + if (value > 0xffff){ + return std::nullopt; + } + return uint16_t(value); +} + +std::optional parse_ivs(const std::string& text){ + const std::vector parts = split(text, '/'); + if (parts.size() != 6){ + return std::nullopt; + } + uint8_t values[6]; + for (size_t c = 0; c < 6; c++){ + const std::string& part = parts[c]; + if (part.empty() || part.size() > 2 || !std::all_of(part.begin(), part.end(), [](char ch){ return ch >= '0' && ch <= '9'; })){ + return std::nullopt; + } + const int value = std::stoi(part); + if (value > 31){ + return std::nullopt; + } + values[c] = uint8_t(value); + } + AdvIVs ivs; + ivs.hp = values[0]; + ivs.attack = values[1]; + ivs.defense = values[2]; + ivs.spatk = values[3]; + ivs.spdef = values[4]; + ivs.speed = values[5]; + return ivs; +} + +std::string ivs_to_string(const AdvIVs& ivs){ + return std::format("{}/{}/{}/{}/{}/{}", ivs.hp, ivs.attack, ivs.defense, ivs.spatk, ivs.spdef, ivs.speed); +} + +std::optional> parse_natures(const std::string& text){ + std::vector natures; + if (trim(text).empty()){ + return natures; + } + for (const std::string& name : split(text, ',')){ + const AdvNature nature = string_to_nature(name); + if (nature == AdvNature::Any){ + return std::nullopt; + } + natures.push_back(nature); + } + return natures; +} + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanInputs.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanInputs.h new file mode 100644 index 0000000000..da7fe44525 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanInputs.h @@ -0,0 +1,39 @@ +/* RNG Plan Inputs + * + * From: https://github.com/PokemonAutomation/ + * + * The TID, IVs and natures an RNG plan is typed in with, as text. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_RngPlanInputs_H +#define PokemonAutomation_PokemonFRLG_RngPlanInputs_H + +#include +#include +#include +#include +#include "Pokemon/Pokemon_AdvRng.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// A TID or SID typed as a number from 0 to 65535. Nothing for any other text. +std::optional parse_trainer_id(const std::string& text); + +// "HP/Atk/Def/SpA/SpD/Spe", as Ten Lines and the RNG programs list IVs. +// Nothing for text that isn't six numbers from 0 to 31. +std::optional parse_ivs(const std::string& text); +std::string ivs_to_string(const Pokemon::AdvIVs& ivs); + +// Natures separated by commas ("Modest, Timid"). Empty text is any nature +// (no entries). Nothing for a name that isn't a nature. +std::optional> parse_natures(const std::string& text); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanInputs_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanInputs_Tests.cpp new file mode 100644 index 0000000000..6cf8e40bde --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanInputs_Tests.cpp @@ -0,0 +1,137 @@ +/* RNG Plan Inputs Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonFRLG_RngPlanInputs.h" +#include "PokemonFRLG_RngPlanInputs_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +using namespace Pokemon; + + +namespace{ + +class Test_RngPlanInputs : public UnitTest{ +public: + Test_RngPlanInputs(std::string name, std::function body) + : UnitTest("PokemonFRLG::RngPlanInputs - " + std::move(name)) + , m_body(std::move(body)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_body(logger); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + std::function m_body; +}; + + +UnitTestResult test_parse_trainer_id(Logger&){ + struct Case{ + std::string text; + std::string expected; + }; + const Case cases[] = { + {.text = "64694", .expected = "64694"}, + {.text = " 0 ", .expected = "0"}, + {.text = "65535", .expected = "65535"}, + {.text = "65536", .expected = "none"}, + {.text = "", .expected = "none"}, + {.text = "-1", .expected = "none"}, + {.text = "12a", .expected = "none"}, + }; + for (const Case& item : cases){ + const std::optional sid = parse_trainer_id(item.text); + const std::string got = sid ? std::to_string(*sid) : "none"; + if (got != item.expected){ + return std::format("SID \"{}\": expected {}, got {}.", item.text, item.expected, got); + } + } + return true; +} + +UnitTestResult test_parse_ivs(Logger&){ + struct Case{ + std::string text; + std::string expected; + }; + const Case cases[] = { + {.text = "31/14/7/29/1/27", .expected = "31/14/7/29/1/27"}, + {.text = " 0 / 0/0/0/0/31 ", .expected = "0/0/0/0/0/31"}, + {.text = "31/0/0", .expected = "none"}, + {.text = "32/0/0/0/0/0", .expected = "none"}, + {.text = "a/0/0/0/0/0", .expected = "none"}, + {.text = "-1/0/0/0/0/0", .expected = "none"}, + {.text = "", .expected = "none"}, + }; + for (const Case& item : cases){ + const std::optional ivs = parse_ivs(item.text); + const std::string got = ivs ? ivs_to_string(*ivs) : "none"; + if (got != item.expected){ + return std::format("IVs \"{}\": expected {}, got {}.", item.text, item.expected, got); + } + } + return true; +} + +UnitTestResult test_parse_natures(Logger&){ + struct Case{ + std::string text; + std::string expected; + }; + const Case cases[] = { + {.text = "", .expected = "any"}, + {.text = " ", .expected = "any"}, + {.text = "Modest", .expected = "Modest"}, + {.text = "Modest, Timid", .expected = "Modest Timid"}, + {.text = "Modset", .expected = "none"}, + {.text = "Modest,", .expected = "none"}, + }; + for (const Case& item : cases){ + const std::optional> natures = parse_natures(item.text); + std::string got; + if (!natures){ + got = "none"; + }else if (natures->empty()){ + got = "any"; + }else{ + for (AdvNature nature : *natures){ + got += (got.empty() ? "" : " ") + nature_to_string(nature); + } + } + if (got != item.expected){ + return std::format("Natures \"{}\": expected {}, got {}.", item.text, item.expected, got); + } + } + return true; +} + +} + + +void add_tests_RngPlanInputs(UnitTestDatabase& database){ + database.add("TID and SID text", test_parse_trainer_id); + database.add("IVs text", test_parse_ivs); + database.add("natures text", test_parse_natures); +} + + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanInputs_Tests.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanInputs_Tests.h new file mode 100644 index 0000000000..5da86a73d4 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanInputs_Tests.h @@ -0,0 +1,23 @@ +/* RNG Plan Inputs Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_RngPlanInputs_Tests_H +#define PokemonAutomation_PokemonFRLG_RngPlanInputs_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +void add_tests_RngPlanInputs(UnitTestDatabase& database); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanLog.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanLog.cpp new file mode 100644 index 0000000000..a10f229e73 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanLog.cpp @@ -0,0 +1,137 @@ +/* RNG Plan Log + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Exceptions.h" +#include "VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanInputs.h" +#include "PokemonFRLG_RngPlanLog.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +using namespace Pokemon; + + +namespace{ + +std::string join_sids(const std::vector& sids){ + std::string text; + for (uint16_t sid : sids){ + text += (text.empty() ? "" : ",") + std::to_string(sid); + } + return text; +} + +} + +std::string rng_log_tid(uint16_t tid, bool from_card){ + return std::format("RngPlan: tid={} source={}", tid, from_card ? "card" : "option"); +} + +std::string rng_log_sid_helper(uint16_t tid, uint32_t target_advances, const std::vector& candidates){ + std::vector sids; + for (const SidCandidate& candidate : candidates){ + sids.push_back(candidate.sid); + } + return std::format("RngPlan: sid_helper tid={} target_advances={} candidates={}", tid, target_advances, join_sids(sids)); +} + +std::string rng_log_attempt(size_t index, const RngAttempt& attempt){ + return std::format( + "RngPlan: attempt={} seed={:04X} advances={} sids={} seed_wait_ms={} ingame_ms={}", + index + 1, attempt.target.seed.seed, attempt.target.advances, join_sids(attempt.sids), + starter_rng_seed_wait_ms(attempt.target.seed), starter_rng_ingame_ms(attempt.target.advances) + ); +} + +namespace{ + +const char* outcome_name(RngAttemptOutcome outcome){ + switch (outcome){ + case RngAttemptOutcome::SHINY: + return "shiny"; + case RngAttemptOutcome::SHINY_OFF_TARGET: + return "shiny_off_target"; + case RngAttemptOutcome::HIT_NOT_SHINY: + return "hit_not_shiny"; + case RngAttemptOutcome::MISSED: + return "missed"; + case RngAttemptOutcome::UNUSABLE: + return "unusable"; + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown RngAttemptOutcome."); +} + +const char* next_name(RngPlanNext next){ + switch (next){ + case RngPlanNext::RETRY: + return "retry"; + case RngPlanNext::NEXT_ATTEMPT: + return "next_attempt"; + case RngPlanNext::CONFIRM: + return "confirm"; + case RngPlanNext::STOP: + return "stop"; + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown RngPlanNext."); +} + +} + +std::string rng_log_result(const RngResultLog& result){ + const std::optional& pokemon = result.pokemon; + return std::format( + "RngPlan: result attempt={} try={} outcome={} starter_rng={} nature={} ivs={} pid={} next={}", + result.index + 1, result.try_number, outcome_name(result.outcome), + result.detail.empty() ? "-" : result.detail, + pokemon ? nature_to_string(pokemon->nature) : "-", + pokemon ? ivs_to_string(pokemon->ivs) : "-", + pokemon ? std::format("{:08X}", pokemon->pid) : "-", + next_name(result.next) + ); +} + +std::string rng_log_confirmed(uint16_t tid, uint16_t sid, const std::vector& sid_helper_candidates){ + size_t k = 0; + for (size_t c = 0; c < sid_helper_candidates.size(); c++){ + if (sid_helper_candidates[c].sid == sid){ + k = c + 1; + break; + } + } + return std::format("RngPlan: confirmed tid={} sid={} candidate={} of={}", tid, sid, k, sid_helper_candidates.size()); +} + + +std::vector rng_attempt_lines(const std::vector& attempts){ + std::vector lines; + for (size_t c = 0; c < attempts.size(); c++){ + const RngTarget& target = attempts[c].target; + std::string shiny; + if (attempts[c].sids.empty()){ + shiny = "not shiny for any SID candidate"; + }else{ + shiny = "shiny for SID"; + for (size_t s = 0; s < attempts[c].sids.size(); s++){ + shiny += (s == 0 ? " " : ", ") + std::to_string(attempts[c].sids[s]); + } + shiny += std::format(" ({:.0f}% of the candidates left)", target.shiny_probability * 100); + } + lines.push_back(std::format( + "Attempt {}: Target Seed {:04X}, Advances {}: {}, IVs {}, PID {:08X}, {}.", + c + 1, target.seed.seed, target.advances, + nature_to_string(target.pokemon.nature), ivs_to_string(target.pokemon.ivs), target.pokemon.pid, + shiny + )); + } + return lines; +} + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanLog.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanLog.h new file mode 100644 index 0000000000..f46954234b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanLog.h @@ -0,0 +1,77 @@ +/* RNG Plan Log + * + * From: https://github.com/PokemonAutomation/ + * + * The lines an RNG plan logs and shows: its attempts, and the "RngPlan:" + * lines below. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_RngPlanLog_H +#define PokemonAutomation_PokemonFRLG_RngPlanLog_H + +#include +#include +#include +#include +#include "Pokemon/Pokemon_AdvRng.h" +#include "VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlan.h" +#include "VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanTracker.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// One line per attempt, for the log and the stop message: the Target Seed +// and Advances to give Starter RNG, the Pokemon, and the SIDs it is shiny for. +std::vector rng_attempt_lines(const std::vector& attempts); + + +// The data to rebuild how likely SID Helper's k-th candidate of N is (the +// 1-2-3-2-1 weights of sid_helper_candidates() are a guess), from the logs +// alone, and to follow AutoStory's Starter RNG loop. Every line starts with +// "RngPlan:"; grep for it. In the order a run logs them: +// RngPlan: tid= source= +// RngPlan: sid_helper tid= target_advances= candidates=,... +// RngPlan: attempt= seed= advances= sids=,... +// seed_wait_ms= ingame_ms= (Starter RNG's waits, before calibration) +// then, with Run Starter RNG in AutoStory, for each try of an attempt +// (rng_log_state() in PokemonFRLG_StarterRngLoop.h): +// RngPlan: state attempt=/ try=/ reset=/ seed= +// advances= sids=,... ruled_out=,... attempt_time=/ +// total_time=/ calibration=// +// (once at the try's start, then after every reset) +// RngPlan: result attempt= try= outcome= +// starter_rng= nature= ivs= +// pid= next= +// RngPlan: confirmed tid= sid= candidate= of= +// Lists that are empty print "-". +// "candidates" are in SID Helper's order; "candidate=k" counts from 1 in +// it, 0 when the SID wasn't among them. Count each TID's "confirmed" once: +// every later run with the SID set logs it again. While the user runs +// Starter RNG by hand, its own "Target Hit!" and "Missed target." lines +// are the attempts' results. +std::string rng_log_tid(uint16_t tid, bool from_card); +std::string rng_log_sid_helper(uint16_t tid, uint32_t target_advances, const std::vector& candidates); +std::string rng_log_attempt(size_t index, const RngAttempt& attempt); + +struct RngResultLog{ + size_t index = 0; + // From 1. + size_t try_number = 1; + RngAttemptOutcome outcome = RngAttemptOutcome::MISSED; + // How the RNG program put it ("out_of_resets"); empty for none. + std::string detail; + // The Pokemon hit, when known. + std::optional pokemon; + RngPlanNext next = RngPlanNext::STOP; +}; +std::string rng_log_result(const RngResultLog& result); +std::string rng_log_confirmed(uint16_t tid, uint16_t sid, const std::vector& sid_helper_candidates); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanLog_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanLog_Tests.cpp new file mode 100644 index 0000000000..c69e725da9 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanLog_Tests.cpp @@ -0,0 +1,109 @@ +/* RNG Plan Log Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonFRLG_RngPlanTestTargets.h" +#include "PokemonFRLG_RngPlanInputs.h" +#include "PokemonFRLG_RngPlanLog.h" +#include "PokemonFRLG_RngPlanLog_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +using namespace Pokemon; +using namespace RngPlanTestTargets; + + +namespace{ + +class Test_RngPlanLog : public UnitTest{ +public: + Test_RngPlanLog(std::string name, std::function body) + : UnitTest("PokemonFRLG::RngPlanLog - " + std::move(name)) + , m_body(std::move(body)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_body(logger); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + std::function m_body; +}; + + +// The "RngPlan:" lines the candidate odds are rebuilt from. +UnitTestResult test_log_lines(Logger&){ + RngAttempt attempt{make_target(1, 50, {0, 1}), {100, 200}}; + attempt.target.seed.seed = 0x2ef6; + attempt.target.advances = 29364; + struct Case{ + std::string got; + std::string expected; + }; + const Case cases[] = { + {.got = rng_log_tid(43771, true), .expected = "RngPlan: tid=43771 source=card"}, + {.got = rng_log_tid(43771, false), .expected = "RngPlan: tid=43771 source=option"}, + {.got = rng_log_sid_helper(43771, 3001, CANDIDATES), .expected = "RngPlan: sid_helper tid=43771 target_advances=3001 candidates=100,200,300"}, + {.got = rng_log_attempt(0, attempt), .expected = std::format( + "RngPlan: attempt=1 seed=2EF6 advances=29364 sids=100,200 seed_wait_ms=49155 ingame_ms={}", starter_rng_ingame_ms(29364) + )}, + { + .got = rng_log_result(RngResultLog{ + .index = 1, .try_number = 2, .outcome = RngAttemptOutcome::HIT_NOT_SHINY, .detail = "target_hit", + .pokemon = attempt.target.pokemon, .next = RngPlanNext::NEXT_ATTEMPT, + }), + .expected = std::format( + "RngPlan: result attempt=2 try=2 outcome=hit_not_shiny starter_rng=target_hit nature={} ivs={} pid={:08X} next=next_attempt", + nature_to_string(attempt.target.pokemon.nature), ivs_to_string(attempt.target.pokemon.ivs), attempt.target.pokemon.pid + ), + }, + { + .got = rng_log_result(RngResultLog{ + .index = 0, .try_number = 1, .outcome = RngAttemptOutcome::MISSED, .detail = "out_of_resets", + .pokemon = std::nullopt, .next = RngPlanNext::RETRY, + }), + .expected = "RngPlan: result attempt=1 try=1 outcome=missed starter_rng=out_of_resets nature=- ivs=- pid=- next=retry", + }, + { + .got = rng_log_result(RngResultLog{ + .index = 2, .try_number = 3, .outcome = RngAttemptOutcome::SHINY_OFF_TARGET, .detail = "shiny_off_target", + .pokemon = std::nullopt, .next = RngPlanNext::STOP, + }), + .expected = "RngPlan: result attempt=3 try=3 outcome=shiny_off_target starter_rng=shiny_off_target nature=- ivs=- pid=- next=stop", + }, + {.got = rng_log_confirmed(43771, 300, CANDIDATES), .expected = "RngPlan: confirmed tid=43771 sid=300 candidate=3 of=3"}, + {.got = rng_log_confirmed(43771, 7, CANDIDATES), .expected = "RngPlan: confirmed tid=43771 sid=7 candidate=0 of=3"}, + }; + for (const Case& item : cases){ + if (item.got != item.expected){ + return "Expected \"" + item.expected + "\", got \"" + item.got + "\"."; + } + } + return true; +} + +} + + +void add_tests_RngPlanLog(UnitTestDatabase& database){ + database.add("RngPlan log lines", test_log_lines); +} + + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanLog_Tests.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanLog_Tests.h new file mode 100644 index 0000000000..9f62eb0b0a --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanLog_Tests.h @@ -0,0 +1,23 @@ +/* RNG Plan Log Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_RngPlanLog_Tests_H +#define PokemonAutomation_PokemonFRLG_RngPlanLog_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +void add_tests_RngPlanLog(UnitTestDatabase& database); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlan_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlan_Tests.cpp new file mode 100644 index 0000000000..508ba5fa34 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlan_Tests.cpp @@ -0,0 +1,287 @@ +/* RNG Plan Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The attempt order on made-up targets; SID Helper's candidates and a + * Starter RNG plan from the FireRed seed list. + * + */ + +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonFRLG_RngPlanTestTargets.h" +#include "PokemonFRLG_RngPlan.h" +#include "PokemonFRLG_RngPlanLog.h" +#include "PokemonFRLG_RngPlan_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +using namespace Pokemon; +using namespace RngPlanTestTargets; + + +namespace{ + +class Test_RngPlan : public UnitTest{ +public: + Test_RngPlan(std::string name, std::function body) + : UnitTest("PokemonFRLG::RngPlan - " + std::move(name)) + , m_body(std::move(body)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_body(logger); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + std::function m_body; +}; + + +UnitTestResult test_sid_helper_candidates(Logger&){ + const uint16_t tid = 43771; + const uint32_t target_advances = 3001; + const std::vector candidates = sid_helper_candidates(tid, target_advances, 5); + + // The SID is the top half of the state after the TID seed is advanced + // once, then "advances" more times. + auto sid_at = [&](uint32_t advances){ + uint32_t state = tid; + for (uint32_t c = 0; c <= advances; c++){ + state = lcrng_next(state); + } + return uint16_t(state >> 16); + }; + const std::vector advances = {2997, 2999, 3001, 3003, 3005}; + const std::vector weights = {1, 2, 3, 2, 1}; + if (candidates.size() != advances.size()){ + return std::format("Expected {} candidates, got {}.", advances.size(), candidates.size()); + } + for (size_t c = 0; c < candidates.size(); c++){ + if (candidates[c].sid != sid_at(advances[c]) || candidates[c].weight != weights[c]){ + return std::format( + "Candidate {}: expected SID {} weight {}, got SID {} weight {}.", + c, sid_at(advances[c]), weights[c], candidates[c].sid, candidates[c].weight + ); + } + } + + const std::vector even = sid_helper_candidates(tid, target_advances, 4); + const std::vector even_weights = {1, 2, 3, 2}; + for (size_t c = 0; c < even.size() && c < even_weights.size(); c++){ + if (even.size() != even_weights.size() || even[c].sid != candidates[c].sid || even[c].weight != even_weights[c]){ + return std::format("Four candidates: expected the first four of five, weighing 1, 2, 3, 2; candidate {} differs.", c); + } + } + return true; +} + + +struct PlanCase{ + std::string name; + std::vector targets; + bool shiny_required; + size_t max_attempts; + std::vector states; + std::vector> sids; +}; +std::vector plan_cases(){ + const std::vector no_300 = {TARGETS[0], TARGETS[2], TARGETS[3]}; + return { + { + .name = "two candidates in one target first, then the last", + .targets = TARGETS, .shiny_required = true, .max_attempts = 5, + .states = {1, 2}, .sids = {{100, 200}, {300}}, + }, + { + .name = "capped at one attempt", + .targets = TARGETS, .shiny_required = true, .max_attempts = 1, + .states = {1}, .sids = {{100, 200}}, + }, + { + .name = "no target shiny for SID 300", + .targets = no_300, .shiny_required = true, .max_attempts = 5, + .states = {1}, .sids = {{100, 200}}, + }, + { + .name = "shiny not required: the quickest", + .targets = TARGETS, .shiny_required = false, .max_attempts = 5, + .states = {4}, .sids = {{}}, + }, + { + .name = "no targets", + .targets = {}, .shiny_required = true, .max_attempts = 5, + .states = {}, .sids = {}, + }, + }; +} + +UnitTestResult test_plan_order(Logger&){ + for (const PlanCase& item : plan_cases()){ + const std::vector attempts = plan_rng_attempts(item.targets, CANDIDATES, item.shiny_required, item.max_attempts); + std::vector states; + std::vector> sids; + for (const RngAttempt& attempt : attempts){ + states.push_back(attempt.target.state); + sids.push_back(attempt.sids); + } + if (states != item.states || sids != item.sids){ + std::string got; + for (size_t c = 0; c < attempts.size(); c++){ + got += std::format(" target {} {}", states[c], join(sids[c])); + } + return item.name + ": got" + (got.empty() ? " no attempts" : got) + "."; + } + } + return true; +} + + +// A plan from the FireRed (English) list: every attempt starts from a seed +// Starter RNG finds, takes at least its smallest Advances, and is shiny for +// the SIDs it lists, which no earlier attempt listed. +// Console B, 2026-11-02, FireRed English, Mono, Help: Starter RNG refused +// both of these as title screen waits under 30 s. AutoStory's plan for TID +// 55740 started with 11A3 at 945 advances (29,628 ms after Starter RNG's +// offsets); StarterRng by hand with 405C at 990 advances listed 30,488 ms +// from the table, 29,643 ms after them. Neither seed may start an attempt. +UnitTestResult test_starter_plan_seed_delay(Logger&){ + struct Case{ + uint16_t seed; + uint64_t table_ms; + }; + const Case cases[] = { + {.seed = 0x11a3, .table_ms = 30473}, + {.seed = 0x405c, .table_ms = 30488}, + }; + const SeedsDatabase database(seeds_json_path(true, Language::English)); + const std::vector seeds = initial_seed_entries(database, SoundSetting::Mono, "Help", true); + for (const Case& item : cases){ + auto entry = std::find_if(seeds.begin(), seeds.end(), [&](const InitialSeedEntry& seed){ return seed.seed == item.seed; }); + if (entry == seeds.end() || entry->seed_time_ms != item.table_ms){ + return std::format("Expected seed {:04X} at {} ms in the FireRed English list (Mono, Help).", item.seed, item.table_ms); + } + if (starter_rng_can_wait_for(*entry)){ + return std::format("Seed {:04X} ({} ms): expected too early for Starter RNG's title screen.", item.seed, item.table_ms); + } + } + + const uint16_t tid = 55740; + const std::vector attempts = plan_starter_rng(StarterRngPlanInputs{ + .firered = true, + .language = Language::English, + .sound = SoundSetting::Mono, + .tid = tid, + .candidates = sid_helper_candidates(tid, 3001, 5), + .filters = {}, + .max_attempts = 5, + }); + if (attempts.empty()){ + return "Expected attempts for TID 55740, got none."; + } + for (size_t c = 0; c < attempts.size(); c++){ + const InitialSeedEntry& seed = attempts[c].target.seed; + if (seed.seed == 0x11a3 || seed.seed == 0x405c || starter_rng_seed_wait_ms(seed) < 31500){ + return std::format("Attempt {}: seed {:04X} at {} ms is too early for the title screen.", c + 1, seed.seed, seed.seed_time_ms); + } + if (attempts[c].target.advances < 1660){ + return std::format("Attempt {}: {} advances leave no room for the in-game calibration.", c + 1, attempts[c].target.advances); + } + } + return true; +} + +// Starter RNG's in-game wait: 7,500 ms at its 940-advance minimum; console +// B's B579 target at 967 advances was 7,725 ms, which one miss's +// calibration (-720) cut under the minimum. Targets start 720 advances on. +UnitTestResult test_starter_ingame_wait(Logger&){ + struct Case{ + uint64_t advances; + int64_t ms; + }; + const Case cases[] = { + {.advances = 940, .ms = 7500}, + {.advances = 967, .ms = 7725}, + {.advances = 1660, .ms = 13500}, + }; + for (const Case& item : cases){ + if (starter_rng_ingame_ms(item.advances) != item.ms){ + return std::format("{} advances: expected {} ms in game, got {}.", item.advances, item.ms, starter_rng_ingame_ms(item.advances)); + } + } + if (STARTER_RNG_PLAN_MIN_ADVANCES != 1660){ + return std::format("Expected the plan's minimum at 1660 advances, got {}.", STARTER_RNG_PLAN_MIN_ADVANCES); + } + return true; +} + +UnitTestResult test_starter_plan(Logger&){ + const uint16_t tid = 43771; + StarterRngPlanInputs inputs{ + .firered = true, + .language = Language::English, + .sound = SoundSetting::Mono, + .tid = tid, + .candidates = sid_helper_candidates(tid, 3001, 5), + .filters = {}, + .max_attempts = 5, + }; + const std::vector attempts = plan_starter_rng(inputs); + if (attempts.empty()){ + return "Expected attempts for TID 43771 with five SID candidates, got none."; + } + + const SeedsDatabase database(seeds_json_path(true, Language::English)); + std::vector listed; + for (size_t c = 0; c < attempts.size(); c++){ + const RngAttempt& attempt = attempts[c]; + if (!database.find_seed(attempt.target.seed.seed, SoundSetting::Mono, 5).found){ + return std::format("Attempt {}: Starter RNG can't find Target Seed {:04X}.", c + 1, attempt.target.seed.seed); + } + if (attempt.target.advances < STARTER_RNG_PLAN_MIN_ADVANCES){ + return std::format("Attempt {}: Advances {} below the plan's {}.", c + 1, attempt.target.advances, STARTER_RNG_PLAN_MIN_ADVANCES); + } + if (attempt.sids.empty()){ + return std::format("Attempt {}: shiny for no SID candidate.", c + 1); + } + for (uint16_t sid : attempt.sids){ + if (shiny_type(attempt.target.pokemon.pid, tid, sid) == AdvShinyType::Normal){ + return std::format("Attempt {}: PID {:08X} not shiny for SID {}.", c + 1, attempt.target.pokemon.pid, sid); + } + if (std::find(listed.begin(), listed.end(), sid) != listed.end()){ + return std::format("Attempt {}: SID {} already listed by an earlier attempt.", c + 1, sid); + } + listed.push_back(sid); + } + } + if (rng_attempt_lines(attempts).size() != attempts.size()){ + return "Expected one line per attempt."; + } + return true; +} + +} + + +void add_tests_RngPlan(UnitTestDatabase& database){ + database.add("SID Helper's candidates and weights", test_sid_helper_candidates); + database.add("attempt order", test_plan_order); + database.add("Starter RNG plan from the FireRed list", test_starter_plan); + database.add("Starter RNG plan: no seed too early for the title screen", test_starter_plan_seed_delay); + database.add("Starter RNG plan: room for the in-game calibration", test_starter_ingame_wait); +} + + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlan_Tests.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlan_Tests.h new file mode 100644 index 0000000000..80d2408130 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlan_Tests.h @@ -0,0 +1,23 @@ +/* RNG Plan Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_RngPlan_Tests_H +#define PokemonAutomation_PokemonFRLG_RngPlan_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +void add_tests_RngPlan(UnitTestDatabase& database); + + +} +} +} +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 4fd1bde52e..2cb4d99044 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1825,6 +1825,16 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngNavigation.h Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlan.cpp Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlan.h + Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlan_Tests.cpp + Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlan_Tests.h + Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanInputs.cpp + Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanInputs.h + Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanInputs_Tests.cpp + Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanInputs_Tests.h + Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanLog.cpp + Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanLog.h + Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanLog_Tests.cpp + Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanLog_Tests.h Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanTestTargets.cpp Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanTestTargets.h Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngPlanTracker.cpp