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
17 changes: 17 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStorySelect.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define PokemonAutomation_Pokemon_AutoStorySelect_H

#include <memory>
#include <span>
#include <string>
#include <vector>
#include "CommonTools/Options/StringSelectOption.h"
Expand Down Expand Up @@ -40,6 +41,22 @@ StringSelectDatabase make_story_select_database(
}


// The start/end dropdown database of a story's checkpoints, given by their
// labels in order ("00.09 Pewter Gym: Camper Liam, then Brock"), slugged
// by their index, as make_story_select_database() slugs SV's. A game whose
// story is in sections (BDSP's segments 00 and 01) passes them joined: a
// section's entry i is then story_index(true, i, first of the section).
// The program keeps it in a function-local static: the options hold a
// reference.
inline StringSelectDatabase story_checkpoint_database(std::span<const std::string> labels){
StringSelectDatabase database;
for (size_t c = 0; c < labels.size(); c++){
database.add_entry(StringSelectEntry(std::to_string(c), labels[c]));
}
return database;
}


// The index in the whole story list of entry `index_in_section` of the
// tutorial or the main story, whose first entry is `first_main_story_index`.
// NOTE: a section ends where the user must step in (e.g. level up the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
*/

#include <format>
#include <memory>
#include <string>
#include <utility>
Expand Down Expand Up @@ -103,6 +104,68 @@
const AutoStorySelectScenario m_scenario;
};

// (slug, label) entries as [0 "label", ...].
std::string describe_entries(const std::vector<std::pair<std::string, std::string>>& entries){
std::string text;
for (const auto& [slug, label] : entries){
text += std::format("{}{} \"{}\"", text.empty() ? "" : ", ", slug, label);
}
return std::format("[{}]", text);
}

// A game's checkpoint labels, in story order.
struct StoryCheckpointDatabaseInput{
std::vector<std::string> labels;
};

// The (slug, label) entries: one per label, in order, slugged by its index.
struct StoryCheckpointDatabaseExpectation{
std::vector<std::pair<std::string, std::string>> entries;
};

// story_checkpoint_database() over a game's labels and the entries it must make.
struct StoryCheckpointDatabaseCase{
std::string name;
StoryCheckpointDatabaseInput input;
StoryCheckpointDatabaseExpectation expectation;
};

class Test_StoryCheckpointDatabase : public UnitTest{
public:
Test_StoryCheckpointDatabase(StoryCheckpointDatabaseCase test_case)
: UnitTest(std::format("Pokemon::AutoStorySelectDatabase - {}", test_case.name))
, m_case(std::move(test_case))
{}

virtual UnitTestResult run(Logger&, CancellableScope&) const override{
const StringSelectDatabase database = story_checkpoint_database(m_case.input.labels);
std::vector<std::pair<std::string, std::string>> got;
for (const StringSelectEntry& entry : database.case_list()){
got.emplace_back(entry.slug, entry.display_name);
}
if (got != m_case.expectation.entries){
return std::format("Expected {}, got {}", describe_entries(m_case.expectation.entries), describe_entries(got));
}
return true;
}

private:
const StoryCheckpointDatabaseCase m_case;
};

const StoryCheckpointDatabaseCase STORY_CHECKPOINT_DATABASE_CASES[] = {
{
.name = "checkpoint labels slugged by index",
.input = {.labels = {"00.01 Pallet Town", "00.02 Oak's lab"}},
.expectation = {.entries = {{"0", "00.01 Pallet Town"}, {"1", "00.02 Oak's lab"}}},
},
{
.name = "no checkpoints, no entries",
.input = {.labels = {}},
.expectation = {.entries = {}},
},
};

