Merge pull request #1857 from GriffinRichards/sync-mapjson

Sync mapjson
This commit is contained in:
GriffinR 2023-02-19 20:48:13 -05:00 committed by GitHub
commit f888021c8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 121 additions and 55 deletions

View File

@ -19,11 +19,11 @@
.4byte \script .4byte \script
.endm .endm
@ Defines an object event template for map data. Mirrors the struct layout of ObjectEventTemplate in include/global.fieldmap.h @ Defines an object event template for map data, to be used by a normal object. Mirrors the struct layout of ObjectEventTemplate in include/global.fieldmap.h
.macro object_event index:req, gfx:req, inConnection:req, x:req, y:req, elevation:req, movement_type:req, x_radius:req, y_radius:req, trainer_type:req, sight_radius_tree_etc:req, script:req, event_flag:req .macro object_event index:req, gfx:req, x:req, y:req, elevation:req, movement_type:req, x_radius:req, y_radius:req, trainer_type:req, sight_radius_tree_etc:req, script:req, event_flag:req
.byte \index .byte \index
.byte \gfx .byte \gfx
.byte \inConnection .byte OBJ_KIND_NORMAL
.space 1 @ Padding .space 1 @ Padding
.2byte \x, \y .2byte \x, \y
.byte \elevation .byte \elevation
@ -38,6 +38,22 @@
inc _num_npcs inc _num_npcs
.endm .endm
@ Defines an object event template for map data, to be used by a clone object. Mirrors the struct layout of ObjectEventTemplate in include/global.fieldmap.h
@ NOTE: The handling for this type of event does not exist in Emerald by default; it is exclusive to FRLG.
.macro clone_event index:req, gfx:req, x:req, y:req, target_local_id:req, target_map_id:req
.byte \index
.byte \gfx
.byte OBJ_KIND_CLONE
.space 1 @ Padding
.2byte \x, \y
.byte \target_local_id
.space 3 @ Padding
.2byte \target_map_id & 0xFF @ map num
.2byte \target_map_id >> 8 @ map group
.space 8 @ Padding
inc _num_npcs
.endm
@ Defines a warp event for map data. Mirrors the struct layout of WarpEvent in include/global.fieldmap.h @ Defines a warp event for map data. Mirrors the struct layout of WarpEvent in include/global.fieldmap.h
.macro warp_def x:req, y:req, elevation:req, warpId:req, map_id:req .macro warp_def x:req, y:req, elevation:req, warpId:req, map_id:req
.2byte \x, \y .2byte \x, \y
@ -49,12 +65,12 @@
.endm .endm
@ Defines a coord event for map data. Mirrors the struct layout of CoordEvent in include/global.fieldmap.h @ Defines a coord event for map data. Mirrors the struct layout of CoordEvent in include/global.fieldmap.h
.macro coord_event x:req, y:req, elevation:req, trigger:req, index:req, script:req .macro coord_event x:req, y:req, elevation:req, var:req, varValue:req, script:req
.2byte \x, \y .2byte \x, \y
.byte \elevation .byte \elevation
.space 1 @ Padding .space 1 @ Padding
.2byte \trigger .2byte \var
.2byte \index .2byte \varValue
.space 2 @ Padding .space 2 @ Padding
.4byte \script .4byte \script
inc _num_traps inc _num_traps

View File

@ -288,6 +288,9 @@
#define FIRST_DECORATION_SPRITE_GFX OBJ_EVENT_GFX_PICHU_DOLL #define FIRST_DECORATION_SPRITE_GFX OBJ_EVENT_GFX_PICHU_DOLL
#define OBJ_KIND_NORMAL 0
#define OBJ_KIND_CLONE 255 // Exclusive to FRLG
// Special object event local ids // Special object event local ids
#define OBJ_EVENT_ID_PLAYER 0xFF #define OBJ_EVENT_ID_PLAYER 0xFF
#define OBJ_EVENT_ID_CAMERA 0x7F #define OBJ_EVENT_ID_CAMERA 0x7F

View File

