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,186 @@
/* Story Action Registry
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <algorithm>
#include <format>
#include <set>
#include <utility>
#include <variant>
#include "Pokemon/AutoStory/Pokemon_MapUtil.h"
#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h"
#include "Pokemon_StoryActionRegistry.h"
#include "Pokemon_StoryBuiltinActions.h"

namespace PokemonAutomation{
namespace Pokemon{


namespace{


// A walk of no legs, or a leg of no tiles.
inline std::vector<std::string> leg_problems(const StoryActionArgs& args){
const std::vector<StoryLeg>* legs = value_if_at_it<std::vector<StoryLeg>>(args, args.find("legs"));
if (legs == nullptr){
return {};
}
std::vector<std::string> problems;
if (legs->empty()){
problems.emplace_back("no legs");
}
for (const StoryLeg& leg : *legs){
if (leg.tiles <= 0){
problems.emplace_back("a leg of no tiles");
}
}
return problems;
}


// Why `declared` is no pose of a game whose poses are `tile` ones or not.
inline std::string undeclarable(const DeclaredPose& declared, bool tile){
const std::string name = std::holds_alternative<TileDeclared>(declared)
? std::get<TileDeclared>(declared).map
: std::get<ZoneDeclared>(declared).place;
return tile
? std::format("\"{}\" is a place, but this game's poses are tiles: declare a map, tiles and a facing", name)
: std::format("\"{}\" is a map, but this game has no tiles: declare a place, a spot and a heading", name);
}

// Whether an arg of `type` only means something on a tile grid.
inline bool tile_arg(StoryArgType type){
return type == StoryArgType::DIRECTION || type == StoryArgType::LEGS || type == StoryArgType::GRID_POSE;
}

// Each tile arg among `game`'s own actions, for a game with no tiles.
template <StoryPose PoseType>
std::vector<std::string> tile_arg_problems(const StoryGame<PoseType>& game){
std::vector<std::string> problems;
if constexpr (!StoryPoseTraits<PoseType>::TILE_MOVES){
for (const StoryAction<PoseType>& action : game.actions){
for (const StoryArgSpec& arg : action.spec.args){
if (tile_arg(arg.type)){
problems.push_back(std::format(
"action {} takes a tile argument \"{}\", but this game has no tiles", action.spec.name, arg.key
));
}
}
}
}
return problems;
}


} // namespace


template <StoryPose PoseType>
StoryActionRegistry<PoseType>::StoryActionRegistry(const StoryGame<PoseType>& game)
: m_game(game)
, m_actions(builtin_story_handlers<PoseType>())
, m_game_problems(tile_arg_problems(game))
, m_pause(game.breakpoints, game.pause_point)
{
// A game's action replaces the built-in of its kind and name in place,
// so a kind's default stays the built-in one's position.
for (const StoryAction<PoseType>& action : game.actions){
const auto same = std::find_if(m_actions.begin(), m_actions.end(), [&](const StoryAction<PoseType>& builtin){
return builtin.spec.kind == action.spec.kind && builtin.spec.name == action.spec.name;
});
if (same != m_actions.end()){
*same = action;
}else{
m_actions.push_back(action);
}
}
for (const StoryAction<PoseType>& action : m_actions){
m_specs.push_back(action.spec);
}
}

template <StoryPose PoseType>
const StoryAction<PoseType>* StoryActionRegistry<PoseType>::find(StoryStepKind kind, std::string_view name) const{
for (const StoryAction<PoseType>& action : m_actions){
if (action.spec.kind == kind && (name.empty() || action.spec.name == name)){
return &action;
}
}
return nullptr;
}

template <StoryPose PoseType>
std::vector<std::string> StoryActionRegistry<PoseType>::check(const StoryStep& step) const{
if (step.kind == StoryStepKind::GAME && step.action.empty()){
return {"no action"};
}
const StoryAction<PoseType>* action = find(step.kind, step.action);
if (action == nullptr){
return {std::format("unknown action \"{}\" for {}", step.action, story_step_kind_name(step.kind))};
}
std::vector<std::string> problems = story_args_errors(action->spec, step.args);
for (std::string& problem : leg_problems(step.args)){
problems.push_back(std::move(problem));
}
if constexpr (!StoryPoseTraits<PoseType>::TILE_MOVES){
for (const auto& [key, value] : step.args){
if (tile_arg(story_arg_type(value))){
problems.push_back(std::format("argument \"{}\" is a tile one, but this game has no tiles", key));
}
}
}
return problems;
}

template <StoryPose PoseType>
std::vector<std::string> StoryActionRegistry<PoseType>::lint(const StoryCheckpoint& checkpoint) const{
constexpr bool TILE = StoryPoseTraits<PoseType>::TILE_MOVES;
std::vector<std::string> problems = m_game_problems;
if (checkpoint.id.empty()){
problems.emplace_back("a checkpoint has no id");
}
if (checkpoint.start && !check_declared<PoseType>(*checkpoint.start)){
problems.push_back(std::format("{} start: {}", checkpoint.id, undeclarable(*checkpoint.start, TILE)));
}
std::set<std::string> names;
for (size_t c = 0; c < checkpoint.steps.size(); c++){
const StoryStep& step = checkpoint.steps[c];
const std::string where = std::format("{} step {} {}", checkpoint.id, c + 1, step.name);
if (step.name.empty() || !names.insert(step.name).second){
problems.push_back(std::format("{}: empty or repeated name", where));
}
for (const std::string& problem : check(step)){
problems.push_back(std::format("{}: {}", where, problem));
}
if (step.pose_after && !check_declared<PoseType>(*step.pose_after)){
problems.push_back(std::format("{}: declared_after: {}", where, undeclarable(*step.pose_after, TILE)));
}
}
return problems;
}

template <StoryPose PoseType>
Outcome StoryActionRegistry<PoseType>::dispatch(StoryBackend<PoseType>& backend, const StoryStep& step, RunState& state) const{
const StoryAction<PoseType>* action = find(step.kind, step.action);
if (action == nullptr || !action->handler){
return unsupported_step(step);
}
if (action->spec.console_only && backend.kind() != StoryBackendKind::CONSOLE){
return outcome_failed(std::string(CONSOLE_ONLY), std::format(
"{} runs only on a console, not on a {} backend", action->spec.name, story_backend_kind_name(backend.kind())
));
}
const StoryActionArgs args = with_fallbacks(action->spec, step.args);
return action->handler(backend, StoryActionCall{.step = step, .args = args, .pause = m_pause}, state);
}


template class StoryActionRegistry<GridPose>;
template class StoryActionRegistry<ZonePose>;



} // namespace Pokemon
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* Story Action Registry
*
* From: https://github.com/PokemonAutomation/
*
* A game's actions, looked up by a step's kind and action name: the
* shared built-ins (Pokemon_StoryBuiltinActions.h) with the game's own on
* top. It checks a step against its action's spec before anything runs,
* and runs a step's action with the spec's fallbacks filled in.
*
*/

