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
389 changes: 389 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Story Step JSON
*
* From: https://github.com/PokemonAutomation/
*
* Readers of the shared JSON wire form of the story data types (Types/):
* poses, steps and checkpoints, read strictly. This is
* the only layer that reads story JSON: a step comes out with its args
* typed and checked, and nothing after it parses.
*
*/

#ifndef PokemonAutomation_Pokemon_StoryStepJson_H
#define PokemonAutomation_Pokemon_StoryStepJson_H

#include <span>
#include <string_view>
#include "Common/Cpp/Json/JsonValue.h"
#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h"
#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h"
#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h"

namespace PokemonAutomation{
namespace Pokemon{


// Read a grid pose, {"map": "PalletTown", "tiles": [[3, 4]], "facing": "north"};
// malformed and unknown fields fail. No "tiles" reads
// as none (any tile, or a game with no tiles).
GridPose read_grid_pose(const JsonValue& value, std::string_view source);

// Read a declared pose (a checkpoint's "start", a step's "declared_after"):
// a tile one as a grid pose, {"map": ..., "tiles": [[x, y]], "facing": ...},
// or a zone one, {"place": "LumioseCity", "spot": "HotelZ", "heading": 90}
// (spot and heading optional). One with both or neither of "map" and "place",
// and malformed or unknown fields, fail.
DeclaredPose read_declared_pose(const JsonValue& value, std::string_view source);


// Read a step, its args parsed against its action among `actions` (by
// default the built-in ones; a game passes them with its own) and their
// fallbacks filled in. Unknown fields, kinds, actions and args, mistyped
// values and missing required args fail.
StoryStep read_story_step(
const JsonValue& value, std::string_view source,
std::span<const StoryActionSpec> actions = builtin_story_actions()
);


// Read the common checkpoint, its steps read as read_story_step() does.
// Its steps_json keeps the "events" array as read, dumped compactly.
StoryCheckpoint read_story_checkpoint(
const JsonValue& value, std::string_view source,
std::span<const StoryActionSpec> actions = builtin_story_actions()
);



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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Story Step JSON Tests
*
* From: https://github.com/PokemonAutomation/
*
*/

#ifndef PokemonAutomation_Pokemon_StoryStepJson_Tests_H
#define PokemonAutomation_Pokemon_StoryStepJson_Tests_H

#include "Common/Cpp/TestRunners/UnitTestDatabase.h"

namespace PokemonAutomation{
namespace Pokemon{


// Registers unit tests for the lossless shared story-data wire contract.
void add_tests_StoryStepJson(UnitTestDatabase& database);



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
#define PokemonAutomation_Pokemon_AutoStory_Tests_H

#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
#include "Data/Pokemon_StoryStepJson_Tests.h"
#include "Pokemon_StoryEvents_Tests.h"
#include "Pokemon_StoryFailureClasses_Tests.h"
#include "Pokemon_StoryLog_Tests.h"
#include "Pokemon_StoryStepExecutor_Tests.h"
#include "Types/Pokemon_StoryActionArg_Tests.h"
#include "Types/Pokemon_StoryRunTypes_Tests.h"

Expand All @@ -28,6 +30,8 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){
add_tests_StoryLog(database);
add_tests_StoryFailureClasses(database);
add_tests_StoryEvents(database);
add_tests_StoryStepExecutor(database);
add_tests_StoryStepJson(database);
add_tests_StoryRunTypes(database);
}

Expand Down
181 changes: 181 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/* Story Step Executor
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <set>
#include <utility>
#include <variant>
#include "Pokemon_BuiltinStoryActions.h"
#include "Pokemon_StoryStepExecutor.h"

namespace PokemonAutomation{
namespace Pokemon{


Pose pose_of(const GridPose& pose){
return tile_pose(pose.map, pose.tiles, pose.facing);
}

GridPose grid_pose_of(const DeclaredPose& pose){
if (const TileDeclared* tile = std::get_if<TileDeclared>(&pose)){
return GridPose{.map = tile->map, .tiles = tile->tiles, .facing = tile->facing};
}
return GridPose{.map = std::get<ZoneDeclared>(pose).place, .tiles = {}, .facing = std::nullopt};
}

Pose pose_of(const DeclaredPose& pose){
return pose_of(grid_pose_of(pose));
}

Outcome unsupported_step(const StoryStep& step){
return outcome_failed("unsupported_step", std::format("{} {}", story_step_kind_name(step.kind), step.name));
}


void StoryStepExecutor::begin_attempt(const StoryCheckpoint&){}

bool StoryStepExecutor::holds(std::string_view condition, RunState& state){
return state.flags.contains(std::string(condition));
}


Event story_event(const StoryStep& step, StoryStepExecutor& executor){
Event event;
event.name = step.name;
event.why = step.why;
for (const std::string& name : step.skip_if){
event.skip_if.push_back(condition(name, [&executor, name](RunState& state){
return executor.holds(name, state);
}));
}
event.act = [&executor, &step](RunState& state){
return executor.run(step, state);
};
if (step.pose_after){
event.pose_learned = pose_of(*step.pose_after);
}
return event;
}

namespace{

// The first event of a checkpoint that starts from a save: where the save
// leaves the player, for a run that reloaded it.
Event from_save_event(const Pose& start){
Event event;
event.name = "from_save";
event.why = "where the checkpoint's save leaves the player";
event.act = [](RunState&){ return outcome_ok(); };
event.pose_learned = start;
return event;
}

} // namespace

namespace{

// `event`, for a once_per_save step known in `prepared` as `key`: skipped
// while `prepared` has it, and added to it when it succeeds changing
// nothing.
void run_once_per_save(std::string key, Event& event, PreparedSteps& prepared){
event.skip_if.push_back(condition("done_from_this_save", [&prepared, key](RunState&){
return prepared.contains(key);
}));
event.act = [act = std::move(event.act), &prepared, key](RunState& state) mutable{
Outcome outcome = act(state);
if (outcome.succeeded() && outcome.changed_nothing){
prepared.insert(key);
}
return outcome;
};
}

} // namespace

Checkpoint build_story_checkpoint(const StoryCheckpoint& data, StoryStepExecutor& executor, PreparedSteps* prepared){
executor.begin_attempt(data);
Checkpoint checkpoint;
checkpoint.id = data.id;
if (data.start){
checkpoint.events.push_back(from_save_event(pose_of(*data.start)));
}
for (size_t index = 0; index < data.steps.size(); index++){
const StoryStep& step = data.steps[index];
checkpoint.events.push_back(story_event(step, executor));
if (StoryPausePoints* pause = executor.pause_points()){
checkpoint.events.back().act = [
act = std::move(checkpoint.events.back().act), pause, label = story_step_pause_label(data.id, index, step.name)
](RunState& state) mutable{
pause->before_step(label);
return act(state);
};
}
if (prepared != nullptr && step.once_per_save){
run_once_per_save(std::format("{}/{}", data.id, step.name), checkpoint.events.back(), *prepared);
}
}
return checkpoint;
}


namespace{

// What is wrong with a step's action and args, each as a line: a GAME
// step without an action, args that don't fit a built-in action's spec,
// an empty leg.
std::vector<std::string> action_problems(const StoryStep& step){
if (step.kind == StoryStepKind::GAME){
return step.action.empty() ? std::vector<std::string>{"no action"} : std::vector<std::string>{};
}
const StoryActionSpec* spec = find_story_action(builtin_story_actions(), step.kind, step.action);
if (spec == nullptr){
// A game's own action: the game checks it when it loads the step.
return {};
}
std::vector<std::string> problems = story_args_errors(*spec, step.args);
const std::vector<StoryLeg>* legs = value_if_at_it<std::vector<StoryLeg>>(step.args, step.args.find("legs"));
if (legs != nullptr && legs->empty()){
problems.emplace_back("no legs");
}
for (const StoryLeg& leg : legs ? *legs : std::vector<StoryLeg>{}){
if (leg.tiles <= 0){
problems.emplace_back("a leg of no tiles");
}
}
return problems;
}

} // namespace

std::vector<std::string> lint_story_checkpoint(const StoryCheckpoint& data){
std::vector<std::string> problems;
if (data.id.empty()){
problems.emplace_back("a checkpoint has no id");
}
std::set<std::string> names;
for (size_t c = 0; c < data.steps.size(); c++){
const StoryStep& step = data.steps[c];
const std::string where = std::format("{} step {}", data.id, c + 1);
if (step.name.empty()){
problems.emplace_back(std::format("{}: no name", where));
}else if (!names.insert(step.name).second){
problems.emplace_back(std::format("{}: a second step named {}", where, step.name));
}
for (const std::string& problem : action_problems(step)){
problems.emplace_back(std::format("{} {}: {} {}", where, step.name, story_step_kind_name(step.kind), problem));
}
// skip_if's conditions must all hold; once per save is one more.
if (step.once_per_save && !step.skip_if.empty()){
problems.emplace_back(std::format("{} {}: once per save with skip_if", where, step.name));
}
}
return problems;
}



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