Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

constexpr int POTION_HP = 20;
constexpr int SUPER_POTION_HP = 50;
constexpr int HYPER_POTION_HP = 200;
// The player's HP bar's width in pixels: its fill is read to one.
constexpr int HP_BAR_PIXELS = 48;
// The most items heal_to_full() uses: more than a full bar takes.
Expand All @@ -38,6 +39,9 @@
if (item == "super-potion"){
return SUPER_POTION_HP;
}
if (item == "hyper-potion"){
return HYPER_POTION_HP;
}
return 0;
}

Expand Down Expand Up @@ -83,7 +87,7 @@
return missing - supplies.healed_since_measured;
}

std::optional<int> heal_to_full(

Check warning on line 90 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHealing.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::heal_to_full( Supplies & supplies , int fallback_max_hp , const std :: function<bool(const std::string&item)> & use , std :: string_view first_item): function_lines=33 (advisory above 25, limit 50)
Supplies& supplies, int fallback_max_hp, const std::function<bool(const std::string& item)>& use,
std::string_view first_item
){
Expand All @@ -96,8 +100,12 @@
if (!missing || *missing <= 0){
return missing;
}
// A Hyper Potion (counted when the bag read shows some) for more
// than a Super Potion heals; else the smallest that covers it.
std::string item;
if (*missing > POTION_HP && supplies.super_potions > 0){
if (*missing > SUPER_POTION_HP && bag_count(supplies, "hyper-potion") > 0){
item = "hyper-potion";
}else if (*missing > POTION_HP && supplies.super_potions > 0){
item = "super-potion";
}else if (supplies.potions > 0){
item = "potion";
Expand Down Expand Up @@ -286,6 +294,36 @@
return true;
}

// A Hyper Potion in the bag heals more than a Super Potion's 50 missing
// HP (user, 2026-11-15: the between-fight heal uses them if present);
// under that, the Super Potions as before, the Hyper Potion kept.
UnitTestResult test_hyper_potion(){

Check warning on line 300 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHealing.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::test_hyper_potion(): function_lines=26 (advisory above 25, limit 50)
Supplies supplies = bag(0, 2);
supplies.pocket.push_back("hyper-potion");
supplies.other_counts = {{"hyper-potion", 1}};
measured_hp(supplies, 0.30);
const std::optional<int> after = heal_to_full(supplies, LEAD_MAX_HP, [&](const std::string& item){
return use_from_bag(supplies, item);
});
if (after != std::optional<int>(-112) || supplies.other_counts.contains("hyper-potion") || supplies.super_potions != 2){
return std::format(
"{} HP missing, {} Hyper Potions, {} Super Potions; expected -112 (one Hyper Potion for 88), none, 2.",
after.value_or(-1), supplies.other_counts.contains("hyper-potion") ? supplies.other_counts["hyper-potion"] : 0, supplies.super_potions
);
}
Supplies small = bag(0, 2);
small.pocket.push_back("hyper-potion");
small.other_counts = {{"hyper-potion", 1}};
measured_hp(small, 0.80);
heal_to_full(small, LEAD_MAX_HP, [&](const std::string& item){
return use_from_bag(small, item);
});
if (small.other_counts["hyper-potion"] != 1 || small.super_potions != 1){
return std::format("27 HP missing: {} Hyper Potions and {} Super Potions left, expected 1 and 1.", small.other_counts["hyper-potion"], small.super_potions);
}
return true;
}

// The Fuchsia Mart buys Super Potions so the bag covers a full bar.
UnitTestResult test_cover_shortfall(){
struct Case{
Expand Down Expand Up @@ -317,6 +355,7 @@
database.add<Test_AutoStoryHealing>(HealingCase{"the Mart buys the shortfall for a full bar", test_cover_shortfall});
database.add<Test_AutoStoryHealing>(HealingCase{"a Full Restore after Agatha, Potions without one", test_full_restore_after_agatha});
database.add<Test_AutoStoryHealing>(HealingCase{"an Elixir after Lance: 10 PP each, at most full", test_elixir_after_lance});
database.add<Test_AutoStoryHealing>(HealingCase{"a Hyper Potion for more than a Super Potion heals", test_hyper_potion});
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* AutoStory Item Use Tests

Check warning on line 1 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemUse_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=724 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -646,9 +646,9 @@

const SuperRepelCase SUPER_REPEL_CASES[] = {
{
.name = "12.03's Super Repel before the Fly to Pallet",
.name = "12.03's Super Repel after the Fuchsia Mart, before the Fly to Pallet",
.input = {.checkpoint = "12.03", .step = "super_repel_fuchsia"},
.expectation = {.previous = "", .next = "outside_fuchsia_center", .in_bag = 5},
.expectation = {.previous = "cut_mart_tree_back", .next = "fly_pallet", .in_bag = 5},
},
{
.name = "13.01's Super Repel after 3F's statue",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@
for (const MartLine& line : plan.lines){
items.push_back({line.item, line.quantity});
}
return {{"map", plan.map}, {"items", std::move(items)}};
Pokemon::StoryActionArgs args{{"map", plan.map}, {"items", std::move(items)}};
if (plan.if_affordable){
args["if_affordable"] = true;
}
return args;
}

std::optional<MartPlan> mart_plan_of(const Pokemon::StoryActionArgs& args){
Expand All @@ -184,7 +188,8 @@
if (map == nullptr || mart_stock(*map).empty() || items == nullptr || items->empty()){
return std::nullopt;
}
MartPlan plan{.map = *map};
const bool* if_affordable = game_arg<bool>(args, "if_affordable");
MartPlan plan{.map = *map, .if_affordable = if_affordable != nullptr && *if_affordable};
for (const Pokemon::StoryItem& item : *items){
if (item.quantity < 1 || item.quantity > MAX_QUANTITY || !stock_row(mart_stock(plan.map), item.item)){
return std::nullopt;
Expand Down Expand Up @@ -301,11 +306,17 @@
return false;
}

bool buy_one(const MartBuy& buy){

Check warning on line 309 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::MartRun::buy_one( const MartBuy & buy): function_lines=28 (advisory above 25, limit 50)
if (!reach(buy)){
return false;
}
switch (m_actions.buy(buy)){
const PurchaseEnd end = m_actions.buy(buy);
const bool short_of_money = end == PurchaseEnd::NOT_ENOUGH_MONEY || end == PurchaseEnd::WRONG_QUANTITY;
if (m_plan.if_affordable && short_of_money){
m_logger.log(std::format("Mart: the money doesn't cover {} {}; skipped (if_affordable).", buy.quantity, buy.item), COLOR_ORANGE);
return true;
}
switch (end){
case PurchaseEnd::BOUGHT:
m_logger.log(std::format("Mart: bought {} {} for ${}.", buy.quantity, buy.item, buy.cost));
m_result.bought.push_back(buy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
* 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 Mart step belongs early in its checkpoint, so
* 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.
*
* The decisions take their game steps from a MartActions, so they run in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ struct MartPlan{
std::string map = {};
// In priority order.
std::vector<MartLine> 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.
bool if_affordable = false;
};

// The GAME action of a Mart step: map WORD (required), the Mart's map;
// items ITEMS (required), the lines in order.
// items ITEMS (required), the lines in order; if_affordable BOOLEAN
// (absent: false), MartPlan::if_affordable.
inline const std::string MART_ACTION = "mart";
// `plan` as its action's args.
Pokemon::StoryActionArgs mart_args(const MartPlan& plan);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* AutoStory Mart Tests

Check warning on line 1 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=700 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand All @@ -14,6 +14,7 @@
#include <format>
#include <functional>
#include <map>
#include <optional>
#include <set>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -301,9 +302,47 @@
};
}

// 20.01a's Full Heals after the Full Restores, only if the money left
// covers them (user, 2026-11-15): a short purse skips the line, the step
// done, where without if_affordable it fails.
std::vector<MartCase> mart_cases_5(){

Check warning on line 308 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::mart_cases_5(): function_lines=34 (advisory above 25, limit 50)
const std::vector<MartLine> two_full_heals{{"full-heal", 2}};
const std::vector<MartLine> restore_then_heal{{"full-restore", 1}, {"full-heal", 1}};
const MartPlan full_heals{.map = "IndigoPlateau_PokemonCenter_1F", .lines = two_full_heals, .if_affordable = true};
const MartPlan restore_first{.map = "IndigoPlateau_PokemonCenter_1F", .lines = restore_then_heal, .if_affordable = true};
return {
{
.name = "if_affordable: both bought when the money covers them",
.plan = full_heals,
.money = 1500,
.bag = {{"full-heal", 2}},
.money_after = 300,
},
{
.name = "if_affordable: money for one, the quantity box wraps: skipped, done",
.plan = full_heals,
.money = 900,
.money_after = 900,
},
{
.name = "if_affordable: money for none: skipped, done",
.plan = full_heals,
.money = 400,
.money_after = 400,
},
{
.name = "if_affordable: a line skipped, the cheaper next one still bought",
.plan = restore_first,
.money = 700,
.bag = {{"full-heal", 1}},
.money_after = 100,
},
};
}

std::vector<MartCase> mart_cases(){
std::vector<MartCase> cases;
for (std::vector<MartCase> part : {mart_cases_1(), mart_cases_2(), mart_cases_3(), mart_cases_4()}){
for (std::vector<MartCase> part : {mart_cases_1(), mart_cases_2(), mart_cases_3(), mart_cases_4(), mart_cases_5()}){
cases.insert(cases.end(), part.begin(), part.end());
}
return cases;
Expand Down Expand Up @@ -358,7 +397,7 @@
}


UnitTestResult test_mart_args(){

Check warning on line 400 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::test_mart_args(): function_lines=31 (advisory above 25, limit 50)
const MartPlan plan = pewter_plan({{"potion", 5}, {"repel", 3}});
const Pokemon::StoryActionArgs args = mart_args(plan);
const std::optional<MartPlan> back = mart_plan_of(args);
Expand All @@ -367,6 +406,12 @@
){
return "The Pewter plan didn't read back from its args.";
}
MartPlan if_affordable = plan;
if_affordable.if_affordable = true;
const std::optional<MartPlan> affordable_back = mart_plan_of(mart_args(if_affordable));
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.";
}
const std::vector<std::pair<std::string, Pokemon::StoryArgValue>> bad_args{
{"items", std::vector<Pokemon::StoryItem>{}},
{"map", std::string("Nowhere_Mart")},
Expand Down Expand Up @@ -502,6 +547,108 @@
return true;
}

// One Mart step of the shopping the user approved on 2026-11-15, with
// the story row it must come before and the money it is run against.
struct ApprovedShopping{
std::string_view checkpoint;
std::string_view step;
std::string_view before;
MartPlan plan;
size_t money;
std::map<std::string, size_t> bag;
size_t money_after;
};

// A plan of one line: `quantity` of `item` at `map`'s Mart.
MartPlan one_line(std::string map, std::string item, size_t quantity){
return {.map = std::move(map), .lines = {MartLine{std::move(item), quantity}}};
}

const std::vector<ApprovedShopping>& approved_shopping(){

Check warning on line 567 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::approved_shopping(): function_lines=30 (advisory above 25, limit 50)
MartPlan status_heals = one_line("ViridianCity_Mart", "antidote", 1);
status_heals.lines.push_back({"parlyz-heal", 2});
MartPlan full_heals = one_line("IndigoPlateau_PokemonCenter_1F", "full-heal", 2);
full_heals.if_affordable = true;
static const std::vector<ApprovedShopping> shopping{
{
.checkpoint = "12.03", .step = "buy_super_potions_for_silph", .before = "16.01",
.plan = one_line("FuchsiaCity_Mart", "super-potion", 4),
.money = 2800, .bag = {{"super-potion", 4}}, .money_after = 0,
},
{
.checkpoint = "18.03", .step = "buy_x_accuracies", .before = "19.01",
.plan = one_line("CeladonCity_DepartmentStore_5F", "x-accuracy", 2),
.money = 1900, .bag = {{"x-accuracy", 2}}, .money_after = 0,
},
{
.checkpoint = "19.01a", .step = "buy_status_heals", .before = "19.01",
.plan = status_heals,
.money = 500, .bag = {{"antidote", 1}, {"parlyz-heal", 2}}, .money_after = 0,
},
{
// After 13 Full Restores, money for one Full Heal: skipped.
.checkpoint = "20.01a", .step = "buy_full_heals", .before = "20.01",
.plan = full_heals,
.money = 1100, .bag = {}, .money_after = 1100,
},
};
return 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()){
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;
}
}
return true;
}

// Each approved Mart step is in its row, before the row it shops for,
// with the approved lines; on a fake Mart with the money given it ends
// done with that bag.
UnitTestResult test_approved_shopping(){

Check warning on line 613 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::test_approved_shopping(): function_lines=38 (advisory above 25, limit 50)
const std::vector<StoryCheckpoint>& rows = story_checkpoints();
for (const ApprovedShopping& shopping : approved_shopping()){
std::optional<size_t> row_index;
std::optional<size_t> before_index;
const StoryStep* found = nullptr;
for (size_t c = 0; c < rows.size(); c++){
if (rows[c].id == shopping.before){
before_index = c;
}
if (rows[c].id != shopping.checkpoint){
continue;
}
row_index = c;
for (const StoryStep& step : rows[c].steps){
found = step.name == shopping.step ? &step : found;
}
}
if (found == nullptr || !row_index || !before_index || *row_index >= *before_index){
return std::format("{}/{}: missing, or not before {}.", shopping.checkpoint, shopping.step, shopping.before);
}
const std::optional<MartPlan> plan = mart_plan_of(found->args);
if (!plan || !same_plan(*plan, shopping.plan)){
return std::format("{}/{}: not the approved lines.", shopping.checkpoint, shopping.step);
}
const UnitTestResult result = check_mart_case({
.name = std::format("{}/{}", shopping.checkpoint, shopping.step),
.plan = *plan,
.money = shopping.money,
.bag = shopping.bag,
.money_after = shopping.money_after,
});
if (result.result != UnitTestResult::PASSED){
return result;
}
}
return true;
}

} // namespace


Expand Down Expand Up @@ -534,6 +681,10 @@
.input = {"every Mart step is early in its checkpoint", test_mart_steps_early},
.expectation = {},
},
{
.input = {"the shopping approved on 2026-11-15", test_approved_shopping},
.expectation = {},
},
};

void add_tests_AutoStoryMart(UnitTestDatabase& database){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,26 @@ inline constexpr std::string_view FRLG_PLAN_GOLDEN_12[] = {
"12.02b walk north 5 (to wall) -> FuchsiaCity (11, 32)",
"12.02b walk east 14 -> FuchsiaCity (25, 32)",
"12.02b face north -> FuchsiaCity (25, 32)",
"12.03 outside_fuchsia_center from FuchsiaCity_PokemonCenter_1F (7, 4) facing north to FuchsiaCity (25, 32) facing south",
"12.03 outside_fuchsia_center from FuchsiaCity_PokemonCenter_1F (7, 4) facing north to FuchsiaCity (27, 32) facing east",
"12.03 walk south 5 (warp) -> FuchsiaCity (25, 32) [walk only]",
"12.03 walk east 2 -> FuchsiaCity (27, 32)",
"12.03 below_mart_tree from FuchsiaCity (29, 32) facing east to FuchsiaCity (24, 23) facing north",
"12.03 walk north 9 -> FuchsiaCity (29, 23)",
"12.03 walk west 5 -> FuchsiaCity (24, 23)",
"12.03 face north -> FuchsiaCity (24, 23)",
"12.03 fuchsia_mart_wall from FuchsiaCity (24, 22) facing north to FuchsiaCity_Mart (4, 2) facing north",
"12.03 walk north 6 -> FuchsiaCity (24, 16)",
"12.03 walk west 13 -> FuchsiaCity (11, 16)",
"12.03 walk north 1 (warp) -> FuchsiaCity_Mart (4, 7)",
"12.03 walk north 5 (to wall) -> FuchsiaCity_Mart (4, 2) [walk only]",
"12.03 fuchsia_mart_clerk from FuchsiaCity_Mart (4, 2) facing north to FuchsiaCity_Mart (4, 3) facing west",
"12.03 walk south 1 -> FuchsiaCity_Mart (4, 3) [walk only]",
"12.03 face west -> FuchsiaCity_Mart (4, 3) [walk only]",
"12.03 above_mart_tree from FuchsiaCity_Mart (4, 3) facing west to FuchsiaCity (24, 21) facing south",
"12.03 walk south 5 (warp) -> FuchsiaCity (11, 16) [walk only]",
"12.03 walk south 1 (to wall) -> FuchsiaCity (11, 17)",
"12.03 walk east 13 (to wall) -> FuchsiaCity (24, 17)",
"12.03 walk south 4 -> FuchsiaCity (24, 21)",
"12.03 pallet_shore from PalletTown (6, 8) facing south to PalletTown (10, 16) facing south",
"12.03 walk south 2 (to wall) -> PalletTown (6, 10)",
"12.03 walk east 4 -> PalletTown (10, 10)",
Expand Down
Loading
Loading