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,44 @@
/* Screen Observation
*
* From: https://github.com/PokemonAutomation/
*
* What one look at the game showed, as facts rather than pixels: the
* events on screen (a fade, a dialog box, a battle) and named readings. A
* console fills it from a frame with the game's detectors; a simulation
* fills it from its model and has no frame. Code that decides on an
* observation alone therefore runs in both.
*
*/

#ifndef PokemonAutomation_CommonTools_ScreenObservation_H
#define PokemonAutomation_CommonTools_ScreenObservation_H

#include <functional>
#include <map>
#include <optional>
#include <string>
#include "Common/Cpp/Time.h"
#include "CommonFramework/VideoPipeline/VideoFeed.h"
#include "CommonTools/ScreenEvents/ScreenEventTypes.h"

namespace PokemonAutomation{


// One look at the game.
struct ScreenObservation{
// When the look was taken. Only a default; no reader treats min() as
// "not set".
WallClock when = WallClock::min();
ScreenEvents events = {};
// Named numbers the game's readers found, by names the game chooses;
// "heading" is degrees clockwise from north, [0, 360).
std::map<std::string, int, std::less<>> readings = {};
// The frame looked at; none in a simulation. A state that needs it
// can only run on a console.
std::optional<VideoSnapshot> frame = std::nullopt;
};



} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* Story Action Not Implemented
*
* From: https://github.com/PokemonAutomation/
*
*/

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

namespace PokemonAutomation{
namespace Pokemon{


StoryActionNotImplemented::StoryActionNotImplemented(std::string message)
: OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, std::move(message))
{}



} // namespace Pokemon
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Story Action Not Implemented
*
* From: https://github.com/PokemonAutomation/
*
*/

#ifndef PokemonAutomation_Pokemon_StoryActionNotImplemented_H
#define PokemonAutomation_Pokemon_StoryActionNotImplemented_H

#include <string>
#include "CommonFramework/Exceptions/OperationFailedException.h"

