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
4 changes: 4 additions & 0 deletions SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_Actions_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_GridSim_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_RoutePlanner_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_Segment00_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryMaps_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_StorySim_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_StorySimulation_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.h"
Expand All @@ -34,6 +36,8 @@ void add_tests(UnitTestDatabase& database){
add_tests_AutoStory_StoryTable(database);
add_tests_AutoStory_Walk(database);
add_tests_AutoStory_PlannedRoute(database);
add_tests_AutoStory_GridSim(database);
add_tests_AutoStory_StorySim(database);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/* BDSP AutoStory - Coord events
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <map>
#include "Common/Cpp/Exceptions.h"
#include "Common/Cpp/Json/JsonArray.h"
#include "Common/Cpp/Json/JsonObject.h"
#include "Common/Cpp/Json/JsonValue.h"
#include "PokemonBDSP_AutoStory_Maps.h"
#include "PokemonBDSP_AutoStory_CoordEvents.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonBDSP{

using Pokemon::TilePos;


namespace{

int read_int(const JsonObject& object, const std::string& key, const std::string& where){
const JsonValue* value = object.get_value(key);
if (value == nullptr || !value->is_integer()){
throw ParseException(where + key + " is missing or not an integer.");
}
return (int)value->to_integer_default();
}

std::string read_string(const JsonObject& object, const std::string& key, const std::string& where){
const JsonValue* value = object.get_value(key);
const std::string* text = value == nullptr ? nullptr : value->to_string();
if (text == nullptr){
throw ParseException(where + key + " is missing or not a string.");
}
return *text;
}

CoordEvent read_event(const JsonObject& object, const std::string& where){
CoordEvent event{
.name = read_string(object, "name", where),
.tiles = {},
.var = read_string(object, "var", where),
.value = read_int(object, "value", where),
.source = read_string(object, "source", where),
};
const int x = read_int(object, "x", where);
const int y = read_int(object, "y", where);
const int width = read_int(object, "width", where);
const int length = read_int(object, "length", where);
if (width < 1 || length < 1){
throw ParseException(where + "width and length must be at least 1.");
}
for (int row = y; row < y + length; row++){
for (int col = x; col < x + width; col++){
event.tiles.push_back({col, row});
}
}
return event;
}

std::vector<CoordEvent> read_events(const std::string& name, const std::string& json){
std::vector<CoordEvent> events;
const JsonValue root = parse_json(json);
const JsonObject* object = root.to_object();
const JsonValue* list = object == nullptr ? nullptr : object->get_value("coord_events");
if (list == nullptr){
return events;
}
const JsonArray* array = list->to_array();
if (array == nullptr){
throw ParseException(name + ".json: coord_events is not an array.");
}
for (size_t c = 0; c < array->size(); c++){
const std::string where = std::format("{}.json: coord_events[{}].", name, c);
const JsonObject* item = (*array)[c].to_object();
if (item == nullptr){
throw ParseException(where + " is not an object.");
}
events.push_back(read_event(*item, where));
}
return events;
}

} // namespace


const std::vector<CoordEvent>& story_coord_events(std::string_view map){
static const std::map<std::string, std::vector<CoordEvent>, std::less<>> by_map = []{
std::map<std::string, std::vector<CoordEvent>, std::less<>> out;
for (const auto& [name, json] : bdsp_map_json()){
out.emplace(name, read_events(name, json));
}
return out;
}();
if (auto iter = by_map.find(map); iter != by_map.end()){
return iter->second;
}
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("No BDSP story map named \"{}\".", map));
}



} // namespace PokemonBDSP
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* BDSP AutoStory - Coord events
*
* From: https://github.com/PokemonAutomation/
*
* The scene triggers of the story maps: stepping onto one of an event's
* tiles while its story variable holds `value` starts its script. They are
* the decomp's coord events (zone_event), read from a map's
* "coord_events", which the shared map schema leaves to the game:
*
* "coord_events": [{"name": "catch_tutorial", "x": 21, "y": 25, "width": 1, "length": 5,
* "var": "VAR_ROUTE_202_STATE", "value": 0, "source": "..."}, ...]
*
* A trigger seen on the console that the decomp does not have names the run
* in `source`.
*
*/

#ifndef PokemonAutomation_PokemonBDSP_AutoStory_CoordEvents_H
#define PokemonAutomation_PokemonBDSP_AutoStory_CoordEvents_H

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

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonBDSP{


// Coordinate-triggered event parsed from BDSP map data.
struct CoordEvent{
std::string name;
// The map's own tiles: `width` columns from x, `length` rows from y.
std::vector<Pokemon::TilePos> tiles;
std::string var;
int value = 0;
std::string source;
};

// The coord events of the map `map` (a JSON map's name), none if it lists
// none. Throws InternalProgramError for an unknown map, and ParseException
// for an event that does not parse.
const std::vector<CoordEvent>& story_coord_events(std::string_view map);



} // namespace PokemonBDSP
} // namespace NintendoSwitch
} // namespace PokemonAutomation
#endif
Loading
Loading