Skip to content
Draft
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "Common/Qt/QtThreadPool_Tests.h"
#include "Kernels/Kernels_Tests.h"
#include "Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.h"
#include "Pokemon/AutoStory/Pokemon_AutoStory_Tests.h"
#include "Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h"
#include "CommonFramework/AudioPipeline/AudioInfo_Tests.h"
#include "CommonFramework/AudioPipeline/Tools/ChannelMixer.h"
Expand Down Expand Up @@ -78,6 +79,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){
Kernels::add_tests(ret);
Pokemon::add_tests_AutoStorySelect(ret);
Pokemon::add_tests_AutoStoryStats(ret);
Pokemon::add_tests_AutoStory(ret);
NintendoSwitch::add_tests_CheckOnlineDetector(ret);
NintendoSwitch::add_tests_FailedToConnectDetector(ret);
NintendoSwitch::add_tests_UpdatePopupDetector(ret);
Expand Down
30 changes: 30 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* AutoStory Tests
*
* From: https://github.com/PokemonAutomation/
*
* The unit tests of the pieces every AutoStory program shares, story
* engine or not. add_tests_AutoStory() is the one entry point the unit
* test runner calls; a new shared AutoStory suite registers there.
*
*/

#ifndef PokemonAutomation_Pokemon_AutoStory_Tests_H
#define PokemonAutomation_Pokemon_AutoStory_Tests_H

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

namespace PokemonAutomation{
namespace Pokemon{


// Registers every shared AutoStory suite, for the unit test runner.
inline void add_tests_AutoStory(UnitTestDatabase& database){
add_tests_StoryActionArg(database);
}



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

#include <utility>
#include <vector>
#include "Pokemon_BuiltinStoryActions.h"

namespace PokemonAutomation{
namespace Pokemon{


namespace{


StoryArgSpec required(std::string key, StoryArgType type, std::string why){
return StoryArgSpec{.key = std::move(key), .type = type, .required = true, .fallback = std::nullopt, .why = std::move(why)};
}

StoryArgSpec optional(std::string key, StoryArgType type, StoryArgValue fallback, std::string why){
return StoryArgSpec{.key = std::move(key), .type = type, .required = false, .fallback = std::move(fallback), .why = std::move(why)};
}

std::vector<StoryActionSpec> make_builtin_actions(){

Check warning on line 26 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::make_builtin_actions(): function_lines=31 (advisory above 25, limit 50)
const StoryArgSpec fight_wild = optional("fight_wild", StoryArgType::BOOLEAN, false, "fight wild battles on the way instead of running");
return {
{.kind = StoryStepKind::ROUTE, .name = "route", .args = {
required("target", StoryArgType::GRID_POSE, "where to go; with several tiles, any of them"),
fight_wild,
}},
{.kind = StoryStepKind::READY, .name = "ready", .args = {
required("target", StoryArgType::GRID_POSE, "the nurse"),
}},
{.kind = StoryStepKind::LEGS, .name = "legs", .args = {
required("legs", StoryArgType::LEGS, "the straight legs to walk"),
fight_wild,
}},
{.kind = StoryStepKind::STEP, .name = "face", .args = {
required("direction", StoryArgType::DIRECTION, "the way to step or face"),
}},
{.kind = StoryStepKind::ANSWER, .name = "answer", .args = {
optional("yes", StoryArgType::BOOLEAN, true, "the answer"),
}},
{.kind = StoryStepKind::BATTLE, .name = "fight", .args = {
optional("may_lose", StoryArgType::BOOLEAN, false, "losing goes on (a rival's first battle)"),
}},
{.kind = StoryStepKind::TRAIN, .name = "train", .args = {
required("battles", StoryArgType::INTEGER, "the battles to fight"),
}},
{.kind = StoryStepKind::SHOP, .name = "shop", .args = {
required("items", StoryArgType::ITEMS, "the items and how many of each"),
}},
};
}


} // namespace


std::span<const StoryActionSpec> builtin_story_actions(){
static const std::vector<StoryActionSpec> actions = make_builtin_actions();
return actions;
}

const StoryActionSpec* find_story_action(std::span<const StoryActionSpec> actions, StoryStepKind kind, std::string_view name){
for (const StoryActionSpec& action : actions){
if (action.kind == kind && (name.empty() || action.name == name)){
return &action;
}
}
return nullptr;
}



} // namespace Pokemon
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* Builtin Story Actions
*
* From: https://github.com/PokemonAutomation/
*
* The shared engine's built-in actions (a route, fixed legs, a battle, a
* shop...) with the args each takes, and reading a step's args. A game
* adds its own actions (GAME steps and its other ways of doing a kind) to
* these when it loads its tables.
*
*/

#ifndef PokemonAutomation_Pokemon_BuiltinStoryActions_H
#define PokemonAutomation_Pokemon_BuiltinStoryActions_H

#include <format>
#include <span>
#include <string>
#include <string_view>
#include "Common/Cpp/Exceptions.h"
#include "Pokemon_MapUtil.h"
#include "Types/Pokemon_StoryActionArg.h"
#include "Types/Pokemon_StoryActionSpec.h"
#include "Types/Pokemon_StoryStepKind.h"

namespace PokemonAutomation{
namespace Pokemon{


// The built-in actions: each kind's usual way. A kind's first one is its
// default, the action of a step that names none.
// route ROUTE target GRID_POSE (required), fight_wild BOOLEAN (false)
// ready READY target GRID_POSE (required): the nurse
// legs LEGS legs LEGS (required), fight_wild BOOLEAN (false)
// face STEP direction DIRECTION (required)
// answer ANSWER yes BOOLEAN (true)
// fight BATTLE may_lose BOOLEAN (false)
// train TRAIN battles INTEGER (required)
// shop SHOP items ITEMS (required)
// Other kinds (TALK, MASH, HEAL, SAVE, ...) take no args.
std::span<const StoryActionSpec> builtin_story_actions();

// The action of `kind` named `name` among `actions`; for an empty name,
// the kind's first. Null when there is none.
const StoryActionSpec* find_story_action(std::span<const StoryActionSpec> actions, StoryStepKind kind, std::string_view name);

// The value of arg `key`, which the step's action requires or gives a
// fallback, so the loaded step has it. Throws InternalProgramError when it
// is missing or of another type: a step built without its action's spec.
template <typename Type>
const Type& story_arg(const StoryActionArgs& args, std::string_view key);


template <typename Type>
const Type& story_arg(const StoryActionArgs& args, std::string_view key){
const Type* value = value_if_at_it<Type>(args, args.find(key));
if (value == nullptr){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("Missing or mistyped story arg: {}", key));
}
return *value;
}



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
44 changes: 44 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_MapUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Map Util
*
* From: https://github.com/PokemonAutomation/
*
* Lookups in a map through the iterator find() returned, so a call site
* says which lookup it does and never copies the value by accident:
* const int64_t* budget = value_if_at_it<int64_t>(args, args.find("budget"));
*
*/

#ifndef PokemonAutomation_Pokemon_MapUtil_H
#define PokemonAutomation_Pokemon_MapUtil_H

#include <variant>

namespace PokemonAutomation{
namespace Pokemon{


// The value at `it`, or null when `it` is `map`'s end.
template <typename Map>
const typename Map::mapped_type* value_at_it(const Map& map, typename Map::const_iterator it);

// The `Type` alternative of the variant at `it`, or null when `it` is
// `map`'s end or the variant holds another type.
template <typename Type, typename Map>
const Type* value_if_at_it(const Map& map, typename Map::const_iterator it);


template <typename Map>
const typename Map::mapped_type* value_at_it(const Map& map, typename Map::const_iterator it){
return it == map.end() ? nullptr : &it->second;
}

template <typename Type, typename Map>
const Type* value_if_at_it(const Map& map, typename Map::const_iterator it){
return it == map.end() ? nullptr : std::get_if<Type>(&it->second);
}



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
37 changes: 37 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_GridPose.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Grid Pose
*
* From: https://github.com/PokemonAutomation/
*
* Where a player stands on a 2D tile grid (FRLG, BDSP): a map, the tiles
* they may be on and the way they face. Games that move on a surface or
* in 3D (PLZA) need their own type.
*
*/

#ifndef PokemonAutomation_Pokemon_GridPose_H
#define PokemonAutomation_Pokemon_GridPose_H

#include <optional>
#include <string>
#include <vector>
#include "Pokemon/Navigation/Pokemon_Grid.h"

namespace PokemonAutomation{
namespace Pokemon{


// One of `tiles` of `map`, facing `facing` if set. As a target, any of the
// tiles will do.
struct GridPose{
std::string map;
std::vector<TilePos> tiles;
std::optional<Direction> facing;

bool operator==(const GridPose& other) const = default;
};



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

#include "Pokemon_StoryActionArg.h"

namespace PokemonAutomation{
namespace Pokemon{


StoryArgType story_arg_type(const StoryArgValue& value){
// The alternatives are declared in StoryArgType's order. A variant left
// valueless by an exception would give variant_npos; none is kept.
return static_cast<StoryArgType>(value.index());
}



} // namespace Pokemon
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* Story Action Arg

Check warning on line 1 in SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.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/
*
* A step's action and its arguments (StoryStep::action, StoryStep::args),
* e.g. a Mart step's {"map": "CeruleanCity_Mart", "budget": 3000}. Each
* action declares the arguments it takes (Pokemon_StoryActionSpec.h), with their
* types and defaults; the JSON reader (Data/Pokemon_StoryStepJson.h)
* parses a step's args against them and fills in the defaults, so a typo
* fails at load instead of mid-run and nothing after it reads JSON.
*
*/

#ifndef PokemonAutomation_Pokemon_StoryActionArg_H
#define PokemonAutomation_Pokemon_StoryActionArg_H

#include <cstddef>
#include <cstdint>
#include <functional>
#include <map>
#include <string>
#include <variant>
#include <vector>
#include "Pokemon/Navigation/Pokemon_Grid.h"
#include "Pokemon_GridPose.h"

namespace PokemonAutomation{
namespace Pokemon{


// A straight leg of a fixed walk.
struct StoryLeg{
Direction direction = Direction::NORTH;
int tiles = 0;

bool operator==(const StoryLeg& other) const = default;
};

// A leg of a timed walk in a game with no tiles: turn `turn` degrees
// (clockwise, from the way the player faces), then walk for `ms`.
struct StoryTimedLeg{
int turn = 0;
int ms = 0;

bool operator==(const StoryTimedLeg& other) const = default;
};

// An item and how many to get.
struct StoryItem{
std::string item;
size_t quantity = 0;

bool operator==(const StoryItem& other) const = default;
};


// The type an action declares for an argument, in StoryArgValue's order.
enum class StoryArgType{
BOOLEAN,
INTEGER,
WORD,
DIRECTION,
GRID_POSE,
LEGS,
WORDS,
ITEMS,
INTEGER_ROWS,
TIMED_LEGS,
};

// One argument's value; the index is its StoryArgType.
using StoryArgValue = std::variant<
bool, int64_t, std::string, // BOOLEAN, INTEGER, WORD
Direction, GridPose, // DIRECTION, GRID_POSE
std::vector<StoryLeg>, // LEGS
std::vector<std::string>, // WORDS
std::vector<StoryItem>, // ITEMS
std::vector<std::vector<int64_t>>, // INTEGER_ROWS: rows of any length, [[12, 7], [3, 9, 1], []]
std::vector<StoryTimedLeg> // TIMED_LEGS: [turn degrees, ms] pairs, [[0, 4500], [90, 1200]]
>;
static_assert(std::variant_size_v<StoryArgValue> == (size_t)StoryArgType::TIMED_LEGS + 1, "one StoryArgType per StoryArgValue type");

// A step's arguments by key.
using StoryActionArgs = std::map<std::string, StoryArgValue, std::less<>>;

// The type `value` holds.
StoryArgType story_arg_type(const StoryArgValue& value);



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