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
361 changes: 361 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,361 @@
/* Story Engine
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "Pokemon/AutoStory/Pokemon_CheckpointAttempts.h"
#include "Pokemon/AutoStory/Pokemon_StoryEvents.h"
#include "Pokemon/AutoStory/Pokemon_StoryOutcomeClass.h"
#include "Pokemon_StoryEngine.h"

namespace PokemonAutomation{
namespace Pokemon{


namespace{


// The StoryStepExecutor interface over the step runner, so the engine
// builds its events with build_story_checkpoint() (from_save,
// once_per_save), as the segment runner does.
template <StoryPose PoseType>
class RunnerExecutor : public StoryStepExecutor{
public:
RunnerExecutor(StoryStepRunner<PoseType>& runner, const StoryGame<PoseType>& game, StoryPausePoints& pause)
: m_runner(runner)
, m_game(game)
, m_pause(pause)
{}

virtual void begin_attempt(const StoryCheckpoint& data) override{
m_checkpoint = &data;
if (m_game.begin_attempt){
m_game.begin_attempt(data);
}
}
virtual Outcome run(const StoryStep& step, RunState& state) override{
m_pause.before_step(pause_label(step));
return m_runner.act(step, state);
}
virtual bool holds(std::string_view condition, RunState& state) override{
return m_runner.holds(condition, state);
}

private:
// The step's place in its checkpoint (step names are unique, which the
// lint checks).
std::string pause_label(const StoryStep& step) const{
size_t index = 0;
if (m_checkpoint != nullptr){
while (index < m_checkpoint->steps.size() && m_checkpoint->steps[index].name != step.name){
index++;
}
}
return story_step_pause_label(m_checkpoint == nullptr ? "" : m_checkpoint->id, index, step.name);
}

StoryStepRunner<PoseType>& m_runner;
const StoryGame<PoseType>& m_game;
StoryPausePoints& m_pause;
const StoryCheckpoint* m_checkpoint = nullptr;
};

// `game` with its SAVE step "save" saving `current`. Sets `saved` when it
// saves.
template <StoryPose PoseType>
StoryGame<PoseType> with_save_step(
StoryGame<PoseType> game, const StoryCheckpoint* const& current, bool& saved
){
StoryActionHandler<PoseType> step = [&current, &saved, save = game.save](
StoryBackend<PoseType>& backend, const StoryActionCall&, RunState&
){
if (!save || current == nullptr){
return outcome_failed("unsupported_step", "save needs StoryGame::save");
}
Outcome outcome = save(backend, *current);
saved = saved || outcome.succeeded();
return outcome;
};
game.actions.push_back({.spec = {.kind = StoryStepKind::SAVE, .name = "save", .args = {}}, .handler = std::move(step)});
return game;
}

// A failure every attempt would repeat: no reload for it.
bool repeats_on_reload(const Outcome& failure){
return failure.failure == GAME_NOT_RUNNING || failure.failure == CONSOLE_ONLY || failure.failure == "lint_failed";
}

// What stopped `report`'s run: its steps' failure, or its postconditions.
Outcome run_failure(const StoryEngineReport& report){
if (!report.run.completed){
return report.run.failure;
}
return outcome_failed("postconditions_failed");
}


// The items ("bag.<ITEM>") and flags ("flag.<name>", non-zero is set) of
// `observation`'s readings.
ObservedState observed_readings(const ScreenObservation& observation){
constexpr std::string_view BAG = "bag.";
constexpr std::string_view FLAG = "flag.";
ObservedState observed;
for (const auto& [name, value] : observation.readings){
if (name.starts_with(BAG)){
observed.items[name.substr(BAG.size())] = value < 0 ? 0 : (size_t)value;
}else if (name.starts_with(FLAG)){
observed.flags[name.substr(FLAG.size())] = value != 0;
}
}
return observed;
}


// What checking `checkpoint`'s postconditions needs read: "pose" for an
// end position, "bag.<ITEM>" per item, "flag.<name>" per flag.
std::vector<std::string> postcondition_needs(const StoryCheckpoint& checkpoint){
std::vector<std::string> needs;
if (checkpoint_end_pose(checkpoint)){
needs.emplace_back("pose");
}
for (const StoryItem& item : checkpoint.postconditions.items){
needs.push_back(std::format("bag.{}", item.item));
}
for (const std::string& flag : checkpoint.postconditions.flags){
needs.push_back(std::format("flag.{}", flag));
}
return needs;
}

// Whether the postcondition check of a checkpoint that `needs` these runs
// `reader` on a backend of `kind`.
template <StoryPose PoseType>
bool runs_after_checkpoint(const StoryGameReader<PoseType>& reader, const std::vector<std::string>& needs, StoryBackendKind kind){
if (!reader.read || reader.use != StoryReaderUse::AFTER_CHECKPOINT){
return false;
}
if (reader.console_only && kind != StoryBackendKind::CONSOLE){
return false;
}
for (const std::string& need : needs){
if (story_reader_supplies(reader.supplies, need)){
return true;
}
}
return false;
}


} // namespace


ObservedState observed_state(const Observation<GridPose>& observation){
ObservedState observed = observed_readings(observation);
if (observation.pose){
observed.map = observation.pose->map;
if (observation.pose->tiles.size() == 1){
observed.tile = observation.pose->tiles[0];
}
observed.facing = observation.pose->facing;
}
return observed;
}

ObservedState observed_state(const Observation<ZonePose>& observation){
ObservedState observed = observed_readings(observation);
if (observation.pose){
observed.map = observation.pose->zone;
}
return observed;
}


template <StoryPose PoseType>
StoryEngine<PoseType>::StoryEngine(const StoryGame<PoseType>& game, StoryBackend<PoseType>& backend)
: m_game(with_save_step(game, m_checkpoint, m_saved_part_way))
, m_backend(backend)
, m_registry(m_game)
, m_runner(m_registry, backend)
{
m_registry.pause_points().set_log([&backend](std::string_view line){
backend.log(line);
});
}

template <StoryPose PoseType>
StoryEngineReport StoryEngine<PoseType>::run_checkpoint(const StoryCheckpoint& checkpoint, RunState& state){

Check warning on line 192 in SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::StoryEngine<PoseType>::run_checkpoint( const StoryCheckpoint & checkpoint , RunState & state): function_lines=26 (advisory above 25, limit 50)
StoryEngineReport report;
const std::vector<std::string> problems = m_registry.lint(checkpoint);
if (!problems.empty()){
for (const std::string& problem : problems){
m_backend.log(std::format("[Lint] {}", problem));
}
report.run.failure = outcome_failed("lint_failed", problems.front());
return report;
}
m_checkpoint = &checkpoint;
m_saved_part_way = false;
report.run = run_steps(checkpoint, state);
report.saved_part_way = m_saved_part_way;
m_checkpoint = nullptr;
if (!report.run.completed){
m_backend.log(std::format("[Save] {}: run failed: not saved", checkpoint.id));
return report;
}
report.postconditions = check_postconditions(checkpoint);
report.verdict = report.postconditions->count(PostconditionVerdict::FAILED) == 0
? CheckpointVerdict::PASSED
: CheckpointVerdict::FAILED;
save_if_passed(checkpoint, report);
return report;
}

template <StoryPose PoseType>
Outcome StoryEngine<PoseType>::reload(RunState& state){
state = RunState{};
if (!m_game.reload){
return outcome_failed("reload_failed", "the game has no reload");
}
const Outcome reloaded = m_game.reload(m_backend);
if (!reloaded.succeeded()){
return outcome_failed("reload_failed", to_string(reloaded));
}
if (reloaded.pose){
state.pose = *reloaded.pose;
}
return outcome_ok();
}

template <StoryPose PoseType>
StoryEngineReport StoryEngine<PoseType>::run_with_retries(

Check warning on line 236 in SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::StoryEngine<PoseType>::run_with_retries( const StoryCheckpoint & checkpoint , size_t attempts , RunState & state , CheckpointRun & run): function_lines=39 (advisory above 25, limit 50)
const StoryCheckpoint& checkpoint, size_t attempts, RunState& state, CheckpointRun& run
){
StoryEngineReport report;
const CheckpointAttempts loop{
.max_attempts = attempts,
.run = [&](size_t attempt){
run.attempts = attempt;
report = run_checkpoint(checkpoint, state);
if (report.verdict == CheckpointVerdict::PASSED){
return AttemptResult::PASSED;
}
if (report.saved_part_way){
m_backend.log(std::format(
"[Checkpoint] {}: a save step saved part-way, so a reload would not return to its start: no retry",
checkpoint.id
));
return AttemptResult::FINAL;
}
return repeats_on_reload(report.run.failure) ? AttemptResult::FINAL : AttemptResult::FAILED;
},
.reset = [&](size_t next_attempt){
m_backend.log(checkpoint_reset_line(checkpoint.id, next_attempt, attempts));
const Outcome reloaded = reload(state);
if (!reloaded.succeeded()){
report.run.completed = false;
report.run.failed_name.clear();
report.run.failure = reloaded;
return false;
}
return true;
},
.log = [this](const std::string& line){
m_backend.log(line);
},
};
run.passed = run_checkpoint_attempts(checkpoint.id, loop).passed;
return report;
}

template <StoryPose PoseType>
SegmentReport StoryEngine<PoseType>::run_segment(

Check warning on line 277 in SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::StoryEngine<PoseType>::run_segment( std :: string_view id , std :: span<const StoryCheckpoint> checkpoints , size_t attempts_per_checkpoint , RunState & state): function_lines=31 (advisory above 25, limit 50)
std::string_view id, std::span<const StoryCheckpoint> checkpoints, size_t attempts_per_checkpoint,
RunState& state
){
SegmentReport report;
m_backend.log(std::format("[Segment] {}: start, {} checkpoints", id, checkpoints.size()));
// Whether a reload returns to the save the next checkpoint starts from.
bool start_saved = true;
for (size_t c = 0; c < checkpoints.size(); c++){
const StoryCheckpoint& checkpoint = checkpoints[c];
const size_t attempts = start_saved ? attempts_per_checkpoint : 1;
if (!start_saved && attempts_per_checkpoint > 1){
m_backend.log(std::format("[Segment] {}: {}: no save at its start, so no reload if it fails", id, checkpoint.id));
}
report.checkpoints_run++;
report.runs.push_back(CheckpointRun{.id = checkpoint.id});
const StoryEngineReport result = run_with_retries(checkpoint, attempts, state, report.runs.back());
report.reloads += report.runs.back().attempts - 1;
if (result.verdict != CheckpointVerdict::PASSED){
report.failed_checkpoint = checkpoint.id;
report.failed_event = result.run.failed_name;
report.failure = run_failure(result);
m_backend.log(std::format("[Segment] {}: failed at {}: {}", id, checkpoint.id, to_string_with_class(report.failure)));
return report;
}
start_saved = result.saved;
}
report.completed = true;
m_backend.log(std::format("[Segment] {}: done, {} checkpoints", id, report.checkpoints_run));
return report;
}

template <StoryPose PoseType>
CheckpointReport StoryEngine<PoseType>::run_steps(const StoryCheckpoint& checkpoint, RunState& state){
RunnerExecutor<PoseType> executor(m_runner, m_game, m_registry.pause_points());
Checkpoint events = build_story_checkpoint(checkpoint, executor, &m_prepared);
if (m_game.interrupts){
events.interrupts = m_game.interrupts(m_backend);
}
EventLogger log = [this](const std::string& line){
m_backend.log(line);
};
return Pokemon::run_checkpoint(state, events, log);
}

template <StoryPose PoseType>
void StoryEngine<PoseType>::save_if_passed(const StoryCheckpoint& checkpoint, StoryEngineReport& report){
if (report.verdict != CheckpointVerdict::PASSED){
m_backend.log(std::format("[Save] {}: not saved: postconditions failed", checkpoint.id));
return;
}
if (!m_game.save){
m_backend.log(std::format("[Save] {}: not saved: the game has no save", checkpoint.id));
return;
}
const Outcome saved = m_game.save(m_backend, checkpoint);
report.saved = saved.succeeded();
m_backend.log(std::format("[Save] {}: {}", checkpoint.id, report.saved ? "saved" : std::format("save failed: {}", to_string(saved))));
}

template <StoryPose PoseType>
PostconditionReport StoryEngine<PoseType>::check_postconditions(const StoryCheckpoint& checkpoint){
const std::vector<std::string> needs = postcondition_needs(checkpoint);
Observation<PoseType> observation = m_backend.observe();
for (const StoryGameReader<PoseType>& reader : m_game.readers){
if (runs_after_checkpoint(reader, needs, m_backend.kind())){
reader.read(m_backend, observation);
}
}
PostconditionReport report = Pokemon::check_postconditions(checkpoint, observed_state(observation));
for (const std::string& line : postcondition_log_lines(report)){
m_backend.log(line);
}
return report;
}


template class StoryEngine<GridPose>;
template class StoryEngine<ZonePose>;




} // namespace Pokemon
} // namespace PokemonAutomation
Loading
Loading