namespace PokemonAutomation{
namespace Pokemon{


// A story action the game has no code for yet, thrown with what it waits
// for instead of pressing buttons blind. A game's backend lets it end the
// run instead of reloading and trying again, since every attempt would
// fail the same way.
class StoryActionNotImplemented : public OperationFailedException{
public:
explicit StoryActionNotImplemented(std::string message);

// code-health: allow char_pointer -- Exception::name() returns a const char pointer in the base class
virtual const char* name() const override{

Check warning on line 26 in SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (bypass)

char_pointer bypassed by the tag at line 25: "Exception::name() returns a const char pointer in the base class"

Check warning on line 26 in SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (bypass)

char_pointer bypassed by the tag at line 25: "Exception::name() returns a const char pointer in the base class"
return "StoryActionNotImplemented";
}
};



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

#include "Pokemon_StoryBackend.h"

namespace PokemonAutomation{
namespace Pokemon{


std::string_view story_backend_kind_name(StoryBackendKind kind){
switch (kind){
case StoryBackendKind::CONSOLE:
return "console";
case StoryBackendKind::SIMULATION:
return "simulation";
case StoryBackendKind::SCRIPT:
return "script";
}
return "unknown";
}

std::optional<int> story_heading(const ScreenObservation& observation){
const auto reading = observation.readings.find(STORY_HEADING);
if (reading == observation.readings.end()){
return std::nullopt;
}
return reading->second;
}



} // namespace Pokemon
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* Story Backend

Check warning on line 1 in SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<header>: header_symbols=7 (advisory above 6, limit 8)

Check warning on line 1 in SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<header>: header_symbols=7 (advisory above 6, limit 8)
*
* From: https://github.com/PokemonAutomation/
*
* Input and observation, nothing else: what the story engine drives. One
* per run. A console backend wraps the console's controller and video and
* builds each observation with the game's detectors; a simulation backend
* wraps a model of the game. The engine and every action run the same on
* both.
*
*/

#ifndef PokemonAutomation_Pokemon_StoryBackend_H
#define PokemonAutomation_Pokemon_StoryBackend_H

#include <optional>
#include <string_view>
#include "Common/Cpp/CancellableScope.h"
#include "Common/Cpp/Time.h"
#include "CommonTools/ScreenMachine/ScreenObservation.h"
#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h"
#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h"
#include "Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h"
#include "Pokemon/Navigation/Pokemon_Grid.h"
#include "Pokemon_StoryPoseConcept.h"
#include "Pokemon_ZonePose.h"

namespace PokemonAutomation{
namespace Pokemon{


// How fast a held direction moves the player.
enum class StoryPace{
WALK,
RUN,
};

// What a backend plays on. An action declared console_only
// (StoryActionSpec) runs only on CONSOLE: it reads or presses through
// screens nothing else models (menus, a keyboard, a battle AI).
enum class StoryBackendKind{
CONSOLE,
SIMULATION,
SCRIPT, // a scripted fake, for unit tests
};

// "console", "simulation", "script".
std::string_view story_backend_kind_name(StoryBackendKind kind);

// The reading (ScreenObservation::readings) of the way the player faces in
// a game without tiles: degrees clockwise from north (straight on, for a
// camera-relative game), in [0, 360). A backend that can read it cheaply
// sets it in each observe(); otherwise a game's reader of this name does.
inline constexpr std::string_view STORY_HEADING = "heading";

// `observation`'s STORY_HEADING reading; none when nothing read it.
std::optional<int> story_heading(const ScreenObservation& observation);

// One look at the game, with where the player is when a reader knows it
// (StoryGame::readers fill `pose` and the readings).
template <StoryPose PoseType>
struct Observation : public ScreenObservation{
std::optional<PoseType> pose = std::nullopt;
};

// The engine's only way to the game.
// code-health: allow public_methods -- the agreed backend boundary is these low-level calls; splitting them would make every action take two objects
template <StoryPose PoseType>
class StoryBackend{

Check warning on line 69 in SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (bypass)

PokemonAutomation::Pokemon::StoryBackend: public_methods=10 over limit 7, bypassed by the tag at line 67: "the agreed backend boundary is these low-level calls; splitting them would make every action take two objects"

Check warning on line 69 in SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (bypass)

PokemonAutomation::Pokemon::StoryBackend: public_methods=10 over limit 7, bypassed by the tag at line 67: "the agreed backend boundary is these low-level calls; splitting them would make every action take two objects"
public:
virtual ~StoryBackend() = default;

virtual StoryBackendKind kind() const = 0;

virtual void press(NintendoSwitch::Button button, Milliseconds hold, Milliseconds release) = 0;
// One tile in `direction`, or a turn to face it.
virtual void tap(Direction direction) = 0;
virtual void hold(Direction direction, Milliseconds duration, StoryPace pace) = 0;
// The move stick held at `degrees` clockwise from north (straight on,
// for a camera-relative game) for `duration`, then let go for `release`
// (a turn's time to finish) like press(): the free-moving games' walk
// and turn. A tile game's hold() is this at a multiple of 90.
virtual void stick(double degrees, Milliseconds duration, Milliseconds release, StoryPace pace) = 0;
virtual void wait(Milliseconds duration) = 0;
// The game now, once every input sent so far has been done.
virtual Observation<PoseType> observe() = 0;
// A whole battle, from its start to the overworld. High level for now:
// a console fights with the game's battle code, a simulation with its
// battle model. `args` are the BATTLE step's.
virtual Outcome battle(const StoryActionArgs& args) = 0;
virtual void log(std::string_view line) = 0;
virtual CancellableScope& scope() = 0;
};



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
139 changes: 139 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryGame.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/* Story Game

Check warning on line 1 in SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryGame.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<header>: header_symbols=8 (advisory above 6, limit 8)

Check warning on line 1 in SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryGame.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<header>: header_symbols=8 (advisory above 6, limit 8)
*
* From: https://github.com/PokemonAutomation/
*
* What a game hands the story engine: its actions, its readers and its
* save, as data plus functions. No subclassing. The engine registers the
* shared built-in actions first (Pokemon_StoryBuiltinActions.h); a game's
* action with the same kind and name replaces the built-in one.
*
*/

#ifndef PokemonAutomation_Pokemon_StoryGame_H
#define PokemonAutomation_Pokemon_StoryGame_H

#include <functional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "Pokemon/AutoStory/Pokemon_StoryCheckpointTypes.h"
#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h"
#include "Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h"
#include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h"
#include "Pokemon/AutoStory/Types/Pokemon_StoryBreakpoints.h"
#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h"
#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h"
#include "Pokemon_StoryBackend.h"

namespace PokemonAutomation{
namespace Pokemon{


// What an action is asked to do: the step it runs (its name and why, for
// the log and for what an action tunes by step) and the step's args with
// their fallbacks filled in. Both live for the call only.
struct StoryActionCall{
const StoryStep& step;
const StoryActionArgs& args;
// The run's pause points: an action that walks legs calls
// before_leg() before each (Pokemon_StoryBreakpoints.h).
StoryPausePoints& pause;
};

// An action's code: reads its call's args and drives the backend. A
// succeeded action that knows where it left the player returns that pose
// in its Outcome.
template <StoryPose PoseType>
using StoryActionHandler = std::function<Outcome(StoryBackend<PoseType>& backend, const StoryActionCall& call, RunState& state)>;

// Fills an observation's pose or readings from what the backend saw: a
// console reads the frame (OCR, templates), a simulation its model.
template <StoryPose PoseType>
using StoryReader = std::function<void(StoryBackend<PoseType>& backend, Observation<PoseType>& observation)>;

// When the engine runs a reader.
enum class StoryReaderUse{
// Only for a step's skip_if condition of its name: it is slow, or
// needs a screen a checkpoint need not end on (PLZA's heading waits
// for the minimap and throws without it).
ON_REQUEST,
// Also after a checkpoint whose postconditions need what it supplies:
// it is cheap and safe on any screen a checkpoint ends on.
AFTER_CHECKPOINT,
};

// What the engine reads the game with. A reader whose name is a step's
// skip_if condition answers it: the condition holds when the reader sets
// a non-zero reading of that name.
template <StoryPose PoseType>
struct StoryGameReader{
std::string name = {};
// What it fills: "pose" (Observation::pose), a reading ("heading"), or
// every reading with a prefix ("bag.*", "flag.*").
std::vector<std::string> supplies = {};
// Opt in to the postcondition check with AFTER_CHECKPOINT.
StoryReaderUse use = StoryReaderUse::ON_REQUEST;
// It reads a console's screen: never run on another backend.
bool console_only = false;
StoryReader<PoseType> read = {};
};

// Whether `supplies` (StoryGameReader::supplies) covers `need`: the same
// name, or a prefix ending in "*" that `need` starts with.
inline bool story_reader_supplies(const std::vector<std::string>& supplies, std::string_view need){
for (const std::string& each : supplies){
if (each == need){
return true;
}
if (each.ends_with('*') && need.starts_with(std::string_view(each).substr(0, each.size() - 1))){
return true;
}
}
return false;
}

// An action: its spec (kind, name, args) and its code.
template <StoryPose PoseType>
struct StoryAction{
StoryActionSpec spec = {};
StoryActionHandler<PoseType> handler = {};
};

// A game, for the engine.
template <StoryPose PoseType>
struct StoryGame{
// "frlg", "bdsp", "plza".
std::string id = {};
// The game's own actions, and its replacements of built-in ones.
std::vector<StoryAction<PoseType>> actions = {};
// "pose", "bag", "heading", or a skip_if condition.
std::vector<StoryGameReader<PoseType>> readers = {};
// The game's save (the menu, or a wait for an autosave) at the end of
// `checkpoint`, or at its SAVE step. The engine decides whether to call
// it (Pokemon_StoryEnginePolicy.h).
std::function<Outcome(StoryBackend<PoseType>& backend, const StoryCheckpoint& checkpoint)> save = {};
// Back to the last save. A succeeded reload that knows where it left
// the player returns that pose in its Outcome.
std::function<Outcome(StoryBackend<PoseType>& backend)> reload = {};
// Called as each attempt at a checkpoint starts, to reset what the game
// keeps per attempt (trainers who can see us again). Optional.
std::function<void(const StoryCheckpoint& checkpoint)> begin_attempt = {};
// What can stop a step part-way (a wild battle, a trainer who saw
// us), built for each checkpoint run. Optional.
std::function<std::vector<Interrupt>(StoryBackend<PoseType>& backend)> interrupts = {};
// Where the run pauses for the user to look at the console, and what a
// pause does: the engine calls the hook with a label such as
// "12.03/5 pallet_shore" before each step, and the walk action with
// "12.03/5 pallet_shore leg 2/5 east 4 -> (10, 10)" before each leg
// (only with LEGS). Optional: with no hook, or OFF, a run never pauses.
// The hook returns when the run goes on, or when the run is cancelled.
StoryBreakpoints breakpoints = StoryBreakpoints::OFF;
StoryPauseHook pause_point = {};
};



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