summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth Barberee <seth.barberee@gmail.com>2021-07-12 17:50:19 -0500
committerSeth Barberee <seth.barberee@gmail.com>2021-07-12 17:50:19 -0500
commita65d358927f668cb5e34a6046afef5bd61380a28 (patch)
treeec6e4ec6a49cb2521fa67fcc392ff209d5b5745b
parentda7b30612f2cac57da0ffe09d1d7c42e309d15f2 (diff)
minor code cleanup
-rw-r--r--README.md78
-rw-r--r--data/dungeon/.gitignore4
-rw-r--r--include/pokemon.h2
-rw-r--r--src/items.c47
-rw-r--r--src/items_1.c90
-rw-r--r--tools/dungeonjson/dungeonjson.cpp1300
6 files changed, 751 insertions, 770 deletions
diff --git a/README.md b/README.md
index efb33d4..81b1762 100644
--- a/README.md
+++ b/README.md
@@ -1,39 +1,39 @@
-# Pokémon Mystery Dungeon: Red Rescue Team
-
-[![build](https://github.com/pret/pmd-red/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/pret/pmd-red/actions/workflows/build.yml)
-
-This is a disassembly of Pokémon Mystery Dungeon: Red Rescue Team.
-
-It builds the following rom:
-
-* pmd_red.gba `sha1: 9f4cfc5b5f4859d17169a485462e977c7aac2b89`
-
-To set up the repository, see [INSTALL.md](INSTALL.md).
-
-## See also
-
-* Disassembly of [**Pokémon Red/Blue**][pokered]
-* Disassembly of [**Pokémon Yellow**][pokeyellow]
-* Disassembly of [**Pokémon Gold**][pokegold]
-* Disassembly of [**Pokémon Crystal**][pokecrystal]
-* Disassembly of [**Pokémon Pinball**][pokepinball]
-* Disassembly of [**Pokémon TCG**][poketcg]
-* Disassembly of [**Pokémon Fire Red**/**Leaf Green**][pokefirered]
-* Disassembly of [**Pokémon Ruby/Sapphire**][pokeruby]
-* Disassembly of [**Pokémon Emerald**][pokeemerald]
-* Disassembly of [**Pokémon Diamond/Pearl**][pokediamond]
-* Discord: [**pret**][Discord]
-* irc: **irc.freenode.net** [**#pret**][irc]
-
-[pokered]: https://github.com/pret/pokered
-[pokeyellow]: https://github.com/pret/pokeyellow
-[pokegold]: https://github.com/pret/pokegold
-[pokecrystal]: https://github.com/pret/pokecrystal
-[pokepinball]: https://github.com/pret/pokepinball
-[poketcg]: https://github.com/pret/poketcg
-[pokefirered]: https://github.com/pret/pokefirered
-[pokeruby]: https://github.com/pret/pokeruby
-[pokeemerald]: https://github.com/pret/pokeemerald
-[pokediamond]: https://github.com/pret/pokediamond
-[Discord]: https://discord.gg/d5dubZ3
-[irc]: https://kiwiirc.com/client/irc.freenode.net/?#pret
+# Pokémon Mystery Dungeon: Red Rescue Team
+
+[![build](https://github.com/pret/pmd-red/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/pret/pmd-red/actions/workflows/build.yml)
+
+This is a disassembly of Pokémon Mystery Dungeon: Red Rescue Team.
+
+It builds the following rom:
+
+* pmd_red.gba `sha1: 9f4cfc5b5f4859d17169a485462e977c7aac2b89`
+
+To set up the repository, see [INSTALL.md](INSTALL.md).
+
+## See also
+
+* Disassembly of [**Pokémon Red/Blue**][pokered]
+* Disassembly of [**Pokémon Yellow**][pokeyellow]
+* Disassembly of [**Pokémon Gold**][pokegold]
+* Disassembly of [**Pokémon Crystal**][pokecrystal]
+* Disassembly of [**Pokémon Pinball**][pokepinball]
+* Disassembly of [**Pokémon TCG**][poketcg]
+* Disassembly of [**Pokémon Fire Red**/**Leaf Green**][pokefirered]
+* Disassembly of [**Pokémon Ruby/Sapphire**][pokeruby]
+* Disassembly of [**Pokémon Emerald**][pokeemerald]
+* Disassembly of [**Pokémon Diamond/Pearl**][pokediamond]
+* Discord: [**pret**][Discord]
+* irc: **irc.freenode.net** [**#pret**][irc]
+
+[pokered]: https://github.com/pret/pokered
+[pokeyellow]: https://github.com/pret/pokeyellow
+[pokegold]: https://github.com/pret/pokegold
+[pokecrystal]: https://github.com/pret/pokecrystal
+[pokepinball]: https://github.com/pret/pokepinball
+[poketcg]: https://github.com/pret/poketcg
+[pokefirered]: https://github.com/pret/pokefirered
+[pokeruby]: https://github.com/pret/pokeruby
+[pokeemerald]: https://github.com/pret/pokeemerald
+[pokediamond]: https://github.com/pret/pokediamond
+[Discord]: https://discord.gg/d5dubZ3
+[irc]: https://kiwiirc.com/client/irc.freenode.net/?#pret
diff --git a/data/dungeon/.gitignore b/data/dungeon/.gitignore
index 5f8029d..9f03cde 100644
--- a/data/dungeon/.gitignore
+++ b/data/dungeon/.gitignore
@@ -1,2 +1,2 @@
-**/pokemon_found.inc
-**/floor_id.inc
+**/pokemon_found.inc
+**/floor_id.inc
diff --git a/include/pokemon.h b/include/pokemon.h
index e3a5ab1..679a1d1 100644
--- a/include/pokemon.h
+++ b/include/pokemon.h
@@ -18,7 +18,7 @@ struct PokemonStruct
u8 fillD[0x10 - 0xD];
u8 unk10;
u8 fill11[0x14 - 0x11];
- /* 0x14 */ u16 IQ;
+ /* 0x14 */ s16 IQ;
/* 0x16 */ u16 pokeHP; // HP
/* 0x18 */ u8 pokeAtt; // attack
/* 0x19 */ u8 pokeSPAtt; // sp attack
diff --git a/src/items.c b/src/items.c
index 8b2de3f..820975c 100644
--- a/src/items.c
+++ b/src/items.c
@@ -70,12 +70,10 @@ s32 GetNumberOfFilledInventorySlots(void)
bool8 IsThrowableItem(u8 itemIndex)
{
- if ((GetItemType(itemIndex) != ITEM_TYPE_THROWABLE) && (GetItemType(itemIndex) != ITEM_TYPE_ROCK)) {
+ if ((GetItemType(itemIndex) != ITEM_TYPE_THROWABLE) && (GetItemType(itemIndex) != ITEM_TYPE_ROCK))
return FALSE;
- }
- else {
+ else
return TRUE;
- }
}
void sub_8090A8C(struct ItemSlot *param_1,u8 itemIndex,u8 param_3)
@@ -91,17 +89,14 @@ void sub_8090A8C(struct ItemSlot *param_1,u8 itemIndex,u8 param_3)
uVar4 = GetItemUnkThrow(itemIndex,1);
param_1->numItems = RandomRange(uVar3,uVar4);
}
- else {
- if (GetItemType(itemIndex) == ITEM_TYPE_MONEY) {
- param_1->numItems = 1;
- }
- else {
- param_1->numItems = 0;
- }
- }
- if (param_3 != 0) {
+ else if (GetItemType(itemIndex) == ITEM_TYPE_MONEY)
+ param_1->numItems = 1;
+ else
+ param_1->numItems = 0;
+
+ if (param_3 != 0)
param_1->unk0 |= 8;
- }
+
}
else {
param_1->unk0 = 0;
@@ -121,14 +116,11 @@ void sub_8090B08(struct ItemSlot_ALT *param_1,u8 itemIndex)
uVar2 = GetItemUnkThrow(itemIndex,0);
uVar3 = GetItemUnkThrow(itemIndex,1);
param_1->numItems = RandomRange(uVar2,uVar3);
- } else {
- if (GetItemType(itemIndex) == ITEM_TYPE_MONEY) {
+ }
+ else if (GetItemType(itemIndex) == ITEM_TYPE_MONEY)
param_1->numItems = 1;
- }
- else {
+ else
param_1->numItems = 0;
- }
- }
}
else {
param_1->itemIndex = 0;
@@ -146,20 +138,11 @@ void sub_8090B64(struct ItemSlot *param_1, struct ItemSlot_ALT *param_2)
param_1->itemIndex = param_2->itemIndex;
r6 = IsThrowableItem(param_1->itemIndex);
if(r6 != 0 || GetItemType(param_1->itemIndex) == ITEM_TYPE_MONEY)
- {
param_1->numItems = param_2->numItems;
- }
+ else if(param_1->itemIndex == ITEM_ID_USED_TM)
+ param_1->numItems = param_2->numItems;
else
- {
- if(param_1->itemIndex == ITEM_ID_USED_TM)
- {
- param_1->numItems = param_2->numItems;
- }
- else
- {
- param_1->numItems = r6;
- }
- }
+ param_1->numItems = 0;
}
else
{
diff --git a/src/items_1.c b/src/items_1.c
index 0c70c67..f0061ce 100644
--- a/src/items_1.c
+++ b/src/items_1.c
@@ -237,10 +237,10 @@ bool8 AddItemToInventory(const struct ItemSlot* slot)
UNUSED struct ItemSlot* current = &gTeamInventory_203B460->teamItems[i];
if (!(i[gTeamInventory_203B460->teamItems].unk0 & 1)) {
gTeamInventory_203B460->teamItems[i] = *slot;
- return 0;
+ return FALSE;
}
}
- return 1;
+ return TRUE;
}
void ConvertMoneyItemToMoney()
@@ -314,7 +314,7 @@ void AddToTeamMoney(s32 amount)
u16 GetItemMove(u8 index)
{
- return gItemParametersData[index & 0xff].move;
+ return gItemParametersData[index].move;
}
u32 sub_80913E0(struct ItemSlot* slot, u32 a2, struct subStruct_203B240 ** a3)
@@ -324,7 +324,7 @@ u32 sub_80913E0(struct ItemSlot* slot, u32 a2, struct subStruct_203B240 ** a3)
GetItemDescription(slot->itemIndex);
sub_8090DC4(buffer88, slot->itemIndex, 0);
if (slot->itemIndex == ITEM_ID_USED_TM) {
- // empty HM
+ // empty TM
sub_8090DC4(&gUnknown_202DE58, (u8)(slot->numItems + 125), 0);
}
sub_80073B8(a2);
@@ -367,83 +367,83 @@ bool8 CanSellItem(u32 id)
&& (id != ITEM_ID_MUSIC_BOX)
&& (GetItemSellPrice(id_))
&& (GetItemBuyPrice(id_))) {
- return 1;
+ return TRUE;
}
- return 0;
+ return FALSE;
}
bool8 IsNotMoneyOrUsedTMItem(u8 id)
{
if (id == ITEM_ID_NOTHING) {
- return 0;
+ return FALSE;
}
else if (id == ITEM_ID_POKE) {
- return 0;
+ return FALSE;
}
else if (id == ITEM_ID_USED_TM) {
- return 0;
+ return FALSE;
}
- return 1;
+ return TRUE;
}
bool8 IsNotSpecialItem(u8 id)
{
if (id == ITEM_ID_NOTHING) {
- return 0;
+ return FALSE;
}
else if (id == ITEM_ID_POKE) {
- return 0;
+ return FALSE;
}
else if (id == ITEM_ID_ROCK_PART) {
- return 0;
+ return FALSE;
}
else if (id == ITEM_ID_ICE_PART) {
- return 0;
+ return FALSE;
}
else if (id == ITEM_ID_STEEL_PART) {
- return 0;
+ return FALSE;
}
else if (id == ITEM_ID_MUSIC_BOX) {
- return 0;
+ return FALSE;
}
- return 1;
+ return TRUE;
}
bool8 IsEdibleItem(u8 id)
{
if (!((GetItemType(id) == ITEM_TYPE_BERRY_SEED) || (GetItemType(id) == ITEM_TYPE_APPLE_GUMMI))) {
- return 0;
+ return FALSE;
}
- return 1;
+ return TRUE;
}
bool8 IsHMItem(u8 id)
{
if (id == ITEM_ID_CUT) {
- return 1;
+ return TRUE;
}
else if (id == ITEM_ID_FLY) {
- return 1;
+ return TRUE;
}
else if (id == ITEM_ID_SURF) {
- return 1;
+ return TRUE;
}
else if (id == ITEM_ID_STRENGTH) {
- return 1;
+ return TRUE;
}
else if (id == ITEM_ID_FLASH) {
- return 1;
+ return TRUE;
}
else if (id == ITEM_ID_ROCK_SMASH) {
- return 1;
+ return TRUE;
}
else if (id == ITEM_ID_WATERFALL) {
- return 1;
+ return TRUE;
}
else if (id == ITEM_ID_DIVE) {
- return 1;
+ return TRUE;
}
- return 0;
+ return FALSE;
}
u32 GetMoneyValue(struct ItemSlot* slot)
@@ -475,14 +475,14 @@ void GetGummiItemStatBoost(struct PokemonStruct* pokemon, u8 itemIndex, u8 a3, s
value0 = gTypeGummiIQBoost[pokemon_type_0][gummi_index];
value1 = gTypeGummiIQBoost[pokemon_type_1][gummi_index];
- diff = (s16)pokemon->IQ;
+ diff = pokemon->IQ;
pokemon->IQ += value0 + value1;
- diff = (s16)pokemon->IQ - diff;
- if ((s16)pokemon->IQ <= 0) {
+ diff = pokemon->IQ - diff;
+ if (pokemon->IQ <= 0) {
pokemon->IQ = 1;
}
- if ((s16)pokemon->IQ > 999) {
+ if (pokemon->IQ > 999) {
pokemon->IQ = 999;
}
@@ -512,7 +512,7 @@ void GetGummiItemStatBoost(struct PokemonStruct* pokemon, u8 itemIndex, u8 a3, s
a4->unk2 = boost_flags;
boost_flags = a4->unk2;
if (a4->unk2 & 1) {
- if (pokemon->pokeAtt < 0xffu) {
+ if (pokemon->pokeAtt < 255) {
pokemon->pokeAtt++;
}
else {
@@ -523,7 +523,7 @@ void GetGummiItemStatBoost(struct PokemonStruct* pokemon, u8 itemIndex, u8 a3, s
}
}
if (a4->unk2 & 2) {
- if (pokemon->pokeSPAtt < 0xffu) {
+ if (pokemon->pokeSPAtt < 255) {
pokemon->pokeSPAtt++;
}
else {
@@ -531,7 +531,7 @@ void GetGummiItemStatBoost(struct PokemonStruct* pokemon, u8 itemIndex, u8 a3, s
}
}
if (a4->unk2 & 4) {
- if (pokemon->pokeDef < 0xffu) {
+ if (pokemon->pokeDef < 255) {
pokemon->pokeDef++;
}
else {
@@ -539,7 +539,7 @@ void GetGummiItemStatBoost(struct PokemonStruct* pokemon, u8 itemIndex, u8 a3, s
}
}
if (a4->unk2 & 8) {
- if (pokemon->pokeSPDef < 0xffu) {
+ if (pokemon->pokeSPDef < 255) {
pokemon->pokeSPDef++;
}
else {
@@ -553,12 +553,12 @@ void GetGummiItemStatBoost(struct PokemonStruct* pokemon, u8 itemIndex, u8 a3, s
bool8 IsGummiItem(u8 itemIndex)
{
if (itemIndex < ITEM_ID_WHITE_GUMMI) {
- return 0;
+ return FALSE;
}
if (itemIndex > ITEM_ID_SILVER_GUMMI) {
- return 0;
+ return FALSE;
}
- return 1;
+ return TRUE;
}
bool8 HasGummiItem()
@@ -567,10 +567,10 @@ bool8 HasGummiItem()
for (i = 0; i < INVENTORY_SIZE; i++) {
UNUSED size_t offs = offsetof(struct TeamInventory, teamItems[i]);
if ((i[gTeamInventory_203B460->teamItems].unk0 & 1) && IsGummiItem(i[gTeamInventory_203B460->teamItems].itemIndex)) {
- return 1;
+ return TRUE;
}
}
- return 0;
+ return FALSE;
}
void MoveToStorage(struct ItemSlot* slot)
@@ -603,16 +603,14 @@ void xxx_init_unk230_substruct(u8 i)
{
struct subStruct_203B460* unk230;
- // the masking makes it seem like there should be an item ID passed, but
- // that would go horribly out of bounds...
- unk230 = &gTeamInventory_203B460->unk230[i & 0xff];
+ unk230 = &gTeamInventory_203B460->unk230[i];
unk230->unk0 = 0;
unk230->unk1 = 0;
}
struct subStruct_203B460* sub_809185C(u8 i)
{
- return &gTeamInventory_203B460->unk230[i & 0xff];
+ return &gTeamInventory_203B460->unk230[i];
}
void xxx_fill_unk230_gaps()
@@ -647,4 +645,4 @@ void xxx_fill_unk230_gaps()
for (; last_filled < 8; last_filled++) {
xxx_init_unk230_substruct(last_filled);
}
-} \ No newline at end of file
+}
diff --git a/tools/dungeonjson/dungeonjson.cpp b/tools/dungeonjson/dungeonjson.cpp
index 1db7986..c1a4c81 100644
--- a/tools/dungeonjson/dungeonjson.cpp
+++ b/tools/dungeonjson/dungeonjson.cpp
@@ -1,650 +1,650 @@
-// dungeonjson.cpp
-
-#include <iostream>
-using std::cout; using std::endl;
-
-#include <string>
-using std::string;
-
-#include <vector>
-using std::vector;
-
-#include <algorithm>
-using std::sort; using std::find;
-
-#include <map>
-using std::map;
-
-#include <fstream>
-using std::ofstream; using std::ifstream;
-
-#include <sstream>
-using std::ostringstream;
-
-#include <limits>
-using std::numeric_limits;
-
-#include "json11.h"
-using json11::Json;
-
-#include "dungeonjson.h"
-
-
-string read_text_file(string filepath) {
- ifstream in_file(filepath);
-
- if (!in_file.is_open())
- FATAL_ERROR("Cannot open file %s for reading.\n", filepath.c_str());
-
- string text;
-
- in_file.seekg(0, std::ios::end);
- text.resize(in_file.tellg());
-
- in_file.seekg(0, std::ios::beg);
- in_file.read(&text[0], text.size());
-
- in_file.close();
-
- return text;
-}
-
-void write_text_file(string filepath, string text) {
- ofstream out_file(filepath, std::ofstream::binary);
-
- if (!out_file.is_open())
- FATAL_ERROR("Cannot open file %s for writing.\n", filepath.c_str());
-
- out_file << text;
-
- out_file.close();
-}
-
-string generate_map_header_text(Json map_data, Json layouts_data, string version) {
- string map_layout_id = map_data["layout"].string_value();
-
- vector<Json> matched;
-
- for (auto &field : layouts_data["layouts"].array_items()) {
- if (map_layout_id == field["id"].string_value())
- matched.push_back(field);
- }
-
- if (matched.size() != 1)
- FATAL_ERROR("Failed to find matching layout for %s.\n", map_layout_id.c_str());
-
- Json layout = matched[0];
-
- ostringstream text;
-
- text << map_data["name"].string_value() << ":\n"
- << "\t.4byte " << layout["name"].string_value() << "\n";
-
- if (map_data.object_items().find("shared_events_map") != map_data.object_items().end())
- text << "\t.4byte " << map_data["shared_events_map"].string_value() << "_MapEvents\n";
- else
- text << "\t.4byte " << map_data["name"].string_value() << "_MapEvents\n";
-
- if (map_data.object_items().find("shared_scripts_map") != map_data.object_items().end())
- text << "\t.4byte " << map_data["shared_scripts_map"].string_value() << "_MapScripts\n";
- else
- text << "\t.4byte " << map_data["name"].string_value() << "_MapScripts\n";
-
- if (map_data.object_items().find("connections") != map_data.object_items().end()
- && map_data["connections"].array_items().size() > 0)
- text << "\t.4byte " << map_data["name"].string_value() << "_MapConnections\n";
- else
- text << "\t.4byte 0x0\n";
-
- text << "\t.2byte " << map_data["music"].string_value() << "\n"
- << "\t.2byte " << layout["id"].string_value() << "\n"
- << "\t.byte " << map_data["region_map_section"].string_value() << "\n"
- << "\t.byte " << map_data["requires_flash"].bool_value() << "\n"
- << "\t.byte " << map_data["weather"].string_value() << "\n"
- << "\t.byte " << map_data["map_type"].string_value() << "\n"
- << "\t.2byte 0\n";
-
- if (version == "ruby")
- text << "\t.byte " << map_data["show_map_name"].bool_value() << "\n";
- else if (version == "emerald")
- text << "\tmap_header_flags "
- << "allow_cycling=" << map_data["allow_cycling"].bool_value() << ", "
- << "allow_escaping=" << map_data["allow_escaping"].bool_value() << ", "
- << "allow_running=" << map_data["allow_running"].bool_value() << ", "
- << "show_map_name=" << map_data["show_map_name"].bool_value() << "\n";
-
- text << "\t.byte " << map_data["battle_scene"].string_value() << "\n\n";
-
- return text.str();
-}
-
-string generate_map_connections_text(Json map_data) {
- if (map_data["connections"] == Json())
- return string("\n");
-
- ostringstream text;
-
- text << map_data["name"].string_value() << "_MapConnectionsList:\n";
-
- for (auto &connection : map_data["connections"].array_items()) {
- text << "\tconnection "
- << connection["direction"].string_value() << ", "
- << connection["offset"].int_value() << ", "
- << connection["map"].string_value() << "\n";
- }
-
- text << "\n" << map_data["name"].string_value() << "_MapConnections:\n"
- << "\t.4byte " << map_data["connections"].array_items().size() << "\n"
- << "\t.4byte " << map_data["name"].string_value() << "_MapConnectionsList\n\n";
-
- return text.str();
-}
-
-string generate_map_events_text(Json map_data) {
- if (map_data.object_items().find("shared_events_map") != map_data.object_items().end())
- return string("\n");
-
- ostringstream text;
-
- string objects_label, warps_label, coords_label, bgs_label;
-
- if (map_data["object_events"].array_items().size() > 0) {
- objects_label = map_data["name"].string_value() + "_ObjectEvents";
- 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];
- text << "\tobject_event " << i + 1 << ", "
- << obj_event["graphics_id"].string_value() << ", 0, "
- << obj_event["x"].int_value() << ", "
- << obj_event["y"].int_value() << ", "
- << obj_event["elevation"].int_value() << ", "
- << obj_event["movement_type"].string_value() << ", "
- << obj_event["movement_range_x"].int_value() << ", "
- << obj_event["movement_range_y"].int_value() << ", "
- << obj_event["trainer_type"].string_value() << ", "
- << obj_event["trainer_sight_or_berry_tree_id"].string_value() << ", "
- << obj_event["script"].string_value() << ", "
- << obj_event["flag"].string_value() << "\n";
- }
- text << "\n";
- } else {
- objects_label = "0x0";
- }
-
- if (map_data["warp_events"].array_items().size() > 0) {
- warps_label = map_data["name"].string_value() + "_MapWarps";
- text << warps_label << ":\n";
- for (auto &warp_event : map_data["warp_events"].array_items()) {
- text << "\twarp_def "
- << warp_event["x"].int_value() << ", "
- << warp_event["y"].int_value() << ", "
- << warp_event["elevation"].int_value() << ", "
- << warp_event["dest_warp_id"].int_value() << ", "
- << warp_event["dest_map"].string_value() << "\n";
- }
- text << "\n";
- } else {
- warps_label = "0x0";
- }
-
- if (map_data["coord_events"].array_items().size() > 0) {
- coords_label = map_data["name"].string_value() + "_MapCoordEvents";
- text << coords_label << ":\n";
- for (auto &coord_event : map_data["coord_events"].array_items()) {
- if (coord_event["type"].string_value() == "trigger") {
- text << "\tcoord_event "
- << coord_event["x"].int_value() << ", "
- << coord_event["y"].int_value() << ", "
- << coord_event["elevation"].int_value() << ", "
- << coord_event["var"].string_value() << ", "
- << coord_event["var_value"].string_value() << ", "
- << coord_event["script"].string_value() << "\n";
- }
- else if (coord_event["type"] == "weather") {
- text << "\tcoord_weather_event "
- << coord_event["x"].int_value() << ", "
- << coord_event["y"].int_value() << ", "
- << coord_event["elevation"].int_value() << ", "
- << coord_event["weather"].string_value() << "\n";
- }
- }
- text << "\n";
- } else {
- coords_label = "0x0";
- }
-
- if (map_data["bg_events"].array_items().size() > 0) {
- bgs_label = map_data["name"].string_value() + "_MapBGEvents";
- text << bgs_label << ":\n";
- for (auto &bg_event : map_data["bg_events"].array_items()) {
- if (bg_event["type"] == "sign") {
- text << "\tbg_event "
- << bg_event["x"].int_value() << ", "
- << bg_event["y"].int_value() << ", "
- << bg_event["elevation"].int_value() << ", "
- << bg_event["player_facing_dir"].string_value() << ", "
- << bg_event["script"].string_value() << "\n";
- }
- else if (bg_event["type"] == "hidden_item") {
- text << "\tbg_hidden_item_event "
- << bg_event["x"].int_value() << ", "
- << bg_event["y"].int_value() << ", "
- << bg_event["elevation"].int_value() << ", "
- << bg_event["item"].string_value() << ", "
- << bg_event["flag"].string_value() << "\n";
- }
- else if (bg_event["type"] == "secret_base") {
- text << "\tbg_secret_base_event "
- << bg_event["x"].int_value() << ", "
- << bg_event["y"].int_value() << ", "
- << bg_event["elevation"].int_value() << ", "
- << bg_event["secret_base_id"].string_value() << "\n";
- }
- }
- text << "\n";
- } else {
- bgs_label = "0x0";
- }
-
- text << map_data["name"].string_value() << "_MapEvents::\n"
- << "\tmap_events " << objects_label << ", " << warps_label << ", "
- << coords_label << ", " << bgs_label << "\n\n";
-
- return text.str();
-}
-
-string generate_pokemon_table_text(Json map_data) {
-
- ostringstream text;
- int pokemon_mask;
- int probability_total;
-
- text << "@ This is auto-generated by " << map_data["name"].string_value() << ".json" << "\n";
- for (auto &table: map_data["tables"].array_items()) {
- probability_total = 0;
-
- text << ".global " << table["name"].string_value() << "\n";
- text << table["name"].string_value() << ":\n";
-
- for (auto &pokemon: table["pokemon"].array_items()) {
- pokemon_mask = (pokemon["level"].int_value() * 512);
- probability_total += pokemon["probability"].int_value();
- text << ".2byte " << pokemon["species"].string_value() << " + " << pokemon_mask;
- // We have to repeat to maintain matching
- if(pokemon["probability"].int_value() == 0)
- {
- text << ", " << "0x00, 0x00";
- }
- else
- {
- text << ", " << probability_total << ", " << probability_total;
- }
- text << ", " << "0x00" << "\n";
- }
-
- // Append null entry to end
- text << "@ END OF TABLE" << "\n";
- text << ".2byte " << "0x00, 0x00, 0x00, 0x00" << "\n";
- }
-
- return text.str();
-}
-
-string generate_floorID_table_text(Json map_data) {
-
- ostringstream text;
-
- text << "@ This is auto-generated by " << map_data["name"].string_value() << ".json" << "\n";
-
- text << ".global " << map_data["name"].string_value() << "\n";
- text << map_data["name"].string_value() << ":\n";
- // Append null entry to Start
- text << ".2byte " << "0, 0, 0, 0, 0, 0, 0, 0" << "\n";
- for (auto &table: map_data["tables"].array_items()) {
-
- text << ".2byte " << table["MainData"].int_value()
- << ", " << table["Pokemon"].int_value()
- << ", " << table["Traps"].int_value()
- << ", " << table["Items"].int_value()
- << ", " << table["KecleonShop"].int_value()
- << ", " << table["MonsterRoomItems"].int_value()
- << ", " << table["BuriedItems"].int_value()
- << ", " << "0" << "\n";
-
- }
- text << "@ END OF TABLE" << "\n";
-
- return text.str();
-}
-
-string get_directory_name(string filename) {
- size_t dir_pos = filename.find_last_of("/\\");
-
- return filename.substr(0, dir_pos + 1);
-}
-
-void process_map(string map_filepath, string layouts_filepath, string version) {
- string mapdata_err, layouts_err;
-
- string mapdata_json_text = read_text_file(map_filepath);
- string layouts_json_text = read_text_file(layouts_filepath);
-
- Json map_data = Json::parse(mapdata_json_text, mapdata_err);
- if (map_data == Json())
- FATAL_ERROR("%s\n", mapdata_err.c_str());
-
- Json layouts_data = Json::parse(layouts_json_text, layouts_err);
- 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 events_text = generate_map_events_text(map_data);
- string connections_text = generate_map_connections_text(map_data);
-
- string files_dir = get_directory_name(map_filepath);
- write_text_file(files_dir + "header.inc", header_text);
- write_text_file(files_dir + "events.inc", events_text);
- write_text_file(files_dir + "connections.inc", connections_text);
-}
-
-void process_dungeon(string map_filepath) {
- string mapdata_err;
-
- string mapdata_json_text = read_text_file(map_filepath);
-
- Json map_data = Json::parse(mapdata_json_text, mapdata_err);
- if (map_data == Json())
- FATAL_ERROR("%s\n", mapdata_err.c_str());
-
- string pokemon_text = generate_pokemon_table_text(map_data);
-
- string files_dir = get_directory_name(map_filepath);
- write_text_file(files_dir + "pokemon_found.inc", pokemon_text);
-}
-
-void process_floorID(string map_filepath) {
- string mapdata_err;
-
- string mapdata_json_text = read_text_file(map_filepath);
-
- Json map_data = Json::parse(mapdata_json_text, mapdata_err);
- if (map_data == Json())
- FATAL_ERROR("%s\n", mapdata_err.c_str());
-
- string pokemon_text = generate_floorID_table_text(map_data);
-
- string files_dir = get_directory_name(map_filepath);
- write_text_file(files_dir + "floor_id.inc", pokemon_text);
-}
-
-string generate_groups_text(Json groups_data) {
- ostringstream text;
-
- for (auto &key : groups_data["group_order"].array_items()) {
- string group = key.string_value();
- text << group << "::\n";
- auto maps = groups_data[group].array_items();
- for (Json &map_name : maps)
- text << "\t.4byte " << map_name.string_value() << "\n";
- text << "\n";
- }
-
- text << "\t.align 2\n" << "gMapGroups::\n";
- for (auto &group : groups_data["group_order"].array_items())
- text << "\t.4byte " << group.string_value() << "\n";
- text << "\n";
-
- return text.str();
-}
-
-string generate_connections_text(Json groups_data) {
- vector<Json> map_names;
-
- for (auto &group : groups_data["group_order"].array_items())
- for (auto map_name : groups_data[group.string_value()].array_items())
- map_names.push_back(map_name);
-
- vector<Json> connections_include_order = groups_data["connections_include_order"].array_items();
-
- if (connections_include_order.size() > 0)
- sort(map_names.begin(), map_names.end(), [connections_include_order](const Json &a, const Json &b) {
- auto iter_a = find(connections_include_order.begin(), connections_include_order.end(), a);
- if (iter_a == connections_include_order.end())
- iter_a = connections_include_order.begin() + numeric_limits<int>::max();
- auto iter_b = find(connections_include_order.begin(), connections_include_order.end(), b);
- if (iter_b == connections_include_order.end())
- iter_b = connections_include_order.begin() + numeric_limits<int>::max();
- return iter_a < iter_b;
- });
-
- ostringstream text;
-
- for (Json map_name : map_names)
- text << "\t.include \"data/maps/" << map_name.string_value() << "/connections.inc\"\n";
-
- return text.str();
-}
-
-string generate_headers_text(Json groups_data) {
- vector<string> map_names;
-
- for (auto &group : groups_data["group_order"].array_items())
- for (auto map_name : groups_data[group.string_value()].array_items())
- map_names.push_back(map_name.string_value());
-
- ostringstream text;
-
- for (string map_name : map_names)
- text << "\t.include \"data/maps/" << map_name << "/header.inc\"\n";
-
- return text.str();
-}
-
-string generate_events_text(Json groups_data) {
- vector<string> map_names;
-
- for (auto &group : groups_data["group_order"].array_items())
- for (auto map_name : groups_data[group.string_value()].array_items())
- map_names.push_back(map_name.string_value());
-
- ostringstream text;
-
- for (string map_name : map_names)
- text << "\t.include \"data/maps/" << map_name << "/events.inc\"\n";
-
- return text.str();
-}
-
-string generate_map_constants_text(string groups_filepath, Json groups_data) {
- string file_dir = get_directory_name(groups_filepath);
- char dir_separator = file_dir.back();
-
- ostringstream text;
-
- text << "#ifndef GUARD_CONSTANTS_MAP_GROUPS_H\n"
- << "#define GUARD_CONSTANTS_MAP_GROUPS_H\n\n";
-
- int group_num = 0;
-
- for (auto &group : groups_data["group_order"].array_items()) {
- text << "// Map Group " << group_num << "\n";
- vector<Json> map_ids;
- size_t max_length = 0;
-
- for (auto &map_name : groups_data[group.string_value()].array_items()) {
- string header_filepath = file_dir + map_name.string_value() + dir_separator + "map.json";
- string err_str;
- Json map_data = Json::parse(read_text_file(header_filepath), err_str);
- map_ids.push_back(map_data["id"]);
- if (map_data["id"].string_value().length() > max_length)
- max_length = map_data["id"].string_value().length();
- }
-
- int map_id_num = 0;
- for (Json map_id : map_ids) {
- text << "#define " << map_id.string_value() << string((max_length - map_id.string_value().length() + 1), ' ')
- << "(" << map_id_num++ << " | (" << group_num << " << 8))\n";
- }
- text << "\n";
-
- group_num++;
- }
-
- text << "#define MAP_GROUPS_COUNT " << group_num << "\n\n";
- text << "#endif // GUARD_CONSTANTS_MAP_GROUPS_H\n";
-
- return text.str();
-}
-
-void process_groups(string groups_filepath) {
- string err;
- Json groups_data = Json::parse(read_text_file(groups_filepath), err);
-
- if (groups_data == Json())
- FATAL_ERROR("%s\n", err.c_str());
-
- string groups_text = generate_groups_text(groups_data);
- string connections_text = generate_connections_text(groups_data);
- string headers_text = generate_headers_text(groups_data);
- string events_text = generate_events_text(groups_data);
- string map_header_text = generate_map_constants_text(groups_filepath, groups_data);
-
- string file_dir = get_directory_name(groups_filepath);
- char s = file_dir.back();
-
- write_text_file(file_dir + "groups.inc", groups_text);
- write_text_file(file_dir + "connections.inc", connections_text);
- write_text_file(file_dir + "headers.inc", headers_text);
- write_text_file(file_dir + "events.inc", events_text);
- write_text_file(file_dir + ".." + s + ".." + s + "include" + s + "constants" + s + "map_groups.h", map_header_text);
-}
-
-string generate_layout_headers_text(Json layouts_data) {
- ostringstream text;
-
- for (auto &layout : layouts_data["layouts"].array_items()) {
- string border_label = layout["name"].string_value() + "_Border";
- string blockdata_label = layout["name"].string_value() + "_Blockdata";
- text << border_label << "::\n"
- << "\t.incbin \"" << layout["border_filepath"].string_value() << "\"\n\n"
- << blockdata_label << "::\n"
- << "\t.incbin \"" << layout["blockdata_filepath"].string_value() << "\"\n\n"
- << "\t.align 2\n"
- << layout["name"].string_value() << "::\n"
- << "\t.4byte " << layout["width"].int_value() << "\n"
- << "\t.4byte " << layout["height"].int_value() << "\n"
- << "\t.4byte " << border_label << "\n"
- << "\t.4byte " << blockdata_label << "\n"
- << "\t.4byte " << layout["primary_tileset"].string_value() << "\n"
- << "\t.4byte " << layout["secondary_tileset"].string_value() << "\n\n";
- }
-
- return text.str();
-}
-
-string generate_layouts_table_text(Json layouts_data) {
- ostringstream text;
-
- text << "\t.align 2\n"
- << layouts_data["layouts_table_label"].string_value() << "::\n";
-
- for (auto &layout : layouts_data["layouts"].array_items())
- text << "\t.4byte " << layout["name"].string_value() << "\n";
-
- return text.str();
-}
-
-string generate_layouts_constants_text(Json layouts_data) {
- ostringstream text;
-
- text << "#ifndef GUARD_CONSTANTS_LAYOUTS_H\n"
- << "#define GUARD_CONSTANTS_LAYOUTS_H\n\n";
-
- int i = 0;
- for (auto &layout : layouts_data["layouts"].array_items())
- text << "#define " << layout["id"].string_value() << " " << ++i << "\n";
-
- text << "\n#endif // GUARD_CONSTANTS_LAYOUTS_H\n";
-
- return text.str();
-}
-
-void process_layouts(string layouts_filepath) {
- string err;
- Json layouts_data = Json::parse(read_text_file(layouts_filepath), err);
-
- if (layouts_data == Json())
- FATAL_ERROR("%s\n", err.c_str());
-
- string layout_headers_text = generate_layout_headers_text(layouts_data);
- string layouts_table_text = generate_layouts_table_text(layouts_data);
- string layouts_constants_text = generate_layouts_constants_text(layouts_data);
-
- string file_dir = get_directory_name(layouts_filepath);
- char s = file_dir.back();
-
- write_text_file(file_dir + "layouts.inc", layout_headers_text);
- write_text_file(file_dir + "layouts_table.inc", layouts_table_text);
- write_text_file(file_dir + ".." + s + ".." + s + "include" + s + "constants" + s + "layouts.h", layouts_constants_text);
-}
-
-int main(int argc, char *argv[]) {
- if (argc < 3)
- FATAL_ERROR("USAGE: dungeonjson <mode> <game-version> [options]\n");
-
- char *version_arg = argv[2];
- string version(version_arg);
- if (version != "emerald" && version != "ruby" && version != "pmd-red")
- FATAL_ERROR("ERROR: <game-version> must be 'emerald' or 'ruby' or 'pmd-red'.\n");
-
- char *mode_arg = argv[1];
- string mode(mode_arg);
- if (mode != "layouts" && mode != "map" && mode != "groups" && mode != "dungeon" && mode != "floor")
- FATAL_ERROR("ERROR: <mode> must be 'layouts', 'map', 'groups', 'dungeon' or 'floor' '.\n");
-
- if (mode == "map") {
- if (argc != 5)
- FATAL_ERROR("USAGE: dungeonjson map <game-version> <map_file> <layouts_file>\n");
-
- string filepath(argv[3]);
- string layouts_filepath(argv[4]);
-
- process_map(filepath, layouts_filepath, version);
- }
- else if (mode == "groups") {
- if (argc != 4)
- FATAL_ERROR("USAGE: dungeonjson groups <game-version> <groups_file>\n");
-
- string filepath(argv[3]);
-
- process_groups(filepath);
- }
- else if (mode == "layouts") {
- if (argc != 4)
- FATAL_ERROR("USAGE: dungeonjson layouts <game-version> <layouts_file>\n");
-
- string filepath(argv[3]);
-
- process_layouts(filepath);
- }
- else if (mode == "dungeon") {
- if (argc != 4)
- FATAL_ERROR("USAGE: dungeonjson dungeon <game-version> <dungeon_file>\n");
-
- string filepath(argv[3]);
-
- process_dungeon(filepath);
- }
- else if (mode == "floor") {
- if (argc != 4)
- FATAL_ERROR("USAGE: dungeonjson dungeon <game-version> <floor_file>\n");
-
- string filepath(argv[3]);
-
- process_floorID(filepath);
- }
-
-
-
- return 0;
-}
+// dungeonjson.cpp
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+#include <vector>
+using std::vector;
+
+#include <algorithm>
+using std::sort; using std::find;
+
+#include <map>
+using std::map;
+
+#include <fstream>
+using std::ofstream; using std::ifstream;
+
+#include <sstream>
+using std::ostringstream;
+
+#include <limits>
+using std::numeric_limits;
+
+#include "json11.h"
+using json11::Json;
+
+#include "dungeonjson.h"
+
+
+string read_text_file(string filepath) {
+ ifstream in_file(filepath);
+
+ if (!in_file.is_open())
+ FATAL_ERROR("Cannot open file %s for reading.\n", filepath.c_str());
+
+ string text;
+
+ in_file.seekg(0, std::ios::end);
+ text.resize(in_file.tellg());
+
+ in_file.seekg(0, std::ios::beg);
+ in_file.read(&text[0], text.size());
+
+ in_file.close();
+
+ return text;
+}
+
+void write_text_file(string filepath, string text) {
+ ofstream out_file(filepath, std::ofstream::binary);
+
+ if (!out_file.is_open())
+ FATAL_ERROR("Cannot open file %s for writing.\n", filepath.c_str());
+
+ out_file << text;
+
+ out_file.close();
+}
+
+string generate_map_header_text(Json map_data, Json layouts_data, string version) {
+ string map_layout_id = map_data["layout"].string_value();
+
+ vector<Json> matched;
+
+ for (auto &field : layouts_data["layouts"].array_items()) {
+ if (map_layout_id == field["id"].string_value())
+ matched.push_back(field);
+ }
+
+ if (matched.size() != 1)
+ FATAL_ERROR("Failed to find matching layout for %s.\n", map_layout_id.c_str());
+
+ Json layout = matched[0];
+
+ ostringstream text;
+
+ text << map_data["name"].string_value() << ":\n"
+ << "\t.4byte " << layout["name"].string_value() << "\n";
+
+ if (map_data.object_items().find("shared_events_map") != map_data.object_items().end())
+ text << "\t.4byte " << map_data["shared_events_map"].string_value() << "_MapEvents\n";
+ else
+ text << "\t.4byte " << map_data["name"].string_value() << "_MapEvents\n";
+
+ if (map_data.object_items().find("shared_scripts_map") != map_data.object_items().end())
+ text << "\t.4byte " << map_data["shared_scripts_map"].string_value() << "_MapScripts\n";
+ else
+ text << "\t.4byte " << map_data["name"].string_value() << "_MapScripts\n";
+
+ if (map_data.object_items().find("connections") != map_data.object_items().end()
+ && map_data["connections"].array_items().size() > 0)
+ text << "\t.4byte " << map_data["name"].string_value() << "_MapConnections\n";
+ else
+ text << "\t.4byte 0x0\n";
+
+ text << "\t.2byte " << map_data["music"].string_value() << "\n"
+ << "\t.2byte " << layout["id"].string_value() << "\n"
+ << "\t.byte " << map_data["region_map_section"].string_value() << "\n"
+ << "\t.byte " << map_data["requires_flash"].bool_value() << "\n"
+ << "\t.byte " << map_data["weather"].string_value() << "\n"
+ << "\t.byte " << map_data["map_type"].string_value() << "\n"
+ << "\t.2byte 0\n";
+
+ if (version == "ruby")
+ text << "\t.byte " << map_data["show_map_name"].bool_value() << "\n";
+ else if (version == "emerald")
+ text << "\tmap_header_flags "
+ << "allow_cycling=" << map_data["allow_cycling"].bool_value() << ", "
+ << "allow_escaping=" << map_data["allow_escaping"].bool_value() << ", "
+ << "allow_running=" << map_data["allow_running"].bool_value() << ", "
+ << "show_map_name=" << map_data["show_map_name"].bool_value() << "\n";
+
+ text << "\t.byte " << map_data["battle_scene"].string_value() << "\n\n";
+
+ return text.str();
+}
+
+string generate_map_connections_text(Json map_data) {
+ if (map_data["connections"] == Json())
+ return string("\n");
+
+ ostringstream text;
+
+ text << map_data["name"].string_value() << "_MapConnectionsList:\n";
+
+ for (auto &connection : map_data["connections"].array_items()) {
+ text << "\tconnection "
+ << connection["direction"].string_value() << ", "
+ << connection["offset"].int_value() << ", "
+ << connection["map"].string_value() << "\n";
+ }
+
+ text << "\n" << map_data["name"].string_value() << "_MapConnections:\n"
+ << "\t.4byte " << map_data["connections"].array_items().size() << "\n"
+ << "\t.4byte " << map_data["name"].string_value() << "_MapConnectionsList\n\n";
+
+ return text.str();
+}
+
+string generate_map_events_text(Json map_data) {
+ if (map_data.object_items().find("shared_events_map") != map_data.object_items().end())
+ return string("\n");
+
+ ostringstream text;
+
+ string objects_label, warps_label, coords_label, bgs_label;
+
+ if (map_data["object_events"].array_items().size() > 0) {
+ objects_label = map_data["name"].string_value() + "_ObjectEvents";
+ 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];
+ text << "\tobject_event " << i + 1 << ", "
+ << obj_event["graphics_id"].string_value() << ", 0, "
+ << obj_event["x"].int_value() << ", "
+ << obj_event["y"].int_value() << ", "
+ << obj_event["elevation"].int_value() << ", "
+ << obj_event["movement_type"].string_value() << ", "
+ << obj_event["movement_range_x"].int_value() << ", "
+ << obj_event["movement_range_y"].int_value() << ", "
+ << obj_event["trainer_type"].string_value() << ", "
+ << obj_event["trainer_sight_or_berry_tree_id"].string_value() << ", "
+ << obj_event["script"].string_value() << ", "
+ << obj_event["flag"].string_value() << "\n";
+ }
+ text << "\n";
+ } else {
+ objects_label = "0x0";
+ }
+
+ if (map_data["warp_events"].array_items().size() > 0) {
+ warps_label = map_data["name"].string_value() + "_MapWarps";
+ text << warps_label << ":\n";
+ for (auto &warp_event : map_data["warp_events"].array_items()) {
+ text << "\twarp_def "
+ << warp_event["x"].int_value() << ", "
+ << warp_event["y"].int_value() << ", "
+ << warp_event["elevation"].int_value() << ", "
+ << warp_event["dest_warp_id"].int_value() << ", "
+ << warp_event["dest_map"].string_value() << "\n";
+ }
+ text << "\n";
+ } else {
+ warps_label = "0x0";
+ }
+
+ if (map_data["coord_events"].array_items().size() > 0) {
+ coords_label = map_data["name"].string_value() + "_MapCoordEvents";
+ text << coords_label << ":\n";
+ for (auto &coord_event : map_data["coord_events"].array_items()) {
+ if (coord_event["type"].string_value() == "trigger") {
+ text << "\tcoord_event "
+ << coord_event["x"].int_value() << ", "
+ << coord_event["y"].int_value() << ", "
+ << coord_event["elevation"].int_value() << ", "
+ << coord_event["var"].string_value() << ", "
+ << coord_event["var_value"].string_value() << ", "
+ << coord_event["script"].string_value() << "\n";
+ }
+ else if (coord_event["type"] == "weather") {
+ text << "\tcoord_weather_event "
+ << coord_event["x"].int_value() << ", "
+ << coord_event["y"].int_value() << ", "
+ << coord_event["elevation"].int_value() << ", "
+ << coord_event["weather"].string_value() << "\n";
+ }
+ }
+ text << "\n";
+ } else {
+ coords_label = "0x0";
+ }
+
+ if (map_data["bg_events"].array_items().size() > 0) {
+ bgs_label = map_data["name"].string_value() + "_MapBGEvents";
+ text << bgs_label << ":\n";
+ for (auto &bg_event : map_data["bg_events"].array_items()) {
+ if (bg_event["type"] == "sign") {
+ text << "\tbg_event "
+ << bg_event["x"].int_value() << ", "
+ << bg_event["y"].int_value() << ", "
+ << bg_event["elevation"].int_value() << ", "
+ << bg_event["player_facing_dir"].string_value() << ", "
+ << bg_event["script"].string_value() << "\n";
+ }
+ else if (bg_event["type"] == "hidden_item") {
+ text << "\tbg_hidden_item_event "
+ << bg_event["x"].int_value() << ", "
+ << bg_event["y"].int_value() << ", "
+ << bg_event["elevation"].int_value() << ", "
+ << bg_event["item"].string_value() << ", "
+ << bg_event["flag"].string_value() << "\n";
+ }
+ else if (bg_event["type"] == "secret_base") {
+ text << "\tbg_secret_base_event "
+ << bg_event["x"].int_value() << ", "
+ << bg_event["y"].int_value() << ", "
+ << bg_event["elevation"].int_value() << ", "
+ << bg_event["secret_base_id"].string_value() << "\n";
+ }
+ }
+ text << "\n";
+ } else {
+ bgs_label = "0x0";
+ }
+
+ text << map_data["name"].string_value() << "_MapEvents::\n"
+ << "\tmap_events " << objects_label << ", " << warps_label << ", "
+ << coords_label << ", " << bgs_label << "\n\n";
+
+ return text.str();
+}
+
+string generate_pokemon_table_text(Json map_data) {
+
+ ostringstream text;
+ int pokemon_mask;
+ int probability_total;
+
+ text << "@ This is auto-generated by " << map_data["name"].string_value() << ".json" << "\n";
+ for (auto &table: map_data["tables"].array_items()) {
+ probability_total = 0;
+
+ text << ".global " << table["name"].string_value() << "\n";
+ text << table["name"].string_value() << ":\n";
+
+ for (auto &pokemon: table["pokemon"].array_items()) {
+ pokemon_mask = (pokemon["level"].int_value() * 512);
+ probability_total += pokemon["probability"].int_value();
+ text << ".2byte " << pokemon["species"].string_value() << " + " << pokemon_mask;
+ // We have to repeat to maintain matching
+ if(pokemon["probability"].int_value() == 0)
+ {
+ text << ", " << "0x00, 0x00";
+ }
+ else
+ {
+ text << ", " << probability_total << ", " << probability_total;
+ }
+ text << ", " << "0x00" << "\n";
+ }
+
+ // Append null entry to end
+ text << "@ END OF TABLE" << "\n";
+ text << ".2byte " << "0x00, 0x00, 0x00, 0x00" << "\n";
+ }
+
+ return text.str();
+}
+
+string generate_floorID_table_text(Json map_data) {
+
+ ostringstream text;
+
+ text << "@ This is auto-generated by " << map_data["name"].string_value() << ".json" << "\n";
+
+ text << ".global " << map_data["name"].string_value() << "\n";
+ text << map_data["name"].string_value() << ":\n";
+ // Append null entry to Start
+ text << ".2byte " << "0, 0, 0, 0, 0, 0, 0, 0" << "\n";
+ for (auto &table: map_data["tables"].array_items()) {
+
+ text << ".2byte " << table["MainData"].int_value()
+ << ", " << table["Pokemon"].int_value()
+ << ", " << table["Traps"].int_value()
+ << ", " << table["Items"].int_value()
+ << ", " << table["KecleonShop"].int_value()
+ << ", " << table["MonsterRoomItems"].int_value()
+ << ", " << table["BuriedItems"].int_value()
+ << ", " << "0" << "\n";
+
+ }
+ text << "@ END OF TABLE" << "\n";
+
+ return text.str();
+}
+
+string get_directory_name(string filename) {
+ size_t dir_pos = filename.find_last_of("/\\");
+
+ return filename.substr(0, dir_pos + 1);
+}
+
+void process_map(string map_filepath, string layouts_filepath, string version) {
+ string mapdata_err, layouts_err;
+
+ string mapdata_json_text = read_text_file(map_filepath);
+ string layouts_json_text = read_text_file(layouts_filepath);
+
+ Json map_data = Json::parse(mapdata_json_text, mapdata_err);
+ if (map_data == Json())
+ FATAL_ERROR("%s\n", mapdata_err.c_str());
+
+ Json layouts_data = Json::parse(layouts_json_text, layouts_err);
+ 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 events_text = generate_map_events_text(map_data);
+ string connections_text = generate_map_connections_text(map_data);
+
+ string files_dir = get_directory_name(map_filepath);
+ write_text_file(files_dir + "header.inc", header_text);
+ write_text_file(files_dir + "events.inc", events_text);
+ write_text_file(files_dir + "connections.inc", connections_text);
+}
+
+void process_dungeon(string map_filepath) {
+ string mapdata_err;
+
+ string mapdata_json_text = read_text_file(map_filepath);
+
+ Json map_data = Json::parse(mapdata_json_text, mapdata_err);
+ if (map_data == Json())
+ FATAL_ERROR("%s\n", mapdata_err.c_str());
+
+ string pokemon_text = generate_pokemon_table_text(map_data);
+
+ string files_dir = get_directory_name(map_filepath);
+ write_text_file(files_dir + "pokemon_found.inc", pokemon_text);
+}
+
+void process_floorID(string map_filepath) {
+ string mapdata_err;
+
+ string mapdata_json_text = read_text_file(map_filepath);
+
+ Json map_data = Json::parse(mapdata_json_text, mapdata_err);
+ if (map_data == Json())
+ FATAL_ERROR("%s\n", mapdata_err.c_str());
+
+ string pokemon_text = generate_floorID_table_text(map_data);
+
+ string files_dir = get_directory_name(map_filepath);
+ write_text_file(files_dir + "floor_id.inc", pokemon_text);
+}
+
+string generate_groups_text(Json groups_data) {
+ ostringstream text;
+
+ for (auto &key : groups_data["group_order"].array_items()) {
+ string group = key.string_value();
+ text << group << "::\n";
+ auto maps = groups_data[group].array_items();
+ for (Json &map_name : maps)
+ text << "\t.4byte " << map_name.string_value() << "\n";
+ text << "\n";
+ }
+
+ text << "\t.align 2\n" << "gMapGroups::\n";
+ for (auto &group : groups_data["group_order"].array_items())
+ text << "\t.4byte " << group.string_value() << "\n";
+ text << "\n";
+
+ return text.str();
+}
+
+string generate_connections_text(Json groups_data) {
+ vector<Json> map_names;
+
+ for (auto &group : groups_data["group_order"].array_items())
+ for (auto map_name : groups_data[group.string_value()].array_items())
+ map_names.push_back(map_name);
+
+ vector<Json> connections_include_order = groups_data["connections_include_order"].array_items();
+
+ if (connections_include_order.size() > 0)
+ sort(map_names.begin(), map_names.end(), [connections_include_order](const Json &a, const Json &b) {
+ auto iter_a = find(connections_include_order.begin(), connections_include_order.end(), a);
+ if (iter_a == connections_include_order.end())
+ iter_a = connections_include_order.begin() + numeric_limits<int>::max();
+ auto iter_b = find(connections_include_order.begin(), connections_include_order.end(), b);
+ if (iter_b == connections_include_order.end())
+ iter_b = connections_include_order.begin() + numeric_limits<int>::max();
+ return iter_a < iter_b;
+ });
+
+ ostringstream text;
+
+ for (Json map_name : map_names)
+ text << "\t.include \"data/maps/" << map_name.string_value() << "/connections.inc\"\n";
+
+ return text.str();
+}
+
+string generate_headers_text(Json groups_data) {
+ vector<string> map_names;
+
+ for (auto &group : groups_data["group_order"].array_items())
+ for (auto map_name : groups_data[group.string_value()].array_items())
+ map_names.push_back(map_name.string_value());
+
+ ostringstream text;
+
+ for (string map_name : map_names)
+ text << "\t.include \"data/maps/" << map_name << "/header.inc\"\n";
+
+ return text.str();
+}
+
+string generate_events_text(Json groups_data) {
+ vector<string> map_names;
+
+ for (auto &group : groups_data["group_order"].array_items())
+ for (auto map_name : groups_data[group.string_value()].array_items())
+ map_names.push_back(map_name.string_value());
+
+ ostringstream text;
+
+ for (string map_name : map_names)
+ text << "\t.include \"data/maps/" << map_name << "/events.inc\"\n";
+
+ return text.str();
+}
+
+string generate_map_constants_text(string groups_filepath, Json groups_data) {
+ string file_dir = get_directory_name(groups_filepath);
+ char dir_separator = file_dir.back();
+
+ ostringstream text;
+
+ text << "#ifndef GUARD_CONSTANTS_MAP_GROUPS_H\n"
+ << "#define GUARD_CONSTANTS_MAP_GROUPS_H\n\n";
+
+ int group_num = 0;
+
+ for (auto &group : groups_data["group_order"].array_items()) {
+ text << "// Map Group " << group_num << "\n";
+ vector<Json> map_ids;
+ size_t max_length = 0;
+
+ for (auto &map_name : groups_data[group.string_value()].array_items()) {
+ string header_filepath = file_dir + map_name.string_value() + dir_separator + "map.json";
+ string err_str;
+ Json map_data = Json::parse(read_text_file(header_filepath), err_str);
+ map_ids.push_back(map_data["id"]);
+ if (map_data["id"].string_value().length() > max_length)
+ max_length = map_data["id"].string_value().length();
+ }
+
+ int map_id_num = 0;
+ for (Json map_id : map_ids) {
+ text << "#define " << map_id.string_value() << string((max_length - map_id.string_value().length() + 1), ' ')
+ << "(" << map_id_num++ << " | (" << group_num << " << 8))\n";
+ }
+ text << "\n";
+
+ group_num++;
+ }
+
+ text << "#define MAP_GROUPS_COUNT " << group_num << "\n\n";
+ text << "#endif // GUARD_CONSTANTS_MAP_GROUPS_H\n";
+
+ return text.str();
+}
+
+void process_groups(string groups_filepath) {
+ string err;
+ Json groups_data = Json::parse(read_text_file(groups_filepath), err);
+
+ if (groups_data == Json())
+ FATAL_ERROR("%s\n", err.c_str());
+
+ string groups_text = generate_groups_text(groups_data);
+ string connections_text = generate_connections_text(groups_data);
+ string headers_text = generate_headers_text(groups_data);
+ string events_text = generate_events_text(groups_data);
+ string map_header_text = generate_map_constants_text(groups_filepath, groups_data);
+
+ string file_dir = get_directory_name(groups_filepath);
+ char s = file_dir.back();
+
+ write_text_file(file_dir + "groups.inc", groups_text);
+ write_text_file(file_dir + "connections.inc", connections_text);
+ write_text_file(file_dir + "headers.inc", headers_text);
+ write_text_file(file_dir + "events.inc", events_text);
+ write_text_file(file_dir + ".." + s + ".." + s + "include" + s + "constants" + s + "map_groups.h", map_header_text);
+}
+
+string generate_layout_headers_text(Json layouts_data) {
+ ostringstream text;
+
+ for (auto &layout : layouts_data["layouts"].array_items()) {
+ string border_label = layout["name"].string_value() + "_Border";
+ string blockdata_label = layout["name"].string_value() + "_Blockdata";
+ text << border_label << "::\n"
+ << "\t.incbin \"" << layout["border_filepath"].string_value() << "\"\n\n"
+ << blockdata_label << "::\n"
+ << "\t.incbin \"" << layout["blockdata_filepath"].string_value() << "\"\n\n"
+ << "\t.align 2\n"
+ << layout["name"].string_value() << "::\n"
+ << "\t.4byte " << layout["width"].int_value() << "\n"
+ << "\t.4byte " << layout["height"].int_value() << "\n"
+ << "\t.4byte " << border_label << "\n"
+ << "\t.4byte " << blockdata_label << "\n"
+ << "\t.4byte " << layout["primary_tileset"].string_value() << "\n"
+ << "\t.4byte " << layout["secondary_tileset"].string_value() << "\n\n";
+ }
+
+ return text.str();
+}
+
+string generate_layouts_table_text(Json layouts_data) {
+ ostringstream text;
+
+ text << "\t.align 2\n"
+ << layouts_data["layouts_table_label"].string_value() << "::\n";
+
+ for (auto &layout : layouts_data["layouts"].array_items())
+ text << "\t.4byte " << layout["name"].string_value() << "\n";
+
+ return text.str();
+}
+
+string generate_layouts_constants_text(Json layouts_data) {
+ ostringstream text;
+
+ text << "#ifndef GUARD_CONSTANTS_LAYOUTS_H\n"
+ << "#define GUARD_CONSTANTS_LAYOUTS_H\n\n";
+
+ int i = 0;
+ for (auto &layout : layouts_data["layouts"].array_items())
+ text << "#define " << layout["id"].string_value() << " " << ++i << "\n";
+
+ text << "\n#endif // GUARD_CONSTANTS_LAYOUTS_H\n";
+
+ return text.str();
+}
+
+void process_layouts(string layouts_filepath) {
+ string err;
+ Json layouts_data = Json::parse(read_text_file(layouts_filepath), err);
+
+ if (layouts_data == Json())
+ FATAL_ERROR("%s\n", err.c_str());
+
+ string layout_headers_text = generate_layout_headers_text(layouts_data);
+ string layouts_table_text = generate_layouts_table_text(layouts_data);
+ string layouts_constants_text = generate_layouts_constants_text(layouts_data);
+
+ string file_dir = get_directory_name(layouts_filepath);
+ char s = file_dir.back();
+
+ write_text_file(file_dir + "layouts.inc", layout_headers_text);
+ write_text_file(file_dir + "layouts_table.inc", layouts_table_text);
+ write_text_file(file_dir + ".." + s + ".." + s + "include" + s + "constants" + s + "layouts.h", layouts_constants_text);
+}
+
+int main(int argc, char *argv[]) {
+ if (argc < 3)
+ FATAL_ERROR("USAGE: dungeonjson <mode> <game-version> [options]\n");
+
+ char *version_arg = argv[2];
+ string version(version_arg);
+ if (version != "emerald" && version != "ruby" && version != "pmd-red")
+ FATAL_ERROR("ERROR: <game-version> must be 'emerald' or 'ruby' or 'pmd-red'.\n");
+
+ char *mode_arg = argv[1];
+ string mode(mode_arg);
+ if (mode != "layouts" && mode != "map" && mode != "groups" && mode != "dungeon" && mode != "floor")
+ FATAL_ERROR("ERROR: <mode> must be 'layouts', 'map', 'groups', 'dungeon' or 'floor' '.\n");
+
+ if (mode == "map") {
+ if (argc != 5)
+ FATAL_ERROR("USAGE: dungeonjson map <game-version> <map_file> <layouts_file>\n");
+
+ string filepath(argv[3]);
+ string layouts_filepath(argv[4]);
+
+ process_map(filepath, layouts_filepath, version);
+ }
+ else if (mode == "groups") {
+ if (argc != 4)
+ FATAL_ERROR("USAGE: dungeonjson groups <game-version> <groups_file>\n");
+
+ string filepath(argv[3]);
+
+ process_groups(filepath);
+ }
+ else if (mode == "layouts") {
+ if (argc != 4)
+ FATAL_ERROR("USAGE: dungeonjson layouts <game-version> <layouts_file>\n");
+
+ string filepath(argv[3]);
+
+ process_layouts(filepath);
+ }
+ else if (mode == "dungeon") {
+ if (argc != 4)
+ FATAL_ERROR("USAGE: dungeonjson dungeon <game-version> <dungeon_file>\n");
+
+ string filepath(argv[3]);
+
+ process_dungeon(filepath);
+ }
+ else if (mode == "floor") {
+ if (argc != 4)
+ FATAL_ERROR("USAGE: dungeonjson dungeon <game-version> <floor_file>\n");
+
+ string filepath(argv[3]);
+
+ process_floorID(filepath);
+ }
+
+
+
+ return 0;
+}