From 270bef0973516322f353b3e40c714005151e407e Mon Sep 17 00:00:00 2001 From: scawful Date: Thu, 22 Aug 2024 10:49:12 -0400 Subject: [PATCH] Refactor OverworldEntity to use uint16_t for map_id and improve code organization --- src/app/editor/overworld/entity.cc | 8 +-- src/app/editor/overworld/overworld_editor.cc | 10 ++-- src/app/zelda3/common.h | 4 +- src/app/zelda3/overworld/overworld.cc | 16 ++--- src/app/zelda3/overworld/overworld.h | 62 +++++++++----------- src/app/zelda3/sprite/sprite.cc | 2 +- src/app/zelda3/sprite/sprite.h | 2 +- 7 files changed, 50 insertions(+), 54 deletions(-) diff --git a/src/app/editor/overworld/entity.cc b/src/app/editor/overworld/entity.cc index ca3098c2..09990c44 100644 --- a/src/app/editor/overworld/entity.cc +++ b/src/app/editor/overworld/entity.cc @@ -132,7 +132,7 @@ bool DrawOverworldEntrancePopup( } if (ImGui::BeginPopupModal("Entrance editor", NULL, ImGuiWindowFlags_AlwaysAutoResize)) { - gui::InputHex("Map ID", &entrance.map_id_); + gui::InputHexWord("Map ID", &entrance.map_id_); gui::InputHexByte("Entrance ID", &entrance.entrance_id_, kInputFieldSize + 20); gui::InputHex("X", &entrance.x_); @@ -209,7 +209,7 @@ bool DrawExitEditorPopup(zelda3::overworld::OverworldExit &exit) { gui::InputHexWord("Room", &exit.room_id_); SameLine(); gui::InputHex("Entity ID", &exit.entity_id_, 4); - gui::InputHex("Map", &exit.map_id_); + gui::InputHexWord("Map", &exit.map_id_); SameLine(); Checkbox("Automatic", &exit.is_automatic_); @@ -350,8 +350,8 @@ bool DrawItemEditorPopup(zelda3::overworld::OverworldItem &item) { ImGui::BeginGroup(); for (int i = 0; i < zelda3::overworld::kSecretItemNames.size(); i++) { if (Selectable(zelda3::overworld::kSecretItemNames[i].c_str(), - item.id == i)) { - item.id = i; + item.id_ == i)) { + item.id_ = i; } } ImGui::EndGroup(); diff --git a/src/app/editor/overworld/overworld_editor.cc b/src/app/editor/overworld/overworld_editor.cc index 5325d866..7374f11c 100644 --- a/src/app/editor/overworld/overworld_editor.cc +++ b/src/app/editor/overworld/overworld_editor.cc @@ -843,8 +843,8 @@ void OverworldEditor::DrawOverworldItems() { int i = 0; for (auto &item : *overworld_.mutable_all_items()) { // Get the item's bitmap and real X and Y positions - if (item.room_map_id < 0x40 + (current_world_ * 0x40) && - item.room_map_id >= (current_world_ * 0x40) && !item.deleted) { + if (item.room_map_id_ < 0x40 + (current_world_ * 0x40) && + item.room_map_id_ >= (current_world_ * 0x40) && !item.deleted) { ow_map_canvas_.DrawRect(item.x_, item.y_, 16, 16, ImVec4(255, 0, 0, 150)); if (current_mode == EditingMode::ITEMS) { @@ -862,10 +862,10 @@ void OverworldEditor::DrawOverworldItems() { } } std::string item_name = ""; - if (item.id < zelda3::overworld::kSecretItemNames.size()) { - item_name = zelda3::overworld::kSecretItemNames[item.id]; + if (item.id_ < zelda3::overworld::kSecretItemNames.size()) { + item_name = zelda3::overworld::kSecretItemNames[item.id_]; } else { - item_name = absl::StrFormat("0x%02X", item.id); + item_name = absl::StrFormat("0x%02X", item.id_); } ow_map_canvas_.DrawText(item_name, item.x_, item.y_); } diff --git a/src/app/zelda3/common.h b/src/app/zelda3/common.h index ac44bf62..6f1bd766 100644 --- a/src/app/zelda3/common.h +++ b/src/app/zelda3/common.h @@ -45,14 +45,14 @@ class OverworldEntity { int game_x_; int game_y_; int entity_id_; - int map_id_; + uint16_t map_id_; auto set_x(int x) { x_ = x; } auto set_y(int y) { y_ = y; } OverworldEntity() = default; - virtual void UpdateMapProperties(short map_id) = 0; + virtual void UpdateMapProperties(uint16_t map_id) = 0; }; static const std::string TileTypeNames[] = { diff --git a/src/app/zelda3/overworld/overworld.cc b/src/app/zelda3/overworld/overworld.cc index caf7bc69..4388790b 100644 --- a/src/app/zelda3/overworld/overworld.cc +++ b/src/app/zelda3/overworld/overworld.cc @@ -468,8 +468,8 @@ absl::Status Overworld::LoadItems() { (y * 16) + (sy * 512), false); auto size = all_items_.size(); - all_items_[size - 1].game_x = (uint8_t)x; - all_items_[size - 1].game_y = (uint8_t)y; + all_items_[size - 1].game_x_ = (uint8_t)x; + all_items_[size - 1].game_y_ = (uint8_t)y; addr += 3; } } @@ -1335,7 +1335,7 @@ bool compareItemsArrays(std::vector itemArray1, // Check all sprite in 2nd array if one match if (itemArray1[i].x_ == itemArray2[j].x_ && itemArray1[i].y_ == itemArray2[j].y_ && - itemArray1[i].id == itemArray2[j].id) { + itemArray1[i].id_ == itemArray2[j].id_) { match = true; break; } @@ -1357,11 +1357,11 @@ absl::Status Overworld::SaveItems() { for (int i = 0; i < 128; i++) { room_items[i] = std::vector(); for (const OverworldItem &item : all_items_) { - if (item.room_map_id == i) { + if (item.room_map_id_ == i) { room_items[i].emplace_back(item); - if (item.id == 0x86) { + if (item.id_ == 0x86) { RETURN_IF_ERROR(rom()->WriteWord( - 0x16DC5 + (i * 2), (item.game_x + (item.game_y * 64)) * 2)); + 0x16DC5 + (i * 2), (item.game_x_ + (item.game_y_ * 64)) * 2)); } } } @@ -1398,11 +1398,11 @@ absl::Status Overworld::SaveItems() { item_pointers[i] = data_pos; for (const OverworldItem &item : room_items[i]) { short map_pos = - static_cast(((item.game_y << 6) + item.game_x) << 1); + static_cast(((item.game_y_ << 6) + item.game_x_) << 1); uint32_t data = static_cast(map_pos & 0xFF) | static_cast(map_pos >> 8) | - static_cast(item.id); + static_cast(item.id_); RETURN_IF_ERROR(rom()->WriteLong(data_pos, data)); data_pos += 3; } diff --git a/src/app/zelda3/overworld/overworld.h b/src/app/zelda3/overworld/overworld.h index 00064db5..7c6ffdd7 100644 --- a/src/app/zelda3/overworld/overworld.h +++ b/src/app/zelda3/overworld/overworld.h @@ -79,21 +79,21 @@ constexpr int overworldItemsEndData = 0xDC89C; // 0DC89E class OverworldItem : public OverworldEntity { public: - bool bg2 = false; - uint8_t game_x; - uint8_t game_y; - uint8_t id; - uint16_t room_map_id; + bool bg2_ = false; + uint8_t id_; + uint8_t game_x_; + uint8_t game_y_; + uint16_t room_map_id_; int unique_id = 0; bool deleted = false; OverworldItem() = default; OverworldItem(uint8_t id, uint16_t room_map_id, int x, int y, bool bg2) { - this->id = id; + this->id_ = id; this->x_ = x; this->y_ = y; - this->bg2 = bg2; - this->room_map_id = room_map_id; + this->bg2_ = bg2; + this->room_map_id_ = room_map_id; this->map_id_ = room_map_id; this->entity_id_ = id; this->type_ = kItem; @@ -101,32 +101,28 @@ class OverworldItem : public OverworldEntity { int map_x = room_map_id - ((room_map_id / 8) * 8); int map_y = room_map_id / 8; - this->game_x = static_cast(std::abs(x - (map_x * 512)) / 16); - this->game_y = static_cast(std::abs(y - (map_y * 512)) / 16); - // this->unique_id = ROM.unique_item_id++; + game_x_ = static_cast(std::abs(x - (map_x * 512)) / 16); + game_y_ = static_cast(std::abs(y - (map_y * 512)) / 16); } - void UpdateMapProperties(int16_t room_map_id) override { - this->room_map_id = static_cast(room_map_id); + void UpdateMapProperties(uint16_t room_map_id) override { + room_map_id_ = room_map_id; - if (room_map_id >= 64) { - room_map_id -= 64; + if (room_map_id_ >= 64) { + room_map_id_ -= 64; } - int map_x = room_map_id - ((room_map_id / 8) * 8); - int map_y = room_map_id / 8; + int map_x = room_map_id_ - ((room_map_id_ / 8) * 8); + int map_y = room_map_id_ / 8; - this->game_x = - static_cast(std::abs(this->x_ - (map_x * 512)) / 16); - this->game_y = - static_cast(std::abs(this->y_ - (map_y * 512)) / 16); + game_x_ = static_cast(std::abs(x_ - (map_x * 512)) / 16); + game_y_ = static_cast(std::abs(y_ - (map_y * 512)) / 16); std::cout << "Item: " << std::hex << std::setw(2) << std::setfill('0') - << static_cast(this->id) << " MapId: " << std::hex - << std::setw(2) << std::setfill('0') - << static_cast(this->room_map_id) - << " X: " << static_cast(this->game_x) - << " Y: " << static_cast(this->game_y) << std::endl; + << static_cast(id_) << " MapId: " << std::hex << std::setw(2) + << std::setfill('0') << static_cast(room_map_id_) + << " X: " << static_cast(game_x_) + << " Y: " << static_cast(game_y_) << std::endl; } }; @@ -191,7 +187,6 @@ class OverworldExit : public OverworldEntity { entrance_id_(0), area_x_(0), area_y_(0), - is_hole_(false), room_id_(room_id), y_scroll_(y_scroll), x_scroll_(x_scroll), @@ -203,12 +198,13 @@ class OverworldExit : public OverworldEntity { scroll_mod_x_(scroll_mod_x), door_type_1_(door_type_1), door_type_2_(door_type_2), + is_hole_(false), deleted_(deleted) { // Initialize entity variables - this->x_ = player_x; - this->y_ = player_y; - this->map_id_ = map_id; - this->type_ = kExit; + x_ = player_x; + y_ = player_y; + map_id_ = map_id; + type_ = kExit; int mapX = (map_id_ - ((map_id_ / 8) * 8)); int mapY = (map_id_ / 8); @@ -242,7 +238,7 @@ class OverworldExit : public OverworldEntity { } // Overworld overworld - void UpdateMapProperties(short map_id) override { + void UpdateMapProperties(uint16_t map_id) override { map_id_ = map_id; int large = 256; @@ -365,7 +361,7 @@ class OverworldEntrance : public OverworldEntity { is_hole_); } - void UpdateMapProperties(short map_id) override { + void UpdateMapProperties(uint16_t map_id) override { map_id_ = map_id; if (map_id_ >= 64) { diff --git a/src/app/zelda3/sprite/sprite.cc b/src/app/zelda3/sprite/sprite.cc index 1929a2b0..358e24c1 100644 --- a/src/app/zelda3/sprite/sprite.cc +++ b/src/app/zelda3/sprite/sprite.cc @@ -6,7 +6,7 @@ namespace yaze { namespace app { namespace zelda3 { -void Sprite::UpdateMapProperties(short map_id) { +void Sprite::UpdateMapProperties(uint16_t map_id) { map_x_ = x_; map_y_ = y_; name_ = kSpriteDefaultNames[id_]; diff --git a/src/app/zelda3/sprite/sprite.h b/src/app/zelda3/sprite/sprite.h index 10cfe2d3..1ef50dfd 100644 --- a/src/app/zelda3/sprite/sprite.h +++ b/src/app/zelda3/sprite/sprite.h @@ -328,7 +328,7 @@ class Sprite : public OverworldEntity { bool mirror_x = false, bool mirror_y = false, int sizex = 2, int sizey = 2); - void UpdateMapProperties(short map_id) override; + void UpdateMapProperties(uint16_t map_id) override; // New methods void UpdateCoordinates(int map_x, int map_y);