diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp index d00e6e19eb..e4e8ec10be 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp @@ -83,6 +83,7 @@ class PlaceNameMatcher : public OCR::SmallDictionaryMatcher{ {"pokemon-tower", "Pokemon Tower"}, {"route-16", "Route 16"}, {"fuchsia-city", "Fuchsia City"}, + {"cinnabar-island", "Cinnabar Island"}, }){ JsonArray names; names.push_back(std::string(name)); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.cpp index b7839f7106..3e5bdbc156 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.cpp @@ -93,6 +93,8 @@ constexpr std::string_view MAP_NAMES[] = { "UndergroundPath_WestEntrance", "Route7", "CeladonCity", + // Celadon Gym, past the tree Cut fells. + "CeladonCity_Gym", // The Game Corner's poster and the Rocket Hideout down to B4F. "CeladonCity_GameCorner", "RocketHideout_B1F", @@ -141,6 +143,10 @@ constexpr std::string_view MAP_NAMES[] = { // Koga's gym, and the Warden's house (GOLD TEETH for HM04). "FuchsiaCity_Gym", "FuchsiaCity_WardensHouse", + // Cinnabar Island, and Route 21 north of it to Pallet Town (12.03). + "CinnabarIsland", + "Route21_North", + "Route21_South", "CeruleanCave_1F", "CeruleanCave_2F", "CeruleanCave_B1F", @@ -167,11 +173,28 @@ void add_unmapped_centers(std::vector& maps){ } } +// Pallet Town's fat man wanders over (7..19, 15..19), which holds the whole +// shore (7..10, 16) the way onto Route 21's water goes by. A planned route +// never crosses a wanderer's box, so none would reach the water. He stands on +// his own tile (13, 17) in the planner's model: east of the shore, off the +// way; a person in the way on the console is waited out (12.03). +void waive_pallet_fat_man_box(MapData& map){ + if (map.name != "PalletTown"){ + return; + } + for (MapObject& object : map.objects){ + if (object.name == "fat_man"){ + object.wander.reset(); + } + } +} + const std::vector& all_maps(){ static const std::vector MAPS = []{ std::vector maps; for (std::string_view name : MAP_NAMES){ maps.push_back(load_map(std::string(name))); + waive_pallet_fat_man_box(maps.back()); } add_unmapped_centers(maps); return maps; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp index 159281afb2..236afd6eb8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp @@ -109,6 +109,7 @@ static void add_tests_AutoStoryRows(UnitTestDatabase& database){ add_tests_AutoStorySafari(database); add_tests_AutoStoryFuchsiaGym(database); add_tests_AutoStoryHealing(database); + add_tests_AutoStoryCeladonGym(database); } void add_tests(UnitTestDatabase& database){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBag.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBag.h index 9bd80334fb..4fa024dbcf 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBag.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBag.h @@ -30,13 +30,17 @@ enum class BagPocket{ }; // The pocket of an item the story uses from the bag: repel, super-repel, -// max-repel, escape-rope (Items) or bicycle (Key Items); nothing for +// max-repel, escape-rope, rare-candy (Items) or bicycle (Key Items); nothing for // another. std::optional usable_item_pocket(std::string_view item); // Whether `item` is a Repel of any kind. bool is_repel(std::string_view item); +// Whether `item` is used on a Pokemon, the lead (the party list opens on +// it, as for a Potion in battle): the Rare Candy. +bool is_party_item(std::string_view item); + // The row of `item` in `rows` (read_bag_pocket()'s, from the top), if it // has one left: a count of -1 (unread) counts as some. std::optional bag_row_of(const std::vector& rows, std::string_view item); @@ -51,6 +55,10 @@ class ItemUseActions{ virtual std::vector read_pocket() = 0; // Moves to row `row`, USE, and through what the item says. virtual void use_row(size_t row) = 0; + // Moves to row `row`, USE, the lead, and through the level-up text + // (the stats window; a new move declined) back to the bag. False if + // the party list or the level-up never showed. + virtual bool use_row_on_lead(size_t row) = 0; // Moves to row `row` and REGISTERs it for SELECT. virtual void register_row(size_t row) = 0; // Back to the overworld (nothing to do if the item closed the bag). @@ -66,6 +74,8 @@ enum class ItemUseEnd{ NO_BAG, // Not an item the story uses from the bag. UNKNOWN_ITEM, + // The item was chosen but its use on the lead never showed its text. + NOT_USED, }; // "used", "none_left": the end's name in logs and failures. diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBagUse.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBagUse.h index 3d150e2084..051d56df9f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBagUse.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBagUse.h @@ -28,6 +28,15 @@ namespace PokemonFRLG{ // overworld. ItemUseEnd use_item_from_bag(Logger& logger, ItemUseActions& actions, std::string_view item); +// `item` used up to `count` times, one bag visit each: as many as the bag +// has. `end` is USED if any was used and the bag ran out; else why none was. +struct ItemUses{ + ItemUseEnd end; + size_t used; +}; +// Uses `item` up to `count` times from the bag. +ItemUses use_items_from_bag(Logger& logger, ItemUseActions& actions, std::string_view item, size_t count); + // The same, REGISTERing it for SELECT (the Bicycle) instead of USE. ItemUseEnd register_item_in_bag(Logger& logger, ItemUseActions& actions, std::string_view item); @@ -44,10 +53,15 @@ class ConsoleItemUseActions : public ItemUseActions{ virtual bool open_pocket(BagPocket pocket) override; virtual std::vector read_pocket() override; virtual void use_row(size_t row) override; + virtual bool use_row_on_lead(size_t row) override; virtual void register_row(size_t row) override; virtual void close_bag() override; private: + // The two halves of use_row_on_lead(). + bool wait_for_level_up_stats(); + bool back_to_bag_declining_moves(); + ConsoleHandle& m_console; ProControllerContext& m_context; Language m_language; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattlePolicy_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattlePolicy_Tests.cpp index 0ecca38e46..d89bfc2024 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattlePolicy_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattlePolicy_Tests.cpp @@ -464,6 +464,10 @@ UnitTestResult test_plan_tower_rival(){ return check_policy_step("10.03", "rival_tower", {.spa = 2, .outspeed = true, .guard_accuracy = true, .torrent_power = true}); } +UnitTestResult test_plan_erika(){ + return check_policy_step("14.04", "erika", {.spa = 1, .accuracy = 2, .outspeed = true, .guard_accuracy = true}); +} + struct AutoStoryBattlePolicyInput{ std::string name; @@ -562,6 +566,10 @@ const std::vector AUTO_STORY_BATTLE_POLICY_CASES = { .input = {.name = "plan: 12.02 koga names the policy, goals and foes", .body = test_plan_koga}, .expectation = {.description = "the policy, goals and 4 foe rows"}, }, + { + .input = {.name = "plan: 14.04 erika names the policy, goals and foes", .body = test_plan_erika}, + .expectation = {.description = "the policy, goals and 3 foe rows"}, + }, { .input = {.name = "plan: 10.03 rival_tower names the policy, goals and foes", .body = test_plan_tower_rival}, .expectation = {.description = "the policy, goals and 5 foe rows"}, diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCeladonGym_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCeladonGym_Tests.cpp new file mode 100644 index 0000000000..0f3c486bb0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCeladonGym_Tests.cpp @@ -0,0 +1,363 @@ +/* AutoStory Celadon Gym Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Rows 12.03, 14.03, 14.04 and 14.04b (StoryData/frlg-12.03.plan.json, 14.03, + * 14.04, 14.04b) + * on the overworld sim: from Fuchsia's nurse out of the Center, Fly to Pallet + * Town and Surf down Route 21 to Cinnabar; from Cinnabar's nurse out of the + * Center and Fly to Celadon, past the yard's tree, up + * the gym's west aisle into Beauty Tamia's line, round her by column 1, + * the inner tree, Beauty Lori, to below Erika; and from Erika (14.04b) back out + * the same way, past the tree, to the nurse. And the lead's attacks against the + * gym's three parties. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h" +#include "PokemonFRLG_AutoStoryEvents.h" +#include "PokemonFRLG_AutoStoryParty.h" +#include "PokemonFRLG_AutoStoryMember.h" +#include "PokemonFRLG_AutoStoryRooms.h" +#include "PokemonFRLG_AutoStoryRowSim.h" +#include "PokemonFRLG_AutoStoryRows_Tests.h" +#include "PokemonFRLG_AutoStoryTable.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +const std::string GYM = "CeladonCity_Gym"; +const std::string CELADON = "CeladonCity"; + +struct AutoStoryCeladonGymInput{ + std::string name; + std::function body; +}; + +struct AutoStoryCeladonGymExpectation{ + std::string_view description; +}; + +struct AutoStoryCeladonGymCase{ + AutoStoryCeladonGymInput input; + AutoStoryCeladonGymExpectation expectation; +}; + +class Test_AutoStoryCeladonGym : public UnitTest{ +public: + explicit Test_AutoStoryCeladonGym(AutoStoryCeladonGymCase test_case) + : UnitTest(std::format("PokemonFRLG::AutoStory - Simulation: {}", test_case.input.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + try{ + return m_case.input.body(); + }catch (Exception& e){ + return std::format("{}: {}", e.name(), e.message()); + } + } + +private: + AutoStoryCeladonGymCase m_case; +}; + +const StoryCheckpoint& checkpoint(std::string_view id){ + const std::vector& rows = story_checkpoints(); + const auto row = std::find_if(rows.begin(), rows.end(), [&](const StoryCheckpoint& data){ return data.id == id; }); + if (row == rows.end()){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("No checkpoint {}.", id)); + } + return *row; +} + +// A step of a row on the sim: row_step_on_sim(), but Erika's battle, +// which a talk starts, leaves the player where the table says. The +// Center's way in and the nurse, which the sim hasn't got, pass. +std::string gym_step_on_sim(const StoryStep& step, const std::string& id, StoryPose& at, OverworldSim& sim){ + if (step.kind == StoryStepKind::BATTLE && step.name == "erika"){ + at = *story_pose(Pokemon::pose_of(*step.pose_after)); + return ""; + } + return row_step_on_sim(sim, at, step, id); +} + +// Row `id`'s steps on `sim` from `at`: the failure, or empty. +std::string row_on_sim(std::string_view id, StoryPose& at, OverworldSim& sim){ + const StoryCheckpoint& row = checkpoint(id); + for (const StoryStep& step : row.steps){ + if (const std::string failure = gym_step_on_sim(step, row.id, at, sim); !failure.empty()){ + return std::format("{}: ended on {}; {}", id, to_string(sim.state().position), failure); + } + } + if (!sim.state().violations.empty()){ + return std::format("{}: a violation on the way: {}", id, sim.state().violations.back()); + } + return ""; +} + +std::string fought(const OverworldSim& sim){ + std::string names; + for (const std::string& name : sim.state().trainers_beaten){ + names += std::format(" {}", name); + } + return names.empty() ? " no one" : names; +} + +// 12.03 on a sim from Fuchsia's nurse: out of the Center, the Fly to +// Pallet Town, the shore, the Surf onto Route 21 and the way south over it +// to Cinnabar's Center door. No trainer is met and no rule is broken. +UnitTestResult test_fuchsia_to_cinnabar(){ + const StoryCheckpoint& row = checkpoint("12.03"); + StoryPose at = *story_pose(Pokemon::pose_of(*row.start)); + OverworldSim sim(story_maps(), MapPosition{at.map, at.tile}, *at.facing); + for (const StoryStep& step : row.steps){ + if (step.action == CENTER_ENTER_VARIANT){ + break; + } + if (const std::string failure = row_step_on_sim(sim, at, step, row.id); !failure.empty()){ + return std::format("12.03: ended on {}; {}", to_string(sim.state().position), failure); + } + } + const MapPosition below_center{"CinnabarIsland", {14, 12}}; + if (!(sim.state().position == below_center)){ + return std::format("12.03: ended on {}, not below Cinnabar's Center door {}.", to_string(sim.state().position), to_string(below_center)); + } + if (!sim.state().trainers_beaten.empty() || !sim.state().violations.empty()){ + return "12.03: met a trainer or broke a rule on the way."; + } + return true; +} + +// 14.03's first steps on a sim from Cinnabar's nurse: out of the Center and +// the Fly to Celadon, landing on its heal spot (48, 12) facing south, where +// the walk to the gym starts. No one is fought on the way. +UnitTestResult test_fly_to_celadon(){ + const StoryCheckpoint& row = checkpoint("14.03"); + StoryPose at = *story_pose(Pokemon::pose_of(*row.start)); + OverworldSim sim(story_maps(), MapPosition{at.map, at.tile}, *at.facing); + for (const StoryStep& step : row.steps){ + if (step.name == "above_gym_tree"){ + break; + } + if (const std::string failure = row_step_on_sim(sim, at, step, row.id); !failure.empty()){ + return std::format("14.03: ended on {}; {}", to_string(sim.state().position), failure); + } + } + const MapPosition landing{CELADON, {48, 12}}; + if (!(sim.state().position == landing) || sim.state().facing != Direction::SOUTH){ + return std::format("14.03: ended on {}, not Celadon's heal spot {} facing south.", to_string(sim.state().position), to_string(landing)); + } + if (!sim.state().trainers_beaten.empty()){ + return std::format("14.03: fought{}.", fought(sim)); + } + return true; +} + +// 14.03 on `sim` from its start, Cinnabar's nurse: the Fly to Celadon's landing (48, 12), the yard's tree +// cut, into the gym and up the west aisle; Tamia walks down to (2, 9) +// and is fought, the walk goes round her by column 1, the inner tree +// (3, 5) is cut, and Lori is fought from (5, 5). Ends below Erika, where +// 14.04 starts. The failure, or empty. +std::string to_erika(StoryPose& at, OverworldSim& sim){ + if (const std::string failure = row_on_sim("14.03", at, sim); !failure.empty()){ + return failure; + } + const MapPosition below_erika{GYM, {6, 5}}; + if (!(sim.state().position == below_erika) || sim.state().facing != Direction::NORTH){ + return std::format("14.03: ended on {}, not below Erika {} facing north.", to_string(sim.state().position), to_string(below_erika)); + } + if (sim.state().trainers_beaten != std::vector{"beauty", "beauty"}){ + return std::format("14.03: fought{}, not Tamia and Lori.", fought(sim)); + } + const StoryPose next = *story_pose(Pokemon::pose_of(*checkpoint("14.04").start)); + if (next.map != GYM || !(next.tile == below_erika.point)){ + return "14.04 doesn't start where 14.03 ends."; + } + return ""; +} + +// 14.03 on the sim (to_erika()), then 14.04, Erika's battle, which ends +// where it starts, then 14.04b on the same sim, the gym as 14.03 leaves it (Tamia on (2, 9), the inner tree cut): out along row +// 5, a step onto the cut tree's tile, down column 1 past Tamia and +// column 2 to the wall, out of the gym, the tree again and back below +// the Center's door (the Center's way in and the nurse need the +// console), meeting no one else. +UnitTestResult test_gym_rows(){ + StoryPose at = *story_pose(Pokemon::pose_of(*checkpoint("14.03").start)); + OverworldSim sim(story_maps(), MapPosition{at.map, at.tile}, *at.facing); + if (const std::string failure = to_erika(at, sim); !failure.empty()){ + return failure; + } + if (const std::string failure = row_on_sim("14.04", at, sim); !failure.empty()){ + return failure; + } + const MapPosition below_erika{GYM, {6, 5}}; + if (!(sim.state().position == below_erika)){ + return std::format("14.04: ended on {}, not in front of Erika where 14.04b starts.", to_string(sim.state().position)); + } + const StoryPose after_erika = *story_pose(Pokemon::pose_of(*checkpoint("14.04b").start)); + if (after_erika.map != GYM || !(after_erika.tile == below_erika.point)){ + return "14.04b doesn't start where 14.04 ends."; + } + if (const std::string failure = row_on_sim("14.04b", at, sim); !failure.empty()){ + return failure; + } + if (sim.state().trainers_beaten.size() != 2){ + return std::format("14.04b: fought{} after Tamia and Lori.", fought(sim)); + } + const MapPosition below_center{CELADON, {48, 12}}; + if (!(sim.state().position == below_center)){ + return std::format("14.04b: ended on {}, not below the Center's door {}.", to_string(sim.state().position), to_string(below_center)); + } + return true; +} + +// The lead as pokeguy brings it to Erika, without the Blizzard he +// teaches there (left out: no TM spent without the user): Surf, Water +// Pulse, Bite, Mega Kick. Against Tamia's, Lori's and Erika's Grass +// types it never picks a Water move, which they resist; against Lori's +// Exeggcute (Grass, Psychic) it picks Bite. +UnitTestResult test_lead_moves_against_grass(){ + PartyMember lead; + lead.species = "blastoise"; + lead.moves_read = true; + lead.level = 42; + lead.moves = {{"surf", 15}, {"water-pulse", 20}, {"bite", 25}, {"mega-kick", 5}}; + constexpr size_t SURF = 0; + constexpr size_t WATER_PULSE = 1; + constexpr size_t BITE = 2; + for (std::string_view foe : {"bellsprout", "exeggcute", "victreebel", "tangela", "vileplume"}){ + const std::optional slot = attack_slot_against(lead, {std::string(foe)}); + if (!slot || *slot == SURF || *slot == WATER_PULSE){ + return std::format("Against {}: picked a Water move or none.", foe); + } + } + if (attack_slot_against(lead, {"exeggcute"}) != BITE){ + return "Expected Bite against Lori's Exeggcute."; + } + return true; +} + +class QuietLogger : public Logger{ +public: + using Logger::log; + virtual void log(const std::string&, Color) override{} +}; + +// Battle checkpoint `id`'s precondition: a LEVEL step on the lead before +// its battle `battle`, at least `pokeguy`, pokeguy's level there (whose +// sims stand for the fight at that level or above). A lead at that level +// passes it; one a level below stops the checkpoint before the fight. +UnitTestResult check_lead_level_precondition(Logger& logger, std::string_view id, std::string_view battle, unsigned pokeguy){ + std::optional check; + for (const StoryStep& step : checkpoint(id).steps){ + if (step.kind == StoryStepKind::BATTLE && step.name == battle){ + break; + } + if (step.action == LEVEL_ACTION){ + check = level_check_of(step.args); + } + } + if (!check || check->slot != 0 || check->level != pokeguy){ + return std::format("{}: expected a LEVEL step on the lead, at least Lv{}, before {}.", id, pokeguy, battle); + } + for (const unsigned level : {pokeguy, pokeguy - 1}){ + const MemberReader lead = [level](size_t slot) -> std::optional{ + if (slot != 0){ + return std::nullopt; + } + return MemberInfo{.species = "blastoise", .level = level}; + }; + const LevelResult result = run_level_check(logger, lead, *check); + const LevelEnd expected = level >= pokeguy ? LevelEnd::REACHED : LevelEnd::BELOW; + if (result.end != expected || result.level != level){ + return std::format("{}: a Lv{} lead {} the Lv{} check.", id, level, level >= pokeguy ? "fails" : "passes", pokeguy); + } + } + return true; +} + +// Erika's rows are PokemonSimulation's planning_risk() for each foe leading +// against the entry state of its erika case (35/137): speed, the 80th +// percentile loss, the mean, the worst case, the worst normal hit, then +// whether it lowers accuracy and whether it explodes. +UnitTestResult test_erika_foe_threats_are_the_generators(){ + for (const StoryStep& step : checkpoint("14.04").steps){ + if (step.name != "erika"){ + continue; + } + const auto found = step.args.find("foe_threats"); + const auto* rows = found == step.args.end() ? nullptr : std::get_if>>(&found->second); + const std::vector> expected = { + {45, 0, 1, 60, 30, 0, 0}, // Victreebel + {33, 18, 8, 42, 20, 0, 0}, // Tangela + {34, 0, 2, 60, 30, 0, 0}, // Vileplume + }; + if (rows == nullptr || *rows != expected){ + return "14.04 erika: foe_threats are not the generator's rows for the erika case."; + } + return true; + } + return "14.04 has no erika battle step."; +} + +const AutoStoryCeladonGymCase AUTO_STORY_CELADON_GYM_CASES[] = { + { + .input = {.name = "12.03 Fuchsia to Cinnabar by Fly and Surf", .body = test_fuchsia_to_cinnabar}, + .expectation = {.description = "Pallet Town's shore, Route 21 surfed, below Cinnabar's Center door, nobody met"}, + }, + { + .input = {.name = "14.03 out of Cinnabar's Center and Fly to Celadon", .body = test_fly_to_celadon}, + .expectation = {.description = "lands on Celadon's heal spot facing south, where the walk to the gym starts"}, + }, + { + .input = {.name = "14.03 to below Erika, 14.04 on the spot and 14.04b back to the nurse", .body = test_gym_rows}, + .expectation = {.description = "Tamia and Lori fought where the table steps into their lines, no one else"}, + }, + { + .input = {.name = "the lead's attacks against Celadon Gym", .body = test_lead_moves_against_grass}, + .expectation = {.description = "no Water move against Grass; Bite against Exeggcute"}, + }, + { + .input = {.name = "14.04 stops before Erika unless the lead is at least pokeguy's Lv44", .body = []{ + QuietLogger logger; + return check_lead_level_precondition(logger, "14.04", "erika", 44); + }}, + .expectation = {.description = "a Lv44 lead passes the LEVEL step, a Lv43 one stops there"}, + }, + { + .input = {.name = "14.04 Erika's foe rows are the generator's", .body = test_erika_foe_threats_are_the_generators}, + .expectation = {.description = "PokemonSimulation planning_risk() rows"}, + }, +}; + +} // namespace + + +void add_tests_AutoStoryCeladonGym(UnitTestDatabase& database){ + for (const auto& item : AUTO_STORY_CELADON_GYM_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp index 3c8cde81f4..690559bd69 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp @@ -370,6 +370,7 @@ std::string save_card_place(const std::string& map){ {"PokemonTower", "pokemon-tower"}, {"Route16", "route-16"}, {"FuchsiaCity", "fuchsia-city"}, + {"CinnabarIsland", "cinnabar-island"}, }; for (const auto& [prefix, place] : PLACES){ if (map == prefix || map.rfind(prefix + "_", 0) == 0){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFaultSweep_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFaultSweep_Tests.cpp index 432b29894e..a01c2de5c2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFaultSweep_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFaultSweep_Tests.cpp @@ -128,6 +128,10 @@ const std::map KNOWN_FAILING{ "below_fuji_house plans from (4, 7), where Mr. Fuji's script warps the player (warp 4, 7, exact); from a tile off, no wall in his house funnels the walk onto (3, 4): legs that re-anchor, if the console shows the warp can land elsewhere"}, {"PokemonFRLG::AutoStory - Fault sweep: 11.01, warp no-op", "fly_celadon: a Fly that doesn't leave Lavender Town goes unseen, and east_of_snorlax walks from Lavender to Route 16 by Routes 8 and 7 instead: check the fly lands on its town's tile (FlyTown::landing) before the step ends"}, + {"PokemonFRLG::AutoStory - Fault sweep: 12.03, warp no-op", + "fly_pallet: a Fly that doesn't leave Fuchsia City goes unseen, and the row, which walks on from Fuchsia's streets to Pallet's shore, ends on Cinnabar's nurse tile: the same fix as 11.01's, the fly landing on its town's tile (FlyTown::landing) checked before the step ends"}, + {"PokemonFRLG::AutoStory - Fault sweep: 14.03, warp no-op", + "fly_celadon: a Fly that doesn't leave Cinnabar Island goes unseen, and the row, which walks on from Celadon's heal spot, ends below Erika: the same fix as 11.01's, the fly landing on its town's tile (FlyTown::landing) checked before the step ends"}, {"PokemonFRLG::AutoStory - Fault sweep: 08.03, off the Bicycle at a bike leg", "riding with no fault, route7_underground_exit's warp to UndergroundPath_EastEntrance is not confirmed on the sim (the screen after the fade reads as before the hold): the sim's warp off the Bicycle, before this class can sweep 08.03"}, }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp index c4dc392fab..bbcb4bf697 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp @@ -116,6 +116,7 @@ std::vector field_action_specs(){ {.kind = StoryStepKind::GAME, .name = USE_ITEM_VARIANT, .args = { required_arg("item", StoryArgType::WORD, "the item's slug, as read_item_list_name() names it"), optional_arg("safari", StoryArgType::BOOLEAN, false, "in the Safari Zone, whose START menu has RETIRE on top"), + optional_arg("count", StoryArgType::INTEGER, int64_t(1), "how often to use it, as many as the bag has (a Rare Candy on the lead each)"), }}, {.kind = StoryStepKind::GAME, .name = RIDE_BIKE_VARIANT, .args = {}}, {.kind = StoryStepKind::GAME, .name = REGISTER_BIKE_VARIANT, .args = {}}, @@ -213,7 +214,7 @@ std::optional run_field_game_step( ConsoleItemUseActions bag(console, context, language, step_start_menu(step.args)); const ItemUseEnd end = step.action == REGISTER_BIKE_VARIANT ? register_item_in_bag(console.logger(), bag, "bicycle") - : use_item_from_bag(console.logger(), bag, *parse_use_item_step(step)); + : use_items_from_bag(console.logger(), bag, *parse_use_item_step(step), use_item_count(step)).end; if (end != ItemUseEnd::USED){ return Pokemon::outcome_failed(std::string("use_item_") + item_use_end_name(end), step.action); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.cpp index 01928a6573..4d2d5250dc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.cpp @@ -5,7 +5,12 @@ */ #include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_BattleDialogs.h" +#include "VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_PartyDialogs.h" +#include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_BagDetector.h" +#include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_PartyMenuDetector.h" #include "VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h" #include "PokemonFRLG_AutoStoryActions.h" #include "PokemonFRLG_AutoStoryEvents.h" @@ -22,6 +27,9 @@ namespace PokemonFRLG{ using namespace std::chrono_literals; +// The most a use_item step's count may be. +constexpr size_t MAX_USE_COUNT = 99; + MoveSpeed move_speed(const StoryStep& step){ if (step.action == BIKE_VARIANT){ return MoveSpeed::BIKE; @@ -33,11 +41,17 @@ std::optional usable_item_pocket(std::string_view item){ if (is_repel(item) || item == "escape-rope"){ return BagPocket::ITEMS; } + if (is_party_item(item)){ + return BagPocket::ITEMS; + } if (item == "bicycle"){ return BagPocket::KEY_ITEMS; } return std::nullopt; } +bool is_party_item(std::string_view item){ + return item == "rare-candy"; +} bool is_repel(std::string_view item){ return item == "repel" || item == "super-repel" || item == "max-repel"; } @@ -47,8 +61,14 @@ std::optional parse_use_item_step(const Pokemon::StoryStep& step){ if (item == nullptr || step.args.contains("slot") || !usable_item_pocket(*item)){ return std::nullopt; } + if (step.args.contains("count") && !number_arg(step.args, "count", 1, MAX_USE_COUNT)){ + return std::nullopt; + } return *item; } +size_t use_item_count(const Pokemon::StoryStep& step){ + return number_arg(step.args, "count", 1, MAX_USE_COUNT).value_or(1); +} bool is_item_use_step(const Pokemon::StoryStep& step){ const bool bike = step.action == RIDE_BIKE_VARIANT || step.action == REGISTER_BIKE_VARIANT; return (bike && step.args.empty()) || parse_use_item_step(step).has_value(); @@ -73,6 +93,8 @@ std::string item_use_end_name(ItemUseEnd end){ return "no_bag"; case ItemUseEnd::UNKNOWN_ITEM: return "unknown_item"; + case ItemUseEnd::NOT_USED: + return "not_used"; } return "?"; } @@ -101,6 +123,12 @@ ItemUseEnd with_bag_row( logger.log("Bag: " + std::string(registers ? "registering" : "using") + " the " + std::string(item) + " on row " + std::to_string(*row + 1) + "."); if (registers){ actions.register_row(*row); + }else if (is_party_item(item)){ + if (!actions.use_row_on_lead(*row)){ + logger.log("Bag: the " + std::string(item) + " showed no level-up."); + actions.close_bag(); + return ItemUseEnd::NOT_USED; + } }else{ actions.use_row(*row); } @@ -113,6 +141,21 @@ ItemUseEnd with_bag_row( ItemUseEnd use_item_from_bag(Logger& logger, ItemUseActions& actions, std::string_view item){ return with_bag_row(logger, actions, item, false); } +ItemUses use_items_from_bag(Logger& logger, ItemUseActions& actions, std::string_view item, size_t count){ + size_t used = 0; + while (used < count){ + const ItemUseEnd end = use_item_from_bag(logger, actions, item); + if (end == ItemUseEnd::NONE_LEFT && used != 0){ + logger.log("Bag: " + std::to_string(used) + " of " + std::to_string(count) + " " + std::string(item) + " used: no more in the bag."); + break; + } + if (end != ItemUseEnd::USED){ + return {end, used}; + } + used++; + } + return {ItemUseEnd::USED, used}; +} ItemUseEnd register_item_in_bag(Logger& logger, ItemUseActions& actions, std::string_view item){ return with_bag_row(logger, actions, item, true); } @@ -139,6 +182,10 @@ constexpr Milliseconds POCKET_SWITCH_WAIT = 600ms; // A cursor step in a pocket, press and wait: 250 ms in all. constexpr Milliseconds CURSOR_TAP = 100ms; constexpr Milliseconds CURSOR_WAIT = 150ms; +// A presses to the stats window, 800 ms each, and the tries (15 B +// presses each) to get from it back to the bag. +constexpr int LEVEL_UP_PRESSES = 30; +constexpr int BACK_TO_BAG_ATTEMPTS = 8; // Out of the bag and its messages, or the Escape Rope's warp. constexpr Milliseconds BAG_CLOSE_TIMEOUT = 30000ms; @@ -185,6 +232,76 @@ void ConsoleItemUseActions::use_row(size_t row){ pbf_press_button(m_context, BUTTON_A, 160ms, 1500ms); } +// Past the party list to the stats window of a Rare Candy. +bool ConsoleItemUseActions::wait_for_level_up_stats(){ + // " grew to Lv. N!", then the stats window (the RNG loop's + // waits, console-proven there). + PartyLevelUpWatcher level_up(COLOR_RED, PartyLevelUpDialog::stats, m_language); + m_context.wait_for_all_requests(); + const int stats = run_until( + m_console, m_context, + [](ProControllerContext& context){ + for (int c = 0; c < LEVEL_UP_PRESSES; c++){ + pbf_press_button(context, BUTTON_A, 200ms, 800ms); + } + }, + {level_up} + ); + if (stats < 0){ + m_console.log("Bag: no level-up stats window after the Rare Candy.", COLOR_RED); + } + return stats >= 0; +} + +// Out of the stats window and the party list back to the bag. A new move +// is declined: B on "Delete a move?" asks "Stop learning?", A answers YES. +// An evolution is stopped with B. +bool ConsoleItemUseActions::back_to_bag_declining_moves(){ + for (int attempt = 0; attempt < BACK_TO_BAG_ATTEMPTS; attempt++){ + BagWatcher bag(COLOR_RED); + PartyMoveLearnWatcher move_learn(COLOR_RED); + BattleDialogWatcher evolution(COLOR_RED); + m_context.wait_for_all_requests(); + const int seen = run_until( + m_console, m_context, + [](ProControllerContext& context){ + pbf_wait(context, 1000ms); + for (int c = 0; c < 15; c++){ + pbf_press_button(context, BUTTON_B, 200ms, 1800ms); + } + }, + {bag, move_learn, evolution} + ); + if (seen == 0){ + return true; + } + if (seen == 1){ + m_console.log("Bag: a new move to learn: declined."); + pbf_press_button(m_context, BUTTON_B, 200ms, 1800ms); + pbf_press_button(m_context, BUTTON_A, 200ms, 2800ms); + }else if (seen == 2){ + m_console.log("Bag: an evolution: stopped."); + pbf_press_button(m_context, BUTTON_B, 200ms, 2800ms); + }else{ + break; + } + } + m_console.log("Bag: never back at the bag after the Rare Candy.", COLOR_RED); + return false; +} + +bool ConsoleItemUseActions::use_row_on_lead(size_t row){ + for (size_t c = 0; c < row; c++){ + pbf_press_dpad(m_context, DPAD_DOWN, CURSOR_TAP, CURSOR_WAIT); + } + // The item's menu, USE, then the party list, whose cursor is on the + // lead, as for a Potion in battle. + pbf_press_button(m_context, BUTTON_A, 160ms, 800ms); + pbf_press_button(m_context, BUTTON_A, 160ms, 1500ms); + pbf_press_button(m_context, BUTTON_A, 160ms, 1000ms); + return wait_for_level_up_stats() && back_to_bag_declining_moves(); +} + void ConsoleItemUseActions::register_row(size_t row){ for (size_t c = 0; c < row; c++){ pbf_press_dpad(m_context, DPAD_DOWN, CURSOR_TAP, CURSOR_WAIT); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.h index 8315a6e5d0..0cfb819c55 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.h @@ -33,8 +33,8 @@ namespace PokemonFRLG{ // The GAME step that uses an item from the bag on the overworld, its -// args the item's slug as read_item_list_name() names it, and for an item -// used on a Pokemon the party slot (none the story uses yet). +// args the item's slug as read_item_list_name() names it and, for an item +// used up more than once (the Rare Candy), its count. inline const std::string USE_ITEM_VARIANT = "use_item"; // The GAME step that gets on the registered Bicycle with SELECT // (OverworldWalkSteps::get_on_bike()). The ROUTE steps after it ride @@ -53,8 +53,12 @@ inline const std::string REGISTER_BIKE_VARIANT = "register_bike"; // The item a use_item step uses (with "safari", from the Safari Zone's // START menu, step_start_menu()), or nothing if it isn't one, the item -// isn't one the story uses from the bag, or it names a slot. +// isn't one the story uses from the bag, or it names a slot or a count +// that is out of range. An item for a Pokemon (is_party_item()) goes on +// the lead. std::optional parse_use_item_step(const Pokemon::StoryStep& step); +// How often the step uses its item ("count", 1 to 99; 1 if left out). +size_t use_item_count(const Pokemon::StoryStep& step); // Whether `step` is one of this file's steps. bool is_item_use_step(const Pokemon::StoryStep& step); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse_Tests.cpp index 111969c447..5fce0c1118 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse_Tests.cpp @@ -38,7 +38,10 @@ class SimBag : public ItemUseActions{ public: std::map> pockets; bool opens = true; + // The lead's level-up text never shows. + bool no_level_up = false; std::vector used; + std::vector used_on_lead; std::vector registered; bool open = false; @@ -57,6 +60,14 @@ class SimBag : public ItemUseActions{ item.count--; } } + virtual bool use_row_on_lead(size_t row) override{ + if (no_level_up){ + return false; + } + use_row(row); + used_on_lead.push_back(pockets[m_pocket][row].item); + return true; + } virtual void register_row(size_t row) override{ registered.push_back(pockets[m_pocket][row].item); } @@ -76,6 +87,9 @@ struct ItemUseInput{ bool opens = true; // renew_repel() instead of use_item_from_bag(). bool renew = false; + // The step's count (use_items_from_bag()). + size_t count = 1; + bool no_level_up = false; }; struct ItemUseExpectation{ ItemUseEnd end; @@ -97,10 +111,14 @@ class Test_AutoStoryItemUse : public UnitTest{ bag.pockets[BagPocket::ITEMS] = m_case.input.items; bag.pockets[BagPocket::KEY_ITEMS] = m_case.input.key_items; bag.opens = m_case.input.opens; + bag.no_level_up = m_case.input.no_level_up; const std::string variant = m_case.input.variant; const std::string item = variant == RIDE_BIKE_VARIANT ? "bicycle" : parse_use_item_step(game_step_of(variant, ITEM)).value_or(variant); - const ItemUseEnd end = m_case.input.renew ? renew_repel(logger, bag, item) : use_item_from_bag(logger, bag, item); - if (end != m_case.expectation.end || bag.used != m_case.expectation.used || bag.open){ + const ItemUseEnd end = m_case.input.renew + ? renew_repel(logger, bag, item) + : use_items_from_bag(logger, bag, item, m_case.input.count).end; + const bool lead_ok = is_party_item(item) ? bag.used_on_lead == bag.used : bag.used_on_lead.empty(); + if (end != m_case.expectation.end || bag.used != m_case.expectation.used || bag.open || !lead_ok){ return std::format( "\"{}\": {} using {} item(s), bag {}; expected {} using {}.", variant, item_use_end_name(end), bag.used.size(), bag.open ? "open" : "closed", @@ -175,6 +193,43 @@ const ItemUseCase ITEM_USE_CASES[] = { }, .expectation = {.end = ItemUseEnd::NONE_LEFT, .used = {}}, }, + { + .input = { + .name = "two Rare Candies on the lead, the bag holding five", + .variant = "use_item rare-candy", + .items = {{"potion", 3}, {"rare-candy", 5}}, + .count = 2, + }, + .expectation = {.end = ItemUseEnd::USED, .used = {"rare-candy", "rare-candy"}}, + }, + { + .input = { + .name = "two Rare Candies asked, one in the bag: the one is used", + .variant = "use_item rare-candy", + .items = {{"rare-candy", 1}}, + .count = 2, + }, + .expectation = {.end = ItemUseEnd::USED, .used = {"rare-candy"}}, + }, + { + .input = { + .name = "no Rare Candy in the bag", + .variant = "use_item rare-candy", + .items = {{"potion", 3}}, + .count = 2, + }, + .expectation = {.end = ItemUseEnd::NONE_LEFT, .used = {}}, + }, + { + .input = { + .name = "a Rare Candy whose level-up never shows", + .variant = "use_item rare-candy", + .items = {{"rare-candy", 2}}, + .count = 2, + .no_level_up = true, + }, + .expectation = {.end = ItemUseEnd::NOT_USED, .used = {}}, + }, { .input = { .name = "an Escape Rope is no Repel to renew", @@ -186,10 +241,22 @@ const ItemUseCase ITEM_USE_CASES[] = { }, }; +// "use_item rare-candy" with its count, an integer as the plans give it. +StoryStep candy_step(int64_t count){ + StoryStep step = game_step_of("use_item rare-candy", ITEM); + step.args["count"] = count; + return step; +} + UnitTestResult test_variants_and_bike(){ if (parse_use_item_step(game_step_of("use_item potion", ITEM)) || parse_use_item_step(game_step_of("use_item", ITEM)) || parse_use_item_step(game_step_of("use_item repel slot=1", ITEM)) || !parse_use_item_step(game_step_of("use_item super-repel", ITEM)) + || parse_use_item_step(game_step_of("use_item rare-candy slot=1", ITEM)) + || parse_use_item_step(candy_step(0)) + || !parse_use_item_step(candy_step(2)) + || use_item_count(candy_step(2)) != 2 + || use_item_count(game_step_of("use_item rare-candy", ITEM)) != 1 ){ return "Expected only the story's bag items as use_item steps."; } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden12.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden12.h index b24554946b..626b3c2668 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden12.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden12.h @@ -17,7 +17,7 @@ namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonFRLG{ -// The stored plan lines from segment 12 on, after FRLG_PLAN_GOLDEN_11's. +// The stored plan lines of segment 12 (12.01 to 12.03, Fuchsia to Cinnabar), after FRLG_PLAN_GOLDEN_11's. inline constexpr std::string_view FRLG_PLAN_GOLDEN_12[] = { "12.01 face_center_door from FuchsiaCity (25, 32) facing south to FuchsiaCity (25, 32) facing north", "12.01 face north -> FuchsiaCity (25, 32)", @@ -87,6 +87,68 @@ inline constexpr std::string_view FRLG_PLAN_GOLDEN_12[] = { "12.02b walk north 5 (to wall) -> FuchsiaCity (11, 32)", "12.02b walk east 14 -> FuchsiaCity (25, 32)", "12.02b face north -> FuchsiaCity (25, 32)", + "12.03 outside_fuchsia_center from FuchsiaCity_PokemonCenter_1F (0, 2) facing north to FuchsiaCity (25, 32) facing south", + "12.03 walk south 3 (to wall) -> FuchsiaCity_PokemonCenter_1F (0, 5) [walk only]", + "12.03 walk east 7 -> FuchsiaCity_PokemonCenter_1F (7, 5) [walk only]", + "12.03 walk south 4 (warp) -> FuchsiaCity (25, 32) [walk only]", + "12.03 pallet_shore from PalletTown (6, 8) facing south to PalletTown (8, 16) facing south", + "12.03 walk south 2 (to wall) -> PalletTown (6, 10)", + "12.03 walk east 4 -> PalletTown (10, 10)", + "12.03 walk south 6 (to wall) -> PalletTown (10, 16)", + "12.03 walk west 2 -> PalletTown (8, 16)", + "12.03 face south -> PalletTown (8, 16)", + "12.03 surf_route_21 from PalletTown (8, 17) facing south to CinnabarIsland (14, 12) facing north", + "12.03 walk south 29 -> Route21_North (8, 26) [surfing] [wild] [connection 3]", + "12.03 walk south 1 -> Route21_North (8, 27) [surfing] [wild]", + "12.03 surf north -> Route21_North (8, 26) [wild]", + "12.03 walk east 5 -> Route21_North (13, 26) [surfing] [wild]", + "12.03 walk east 1 -> Route21_North (14, 26) [surfing] [wild]", + "12.03 surf west -> Route21_North (13, 26) [wild]", + "12.03 walk south 68 -> Route21_South (13, 44) [surfing] [wild] [connection 24]", + "12.03 walk south 1 -> Route21_South (13, 45) [surfing] [wild]", + "12.03 walk south 10 -> CinnabarIsland (13, 5) [connection 5]", + "12.03 walk east 8 (to wall) -> CinnabarIsland (21, 5)", + "12.03 walk south 7 -> CinnabarIsland (21, 12)", + "12.03 walk west 7 -> CinnabarIsland (14, 12)", + "12.03 face north -> CinnabarIsland (14, 12)", +}; + +// The stored plan lines of Erika's rows, 14.03 to 14.04b, after the rows of Blaine's +// (14.01 to 14.02b) in the story's order. +inline constexpr std::string_view FRLG_PLAN_GOLDEN_12_CELADON[] = { + "14.03 cinnabar_outside from CinnabarIsland_PokemonCenter_1F (7, 4) facing north to CinnabarIsland (14, 12) facing south", + "14.03 walk south 5 (warp) -> CinnabarIsland (14, 12) [walk only]", + "14.03 above_gym_tree from CeladonCity (48, 12) facing south to CeladonCity (40, 34) facing south", + "14.03 walk west 7 -> CeladonCity (41, 12)", + "14.03 walk south 13 (to wall) -> CeladonCity (41, 25)", + "14.03 walk west 10 -> CeladonCity (31, 25)", + "14.03 walk south 8 (to wall) -> CeladonCity (31, 33)", + "14.03 walk east 9 -> CeladonCity (40, 33)", + "14.03 walk south 1 -> CeladonCity (40, 34)", + "14.03 below_tamia_line from CeladonCity (40, 35) facing south to CeladonCity_Gym (2, 11) facing north", + "14.03 walk south 2 (to wall) -> CeladonCity (40, 37)", + "14.03 walk west 29 -> CeladonCity (11, 37)", + "14.03 walk north 7 (warp) -> CeladonCity_Gym (6, 18)", + "14.03 walk west 4 -> CeladonCity_Gym (2, 18) [walk only]", + "14.03 walk north 7 -> CeladonCity_Gym (2, 11) [walk only]", + "14.03 below_erika from CeladonCity_Gym (6, 5) facing east to CeladonCity_Gym (6, 5) facing north", + "14.03 face north -> CeladonCity_Gym (6, 5) [walk only]", + "14.04b below_gym_tree_out from CeladonCity_Gym (2, 18) facing south to CeladonCity (40, 36) facing north", + "14.04b walk east 4 -> CeladonCity_Gym (6, 18) [walk only] [tapped]", + "14.04b walk south 1 (warp) -> CeladonCity (11, 31) [walk only]", + "14.04b walk south 6 (to wall) -> CeladonCity (11, 37)", + "14.04b walk east 30 (to wall) -> CeladonCity (41, 37)", + "14.04b walk north 1 (to wall) -> CeladonCity (41, 36)", + "14.04b walk west 1 -> CeladonCity (40, 36)", + "14.04b face north -> CeladonCity (40, 36)", + "14.04b celadon_below_center from CeladonCity (40, 35) facing north to CeladonCity (48, 12) facing north", + "14.04b walk north 3 -> CeladonCity (40, 32)", + "14.04b walk west 9 -> CeladonCity (31, 32)", + "14.04b walk north 8 (to wall) -> CeladonCity (31, 24)", + "14.04b walk east 12 -> CeladonCity (43, 24)", + "14.04b walk north 12 (to wall) -> CeladonCity (43, 12)", + "14.04b walk east 5 -> CeladonCity (48, 12)", + "14.04b face north -> CeladonCity (48, 12)", }; } // namespace PokemonFRLG diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRows_Tests.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRows_Tests.h index 2f521f46bc..7ba79bbc23 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRows_Tests.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRows_Tests.h @@ -10,6 +10,7 @@ #ifndef PokemonAutomation_PokemonFRLG_AutoStoryRows_Tests_H #define PokemonAutomation_PokemonFRLG_AutoStoryRows_Tests_H +#include #include "Common/Cpp/TestRunners/UnitTestDatabase.h" namespace PokemonAutomation{ @@ -34,6 +35,8 @@ void add_tests_AutoStorySnorlax(UnitTestDatabase& database); void add_tests_AutoStorySlope(UnitTestDatabase& database); // Row 11.03 on the sims (PokemonFRLG_AutoStorySafari_Tests.cpp). void add_tests_AutoStorySafari(UnitTestDatabase& database); +// Rows 14.03, 14.04 and 14.04b, Celadon Gym, on the overworld sim (PokemonFRLG_AutoStoryCeladonGym_Tests.cpp). +void add_tests_AutoStoryCeladonGym(UnitTestDatabase& database); } // namespace PokemonFRLG diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRunPlan_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRunPlan_Tests.cpp index df76ea1bfc..9775ae4c39 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRunPlan_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRunPlan_Tests.cpp @@ -145,7 +145,7 @@ class Test_RunPlan : public UnitTest{ const RunPlanScenario m_scenario; }; -const std::string TODAY = "01.01 01.03 01.04 01.05 02.01 02.02 02.03 03.01 03.02 03.03 04.01 04.02 04.04 04.05 04.06 05.01 05.02 05.03 05.04 05.05 05.06 05.07 05.08 05.09 05.10 06.01 06.02 06.03 06.04 07.01 07.02 08.01 08.02 08.03 09.01 09.02 09.03 10.01 10.02 10.03 10.04 10.05 10.06 11.01 11.02 11.03 12.01 12.02 12.02b"; +const std::string TODAY = "01.01 01.03 01.04 01.05 02.01 02.02 02.03 03.01 03.02 03.03 04.01 04.02 04.04 04.05 04.06 05.01 05.02 05.03 05.04 05.05 05.06 05.07 05.08 05.09 05.10 06.01 06.02 06.03 06.04 07.01 07.02 08.01 08.02 08.03 09.01 09.02 09.03 10.01 10.02 10.03 10.04 10.05 10.06 11.01 11.02 11.03 12.01 12.02 12.02b 12.03 14.03 14.04 14.04b"; const RunPlanScenario RUN_PLAN_CASES[] = { { @@ -236,11 +236,11 @@ const RunPlanScenario RUN_PLAN_CASES[] = { }, { .input = {.name = "Gift RNG off: 04.03 is passed over", .first = "04.01"}, - .expectation = {.expected_ids = "04.01 04.02 04.04 04.05 04.06 05.01 05.02 05.03 05.04 05.05 05.06 05.07 05.08 05.09 05.10 06.01 06.02 06.03 06.04 07.01 07.02 08.01 08.02 08.03 09.01 09.02 09.03 10.01 10.02 10.03 10.04 10.05 10.06 11.01 11.02 11.03 12.01 12.02 12.02b"}, + .expectation = {.expected_ids = "04.01 04.02 04.04 04.05 04.06 05.01 05.02 05.03 05.04 05.05 05.06 05.07 05.08 05.09 05.10 06.01 06.02 06.03 06.04 07.01 07.02 08.01 08.02 08.03 09.01 09.02 09.03 10.01 10.02 10.03 10.04 10.05 10.06 11.01 11.02 11.03 12.01 12.02 12.02b 12.03 14.03 14.04 14.04b"}, }, { .input = {.name = "Gift RNG off: 04.03 runs when started at", .first = "04.03"}, - .expectation = {.expected_ids = "04.03 04.04 04.05 04.06 05.01 05.02 05.03 05.04 05.05 05.06 05.07 05.08 05.09 05.10 06.01 06.02 06.03 06.04 07.01 07.02 08.01 08.02 08.03 09.01 09.02 09.03 10.01 10.02 10.03 10.04 10.05 10.06 11.01 11.02 11.03 12.01 12.02 12.02b"}, + .expectation = {.expected_ids = "04.03 04.04 04.05 04.06 05.01 05.02 05.03 05.04 05.05 05.06 05.07 05.08 05.09 05.10 06.01 06.02 06.03 06.04 07.01 07.02 08.01 08.02 08.03 09.01 09.02 09.03 10.01 10.02 10.03 10.04 10.05 10.06 11.01 11.02 11.03 12.01 12.02 12.02b 12.03 14.03 14.04 14.04b"}, }, { .input = { .name = "Gift RNG: started at 04.03, from the save beside the salesman", @@ -279,7 +279,7 @@ const RunPlanScenario RUN_PLAN_CASES[] = { }, { .input = {.name = "Starter RNG alone: 04.03 runs when started at", .first = "04.03", .handoffs = {.starter_rng = true}}, - .expectation = {.expected_ids = "04.03 04.04 04.05 04.06 05.01 05.02 05.03 05.04 05.05 05.06 05.07 05.08 05.09 05.10 06.01 06.02 06.03 06.04 07.01 07.02 08.01 08.02 08.03 09.01 09.02 09.03 10.01 10.02 10.03 10.04 10.05 10.06 11.01 11.02 11.03 12.01 12.02 12.02b"}, + .expectation = {.expected_ids = "04.03 04.04 04.05 04.06 05.01 05.02 05.03 05.04 05.05 05.06 05.07 05.08 05.09 05.10 06.01 06.02 06.03 06.04 07.01 07.02 08.01 08.02 08.03 09.01 09.02 09.03 10.01 10.02 10.03 10.04 10.05 10.06 11.01 11.02 11.03 12.01 12.02 12.02b 12.03 14.03 14.04 14.04b"}, }, { .input = { .name = "Starter RNG first, with both on", diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySimulation_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySimulation_Tests.cpp index 990dfd737c..8c655a9757 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySimulation_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySimulation_Tests.cpp @@ -37,6 +37,7 @@ #include "PokemonFRLG_AutoStoryPaceSim.h" #include "PokemonFRLG_AutoStoryRngLoop.h" #include "PokemonFRLG_AutoStoryRowSim.h" +#include "PokemonFRLG_AutoStoryRows_Tests.h" #include "PokemonFRLG_AutoStoryStepTable.h" #include "PokemonFRLG_AutoStoryTable.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.cpp index f2a78d75e8..44ecee7cb2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.cpp @@ -6,6 +6,8 @@ #include #include +#include +#include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/Json/JsonArray.h" #include "Common/Cpp/Json/JsonObject.h" @@ -220,13 +222,48 @@ StoryCheckpoint read_story_checkpoint_file(const JsonValue& value, std::string_v return checkpoint; } +namespace{ + +// The checkpoints' ids in the order the story plays them. Ids are labels, +// not positions: a gym's rows may be numbered before rows that come first +// in the story (the table is not sorted by id). Every file in StoryData/ +// is listed once; a new checkpoint is added at its place in the story. +constexpr std::string_view STORY_ORDER[] = { + "01.01", "01.02", "01.03", "01.04", "01.05", "01.06", "02.01", "02.02", "02.03", "03.01", + "03.02", "03.03", "04.01", "04.02", "04.03", "04.04", "04.05", "04.06", "05.01", "05.02", + "05.03", "05.04", "05.05", "05.06", "05.07", "05.08", "05.09", "05.10", "06.01", "06.02", + "06.03", "06.04", "07.01", "07.02", "08.01", "08.02", "08.03", "09.01", "09.02", "09.03", + "09.04", "10.01", "10.02", "10.03", "10.04", "10.05", "10.06", "11.01", "11.02", "11.03", + "12.01", "12.02", "12.02b", "12.03", "14.03", "14.04", "14.04b", +}; + +// `checkpoints` in STORY_ORDER's order. Throws ParseException for a +// checkpoint that is not listed there, or listed twice. +std::vector in_story_order(std::vector checkpoints){ + std::vector ordered; + for (std::string_view id : STORY_ORDER){ + for (StoryCheckpoint& checkpoint : checkpoints){ + if (checkpoint.id == id){ + ordered.push_back(std::move(checkpoint)); + checkpoint.id = ""; + } + } + } + if (ordered.size() != checkpoints.size()){ + throw ParseException("Every checkpoint file needs its id once in STORY_ORDER (PokemonFRLG_AutoStoryTable.cpp)."); + } + return ordered; +} + +} // namespace + const std::vector& story_checkpoints(){ static const std::vector checkpoints = []{ std::vector result; for (const auto& [name, text] : autostory_checkpoint_json()){ result.push_back(read_story_checkpoint_file(parse_json(text), name)); } - return result; + return in_story_order(std::move(result)); }(); return checkpoints; } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.h index 3c96de3850..83f92808b3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.h @@ -65,7 +65,8 @@ const std::vector>& autostory_checkpoint_jso // action args it doesn't take, or has notes for a step it doesn't have. StoryCheckpoint read_story_checkpoint_file(const JsonValue& value, std::string_view source); -// Checkpoints 01.01 to 07.02, in order: every file in StoryData/. With +// Every file in StoryData/ in the order the story plays them (STORY_ORDER in +// PokemonFRLG_AutoStoryTable.cpp), not in id order. Checkpoints 01.01 to 07.02 with // 01.02, the way back in after SID Helper, after 01.01, 01.05 split in two at Squirtle's ball for // Starter RNG: 01.05 and 01.06, 04.03, the way back in after Gift RNG, // after 04.02, 04.04, the Magikarp put in BOX 1, after it, 05.01 and diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp index ef86fed9c5..bef60a8766 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp @@ -284,8 +284,8 @@ UnitTestResult check_checkpoint(const StoryCheckpoint& checkpoint, StoryPose& po // before leaves the player, and adds the plans to `golden`. UnitTestResult chain_story_routes(std::vector& golden){ const std::vector& checkpoints = story_checkpoints(); - if (checkpoints.size() != 53 || checkpoints[0].start){ - return "Expected 53 checkpoints, the first from a new game."; + if (checkpoints.size() != 57 || checkpoints[0].start){ + return "Expected 57 checkpoints, the first from a new game."; } StoryPose pose; for (size_t c = 0; c < checkpoints.size(); c++){ @@ -327,6 +327,7 @@ UnitTestResult test_golden_plans(){ golden.insert(golden.end(), std::begin(FRLG_PLAN_GOLDEN_10), std::end(FRLG_PLAN_GOLDEN_10)); golden.insert(golden.end(), std::begin(FRLG_PLAN_GOLDEN_11), std::end(FRLG_PLAN_GOLDEN_11)); golden.insert(golden.end(), std::begin(FRLG_PLAN_GOLDEN_12), std::end(FRLG_PLAN_GOLDEN_12)); + golden.insert(golden.end(), std::begin(FRLG_PLAN_GOLDEN_12_CELADON), std::end(FRLG_PLAN_GOLDEN_12_CELADON)); const size_t stored = golden.size(); size_t first_diff = 0; while (first_diff < actual.size() && first_diff < stored && actual[first_diff] == golden[first_diff]){ @@ -703,7 +704,7 @@ UnitTestResult check_lines(const StoryCheckpoint& checkpoint, std::set