void add_index_tests(UnitTestDatabase& database){
const AutoStoryIndexScenario scenarios[] = {
{
Expand Down Expand Up @@ -134,7 +197,7 @@
}
}

void add_select_database_tests(UnitTestDatabase& database){

Check warning on line 200 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::add_select_database_tests( UnitTestDatabase & database): function_lines=28 (advisory above 25, limit 50)
const AutoStorySelectScenario select_scenarios[] = {
{
.name = "the tutorial dropdown",
Expand All @@ -158,6 +221,9 @@
for (const AutoStorySelectScenario& scenario : select_scenarios){
database.add<Test_AutoStorySelectDatabase>(scenario);
}
for (const StoryCheckpointDatabaseCase& test_case : STORY_CHECKPOINT_DATABASE_CASES){
database.add<Test_StoryCheckpointDatabase>(test_case);
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "Engine/Pokemon_StoryEnginePolicy_Tests.h"
#include "Engine/Pokemon_StoryEngineRuns_Tests.h"
#include "Engine/Pokemon_StoryEngineSaveRetry_Tests.h"
#include "Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h"
#include "Pokemon/Options/Pokemon_StoryRunOptions_Tests.h"
#include "Pokemon_SceneJudges_Tests.h"
#include "Pokemon_StoryEvents_Tests.h"
#include "Pokemon_StoryFailureClasses_Tests.h"
Expand Down Expand Up @@ -48,6 +50,8 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){
add_tests_StorySavePolicy(database);
add_tests_SceneJudges(database);
add_tests_StoryFiles(database);
add_tests_StoryRangeOptions(database);
add_tests_StoryRunOptions(database);
add_tests_StoryRunTypes(database);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Story Steps Tests

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

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=514 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand All @@ -20,6 +20,8 @@
#include <utility>
#include <vector>
#include "Common/Cpp/TestRunners/UnitTest.h"
#include "Pokemon/Options/Pokemon_StoryRunOptions.h"
#include "Pokemon_AutoStorySelect.h"
#include "Pokemon_AutoStoryStats.h"
#include "Pokemon_StoryEvents.h"
#include "Pokemon_BuiltinStoryActions.h"
Expand Down Expand Up @@ -353,6 +355,60 @@
}


// Two sections of a story, as BDSP has segments 00 and 01.
const std::vector<StoryCheckpoint>& two_sections(){
static const std::vector<StoryCheckpoint> rows{
StoryCheckpoint{.id = "00.01", .title = "Leave the house", .start = std::nullopt, .steps = {}},
StoryCheckpoint{.id = "00.02", .title = "Walk to Sandgem Town", .start = std::nullopt, .steps = {}},
StoryCheckpoint{.id = "01.01", .title = "Jubilife to Barry", .start = std::nullopt, .steps = {}},
};
return rows;
}

const StringSelectDatabase& two_sections_database(){
static const StringSelectDatabase database = []{
std::vector<std::string> labels;
for (const StoryCheckpoint& checkpoint : two_sections()){
labels.push_back(story_checkpoint_label(checkpoint));
}
return story_checkpoint_database(labels);
}();
return database;
}

// The dropdowns list the checkpoints as "id title", slugged by their index
// in the whole story, which is story_index() of a section's own index.
UnitTestResult test_checkpoint_dropdowns(){

Check warning on line 381 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::test_checkpoint_dropdowns(): function_lines=30 (advisory above 25, limit 50)
const StringSelectDatabase& database = two_sections_database();
const std::vector<std::string> expected{"00.01 Leave the house", "00.02 Walk to Sandgem Town", "01.01 Jubilife to Barry"};
if (database.size() != expected.size()){
return std::format("Expected 3 entries, got {}", database.size());
}
for (size_t c = 0; c < expected.size(); c++){
if (database[c].display_name != expected[c] || database[c].slug != std::to_string(c)){
return std::format("Unexpected entry {}: {} {}", c, database[c].slug, database[c].display_name);
}
}
if (database[story_index(true, 0, 2)].display_name != "01.01 Jubilife to Barry"){
return "Expected the second section's first entry at story_index(true, 0, 2).";
}
if (story_checkpoint_label(StoryCheckpoint{.id = "00.09", .title = "Pewter Gym: Camper Liam, then Brock", .start = std::nullopt, .steps = {}})
!= "00.09 Pewter Gym: Camper Liam, then Brock"
){
return "Unexpected label.";
}
StoryRunOptions options(database);
if (options.start_index() != 0 || options.end_index() != 2 || !options.check_validity().empty()){
return "Expected the whole story by default.";
}
options.START_CHECKPOINT.set_by_index(2);
options.END_CHECKPOINT.set_by_index(1);
if (options.check_validity().empty()){
return "Expected a start after the end to be refused.";
}
return true;
}

// A segment's report in the shared AutoStory stats.
UnitTestResult test_story_stats(){
SegmentReport report;
Expand All @@ -370,6 +426,20 @@
return true;
}

// The program's option group: 3 attempts, no stop, Thorough, by default;
// Fast when chosen.
UnitTestResult test_run_options(){
StoryRunOptions options(two_sections_database());
if (options.ATTEMPTS_PER_CHECKPOINT != 3 || options.check_level() != StoryCheckLevel::DEFAULT){
return "Expected 3 attempts, no stop at the first failure, and Thorough checks by default.";
}
options.CHECKS.set(StoryCheckLevel::FAST);
if (options.check_level() != StoryCheckLevel::FAST){
return "Expected Fast checks once chosen.";
}
return true;
}

UnitTestResult test_lint(){
StoryStep no_target = make(StoryStepKind::ROUTE, "walk");
StoryStep again = make(StoryStepKind::TALK, "walk");
Expand Down Expand Up @@ -424,6 +494,8 @@
{.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 = "the run options' defaults", .test = test_run_options},
{.name = "start and end checkpoint dropdowns", .test = test_checkpoint_dropdowns},
{.name = "a segment's run in the AutoStory stats", .test = test_story_stats},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* Story Checks
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "Pokemon_StoryCheckLevel.h"

namespace PokemonAutomation{
namespace Pokemon{


std::string story_check_level_name(StoryCheckLevel level){
switch (level){
case StoryCheckLevel::TESTING:
return "testing";
case StoryCheckLevel::THOROUGH:
return "thorough";
case StoryCheckLevel::DEFAULT:
return "default";
case StoryCheckLevel::FAST:
return "fast";
case StoryCheckLevel::SPEEDRUN:
return "speedrun";
}
return "?";
}



} // namespace Pokemon
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* Story Check Level
*
* From: https://github.com/PokemonAutomation/
*
* How much a story step reads the screen to confirm what it did. The
* program's options choose it (StoryRunOptions); each game decides what
* its steps check at each level.
*
*/

#ifndef PokemonAutomation_Pokemon_StoryCheckLevel_H
#define PokemonAutomation_Pokemon_StoryCheckLevel_H

#include <string>

namespace PokemonAutomation{
namespace Pokemon{


// How much a step reads the screen to confirm what it did.
enum class StoryCheckLevel{
// Everything THOROUGH checks, each checkpoint run 3 times by default to
// make sure it really passes.
TESTING,
// Check everything for the user: Bag items, Pokemon stats,
// preconditions.
THOROUGH,
// Assume most things are fine; check the most important ones.
DEFAULT,
// Minimal checks.
FAST,
// Almost no checks, to be as fast as possible.
SPEEDRUN,
};

// Returns the readable string representation of a story check level.
std::string story_check_level_name(StoryCheckLevel level);



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Story Range Options
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <utility>
#include "Pokemon_StoryRangeOptions.h"

namespace PokemonAutomation{
namespace Pokemon{


StoryRangeOptions::StoryRangeOptions(std::string label, const StringSelectDatabase& checkpoints, LockMode end_lock)
: GroupOption(std::move(label), LockMode::LOCK_WHILE_RUNNING)
, START_CHECKPOINT(
"<b>Start checkpoint:</b><br>The program starts from this checkpoint's save.",
checkpoints,
LockMode::LOCK_WHILE_RUNNING,
0
)
, END_CHECKPOINT(
"<b>End checkpoint:</b><br>The program stops after this checkpoint.",
checkpoints,
end_lock,
checkpoints.size() == 0 ? 0 : checkpoints.size() - 1
)
{
PA_ADD_OPTION(START_CHECKPOINT);
PA_ADD_OPTION(END_CHECKPOINT);
}

size_t StoryRangeOptions::start_index() const{
return START_CHECKPOINT.index();
}

size_t StoryRangeOptions::end_index() const{
return END_CHECKPOINT.index();
}

std::string StoryRangeOptions::check_validity() const{
if (start_index() > end_index()){
return "The start checkpoint comes after the end checkpoint.";
}
return GroupOption::check_validity();
}



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