Skip to content
Closed
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
20 changes: 17 additions & 3 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Story Segments

Check warning on line 1 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=520 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -223,6 +223,9 @@
}
if (hooks.verify_pass){
const Outcome passed = hooks.verify_pass(id, state);
if (passed.failure == VERIFY_UNREAD){
return passed;
}
if (!passed.succeeded()){
return outcome_failed("verify_no_pass", to_string(passed));
}
Expand Down Expand Up @@ -290,8 +293,9 @@
}else{
set_save(m_row, m_policy, m_hooks, checkpoint);
}
checkpoint.save = [save = std::move(checkpoint.save), id = m_row.spec.id, &hooks = m_hooks](RunState& state) mutable{
checkpoint.save = [save = std::move(checkpoint.save), id = m_row.spec.id, &hooks = m_hooks, &unread = m_unread](RunState& state) mutable{
const Outcome passed = verify_pass(id, hooks, state);
unread = passed.failure == VERIFY_UNREAD;
if (!passed.succeeded() || !save){
return passed;
}
Expand All @@ -302,8 +306,8 @@

// One try, reloading the start save first after the first; whether it
// passed, or none when the loop stops (no reload, the start check
// failed, the game is not running).
// or a step's precondition failed, the game is not running).
std::optional<bool> try_once(size_t phase, size_t index, size_t of){

Check warning on line 310 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::VerifyRow::try_once( size_t phase , size_t index , size_t of): function_lines=36 (advisory above 25, limit 50)
const std::string& id = m_row.spec.id;
if (m_run.attempts > 0){
m_report.reloads++;
Expand All @@ -314,18 +318,26 @@
}
}
m_run.attempts++;
m_unread = false;
Checkpoint checkpoint = build(phase);
EventLogger log = [&hooks = m_hooks](const std::string& line){ say(line, hooks); };
m_result = run_checkpoint(m_state, checkpoint, log);
if (m_result.completed){
say(std::format("[Verify] {}: phase {} try {}/{} passed", id, phase, index, of), m_hooks);
return true;
}
if (m_unread){
say(std::format("[Verify] {}: the pass check couldn't read the game; no save, stopping", id), m_hooks);
m_result = failed_report(outcome_failed(std::string(VERIFY_UNREAD), to_string(m_result.failure)));
return std::nullopt;
}
say(std::format(
"[Verify] {}: phase {} try {}/{} failed{}: {}", id, phase, index, of,
m_result.failed_name.empty() ? "" : std::format(" at {}", m_result.failed_name), to_string(m_result.failure)
), m_hooks);
if (m_result.failure.failure == "start_failed" || m_result.failure.failure == GAME_NOT_RUNNING){
if (m_result.failure.failure == "start_failed" || m_result.failure.failure == "precondition_failed"
|| m_result.failure.failure == GAME_NOT_RUNNING
){
say(std::format("[Verify] {}: stopping", id), m_hooks);
return std::nullopt;
}
Expand All @@ -340,6 +352,8 @@
CheckpointRun& m_run;
RunState& m_state;
CheckpointReport m_result;
// The last try's pass check couldn't read the game (VERIFY_UNREAD).
bool m_unread = false;
};

SegmentReport stop_segment(const std::string& id, Outcome failure, SegmentHooks& hooks, SegmentReport& report){
Expand Down
13 changes: 8 additions & 5 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ struct SegmentHooks{
MoveOnlyFunction<Outcome(const std::string& checkpoint, RunState& state)> check_postconditions;
// Optional: whether a verify row's try that completed passes, by the
// game's own measure (FRLG's League: the items a fight spent within
// its caps). Run after check_postconditions.
// its caps). Run after check_postconditions, right before the save;
// either failing blocks the save. A failure named VERIFY_UNREAD (the
// game couldn't read what it checks) stops the loop: never a save,
// nor another try, on a guess.
MoveOnlyFunction<Outcome(const std::string& checkpoint, RunState& state)> verify_pass;
};

Expand All @@ -78,10 +81,10 @@ struct SegmentHooks{
// 1's tries save nothing and reload between them; below min_pass the
// segment stops ("verify_below_min_pass"); phase 2 reloads and tries
// again until a try passes, which saves, or stops after reset_budget
// ("verify_out_of_budget"). A try whose start check fails stops the loop
// at once: the save does not meet the row's preconditions. Logs through
// `hooks.log`,
// around the checkpoint lines:
// ("verify_out_of_budget"). A try whose start check fails, or whose step
// fails "precondition_failed" (FRLG's League top-up and entry contract),
// stops the loop at once: the save does not meet the row's preconditions.
// Logs through `hooks.log`, around the checkpoint lines:
// [Segment] 00: start, 3 checkpoints
// [Checkpoint] 00.02: failed, resetting for attempt 2/3
// [Checkpoint] 00.02: passed on attempt 2 (1 reset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
int saved_potions = 0;
int potions = 0;
std::vector<ScriptedTry> tries;
// The game's postcondition check holds; its pass check reads the game.
bool postconditions_hold = true;
bool pass_readable = true;
int fights = 0;
int saves = 0;
int reloads = 0;
Expand Down Expand Up @@ -103,7 +106,13 @@
potions = saved_potions;
return outcome_ok();
};
hooks.check_postconditions = [this](const std::string&, RunState&){
return postconditions_hold ? outcome_ok() : outcome_failed("postconditions", "the bag is short of the next fight's entry");
};
hooks.verify_pass = [this](const std::string&, RunState&){
if (!pass_readable){
return outcome_failed(std::string(VERIFY_UNREAD), "the bag wasn't read");
}
return spent <= POTION_CAP ? outcome_ok() : outcome_failed("over_cap", std::format("{} potions", spent));
};
hooks.log = [&lines](const std::string& line){ lines.push_back(line); };
Expand All @@ -117,6 +126,8 @@
StoryVerify verify = {};
int potions = POTIONS_NEEDED;
std::vector<ScriptedTry> tries = {};
bool postconditions_hold = true;
bool pass_readable = true;
};
struct Expectation{
bool completed = false;
Expand All @@ -138,8 +149,11 @@
: UnitTest(std::format("Pokemon::StoryVerifyRows - {}", test_case.name))
, m_case(std::move(test_case))
{}
virtual UnitTestResult run(Logger&, CancellableScope&) const override{

Check warning on line 152 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryVerifyRows_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::Test_StoryVerifyRow::run( Logger & , CancellableScope &) const: function_lines=29 (advisory above 25, limit 50)
FakeFight game{.saved_potions = m_case.input.potions, .potions = m_case.input.potions, .tries = m_case.input.tries};
FakeFight game{
.saved_potions = m_case.input.potions, .potions = m_case.input.potions, .tries = m_case.input.tries,
.postconditions_hold = m_case.input.postconditions_hold, .pass_readable = m_case.input.pass_readable,
};
Segment segment;
segment.id = "20";
segment.checkpoints.push_back(game.spec(m_case.input.verify));
Expand Down Expand Up @@ -200,6 +214,16 @@
.input = {.potions = POTIONS_NEEDED - 1, .tries = {win()}},
.expectation = {.failure = "start_failed", .fights = 0, .saves = 0, .reloads = 0},
},
{
.name = "a won try whose postcondition check fails right before the save: never saved",
.input = {.verify = {.tries = 2, .min_pass = 0, .reset_budget = 2}, .tries = {win()}, .postconditions_hold = false},
.expectation = {.failure = "verify_out_of_budget", .fights = 4, .saves = 0, .reloads = 3},
},
{
.name = "the pass check can't read the game: no save, the loop stops at once",
.input = {.tries = {win()}, .pass_readable = false},
.expectation = {.failure = "postcondition_unread", .fights = 1, .saves = 0, .reloads = 0},
},
{
.name = "min_pass 0 of 2: straight to phase 2",
.input = {.verify = {.tries = 2, .min_pass = 0, .reset_budget = 1}, .tries = {lose(), lose(), win()}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
#define PokemonAutomation_Pokemon_StoryVerify_H

#include <cstddef>
#include <string_view>

namespace PokemonAutomation{
namespace Pokemon{


// A verify pass check that couldn't read the game
// (SegmentHooks::verify_pass): the loop stops, with no save.
inline constexpr std::string_view VERIFY_UNREAD = "postcondition_unread";

// The verification loop's figures.
struct StoryVerify{
size_t tries = 10;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* AutoStory

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

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=920 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -35,7 +35,7 @@
#include "PokemonFRLG_AutoStoryEvents.h"
#include "PokemonFRLG_AutoStoryFailureClasses.h"
#include "PokemonFRLG_AutoStoryLaunch.h"
#include "PokemonFRLG_AutoStoryLeagueRows.h"
#include "PokemonFRLG_AutoStoryLeagueSteps.h"
#include "PokemonFRLG_AutoStoryRngLoop.h"
#include "PokemonFRLG_AutoStoryTable.h"
#include "PokemonFRLG_AutoStory.h"
Expand Down Expand Up @@ -546,7 +546,7 @@

// Runs `rows` as one segment, calling `before` (if set) just before the
// last row saves. Throws if a checkpoint fails for good.
static void run_story_rows(

Check warning on line 549 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::run_story_rows( SingleSwitchProgramEnvironment & env , StoryEnv & story , StoryExecutor & executor , const std :: vector<StoryCheckpoint> & rows , const Pokemon :: SegmentPolicy & policy , std :: function<void()> before): function_lines=40 (advisory above 25, limit 50)
SingleSwitchProgramEnvironment& env, StoryEnv& story, StoryExecutor& executor,
const std::vector<StoryCheckpoint>& rows, const Pokemon::SegmentPolicy& policy,
std::function<void()> before
Expand All @@ -558,11 +558,13 @@
Pokemon::StoryGameHooks game = story_game(env, story);
Pokemon::SegmentHooks hooks = Pokemon::story_segment_hooks(game);
// A verify row's try passes by the League's item caps, counted from
// its start save (the loop reloads it before each try).
// its start save (the loop reloads it before each try), and by our
// side as read off the console meeting the next fight's entry
// contract: checked right before the save, never on a guess.
hooks.verify_pass = [&story, &rows](const std::string& id, Pokemon::RunState&){
for (const StoryCheckpoint& row : rows){
if (row.id == id){
return league_row_pass(row, story.saved_supplies, story.supplies);
return check_league_save(story, row, story.saved_supplies);
}
}
return Pokemon::outcome_ok();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* AutoStory Battle Decisions

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

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=745 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -209,12 +209,6 @@
if (!m_planned && kind == BattleKind::TRAINER && supplies.policy.kind != BattlePolicyKind::NONE && supplies.lead.stats){
m_policy.emplace(supplies.policy);
}
// A League fight spends its plan's budget and what the fights before
// left (end_trainer_battle() rolls it forward).
const LeagueFightPlan* league = supplies.league ? league_fight(supplies.league->plan, supplies.trainer) : nullptr;
if (kind == BattleKind::TRAINER && league != nullptr){
supplies.budget = add_budgets(league->budget, supplies.league->carried);
}
}

TurnAction BattleDecisions::turn(const PartyStatus& status){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* AutoStory Battle Policy

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

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

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

void end_trainer_battle(Supplies& supplies){
if (supplies.league && supplies.budget){
supplies.league->carried = *supplies.budget;
}
supplies.budget.reset();
supplies.foes.clear();
supplies.trainer.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
* min(75% max HP, D + (setup turns - 1) x mean hit) first heals
* within the budget, else the setup is dropped;
* 10. attack.
* The budget comes from the League plan (PokemonFRLG_AutoStoryLeaguePlan.h);
* The budget is the battle's Supplies::budget (none: no cap);
* every turn's state and choice is logged on one line (state_line()).
*
*/
Expand Down Expand Up @@ -122,7 +122,7 @@ struct PolicyStages{
};

// A trainer battle's plan is over: its party, items and policy go from
// `supplies`; a League fight's budget left rolls forward to the next.
// `supplies`.
void end_trainer_battle(Supplies& supplies);

// The lead's side of the battle as the policy tracks it: the stages and
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* AutoStory Battle Policy Tests

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

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

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

UnitTestResult test_plan_bruno(){
const UnitTestResult result = check_policy_step("20.01", "bruno", {.spa = 2, .outspeed = true, .guard_accuracy = true}, /*policy=*/"budgeted-setup");
const UnitTestResult result = check_policy_step("20.02", "bruno", {.spa = 2, .outspeed = true, .guard_accuracy = true}, /*policy=*/"budgeted-setup");
if (result.result != UnitTestResult::PASSED){
return result;
}
if (arg_of<bool>(battle_step("20.01", "bruno"), "setup_target_torrent") != true){
return "20.01 bruno: expected the doc's target_torrent.";
if (arg_of<bool>(battle_step("20.02", "bruno"), "setup_target_torrent") != true){
return "20.02 bruno: expected the doc's target_torrent.";
}
return true;
}
Expand Down Expand Up @@ -778,14 +778,14 @@
// out. Setup only where it pays: not against a foe the best attack KOs,
// nor one that phazes; still against Lorelei's Dewgong.
const LeagueTurnCase LEAGUE_TURN_CASES[] = {
{"Bruno's Onix at +0, 71% HP: Surf", {"20.01", "bruno", 0, 0, 117}, {"", "surf"}},
{"Bruno's Onix at +2: Surf", {"20.01", "bruno", 0, 2, 165}, {"", "surf"}},
{"Bruno's Onix at +4: Surf", {"20.01", "bruno", 0, 4, 165}, {"", "surf"}},
{"Bruno's second Onix at +0: Surf", {"20.01", "bruno", 3, 0, 165}, {"", "surf"}},
{"Bruno's Hitmonchan at +0: X Special", {"20.01", "bruno", 1, 0, 165}, {"x-special", ""}},
{"Bruno's Onix at +0, 71% HP: Surf", {"20.02", "bruno", 0, 0, 117}, {"", "surf"}},
{"Bruno's Onix at +2: Surf", {"20.02", "bruno", 0, 2, 165}, {"", "surf"}},
{"Bruno's Onix at +4: Surf", {"20.02", "bruno", 0, 4, 165}, {"", "surf"}},
{"Bruno's second Onix at +0: Surf", {"20.02", "bruno", 3, 0, 165}, {"", "surf"}},
{"Bruno's Hitmonchan at +0: X Special", {"20.02", "bruno", 1, 0, 165}, {"x-special", ""}},
{"Lorelei's Dewgong at +0: X Special", {"20.01", "lorelei", 0, 0, 165}, {"x-special", ""}},
{"the Champion's Pidgeot (Whirlwind): no X item", {"20.01", "champion", 0, 0, 165}, {"", ""}},
{"the Champion's Arcanine (Roar): no X item", {"20.01", "champion", 4, 0, 165}, {"", ""}},
{"the Champion's Pidgeot (Whirlwind): no X item", {"20.05", "champion", 0, 0, 165}, {"", ""}},
{"the Champion's Arcanine (Roar): no X item", {"20.05", "champion", 4, 0, 165}, {"", ""}},
};

UnitTestResult test_league_turns(){
Expand Down Expand Up @@ -923,7 +923,7 @@
.expectation = {.description = "4 in the opening, 2 with Torrent, on a later foe or a lone one"},
},
{
.input = {.name = "plan: 20.01 bruno names budgeted-setup, its goals and target_torrent", .body = test_plan_bruno},
.input = {.name = "plan: 20.02 bruno names budgeted-setup, its goals and target_torrent", .body = test_plan_bruno},
.expectation = {.description = "the policy, goals, target_torrent and 5 foe rows"},
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
// is marked where it is used (StoryWildBattle). TMs, HMs, Poke Balls and
// key items have pockets of their own: a TEACH, a capture or a fossil
// leave the Items pocket as it is.
const std::vector<ItemsPocketChangeRow>& items_pocket_change_rows(){

Check warning on line 57 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemsPocketChange.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::items_pocket_change_rows(): function_lines=48 (advisory above 25, limit 50)
static const std::vector<ItemsPocketChangeRow> ROWS = {
{StoryStepKind::NEW_GAME, "", ItemsPocketChange::NONE},
{StoryStepKind::OPTIONS, "", ItemsPocketChange::NONE},
Expand Down Expand Up @@ -98,8 +98,7 @@
{StoryStepKind::GAME, RESTORE_PP, ItemsPocketChange::COUNTED},
{StoryStepKind::GAME, CHECK_FULL_HP_ACTION, ItemsPocketChange::NONE},
{StoryStepKind::GAME, BAG_CHECK_ACTION, ItemsPocketChange::NONE},
{StoryStepKind::GAME, LEAGUE_START_ACTION, ItemsPocketChange::COUNTED},
{StoryStepKind::GAME, LEAGUE_BETWEEN_FIGHTS_ACTION, ItemsPocketChange::COUNTED},
{StoryStepKind::GAME, LEAGUE_TOP_UP_ACTION, ItemsPocketChange::COUNTED},
};
return ROWS;
}
Expand Down
Loading
Loading