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
@@ -0,0 +1,36 @@
/* Checkpoint Attempts
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "Pokemon_StoryLog.h"
#include "Pokemon_CheckpointAttempts.h"

namespace PokemonAutomation{
namespace Pokemon{


CheckpointAttemptsResult run_checkpoint_attempts(std::string_view checkpoint, const CheckpointAttempts& attempts){
CheckpointAttemptsResult result;
for (size_t attempt = 1;; attempt++){
result.attempts = attempt;
const AttemptResult outcome = attempts.run(attempt);
if (outcome == AttemptResult::PASSED){
result.passed = true;
attempts.log(checkpoint_pass_line(checkpoint, attempt, attempts.prefix_kind));
return result;
}
if (outcome == AttemptResult::FINAL || attempt >= attempts.max_attempts){
return result;
}
if (!attempts.reset(attempt + 1)){
return result;
}
}
}



} // namespace Pokemon
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* Checkpoint Attempts
*
* From: https://github.com/PokemonAutomation/
*
* The retry loop every AutoStory runs a checkpoint in: run an attempt, and
* when it fails go back to the checkpoint's start save and try again,
* until an attempt passes, fails in a way another attempt would repeat,
* the last attempt fails, or a reset fails. The story segments and the
* story engine run their checkpoints through it, so each writes the same
* pass line (Pokemon_StoryLog.h) and counts attempts the same way. The
* game supplies the attempt and the reset.
*
*/

#ifndef PokemonAutomation_Pokemon_CheckpointAttempts_H
#define PokemonAutomation_Pokemon_CheckpointAttempts_H

#include <cstddef>
#include <functional>
#include <string>
#include <string_view>
#include "Pokemon_StoryLog.h"