#ifndef PokemonAutomation_Pokemon_StoryActionRegistry_H
#define PokemonAutomation_Pokemon_StoryActionRegistry_H

#include <string>
#include <string_view>
#include <vector>
#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h"
#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h"
#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h"
#include "Pokemon_StoryGame.h"

namespace PokemonAutomation{
namespace Pokemon{


// The failure of a console_only action (StoryActionSpec) on a backend that
// is not a console. Every attempt would fail the same way, so the engine
// does not reload for it.
inline constexpr std::string_view CONSOLE_ONLY = "console_only";

// `game`'s actions. `game` must outlive the registry.
template <StoryPose PoseType>
class StoryActionRegistry{

Check warning on line 34 in SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::StoryActionRegistry: public_methods=6 (advisory above 5, limit 7)
public:
explicit StoryActionRegistry(const StoryGame<PoseType>& game);
StoryActionRegistry(const StoryActionRegistry&) = delete;
StoryActionRegistry& operator=(const StoryActionRegistry&) = delete;

const StoryGame<PoseType>& game() const{
return m_game;
}

// Every action's spec, for the JSON reader (read_story_checkpoint()).
const std::vector<StoryActionSpec>& specs() const{
return m_specs;
}

// What is wrong with `step` before it runs: an action that is not
// registered, args that don't fit its spec, a walk of no legs.
std::vector<std::string> check(const StoryStep& step) const;

// check() for each step of `checkpoint`, its step names (none empty,
// none repeated), and its declared poses (its start, each step's
// declared_after): each must be the kind the game's pose type
// declares (check_declared()). In a game with no tiles, a tile arg
// (DIRECTION, LEGS, GRID_POSE) in a step or in one of the game's
// actions fails too.
std::vector<std::string> lint(const StoryCheckpoint& checkpoint) const;

// The pause points of this game's run (StoryGame::breakpoints and
// pause_point). Mutable state of the run, not of the game's actions.
StoryPausePoints& pause_points() const{
return m_pause;
}

// Runs `step`'s action with its args' fallbacks filled in; a step with
// no registered action fails as unsupported_step(), a console_only
// action on another backend as CONSOLE_ONLY, before any input.
Outcome dispatch(StoryBackend<PoseType>& backend, const StoryStep& step, RunState& state) const;

private:
// The action of `kind` named `name`; for an empty name, the kind's
// first. Null when there is none.
const StoryAction<PoseType>* find(StoryStepKind kind, std::string_view name) const;

const StoryGame<PoseType>& m_game;
std::vector<StoryAction<PoseType>> m_actions;
std::vector<StoryActionSpec> m_specs;
// What is wrong with the game's own actions, reported with every lint.
std::vector<std::string> m_game_problems;
mutable StoryPausePoints m_pause;
};

// Instantiated once, in Pokemon_StoryActionRegistry.cpp.
extern template class StoryActionRegistry<GridPose>;
// And for ZonePose (Pokemon_ZonePose.h).
extern template class StoryActionRegistry<ZonePose>;



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