Skip to content
Merged
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 @@ -19,6 +19,7 @@
#include "Engine/Pokemon_StoryEngine_Tests.h"
#include "Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h"
#include "Pokemon_AutoStorySelect_Tests.h"
#include "Pokemon_StoryRun_Tests.h"
#include "Pokemon_AutoStoryStats_Tests.h"
#include "Pokemon_Interrupts_Tests.h"
#include "Pokemon_SceneJudges_Tests.h"
Expand All @@ -40,6 +41,7 @@ namespace Pokemon{
// Registers every shared AutoStory suite, for the unit test runner.
inline void add_tests_AutoStory(UnitTestDatabase& database){
add_tests_AutoStorySelect(database);
add_tests_StoryRun(database);
add_tests_AutoStoryStats(database);
add_tests_StoryLog(database);
add_tests_StoryRangeOptions(database);
Expand Down
130 changes: 130 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRun.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/* AutoStory Story Run
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <utility>
#include "CommonFramework/Exceptions/OperationFailedException.h"
#include "Pokemon_StoryRun.h"

namespace PokemonAutomation{
namespace Pokemon{


namespace{

void log_line(const std::string& line, StoryGameHooks& game){
if (game.log){
game.log(line);
}
}

std::string story_stop_message(const SegmentReport& report){
if (report.completed){
return "";
}
return std::format(
"AutoStory stopped at {} {}: {}",
report.failed_checkpoint, report.failed_event, to_string(report.failure)
);
}

std::string story_verify_summary(std::string_view checkpoint, const VerifyPolicy& policy, const VerifyReport& report){
return std::format(
"AutoStory verify: {} {}/{} passed, {} needed.", checkpoint, report.passes, policy.runs, policy.passes_needed
);
}

Outcome load_story_save(std::string_view reason, StoryGameHooks& game, RunState& state){
log_line(std::format("[Story] {}: loading the save", reason), game);
Outcome outcome = game.load_save(state);
if (outcome.succeeded() && game.check_save_place){
const std::string error = game.check_save_place();
if (!error.empty()){
outcome = outcome_failed("wrong_save", error);
}
}
if (outcome.succeeded() && game.after_load){
outcome = game.after_load(state);
}
if (!outcome.succeeded()){
log_line(std::format("[Story] {}", to_string(outcome)), game);
}
return outcome;
}

} // namespace

Outcome prepare_story_start(std::string_view checkpoint, StoryStart start, StoryGameHooks& game, RunState& state){
switch (start){
case StoryStart::NEW_GAME:
log_line(std::format("[Story] {}: starts a new game, no save to load", checkpoint), game);
return outcome_ok();
case StoryStart::LIVE:
log_line(std::format("[Story] {}: starts from the game as it is, no save to load", checkpoint), game);
return outcome_ok();
case StoryStart::FROM_SAVE:
break;
}
Outcome outcome = load_story_save(checkpoint, game, state);
if (outcome.succeeded()){
state.flags.insert("save_loaded");
}
return outcome;
}

SegmentHooks story_segment_hooks(StoryGameHooks& game){
SegmentHooks hooks;
hooks.reload = [&game](RunState& state){
return load_story_save("retry", game, state);
};
hooks.save = [&game](RunState& state){
if (!game.save){
return outcome_failed("no_save_hook", "this game's rows save at their start");
}
return game.save(state);
};
hooks.log = [&game](const std::string& line){
log_line(line, game);
};
hooks.status = [&game](const std::string& line){
if (game.status){
game.status(line);
}
};
return hooks;
}

void log_story_failure(StoryGameHooks& game){
log_line(std::string(STORY_LEFT_WHERE_FAILED), game);
}

std::string finish_story_run(const SegmentReport& report, std::string_view last, AutoStoryStats& stats, StoryGameHooks& game){
record_story_counts(report.story_counts(), stats);
if (!report.completed){
log_story_failure(game);
throw OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, story_stop_message(report));
}
std::string done = std::format("AutoStory: done through {}.", last);
log_line(done, game);
return done;
}

std::string finish_story_verify(
std::string_view checkpoint, const VerifyPolicy& policy, const VerifyReport& report, StoryGameHooks& game
){
std::string summary = story_verify_summary(checkpoint, policy, report);
if (!report.verified){
log_story_failure(game);
throw OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, summary);
}
log_line(summary, game);
return summary;
}



} // namespace Pokemon
} // namespace PokemonAutomation
92 changes: 92 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRun.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* AutoStory Story Run
*
* From: https://github.com/PokemonAutomation/
*
* The start and finish of a story run: get the
* game to the start checkpoint, lend the segment runner its reload, and
* finish a run or a verify run with the same log lines, stats and errors.
* A game lends only what really differs: how it loads its save (BDSP
* resets from HOME, FRLG soft-resets) and how it checks where the save is.
*
* Two rules hold for every game:
* - A run that starts at a checkpoint with a save loads that save first,
* so it never walks from wherever the game was left.
* - A failure leaves the game where it failed. Nothing here closes the
* game; the user (or the next run from the save) resumes from there.
*
*/

#ifndef PokemonAutomation_Pokemon_StoryRun_H
#define PokemonAutomation_Pokemon_StoryRun_H

#include <string>
#include <string_view>
#include "Common/Cpp/MoveOnlyFunction.h"
#include "Pokemon_AutoStoryStats.h"
#include "Pokemon_StoryCheckpointTypes.h"
#include "Pokemon_StoryOutcome.h"
#include "Pokemon_StorySegments.h"

namespace PokemonAutomation{
namespace Pokemon{


// How the game reaches the start checkpoint.
enum class StoryStart{
// A new game from the title screen: there is no save.
NEW_GAME,
// The game as another program left it: nothing is loaded.
LIVE,
// The checkpoint's save, loaded first.
FROM_SAVE,
};

// What a game lends the story run.
struct StoryGameHooks{
// Loads the last save: the game reset (from HOME or soft) and back in
// the overworld. A failed Outcome says why.
Act load_save;
// Optional: after a load, "" when the save is where the checkpoint
// about to run starts, else why not.
MoveOnlyFunction<std::string()> check_save_place;
// Optional: after a load and its place check, rereads what the save
// holds (FRLG: the bag's supplies).
Act after_load;
// Optional: saves the game (rows that save at their end).
Act save;
EventLogger log;
// Optional: the step under way, for the video overlay.
EventLogger status;
};

// The line logged after any failure.
inline constexpr std::string_view STORY_LEFT_WHERE_FAILED = "AutoStory failed: the game stays where it failed.";

// Gets the game to where checkpoint `checkpoint` starts, as `start` says.
// FROM_SAVE always loads the save (logging "[Story] 00.04b: loading the
// save"), checks its place and rereads it, and sets the "save_loaded" flag.
Outcome prepare_story_start(std::string_view checkpoint, StoryStart start, StoryGameHooks& game, RunState& state);

// The runner's hooks: reload is load_story_save(); save, log and status
// are the game's. `game` must outlive them.
SegmentHooks story_segment_hooks(StoryGameHooks& game);

// Ends a run: its counts into `stats`, then the failure line and an
// OperationFailedException if it stopped short, else "AutoStory: done
// through <last>." Returns that line, for the finish notification.
std::string finish_story_run(const SegmentReport& report, std::string_view last, AutoStoryStats& stats, StoryGameHooks& game);

// Ends a verify run: the summary, and the failure line and an
// OperationFailedException unless verified. Returns the summary.
std::string finish_story_verify(
std::string_view checkpoint, const VerifyPolicy& policy, const VerifyReport& report, StoryGameHooks& game
);

// The failure line, for a game whose run threw before the story run saw it.
void log_story_failure(StoryGameHooks& game);



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
Loading