@ -67,7 +67,7 @@ struct ObjectEventTemplate
{ {
/*0x00*/ u8 localId; /*0x00*/ u8 localId;
/*0x01*/ u8 graphicsId; /*0x01*/ u8 graphicsId;
/*0x02*/ u8 inConnection; // Leftover from FRLG /*0x02*/ u8 kind; // Always OBJ_KIND_NORMAL in Emerald.
/*0x03*/ //u8 padding1; /*0x03*/ //u8 padding1;
/*0x04*/ s16 x; /*0x04*/ s16 x;
/*0x06*/ s16 y; /*0x06*/ s16 y;

View File

@ -1498,7 +1498,7 @@ u8 SpawnSpecialObjectEventParameterized(u8 graphicsId, u8 movementBehavior, u8 l
y -= MAP_OFFSET; y -= MAP_OFFSET;
objectEventTemplate.localId = localId; objectEventTemplate.localId = localId;
objectEventTemplate.graphicsId = graphicsId; objectEventTemplate.graphicsId = graphicsId;
objectEventTemplate.inConnection = 0; objectEventTemplate.kind = OBJ_KIND_NORMAL;
objectEventTemplate.x = x; objectEventTemplate.x = x;
objectEventTemplate.y = y; objectEventTemplate.y = y;
objectEventTemplate.elevation = elevation; objectEventTemplate.elevation = elevation;

View File

@ -29,6 +29,7 @@ using json11::Json;
#include "mapjson.h" #include "mapjson.h"
string version;
string read_text_file(string filepath) { string read_text_file(string filepath) {
ifstream in_file(filepath); ifstream in_file(filepath);
@ -61,7 +62,7 @@ void write_text_file(string filepath, string text) {
} }
string json_to_string(const Json &data, const string &field = "") { string json_to_string(const Json &data, const string &field = "", bool silent = false) {
const Json value = !field.empty() ? data[field] : data; const Json value = !field.empty() ? data[field] : data;
string output = ""; string output = "";
switch (value.type()) { switch (value.type()) {
@ -75,12 +76,14 @@ string json_to_string(const Json &data, const string &field = "") {
output = value.bool_value() ? "TRUE" : "FALSE"; output = value.bool_value() ? "TRUE" : "FALSE";
break; break;
default:{ default:{
if (!silent) {
string s = !field.empty() ? ("Value for '" + field + "'") : "JSON field"; string s = !field.empty() ? ("Value for '" + field + "'") : "JSON field";
FATAL_ERROR("%s is unexpected type; expected string, number, or bool.\n", s.c_str()); FATAL_ERROR("%s is unexpected type; expected string, number, or bool.\n", s.c_str());
} }
} }
}
if (output.empty()){ if (!silent && output.empty()) {
string s = !field.empty() ? ("Value for '" + field + "'") : "JSON field"; string s = !field.empty() ? ("Value for '" + field + "'") : "JSON field";
FATAL_ERROR("%s cannot be empty.\n", s.c_str()); FATAL_ERROR("%s cannot be empty.\n", s.c_str());
} }
@ -88,14 +91,14 @@ string json_to_string(const Json &data, const string &field = "") {
return output; return output;
} }
string generate_map_header_text(Json map_data, Json layouts_data, string version) { string generate_map_header_text(Json map_data, Json layouts_data) {
string map_layout_id = json_to_string(map_data, "layout"); string map_layout_id = json_to_string(map_data, "layout");
vector<Json> matched; vector<Json> matched;
for (auto &field : layouts_data["layouts"].array_items()) { for (auto &layout : layouts_data["layouts"].array_items()) {
if (map_layout_id == json_to_string(field, "id")) if (map_layout_id == json_to_string(layout, "id", true))
matched.push_back(field); matched.push_back(layout);
} }
if (matched.size() != 1) if (matched.size() != 1)
@ -123,28 +126,33 @@ string generate_map_header_text(Json map_data, Json layouts_data, string version
text << "\t.4byte " << mapName << "_MapScripts\n"; text << "\t.4byte " << mapName << "_MapScripts\n";
if (map_data.object_items().find("connections") != map_data.object_items().end() if (map_data.object_items().find("connections") != map_data.object_items().end()
&& map_data["connections"].array_items().size() > 0) && map_data["connections"].array_items().size() > 0 && json_to_string(map_data, "connections_no_include", true) != "TRUE")
text << "\t.4byte " << mapName << "_MapConnections\n"; text << "\t.4byte " << mapName << "_MapConnections\n";
else else
text << "\t.4byte 0x0\n"; text << "\t.4byte NULL\n";
text << "\t.2byte " << json_to_string(map_data, "music") << "\n" text << "\t.2byte " << json_to_string(map_data, "music") << "\n"
<< "\t.2byte " << json_to_string(layout, "id") << "\n" << "\t.2byte " << json_to_string(layout, "id") << "\n"
<< "\t.byte " << json_to_string(map_data, "region_map_section") << "\n" << "\t.byte " << json_to_string(map_data, "region_map_section") << "\n"
<< "\t.byte " << json_to_string(map_data, "requires_flash") << "\n" << "\t.byte " << json_to_string(map_data, "requires_flash") << "\n"
<< "\t.byte " << json_to_string(map_data, "weather") << "\n" << "\t.byte " << json_to_string(map_data, "weather") << "\n"
<< "\t.byte " << json_to_string(map_data, "map_type") << "\n" << "\t.byte " << json_to_string(map_data, "map_type") << "\n";
<< "\t.2byte 0\n";
if (version != "firered")
text << "\t.2byte 0\n";
if (version == "ruby") if (version == "ruby")
text << "\t.byte " << json_to_string(map_data, "show_map_name") << "\n"; text << "\t.byte " << json_to_string(map_data, "show_map_name") << "\n";
else if (version == "emerald") else if (version == "emerald" || version == "firered")
text << "\tmap_header_flags " text << "\tmap_header_flags "
<< "allow_cycling=" << json_to_string(map_data, "allow_cycling") << ", " << "allow_cycling=" << json_to_string(map_data, "allow_cycling") << ", "
<< "allow_escaping=" << json_to_string(map_data, "allow_escaping") << ", " << "allow_escaping=" << json_to_string(map_data, "allow_escaping") << ", "
<< "allow_running=" << json_to_string(map_data, "allow_running") << ", " << "allow_running=" << json_to_string(map_data, "allow_running") << ", "
<< "show_map_name=" << json_to_string(map_data, "show_map_name") << "\n"; << "show_map_name=" << json_to_string(map_data, "show_map_name") << "\n";
if (version == "firered")
text << "\t.byte " << json_to_string(map_data, "floor_number") << "\n";
text << "\t.byte " << json_to_string(map_data, "battle_scene") << "\n\n"; text << "\t.byte " << json_to_string(map_data, "battle_scene") << "\n\n";
return text.str(); return text.str();
@ -193,8 +201,12 @@ string generate_map_events_text(Json map_data) {
text << objects_label << ":\n"; text << objects_label << ":\n";
for (unsigned int i = 0; i < map_data["object_events"].array_items().size(); i++) { for (unsigned int i = 0; i < map_data["object_events"].array_items().size(); i++) {
auto obj_event = map_data["object_events"].array_items()[i]; auto obj_event = map_data["object_events"].array_items()[i];
string type = json_to_string(obj_event, "type", true);
// If no type field is present, assume it's a regular object event.
if (type == "" || type == "object") {
text << "\tobject_event " << i + 1 << ", " text << "\tobject_event " << i + 1 << ", "
<< json_to_string(obj_event, "graphics_id") << ", 0, " << json_to_string(obj_event, "graphics_id") << ", "
<< json_to_string(obj_event, "x") << ", " << json_to_string(obj_event, "x") << ", "
<< json_to_string(obj_event, "y") << ", " << json_to_string(obj_event, "y") << ", "
<< json_to_string(obj_event, "elevation") << ", " << json_to_string(obj_event, "elevation") << ", "
@ -205,10 +217,20 @@ string generate_map_events_text(Json map_data) {
<< json_to_string(obj_event, "trainer_sight_or_berry_tree_id") << ", " << json_to_string(obj_event, "trainer_sight_or_berry_tree_id") << ", "
<< json_to_string(obj_event, "script") << ", " << json_to_string(obj_event, "script") << ", "
<< json_to_string(obj_event, "flag") << "\n"; << json_to_string(obj_event, "flag") << "\n";
} else if (type == "clone") {
text << "\tclone_event " << i + 1 << ", "
<< json_to_string(obj_event, "graphics_id") << ", "
<< json_to_string(obj_event, "x") << ", "
<< json_to_string(obj_event, "y") << ", "
<< json_to_string(obj_event, "target_local_id") << ", "
<< json_to_string(obj_event, "target_map") << "\n";
} else {
FATAL_ERROR("Unknown object event type '%s'. Expected 'object' or 'clone'.\n", type.c_str());
}
} }
text << "\n"; text << "\n";
} else { } else {
objects_label = "0x0"; objects_label = "NULL";
} }
if (map_data["warp_events"].array_items().size() > 0) { if (map_data["warp_events"].array_items().size() > 0) {
@ -224,14 +246,15 @@ string generate_map_events_text(Json map_data) {
} }
text << "\n"; text << "\n";
} else { } else {
warps_label = "0x0"; warps_label = "NULL";
} }
if (map_data["coord_events"].array_items().size() > 0) { if (map_data["coord_events"].array_items().size() > 0) {
coords_label = mapName + "_MapCoordEvents"; coords_label = mapName + "_MapCoordEvents";
text << coords_label << ":\n"; text << coords_label << ":\n";
for (auto &coord_event : map_data["coord_events"].array_items()) { for (auto &coord_event : map_data["coord_events"].array_items()) {
if (json_to_string(coord_event, "type") == "trigger") { string type = json_to_string(coord_event, "type");
if (type == "trigger") {
text << "\tcoord_event " text << "\tcoord_event "
<< json_to_string(coord_event, "x") << ", " << json_to_string(coord_event, "x") << ", "
<< json_to_string(coord_event, "y") << ", " << json_to_string(coord_event, "y") << ", "
@ -240,24 +263,27 @@ string generate_map_events_text(Json map_data) {
<< json_to_string(coord_event, "var_value") << ", " << json_to_string(coord_event, "var_value") << ", "
<< json_to_string(coord_event, "script") << "\n"; << json_to_string(coord_event, "script") << "\n";
} }
else if (coord_event["type"] == "weather") { else if (type == "weather") {
text << "\tcoord_weather_event " text << "\tcoord_weather_event "
<< json_to_string(coord_event, "x") << ", " << json_to_string(coord_event, "x") << ", "
<< json_to_string(coord_event, "y") << ", " << json_to_string(coord_event, "y") << ", "
<< json_to_string(coord_event, "elevation") << ", " << json_to_string(coord_event, "elevation") << ", "
<< json_to_string(coord_event, "weather") << "\n"; << json_to_string(coord_event, "weather") << "\n";
} else {
FATAL_ERROR("Unknown coord event type '%s'. Expected 'trigger' or 'weather'.\n", type.c_str());
} }
} }
text << "\n"; text << "\n";
} else { } else {
coords_label = "0x0"; coords_label = "NULL";
} }
if (map_data["bg_events"].array_items().size() > 0) { if (map_data["bg_events"].array_items().size() > 0) {
bgs_label = mapName + "_MapBGEvents"; bgs_label = mapName + "_MapBGEvents";
text << bgs_label << ":\n"; text << bgs_label << ":\n";
for (auto &bg_event : map_data["bg_events"].array_items()) { for (auto &bg_event : map_data["bg_events"].array_items()) {
if (bg_event["type"] == "sign") { string type = json_to_string(bg_event, "type");
if (type == "sign") {
text << "\tbg_sign_event " text << "\tbg_sign_event "
<< json_to_string(bg_event, "x") << ", " << json_to_string(bg_event, "x") << ", "
<< json_to_string(bg_event, "y") << ", " << json_to_string(bg_event, "y") << ", "
@ -265,25 +291,33 @@ string generate_map_events_text(Json map_data) {
<< json_to_string(bg_event, "player_facing_dir") << ", " << json_to_string(bg_event, "player_facing_dir") << ", "
<< json_to_string(bg_event, "script") << "\n"; << json_to_string(bg_event, "script") << "\n";
} }
else if (bg_event["type"] == "hidden_item") { else if (type == "hidden_item") {
text << "\tbg_hidden_item_event " text << "\tbg_hidden_item_event "
<< json_to_string(bg_event, "x") << ", " << json_to_string(bg_event, "x") << ", "
<< json_to_string(bg_event, "y") << ", " << json_to_string(bg_event, "y") << ", "
<< json_to_string(bg_event, "elevation") << ", " << json_to_string(bg_event, "elevation") << ", "
<< json_to_string(bg_event, "item") << ", " << json_to_string(bg_event, "item") << ", "
<< json_to_string(bg_event, "flag") << "\n"; << json_to_string(bg_event, "flag");
if (version == "firered") {
text << ", "
<< json_to_string(bg_event, "quantity") << ", "
<< json_to_string(bg_event, "underfoot");
} }
else if (bg_event["type"] == "secret_base") { text << "\n";
}
else if (type == "secret_base") {
text << "\tbg_secret_base_event " text << "\tbg_secret_base_event "
<< json_to_string(bg_event, "x") << ", " << json_to_string(bg_event, "x") << ", "
<< json_to_string(bg_event, "y") << ", " << json_to_string(bg_event, "y") << ", "
<< json_to_string(bg_event, "elevation") << ", " << json_to_string(bg_event, "elevation") << ", "
<< json_to_string(bg_event, "secret_base_id") << "\n"; << json_to_string(bg_event, "secret_base_id") << "\n";
} else {
FATAL_ERROR("Unknown bg event type '%s'. Expected 'sign', 'hidden_item', or 'secret_base'.\n", type.c_str());
} }
} }
text << "\n"; text << "\n";
} else { } else {
bgs_label = "0x0"; bgs_label = "NULL";
} }
text << mapName << "_MapEvents::\n" text << mapName << "_MapEvents::\n"
@ -299,7 +333,7 @@ string get_directory_name(string filename) {
return filename.substr(0, dir_pos + 1); return filename.substr(0, dir_pos + 1);
} }
void process_map(string map_filepath, string layouts_filepath, string version) { void process_map(string map_filepath, string layouts_filepath) {
string mapdata_err, layouts_err; string mapdata_err, layouts_err;
string mapdata_json_text = read_text_file(map_filepath); string mapdata_json_text = read_text_file(map_filepath);
@ -313,7 +347,7 @@ void process_map(string map_filepath, string layouts_filepath, string version) {
if (layouts_data == Json()) if (layouts_data == Json())
FATAL_ERROR("%s\n", layouts_err.c_str()); FATAL_ERROR("%s\n", layouts_err.c_str());
string header_text = generate_map_header_text(map_data, layouts_data, version); string header_text = generate_map_header_text(map_data, layouts_data);
string events_text = generate_map_events_text(map_data); string events_text = generate_map_events_text(map_data);
string connections_text = generate_map_connections_text(map_data); string connections_text = generate_map_connections_text(map_data);
@ -484,6 +518,7 @@ string generate_layout_headers_text(Json layouts_data) {
text << "@\n@ DO NOT MODIFY THIS FILE! It is auto-generated from data/layouts/layouts.json\n@\n\n"; text << "@\n@ DO NOT MODIFY THIS FILE! It is auto-generated from data/layouts/layouts.json\n@\n\n";
for (auto &layout : layouts_data["layouts"].array_items()) { for (auto &layout : layouts_data["layouts"].array_items()) {
if (layout == Json::object()) continue;
string layoutName = json_to_string(layout, "name"); string layoutName = json_to_string(layout, "name");
string border_label = layoutName + "_Border"; string border_label = layoutName + "_Border";
string blockdata_label = layoutName + "_Blockdata"; string blockdata_label = layoutName + "_Blockdata";
@ -498,7 +533,13 @@ string generate_layout_headers_text(Json layouts_data) {
<< "\t.4byte " << border_label << "\n" << "\t.4byte " << border_label << "\n"
<< "\t.4byte " << blockdata_label << "\n" << "\t.4byte " << blockdata_label << "\n"
<< "\t.4byte " << json_to_string(layout, "primary_tileset") << "\n" << "\t.4byte " << json_to_string(layout, "primary_tileset") << "\n"
<< "\t.4byte " << json_to_string(layout, "secondary_tileset") << "\n\n"; << "\t.4byte " << json_to_string(layout, "secondary_tileset") << "\n";
if (version == "firered") {
text << "\t.byte " << json_to_string(layout, "border_width") << "\n"
<< "\t.byte " << json_to_string(layout, "border_height") << "\n"
<< "\t.2byte 0\n";
}
text << "\n";
} }
return text.str(); return text.str();
@ -512,8 +553,11 @@ string generate_layouts_table_text(Json layouts_data) {
text << "\t.align 2\n" text << "\t.align 2\n"
<< json_to_string(layouts_data, "layouts_table_label") << "::\n"; << json_to_string(layouts_data, "layouts_table_label") << "::\n";
for (auto &layout : layouts_data["layouts"].array_items()) for (auto &layout : layouts_data["layouts"].array_items()) {
text << "\t.4byte " << json_to_string(layout, "name") << "\n"; string layout_name = json_to_string(layout, "name", true);
if (layout_name.empty()) layout_name = "NULL";
text << "\t.4byte " << layout_name << "\n";
}
return text.str(); return text.str();
} }
@ -526,9 +570,12 @@ string generate_layouts_constants_text(Json layouts_data) {
text << "//\n// DO NOT MODIFY THIS FILE! It is auto-generated from data/layouts/layouts.json\n//\n\n"; text << "//\n// DO NOT MODIFY THIS FILE! It is auto-generated from data/layouts/layouts.json\n//\n\n";
int i = 0; int i = 1;
for (auto &layout : layouts_data["layouts"].array_items()) for (auto &layout : layouts_data["layouts"].array_items()) {
text << "#define " << json_to_string(layout, "id") << " " << ++i << "\n"; if (layout != Json::object())
text << "#define " << json_to_string(layout, "id") << " " << i << "\n";
i++;
}
text << "\n#endif // GUARD_CONSTANTS_LAYOUTS_H\n"; text << "\n#endif // GUARD_CONSTANTS_LAYOUTS_H\n";
@ -559,9 +606,9 @@ int main(int argc, char *argv[]) {
FATAL_ERROR("USAGE: mapjson <mode> <game-version> [options]\n"); FATAL_ERROR("USAGE: mapjson <mode> <game-version> [options]\n");
char *version_arg = argv[2]; char *version_arg = argv[2];
string version(version_arg); version = string(version_arg);
if (version != "emerald" && version != "ruby") if (version != "emerald" && version != "ruby" && version != "firered")
FATAL_ERROR("ERROR: <game-version> must be 'emerald' or 'ruby'.\n"); FATAL_ERROR("ERROR: <game-version> must be 'emerald', 'firered', or 'ruby'.\n");
char *mode_arg = argv[1]; char *mode_arg = argv[1];
string mode(mode_arg); string mode(mode_arg);
@ -575,7 +622,7 @@ int main(int argc, char *argv[]) {
string filepath(argv[3]); string filepath(argv[3]);
string layouts_filepath(argv[4]); string layouts_filepath(argv[4]);
process_map(filepath, layouts_filepath, version); process_map(filepath, layouts_filepath);
} }
else if (mode == "groups") { else if (mode == "groups") {
if (argc != 4) if (argc != 4)