Sync mapjson

This commit is contained in:
GriffinR 2023-02-08 12:10:17 -05:00
parent bbf0cf0103
commit db5edc49d7

View File

@ -29,6 +29,7 @@ using json11::Json;
#include "mapjson.h"
string version;
string read_text_file(string 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;
string output = "";
switch (value.type()) {
@ -75,12 +76,14 @@ string json_to_string(const Json &data, const string &field = "") {
output = value.bool_value() ? "TRUE" : "FALSE";
break;
default:{
if (!silent) {
string s = !field.empty() ? ("Value for '" + field + "'") : "JSON field";
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";
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;
}
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");
vector<Json> matched;
for (auto &field : layouts_data["layouts"].array_items()) {
if (map_layout_id == json_to_string(field, "id"))
matched.push_back(field);
for (auto &layout : layouts_data["layouts"].array_items()) {
if (map_layout_id == json_to_string(layout, "id", true))
matched.push_back(layout);
}
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";
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";
else
text << "\t.4byte 0x0\n";
text << "\t.4byte NULL\n";
text << "\t.2byte " << json_to_string(map_data, "music") << "\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, "requires_flash") << "\n"
<< "\t.byte " << json_to_string(map_data, "weather") << "\n"
<< "\t.byte " << json_to_string(map_data, "map_type") << "\n"
<< "\t.2byte 0\n";
<< "\t.byte " << json_to_string(map_data, "map_type") << "\n";
if (version != "firered")
text << "\t.2byte 0\n";
if (version == "ruby")
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 "
<< "allow_cycling=" << json_to_string(map_data, "allow_cycling") << ", "
<< "allow_escaping=" << json_to_string(map_data, "allow_escaping") << ", "
<< "allow_running=" << json_to_string(map_data, "allow_running") << ", "
<< "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";
return text.str();
@ -193,6 +201,10 @@ string generate_map_events_text(Json map_data) {
text << objects_label << ":\n";
for (unsigned int i = 0; i < map_data["object_events"].array_items().size(); 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 << ", "
<< json_to_string(obj_event, "graphics_id") << ", 0, "
<< json_to_string(obj_event, "x") << ", "
@ -205,10 +217,18 @@ string generate_map_events_text(Json map_data) {
<< json_to_string(obj_event, "trainer_sight_or_berry_tree_id") << ", "
<< json_to_string(obj_event, "script") << ", "
<< 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";
}
}
text << "\n";
} else {
objects_label = "0x0";
objects_label = "NULL";
}
if (map_data["warp_events"].array_items().size() > 0) {
@ -224,14 +244,15 @@ string generate_map_events_text(Json map_data) {
}
text << "\n";
} else {
warps_label = "0x0";
warps_label = "NULL";
}
if (map_data["coord_events"].array_items().size() > 0) {
coords_label = mapName + "_MapCoordEvents";
text << coords_label << ":\n";
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 "
<< json_to_string(coord_event, "x") << ", "
<< json_to_string(coord_event, "y") << ", "
@ -240,7 +261,7 @@ string generate_map_events_text(Json map_data) {
<< json_to_string(coord_event, "var_value") << ", "
<< json_to_string(coord_event, "script") << "\n";
}
else if (coord_event["type"] == "weather") {
else if (type == "weather") {
text << "\tcoord_weather_event "
<< json_to_string(coord_event, "x") << ", "
<< json_to_string(coord_event, "y") << ", "
@ -250,14 +271,15 @@ string generate_map_events_text(Json map_data) {
}
text << "\n";
} else {
coords_label = "0x0";
coords_label = "NULL";
}
if (map_data["bg_events"].array_items().size() > 0) {
bgs_label = mapName + "_MapBGEvents";
text << bgs_label << ":\n";
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 "
<< json_to_string(bg_event, "x") << ", "
<< json_to_string(bg_event, "y") << ", "
@ -265,15 +287,21 @@ string generate_map_events_text(Json map_data) {
<< json_to_string(bg_event, "player_facing_dir") << ", "
<< json_to_string(bg_event, "script") << "\n";
}
else if (bg_event["type"] == "hidden_item") {
else if (type == "hidden_item") {
text << "\tbg_hidden_item_event "
<< json_to_string(bg_event, "x") << ", "
<< json_to_string(bg_event, "y") << ", "
<< json_to_string(bg_event, "elevation") << ", "
<< 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 "
<< json_to_string(bg_event, "x") << ", "
<< json_to_string(bg_event, "y") << ", "
@ -283,7 +311,7 @@ string generate_map_events_text(Json map_data) {
}
text << "\n";
} else {
bgs_label = "0x0";
bgs_label = "NULL";
}
text << mapName << "_MapEvents::\n"
@ -299,7 +327,7 @@ string get_directory_name(string filename) {
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_json_text = read_text_file(map_filepath);
@ -313,7 +341,7 @@ void process_map(string map_filepath, string layouts_filepath, string version) {
if (layouts_data == Json())
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 connections_text = generate_map_connections_text(map_data);
@ -337,7 +365,10 @@ string generate_groups_text(Json groups_data) {
text << "\n";
}
text << "\t.align 2\n" << "gMapGroups::\n";
if (version != "firered")
text << "\t.align 2\n";
text << "gMapGroups::\n";
for (auto &group : groups_data["group_order"].array_items())
text << "\t.4byte " << json_to_string(group) << "\n";
text << "\n";
@ -484,6 +515,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";
for (auto &layout : layouts_data["layouts"].array_items()) {
if (layout == Json::object()) continue;
string layoutName = json_to_string(layout, "name");
string border_label = layoutName + "_Border";
string blockdata_label = layoutName + "_Blockdata";
@ -498,7 +530,13 @@ string generate_layout_headers_text(Json layouts_data) {
<< "\t.4byte " << border_label << "\n"
<< "\t.4byte " << blockdata_label << "\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();
@ -512,8 +550,11 @@ string generate_layouts_table_text(Json layouts_data) {
text << "\t.align 2\n"
<< json_to_string(layouts_data, "layouts_table_label") << "::\n";
for (auto &layout : layouts_data["layouts"].array_items())
text << "\t.4byte " << json_to_string(layout, "name") << "\n";
for (auto &layout : layouts_data["layouts"].array_items()) {
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();
}
@ -526,9 +567,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";
int i = 0;
for (auto &layout : layouts_data["layouts"].array_items())
text << "#define " << json_to_string(layout, "id") << " " << ++i << "\n";
int i = 1;
for (auto &layout : layouts_data["layouts"].array_items()) {
if (layout != Json::object())
text << "#define " << json_to_string(layout, "id") << " " << i << "\n";
i++;
}
text << "\n#endif // GUARD_CONSTANTS_LAYOUTS_H\n";
@ -559,9 +603,9 @@ int main(int argc, char *argv[]) {
FATAL_ERROR("USAGE: mapjson <mode> <game-version> [options]\n");
char *version_arg = argv[2];
string version(version_arg);
if (version != "emerald" && version != "ruby")
FATAL_ERROR("ERROR: <game-version> must be 'emerald' or 'ruby'.\n");
version = string(version_arg);
if (version != "emerald" && version != "ruby" && version != "firered")
FATAL_ERROR("ERROR: <game-version> must be 'emerald', 'firered', or 'ruby'.\n");
char *mode_arg = argv[1];
string mode(mode_arg);
@ -575,7 +619,7 @@ int main(int argc, char *argv[]) {
string filepath(argv[3]);
string layouts_filepath(argv[4]);
process_map(filepath, layouts_filepath, version);
process_map(filepath, layouts_filepath);
}
else if (mode == "groups") {
if (argc != 4)