namespace PokemonAutomation{
namespace Pokemon{


// How one attempt at a checkpoint ended.
enum class AttemptResult{
PASSED,
// Failed; another attempt from the start save may pass.
FAILED,
// Failed in a way another attempt would repeat (the game is not
// running): no more attempts.
FINAL,
};

// Writes one log line.
using CheckpointLogger = std::function<void(const std::string& line)>;

// What the game does at each step of the retry loop.
struct CheckpointAttempts{
// Attempts before the loop gives up. The first attempt always runs,
// so 0 runs one, as 1 does.
size_t max_attempts = 1;
// Runs attempt `attempt`, counting from 1.
std::function<AttemptResult(size_t attempt)> run;
// Goes back to the checkpoint's start save before attempt
// `next_attempt`, writing its own log line. Returns false when it could
// not, which ends the loop.
std::function<bool(size_t next_attempt)> reset;
CheckpointLogger log;
// AS_GIVEN when `checkpoint` is the game's own text that already says
// it is a checkpoint, so the pass line leaves out "[Checkpoint] "
// (checkpoint_start_line()).
CheckpointPrefix prefix_kind = CheckpointPrefix::ADD;
};

// What the loop came to.
struct CheckpointAttemptsResult{
bool passed = false;
// Attempts run, counting the last one.
size_t attempts = 0;
};

// Runs `checkpoint` until an attempt passes, an attempt is FINAL,
// `max_attempts` attempts failed, or a reset failed. Writes the pass line.
// Exceptions from `run` and `reset` pass through.
CheckpointAttemptsResult run_checkpoint_attempts(std::string_view checkpoint, const CheckpointAttempts& attempts);



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
#include <utility>
#include <vector>
#include "Common/Cpp/TestRunners/UnitTest.h"
#include "Pokemon_AutoStoryStats.h"
#include "Pokemon_StoryEvents.h"
#include "Pokemon_BuiltinStoryActions.h"
#include "Types/Pokemon_StorySegmentTypes.h"
#include "Pokemon_StoryStepExecutor.h"
#include "Pokemon_TilePose.h"
#include "Pokemon_StoryStepExecutor_Tests.h"
Expand Down Expand Up @@ -351,6 +353,23 @@ UnitTestResult test_pause_points(){
}


// A segment's report in the shared AutoStory stats.
UnitTestResult test_story_stats(){
SegmentReport report;
report.reloads = 2;
report.runs = {
CheckpointRun{.id = "00.03", .passed = true, .attempts = 1},
CheckpointRun{.id = "00.04", .passed = true, .attempts = 3},
CheckpointRun{.id = "00.05", .passed = false, .attempts = 1},
};
AutoStoryStats stats;
record_story_counts(report.story_counts(), stats);
if (stats.m_segments_started != 1 || stats.m_checkpoints_started != 3 || stats.m_resets != 2){
return "Expected 1 segment, 3 checkpoints started and 2 resets.";
}
return true;
}

UnitTestResult test_lint(){
StoryStep no_target = make(StoryStepKind::ROUTE, "walk");
StoryStep again = make(StoryStepKind::TALK, "walk");
Expand Down Expand Up @@ -405,6 +424,7 @@ const StoryStepTestCase STORY_STEP_CASES[] = {
{.name = "unsupported kinds and lost battles", .test = test_unsupported_and_lost_battles},
{.name = "pause points pause before each step", .test = test_pause_points},
{.name = "lint", .test = test_lint},
{.name = "a segment's run in the AutoStory stats", .test = test_story_stats},
};

} // namespace
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Story Segment Types
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "Pokemon_StorySegmentTypes.h"

namespace PokemonAutomation{
namespace Pokemon{


void record_story_counts(const StoryRunCounts& counts, AutoStoryStats& stats){
stats.m_segments_started += counts.segments;
stats.m_checkpoints_started += counts.checkpoints;
stats.m_resets += counts.resets;
}

bool CheckpointRun::one_shot() const{
return passed && attempts == 1;
}

size_t SegmentReport::one_shot_passes() const{
size_t count = 0;
for (const CheckpointRun& run : runs){
count += run.one_shot() ? 1 : 0;
}
return count;
}

StoryRunCounts SegmentReport::story_counts() const{
return StoryRunCounts{.segments = 1, .checkpoints = runs.size(), .resets = reloads};
}



} // namespace Pokemon
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* Story Segment Types
*
* From: https://github.com/PokemonAutomation/
*
* The plain data of a segment run (Engine/Pokemon_StoryEngine.h): the
* report of what happened.
*
*/

#ifndef PokemonAutomation_Pokemon_StorySegmentTypes_H
#define PokemonAutomation_Pokemon_StorySegmentTypes_H

#include <cstddef>
#include <string>
#include <vector>
#include "Pokemon/AutoStory/Pokemon_AutoStoryStats.h"
#include "Pokemon/AutoStory/Pokemon_StoryEvents.h"


namespace PokemonAutomation{
namespace Pokemon{




// What a story run adds to the program's stats.
struct StoryRunCounts{
size_t segments = 0;
// Checkpoints started, passed or not.
size_t checkpoints = 0;
// Game resets or reloads after failed attempts.
size_t resets = 0;
};

// Adds `counts` to the shared AutoStory stats.
void record_story_counts(const StoryRunCounts& counts, AutoStoryStats& stats);


// Summary of checkpoint attempts and pass status.
struct CheckpointRun{
std::string id;
bool passed = false;
size_t attempts = 0;

bool one_shot() const;
};

// Execution results and failure diagnostics for a story segment run.
struct SegmentReport{
bool completed = false;
// Both empty when nothing failed; the event is empty when a start
// check, an end check, a save or a reload failed.
std::string failed_checkpoint;
std::string failed_event;
Outcome failure;
size_t checkpoints_run = 0;
// Reloads after failed attempts.
size_t reloads = 0;
// Each checkpoint run, in order, the failed one last.
std::vector<CheckpointRun> runs;

size_t one_shot_passes() const;
// What the run adds to the program's stats (record_story_counts()):
// the segment, each checkpoint it started, each reload.
StoryRunCounts story_counts() const;
};



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
4 changes: 4 additions & 0 deletions SerialPrograms/cmake/SourceFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,8 @@ file(GLOB LIBRARY_SOURCES
Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h
Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.cpp
Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h
Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp
Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h
Source/Pokemon/AutoStory/Pokemon_MapUtil.h
Source/Pokemon/AutoStory/Pokemon_StoryCheckpointTypes.cpp
Source/Pokemon/AutoStory/Pokemon_StoryCheckpointTypes.h
Expand Down Expand Up @@ -1504,6 +1506,8 @@ file(GLOB LIBRARY_SOURCES
Source/Pokemon/AutoStory/Types/Pokemon_StoryPostconditions.h
Source/Pokemon/AutoStory/Types/Pokemon_StoryRunTypes_Tests.cpp
Source/Pokemon/AutoStory/Types/Pokemon_StoryRunTypes_Tests.h
Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.cpp
Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h
Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h
Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.cpp
Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.h
Expand Down
Loading