From f3c8afa2c1bf52f3160ec36b55ea21f8bf07f5b6 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 22:32:03 -0800 Subject: [PATCH 1/5] FRLG 19.01a: sell the Nugget for Super Potions at Lavender; use_repel on Route 23 and Victory Road The Mart step sells before it buys ("sell" lines, sale_price()), reads the money box on the BUY list, fails when it shows less than the sales earned, and cuts an if_affordable line to what the money covers; unread, it buys blind as before. 19.01a flies to Lavender's Mart (Celadon sells no Hyper Potion and its 2F has no map), sells the Nugget, buys 6 Super Potions, and flies back to Viridian for the Antidote and 2 Parlyz Heals. The new use_repel step uses the strongest Repel in the bag and renews strongest first when it wears off; 19.03 and 19.04 use it. --- .../Menus/PokemonFRLG_MoneyBoxReader.cpp | 45 ++++++ .../Menus/PokemonFRLG_MoneyBoxReader.h | 32 +++++ .../PokemonFRLG_AutoStoryAbilities_Tests.cpp | 2 + .../AutoStory/PokemonFRLG_AutoStoryBagUse.h | 4 + .../AutoStory/PokemonFRLG_AutoStoryEvents.cpp | 2 +- .../PokemonFRLG_AutoStoryHideout.cpp | 8 +- .../PokemonFRLG_AutoStoryItemUse.cpp | 5 +- .../AutoStory/PokemonFRLG_AutoStoryItemUse.h | 8 ++ .../PokemonFRLG_AutoStoryItemUse_Tests.cpp | 43 +++++- .../PokemonFRLG_AutoStoryItems_Tests.cpp | 2 + .../AutoStory/PokemonFRLG_AutoStoryMart.cpp | 91 +++++++++++- .../AutoStory/PokemonFRLG_AutoStoryMart.h | 38 +++-- .../AutoStory/PokemonFRLG_AutoStoryMartBuys.h | 14 ++ .../AutoStory/PokemonFRLG_AutoStoryMartPlan.h | 22 ++- .../PokemonFRLG_AutoStoryMartSell.cpp | 135 ++++++++++++++++++ .../PokemonFRLG_AutoStoryMart_Tests.cpp | 99 ++++++++++++- .../PokemonFRLG_AutoStoryStepTable.cpp | 3 +- ...PokemonFRLG_AutoStoryVictoryRoad_Tests.cpp | 22 ++- .../AutoStory/PokemonFRLG_AutoStory_Tests.cpp | 2 +- .../AutoStory/StoryData/frlg-19.01a.plan.json | 66 +++++++-- .../AutoStory/StoryData/frlg-19.03.plan.json | 7 + .../AutoStory/StoryData/frlg-19.04.plan.json | 7 + SerialPrograms/cmake/SourceFiles.cmake | 3 + 23 files changed, 616 insertions(+), 44 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_MoneyBoxReader.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_MoneyBoxReader.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartSell.cpp diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_MoneyBoxReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_MoneyBoxReader.cpp new file mode 100644 index 0000000000..c5f9d4734b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_MoneyBoxReader.cpp @@ -0,0 +1,45 @@ +/* Money Box Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/OCR/OCR_NumberReader.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_MoneyBoxReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +// The amount's digits in the game's 240 x 160 screen, from right of the +// POKEDOLLAR sign to the box's right edge: x 44.5 to 75, y 22 to 33 +// (measured on a Fuchsia Mart BUY list capture, "MONEY / P 10586": +// digits at x 47..71, y 24..31). +const ImageFloatBox MONEY_DIGITS(44.5 / 240, 22.0 / 160, 30.5 / 240, 11.0 / 160); +// The most the game holds (money.c MAX_MONEY). +constexpr int MAX_MONEY = 999999; + +} // namespace + + +std::optional read_money_box(Logger& logger, const ImageViewRGB32& screen){ + if (screen.width() == 0 || screen.height() == 0){ + return std::nullopt; + } + const ImageViewRGB32 game = extract_box_reference(screen, GameSettings::instance().GAME_BOX); + const int money = OCR::read_number(logger, extract_box_reference(game, MONEY_DIGITS)); + if (money < 0 || money > MAX_MONEY){ + return std::nullopt; + } + return (size_t)money; +} + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_MoneyBoxReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_MoneyBoxReader.h new file mode 100644 index 0000000000..0e0cf2467f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_MoneyBoxReader.h @@ -0,0 +1,32 @@ +/* Money Box Reader + * + * From: https://github.com/PokemonAutomation/ + * + * Reads the money box a Mart's BUY menu shows on the top left: "MONEY", + * and below it the amount after the POKEDOLLAR sign, right-aligned + * (pokefirered money.c PrintMoneyAmountInMoneyBox). + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_MoneyBoxReader_H +#define PokemonAutomation_PokemonFRLG_MoneyBoxReader_H + +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The money the box on `screen` (the whole frame) shows; nothing if it +// can't be read. +std::optional read_money_box(Logger& logger, const ImageViewRGB32& screen); + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities_Tests.cpp index b832f421a7..6a52c5e3d3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities_Tests.cpp @@ -40,6 +40,8 @@ class StubMart : public MartActions{ virtual void dump_screen(std::string_view) override{} virtual PurchaseEnd buy(const MartBuy&) override{ return PurchaseEnd::FAILED; } virtual bool leave() override{ return true; } + virtual std::optional read_money() override{ return std::nullopt; } + virtual SaleEnd sell(const MartLine&) override{ return SaleEnd::FAILED; } }; class StubTeach : public TeachActions{ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBagUse.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBagUse.h index 752e00b161..f2091132de 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBagUse.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBagUse.h @@ -45,6 +45,10 @@ struct ItemUses{ // 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); +// Uses the strongest Repel the bag holds: a Max Repel, else a Super +// Repel, else a Repel (a use_repel step). NONE_LEFT if it holds none. +ItemUseEnd use_strongest_repel(Logger& logger, ItemUseActions& actions); + // Whether a use_item step that ended with `end` has done its job: the // item used, or a Repel used before still on. With `if_any` (the step's // arg), also when the bag has none left: the walk goes on without it diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp index ce4dd0fe37..0ac83fea41 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp @@ -692,7 +692,7 @@ Outcome other_game_step(StoryEnv& env, RunState& state, const StoryStep& step){ ConsolePcStorageActions pc(env.console, env.context); std::optional done = run_field_game_step(env.console, env.context, env.language, env.player_gender, step, env.party); if (done && done->succeeded()){ - if (const std::optional item = parse_use_item_step(step); item && is_repel(*item)){ + if (const auto item = step.action == USE_REPEL_VARIANT ? std::optional(STRONGEST_REPEL) : parse_use_item_step(step); item && is_repel(*item)){ env.supplies.repel = *item; env.supplies.repel_menu = step_start_menu(step.args); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp index 52a35df051..89985d094b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp @@ -119,6 +119,10 @@ std::vector field_action_specs(){ 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)"), optional_arg("if_any", StoryArgType::BOOLEAN, false, "skip the step, logged, if the bag has none left, instead of failing"), }}, + {.kind = StoryStepKind::GAME, .name = USE_REPEL_VARIANT, .args = { + optional_arg("safari", StoryArgType::BOOLEAN, false, "in the Safari Zone, whose START menu has RETIRE on top"), + optional_arg("if_any", StoryArgType::BOOLEAN, false, "skip the step, logged, if the bag has no Repel left, instead of failing"), + }}, {.kind = StoryStepKind::GAME, .name = RIDE_BIKE_VARIANT, .args = {}}, {.kind = StoryStepKind::GAME, .name = REGISTER_BIKE_VARIANT, .args = {}}, }; @@ -213,8 +217,8 @@ std::optional run_field_game_step( return done(); } 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") + const ItemUseEnd end = step.action == REGISTER_BIKE_VARIANT ? register_item_in_bag(console.logger(), bag, "bicycle") + : step.action == USE_REPEL_VARIANT ? use_strongest_repel(console.logger(), bag) : use_items_from_bag(console.logger(), bag, *parse_use_item_step(step), use_item_count(step)).end; // A Repel that lingered from before leaves one on: the step's purpose. const bool* if_any = game_arg(step.args, "if_any"); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.cpp index 36546d5c91..d1dd51538a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.cpp @@ -105,7 +105,7 @@ size_t use_item_count(const Pokemon::StoryStep& step){ } 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(); + return (bike && step.args.empty()) || step.action == USE_REPEL_VARIANT || parse_use_item_step(step).has_value(); } std::optional bag_row_of(const std::vector& rows, std::string_view item){ @@ -232,6 +232,9 @@ ItemUses use_items_from_bag(Logger& logger, ItemUseActions& actions, std::string } return {ItemUseEnd::USED, used}; } +ItemUseEnd use_strongest_repel(Logger& logger, ItemUseActions& actions){ + return with_bag_row(logger, actions, repel_renew_order(STRONGEST_REPEL, std::nullopt), /*registers=*/false); +} ItemUseEnd register_item_in_bag(Logger& logger, ItemUseActions& actions, std::string_view item){ return with_bag_row(logger, actions, {std::string(item)}, true); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.h index ac461ee801..e74426be5c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.h @@ -49,6 +49,14 @@ inline const std::string BIKE_VARIANT = "bike"; // the registration is saved with the game, and a save made before it // doesn't have it. inline const std::string REGISTER_BIKE_VARIANT = "register_bike"; +// The GAME step that uses the strongest Repel the bag holds +// (use_strongest_repel()); one that wears off during the walks after it +// is renewed strongest first (STRONGEST_REPEL). Args as a use_item +// step's "safari" and "if_any". +inline const std::string USE_REPEL_VARIANT = "use_repel"; +// The Repel a use_repel step's renewals start from: repel_renew_order() +// tries it, then the weaker ones. +inline const std::string STRONGEST_REPEL = "max-repel"; // The item a use_item step uses (with "safari", from the Safari Zone's 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 eceedcb97c..eca98c8cc8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse_Tests.cpp @@ -145,6 +145,8 @@ struct ItemUseInput{ bool opens = true; // renew_repel() instead of use_item_from_bag(). bool renew = false; + // use_strongest_repel() (a use_repel step) instead. + bool strongest = false; // The step's count (use_items_from_bag()). size_t count = 1; bool no_level_up = false; @@ -179,8 +181,8 @@ class Test_AutoStoryItemUse : public UnitTest{ bag.repel_says = m_case.input.repel_says; 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) + const ItemUseEnd end = m_case.input.strongest ? use_strongest_repel(logger, bag) + : 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) || is_lead_recovery_item(item) ? bag.used_on_lead == bag.used : bag.used_on_lead.empty(); const std::optional downs = m_case.expectation.downs; @@ -200,6 +202,43 @@ class Test_AutoStoryItemUse : public UnitTest{ }; const ItemUseCase ITEM_USE_CASES[] = { + // The bag at 17.04b (console B, 2026-11-15): Repel x2, Super Repel x4. + { + .name = "use_repel: the Super Repel over the Repels", + .input = { + .variant = "use_repel", + .items = {{"potion", 3}, {"repel", 2}, {"super-repel", 4}, {"escape-rope", 4}}, + .strongest = true, + }, + .expectation = {.end = ItemUseEnd::USED, .used = {"super-repel"}}, + }, + { + .name = "use_repel: the Max Repel over the Super Repels", + .input = { + .variant = "use_repel", + .items = {{"super-repel", 4}, {"max-repel", 1}}, + .strongest = true, + }, + .expectation = {.end = ItemUseEnd::USED, .used = {"max-repel"}}, + }, + { + .name = "use_repel: no Repel left", + .input = { + .variant = "use_repel", + .items = {{"potion", 3}, {"repel", 0}}, + .strongest = true, + }, + .expectation = {.end = ItemUseEnd::NONE_LEFT, .used = {}}, + }, + { + .name = "a use_repel's Repel wore off: the strongest left renews it", + .input = { + .variant = "use_item max-repel", + .items = {{"repel", 2}, {"super-repel", 1}}, + .renew = true, + }, + .expectation = {.end = ItemUseEnd::USED, .used = {"super-repel"}}, + }, { .name = "a Repel below the Potions", .input = { diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp index bf1a30af1b..89feedf1fc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp @@ -340,6 +340,8 @@ std::vector items_read_by(const StoryStep& step){ if (const auto* item = step_arg(step, "item")){ items.push_back(*item); } + }else if (step.action == USE_REPEL_VARIANT){ + items.insert(items.end(), {"max-repel", "super-repel", "repel"}); }else if (step.action == COUNT_ITEMS_VARIANT){ if (const auto* counted = step_arg>(step, "items")){ items.insert(items.end(), counted->begin(), counted->end()); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.cpp index d328fe288b..7f5ee8cf5f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.cpp @@ -179,6 +179,13 @@ Pokemon::StoryActionArgs mart_args(const MartPlan& plan){ if (plan.if_affordable){ args["if_affordable"] = true; } + if (!plan.sells.empty()){ + std::vector sells; + for (const MartLine& line : plan.sells){ + sells.push_back({line.item, line.quantity}); + } + args["sell"] = std::move(sells); + } return args; } @@ -196,6 +203,14 @@ std::optional mart_plan_of(const Pokemon::StoryActionArgs& args){ } plan.lines.push_back({item.item, item.quantity}); } + if (const auto* sells = game_arg>(args, "sell")){ + for (const Pokemon::StoryItem& item : *sells){ + if (item.quantity < 1 || item.quantity > MAX_QUANTITY || !sale_price(item.item)){ + return std::nullopt; + } + plan.sells.push_back({item.item, item.quantity}); + } + } return plan; } @@ -250,15 +265,27 @@ class MartRun{ {} MartResult run(){ + for (const MartLine& sale : m_plan.sells){ + if (!sell_one(sale)){ + return end(MartEnd::FAILED, m_result.detail); + } + } if (!m_actions.open_buy_list()){ return end(MartEnd::FAILED, "the BUY list didn't show"); } - const std::optional> buys = plan_mart_buys(m_stock, m_plan); + std::optional> buys = plan_mart_buys(m_stock, m_plan); if (!buys){ return leave(MartEnd::FAILED, "an item the Mart doesn't sell"); } + m_result.money = m_actions.read_money(); + if (!check_sales()){ + return leave(MartEnd::FAILED, m_result.detail); + } + if (m_plan.if_affordable && m_result.money){ + cut_to_money(*m_result.money, *buys); + } for (const MartBuy& buy : *buys){ - if (!buy_one(buy)){ + if (buy.quantity != 0 && !buy_one(buy)){ return leave(MartEnd::FAILED, m_result.detail); } } @@ -279,6 +306,55 @@ class MartRun{ return this->end(end, std::move(detail)); } + // Sells `sale`, facing the clerk before and after. None in the bag is + // logged and passed. + bool sell_one(const MartLine& sale){ + switch (m_actions.sell(sale)){ + case SaleEnd::SOLD:{ + const size_t earned = sale.quantity * sale_price(sale.item).value_or(0); + m_logger.log(std::format("Mart: sold {} {} for ${}.", sale.quantity, sale.item, earned)); + m_result.sold.push_back(sale); + m_result.earned += earned; + return true; + } + case SaleEnd::NONE_IN_BAG: + m_logger.log(std::format("Mart: no {} in the bag to sell; skipped.", sale.item), COLOR_ORANGE); + return true; + case SaleEnd::FAILED: + break; + } + m_result.detail = std::format("selling {} {} didn't come back to the clerk", sale.quantity, sale.item); + return false; + } + // The money read on the BUY list holds at least what the sales + // earned: less, and a sale didn't happen. + bool check_sales(){ + if (!m_result.money){ + m_logger.log("Mart: the money box wasn't read; buying blind.", COLOR_ORANGE); + return true; + } + m_logger.log(std::format("Mart: ${} on the BUY list, ${} of it from sales.", *m_result.money, m_result.earned)); + if (*m_result.money < m_result.earned){ + m_result.detail = std::format("${} after selling for ${}", *m_result.money, m_result.earned); + return false; + } + return true; + } + // Each line, in order, cut to what `money` still covers; a line it + // covers none of is skipped, logged. + void cut_to_money(size_t money, std::vector& buys){ + for (MartBuy& buy : buys){ + const size_t price = buy.cost / buy.quantity; + const size_t affordable = std::min(buy.quantity, money / price); + if (affordable < buy.quantity){ + m_logger.log(std::format("Mart: ${} covers {} of {} {}.", money, affordable, buy.quantity, buy.item), COLOR_ORANGE); + } + buy.quantity = affordable; + buy.cost = affordable * price; + money -= buy.cost; + } + } + // Moves the cursor to `buy`'s row one press at a time, reading the // cursor's row after each: a name that isn't the row's stops it, the // screen saved. A row that can't be read is passed, or bought, by its @@ -359,11 +435,18 @@ std::string mart_message(const MartPlan& plan, const MartResult& result){ if (bought.empty()){ bought = "nothing"; } + std::string sold; + for (const MartLine& sale : result.sold){ + sold += std::format("{}{} {}", sold.empty() ? "sold " : ", ", sale.quantity, sale.item); + } + if (!sold.empty()){ + sold += std::format(" for ${}, ", result.earned); + } switch (result.end){ case MartEnd::DONE: - return std::format("Mart ({}): bought {} for ${}.", plan.map, bought, result.spent); + return std::format("Mart ({}): {}bought {} for ${}.", plan.map, sold, bought, result.spent); case MartEnd::FAILED: - return std::format("Mart ({}): {}; bought {} for ${}.", plan.map, result.detail, bought, result.spent); + return std::format("Mart ({}): {}; {}bought {} for ${}.", plan.map, result.detail, sold, bought, result.spent); } return "Mart: ?"; } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.h index 0801ef4e0c..df6858ae99 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.h @@ -23,13 +23,21 @@ * until its top shows. A row on the screen is not the list's row once * it has scrolled. * - * The step never opens SELL. The money can't be read, so each line buys - * its quantity, in the order listed, blind. The only money check is the - * "You don't have enough money." dialog, which fails the step; a - * quantity typed past the box's most wraps, which the box's read catches - * where it can be read. A plan with if_affordable skips such a line - * instead, logged. A Mart step belongs early in its checkpoint, so - * that a failure costs little. + * - SELL opens the bag (item_menu.c, ITEMMENULOCATION_SHOP) on its last + * pocket. A on an item: "How many would you like to sell?" with the + * box on 1 when the bag holds more than one, then "I can pay $X. Would + * that be okay?" YES/NO on YES (half the item's price; one priced 0 + * gets "OH, no. I can't buy that."); YES: "Turned over the and + * received $X.", back to the bag. B out of the bag: the clerk's menu. + * + * A plan's sales go first, each from facing the clerk back to facing + * the clerk. The money box is read once on the BUY list: a read below + * what the sales earned fails the step (a sale that didn't happen), and + * an if_affordable line is cut to what the money covers. Unread, each + * line buys its quantity, in the order listed, blind: the only money + * check is then the "You don't have enough money." dialog, which fails + * the step, or skips the line with if_affordable, logged. A Mart step + * belongs early in its checkpoint, so that a failure costs little. * * The decisions take their game steps from a MartActions, so they run in * tests against a fake Mart; ConsoleMartActions does them on the console @@ -73,13 +81,21 @@ std::string_view mart_end_name(MartEnd end); // The summary result of a Mart shopping session. struct MartResult{ MartEnd end = MartEnd::FAILED; + std::vector sold = {}; + // What the clerk paid for `sold` (sale_price()). + size_t earned = 0; + // The money box on the BUY list before the first buy, if read. + std::optional money = std::nullopt; std::vector bought = {}; size_t spent = 0; // FAILED: what failed. std::string detail = {}; }; -// Facing the clerk: buys as `plan` says, then back to the overworld. +// Facing the clerk: sells, then buys, as `plan` says, then back to the +// overworld. The money box, read once on the BUY list, checks the sales +// (it holds at least what they earned) and cuts an if_affordable line to +// what it covers; unread, the buys go on blind. MartResult run_mart(Logger& logger, MartActions& actions, const MartPlan& plan); // Formats a log message summarizing the Mart shopping result. @@ -105,8 +121,14 @@ class ConsoleMartActions : public MartActions{ virtual void dump_screen(std::string_view name) override; virtual PurchaseEnd buy(const MartBuy& buy) override; virtual bool leave() override; + virtual std::optional read_money() override; + virtual SaleEnd sell(const MartLine& sale) override; private: + // From the bag: out to the overworld facing the clerk; `end`, or + // FAILED if the overworld didn't come back. + SaleEnd back_to_clerk(SaleEnd end); + ConsoleHandle& m_console; ProControllerContext& m_context; Language m_language; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartBuys.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartBuys.h index d7736f955d..3d951cc3c2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartBuys.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartBuys.h @@ -64,6 +64,15 @@ enum class PurchaseEnd{ FAILED, }; +// How selling one line ended. +enum class SaleEnd{ + // "Turned over the and received $N.": back facing the clerk. + SOLD, + // The pocket has none of the item: back facing the clerk. + NONE_IN_BAG, + FAILED, +}; + // The game steps a Mart step takes, injected so its decisions run in // tests. class MartActions{ @@ -88,6 +97,11 @@ class MartActions{ // From the list: CANCEL, SEE YA!, to the overworld. False if it didn't // come back. virtual bool leave() = 0; + // On the list: the money box's amount; nothing if it can't be read. + virtual std::optional read_money() = 0; + // Facing the clerk: talk, SELL, `sale.quantity` of `sale.item` from + // its pocket, YES, out of the bag and the menu, facing the clerk again. + virtual SaleEnd sell(const MartLine& sale) = 0; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartPlan.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartPlan.h index 73d68560b4..756ff52e4d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartPlan.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartPlan.h @@ -46,8 +46,13 @@ struct MartLine{ size_t quantity = 1; }; -// What a Mart step buys: each line's quantity, blind (the money can't be -// read). +// What the clerk pays for one `item` (half its pokefirered items.json +// price): the items the story sells (the NUGGET); nothing for another, +// or for one priced 0, which the clerk won't buy ("OH, no. I can't buy +// that.", as the MOON STONE). +std::optional sale_price(std::string_view item); + +// What a Mart step sells, then buys: each line's quantity. struct MartPlan{ // The Mart's map, for its stock. std::string map = {}; @@ -55,18 +60,23 @@ struct MartPlan{ std::vector lines = {}; // A line the money doesn't cover (the Mart refuses it, or its // quantity box can't reach the count) is skipped with a log line, - // and the step goes on, instead of failing. + // and the step goes on, instead of failing. With the money read on + // the BUY list, a line is cut to what the money covers instead. bool if_affordable = false; + // Sold at the same counter (SELL) before the buys, each from its + // pocket; a line the bag has none of is skipped, logged. + std::vector sells = {}; }; // The GAME action of a Mart step: map WORD (required), the Mart's map; // items ITEMS (required), the lines in order; if_affordable BOOLEAN -// (absent: false), MartPlan::if_affordable. +// (absent: false), MartPlan::if_affordable; sell ITEMS (absent: none), +// MartPlan::sells. inline const std::string MART_ACTION = "mart"; // `plan` as its action's args. Pokemon::StoryActionArgs mart_args(const MartPlan& plan); -// The plan a MART_ACTION step's args give; nothing if it has no line or -// names an item its Mart doesn't sell. +// The plan a MART_ACTION step's args give; nothing if it has no line, +// names an item its Mart doesn't sell, or sells one the clerk won't buy. std::optional mart_plan_of(const Pokemon::StoryActionArgs& args); // The table's Mart step, once per save (StoryStep::once_per_save): a diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartSell.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartSell.cpp new file mode 100644 index 0000000000..099fa0a7ac --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartSell.cpp @@ -0,0 +1,135 @@ +/* AutoStory Mart Sell + * + * From: https://github.com/PokemonAutomation/ + * + * The Mart's SELL and its money box on the console, and what the clerk + * pays. + * + */ + +#include +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_MoneyBoxReader.h" +#include "PokemonFRLG_AutoStoryPresses.h" +#include "PokemonFRLG_AutoStoryBag.h" +#include "PokemonFRLG_AutoStoryMart.h" +#include "PokemonFRLG_AutoStoryPocketList.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +using namespace std::chrono_literals; + + +namespace{ + +// The bag slides in after SELL and ignores presses until it is done, as +// from the START menu (PokemonFRLG_AutoStoryItemUse.cpp). +constexpr Milliseconds BAG_SLIDE_IN = 1500ms; +constexpr Milliseconds POCKET_TAP = 100ms; +constexpr Milliseconds POCKET_SWITCH_WAIT = 600ms; +constexpr Milliseconds CURSOR_TAP = 100ms; +constexpr Milliseconds CURSOR_WAIT = 150ms; +// Out of the bag, "Is there anything else I may do?", SEE YA!. +constexpr Milliseconds MENU_CLOSE_TIMEOUT = 20000ms; + +// Half of pokefirered items.json's price, for the items the story sells. +struct SaleItem{ + std::string_view item; + size_t price; +}; +constexpr SaleItem SALE_ITEMS[] = { + {"nugget", 10000 / 2}, +}; + +} // namespace + + +std::optional sale_price(std::string_view item){ + for (const SaleItem& sale : SALE_ITEMS){ + if (sale.item == item){ + return sale.price; + } + } + return std::nullopt; +} + + +std::optional ConsoleMartActions::read_money(){ + m_context.wait_for_all_requests(); + const VideoSnapshot screen = m_console.video().snapshot(); + if (!screen){ + return std::nullopt; + } + return read_money_box(m_console.logger(), screen); +} + +SaleEnd ConsoleMartActions::sell(const MartLine& sale){ + // "May I help you?" BUY, SELL, SEE YA!: SELL, and the bag slides in. + // Not timed on the console: the BUY waits. + pbf_press_button(m_context, BUTTON_A, 160ms, 2000ms); + pbf_press_dpad(m_context, DPAD_DOWN, 160ms, 300ms); + pbf_press_button(m_context, BUTTON_A, 160ms, BAG_SLIDE_IN); + // Left to the Items pocket, which doesn't wrap, and up to its top. + for (int c = 0; c < 3; c++){ + pbf_press_dpad(m_context, DPAD_LEFT, POCKET_TAP, POCKET_SWITCH_WAIT); + } + for (int c = 0; c < 20; c++){ + pbf_press_dpad(m_context, DPAD_UP, CURSOR_TAP, CURSOR_WAIT); + } + const PocketRead read = read_open_pocket(m_console, m_context, m_language); + for (size_t c = 0; c < read.downs; c++){ + pbf_press_dpad(m_context, DPAD_UP, CURSOR_TAP, CURSOR_WAIT); + } + const std::optional row = bag_row_of(read.rows, sale.item); + if (!row){ + return back_to_clerk(SaleEnd::NONE_IN_BAG); + } + const int held = read.rows[*row].count; + if (held < 1){ + m_console.log(std::format("Mart: the {} count wasn't read; not sold.", sale.item), COLOR_RED); + return back_to_clerk(SaleEnd::FAILED); + } + for (size_t c = 0; c < *row; c++){ + pbf_press_dpad(m_context, DPAD_DOWN, CURSOR_TAP, CURSOR_WAIT); + } + // One held: straight to "I can pay $X." YES/NO. More: "How many would + // you like to sell?" on 1 first, typed as a purchase's. + pbf_press_button(m_context, BUTTON_A, 160ms, 2500ms); + if (held > 1){ + const QuantityPresses presses = quantity_presses(std::min(sale.quantity, (size_t)held)); + for (size_t c = 0; c < presses.right; c++){ + pbf_press_dpad(m_context, DPAD_RIGHT, 160ms, 340ms); + } + for (size_t c = 0; c < presses.up; c++){ + pbf_press_dpad(m_context, DPAD_UP, 160ms, 340ms); + } + pbf_press_button(m_context, BUTTON_A, 160ms, 2500ms); + } + // YES, then "Turned over the and received $X.", back to the bag. + pbf_press_button(m_context, BUTTON_A, 160ms, 2500ms); + pbf_press_button(m_context, BUTTON_A, 160ms, 1500ms); + return back_to_clerk(SaleEnd::SOLD); +} + +SaleEnd ConsoleMartActions::back_to_clerk(SaleEnd end){ + // B out of the bag to the clerk's menu, B (SEE YA!) and through + // "Please come again!". + pbf_press_button(m_context, BUTTON_B, 160ms, 1500ms); + try{ + mash_until_overworld(m_console, m_context, MENU_CLOSE_TIMEOUT); + return end; + }catch (OperationFailedException& e){ + m_console.log("Mart: " + e.message(), COLOR_RED); + return SaleEnd::FAILED; + } +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp index 9b173b5a42..5def9b5132 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp @@ -52,6 +52,10 @@ class FakeMart : public MartActions{ size_t money; bool rows_readable = true; bool quantity_readable = true; + // The money box: unread by default, as before it had a reader. + bool money_readable = false; + // A sale says SOLD but the clerk pays nothing. + bool sales_unpaid = false; // Rows that read as another item. std::map misread = {}; std::map bag = {}; @@ -108,6 +112,31 @@ class FakeMart : public MartActions{ in_list = false; return true; } + virtual std::optional read_money() override{ + if (!in_list){ + error = "read the money off the list"; + } + return money_readable ? std::optional(money) : std::nullopt; + } + // The clerk buys from `bag` at sale_price(), from facing the clerk. + virtual SaleEnd sell(const MartLine& sale) override{ + const auto held = bag.find(sale.item); + const std::optional price = sale_price(sale.item); + if (in_list || !price){ + error = in_list ? "SELL from the BUY list" : "sold an item the clerk won't buy"; + return SaleEnd::FAILED; + } + if (held == bag.end()){ + return SaleEnd::NONE_IN_BAG; + } + const size_t quantity = std::min(sale.quantity, held->second); + held->second -= quantity; + if (held->second == 0){ + bag.erase(held); + } + money += sales_unpaid ? 0 : quantity * *price; + return SaleEnd::SOLD; + } private: // One UP or DOWN on the list (the stock, then CANCEL), as pokefirered's @@ -340,9 +369,50 @@ std::vector mart_cases_5(){ }; } +// 19.01a sells the Nugget ($5,000, half its 10,000) and buys Super +// Potions with it at Lavender's Mart: the money box, read on the BUY +// list, checks the sale and cuts the if_affordable line to what it +// covers; unread, the line is bought blind. +std::vector mart_cases_6(){ + const auto nugget = [](FakeMart& mart){ mart.bag["nugget"] = 1; }; + const auto nugget_read = [](FakeMart& mart){ mart.bag["nugget"] = 1; mart.money_readable = true; }; + const auto unpaid = [](FakeMart& mart){ mart.bag["nugget"] = 1; mart.money_readable = true; mart.sales_unpaid = true; }; + const auto read_only = [](FakeMart& mart){ mart.money_readable = true; }; + MartPlan six{.map = "LavenderTown_Mart", .lines = {{"super-potion", 6}}, .if_affordable = true, .sells = {{"nugget", 1}}}; + MartPlan nine = six; + nine.lines[0].quantity = 9; + return { + { + .name = "sell the Nugget, then 6 Super Potions from its $5000", + .plan = six, .money = 0, .setup = nugget_read, + .bag = {{"super-potion", 6}}, .money_after = 800, + }, + { + .name = "the money unread: the 6 bought blind", + .plan = six, .money = 0, .setup = nugget, + .bag = {{"super-potion", 6}}, .money_after = 800, + }, + { + .name = "9 wanted, the money read covers 7", + .plan = nine, .money = 0, .setup = nugget_read, + .bag = {{"super-potion", 7}}, .money_after = 100, + }, + { + .name = "no Nugget in the bag: the sale skipped, the money read covers 1", + .plan = six, .money = 1000, .setup = read_only, + .bag = {{"super-potion", 1}}, .money_after = 300, + }, + { + .name = "a sale the money doesn't show fails before buying", + .plan = six, .money = 0, .setup = unpaid, + .end = MartEnd::FAILED, .bag = {}, .money_after = 0, + }, + }; +} + std::vector mart_cases(){ std::vector cases; - for (std::vector part : {mart_cases_1(), mart_cases_2(), mart_cases_3(), mart_cases_4(), mart_cases_5()}){ + for (std::vector part : {mart_cases_1(), mart_cases_2(), mart_cases_3(), mart_cases_4(), mart_cases_5(), mart_cases_6()}){ cases.insert(cases.end(), part.begin(), part.end()); } return cases; @@ -412,12 +482,20 @@ UnitTestResult test_mart_args(){ if (!affordable_back || !affordable_back->if_affordable || back->if_affordable || args.contains("if_affordable")){ return "if_affordable didn't read back from its args, or was written when false."; } + MartPlan selling = plan; + selling.sells = {{"nugget", 1}}; + const std::optional selling_back = mart_plan_of(mart_args(selling)); + if (!selling_back || selling_back->sells.size() != 1 || selling_back->sells[0].item != "nugget" || args.contains("sell")){ + return "The sales didn't read back from their args, or were written when none."; + } const std::vector> bad_args{ {"items", std::vector{}}, {"map", std::string("Nowhere_Mart")}, {"map", std::string("ViridianCity_Mart")}, {"items", std::vector{{"potion", 0}}}, {"items", std::vector{{"potion", 100}}}, + {"sell", std::vector{{"moon-stone", 1}}}, + {"sell", std::vector{{"nugget", 0}}}, }; for (const auto& [key, value] : bad_args){ Pokemon::StoryActionArgs bad = args; @@ -557,6 +635,8 @@ struct ApprovedShopping{ size_t money; std::map bag; size_t money_after; + // In the bag before the step, for its sales. + std::map held = {}; }; // A plan of one line: `quantity` of `item` at `map`'s Mart. @@ -569,6 +649,9 @@ const std::vector& approved_shopping(){ status_heals.lines.push_back({"parlyz-heal", 2}); MartPlan full_heals = one_line("IndigoPlateau_PokemonCenter_1F", "full-heal", 2); full_heals.if_affordable = true; + MartPlan nugget_for_potions = one_line("LavenderTown_Mart", "super-potion", 6); + nugget_for_potions.if_affordable = true; + nugget_for_potions.sells = {{"nugget", 1}}; static const std::vector shopping{ { .checkpoint = "12.03", .step = "buy_super_potions_for_silph", .before = "16.01", @@ -580,6 +663,12 @@ const std::vector& approved_shopping(){ .plan = one_line("CeladonCity_DepartmentStore_5F", "x-accuracy", 2), .money = 1900, .bag = {{"x-accuracy", 2}}, .money_after = 0, }, + { + // The Nugget pays for all 6 with no money before. + .checkpoint = "19.01a", .step = "sell_nugget_buy_super_potions", .before = "19.01", + .plan = nugget_for_potions, + .money = 0, .bag = {{"super-potion", 6}}, .money_after = 800, .held = {{"nugget", 1}}, + }, { .checkpoint = "19.01a", .step = "buy_status_heals", .before = "19.01", .plan = status_heals, @@ -596,9 +685,14 @@ const std::vector& approved_shopping(){ } bool same_plan(const MartPlan& a, const MartPlan& b){ - if (a.map != b.map || a.if_affordable != b.if_affordable || a.lines.size() != b.lines.size()){ + if (a.map != b.map || a.if_affordable != b.if_affordable || a.lines.size() != b.lines.size() || a.sells.size() != b.sells.size()){ return false; } + for (size_t c = 0; c < a.sells.size(); c++){ + if (a.sells[c].item != b.sells[c].item || a.sells[c].quantity != b.sells[c].quantity){ + return false; + } + } for (size_t c = 0; c < a.lines.size(); c++){ if (a.lines[c].item != b.lines[c].item || a.lines[c].quantity != b.lines[c].quantity){ return false; @@ -639,6 +733,7 @@ UnitTestResult test_approved_shopping(){ .name = std::format("{}/{}", shopping.checkpoint, shopping.step), .plan = *plan, .money = shopping.money, + .setup = [&](FakeMart& mart){ mart.bag.insert(shopping.held.begin(), shopping.held.end()); }, .bag = shopping.bag, .money_after = shopping.money_after, }); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable.cpp index 37261b04bd..0ebee4bc1c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable.cpp @@ -124,7 +124,8 @@ Pokemon::StoryActionSpec mart_action_spec(){ required_arg("map", StoryArgType::WORD, "the Mart's map, for its stock"), required_arg("items", StoryArgType::ITEMS, "what to buy, in priority order"), absent_arg("fill_to", StoryArgType::INTEGER, "buy only what the bag lacks of the first line's item to hold this many"), - absent_arg("if_affordable", StoryArgType::BOOLEAN, "skip a line the money doesn't cover, logged, instead of failing"), + absent_arg("if_affordable", StoryArgType::BOOLEAN, "skip a line the money doesn't cover, logged, instead of failing; with the money read, cut it to what the money covers"), + absent_arg("sell", StoryArgType::ITEMS, "what to sell at the same counter before buying (sale_price())"), }}; } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryVictoryRoad_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryVictoryRoad_Tests.cpp index df0be63067..caa4b485c4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryVictoryRoad_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryVictoryRoad_Tests.cpp @@ -80,8 +80,8 @@ const StoryCheckpoint& checkpoint(std::string_view id){ // Runs the walking steps of `row` on the sim from its start, up to (not // including) the step named `until`, or all of them if `until` is empty; -// the steps that are not walks (the Mart, the level check, a scene, the -// battle) are the console's. `marked` is where the sim stood after the +// a Fly moves the sim to its landing; the steps that are not walks (the +// Mart, the level check, a scene, the battle) are the console's. `marked` is where the sim stood after the // step named `mark`. std::string walk_row( OverworldSim& sim, StoryPose& at, const StoryCheckpoint& row, @@ -91,7 +91,8 @@ std::string walk_row( if (step.name == until){ return ""; } - if (step.kind != StoryStepKind::ROUTE && step.kind != StoryStepKind::LEGS && step.kind != StoryStepKind::STEP){ + const bool walks = step.kind == StoryStepKind::ROUTE || step.kind == StoryStepKind::LEGS || step.kind == StoryStepKind::STEP; + if (!walks && !parse_fly_step(step)){ continue; } if (const std::string failure = row_step_on_sim(sim, at, step, row.id); !failure.empty()){ @@ -104,7 +105,9 @@ std::string walk_row( return until.empty() ? "" : std::format("{}: no step {}.", row.id, until); } -// 19.01a: the street to the Mart's counter, where 19.01 starts. 19.01: out +// 19.01a: out to the street, the Fly to Lavender and its Mart's counter, +// back out, the Fly to Viridian and its Mart's counter, where 19.01 +// starts. 19.01: out // of the Mart, along the street to Route 22 and along it to the rival's // trigger column, then the step onto it. The rival is the battle step's, so // the walk ends on the trigger tile (33, 5), facing west, with no trainer met. @@ -115,6 +118,15 @@ UnitTestResult test_route22_rival_trigger(){ if (const std::string failure = walk_row(sim, at, shop, ""); !failure.empty()){ return failure; } + StoryPose lavender = *story_pose(Pokemon::pose_of(*shop.start)); + OverworldSim to_lavender(story_maps(), MapPosition{lavender.map, lavender.tile}, *lavender.facing); + if (const std::string failure = walk_row(to_lavender, lavender, shop, "sell_nugget_buy_super_potions"); !failure.empty()){ + return failure; + } + const MapPosition lavender_counter{"LavenderTown_Mart", {4, 3}}; + if (!(to_lavender.state().position == lavender_counter) || lavender.facing != Direction::WEST){ + return std::format("19.01a: sold and bought on {}, not facing Lavender's clerk from {}.", to_string(to_lavender.state().position), to_string(lavender_counter)); + } const MapPosition counter{"ViridianCity_Mart", {4, 3}}; if (!(sim.state().position == counter)){ return std::format("19.01a: ended on {}, not the Mart's counter {}.", to_string(sim.state().position), to_string(counter)); @@ -349,7 +361,7 @@ UnitTestResult test_indigo_center_walk(){ const AutoStoryVictoryRoadCase AUTO_STORY_VICTORY_ROAD_CASES[] = { { - .input = {.name = "19.01a Viridian's Mart and 19.01 to the Route 22 rival's trigger", .body = test_route22_rival_trigger}, + .input = {.name = "19.01a Lavender's and Viridian's Marts and 19.01 to the Route 22 rival's trigger", .body = test_route22_rival_trigger}, .expectation = {.description = "Route 22 (33, 5), no trainer met"}, }, { 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 88eb14b621..41ab6fb672 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp @@ -849,7 +849,7 @@ UnitTestResult check_lines(const StoryCheckpoint& checkpoint, std::set(step.args, "item"))){ + if (step.action == USE_REPEL_VARIANT || (step.action == USE_ITEM_VARIANT && is_repel(Pokemon::story_arg(step.args, "item")))){ repelled = true; } const bool walks = step.kind == StoryStepKind::ROUTE || step.kind == StoryStepKind::LEGS; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.01a.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.01a.plan.json index 06890fd155..c08d3306d8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.01a.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.01a.plan.json @@ -3,7 +3,7 @@ "game": "frlg", "checkpoint": { "id": "19.01a", - "title": "Viridian's Mart: potions before the rival on Route 22", + "title": "Restock before the Route 22 rival: the Nugget for Super Potions at Lavender's Mart, status heals at Viridian's", "start": { "map": "ViridianCity_PokemonCenter_1F", "tiles": [[7, 4]], @@ -11,24 +11,68 @@ }, "events": [ { - "id": "viridian_mart_clerk", + "id": "viridian_outside", "kind": "route", "action": "run", "args": { - "target": {"map": "ViridianCity_Mart", "tiles": [[4, 3]], "facing": "west"} + "target": {"map": "ViridianCity", "tiles": [[26, 27]], "facing": "south"} }, - "why": "out of the Center and along the street to the Mart's door, in and up to the counter, facing the clerk (as 02.03)" + "why": "out of Viridian's Center onto the street below its door (26, 26), where 17.04b's Fly landed: Fly works outdoors only" }, { - "id": "buy_potions", + "id": "fly_lavender", + "kind": "game_action", + "action": "fly", + "args": {"town": "LavenderTown", "species": "pidgey"}, + "declared_after": {"map": "LavenderTown", "tiles": [[6, 6]], "facing": "south"}, + "why": "the Pidgey flies to Lavender Town, landing below the Pokemon Center's door (6, 5), as 10.01. Celadon's Department Store sells no Hyper Potion (pokefirered: only Saffron's, Cinnabar's and the Sevii Marts do) and its 2F items clerk, whose best healing is the Super Potion, is on a floor the walker has no map of; Lavender's Mart sells the same Super Potion (700) a short walk from its landing (08.03)" + }, + { + "id": "lavender_mart_clerk", + "kind": "route", + "action": "run", + "args": { + "target": {"map": "LavenderTown_Mart", "tiles": [[4, 3]], "facing": "west"} + }, + "why": "along the street to the Mart's door (20, 15), in and up to face the clerk (2, 3) over the counter (3, 3), as 08.03" + }, + { + "id": "sell_nugget_buy_super_potions", "kind": "game_action", "action": "mart", "args": { - "map": "ViridianCity_Mart", - "items": [{"item": "potion", "quantity": 4}], - "fill_to": 4 + "map": "LavenderTown_Mart", + "sell": [{"item": "nugget", "quantity": 1}], + "items": [{"item": "super-potion", "quantity": 6}], + "if_affordable": true + }, + "why": "the Nugget sold ($5,000, half its 10,000) pays for 6 Super Potions (4,200) whatever the money was: the bag at 17.04b holds potion x3 and super-potion x0 (console B, 2026-11-15 22:10) against PokeGuy's potion x4, superpotion x4 at the Route 22 rival (PokemonSimulation docs/battles/pokeguy-conditions.md). The 2 Moon Stones stay: the clerk pays nothing for them (price 0, \"OH, no. I can't buy that.\"). The money box, read on the BUY list, checks the sale and cuts the line to what it covers; unread, the 6 are bought blind, which the Nugget alone covers" + }, + { + "id": "out_of_lavender_mart", + "kind": "route", + "action": "run", + "args": { + "target": {"map": "LavenderTown", "tiles": [[20, 16]], "facing": "south"} + }, + "why": "back out of the Mart's door (20, 15) onto the street below it, as 08.03" + }, + { + "id": "fly_viridian", + "kind": "game_action", + "action": "fly", + "args": {"town": "ViridianCity", "species": "pidgey"}, + "declared_after": {"map": "ViridianCity", "tiles": [[26, 27]], "facing": "south"}, + "why": "the Pidgey flies back to Viridian City, landing below the Pokemon Center's door (26, 26), as 17.04b" + }, + { + "id": "viridian_mart_clerk", + "kind": "route", + "action": "run", + "args": { + "target": {"map": "ViridianCity_Mart", "tiles": [[4, 3]], "facing": "west"} }, - "why": "Potions up to 4 in the bag, as in PokeGuy's bag at the Route 22 rival (potion x4, superpotion x4, xaccuracy x4, xspecial x17, xspeed x5, guardspec x2; PokemonSimulation docs/battles/pokeguy-conditions.md); the Super Potions are 12.01's. Early in the row, so a failed purchase stops it before the walk. Buys 4 less what the bag holds, skipped at 4" + "why": "along the street to the Mart's door, in and up to the counter, facing the clerk (as 02.03), where 19.01 starts" }, { "id": "buy_status_heals", @@ -38,7 +82,7 @@ "map": "ViridianCity_Mart", "items": [{"item": "antidote", "quantity": 1}, {"item": "parlyz-heal", "quantity": 2}] }, - "why": "an Antidote and 2 Parlyz Heals for the League's poison and paralysis (user's decision 2026-11-15, from the shopping audit): PokeGuy's bag at the Route 22 rival and at Lorelei holds antidote x1 and parlyzheal x2 (PokemonSimulation docs/battles/pokeguy-conditions.md), which the story's bag never bought for the League; recovery_items() cures PSN and PAR with them. Viridian sells both (ANTIDOTE 100, PARLYZ HEAL 200: 500 in all); its own step, as buy_potions's fill_to would skip a whole step when the bag holds 4 Potions. Bought blind after 17.04's Giovanni prize" + "why": "an Antidote and 2 Parlyz Heals for the League's poison and paralysis (user's decision 2026-11-15, from the shopping audit): PokeGuy's bag at the Route 22 rival and at Lorelei holds antidote x1 and parlyzheal x2 (PokemonSimulation docs/battles/pokeguy-conditions.md), which the story's bag never bought for the League; recovery_items() cures PSN and PAR with them. Viridian sells both (ANTIDOTE 100, PARLYZ HEAL 200: 500 in all), paid from what the Nugget left (800 at least). The Potions this row used to fill to 4 are left out: the 6 Super Potions replace them" } ] }, @@ -46,7 +90,7 @@ "menu": "full", "notes": { "checkpoint": [ - "Starts at Viridian's nurse, where 17.04b ends. The Mart's clerk is first in the checkpoint: the potions are bought before the rival, and 19.01 starts in the Mart with the bag as it should be.", + "Starts at Viridian's nurse, where 17.04b ends. Flies to Lavender for the Super Potions, back to Viridian for the status heals, so that 19.01 starts in the Viridian Mart with the bag as the rival needs it.", "Ends in the Viridian Mart at (4, 3) facing the clerk, where 19.01 starts." ] } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.03.plan.json index 7c4d9edbb9..6ec4d04fe4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.03.plan.json @@ -6,6 +6,13 @@ "title": "Route 23: the seven badge guards and the Surf stretch, to Victory Road's mouth", "start": {"map": "Route23", "tiles": [[8, 153]], "facing": "north"}, "events": [ + { + "id": "repel_route23", + "kind": "game_action", + "action": "use_repel", + "args": {"if_any": true}, + "why": "the strongest Repel in the bag (a Super Repel from 17.04b's super-repel x4: 200 steps) before Route 23's grass and water: the lead (Lv49 at least, 19.01) is above every Route 23 wild Pokemon (pokefirered wild_encounters.json), so none shows; one that wears off on the way is renewed, strongest first. With none left the step is skipped (if_any) and the walker fights what comes" + }, { "id": "cascade_guard_approach", "kind": "route", diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.04.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.04.plan.json index 6419b15000..06c0c0aa88 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.04.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.04.plan.json @@ -18,6 +18,13 @@ }, "why": "HM04 Strength in place of Water Pulse, as pokeguy at Victory Road (PokemonSimulation docs/progression/pokeguy-moves.md: Strength, Surf, Bite, Blizzard; docs/battles/pokeguy-conditions.md, the move-learning table: STRENGTH replaces WATER_PULSE); an HM, so not a one-shot TM; keep list: Surf, Bite and Blizzard stay and the one other move (Water Pulse) goes; fails if none or several are outside the list. The Rainbow Badge it needs is 14.04's" }, + { + "id": "repel_victory_road", + "kind": "game_action", + "action": "use_repel", + "args": {"if_any": true}, + "why": "the strongest Repel left before the cave: Victory Road's wild Pokemon (Lv40s at most, pokefirered wild_encounters.json) are below the lead, so none shows while it lasts, and one that wears off on a floor is renewed, strongest first. One still on from Route 23 (19.03) lingers, which is done. With none left the step is skipped (if_any) and the walker fights what comes" + }, { "id": "cave_mouth", "kind": "step", diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 00536c3409..e8750681d5 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1980,6 +1980,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_BagDetector.h Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.h + Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_MoneyBoxReader.cpp + Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_MoneyBoxReader.h Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_LoadMenuDetector.cpp Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_LoadMenuDetector.h Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_NamingScreenDetector.cpp @@ -2272,6 +2274,7 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartBuys.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartPlan.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartSell.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMember.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMember.h From d838fedfa719c2fdce8e6105880466e9f3d20883 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 23:04:00 -0800 Subject: [PATCH 2/5] FRLG 19.01a: SELL behind its own MartSellActions, use_repel in its own header; plan golden 19 MartActions keeps the BUY list (7 methods); the SELL step, SaleEnd and sale_price() move to PokemonFRLG_AutoStoryMartSell.h with ConsoleMartSellActions, and run_mart() takes both. USE_REPEL_VARIANT and use_strongest_repel() move to PokemonFRLG_AutoStoryRepelStep.h, with repel_put_on_by() replacing the Events.cpp special case; STRONGEST_REPEL is internal. Golden 19 regenerated from the Fork Draft CI output for the new 19.01a Lavender route. --- .../PokemonFRLG_AutoStoryAbilities.cpp | 2 +- .../PokemonFRLG_AutoStoryAbilities.h | 1 + .../PokemonFRLG_AutoStoryAbilities_Tests.cpp | 13 +++- .../AutoStory/PokemonFRLG_AutoStoryBagUse.h | 4 - .../AutoStory/PokemonFRLG_AutoStoryEvents.cpp | 10 +-- .../PokemonFRLG_AutoStoryHideout.cpp | 1 + .../PokemonFRLG_AutoStoryItemUse.cpp | 15 ++++ .../AutoStory/PokemonFRLG_AutoStoryItemUse.h | 8 -- .../PokemonFRLG_AutoStoryItemUse_Tests.cpp | 1 + .../PokemonFRLG_AutoStoryItems_Tests.cpp | 1 + .../AutoStory/PokemonFRLG_AutoStoryMart.cpp | 20 ++++- .../AutoStory/PokemonFRLG_AutoStoryMart.h | 13 ++-- .../AutoStory/PokemonFRLG_AutoStoryMartBuys.h | 12 --- .../AutoStory/PokemonFRLG_AutoStoryMartPlan.h | 11 +-- .../PokemonFRLG_AutoStoryMartSell.cpp | 25 +++---- .../AutoStory/PokemonFRLG_AutoStoryMartSell.h | 75 +++++++++++++++++++ .../PokemonFRLG_AutoStoryMart_Tests.cpp | 53 ++++++++----- .../PokemonFRLG_AutoStoryPlanGolden19.h | 13 +++- .../PokemonFRLG_AutoStoryRepelStep.h | 46 ++++++++++++ .../AutoStory/PokemonFRLG_AutoStory_Tests.cpp | 1 + SerialPrograms/cmake/SourceFiles.cmake | 2 + 21 files changed, 237 insertions(+), 90 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartSell.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRepelStep.h diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.cpp index 901c235886..bff94cfc58 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.cpp @@ -19,7 +19,7 @@ using Pokemon::Outcome; namespace{ Outcome mart(Logger& logger, const MartPlan& plan, StoryAbilities& abilities){ - const MartResult result = run_mart(logger, abilities.mart, plan); + const MartResult result = run_mart(logger, abilities.mart, abilities.mart_sell, plan); const std::string message = mart_message(plan, result); logger.log(message, result.end == MartEnd::DONE ? COLOR_BLACK : COLOR_RED); return result.end == MartEnd::DONE ? Pokemon::outcome_ok() : Pokemon::outcome_failed(std::format("mart_{}", mart_end_name(result.end)), message); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.h index 80e75883b0..3df35411f9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.h @@ -35,6 +35,7 @@ namespace PokemonFRLG{ struct StoryAbilities{ MemberReader members; MartActions& mart; + MartSellActions& mart_sell; TeachActions& teach; GrindActions& grind; ChoiceActions& choice; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities_Tests.cpp index 6a52c5e3d3..128baa3306 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities_Tests.cpp @@ -41,6 +41,10 @@ class StubMart : public MartActions{ virtual PurchaseEnd buy(const MartBuy&) override{ return PurchaseEnd::FAILED; } virtual bool leave() override{ return true; } virtual std::optional read_money() override{ return std::nullopt; } +}; + +class StubMartSell : public MartSellActions{ +public: virtual SaleEnd sell(const MartLine&) override{ return SaleEnd::FAILED; } }; @@ -78,6 +82,7 @@ class StubFossils : public ChoiceActions{ struct StubGame{ std::optional level = 16; StubMart mart; + StubMartSell mart_sell; StubTeach teach; StubPatch patch; StubFossils fossils; @@ -136,7 +141,7 @@ std::string run_step(StubGame& game, const Pokemon::StoryStep& step){ } return MemberInfo{.species = "squirtle", .level = game.level, .moves = {"water-gun", "surf"}}; }; - StoryAbilities abilities{members, game.mart, game.teach, game.patch, game.fossils}; + StoryAbilities abilities{members, game.mart, game.mart_sell, game.teach, game.patch, game.fossils}; const std::optional outcome = run_ability_game_step(logger, nullptr, step, abilities); if (!outcome){ return "none"; @@ -237,7 +242,7 @@ UnitTestResult test_teach_records_fly_user(){ return MemberInfo{.species = "squirtle", .level = 16, .moves = {"water-gun", "surf"}}; }; PartyCache party; - StoryAbilities abilities{members, game.mart, game.teach, game.patch, game.fossils, {}, &party}; + StoryAbilities abilities{members, game.mart, game.mart_sell, game.teach, game.patch, game.fossils, {}, &party}; run_ability_game_step(logger, nullptr, teach_step("teach_surf", "", {.item = "hm03", .move = "surf", .member = 0}), abilities); run_ability_game_step(logger, nullptr, teach_step("teach_tm", "", {.item = "tm03", .move = "water-pulse", .member = 0, .forget_move = "water-gun"}), abilities); const std::optional user = party.users().find("surf"); @@ -304,7 +309,7 @@ UnitTestResult test_teach_updates_battle_lead(){ PartyMember lead = member_from_summary( "blastoise", 43, {{"bite", 25, 25}, {"water-pulse", 20, 20}, {"surf", 14, 15}, {"dig", 10, 10}}, std::nullopt ); - StoryAbilities abilities{members, game.mart, teach, game.patch, game.fossils, {}, nullptr, &lead}; + StoryAbilities abilities{members, game.mart, game.mart_sell, teach, game.patch, game.fossils, {}, nullptr, &lead}; const Pokemon::StoryStep step = teach_step("teach_blizzard", "", {.item = "tm14", .move = "blizzard", .member = 0, .forget_move = "dig"}); run_ability_game_step(logger, nullptr, step, abilities); if (lead.moves.size() != 4 || lead.moves[3].move != "blizzard" || lead.moves[3].pp != 5 || lead.moves[2].pp != 14){ @@ -315,7 +320,7 @@ UnitTestResult test_teach_updates_battle_lead(){ return "expected Blizzard against Venusaur after the TEACH."; } LearningTeach other({"bite", "water-pulse", "surf", "blizzard"}); - StoryAbilities second{members, game.mart, other, game.patch, game.fossils, {}, nullptr, &lead}; + StoryAbilities second{members, game.mart, game.mart_sell, other, game.patch, game.fossils, {}, nullptr, &lead}; run_ability_game_step(logger, nullptr, teach_step("teach_slot2", "", {.item = "tm13", .move = "ice-beam", .member = 1, .forget_move = "surf"}), second); if (lead.moves[2].move != "surf"){ return "a TEACH to party slot 2 changed the lead's moves."; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBagUse.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBagUse.h index f2091132de..752e00b161 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBagUse.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBagUse.h @@ -45,10 +45,6 @@ struct ItemUses{ // 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); -// Uses the strongest Repel the bag holds: a Max Repel, else a Super -// Repel, else a Repel (a use_repel step). NONE_LEFT if it holds none. -ItemUseEnd use_strongest_repel(Logger& logger, ItemUseActions& actions); - // Whether a use_item step that ended with `end` has done its job: the // item used, or a Repel used before still on. With `if_any` (the step's // arg), also when the bag has none left: the walk goes on without it diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp index 0ac83fea41..22ae865969 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp @@ -22,9 +22,8 @@ #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h" #include "VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h" #include "PokemonFRLG_AutoStoryAbilities.h" -#include "PokemonFRLG_AutoStoryBag.h" #include "PokemonFRLG_AutoStoryBattleData.h" -#include "PokemonFRLG_AutoStoryItemUse.h" +#include "PokemonFRLG_AutoStoryRepelStep.h" #include "PokemonFRLG_AutoStoryStartMenu.h" #include "PokemonFRLG_AutoStoryCapture.h" #include "PokemonFRLG_AutoStoryCenterHeal.h" @@ -562,6 +561,7 @@ void pace_trainer_battle(StoryEnv& env, RunState& state, const StoryStep& step, std::optional run_abilities(StoryEnv& env, RunState& state, const StoryStep& step){ const Pokemon::Pose patch = state.pose; ConsoleMartActions mart(env.console, env.context, env.language, env.supplies); + ConsoleMartSellActions mart_sell(env.console, env.context, env.language); ConsoleTeachScreens teach_screens(env.console, env.context, env.language); ScreenTeachActions teach(env.console.logger(), teach_screens); ConsoleGrindActions grind( @@ -572,7 +572,7 @@ std::optional run_abilities(StoryEnv& env, RunState& state, const Story [&]{ pace_trainer_battle(env, state, step, patch); } ); ConsoleChoiceActions choice(env.console, env.context); - StoryAbilities abilities{console_member_reader(env.console, env.context, env.language, StartMenu::FULL), mart, teach, grind, choice, console_species_finder(env.console, env.context, env.language, env.party), &env.party, &env.supplies.lead}; + StoryAbilities abilities{console_member_reader(env.console, env.context, env.language, StartMenu::FULL), mart, mart_sell, teach, grind, choice, console_species_finder(env.console, env.context, env.language, env.party), &env.party, &env.supplies.lead}; if (const std::optional plan = step.action == GRIND_CHECK_ACTION ? grind_plan_of(step.args) : std::nullopt){ if (grind_reached_already(env.console.logger(), grind, abilities.members, *plan)){ state.flags.insert(step.name); @@ -692,8 +692,8 @@ Outcome other_game_step(StoryEnv& env, RunState& state, const StoryStep& step){ ConsolePcStorageActions pc(env.console, env.context); std::optional done = run_field_game_step(env.console, env.context, env.language, env.player_gender, step, env.party); if (done && done->succeeded()){ - if (const auto item = step.action == USE_REPEL_VARIANT ? std::optional(STRONGEST_REPEL) : parse_use_item_step(step); item && is_repel(*item)){ - env.supplies.repel = *item; + if (const std::optional repel = repel_put_on_by(step)){ + env.supplies.repel = *repel; env.supplies.repel_menu = step_start_menu(step.args); } } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp index 89985d094b..9298c01fe7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp @@ -15,6 +15,7 @@ #include "PokemonFRLG_AutoStoryHideout.h" #include "PokemonFRLG_AutoStoryHideoutLift.h" #include "PokemonFRLG_AutoStoryItemUse.h" +#include "PokemonFRLG_AutoStoryRepelStep.h" #include "PokemonFRLG_AutoStoryBagUse.h" #include "PokemonFRLG_AutoStoryBag.h" #include "PokemonFRLG_AutoStoryRooms.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.cpp index d1dd51538a..6d9591737b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.cpp @@ -20,6 +20,7 @@ #include "PokemonFRLG_AutoStoryBag.h" #include "PokemonFRLG_AutoStoryBattleData.h" #include "PokemonFRLG_AutoStoryPocketList.h" +#include "PokemonFRLG_AutoStoryRepelStep.h" #include "PokemonFRLG_AutoStoryGameArgs.h" namespace PokemonAutomation{ @@ -140,6 +141,10 @@ std::string item_use_end_name(ItemUseEnd end){ namespace{ +// The Repel a use_repel step's renewals start from: repel_renew_order() +// tries it, then the weaker ones. +const std::string STRONGEST_REPEL = "max-repel"; + // After USE on the Repel `item`: what the game said, LINGERED if one // was still on. A message not read is taken as used, as before it was // read. @@ -235,6 +240,16 @@ ItemUses use_items_from_bag(Logger& logger, ItemUseActions& actions, std::string ItemUseEnd use_strongest_repel(Logger& logger, ItemUseActions& actions){ return with_bag_row(logger, actions, repel_renew_order(STRONGEST_REPEL, std::nullopt), /*registers=*/false); } +std::optional repel_put_on_by(const Pokemon::StoryStep& step){ + if (step.action == USE_REPEL_VARIANT){ + return STRONGEST_REPEL; + } + const std::optional item = parse_use_item_step(step); + if (item && is_repel(*item)){ + return item; + } + return std::nullopt; +} ItemUseEnd register_item_in_bag(Logger& logger, ItemUseActions& actions, std::string_view item){ return with_bag_row(logger, actions, {std::string(item)}, true); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.h index e74426be5c..ac461ee801 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse.h @@ -49,14 +49,6 @@ inline const std::string BIKE_VARIANT = "bike"; // the registration is saved with the game, and a save made before it // doesn't have it. inline const std::string REGISTER_BIKE_VARIANT = "register_bike"; -// The GAME step that uses the strongest Repel the bag holds -// (use_strongest_repel()); one that wears off during the walks after it -// is renewed strongest first (STRONGEST_REPEL). Args as a use_item -// step's "safari" and "if_any". -inline const std::string USE_REPEL_VARIANT = "use_repel"; -// The Repel a use_repel step's renewals start from: repel_renew_order() -// tries it, then the weaker ones. -inline const std::string STRONGEST_REPEL = "max-repel"; // The item a use_item step uses (with "safari", from the Safari Zone's 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 eca98c8cc8..89ce6ef195 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse_Tests.cpp @@ -22,6 +22,7 @@ #include "PokemonFRLG_AutoStoryEvents.h" #include "PokemonFRLG_AutoStoryItemUse.h" #include "PokemonFRLG_AutoStoryBagUse.h" +#include "PokemonFRLG_AutoStoryRepelStep.h" #include "PokemonFRLG_AutoStoryBag.h" #include "PokemonFRLG_AutoStoryFieldSteps.h" #include "PokemonFRLG_AutoStoryGameArgs.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp index 89feedf1fc..eff1a3201e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp @@ -40,6 +40,7 @@ #include "PokemonFRLG_AutoStoryBattleTurns.h" #include "PokemonFRLG_AutoStoryEvolutionWait_Tests.h" #include "PokemonFRLG_AutoStoryItemUse.h" +#include "PokemonFRLG_AutoStoryRepelStep.h" #include "PokemonFRLG_AutoStoryMartPlan.h" #include "PokemonFRLG_AutoStoryNewMembers.h" #include "PokemonFRLG_AutoStorySlopeRun.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.cpp index 7f5ee8cf5f..ab32272d49 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.cpp @@ -12,6 +12,7 @@ #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.h" +#include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_MoneyBoxReader.h" #include "PokemonFRLG_AutoStoryActions.h" #include "PokemonFRLG_AutoStoryGameArgs.h" #include "PokemonFRLG_AutoStoryMart.h" @@ -256,9 +257,10 @@ namespace{ // One Mart step under way. class MartRun{ public: - MartRun(Logger& logger, MartActions& actions, const MartPlan& plan) + MartRun(Logger& logger, MartActions& actions, MartSellActions& sell, const MartPlan& plan) : m_logger(logger) , m_actions(actions) + , m_sell(sell) , m_plan(plan) , m_stock(mart_stock(plan.map)) , m_window(m_stock.size() + 1) @@ -309,7 +311,7 @@ class MartRun{ // Sells `sale`, facing the clerk before and after. None in the bag is // logged and passed. bool sell_one(const MartLine& sale){ - switch (m_actions.sell(sale)){ + switch (m_sell.sell(sale)){ case SaleEnd::SOLD:{ const size_t earned = sale.quantity * sale_price(sale.item).value_or(0); m_logger.log(std::format("Mart: sold {} {} for ${}.", sale.quantity, sale.item, earned)); @@ -413,6 +415,7 @@ class MartRun{ Logger& m_logger; MartActions& m_actions; + MartSellActions& m_sell; const MartPlan& m_plan; const std::vector& m_stock; // Where the cursor is, from the list's top row as open_buy_list() @@ -423,8 +426,8 @@ class MartRun{ } // namespace -MartResult run_mart(Logger& logger, MartActions& actions, const MartPlan& plan){ - return MartRun(logger, actions, plan).run(); +MartResult run_mart(Logger& logger, MartActions& actions, MartSellActions& sell, const MartPlan& plan){ + return MartRun(logger, actions, sell, plan).run(); } std::string mart_message(const MartPlan& plan, const MartResult& result){ @@ -524,6 +527,15 @@ bool ConsoleMartActions::leave(){ } } +std::optional ConsoleMartActions::read_money(){ + m_context.wait_for_all_requests(); + const VideoSnapshot screen = m_console.video().snapshot(); + if (!screen){ + return std::nullopt; + } + return read_money_box(m_console.logger(), screen); +} + void count_supplies(Supplies& supplies, std::string_view item, size_t quantity){ int Supplies::* count = nullptr; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.h index df6858ae99..56fb723650 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.h @@ -64,6 +64,7 @@ #include "PokemonFRLG_AutoStoryBattle.h" #include "PokemonFRLG_AutoStoryMartBuys.h" #include "PokemonFRLG_AutoStoryMartPlan.h" +#include "PokemonFRLG_AutoStoryMartSell.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -96,7 +97,8 @@ struct MartResult{ // overworld. The money box, read once on the BUY list, checks the sales // (it holds at least what they earned) and cuts an if_affordable line to // what it covers; unread, the buys go on blind. -MartResult run_mart(Logger& logger, MartActions& actions, const MartPlan& plan); +// The sales take their game steps from `sell`, the buys from `actions`. +MartResult run_mart(Logger& logger, MartActions& actions, MartSellActions& sell, const MartPlan& plan); // Formats a log message summarizing the Mart shopping result. std::string mart_message(const MartPlan& plan, const MartResult& result); @@ -108,8 +110,8 @@ std::string mart_message(const MartPlan& plan, const MartResult& result); void count_supplies(Supplies& supplies, std::string_view item, size_t quantity); -// The console's Mart. The money box and the quantity box have no reader -// yet, so the quantity is typed by presses. Potions, Antidotes, Parlyz Heals and Awakenings bought are +// The console's Mart's BUY list. The money box is read with +// read_money_box(); the quantity is typed by presses. Potions, Antidotes, Parlyz Heals and Awakenings bought are // counted into `supplies`. class ConsoleMartActions : public MartActions{ public: @@ -122,13 +124,8 @@ class ConsoleMartActions : public MartActions{ virtual PurchaseEnd buy(const MartBuy& buy) override; virtual bool leave() override; virtual std::optional read_money() override; - virtual SaleEnd sell(const MartLine& sale) override; private: - // From the bag: out to the overworld facing the clerk; `end`, or - // FAILED if the overworld didn't come back. - SaleEnd back_to_clerk(SaleEnd end); - ConsoleHandle& m_console; ProControllerContext& m_context; Language m_language; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartBuys.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartBuys.h index 3d951cc3c2..f7052a9b64 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartBuys.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartBuys.h @@ -64,15 +64,6 @@ enum class PurchaseEnd{ FAILED, }; -// How selling one line ended. -enum class SaleEnd{ - // "Turned over the and received $N.": back facing the clerk. - SOLD, - // The pocket has none of the item: back facing the clerk. - NONE_IN_BAG, - FAILED, -}; - // The game steps a Mart step takes, injected so its decisions run in // tests. class MartActions{ @@ -99,9 +90,6 @@ class MartActions{ virtual bool leave() = 0; // On the list: the money box's amount; nothing if it can't be read. virtual std::optional read_money() = 0; - // Facing the clerk: talk, SELL, `sale.quantity` of `sale.item` from - // its pocket, YES, out of the bag and the menu, facing the clerk again. - virtual SaleEnd sell(const MartLine& sale) = 0; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartPlan.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartPlan.h index 756ff52e4d..4a7fa8c971 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartPlan.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartPlan.h @@ -46,12 +46,6 @@ struct MartLine{ size_t quantity = 1; }; -// What the clerk pays for one `item` (half its pokefirered items.json -// price): the items the story sells (the NUGGET); nothing for another, -// or for one priced 0, which the clerk won't buy ("OH, no. I can't buy -// that.", as the MOON STONE). -std::optional sale_price(std::string_view item); - // What a Mart step sells, then buys: each line's quantity. struct MartPlan{ // The Mart's map, for its stock. @@ -63,8 +57,9 @@ struct MartPlan{ // and the step goes on, instead of failing. With the money read on // the BUY list, a line is cut to what the money covers instead. bool if_affordable = false; - // Sold at the same counter (SELL) before the buys, each from its - // pocket; a line the bag has none of is skipped, logged. + // Sold at the same counter (SELL, PokemonFRLG_AutoStoryMartSell.h) + // before the buys, each from its pocket; a line the bag has none of + // is skipped, logged. std::vector sells = {}; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartSell.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartSell.cpp index 099fa0a7ac..43bef71fb3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartSell.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartSell.cpp @@ -2,19 +2,17 @@ * * From: https://github.com/PokemonAutomation/ * - * The Mart's SELL and its money box on the console, and what the clerk - * pays. + * The Mart's SELL on the console, and what the clerk pays. * */ #include #include "CommonFramework/Exceptions/OperationFailedException.h" -#include "CommonFramework/VideoPipeline/VideoFeed.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" -#include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_MoneyBoxReader.h" #include "PokemonFRLG_AutoStoryPresses.h" #include "PokemonFRLG_AutoStoryBag.h" -#include "PokemonFRLG_AutoStoryMart.h" +#include "PokemonFRLG_AutoStoryMartBuys.h" +#include "PokemonFRLG_AutoStoryMartSell.h" #include "PokemonFRLG_AutoStoryPocketList.h" namespace PokemonAutomation{ @@ -58,16 +56,13 @@ std::optional sale_price(std::string_view item){ } -std::optional ConsoleMartActions::read_money(){ - m_context.wait_for_all_requests(); - const VideoSnapshot screen = m_console.video().snapshot(); - if (!screen){ - return std::nullopt; - } - return read_money_box(m_console.logger(), screen); -} +ConsoleMartSellActions::ConsoleMartSellActions(ConsoleHandle& console, ProControllerContext& context, Language language) + : m_console(console) + , m_context(context) + , m_language(language) +{} -SaleEnd ConsoleMartActions::sell(const MartLine& sale){ +SaleEnd ConsoleMartSellActions::sell(const MartLine& sale){ // "May I help you?" BUY, SELL, SEE YA!: SELL, and the bag slides in. // Not timed on the console: the BUY waits. pbf_press_button(m_context, BUTTON_A, 160ms, 2000ms); @@ -115,7 +110,7 @@ SaleEnd ConsoleMartActions::sell(const MartLine& sale){ return back_to_clerk(SaleEnd::SOLD); } -SaleEnd ConsoleMartActions::back_to_clerk(SaleEnd end){ +SaleEnd ConsoleMartSellActions::back_to_clerk(SaleEnd end){ // B out of the bag to the clerk's menu, B (SEE YA!) and through // "Please come again!". pbf_press_button(m_context, BUTTON_B, 160ms, 1500ms); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartSell.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartSell.h new file mode 100644 index 0000000000..041f0a9b56 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartSell.h @@ -0,0 +1,75 @@ +/* AutoStory Mart Sell + * + * From: https://github.com/PokemonAutomation/ + * + * What a MART step sells at the counter before it buys: what the clerk + * pays, and the SELL game step, injected so the step's decisions run in + * tests. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStoryMartSell_H +#define PokemonAutomation_PokemonFRLG_AutoStoryMartSell_H + +#include +#include +#include +#include "CommonFramework/Language.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "PokemonFRLG_AutoStoryMartPlan.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// What the clerk pays for one `item` (half its pokefirered items.json +// price): the items the story sells (the NUGGET); nothing for another, +// or for one priced 0, which the clerk won't buy ("OH, no. I can't buy +// that.", as the MOON STONE). +std::optional sale_price(std::string_view item); + +// How selling one line ended. +enum class SaleEnd{ + // "Turned over the and received $N.": back facing the clerk. + SOLD, + // The pocket has none of the item: back facing the clerk. + NONE_IN_BAG, + FAILED, +}; + +// The SELL game step a Mart step takes for each of its sales. +class MartSellActions{ +public: + virtual ~MartSellActions() = default; + + // Facing the clerk: talk, SELL, `sale.quantity` of `sale.item` from + // its pocket, YES, out of the bag and the menu, facing the clerk again. + virtual SaleEnd sell(const MartLine& sale) = 0; +}; + +// The console's SELL: the bag on its Items pocket, read with +// read_open_pocket(), the quantity typed as a purchase's. +class ConsoleMartSellActions : public MartSellActions{ +public: + ConsoleMartSellActions(ConsoleHandle& console, ProControllerContext& context, Language language); + + virtual SaleEnd sell(const MartLine& sale) override; + +private: + // From the bag: out to the overworld facing the clerk; `end`, or + // FAILED if the overworld didn't come back. + SaleEnd back_to_clerk(SaleEnd end); + + ConsoleHandle& m_console; + ProControllerContext& m_context; + Language m_language; +}; + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp index 5def9b5132..9b6a1ddc32 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp @@ -118,25 +118,6 @@ class FakeMart : public MartActions{ } return money_readable ? std::optional(money) : std::nullopt; } - // The clerk buys from `bag` at sale_price(), from facing the clerk. - virtual SaleEnd sell(const MartLine& sale) override{ - const auto held = bag.find(sale.item); - const std::optional price = sale_price(sale.item); - if (in_list || !price){ - error = in_list ? "SELL from the BUY list" : "sold an item the clerk won't buy"; - return SaleEnd::FAILED; - } - if (held == bag.end()){ - return SaleEnd::NONE_IN_BAG; - } - const size_t quantity = std::min(sale.quantity, held->second); - held->second -= quantity; - if (held->second == 0){ - bag.erase(held); - } - money += sales_unpaid ? 0 : quantity * *price; - return SaleEnd::SOLD; - } private: // One UP or DOWN on the list (the stock, then CANCEL), as pokefirered's @@ -188,6 +169,37 @@ class FakeMart : public MartActions{ size_t m_row = 0; }; +// The same Mart's SELL: the clerk buys from its `bag` at sale_price(), +// from facing the clerk. +class FakeMartSell : public MartSellActions{ +public: + explicit FakeMartSell(FakeMart& mart) + : m_mart(mart) + {} + + virtual SaleEnd sell(const MartLine& sale) override{ + const auto held = m_mart.bag.find(sale.item); + const std::optional price = sale_price(sale.item); + if (m_mart.in_list || !price){ + m_mart.error = m_mart.in_list ? "SELL from the BUY list" : "sold an item the clerk won't buy"; + return SaleEnd::FAILED; + } + if (held == m_mart.bag.end()){ + return SaleEnd::NONE_IN_BAG; + } + const size_t quantity = std::min(sale.quantity, held->second); + held->second -= quantity; + if (held->second == 0){ + m_mart.bag.erase(held); + } + m_mart.money += m_mart.sales_unpaid ? 0 : quantity * *price; + return SaleEnd::SOLD; + } + +private: + FakeMart& m_mart; +}; + struct AutoStoryMartInput{ std::string name; @@ -424,7 +436,8 @@ UnitTestResult check_mart_case(const MartCase& item){ if (item.setup){ item.setup(mart); } - const MartResult result = run_mart(logger, mart, item.plan); + FakeMartSell sell(mart); + const MartResult result = run_mart(logger, mart, sell, item.plan); if (!mart.error.empty()){ return item.name + ": " + mart.error; } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden19.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden19.h index 4ebda27e66..2ddbe3cfda 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden19.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden19.h @@ -19,8 +19,19 @@ namespace PokemonFRLG{ // The stored plan lines of 19.01a and after. inline constexpr std::string_view FRLG_PLAN_GOLDEN_19[] = { - "19.01a viridian_mart_clerk from ViridianCity_PokemonCenter_1F (7, 4) facing north to ViridianCity_Mart (4, 3) facing west", + "19.01a viridian_outside from ViridianCity_PokemonCenter_1F (7, 4) facing north to ViridianCity (26, 27) facing south", "19.01a walk south 5 (warp) -> ViridianCity (26, 27) [walk only]", + "19.01a lavender_mart_clerk from LavenderTown (6, 6) facing south to LavenderTown_Mart (4, 3) facing west", + "19.01a walk south 7 (to wall) -> LavenderTown (6, 13)", + "19.01a walk east 11 (to wall) -> LavenderTown (17, 13)", + "19.01a walk south 5 (to wall) -> LavenderTown (17, 18)", + "19.01a walk east 3 -> LavenderTown (20, 18)", + "19.01a walk north 3 (warp) -> LavenderTown_Mart (4, 7)", + "19.01a walk north 4 -> LavenderTown_Mart (4, 3) [walk only]", + "19.01a face west -> LavenderTown_Mart (4, 3) [walk only]", + "19.01a out_of_lavender_mart from LavenderTown_Mart (4, 3) facing west to LavenderTown (20, 16) facing south", + "19.01a walk south 5 (warp) -> LavenderTown (20, 16) [walk only]", + "19.01a viridian_mart_clerk from ViridianCity (26, 27) facing south to ViridianCity_Mart (4, 3) facing west", "19.01a walk south 2 -> ViridianCity (26, 29)", "19.01a walk east 10 -> ViridianCity (36, 29)", "19.01a walk north 10 (warp) -> ViridianCity_Mart (4, 7)", diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRepelStep.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRepelStep.h new file mode 100644 index 0000000000..4e65e136ad --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRepelStep.h @@ -0,0 +1,46 @@ +/* AutoStory Repel Step + * + * From: https://github.com/PokemonAutomation/ + * + * The use_repel step: the strongest Repel the bag holds, for the long + * walks of Route 23 and Victory Road, where the bag may hold any of the + * three. Defined with the other bag uses in + * PokemonFRLG_AutoStoryItemUse.cpp. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStoryRepelStep_H +#define PokemonAutomation_PokemonFRLG_AutoStoryRepelStep_H + +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" +#include "PokemonFRLG_AutoStoryBag.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The GAME step that uses the strongest Repel the bag holds +// (use_strongest_repel()). Args as a use_item step's "safari" and +// "if_any". +inline const std::string USE_REPEL_VARIANT = "use_repel"; + +// Uses the strongest Repel the bag holds: a Max Repel, else a Super +// Repel, else a Repel (a use_repel step). NONE_LEFT if it holds none. +ItemUseEnd use_strongest_repel(Logger& logger, ItemUseActions& actions); + +// The Repel that `step`, done, put on, for renew_repel() to renew when +// it wears off: a use_item step's Repel, or for a use_repel step the Max +// Repel, so that its renewals go strongest first whichever it used. +// Nothing for another step. +std::optional repel_put_on_by(const Pokemon::StoryStep& step); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif 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 41ab6fb672..7747d09b45 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp @@ -36,6 +36,7 @@ #include "PokemonFRLG_AutoStoryConsoleFailure_Tests.h" #include "PokemonFRLG_AutoStoryGrind.h" #include "PokemonFRLG_AutoStoryItemUse.h" +#include "PokemonFRLG_AutoStoryRepelStep.h" #include "PokemonFRLG_AutoStoryPcStorage.h" #include "PokemonFRLG_AutoStoryPlanGolden.h" #include "PokemonFRLG_AutoStoryPlanGolden10.h" diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index e8750681d5..cbc8b6efe9 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2275,6 +2275,7 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartBuys.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartPlan.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartSell.cpp + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartSell.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMember.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMember.h @@ -2314,6 +2315,7 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden19.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden20.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden13.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRepelStep.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryResources.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryResources.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryResources_Tests.cpp From 6114fded2967b53dfc2a059f6d9a155da8a4444f Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 23:14:22 -0800 Subject: [PATCH 3/5] FRLG 19.01b: the Route 22 rival is its own checkpoint; 19.01a shops at one Mart 19.01 walked from the Viridian Mart to Route 22 and fought the rival in the same checkpoint, so a retry replayed the walk. 19.01 now only walks to (34, 5), a tile short of the trigger, and 19.01b (level check, the step onto the trigger, the battle) starts from that save. 19.01a flew to Lavender's Mart, sold the Nugget for 6 Super Potions, then flew back into Viridian's Mart for an Antidote and 2 Parlyz Heals, which Lavender sells too. With ~$51k on the save, it now buys 15 Super Potions, the Antidote and the Parlyz Heals at Lavender in one step, no sale, and ends at Viridian's Fly landing, where 19.01 starts. Two step lints: a boss battle (a HIGH risk trainer, default_battle_risk()) has only in-place checks and its trigger before it in its checkpoint, with the twelve that still share listed until they are split; and a checkpoint never shops at two Marts when one the story visits stocks its whole list. --- .../PokemonFRLG_AutoStoryMart_Tests.cpp | 30 ++-- .../PokemonFRLG_AutoStoryPlanGolden19.h | 9 +- .../PokemonFRLG_AutoStoryRunPlan_Tests.cpp | 8 +- .../PokemonFRLG_AutoStoryStepLint_Tests.cpp | 153 ++++++++++++++++++ .../AutoStory/PokemonFRLG_AutoStoryTable.cpp | 2 +- .../PokemonFRLG_AutoStoryTable_Tests.cpp | 2 +- ...PokemonFRLG_AutoStoryVictoryRoad_Tests.cpp | 65 +++++--- .../AutoStory/PokemonFRLG_AutoStory_Tests.cpp | 4 +- .../AutoStory/StoryData/frlg-19.01.plan.json | 59 +------ .../AutoStory/StoryData/frlg-19.01a.plan.json | 40 ++--- .../AutoStory/StoryData/frlg-19.01b.plan.json | 73 +++++++++ 11 files changed, 304 insertions(+), 141 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.01b.plan.json diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp index 9b6a1ddc32..00c16707c8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp @@ -381,10 +381,10 @@ std::vector mart_cases_5(){ }; } -// 19.01a sells the Nugget ($5,000, half its 10,000) and buys Super -// Potions with it at Lavender's Mart: the money box, read on the BUY -// list, checks the sale and cuts the if_affordable line to what it -// covers; unread, the line is bought blind. +// A Nugget ($5,000, half its 10,000) sold for Super Potions at +// Lavender's Mart: the money box, read on the BUY list, checks the sale +// and cuts the if_affordable line to what it covers; unread, the line is +// bought blind. std::vector mart_cases_6(){ const auto nugget = [](FakeMart& mart){ mart.bag["nugget"] = 1; }; const auto nugget_read = [](FakeMart& mart){ mart.bag["nugget"] = 1; mart.money_readable = true; }; @@ -658,13 +658,12 @@ MartPlan one_line(std::string map, std::string item, size_t quantity){ } const std::vector& approved_shopping(){ - MartPlan status_heals = one_line("ViridianCity_Mart", "antidote", 1); - status_heals.lines.push_back({"parlyz-heal", 2}); + MartPlan rival_restock = one_line("LavenderTown_Mart", "super-potion", 15); + rival_restock.lines.push_back({"antidote", 1}); + rival_restock.lines.push_back({"parlyz-heal", 2}); + rival_restock.if_affordable = true; MartPlan full_heals = one_line("IndigoPlateau_PokemonCenter_1F", "full-heal", 2); full_heals.if_affordable = true; - MartPlan nugget_for_potions = one_line("LavenderTown_Mart", "super-potion", 6); - nugget_for_potions.if_affordable = true; - nugget_for_potions.sells = {{"nugget", 1}}; static const std::vector shopping{ { .checkpoint = "12.03", .step = "buy_super_potions_for_silph", .before = "16.01", @@ -677,15 +676,10 @@ const std::vector& approved_shopping(){ .money = 1900, .bag = {{"x-accuracy", 2}}, .money_after = 0, }, { - // The Nugget pays for all 6 with no money before. - .checkpoint = "19.01a", .step = "sell_nugget_buy_super_potions", .before = "19.01", - .plan = nugget_for_potions, - .money = 0, .bag = {{"super-potion", 6}}, .money_after = 800, .held = {{"nugget", 1}}, - }, - { - .checkpoint = "19.01a", .step = "buy_status_heals", .before = "19.01", - .plan = status_heals, - .money = 500, .bag = {{"antidote", 1}, {"parlyz-heal", 2}}, .money_after = 0, + // One Mart for the whole list: Lavender stocks all three. + .checkpoint = "19.01a", .step = "buy_potions_and_status_heals", .before = "19.01b", + .plan = rival_restock, + .money = 11000, .bag = {{"super-potion", 15}, {"antidote", 1}, {"parlyz-heal", 2}}, .money_after = 0, }, { // After 13 Full Restores, money for one Full Heal: skipped. diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden19.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden19.h index be77034614..972bafc5d6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden19.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden19.h @@ -31,14 +31,7 @@ inline constexpr std::string_view FRLG_PLAN_GOLDEN_19[] = { "19.01a face west -> LavenderTown_Mart (4, 3) [walk only]", "19.01a out_of_lavender_mart from LavenderTown_Mart (4, 3) facing west to LavenderTown (20, 16) facing south", "19.01a walk south 5 (warp) -> LavenderTown (20, 16) [walk only]", - "19.01a viridian_mart_clerk from ViridianCity (26, 27) facing south to ViridianCity_Mart (4, 3) facing west", - "19.01a walk south 2 -> ViridianCity (26, 29)", - "19.01a walk east 10 -> ViridianCity (36, 29)", - "19.01a walk north 10 (warp) -> ViridianCity_Mart (4, 7)", - "19.01a walk north 4 -> ViridianCity_Mart (4, 3) [walk only]", - "19.01a face west -> ViridianCity_Mart (4, 3) [walk only]", - "19.01 route22_east_of_trigger from ViridianCity_Mart (4, 3) facing west to Route22 (34, 5) facing west", - "19.01 walk south 5 (warp) -> ViridianCity (36, 20) [walk only]", + "19.01 route22_east_of_trigger from ViridianCity (26, 27) facing south to Route22 (34, 5) facing west", "19.01 walk west 16 -> ViridianCity (20, 20)", "19.01 walk north 1 -> ViridianCity (20, 19)", "19.01 walk west 26 (to wall) -> Route22 (42, 9) [connection 21]", 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 e0f4e8a237..49f90d2274 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 12.03 13.01 13.02 13.03 13.04 14.01 14.02 14.02b 14.03 14.04 14.04b 15.01 16.01 16.02 16.03 16.03b 16.05 16.05b 16.06 16.06b 18.01 18.02 18.02b 18.03 17.02 17.03 17.04 17.04b 19.01a 19.01 19.02 19.03 19.04 19.04b 19.04c 19.04d 19.05 20.01a 20.01 20.02 20.03 20.04 20.05"; +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 13.01 13.02 13.03 13.04 14.01 14.02 14.02b 14.03 14.04 14.04b 15.01 16.01 16.02 16.03 16.03b 16.05 16.05b 16.06 16.06b 18.01 18.02 18.02b 18.03 17.02 17.03 17.04 17.04b 19.01a 19.01 19.01b 19.02 19.03 19.04 19.04b 19.04c 19.04d 19.05 20.01a 20.01 20.02 20.03 20.04 20.05"; 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 12.03 13.01 13.02 13.03 13.04 14.01 14.02 14.02b 14.03 14.04 14.04b 15.01 16.01 16.02 16.03 16.03b 16.05 16.05b 16.06 16.06b 18.01 18.02 18.02b 18.03 17.02 17.03 17.04 17.04b 19.01a 19.01 19.02 19.03 19.04 19.04b 19.04c 19.04d 19.05 20.01a 20.01 20.02 20.03 20.04 20.05"}, + .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 13.01 13.02 13.03 13.04 14.01 14.02 14.02b 14.03 14.04 14.04b 15.01 16.01 16.02 16.03 16.03b 16.05 16.05b 16.06 16.06b 18.01 18.02 18.02b 18.03 17.02 17.03 17.04 17.04b 19.01a 19.01 19.01b 19.02 19.03 19.04 19.04b 19.04c 19.04d 19.05 20.01a 20.01 20.02 20.03 20.04 20.05"}, }, { .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 12.03 13.01 13.02 13.03 13.04 14.01 14.02 14.02b 14.03 14.04 14.04b 15.01 16.01 16.02 16.03 16.03b 16.05 16.05b 16.06 16.06b 18.01 18.02 18.02b 18.03 17.02 17.03 17.04 17.04b 19.01a 19.01 19.02 19.03 19.04 19.04b 19.04c 19.04d 19.05 20.01a 20.01 20.02 20.03 20.04 20.05"}, + .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 13.01 13.02 13.03 13.04 14.01 14.02 14.02b 14.03 14.04 14.04b 15.01 16.01 16.02 16.03 16.03b 16.05 16.05b 16.06 16.06b 18.01 18.02 18.02b 18.03 17.02 17.03 17.04 17.04b 19.01a 19.01 19.01b 19.02 19.03 19.04 19.04b 19.04c 19.04d 19.05 20.01a 20.01 20.02 20.03 20.04 20.05"}, }, { .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 12.03 13.01 13.02 13.03 13.04 14.01 14.02 14.02b 14.03 14.04 14.04b 15.01 16.01 16.02 16.03 16.03b 16.05 16.05b 16.06 16.06b 18.01 18.02 18.02b 18.03 17.02 17.03 17.04 17.04b 19.01a 19.01 19.02 19.03 19.04 19.04b 19.04c 19.04d 19.05 20.01a 20.01 20.02 20.03 20.04 20.05"}, + .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 13.01 13.02 13.03 13.04 14.01 14.02 14.02b 14.03 14.04 14.04b 15.01 16.01 16.02 16.03 16.03b 16.05 16.05b 16.06 16.06b 18.01 18.02 18.02b 18.03 17.02 17.03 17.04 17.04b 19.01a 19.01 19.01b 19.02 19.03 19.04 19.04b 19.04c 19.04d 19.05 20.01a 20.01 20.02 20.03 20.04 20.05"}, }, { .input = { .name = "Starter RNG first, with both on", diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepLint_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepLint_Tests.cpp index 046384d766..a659e73486 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepLint_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepLint_Tests.cpp @@ -8,16 +8,21 @@ * */ +#include #include +#include #include #include #include #include #include #include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" #include "Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.h" #include "Pokemon/AutoStory/Pokemon_TilePose.h" +#include "PokemonFRLG_AutoStoryBattleRisk.h" #include "PokemonFRLG_AutoStoryEvents.h" +#include "PokemonFRLG_AutoStoryMartPlan.h" #include "PokemonFRLG_AutoStoryStepLint_Tests.h" #include "PokemonFRLG_AutoStoryTable.h" #include "PokemonFRLG_AutoStoryUnauthoredStarts_Tests.h" @@ -164,6 +169,144 @@ UnitTestResult test_legs_need_a_reason(){ return true; } +// The boss battles that, on 2026-11-16, still share their checkpoint +// with the walk or errands before them, row/step. The user (2026-11-16, +// 19.01's Route 22 rival: "This fight also isn't its own checkpoint"): +// a boss battle is its own checkpoint, saved right before it, so a retry +// replays no walk. These are listed, not split yet, and leave the list as +// they are split. +const std::set>& bosses_sharing_before_2026_11_16(){ + static const std::set> BOSSES{ + "01.06/rival_battle", "03.02/brock", "05.02/misty", "06.01/rival_ss_anne", "09.02/giovanni", + "16.03/rival", "16.05/giovanni", "20.01/lorelei", "20.02/bruno", "20.03/agatha", "20.04/lance", + "20.05/champion", + }; + return BOSSES; +} + +// A step that leaves the player where they stand: a GAME action with no +// declared pose after it (a level check, a bag count, a teach), not a +// Fly, a walk, a scene or another battle. +bool stays_in_place(const StoryStep& step){ + return step.kind == StoryStepKind::GAME && !step.pose_after; +} + +// The first step of `row` before its battle `battle` that is not in +// place, the battle's trigger (a STEP or TALK right before it) aside; +// nothing if the battle starts its checkpoint. +std::optional walk_before_battle(const StoryCheckpoint& row, size_t battle){ + for (size_t c = 0; c < battle; c++){ + const StoryStep& step = row.steps[c]; + const bool trigger = c + 1 == battle && (step.kind == StoryStepKind::STEP || step.kind == StoryStepKind::TALK); + if (!trigger && !stays_in_place(step)){ + return step.name; + } + } + return std::nullopt; +} + +// A boss battle (the rival, gym leaders, Giovanni, the Elite Four and the +// Champion: the HIGH risk trainers, default_battle_risk()) is the first +// step of its checkpoint but in-place checks and its trigger, so the +// checkpoint's save is right before it; every listed one still shares. +UnitTestResult test_boss_battle_is_its_own_checkpoint(){ + std::set> listed_seen; + for (const StoryCheckpoint& row : story_checkpoints()){ + for (size_t c = 0; c < row.steps.size(); c++){ + const StoryStep& step = row.steps[c]; + if (step.kind != StoryStepKind::BATTLE){ + continue; + } + if (default_battle_risk(Pokemon::story_arg(step.args, "trainer")) != BattleRisk::HIGH){ + continue; + } + const std::optional walk = walk_before_battle(row, c); + const std::string key = std::format("{}/{}", row.id, step.name); + if (bosses_sharing_before_2026_11_16().contains(key)){ + if (!walk){ + return std::format("{} starts its checkpoint now: take it off bosses_sharing_before_2026_11_16().", key); + } + listed_seen.insert(key); + continue; + } + if (walk){ + return std::format("{}: a boss battle after {}; end the checkpoint before it, so its save is right before the battle.", key, *walk); + } + } + } + for (const std::string& key : bosses_sharing_before_2026_11_16()){ + if (!listed_seen.contains(key)){ + return std::format("{} is no boss battle now: take it off bosses_sharing_before_2026_11_16().", key); + } + } + return true; +} + +// The Mart steps' plans, each with its row. +struct RowMart{ + std::string_view row; + MartPlan plan; +}; +std::vector story_marts(){ + std::vector marts; + for (const StoryCheckpoint& row : story_checkpoints()){ + for (const StoryStep& step : row.steps){ + if (step.kind != StoryStepKind::GAME || step.action != MART_ACTION){ + continue; + } + if (const std::optional plan = mart_plan_of(step.args)){ + marts.push_back({row.id, *plan}); + } + } + } + return marts; +} + +// Whether the Mart on `map` sells every line of `lines`. +bool stocks_all(std::string_view map, const std::vector& lines){ + const std::vector& stock = mart_stock(map); + for (const MartLine& line : lines){ + const bool sold = std::any_of(stock.begin(), stock.end(), [&](const MartStockItem& item){ return item.item == line.item; }); + if (!sold){ + return false; + } + } + return true; +} + +// An errand (one checkpoint) never shops at two Marts when one Mart the +// story visits stocks its whole list: 19.01a flew from Lavender's Mart +// back to Viridian's for an Antidote and 2 Parlyz Heals, which Lavender +// sells too (user, 2026-11-16). +UnitTestResult test_errand_shops_at_one_mart(){ + const std::vector marts = story_marts(); + std::set> maps; + for (const RowMart& mart : marts){ + maps.insert(mart.plan.map); + } + std::map> by_row; + for (const RowMart& mart : marts){ + by_row[mart.row].push_back(&mart.plan); + } + for (const auto& [row, plans] : by_row){ + std::set visited; + std::vector lines; + for (const MartPlan* plan : plans){ + visited.insert(plan->map); + lines.insert(lines.end(), plan->lines.begin(), plan->lines.end()); + } + if (visited.size() < 2){ + continue; + } + for (const std::string& map : maps){ + if (stocks_all(map, lines)){ + return std::format("{}: shops at {} Marts, but {} stocks the whole list; buy it there in one step.", row, visited.size(), map); + } + } + } + return true; +} + struct StepLintInput{ UnitTestResult (*body)(); }; @@ -208,6 +351,16 @@ const StepLintCase STEP_LINT_CASES[] = { .input = {.body = test_legs_need_a_reason}, .expectation = {.description = "every walk a planned route, but the listed legs and those whose why says \"not planned: \""}, }, + { + .name = "a boss battle is its own checkpoint", + .input = {.body = test_boss_battle_is_its_own_checkpoint}, + .expectation = {.description = "only in-place checks and its trigger come before it in its checkpoint, but the listed ones"}, + }, + { + .name = "an errand shops at one Mart when one stocks its whole list", + .input = {.body = test_errand_shops_at_one_mart}, + .expectation = {.description = "no checkpoint visits two Marts for a list one Mart sells"}, + }, }; } // namespace diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.cpp index bad23e952d..4fe5b96926 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.cpp @@ -248,7 +248,7 @@ constexpr std::string_view STORY_ORDER[] = { "13.01", "13.02", "13.03", "13.04", "14.01", "14.02", "14.02b", "14.03", "14.04", "14.04b", "15.01", "16.01", "16.02", "16.03", "16.03b", "16.04", "16.05", "16.05b", "16.06", "16.06b", "18.01", "18.02", "18.02b", "18.03", - "17.02", "17.03", "17.04", "17.04b", "19.01a", "19.01", "19.02", "19.03", "19.04", "19.04b", "19.04c", "19.04d", "19.05", + "17.02", "17.03", "17.04", "17.04b", "19.01a", "19.01", "19.01b", "19.02", "19.03", "19.04", "19.04b", "19.04c", "19.04d", "19.05", "20.01a", "20.01", "20.02", "20.03", "20.04", "20.05", }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable_Tests.cpp index b7c2659ce5..7ff08e1967 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable_Tests.cpp @@ -37,7 +37,7 @@ const std::vector PINNED_ORDER = { "13.01", "13.02", "13.03", "13.04", "14.01", "14.02", "14.02b", "14.03", "14.04", "14.04b", "15.01", "16.01", "16.02", "16.03", "16.03b", "16.04", "16.05", "16.05b", "16.06", "16.06b", "18.01", "18.02", "18.02b", "18.03", - "17.02", "17.03", "17.04", "17.04b", "19.01a", "19.01", "19.02", "19.03", "19.04", "19.04b", "19.04c", "19.04d", "19.05", + "17.02", "17.03", "17.04", "17.04b", "19.01a", "19.01", "19.01b", "19.02", "19.03", "19.04", "19.04b", "19.04c", "19.04d", "19.05", "20.01a", "20.01", "20.02", "20.03", "20.04", "20.05", }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryVictoryRoad_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryVictoryRoad_Tests.cpp index caa4b485c4..7aeb6299b8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryVictoryRoad_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryVictoryRoad_Tests.cpp @@ -106,11 +106,11 @@ std::string walk_row( } // 19.01a: out to the street, the Fly to Lavender and its Mart's counter, -// back out, the Fly to Viridian and its Mart's counter, where 19.01 -// starts. 19.01: out -// of the Mart, along the street to Route 22 and along it to the rival's -// trigger column, then the step onto it. The rival is the battle step's, so -// the walk ends on the trigger tile (33, 5), facing west, with no trainer met. +// back out and the Fly back to Viridian, where 19.01 starts. 19.01: along +// the street to Route 22 and along it to (34, 5), a tile short of the +// rival's trigger column, where 19.01b starts, saved before his battle. +// 19.01b's step lands on the trigger tile (33, 5), facing west, with no +// trainer met on the way. UnitTestResult test_route22_rival_trigger(){ const StoryCheckpoint& shop = checkpoint("19.01a"); StoryPose at = *story_pose(Pokemon::pose_of(*shop.start)); @@ -120,31 +120,44 @@ UnitTestResult test_route22_rival_trigger(){ } StoryPose lavender = *story_pose(Pokemon::pose_of(*shop.start)); OverworldSim to_lavender(story_maps(), MapPosition{lavender.map, lavender.tile}, *lavender.facing); - if (const std::string failure = walk_row(to_lavender, lavender, shop, "sell_nugget_buy_super_potions"); !failure.empty()){ + if (const std::string failure = walk_row(to_lavender, lavender, shop, "buy_potions_and_status_heals"); !failure.empty()){ return failure; } const MapPosition lavender_counter{"LavenderTown_Mart", {4, 3}}; if (!(to_lavender.state().position == lavender_counter) || lavender.facing != Direction::WEST){ - return std::format("19.01a: sold and bought on {}, not facing Lavender's clerk from {}.", to_string(to_lavender.state().position), to_string(lavender_counter)); + return std::format("19.01a: bought on {}, not facing Lavender's clerk from {}.", to_string(to_lavender.state().position), to_string(lavender_counter)); } - const MapPosition counter{"ViridianCity_Mart", {4, 3}}; - if (!(sim.state().position == counter)){ - return std::format("19.01a: ended on {}, not the Mart's counter {}.", to_string(sim.state().position), to_string(counter)); - } - const StoryCheckpoint& row = checkpoint("19.01"); - const StoryPose next = *story_pose(Pokemon::pose_of(*row.start)); - if (next.map != counter.map || !(next.tile == counter.point)){ - return "19.01 doesn't start where 19.01a ends."; - } - if (const std::string failure = walk_row(sim, at, row, "rival_route22"); !failure.empty()){ - return failure; + const MapPosition landing{"ViridianCity", {26, 27}}; + if (!(sim.state().position == landing)){ + return std::format("19.01a: ended on {}, not the Fly's landing {}.", to_string(sim.state().position), to_string(landing)); } + const MapPosition east_of_trigger{"Route22", {34, 5}}; const MapPosition trigger{"Route22", {33, 5}}; - if (!(sim.state().position == trigger)){ - return std::format("19.01: ended on {}, not the rival's trigger {}.", to_string(sim.state().position), to_string(trigger)); + const struct{ + std::string_view id; + MapPosition start; + MapPosition end; + // The console's battle step the walk stops at, if any. + std::string_view until; + } legs[] = { + {.id = "19.01", .start = landing, .end = east_of_trigger, .until = {}}, + {.id = "19.01b", .start = east_of_trigger, .end = trigger, .until = "rival_route22"}, + }; + for (const auto& leg : legs){ + const StoryCheckpoint& row = checkpoint(leg.id); + const StoryPose start = *story_pose(Pokemon::pose_of(*row.start)); + if (start.map != leg.start.map || !(start.tile == leg.start.point)){ + return std::format("{} doesn't start on {}, where the row before it ends.", leg.id, to_string(leg.start)); + } + if (const std::string failure = walk_row(sim, at, row, leg.until); !failure.empty()){ + return failure; + } + if (!(sim.state().position == leg.end)){ + return std::format("{}: ended on {}, not {}.", leg.id, to_string(sim.state().position), to_string(leg.end)); + } } if (!sim.state().trainers_beaten.empty() || !sim.state().violations.empty()){ - return "19.01: met a trainer or broke a rule on the way."; + return "19.01 to 19.01b: met a trainer or broke a rule on the way."; } return true; } @@ -184,12 +197,12 @@ const StoryStep& step_of(std::string_view id, std::string_view name){ throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("No step {} in {}.", name, id)); } -// 19.01's rival rows are PokemonSimulation's planning_risk() for each foe +// 19.01b's rival rows are PokemonSimulation's planning_risk() for each foe // leading against the entry state of its route22-rival case // (src/pokesim/games/frlg/battle/player_strategy/planning.py): speed, the // 80th percentile loss, the mean, the worst case, and the worst normal hit. UnitTestResult test_rival_foe_threats_are_the_generators(){ - const StoryStep& rival = step_of("19.01", "rival_route22"); + const StoryStep& rival = step_of("19.01b", "rival_route22"); const auto found = rival.args.find("foe_threats"); const auto* rows = found == rival.args.end() ? nullptr : std::get_if>>(&found->second); const std::vector> expected = { @@ -201,7 +214,7 @@ UnitTestResult test_rival_foe_threats_are_the_generators(){ {105, 69, 20, 156, 78, 0, 0}, // Venusaur }; if (rows == nullptr || *rows != expected){ - return "19.01 rival_route22: foe_threats are not the generator's rows for the route22-rival case."; + return "19.01b rival_route22: foe_threats are not the generator's rows for the route22-rival case."; } return true; } @@ -361,7 +374,7 @@ UnitTestResult test_indigo_center_walk(){ const AutoStoryVictoryRoadCase AUTO_STORY_VICTORY_ROAD_CASES[] = { { - .input = {.name = "19.01a Lavender's and Viridian's Marts and 19.01 to the Route 22 rival's trigger", .body = test_route22_rival_trigger}, + .input = {.name = "19.01a Lavender's Mart, 19.01 to Route 22 and 19.01b onto the rival's trigger", .body = test_route22_rival_trigger}, .expectation = {.description = "Route 22 (33, 5), no trainer met"}, }, { @@ -369,7 +382,7 @@ const AutoStoryVictoryRoadCase AUTO_STORY_VICTORY_ROAD_CASES[] = { .expectation = {.description = "the trigger (7, 2) stepped onto, Route 23 (8, 153), no trainer met"}, }, { - .input = {.name = "19.01 the rival's foe rows are the generator's", .body = test_rival_foe_threats_are_the_generators}, + .input = {.name = "19.01b the rival's foe rows are the generator's", .body = test_rival_foe_threats_are_the_generators}, .expectation = {.description = "PokemonSimulation planning_risk() rows"}, }, { 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 7747d09b45..40b6eafb2e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp @@ -424,8 +424,8 @@ UnitTestResult check_checkpoint(const StoryCheckpoint& checkpoint, StoryPose& po // start: starts_after_unauthored_rows()), and adds the plans to `golden`. UnitTestResult chain_story_routes(std::vector& golden){ const std::vector& checkpoints = story_checkpoints(); - if (checkpoints.size() != 97 || checkpoints[0].start){ - return "Expected 97 checkpoints, the first from a new game."; + if (checkpoints.size() != 98 || checkpoints[0].start){ + return "Expected 98 checkpoints, the first from a new game."; } StoryPose pose; walk_state() = WalkGates{}; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.01.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.01.plan.json index 68715f1332..b2cbfcad59 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.01.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.01.plan.json @@ -3,19 +3,9 @@ "game": "frlg", "checkpoint": { "id": "19.01", - "title": "The rival on Route 22, at the road's east end", - "start": {"map": "ViridianCity_Mart", "tiles": [[4, 3]], "facing": "west"}, + "title": "Route 22, to the east end of the rival's road", + "start": {"map": "ViridianCity", "tiles": [[26, 27]], "facing": "south"}, "events": [ - { - "id": "lead_lv49_for_rival_route22", - "kind": "game_action", - "action": "level", - "args": { - "slot": 1, - "min": 49 - }, - "why": "the precondition for the Route 22 rival: the lead at least Lv49, pokeguy's Blastoise here (docs/battles/pokeguy-conditions.md route22-rival); 17.04's Giovanni needs Lv50" - }, { "id": "route22_east_of_trigger", "kind": "route", @@ -23,44 +13,7 @@ "args": { "target": {"map": "Route22", "tiles": [[34, 5]], "facing": "west"} }, - "why": "out of the Mart and west out of Viridian City onto Route 22, along row 5 to (34, 5), a tile short of the rival's trigger column (33, 4..6)" - }, - { - "id": "rival_trigger", - "kind": "step", - "args": {"direction": "west"}, - "declared_after": {"map": "Route22", "tiles": [[33, 5]], "facing": "west"}, - "why": "onto (33, 5): Route22_EventScript_LateRivalTriggerMid (VAR_MAP_SCENE_ROUTE22 is 3 once Giovanni is beaten, 17.04) puts the rival at (25, 5) and walks him seven tiles east to face the player" - }, - { - "id": "rival_route22", - "kind": "battle", - "args": { - "trainer": "TRAINER_RIVAL_ROUTE22_LATE_BULBASAUR", - "foes": [ - "pidgeot", - "rhyhorn", - "gyarados", - "growlithe", - "alakazam", - "venusaur" - ], - "policy": "goal-based-setup", - "setup_spa": 2, - "setup_accuracy": 2, - "setup_outspeed": true, - "setup_guard_accuracy": true, - "foe_threats": [ - [99, 29, 27, 60, 30, 0, 0], - [35, 46, 30, 126, 59, 0, 0], - [86, 15, 4, 36, 18, 0, 0], - [67, 0, 1, 50, 25, 0, 0], - [126, 55, 20, 120, 60, 0, 0], - [105, 69, 20, 156, 78, 0, 0] - ] - }, - "declared_after": {"map": "Route22", "tiles": [[33, 5]], "facing": "west"}, - "why": "the rival (TRAINER_RIVAL_ROUTE22_LATE_BULBASAUR for a player who took Squirtle): Pidgeot Lv47, Rhyhorn Lv45, Gyarados Lv45, Growlithe Lv45, Alakazam Lv47, Venusaur Lv53; fought by pokeguy's policy C2 (goal-based-setup: Sp. Atk +2 and Accuracy +2 by X Specials and X Accuracies, X Speed until faster, the doc's route22-rival row); foe_threats are the rows PokemonSimulation's player-strategy planning_risk() gives for each foe leading against the entry state (153/153, the doc's route22-rival case: the 80th percentile of the foe's move choice, its mean, its worst case, and its worst normal hit), one row per foe: Venusaur's worst case (crit Razor Leaf) is 156 of 153 HP, Rhyhorn's 126. Then he walks off and the player stays facing west" + "why": "west out of Viridian City onto Route 22, along row 5 to (34, 5), a tile short of the rival's trigger column (33, 4..6), which 19.01b, saved here, steps onto" } ] }, @@ -68,15 +21,15 @@ "menu": "full", "notes": { "checkpoint": [ - "Starts in the Viridian Mart where 19.01a ends, with the potions bought. The rival's trigger needs VAR_MAP_SCENE_ROUTE22 at 3, which Giovanni's defeat sets (ViridianCity_Gym scripts), so the row can not be reached by hand before the Earth Badge. PokeGuy has this fight at 1:46 (docs/battles/pokeguy-conditions.md, route22-rival) at 153/153 HP; the Center heal in 17.04b is that entry.", - "Ends on Route 22 at (33, 5) facing west, saved there (the battle saves after it): 19.02 walks on to the north gate (8, 5) and Route 23." + "Starts below Viridian's Pokemon Center, where 19.01a's Fly lands. Walks only: the rival is its own checkpoint (19.01b), saved a step short of his trigger, so a retry of the battle replays no walk.", + "Ends on Route 22 at (34, 5) facing west, a tile east of the rival's trigger column, where 19.01b starts." ] } }, "metadata": { "phase": "testing", "postconditions": { - "position": {"map": "Route22", "tiles": [[33, 5]], "facing": "west"} + "position": {"map": "Route22", "tiles": [[34, 5]], "facing": "west"} } } } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.01a.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.01a.plan.json index c08d3306d8..259cbea236 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.01a.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.01a.plan.json @@ -3,7 +3,7 @@ "game": "frlg", "checkpoint": { "id": "19.01a", - "title": "Restock before the Route 22 rival: the Nugget for Super Potions at Lavender's Mart, status heals at Viridian's", + "title": "Restock before the Route 22 rival: Super Potions and status heals at Lavender's Mart", "start": { "map": "ViridianCity_PokemonCenter_1F", "tiles": [[7, 4]], @@ -37,16 +37,19 @@ "why": "along the street to the Mart's door (20, 15), in and up to face the clerk (2, 3) over the counter (3, 3), as 08.03" }, { - "id": "sell_nugget_buy_super_potions", + "id": "buy_potions_and_status_heals", "kind": "game_action", "action": "mart", "args": { "map": "LavenderTown_Mart", - "sell": [{"item": "nugget", "quantity": 1}], - "items": [{"item": "super-potion", "quantity": 6}], + "items": [ + {"item": "super-potion", "quantity": 15}, + {"item": "antidote", "quantity": 1}, + {"item": "parlyz-heal", "quantity": 2} + ], "if_affordable": true }, - "why": "the Nugget sold ($5,000, half its 10,000) pays for 6 Super Potions (4,200) whatever the money was: the bag at 17.04b holds potion x3 and super-potion x0 (console B, 2026-11-15 22:10) against PokeGuy's potion x4, superpotion x4 at the Route 22 rival (PokemonSimulation docs/battles/pokeguy-conditions.md). The 2 Moon Stones stay: the clerk pays nothing for them (price 0, \"OH, no. I can't buy that.\"). The money box, read on the BUY list, checks the sale and cuts the line to what it covers; unread, the 6 are bought blind, which the Nugget alone covers" + "why": "the errand's whole list at one Mart: Lavender stocks the Super Potion (700), the Antidote (100) and the Parlyz Heal (200), so no second Mart is visited (this row once flew back to Viridian's Mart for the status heals alone). The bag at 17.04b holds potion x3 and super-potion x0 (console B, 2026-11-15 22:10) against PokeGuy's potion x4, superpotion x4 at the Route 22 rival and his antidote x1, parlyzheal x2 for the League (PokemonSimulation docs/battles/pokeguy-conditions.md; user's decision 2026-11-15); recovery_items() cures PSN and PAR with them. 11,000 in all from the ~$51,000 the save holds, so nothing is sold; if_affordable cuts a line to what a smaller purse covers" }, { "id": "out_of_lavender_mart", @@ -63,26 +66,7 @@ "action": "fly", "args": {"town": "ViridianCity", "species": "pidgey"}, "declared_after": {"map": "ViridianCity", "tiles": [[26, 27]], "facing": "south"}, - "why": "the Pidgey flies back to Viridian City, landing below the Pokemon Center's door (26, 26), as 17.04b" - }, - { - "id": "viridian_mart_clerk", - "kind": "route", - "action": "run", - "args": { - "target": {"map": "ViridianCity_Mart", "tiles": [[4, 3]], "facing": "west"} - }, - "why": "along the street to the Mart's door, in and up to the counter, facing the clerk (as 02.03), where 19.01 starts" - }, - { - "id": "buy_status_heals", - "kind": "game_action", - "action": "mart", - "args": { - "map": "ViridianCity_Mart", - "items": [{"item": "antidote", "quantity": 1}, {"item": "parlyz-heal", "quantity": 2}] - }, - "why": "an Antidote and 2 Parlyz Heals for the League's poison and paralysis (user's decision 2026-11-15, from the shopping audit): PokeGuy's bag at the Route 22 rival and at Lorelei holds antidote x1 and parlyzheal x2 (PokemonSimulation docs/battles/pokeguy-conditions.md), which the story's bag never bought for the League; recovery_items() cures PSN and PAR with them. Viridian sells both (ANTIDOTE 100, PARLYZ HEAL 200: 500 in all), paid from what the Nugget left (800 at least). The Potions this row used to fill to 4 are left out: the 6 Super Potions replace them" + "why": "the Pidgey flies back to Viridian City, landing below the Pokemon Center's door (26, 26), as 17.04b, where 19.01 starts its walk to Route 22" } ] }, @@ -90,15 +74,15 @@ "menu": "full", "notes": { "checkpoint": [ - "Starts at Viridian's nurse, where 17.04b ends. Flies to Lavender for the Super Potions, back to Viridian for the status heals, so that 19.01 starts in the Viridian Mart with the bag as the rival needs it.", - "Ends in the Viridian Mart at (4, 3) facing the clerk, where 19.01 starts." + "Starts at Viridian's nurse, where 17.04b ends. Flies to Lavender for the whole list at its one Mart, then back to Viridian.", + "Ends below Viridian's Pokemon Center at (26, 27) facing south, the Fly's landing, where 19.01 starts." ] } }, "metadata": { "phase": "testing", "postconditions": { - "position": {"map": "ViridianCity_Mart", "tiles": [[4, 3]], "facing": "west"} + "position": {"map": "ViridianCity", "tiles": [[26, 27]], "facing": "south"} } } } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.01b.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.01b.plan.json new file mode 100644 index 0000000000..c65a50d944 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-19.01b.plan.json @@ -0,0 +1,73 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "frlg", + "checkpoint": { + "id": "19.01b", + "title": "The rival on Route 22, at the road's east end", + "start": {"map": "Route22", "tiles": [[34, 5]], "facing": "west"}, + "events": [ + { + "id": "lead_lv49_for_rival_route22", + "kind": "game_action", + "action": "level", + "args": { + "slot": 1, + "min": 49 + }, + "why": "the precondition for the Route 22 rival: the lead at least Lv49, pokeguy's Blastoise here (docs/battles/pokeguy-conditions.md route22-rival); 17.04's Giovanni needs Lv50" + }, + { + "id": "rival_trigger", + "kind": "step", + "args": {"direction": "west"}, + "declared_after": {"map": "Route22", "tiles": [[33, 5]], "facing": "west"}, + "why": "onto (33, 5): Route22_EventScript_LateRivalTriggerMid (VAR_MAP_SCENE_ROUTE22 is 3 once Giovanni is beaten, 17.04) puts the rival at (25, 5) and walks him seven tiles east to face the player" + }, + { + "id": "rival_route22", + "kind": "battle", + "args": { + "trainer": "TRAINER_RIVAL_ROUTE22_LATE_BULBASAUR", + "foes": [ + "pidgeot", + "rhyhorn", + "gyarados", + "growlithe", + "alakazam", + "venusaur" + ], + "policy": "goal-based-setup", + "setup_spa": 2, + "setup_accuracy": 2, + "setup_outspeed": true, + "setup_guard_accuracy": true, + "foe_threats": [ + [99, 29, 27, 60, 30, 0, 0], + [35, 46, 30, 126, 59, 0, 0], + [86, 15, 4, 36, 18, 0, 0], + [67, 0, 1, 50, 25, 0, 0], + [126, 55, 20, 120, 60, 0, 0], + [105, 69, 20, 156, 78, 0, 0] + ] + }, + "declared_after": {"map": "Route22", "tiles": [[33, 5]], "facing": "west"}, + "why": "the rival (TRAINER_RIVAL_ROUTE22_LATE_BULBASAUR for a player who took Squirtle): Pidgeot Lv47, Rhyhorn Lv45, Gyarados Lv45, Growlithe Lv45, Alakazam Lv47, Venusaur Lv53; fought by pokeguy's policy C2 (goal-based-setup: Sp. Atk +2 and Accuracy +2 by X Specials and X Accuracies, X Speed until faster, the doc's route22-rival row); foe_threats are the rows PokemonSimulation's player-strategy planning_risk() gives for each foe leading against the entry state (153/153, the doc's route22-rival case: the 80th percentile of the foe's move choice, its mean, its worst case, and its worst normal hit), one row per foe: Venusaur's worst case (crit Razor Leaf) is 156 of 153 HP, Rhyhorn's 126. Then he walks off and the player stays facing west" + } + ] + }, + "execution": { + "menu": "full", + "notes": { + "checkpoint": [ + "Starts on Route 22 at (34, 5) facing west, where 19.01 ends, saved a step short of the rival's trigger: a boss battle is its own checkpoint. The trigger needs VAR_MAP_SCENE_ROUTE22 at 3, which Giovanni's defeat sets (ViridianCity_Gym scripts), so the row can not be reached by hand before the Earth Badge. PokeGuy has this fight at 1:46 (docs/battles/pokeguy-conditions.md, route22-rival) at 153/153 HP; the Center heal in 17.04b is that entry.", + "Ends on Route 22 at (33, 5) facing west, saved there (the battle saves after it): 19.02 walks on to the north gate (8, 5) and Route 23." + ] + } + }, + "metadata": { + "phase": "testing", + "postconditions": { + "position": {"map": "Route22", "tiles": [[33, 5]], "facing": "west"} + } + } +} From 96a83ee31706e79b62236ad0f4697c728946c09c Mon Sep 17 00:00:00 2001 From: dayfine Date: Mon, 16 Nov 2026 00:36:14 -0800 Subject: [PATCH 4/5] FRLG save card: one table of Kanto places for the reader and AutoStory 19.04 ended in Victory Road and its save was refused: the card read "VICTORY ROAO", but the reader's dictionary had no Victory Road, while AutoStory's save_card_place() did. Both now read one table in the save card reader, with every Kanto map section from pokefirered's region_map_sections.json (52 places). The Elite Four's rooms are the POKeMON LEAGUE on the card, not Indigo Plateau. Tests: every story checkpoint's start and end map has a place; the reader matches every place's card text and "VICTORY ROAO". --- .../Menus/PokemonFRLG_SaveCardReader.cpp | 166 ++++++++++++++---- .../Menus/PokemonFRLG_SaveCardReader.h | 23 ++- .../AutoStory/PokemonFRLG_AutoStoryEvents.cpp | 47 +---- .../PokemonFRLG_AutoStoryRunPlan_Tests.cpp | 4 +- .../PokemonFRLG_AutoStorySaveChecks.cpp | 9 +- .../PokemonFRLG_AutoStorySaveChecks.h | 7 +- .../AutoStory/PokemonFRLG_AutoStory_Tests.cpp | 63 ++++--- 7 files changed, 204 insertions(+), 115 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp index aa2658e204..667030e84e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp @@ -4,6 +4,8 @@ * */ +#include +#include #include #include #include "Common/Cpp/Color.h" @@ -25,6 +27,75 @@ namespace PokemonFRLG{ namespace{ +// A place the save card can name: the pokefirered map name its maps start +// with ("Route4" for "Route4" and "Route4_PokemonCenter_1F"), the slug +// read_save_card_location() returns, and the card's text in English (the +// region map section's name in pokefirered's region_map_sections.json). +struct SaveCardPlace{ + std::string_view map_prefix; + std::string_view slug; + std::string_view card_name; +}; + +// Every Kanto place: the save card's names and the maps they are on come +// from this one table, so a place the story saves in is one the reader +// knows (19.04 saved in Victory Road, which the reader didn't have: +// console B, 2026-11-15). +constexpr SaveCardPlace SAVE_CARD_PLACES[] = { + {"PalletTown", "pallet-town", "Pallet Town"}, + {"ViridianCity", "viridian-city", "Viridian City"}, + {"PewterCity", "pewter-city", "Pewter City"}, + {"CeruleanCity", "cerulean-city", "Cerulean City"}, + {"LavenderTown", "lavender-town", "Lavender Town"}, + {"VermilionCity", "vermilion-city", "Vermilion City"}, + {"CeladonCity", "celadon-city", "Celadon City"}, + {"FuchsiaCity", "fuchsia-city", "Fuchsia City"}, + {"CinnabarIsland", "cinnabar-island", "Cinnabar Island"}, + {"IndigoPlateau", "indigo-plateau", "Indigo Plateau"}, + {"SaffronCity", "saffron-city", "Saffron City"}, + {"Route1", "route-1", "Route 1"}, + {"Route2", "route-2", "Route 2"}, + {"Route3", "route-3", "Route 3"}, + {"Route4", "route-4", "Route 4"}, + {"Route5", "route-5", "Route 5"}, + {"Route6", "route-6", "Route 6"}, + {"Route7", "route-7", "Route 7"}, + {"Route8", "route-8", "Route 8"}, + {"Route9", "route-9", "Route 9"}, + {"Route10", "route-10", "Route 10"}, + {"Route11", "route-11", "Route 11"}, + {"Route12", "route-12", "Route 12"}, + {"Route13", "route-13", "Route 13"}, + {"Route14", "route-14", "Route 14"}, + {"Route15", "route-15", "Route 15"}, + {"Route16", "route-16", "Route 16"}, + {"Route17", "route-17", "Route 17"}, + {"Route18", "route-18", "Route 18"}, + {"Route19", "route-19", "Route 19"}, + {"Route20", "route-20", "Route 20"}, + {"Route21", "route-21", "Route 21"}, + {"Route22", "route-22", "Route 22"}, + {"Route23", "route-23", "Route 23"}, + {"Route24", "route-24", "Route 24"}, + {"Route25", "route-25", "Route 25"}, + {"ViridianForest", "viridian-forest", "Viridian Forest"}, + {"MtMoon", "mt-moon", "Mt. Moon"}, + {"SSAnne", "ss-anne", "S.S. Anne"}, + {"UndergroundPath", "underground-path", "Underground Path"}, + {"DiglettsCave", "digletts-cave", "Diglett's Cave"}, + {"VictoryRoad", "victory-road", "Victory Road"}, + {"RocketHideout", "rocket-hideout", "Rocket Hideout"}, + {"SilphCo", "silph-co", "Silph Co."}, + {"PokemonMansion", "pokemon-mansion", "Pokemon Mansion"}, + {"SafariZone", "safari-zone", "Safari Zone"}, + {"PokemonLeague", "pokemon-league", "Pokemon League"}, + {"RockTunnel", "rock-tunnel", "Rock Tunnel"}, + {"SeafoamIslands", "seafoam-islands", "Seafoam Islands"}, + {"PokemonTower", "pokemon-tower", "Pokemon Tower"}, + {"CeruleanCave", "cerulean-cave", "Cerulean Cave"}, + {"PowerPlant", "power-plant", "Power Plant"}, +}; + // The title's blue, about (30, 80, 225) on the capture. const std::vector& BLUE_TEXT_FILTERS(){ static const std::vector filters{ @@ -63,37 +134,10 @@ class PlaceNameMatcher : public OCR::SmallDictionaryMatcher{ static JsonObject dictionary(){ JsonObject english; - for (const auto& [slug, name] : { - std::pair{"pallet-town", "Pallet Town"}, - {"viridian-city", "Viridian City"}, - {"viridian-forest", "Viridian Forest"}, - {"pewter-city", "Pewter City"}, - {"route-1", "Route 1"}, - {"route-2", "Route 2"}, - {"route-3", "Route 3"}, - {"route-4", "Route 4"}, - {"cerulean-city", "Cerulean City"}, - {"route-21", "Route 21"}, - {"route-22", "Route 22"}, - {"route-23", "Route 23"}, - {"route-24", "Route 24"}, - {"route-25", "Route 25"}, - {"route-10", "Route 10"}, - {"lavender-town", "Lavender Town"}, - {"vermilion-city", "Vermilion City"}, - {"celadon-city", "Celadon City"}, - {"rocket-hideout", "Rocket Hideout"}, - {"pokemon-tower", "Pokemon Tower"}, - {"route-16", "Route 16"}, - {"fuchsia-city", "Fuchsia City"}, - {"saffron-city", "Saffron City"}, - {"cinnabar-island", "Cinnabar Island"}, - {"silph-co", "Silph Co"}, - {"pokemon-mansion", "Pokemon Mansion"}, - }){ + for (const SaveCardPlace& place : SAVE_CARD_PLACES){ JsonArray names; - names.push_back(std::string(name)); - english[std::string(slug)] = std::move(names); + names.push_back(std::string(place.card_name)); + english[std::string(place.slug)] = std::move(names); } JsonObject json; json["eng"] = std::move(english); @@ -108,6 +152,17 @@ ImageFloatBox save_card_title_box(){ return ImageFloatBox(8.0 / 240, 9.0 / 160, 114.0 / 240, 13.0 / 160); } +std::optional save_card_place(std::string_view map){ + for (const SaveCardPlace& place : SAVE_CARD_PLACES){ + if (map.starts_with(place.map_prefix) + && (map.size() == place.map_prefix.size() || map[place.map_prefix.size()] == '_') + ){ + return std::string(place.slug); + } + } + return std::nullopt; +} + std::string read_save_card_location(Logger& logger, Language language, const ImageViewRGB32& screen){ const ImageViewRGB32 game = extract_box_reference(screen, GameSettings::instance().GAME_BOX); return PlaceNameMatcher::instance().read(logger, language, extract_box_reference(game, save_card_title_box())); @@ -145,9 +200,60 @@ class Test_SaveCard : public ImageUnitTest{ } }; +// OCR text off the card, matched against the places' names. +struct SaveCardTextCase{ + std::string_view name; + struct Input{ + std::string_view text; + } input; + struct Expectation{ + std::string_view slug; + } expectation; +}; + +const SaveCardTextCase SAVE_CARD_TEXT_CASES[] = { + // 19.04's card in Victory Road (console B, 2026-11-15). + {.name = "a misread D", .input = {.text = "VICTORY ROAO"}, .expectation = {.slug = "victory-road"}}, + {.name = "a one-digit route among two-digit ones", .input = {.text = "ROUTE 1"}, .expectation = {.slug = "route-1"}}, + {.name = "a two-digit route", .input = {.text = "ROUTE 22"}, .expectation = {.slug = "route-22"}}, +}; + +class Test_SaveCardText : public UnitTest{ +public: + Test_SaveCardText() + : UnitTest("PokemonFRLG::SaveCardReader - text") + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + auto best = [](std::string_view text) -> std::string{ + const OCR::StringMatchResult result = PlaceNameMatcher::instance().match_substring(Language::English, std::string(text)); + return result.results.empty() ? "" : result.results.begin()->second.token; + }; + for (const SaveCardTextCase& c : SAVE_CARD_TEXT_CASES){ + const std::string read = best(c.input.text); + if (read != c.expectation.slug){ + return std::format("{}: \"{}\" read as \"{}\", expected {}.", c.name, c.input.text, read, c.expectation.slug); + } + } + // Every place a save can be in is one the card's text is read as. + for (const SaveCardPlace& place : SAVE_CARD_PLACES){ + std::string upper(place.card_name); + for (char& ch : upper){ + ch = (char)std::toupper((unsigned char)ch); + } + const std::string read = best(upper); + if (read != place.slug){ + return std::format("\"{}\" read as \"{}\", expected {}.", upper, read, place.slug); + } + } + return true; + } +}; + } // namespace void add_tests_SaveCardReader(UnitTestDatabase& database){ + database.add(); // FireRed on console B, 2026-11-02. The card covers the same corner of // the screen everywhere; the places behind it differ. database.add("PokemonFRLG/SaveCardReader/Bedroom_eng_pallet-town.png"); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.h index 68e82bec51..948ed4627b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.h @@ -4,17 +4,18 @@ * * Reads where the game would be saved: the place name in blue at the top * of the card the START menu's SAVE shows ("PALLET TOWN" over PLAYER, - * BADGES and TIME). It knows the places from a new game to Route 4, - * Cerulean City, Route 24 and Route 25 (Bill's Sea Cottage is on it), in - * English only so far: in another language nothing is read. Route 3, - * Route 4, Cerulean City, Route 24 and Route 25 have no test capture yet. + * BADGES and TIME). It knows every Kanto place, in English only so far: + * in another language nothing is read. Only Pallet Town, Viridian Forest + * and Pewter City have a test capture yet. * */ #ifndef PokemonAutomation_PokemonFRLG_SaveCardReader_H #define PokemonAutomation_PokemonFRLG_SaveCardReader_H +#include #include +#include #include "Common/Cpp/Logging/AbstractLogger.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/ImageTools/ImageBoxes.h" @@ -31,13 +32,17 @@ namespace PokemonFRLG{ // spans x 31 to 96, y 11 to 19 of the 240x160 screen, centered. ImageFloatBox save_card_title_box(); -// The place on the save card: "pallet-town", "viridian-city", -// "viridian-forest", "pewter-city", "route-1" to "route-4", "cerulean-city", -// "route-10", "route-24", "route-25", "vermilion-city", -// "lavender-town"; empty if it can't be read, as -// in a language the reader has no names for. +// The place on the save card, as a slug ("pallet-town", "route-22", +// "victory-road"); empty if it can't be read, as in a language the reader +// has no names for. std::string read_save_card_location(Logger& logger, Language language, const ImageViewRGB32& screen); +// The slug read_save_card_location() returns for a save on `map`, a +// pokefirered map name: a place's map or a building in it +// ("Route4_PokemonCenter_1F" is on Route 4; "Route24" isn't on Route 2). +// Nullopt for a map outside Kanto. +std::optional save_card_place(std::string_view map); + // Registers save card reader unit tests. void add_tests_SaveCardReader(UnitTestDatabase& database); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp index 22ae865969..abe2c0f3d8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp @@ -341,51 +341,12 @@ std::optional story_pose(const Pokemon::Pose& pose){ return StoryPose{pose.map, tiles[0], facing_of(pose)}; } -std::string save_card_place(const std::string& map){ - // A map of the place, or a building in it ("Route4_PokemonCenter_1F" - // is on Route 4, "Route24" isn't on Route 2). - static const std::pair PLACES[] = { - {"PalletTown", "pallet-town"}, - {"ViridianForest", "viridian-forest"}, - {"ViridianCity", "viridian-city"}, - {"PewterCity", "pewter-city"}, - {"Route3", "route-3"}, - {"Route4", "route-4"}, - {"CeruleanCity", "cerulean-city"}, - {"Route24", "route-24"}, - {"Route25", "route-25"}, - {"Route21", "route-21"}, - {"Route22", "route-22"}, - {"Route23", "route-23"}, - {"VictoryRoad", "victory-road"}, - {"IndigoPlateau", "indigo-plateau"}, {"PokemonLeague", "indigo-plateau"}, - {"VermilionCity", "vermilion-city"}, - {"Route10", "route-10"}, - {"LavenderTown", "lavender-town"}, - {"CeladonCity", "celadon-city"}, - {"RocketHideout", "rocket-hideout"}, - {"PokemonTower", "pokemon-tower"}, - {"Route16", "route-16"}, - {"FuchsiaCity", "fuchsia-city"}, - {"SaffronCity", "saffron-city"}, - {"CinnabarIsland", "cinnabar-island"}, - {"SilphCo", "silph-co"}, - {"PokemonMansion", "pokemon-mansion"}, - }; - for (const auto& [prefix, place] : PLACES){ - if (map == prefix || map.rfind(prefix + "_", 0) == 0){ - return std::string(place); - } - } - return ""; -} - std::string save_place_error(const std::string& start_map, const std::string& place){ - const std::string expected = save_card_place(start_map); - if (expected.empty() || place.empty() || place == expected){ + const std::optional expected = save_card_place(start_map); + if (!expected || place.empty() || place == *expected){ return ""; } - return "The save is in " + place + ", but the checkpoint starts in " + expected + " (" + start_map + ")."; + return std::format("The save is in {}, but the checkpoint starts in {} ({}).", place, *expected, start_map); } std::string start_pose_mismatch(const Pokemon::Pose& start, const StoryPose& stands){ @@ -908,7 +869,7 @@ StoryRunPlan plan_story_run( std::string check_save_place(StoryEnv& env, const std::string& start_map){ - if (save_card_place(start_map).empty()){ + if (!save_card_place(start_map)){ return ""; } const std::string place = read_save_place(env.console, env.context, env.language); 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 49f90d2274..5fa1014d1f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRunPlan_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRunPlan_Tests.cpp @@ -478,7 +478,7 @@ UnitTestResult test_cerulean_rival_row(){ const StoryCheckpoint& rival = checkpoints[index]; const std::optional start = rival.start ? story_pose(Pokemon::pose_of(*rival.start)) : std::nullopt; if (!start || rival.live_start || start->map != "CeruleanCity" || !(start->tile == TilePos{23, 7}) - || save_card_place(start->map).empty() + || !save_card_place(start->map) ){ return "05.04: expected it to start from a save on (23, 7) in Cerulean City."; } @@ -542,7 +542,7 @@ UnitTestResult test_misty_rows(){ } const std::optional start = misty.start ? story_pose(Pokemon::pose_of(*misty.start)) : std::nullopt; if (!start || misty.live_start || start->map != "CeruleanCity_Gym" || !(start->tile == TilePos{9, 7}) - || save_card_place(start->map).empty() + || !save_card_place(start->map) ){ return "05.02: expected it to start from a save on (9, 7) in Cerulean Gym."; } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaveChecks.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaveChecks.cpp index c28c9ce9ba..b7f238964c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaveChecks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaveChecks.cpp @@ -4,6 +4,7 @@ * */ +#include #include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" #include "PokemonFRLG_AutoStorySaveChecks.h" #include "PokemonFRLG_AutoStoryTable.h" @@ -24,14 +25,14 @@ std::string checkpoint_end_map(const std::string& id){ } std::string save_end_error(const std::string& end_map, const std::string& place){ - const std::string expected = save_card_place(end_map); - if (expected.empty() || place == expected){ + const std::optional expected = save_card_place(end_map); + if (!expected || place == *expected){ return ""; } if (place.empty()){ - return "The save card's place can't be read, so the end in " + expected + " (" + end_map + ") isn't confirmed."; + return std::format("The save card's place can't be read, so the end in {} ({}) isn't confirmed.", *expected, end_map); } - return "The save card names " + place + ", but the checkpoint ends in " + expected + " (" + end_map + ")."; + return std::format("The save card names {}, but the checkpoint ends in {} ({}).", place, *expected, end_map); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaveChecks.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaveChecks.h index 9dcc1c87d2..4590b181b0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaveChecks.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaveChecks.h @@ -20,6 +20,7 @@ #include "Pokemon/AutoStory/Pokemon_StoryEvents.h" #include "Pokemon/AutoStory/Pokemon_StorySegments.h" #include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMap.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h" @@ -35,13 +36,9 @@ namespace NintendoSwitch{ namespace PokemonFRLG{ -// The place the save card names for a save on `map` (see -// read_save_card_location()), or empty for a map whose place isn't known. -std::string save_card_place(const std::string& map); - // Why a save whose card names `place` can't start a checkpoint that starts // on `start_map`, or empty if it can: the places match, the card couldn't -// be read (empty `place`), or the map has no known place. +// be read (empty `place`), or the map has no place (save_card_place()). std::string save_place_error(const std::string& start_map, const std::string& place); // The map checkpoint `id` ends on: where the table's next row starts. 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 40b6eafb2e..ea0f3c27de 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp @@ -1231,32 +1231,51 @@ UnitTestResult test_nugget_bridge_order(){ return true; } -// Every checkpoint that starts from a save has a place the save card -// names, so a save left elsewhere is caught before it runs; and the -// places of the starts that matter differ. +// Every checkpoint's start and end (the map it saves on) has a place the +// save card names, so a save left elsewhere is caught; 19.04 ended in +// Victory Road, a place the card reader didn't know (console B, +// 2026-11-15). The reader's own tests match every place's card text. UnitTestResult test_save_places(){ for (const StoryCheckpoint& checkpoint : story_checkpoints()){ - if (checkpoint.start && save_card_place(Pokemon::grid_pose_of(*checkpoint.start).map).empty()){ - return checkpoint.id + " starts on " + Pokemon::grid_pose_of(*checkpoint.start).map + ", which has no save card place."; + if (checkpoint.start){ + const std::string start = Pokemon::grid_pose_of(*checkpoint.start).map; + if (!save_card_place(start)){ + return std::format("{} starts on {}, which has no save card place.", checkpoint.id, start); + } + } + const std::string end = checkpoint_end_map(checkpoint.id); + if (!end.empty() && !save_card_place(end)){ + return std::format("{} ends on {}, which has no save card place.", checkpoint.id, end); } } - if (save_card_place("ViridianForest") != "viridian-forest" || save_card_place("ViridianCity_Mart") != "viridian-city" - || save_card_place("PewterCity_PokemonCenter_1F") != "pewter-city" || save_card_place("PalletTown_ProfessorOaksLab") != "pallet-town" - ){ - return "Wrong save card places."; - } - if (save_card_place("Route3") != "route-3" || save_card_place("Route4_PokemonCenter_1F") != "route-4"){ - return "Wrong save card places on Route 3 and 4."; - } - if (save_card_place("CeruleanCity_PokemonCenter_1F") != "cerulean-city"){ - return "Wrong save card place in Cerulean City."; - } - // Not Route 2's or Route 4's: a map name is a place's or a building's in it. - if (save_card_place("Route24") != "route-24" || save_card_place("Route25_SeaCottage") != "route-25"){ - return "Wrong save card places on Route 24 and 25."; - } - if (!save_card_place("Route9").empty() || !save_card_place("Route2").empty()){ - return "A place for a map the table doesn't use."; + struct Case{ + std::string_view name; + struct Input{ + std::string_view map; + } input; + struct Expectation{ + std::optional place; + } expectation; + }; + const Case cases[] = { + {.name = "a place's own map", .input = {.map = "ViridianForest"}, .expectation = {.place = "viridian-forest"}}, + {.name = "a building in a town", .input = {.map = "PewterCity_PokemonCenter_1F"}, .expectation = {.place = "pewter-city"}}, + {.name = "the Route 4 Center", .input = {.map = "Route4_PokemonCenter_1F"}, .expectation = {.place = "route-4"}}, + {.name = "a route whose number starts another's", .input = {.map = "Route24"}, .expectation = {.place = "route-24"}}, + {.name = "a building on a route", .input = {.map = "Route25_SeaCottage"}, .expectation = {.place = "route-25"}}, + {.name = "a cave floor", .input = {.map = "VictoryRoad_1F"}, .expectation = {.place = "victory-road"}}, + // The Elite Four's rooms are the POKeMON LEAGUE, not INDIGO PLATEAU. + {.name = "the League's rooms", .input = {.map = "PokemonLeague_LoreleisRoom"}, .expectation = {.place = "pokemon-league"}}, + {.name = "a Sevii island", .input = {.map = "OneIsland_PokemonCenter_1F"}, .expectation = {.place = std::nullopt}}, + }; + for (const Case& c : cases){ + const std::optional place = save_card_place(c.input.map); + if (place != c.expectation.place){ + return std::format( + "{}: {} has place {}, expected {}.", + c.name, c.input.map, place.value_or("none"), c.expectation.place.value_or("none") + ); + } } return true; } From edd144b46e9b93a67f66035bea36b5c2282481a4 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 17 Nov 2026 03:41:37 -0800 Subject: [PATCH 5/5] FRLG save card: no-place cases use a Sevii map, now that Route 9 has a place The one Kanto table (96a83ee31) gives Route 9 its place, so a save in pewter-city for a start or end on Route 9 is an error. The "no known place" cases use One Island's Center, which the table leaves out. --- .../Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 ea0f3c27de..b17b7410fc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp @@ -1295,8 +1295,8 @@ UnitTestResult test_save_place_error(){ // A card that can't be read is let pass. {.start_map = "PalletTown_PlayersHouse_2F", .place = "", .error = false}, {.start_map = "ViridianForest", .place = "viridian-city", .error = true}, - // A map with no known place isn't checked. - {.start_map = "Route9", .place = "pewter-city", .error = false}, + // A map with no known place (a Sevii island) isn't checked. + {.start_map = "OneIsland_PokemonCenter_1F", .place = "pewter-city", .error = false}, }; for (const Case& c : CASES){ const std::string error = save_place_error(c.start_map, c.place); @@ -1372,8 +1372,8 @@ UnitTestResult test_save_end_error(){ {.end_map = end, .place = "cinnabar-island", .error = false}, {.end_map = end, .place = "", .error = true}, {.end_map = "Route21_North", .place = "route-21", .error = false}, - // A map with no known place isn't checked. - {.end_map = "Route9", .place = "pewter-city", .error = false}, + // A map with no known place (a Sevii island) isn't checked. + {.end_map = "OneIsland_PokemonCenter_1F", .place = "pewter-city", .error = false}, {.end_map = "", .place = "route-21", .error = false}, }; for (const Case& c : CASES){